Skip to content

Commit 81b8de3

Browse files
author
Cache Hamm
authored
Merge pull request #35 from CacheControl/rule-results
Rule results
2 parents a95417c + 91a390f commit 81b8de3

26 files changed

Lines changed: 804 additions & 350 deletions

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
2.0.0-beta2 / 2017-04-10
2+
==================
3+
* Fix fact path object checking to work with objects that have prototypes (lodash isObjectLike instead of isPlainObject)
4+
5+
2.0.0-beta1 / 2017-04-09
6+
==================
7+
8+
* Add rule results
9+
* Document fact .path ability to parse properties containing dots
10+
* Bump dependencies
11+
* BREAKING CHANGES
12+
* `engine.on('failure', (rule, almanac))` is now `engine.on('failure', (event, almanac, ruleResult))`
13+
* `engine.on(eventType, (eventParams, engine))` is now `engine.on(eventType, (eventParams, almanac, ruleResult))`
14+
115
1.5.1 / 2017-03-19
216
==================
317

dist/almanac.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
2323
var debug = require('debug')('json-rules-engine');
2424
var verbose = require('debug')('json-rules-engine-verbose');
2525
var selectn = require('selectn');
26-
var isPlainObject = require('lodash.isplainobject');
26+
var isObjectLike = require('lodash.isobjectlike');
2727
var warn = require('debug')('json-rules-engine:warn');
2828

2929
/**
3030
* Fact results lookup
3131
* Triggers fact computations and saves the results
3232
* A new almanac is used for every engine run()
3333
*/
34+
3435
var Almanac = function () {
3536
function Almanac(factMap) {
3637
var runtimeFacts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
@@ -166,7 +167,7 @@ var Almanac = function () {
166167

167168
case 15:
168169
if (path) {
169-
if (isPlainObject(factValue) || Array.isArray(factValue)) {
170+
if (isObjectLike(factValue)) {
170171
factValue = selectn(path)(factValue);
171172
debug('condition::evaluate extracting object property ' + path + ', received: ' + factValue);
172173
} else {

dist/condition.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, a
1010

1111
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
1212

13-
var params = require('params');
1413
var debug = require('debug')('json-rules-engine');
15-
var isPlainObject = require('lodash.isplainobject');
14+
var isObjectLike = require('lodash.isobjectlike');
1615

1716
var Condition = function () {
1817
function Condition(properties) {
1918
_classCallCheck(this, Condition);
2019

20+
if (!properties) throw new Error('Condition: constructor options required');
2121
var booleanOperator = Condition.booleanOperator(properties);
2222
Object.assign(this, properties);
2323
if (booleanOperator) {
@@ -32,7 +32,10 @@ var Condition = function () {
3232
return new Condition(c);
3333
});
3434
} else {
35-
properties = params(properties).require(['fact', 'operator', 'value']);
35+
if (!properties.hasOwnProperty('fact')) throw new Error('Condition: constructor "fact" property required');
36+
if (!properties.hasOwnProperty('operator')) throw new Error('Condition: constructor "operator" property required');
37+
if (!properties.hasOwnProperty('value')) throw new Error('Condition: constructor "value" property required');
38+
3639
// a non-boolean condition does not have a priority by default. this allows
3740
// priority to be dictated by the fact definition
3841
if (properties.hasOwnProperty('priority')) {
@@ -94,7 +97,7 @@ var Condition = function () {
9497
case 0:
9598
value = this.value;
9699

97-
if (!(isPlainObject(value) && value.hasOwnProperty('fact'))) {
100+
if (!(isObjectLike(value) && value.hasOwnProperty('fact'))) {
98101
_context.next = 5;
99102
break;
100103
}
@@ -137,7 +140,7 @@ var Condition = function () {
137140
key: 'evaluate',
138141
value: function () {
139142
var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2(almanac, operatorMap) {
140-
var op, rightHandSideValue, leftHandSideValue, evaluationResult;
143+
var op, rightHandSideValue, leftHandSideValue, result;
141144
return regeneratorRuntime.wrap(function _callee2$(_context2) {
142145
while (1) {
143146
switch (_context2.prev = _context2.next) {
@@ -186,10 +189,10 @@ var Condition = function () {
186189

187190
case 14:
188191
leftHandSideValue = _context2.sent;
189-
evaluationResult = op.evaluate(leftHandSideValue, rightHandSideValue);
192+
result = op.evaluate(leftHandSideValue, rightHandSideValue);
190193

191-
debug('condition::evaluate <' + leftHandSideValue + ' ' + this.operator + ' ' + rightHandSideValue + '?> (' + evaluationResult + ')');
192-
return _context2.abrupt('return', evaluationResult);
194+
debug('condition::evaluate <' + leftHandSideValue + ' ' + this.operator + ' ' + rightHandSideValue + '?> (' + result + ')');
195+
return _context2.abrupt('return', { result: result, leftHandSideValue: leftHandSideValue, rightHandSideValue: rightHandSideValue, operator: this.operator });
193196

194197
case 18:
195198
case 'end':

dist/engine.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ exports.FINISHED = exports.RUNNING = exports.READY = undefined;
77

88
var _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; }; }();
99

10-
var _params = require('params');
11-
12-
var _params2 = _interopRequireDefault(_params);
13-
1410
var _fact = require('./fact');
1511

1612
var _fact2 = _interopRequireDefault(_fact);
@@ -94,7 +90,9 @@ var Engine = function (_EventEmitter) {
9490
_createClass(Engine, [{
9591
key: 'addRule',
9692
value: function addRule(properties) {
97-
(0, _params2.default)(properties).require(['conditions', 'event']);
93+
if (!properties) throw new Error('Engine: addRule() requires options');
94+
if (!properties.hasOwnProperty('conditions')) throw new Error('Engine: addRule() argument requires "conditions" property');
95+
if (!properties.hasOwnProperty('event')) throw new Error('Engine: addRule() argument requires "event" property');
9896

9997
var rule = void 0;
10098
if (properties instanceof _rule2.default) {
@@ -224,14 +222,15 @@ var Engine = function (_EventEmitter) {
224222
debug('engine::run status:' + _this2.status + '; skipping remaining rules');
225223
return;
226224
}
227-
return rule.evaluate(almanac).then(function (rulePasses) {
228-
debug('engine::run ruleResult:' + rulePasses);
229-
if (rulePasses) {
230-
_this2.emit('success', rule.event, almanac);
231-
_this2.emit(rule.event.type, rule.event.params, _this2);
225+
return rule.evaluate(almanac).then(function (ruleResult) {
226+
debug('engine::run ruleResult:' + ruleResult.result);
227+
if (ruleResult.result) {
228+
_this2.emit('success', rule.event, almanac, ruleResult);
229+
_this2.emit(rule.event.type, rule.event.params, almanac, ruleResult);
232230
almanac.factValue('success-events', { event: rule.event });
231+
} else {
232+
_this2.emit('failure', rule.event, almanac, ruleResult);
233233
}
234-
if (!rulePasses) _this2.emit('failure', rule, almanac);
235234
});
236235
})));
237236

0 commit comments

Comments
 (0)