UNPKG

11.9 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.FINISHED = exports.RUNNING = exports.READY = undefined;
7
8var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
9
10var _fact = require('./fact');
11
12var _fact2 = _interopRequireDefault(_fact);
13
14var _rule = require('./rule');
15
16var _rule2 = _interopRequireDefault(_rule);
17
18var _operator = require('./operator');
19
20var _operator2 = _interopRequireDefault(_operator);
21
22var _almanac = require('./almanac');
23
24var _almanac2 = _interopRequireDefault(_almanac);
25
26var _events = require('events');
27
28var _engineFacts = require('./engine-facts');
29
30var _engineDefaultOperators = require('./engine-default-operators');
31
32var _engineDefaultOperators2 = _interopRequireDefault(_engineDefaultOperators);
33
34function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
35
36function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
37
38function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
39
40function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
41
42var debug = require('debug')('json-rules-engine');
43
44var READY = exports.READY = 'READY';
45var RUNNING = exports.RUNNING = 'RUNNING';
46var FINISHED = exports.FINISHED = 'FINISHED';
47
48var Engine = function (_EventEmitter) {
49 _inherits(Engine, _EventEmitter);
50
51 /**
52 * Returns a new Engine instance
53 * @param {Rule[]} rules - array of rules to initialize with
54 */
55 function Engine() {
56 var rules = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
57 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
58
59 _classCallCheck(this, Engine);
60
61 var _this = _possibleConstructorReturn(this, (Engine.__proto__ || Object.getPrototypeOf(Engine)).call(this));
62
63 _this.rules = [];
64 _this.allowUndefinedFacts = options.allowUndefinedFacts || false;
65 _this.operators = new Map();
66 _this.facts = new Map();
67 _this.status = READY;
68 rules.map(function (r) {
69 return _this.addRule(r);
70 });
71 _engineDefaultOperators2.default.map(function (o) {
72 return _this.addOperator(o);
73 });
74 return _this;
75 }
76
77 /**
78 * Add a rule definition to the engine
79 * @param {object|Rule} properties - rule definition. can be JSON representation, or instance of Rule
80 * @param {integer} properties.priority (>1) - higher runs sooner.
81 * @param {Object} properties.event - event to fire when rule evaluates as successful
82 * @param {string} properties.event.type - name of event to emit
83 * @param {string} properties.event.params - parameters to pass to the event listener
84 * @param {Object} properties.conditions - conditions to evaluate when processing this rule
85 */
86
87
88 _createClass(Engine, [{
89 key: 'addRule',
90 value: function addRule(properties) {
91 if (!properties) throw new Error('Engine: addRule() requires options');
92 if (!properties.hasOwnProperty('conditions')) throw new Error('Engine: addRule() argument requires "conditions" property');
93 if (!properties.hasOwnProperty('event')) throw new Error('Engine: addRule() argument requires "event" property');
94
95 var rule = void 0;
96 if (properties instanceof _rule2.default) {
97 rule = properties;
98 } else {
99 rule = new _rule2.default(properties);
100 }
101 rule.setEngine(this);
102
103 this.rules.push(rule);
104 this.prioritizedRules = null;
105 return this;
106 }
107
108 /**
109 * Remove a rule from the engine
110 * @param {object|Rule} rule - rule definition. Must be a instance of Rule
111 */
112
113 }, {
114 key: 'removeRule',
115 value: function removeRule(rule) {
116 if (rule instanceof _rule2.default === false) throw new Error('Engine: removeRule() rule must be a instance of Rule');
117
118 var index = this.rules.indexOf(rule);
119 if (index === -1) return false;
120 return Boolean(this.rules.splice(index, 1).length);
121 }
122
123 /**
124 * Add a custom operator definition
125 * @param {string} operatorOrName - operator identifier within the condition; i.e. instead of 'equals', 'greaterThan', etc
126 * @param {function(factValue, jsonValue)} callback - the method to execute when the operator is encountered.
127 */
128
129 }, {
130 key: 'addOperator',
131 value: function addOperator(operatorOrName, cb) {
132 var operator = void 0;
133 if (operatorOrName instanceof _operator2.default) {
134 operator = operatorOrName;
135 } else {
136 operator = new _operator2.default(operatorOrName, cb);
137 }
138 debug('engine::addOperator name:' + operator.name);
139 this.operators.set(operator.name, operator);
140 }
141
142 /**
143 * Remove a custom operator definition
144 * @param {string} operatorOrName - operator identifier within the condition; i.e. instead of 'equals', 'greaterThan', etc
145 * @param {function(factValue, jsonValue)} callback - the method to execute when the operator is encountered.
146 */
147
148 }, {
149 key: 'removeOperator',
150 value: function removeOperator(operatorOrName) {
151 var operatorName = void 0;
152 if (operatorOrName instanceof _operator2.default) {
153 operatorName = operatorOrName.name;
154 } else {
155 operatorName = operatorOrName;
156 }
157
158 return this.operators.delete(operatorName);
159 }
160
161 /**
162 * Add a fact definition to the engine. Facts are called by rules as they are evaluated.
163 * @param {object|Fact} id - fact identifier or instance of Fact
164 * @param {function} definitionFunc - function to be called when computing the fact value for a given rule
165 * @param {Object} options - options to initialize the fact with. used when "id" is not a Fact instance
166 */
167
168 }, {
169 key: 'addFact',
170 value: function addFact(id, valueOrMethod, options) {
171 var factId = id;
172 var fact = void 0;
173 if (id instanceof _fact2.default) {
174 factId = id.id;
175 fact = id;
176 } else {
177 fact = new _fact2.default(id, valueOrMethod, options);
178 }
179 debug('engine::addFact id:' + factId);
180 this.facts.set(factId, fact);
181 return this;
182 }
183
184 /**
185 * Add a fact definition to the engine. Facts are called by rules as they are evaluated.
186 * @param {object|Fact} id - fact identifier or instance of Fact
187 */
188
189 }, {
190 key: 'removeFact',
191 value: function removeFact(factOrId) {
192 var factId = void 0;
193 if (!(factOrId instanceof _fact2.default)) {
194 factId = factOrId;
195 } else {
196 factId = factOrId.id;
197 }
198
199 return this.facts.delete(factId);
200 }
201
202 /**
203 * Iterates over the engine rules, organizing them by highest -> lowest priority
204 * @return {Rule[][]} two dimensional array of Rules.
205 * Each outer array element represents a single priority(integer). Inner array is
206 * all rules with that priority.
207 */
208
209 }, {
210 key: 'prioritizeRules',
211 value: function prioritizeRules() {
212 if (!this.prioritizedRules) {
213 var ruleSets = this.rules.reduce(function (sets, rule) {
214 var priority = rule.priority;
215 if (!sets[priority]) sets[priority] = [];
216 sets[priority].push(rule);
217 return sets;
218 }, {});
219 this.prioritizedRules = Object.keys(ruleSets).sort(function (a, b) {
220 return Number(a) > Number(b) ? -1 : 1; // order highest priority -> lowest
221 }).map(function (priority) {
222 return ruleSets[priority];
223 });
224 }
225 return this.prioritizedRules;
226 }
227
228 /**
229 * Stops the rules engine from running the next priority set of Rules. All remaining rules will be resolved as undefined,
230 * and no further events emitted. Since rules of the same priority are evaluated in parallel(not series), other rules of
231 * the same priority may still emit events, even though the engine is in a "finished" state.
232 * @return {Engine}
233 */
234
235 }, {
236 key: 'stop',
237 value: function stop() {
238 this.status = FINISHED;
239 return this;
240 }
241
242 /**
243 * Returns a fact by fact-id
244 * @param {string} factId - fact identifier
245 * @return {Fact} fact instance, or undefined if no such fact exists
246 */
247
248 }, {
249 key: 'getFact',
250 value: function getFact(factId) {
251 return this.facts.get(factId);
252 }
253
254 /**
255 * Runs an array of rules
256 * @param {Rule[]} array of rules to be evaluated
257 * @return {Promise} resolves when all rules in the array have been evaluated
258 */
259
260 }, {
261 key: 'evaluateRules',
262 value: function evaluateRules(ruleArray, almanac) {
263 var _this2 = this;
264
265 return Promise.all(ruleArray.map(function (rule) {
266 if (_this2.status !== RUNNING) {
267 debug('engine::run status:' + _this2.status + '; skipping remaining rules');
268 return;
269 }
270 return rule.evaluate(almanac).then(function (ruleResult) {
271 debug('engine::run ruleResult:' + ruleResult.result);
272 if (ruleResult.result) {
273 _this2.emit('success', rule.event, almanac, ruleResult);
274 _this2.emit(rule.event.type, rule.event.params, almanac, ruleResult);
275 almanac.factValue('success-events', { event: rule.event });
276 } else {
277 _this2.emit('failure', rule.event, almanac, ruleResult);
278 }
279 });
280 }));
281 }
282
283 /**
284 * Runs the rules engine
285 * @param {Object} runtimeFacts - fact values known at runtime
286 * @param {Object} runOptions - run options
287 * @return {Promise} resolves when the engine has completed running
288 */
289
290 }, {
291 key: 'run',
292 value: function run() {
293 var _this3 = this;
294
295 var runtimeFacts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
296
297 debug('engine::run started');
298 debug('engine::run runtimeFacts:', runtimeFacts);
299 runtimeFacts['success-events'] = new _fact2.default('success-events', (0, _engineFacts.SuccessEventFact)(), { cache: false });
300 this.status = RUNNING;
301 var almanac = new _almanac2.default(this.facts, runtimeFacts);
302 var orderedSets = this.prioritizeRules();
303 var cursor = Promise.resolve();
304 // for each rule set, evaluate in parallel,
305 // before proceeding to the next priority set.
306 return new Promise(function (resolve, reject) {
307 orderedSets.map(function (set) {
308 cursor = cursor.then(function () {
309 return _this3.evaluateRules(set, almanac);
310 }).catch(reject);
311 return cursor;
312 });
313 cursor.then(function () {
314 _this3.status = FINISHED;
315 debug('engine::run completed');
316 resolve(almanac.factValue('success-events'));
317 }).catch(reject);
318 });
319 }
320 }]);
321
322 return Engine;
323}(_events.EventEmitter);
324
325exports.default = Engine;
\No newline at end of file