UNPKG

12 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 this.prioritizedRules = null;
121 return Boolean(this.rules.splice(index, 1).length);
122 }
123
124 /**
125 * Add a custom operator definition
126 * @param {string} operatorOrName - operator identifier within the condition; i.e. instead of 'equals', 'greaterThan', etc
127 * @param {function(factValue, jsonValue)} callback - the method to execute when the operator is encountered.
128 */
129
130 }, {
131 key: 'addOperator',
132 value: function addOperator(operatorOrName, cb) {
133 var operator = void 0;
134 if (operatorOrName instanceof _operator2.default) {
135 operator = operatorOrName;
136 } else {
137 operator = new _operator2.default(operatorOrName, cb);
138 }
139 debug('engine::addOperator name:' + operator.name);
140 this.operators.set(operator.name, operator);
141 }
142
143 /**
144 * Remove a custom operator definition
145 * @param {string} operatorOrName - operator identifier within the condition; i.e. instead of 'equals', 'greaterThan', etc
146 * @param {function(factValue, jsonValue)} callback - the method to execute when the operator is encountered.
147 */
148
149 }, {
150 key: 'removeOperator',
151 value: function removeOperator(operatorOrName) {
152 var operatorName = void 0;
153 if (operatorOrName instanceof _operator2.default) {
154 operatorName = operatorOrName.name;
155 } else {
156 operatorName = operatorOrName;
157 }
158
159 return this.operators.delete(operatorName);
160 }
161
162 /**
163 * Add a fact definition to the engine. Facts are called by rules as they are evaluated.
164 * @param {object|Fact} id - fact identifier or instance of Fact
165 * @param {function} definitionFunc - function to be called when computing the fact value for a given rule
166 * @param {Object} options - options to initialize the fact with. used when "id" is not a Fact instance
167 */
168
169 }, {
170 key: 'addFact',
171 value: function addFact(id, valueOrMethod, options) {
172 var factId = id;
173 var fact = void 0;
174 if (id instanceof _fact2.default) {
175 factId = id.id;
176 fact = id;
177 } else {
178 fact = new _fact2.default(id, valueOrMethod, options);
179 }
180 debug('engine::addFact id:' + factId);
181 this.facts.set(factId, fact);
182 return this;
183 }
184
185 /**
186 * Add a fact definition to the engine. Facts are called by rules as they are evaluated.
187 * @param {object|Fact} id - fact identifier or instance of Fact
188 */
189
190 }, {
191 key: 'removeFact',
192 value: function removeFact(factOrId) {
193 var factId = void 0;
194 if (!(factOrId instanceof _fact2.default)) {
195 factId = factOrId;
196 } else {
197 factId = factOrId.id;
198 }
199
200 return this.facts.delete(factId);
201 }
202
203 /**
204 * Iterates over the engine rules, organizing them by highest -> lowest priority
205 * @return {Rule[][]} two dimensional array of Rules.
206 * Each outer array element represents a single priority(integer). Inner array is
207 * all rules with that priority.
208 */
209
210 }, {
211 key: 'prioritizeRules',
212 value: function prioritizeRules() {
213 if (!this.prioritizedRules) {
214 var ruleSets = this.rules.reduce(function (sets, rule) {
215 var priority = rule.priority;
216 if (!sets[priority]) sets[priority] = [];
217 sets[priority].push(rule);
218 return sets;
219 }, {});
220 this.prioritizedRules = Object.keys(ruleSets).sort(function (a, b) {
221 return Number(a) > Number(b) ? -1 : 1; // order highest priority -> lowest
222 }).map(function (priority) {
223 return ruleSets[priority];
224 });
225 }
226 return this.prioritizedRules;
227 }
228
229 /**
230 * Stops the rules engine from running the next priority set of Rules. All remaining rules will be resolved as undefined,
231 * and no further events emitted. Since rules of the same priority are evaluated in parallel(not series), other rules of
232 * the same priority may still emit events, even though the engine is in a "finished" state.
233 * @return {Engine}
234 */
235
236 }, {
237 key: 'stop',
238 value: function stop() {
239 this.status = FINISHED;
240 return this;
241 }
242
243 /**
244 * Returns a fact by fact-id
245 * @param {string} factId - fact identifier
246 * @return {Fact} fact instance, or undefined if no such fact exists
247 */
248
249 }, {
250 key: 'getFact',
251 value: function getFact(factId) {
252 return this.facts.get(factId);
253 }
254
255 /**
256 * Runs an array of rules
257 * @param {Rule[]} array of rules to be evaluated
258 * @return {Promise} resolves when all rules in the array have been evaluated
259 */
260
261 }, {
262 key: 'evaluateRules',
263 value: function evaluateRules(ruleArray, almanac) {
264 var _this2 = this;
265
266 return Promise.all(ruleArray.map(function (rule) {
267 if (_this2.status !== RUNNING) {
268 debug('engine::run status:' + _this2.status + '; skipping remaining rules');
269 return;
270 }
271 return rule.evaluate(almanac).then(function (ruleResult) {
272 debug('engine::run ruleResult:' + ruleResult.result);
273 if (ruleResult.result) {
274 _this2.emit('success', rule.event, almanac, ruleResult);
275 _this2.emit(rule.event.type, rule.event.params, almanac, ruleResult);
276 almanac.factValue('success-events', { event: rule.event });
277 } else {
278 _this2.emit('failure', rule.event, almanac, ruleResult);
279 }
280 });
281 }));
282 }
283
284 /**
285 * Runs the rules engine
286 * @param {Object} runtimeFacts - fact values known at runtime
287 * @param {Object} runOptions - run options
288 * @return {Promise} resolves when the engine has completed running
289 */
290
291 }, {
292 key: 'run',
293 value: function run() {
294 var _this3 = this;
295
296 var runtimeFacts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
297
298 debug('engine::run started');
299 debug('engine::run runtimeFacts:', runtimeFacts);
300 runtimeFacts['success-events'] = new _fact2.default('success-events', (0, _engineFacts.SuccessEventFact)(), { cache: false });
301 this.status = RUNNING;
302 var almanac = new _almanac2.default(this.facts, runtimeFacts);
303 var orderedSets = this.prioritizeRules();
304 var cursor = Promise.resolve();
305 // for each rule set, evaluate in parallel,
306 // before proceeding to the next priority set.
307 return new Promise(function (resolve, reject) {
308 orderedSets.map(function (set) {
309 cursor = cursor.then(function () {
310 return _this3.evaluateRules(set, almanac);
311 }).catch(reject);
312 return cursor;
313 });
314 cursor.then(function () {
315 _this3.status = FINISHED;
316 debug('engine::run completed');
317 resolve(almanac.factValue('success-events'));
318 }).catch(reject);
319 });
320 }
321 }]);
322
323 return Engine;
324}(_events.EventEmitter);
325
326exports.default = Engine;
\No newline at end of file