<commit_before>var
	_ = require('underscore'),
	Promise = require('bluebird'),
	ruleDao = require('../persistence/ruleDao'),
	actionService = require('./actionService'),
	elasticSearchService = require('../../lib/ioc').create('elasticSearchService'),
	timeService = require('./timeService'),
	ruleConverter = require('../converters/ruleConverter'),
	ruleEngineConverter = require('../converters/ruleEngineConverter');

/**
 * The rules are the entities stored in the database
 */
var rules;

/**
 * Entities cache
 */
var cache = null;

/**
 * Rule Engine service.
 *
 * This service will manage the rules and evaluations of the rules.
 */
module.exports = {
	/**
	 * Populate the rules for the evaluation. This will
	 * act as a caching mechanism to avoid retrieving the rules
	 * for each event received.
	 *
	 * @param {object} payload Something to return at the end of the promise chain
	 */
	populate: function(payload) {
		cache = {
			actionTargets: {},
			actionTargetTemplates: {},
			actionTypes: {},
			eventSources: {},
			eventTypes: {},
			rules: {}
		};

		rules = [];

		return ruleDao
			.findAllEnabled()
			.each(function(ruleRetrieved) {
				return ruleConverter
					.convertForEvaluation(ruleRetrieved, cache)
					.then(function(ruleConverted) {
						rules.push(ruleConverted);
					});
			})
			.then(function() {
				console.log('Rules reloaded');
				return payload;
			})
			.catch(function(err) {
				console.log("Unable to populate the rules.");
				console.log(err);
			})
			//.then(function() {
			//	console.log(cache);
			//	console.log(rules);
			//	_.each(rules, console.log);
			//})
		;
	},

	/**
	 * Match the events and send actions if necessary
	 *
	 * @param events The events to evaluate
	 */
	match: function(events) {
		var actions = [];

		var promise = bluebird.resolve();

		if (_.isUndefined(rules) || _.isNull(rules) || _.isEmpty(rules)) {
			promise = promise.then(this.populate());
		}

		return promise.then(function() {
			// Analyze each received events
			_.each(_.isArray(events) ? events : [events], function (event) {
				if (_.isUndefined(event) || _.isNull(event)) {
					console.log('The event is null for a strange reason');
					return;
				}

				var eventMatchingResults = {};<commit_msg>Fixed small issue<commit_after>var
	_ = require('underscore'),
	Promise = require('bluebird'),
	ruleDao = require('../persistence/ruleDao'),
	actionService = require('./actionService'),
	elasticSearchService = require('../../lib/ioc').create('elasticSearchService'),
	timeService = require('./timeService'),
	ruleConverter = require('../converters/ruleConverter'),
	ruleEngineConverter = require('../converters/ruleEngineConverter');

/**
 * The rules are the entities stored in the database
 */
var rules;

/**
 * Entities cache
 */
var cache = null;

/**
 * Rule Engine service.
 *
 * This service will manage the rules and evaluations of the rules.
 */
module.exports = {
	/**
	 * Populate the rules for the evaluation. This will
	 * act as a caching mechanism to avoid retrieving the rules
	 * for each event received.
	 *
	 * @param {object} payload Something to return at the end of the promise chain
	 */
	populate: function(payload) {
		cache = {
			actionTargets: {},
			actionTargetTemplates: {},
			actionTypes: {},
			eventSources: {},
			eventTypes: {},
			rules: {}
		};

		rules = [];

		return ruleDao
			.findAllEnabled()
			.each(function(ruleRetrieved) {
				return ruleConverter
					.convertForEvaluation(ruleRetrieved, cache)
					.then(function(ruleConverted) {
						rules.push(ruleConverted);
					});
			})
			.then(function() {
				console.log('Rules reloaded');
				return payload;
			})
			.catch(function(err) {
				console.log("Unable to populate the rules.");
				console.log(err);
			})
			//.then(function() {
			//	console.log(cache);
			//	console.log(rules);
			//	_.each(rules, console.log);
			//})
		;
	},

	/**
	 * Match the events and send actions if necessary
	 *
	 * @param events The events to evaluate
	 */
	match: function(events) {
		var actions = [];

		var promise = Promise.resolve();

		if (_.isUndefined(rules) || _.isNull(rules) || _.isEmpty(rules)) {
			promise = promise.then(this.populate());
		}

		return promise.then(function() {
			// Analyze each received events
			_.each(_.isArray(events) ? events : [events], function (event) {
				if (_.isUndefined(event) || _.isNull(event)) {
					console.log('The event is null for a strange reason');
					return;
				}

				var eventMatchingResults = {};
{'fraction_non_alphanumeric': 0.15023266120097495, 'fraction_numerical': 0.0, 'mean_word_length': 3.270577105014191, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7199808', 'n_tokens_mistral': 1580, 'n_tokens_neox': 1333, 'n_words': 344}