json-rules-engine
Version:
Rules Engine expressed in simple json
200 lines (167 loc) • 8.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
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; }; }();
var _fact = require('./fact');
var _fact2 = _interopRequireDefault(_fact);
var _errors = require('./errors');
var _debug = require('./debug');
var _debug2 = _interopRequireDefault(_debug);
var _jsonpathPlus = require('jsonpath-plus');
var _lodash = require('lodash.isobjectlike');
var _lodash2 = _interopRequireDefault(_lodash);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Fact results lookup
* Triggers fact computations and saves the results
* A new almanac is used for every engine run()
*/
var Almanac = function () {
function Almanac(factMap) {
var runtimeFacts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
_classCallCheck(this, Almanac);
this.factMap = new Map(factMap);
this.factResultsCache = new Map(); // { cacheKey: Promise<factValu> }
this.allowUndefinedFacts = Boolean(options.allowUndefinedFacts);
for (var factId in runtimeFacts) {
var fact = void 0;
if (runtimeFacts[factId] instanceof _fact2.default) {
fact = runtimeFacts[factId];
} else {
fact = new _fact2.default(factId, runtimeFacts[factId]);
}
this._addConstantFact(fact);
(0, _debug2.default)('almanac::constructor initialized runtime fact:' + fact.id + ' with ' + fact.value + '<' + _typeof(fact.value) + '>');
}
}
/**
* Retrieve fact by id, raising an exception if it DNE
* @param {String} factId
* @return {Fact}
*/
_createClass(Almanac, [{
key: '_getFact',
value: function _getFact(factId) {
return this.factMap.get(factId);
}
/**
* Registers fact with the almanac
* @param {[type]} fact [description]
*/
}, {
key: '_addConstantFact',
value: function _addConstantFact(fact) {
this.factMap.set(fact.id, fact);
this._setFactValue(fact, {}, fact.value);
}
/**
* Sets the computed value of a fact
* @param {Fact} fact
* @param {Object} params - values for differentiating this fact value from others, used for cache key
* @param {Mixed} value - computed value
*/
}, {
key: '_setFactValue',
value: function _setFactValue(fact, params, value) {
var cacheKey = fact.getCacheKey(params);
var factValue = Promise.resolve(value);
if (cacheKey) {
this.factResultsCache.set(cacheKey, factValue);
}
return factValue;
}
/**
* Adds a constant fact during runtime. Can be used mid-run() to add additional information
* @param {String} fact - fact identifier
* @param {Mixed} value - constant value of the fact
*/
}, {
key: 'addRuntimeFact',
value: function addRuntimeFact(factId, value) {
(0, _debug2.default)('almanac::addRuntimeFact id:' + factId);
var fact = new _fact2.default(factId, value);
return this._addConstantFact(fact);
}
/**
* Returns the value of a fact, based on the given parameters. Utilizes the 'almanac' maintained
* by the engine, which cache's fact computations based on parameters provided
* @param {string} factId - fact identifier
* @param {Object} params - parameters to feed into the fact. By default, these will also be used to compute the cache key
* @param {String} path - object
* @return {Promise} a promise which will resolve with the fact computation.
*/
}, {
key: 'factValue',
value: function factValue(factId) {
var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
var factValuePromise = void 0;
var fact = this._getFact(factId);
if (fact === undefined) {
if (this.allowUndefinedFacts) {
return Promise.resolve(undefined);
} else {
return Promise.reject(new _errors.UndefinedFactError('Undefined fact: ' + factId));
}
}
if (fact.isConstant()) {
factValuePromise = Promise.resolve(fact.calculate(params, this));
} else {
var cacheKey = fact.getCacheKey(params);
var cacheVal = cacheKey && this.factResultsCache.get(cacheKey);
if (cacheVal) {
factValuePromise = Promise.resolve(cacheVal);
(0, _debug2.default)('almanac::factValue cache hit for fact:' + factId);
} else {
(0, _debug2.default)('almanac::factValue cache miss for fact:' + factId + '; calculating');
factValuePromise = this._setFactValue(fact, params, fact.calculate(params, this));
}
}
if (path) {
// selectn supports arrays and strings as a 'path'
// strings starting with '$' denotes json path. otherwise fall back to deprecated 'selectn' syntax
if (typeof path === 'string' && path.startsWith('$')) {
(0, _debug2.default)('condition::evaluate extracting object property ' + path);
return factValuePromise.then(function (factValue) {
if ((0, _lodash2.default)(factValue)) {
var pathValue = (0, _jsonpathPlus.JSONPath)({ path: path, json: factValue, wrap: false });
(0, _debug2.default)('condition::evaluate extracting object property ' + path + ', received: ' + pathValue);
return pathValue;
} else {
(0, _debug2.default)('condition::evaluate could not compute object path(' + path + ') of non-object: ' + factValue + ' <' + (typeof factValue === 'undefined' ? 'undefined' : _typeof(factValue)) + '>; continuing with ' + factValue);
return factValue;
}
});
} else {
var selectn = void 0;
try {
selectn = require('selectn');
} catch (err) {
console.error('Oops! Looks like you\'re trying to use the deprecated syntax for the ".path" property.');
console.error('Please convert your "path" properties to JsonPath syntax (ensure your path starts with "$")');
console.error('Alternatively, if you wish to continue using old syntax (provided by selectn), you may "npm install selectn" as a direct dependency.');
console.error('See https://github.com/CacheControl/json-rules-engine/blob/master/CHANGELOG.md#500--2019-10-27 for more information.');
throw new Error('json-rules-engine: Unmet peer dependency "selectn" required for use of deprecated ".path" syntax. please "npm install selectn" or convert to json-path syntax');
}
return factValuePromise.then(function (factValue) {
if ((0, _lodash2.default)(factValue)) {
var pathValue = selectn(path)(factValue);
(0, _debug2.default)('condition::evaluate extracting object property ' + path + ', received: ' + pathValue);
return pathValue;
} else {
(0, _debug2.default)('condition::evaluate could not compute object path(' + path + ') of non-object: ' + factValue + ' <' + (typeof factValue === 'undefined' ? 'undefined' : _typeof(factValue)) + '>; continuing with ' + factValue);
return factValue;
}
});
}
}
return factValuePromise;
}
}]);
return Almanac;
}();
exports.default = Almanac;