dmn-eval-js-es5
Version:
Evaluation of DMN 1.1 decision tables, limited to S-FEEL (Simple Friendly Enough Expression Language), es5 browser compatible
231 lines (214 loc) • 8.44 kB
JavaScript
;
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; };
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
/*
*
* ©2016-2017 EdgeVerve Systems Limited (a fully owned Infosys subsidiary),
* Bangalore, India. All Rights Reserved.
*
*/
/*
Single hit policies for single output decision tables are:
1. Unique: no overlap is possible and all rules are disjoint. Only a single rule can be matched. This is the default.
2. Any: there may be overlap, but all of the matching rules show equal output entries for each output, so any match can
be used. If the output entries are non-equal, the hit policy is incorrect and the result is undefined.
3. Priority: multiple rules can match, with different output entries. This policy returns the matching rule with the
highest output priority. Output priorities are specified in the ordered list of output values, in decreasing order of
priority. Note that priorities are independent from rule sequence.
4. First: multiple (overlapping) rules can match, with different output entries. The first hit by rule order is returned (and
evaluation can halt). This is still a common usage, because it resolves inconsistencies by forcing the first hit.
However, first hit tables are not considered good practice because they do not offer a clear overview of the decision
logic. It is important to distinguish this type of table from others because the meaning depends on the order of the
rules. The last rule is often the catch-remainder. Because of this order, the table is hard to validate manually and
therefore has to be used with care.
Multiple hit policies for single output decision tables can be:
5. Output order: returns all hits in decreasing output priority order. Output priorities are specified in the ordered list of
output values in decreasing order of priority.
6. Rule order: returns all hits in rule order. Note: the meaning may depend on the sequence of the rules.
7. Collect: returns all hits in arbitrary order. An operator (‘+’, ‘<’, ‘>’, ‘#’) can be added to apply a simple function to
the outputs. If no operator is present, the result is the list of all the output entries.
Collect operators are:
a) + (sum): the result of the decision table is the sum of all the distinct outputs.
b) < (min): the result of the decision table is the smallest value of all the outputs.
c) > (max): the result of the decision table is the largest value of all the outputs.
d) # (count): the result of the decision table is the number of distinct outputs.
Other policies, such as more complex manipulations on the outputs, can be performed by post-processing the
output list (outside the decision table).
NOTE : Decision tables with compound outputs support only the following hit policies: Unique, Any, Priority, First, Output
order, Rule order and Collect without operator, because the collect operator is undefined over multiple outputs.
*/
var _ = require('lodash');
var getDistinct = function getDistinct(arr) {
return arr.filter(function (item, index, arr) {
return arr.indexOf(item) === index;
});
};
var sum = function sum(arr) {
// const distinctArr = getDistinct(arr);
var distinctArr = arr;
var elem = distinctArr[0];
if (typeof elem === 'string') {
return distinctArr.join(' ');
} else if (typeof elem === 'number') {
return distinctArr.reduce(function (a, b) {
return a + b;
}, 0);
} else if (typeof elem === 'boolean') {
return distinctArr.reduce(function (a, b) {
return a && b;
}, true);
}
throw new Error('sum operation not supported for type ' + (typeof elem === 'undefined' ? 'undefined' : _typeof(elem)));
};
var count = function count(arr) {
if (Array.isArray(arr)) {
var distinctArr = getDistinct(arr);
return distinctArr.length;
}
throw new Error('count operation not supported for type ' + (typeof arr === 'undefined' ? 'undefined' : _typeof(arr)));
};
var min = function min(arr) {
var elem = arr[0];
if (typeof elem === 'string') {
arr.sort();
return arr[0];
} else if (typeof elem === 'number') {
return Math.min.apply(Math, _toConsumableArray(arr));
} else if (typeof elem === 'boolean') {
return arr.reduce(function (a, b) {
return a && b;
}, true) ? 1 : 0;
}
throw new Error('min operation not supported for type ' + (typeof elem === 'undefined' ? 'undefined' : _typeof(elem)));
};
var max = function max(arr) {
var elem = arr[0];
if (typeof elem === 'string') {
arr.sort();
return arr[arr.length - 1];
} else if (typeof elem === 'number') {
return Math.max.apply(Math, _toConsumableArray(arr));
} else if (typeof elem === 'boolean') {
return arr.reduce(function (a, b) {
return a || b;
}, false) ? 1 : 0;
}
throw new Error('max operation not supported for type ' + (typeof elem === 'undefined' ? 'undefined' : _typeof(elem)));
};
var collectOperatorMap = {
'+': sum,
'#': count,
'<': min,
'>': max
};
var checkEntriesEquality = function checkEntriesEquality(output) {
var isEqual = true;
if (output.length > 1) {
var value = output[0];
output.every(function (other) {
isEqual = _.isEqual(value, other);
return isEqual;
});
return isEqual;
}
return isEqual;
};
var getValidationErrors = function getValidationErrors(output) {
return output.filter(function (ruleStatus) {
return ruleStatus.isValid === false;
}).map(function (rule) {
var newRule = rule;
delete newRule.isValid;
return newRule;
});
};
var hitPolicyPass = function hitPolicyPass(hitPolicy, output) {
return new Promise(function (resolve, reject) {
var policy = hitPolicy.charAt(0);
var ruleOutput = [];
switch (policy) {
// Single hit policies
case 'U':
ruleOutput = output.length > 1 ? {} : output[0];
break;
case 'A':
ruleOutput = checkEntriesEquality(output) ? output[0] : undefined;
break;
case 'P':
ruleOutput = output[0];
break;
case 'F':
ruleOutput = output[0];
break;
// Multiple hit policies
case 'C':
{
var operator = hitPolicy.charAt(1);
if (operator.length > 0 && output.length > 0) {
var fn = collectOperatorMap[operator];
var key = Object.keys(output[0])[0];
var arr = output.map(function (item) {
return item[key];
});
var result = {};
try {
result[key] = fn(arr);
} catch (e) {
reject(e);
}
ruleOutput = result;
} else {
ruleOutput = output;
}
break;
}
case 'R':
ruleOutput = output;
break;
case 'O':
ruleOutput = output;
break;
case 'V':
ruleOutput = getValidationErrors(output);
break;
default:
ruleOutput = output;
}
resolve(ruleOutput);
});
};
var prepareOutputOrder = function prepareOutputOrder(output, priorityList) {
var arr = output.map(function (rule) {
var obj = {};
obj.rule = rule;
obj.priority = priorityList[rule];
return obj;
});
var sortedPriorityList = _.sortBy(arr, ['priority']);
var outputList = sortedPriorityList.map(function (ruleObj) {
return ruleObj.rule;
});
return outputList;
};
var getOrderedOutput = function getOrderedOutput(root, outputList) {
var policy = root.hitPolicy.charAt(0);
var outputOrderedList = [];
switch (policy) {
case 'P':
outputOrderedList.push(prepareOutputOrder(outputList, root.priorityList)[0]);
break;
case 'O':
outputOrderedList = prepareOutputOrder(outputList, root.priorityList);
break;
case 'F':
outputOrderedList = outputList.sort().slice(0, 1);
break;
case 'R':
outputOrderedList = outputList.sort();
break;
default:
outputOrderedList = outputList;
}
return outputOrderedList;
};
module.exports = { hitPolicyPass: hitPolicyPass, getOrderedOutput: getOrderedOutput };