json-rules-engine
Version:
Rules Engine expressed in simple json
119 lines (97 loc) • 4.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
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 _objectHash = require('object-hash');
var _objectHash2 = _interopRequireDefault(_objectHash);
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"); } }
var verbose = require('debug')('json-rules-engine-verbose');
var Fact = function () {
/**
* Returns a new fact instance
* @param {string} id - fact unique identifer
* @param {object} options
* @param {boolean} options.cache - whether to cache the fact's value for future rules
* @param {primitive|function} valueOrMethod - constant primitive, or method to call when computing the fact's value
* @return {Fact}
*/
function Fact(id, valueOrMethod, options) {
_classCallCheck(this, Fact);
this.id = id;
var defaultOptions = { cache: true };
if (typeof options === 'undefined') {
options = defaultOptions;
}
if (typeof valueOrMethod !== 'function') {
this.value = valueOrMethod;
} else {
this.calculationMethod = valueOrMethod;
}
if (!this.id) throw new Error('factId required');
if (typeof this.value === 'undefined' && typeof this.calculationMethod === 'undefined') {
throw new Error('facts must have a value or method');
}
this.priority = parseInt(options.priority || 1, 10);
this.options = Object.assign({}, defaultOptions, options);
this.cacheKeyMethod = this.defaultCacheKeys;
return this;
}
/**
* Return the fact value, based on provided parameters
* @param {object} params
* @param {Almanac} almanac
* @return {any} calculation method results
*/
_createClass(Fact, [{
key: 'calculate',
value: function calculate(params, almanac) {
// if constant fact w/set value, return immediately
if (this.hasOwnProperty('value')) {
return this.value;
}
return this.calculationMethod(params, almanac);
}
/**
* Return a cache key (MD5 string) based on parameters
* @param {object} obj - properties to generate a hash key from
* @return {string} MD5 string based on the hash'd object
*/
}, {
key: 'defaultCacheKeys',
/**
* Default properties to use when caching a fact
* Assumes every fact is a pure function, whose computed value will only
* change when input params are modified
* @param {string} id - fact unique identifer
* @param {object} params - parameters passed to fact calcution method
* @return {object} id + params
*/
value: function defaultCacheKeys(id, params) {
return { params: params, id: id };
}
/**
* Generates the fact's cache key(MD5 string)
* Returns nothing if the fact's caching has been disabled
* @param {object} params - parameters that would be passed to the computation method
* @return {string} cache key
*/
}, {
key: 'getCacheKey',
value: function getCacheKey(params) {
if (this.options.cache === true) {
var cacheProperties = this.cacheKeyMethod(this.id, params);
return Fact.hashFromObject(cacheProperties);
}
}
}], [{
key: 'hashFromObject',
value: function hashFromObject(obj) {
verbose('fact::hashFromObject generating cache key from:', obj);
return (0, _objectHash2.default)(obj);
}
}]);
return Fact;
}();
exports.default = Fact;