1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _OPERATORS$IN, _OPERATORS$GTE, _OPERATORS$LT, _REDUCER_FROM_DECISIO;
|
8 |
|
9 | var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
|
10 |
|
11 | exports.reduceDecisionRules = reduceDecisionRules;
|
12 |
|
13 | var _lodash = require('lodash');
|
14 |
|
15 | var _lodash2 = _interopRequireDefault(_lodash);
|
16 |
|
17 | var _formatter = require('./formatter');
|
18 |
|
19 | var _constants = require('./constants');
|
20 |
|
21 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
22 |
|
23 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
24 |
|
25 | var REDUCER_FROM_DECISION_RULE = (_REDUCER_FROM_DECISIO = {}, _defineProperty(_REDUCER_FROM_DECISIO, _constants.OPERATORS.IS, _defineProperty({}, _constants.OPERATORS.IS, function (decisionRule1, decisionRule2) {
|
26 | if (decisionRule1.operand && decisionRule1.operand != decisionRule2.operand) {
|
27 | throw new Error('Operator "' + _constants.OPERATORS.IS + '" can\'t have different value. Set to "' + decisionRule1.operand + '" and receive "' + decisionRule2.operand + '"');
|
28 | }
|
29 | return {
|
30 | property: decisionRule1.property,
|
31 | operator: _constants.OPERATORS.IS,
|
32 | operand: decisionRule2.operand
|
33 | };
|
34 | })), _defineProperty(_REDUCER_FROM_DECISIO, _constants.OPERATORS.IN, (_OPERATORS$IN = {}, _defineProperty(_OPERATORS$IN, _constants.OPERATORS.IN, function (decisionRule1, decisionRule2) {
|
35 | var _decisionRule1$operan = _slicedToArray(decisionRule1.operand, 2),
|
36 | o1From = _decisionRule1$operan[0],
|
37 | o1To = _decisionRule1$operan[1];
|
38 |
|
39 | var _decisionRule2$operan = _slicedToArray(decisionRule2.operand, 2),
|
40 | o2From = _decisionRule2$operan[0],
|
41 | o2To = _decisionRule2$operan[1];
|
42 |
|
43 | var o1IsCyclic = o1From > o1To;
|
44 | var o2IsCyclic = o2From > o2To;
|
45 | var o2FromInO1 = o1IsCyclic ? o2From >= o1From || o2From <= o1To : o2From >= o1From && o2From <= o1To;
|
46 | var o2ToInO1 = o1IsCyclic ? o2To >= o1From || o2To <= o1To : o2To >= o1From && o2To <= o1To;
|
47 | var o1FromInO2 = o2IsCyclic ? o1From >= o2From || o1From <= o2To : o1From >= o2From && o1From <= o2To;
|
48 | var o1ToInO2 = o2IsCyclic ? o1To >= o2From || o1To <= o2To : o1To >= o2From && o1To <= o2To;
|
49 |
|
50 | if (o1FromInO2 && o1ToInO2) {
|
51 |
|
52 |
|
53 |
|
54 | return decisionRule1;
|
55 | }
|
56 |
|
57 | if (o2FromInO1 && o2ToInO1) {
|
58 |
|
59 |
|
60 |
|
61 | return decisionRule2;
|
62 | }
|
63 |
|
64 | if (o2FromInO1 && o1ToInO2) {
|
65 |
|
66 |
|
67 |
|
68 | return {
|
69 | property: decisionRule1.property,
|
70 | operator: _constants.OPERATORS.IN,
|
71 | operand: [o2From, o1To]
|
72 | };
|
73 | }
|
74 |
|
75 | if (o2ToInO1 && o1FromInO2) {
|
76 |
|
77 |
|
78 |
|
79 | return {
|
80 | property: decisionRule1.property,
|
81 | operator: _constants.OPERATORS.IN,
|
82 | operand: [o1From, o2To]
|
83 | };
|
84 | }
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | throw new Error('Unable to reduce decision rules \'' + (0, _formatter.formatDecisionRules)([decisionRule1]) + '\' and \'' + (0, _formatter.formatDecisionRules)([decisionRule2]) + '\': the resulting rule is not fulfillable.');
|
90 | }), _defineProperty(_OPERATORS$IN, _constants.OPERATORS.GTE, function (decisionRule1, decisionRule2) {
|
91 | var _decisionRule1$operan2 = _slicedToArray(decisionRule1.operand, 2),
|
92 | o1From = _decisionRule1$operan2[0],
|
93 | o1To = _decisionRule1$operan2[1];
|
94 |
|
95 | var o2 = decisionRule2.operand;
|
96 |
|
97 | var o1IsCyclic = o1From > o1To;
|
98 |
|
99 | if (o1IsCyclic) {
|
100 |
|
101 | throw new Error('Unable to reduce decision rules \'' + (0, _formatter.formatDecisionRules)([decisionRule1]) + '\' and \'' + (0, _formatter.formatDecisionRules)([decisionRule2]) + '\': the resulting rule is not fulfillable.');
|
102 | }
|
103 |
|
104 | if (o2 >= o1To) {
|
105 |
|
106 |
|
107 |
|
108 | throw new Error('Unable to reduce decision rules \'' + (0, _formatter.formatDecisionRules)([decisionRule1]) + '\' and \'' + (0, _formatter.formatDecisionRules)([decisionRule2]) + '\': the resulting rule is not fulfillable.');
|
109 | }
|
110 |
|
111 | if (o2 >= o1From && o2 < o1To) {
|
112 |
|
113 |
|
114 |
|
115 | return {
|
116 | property: decisionRule1.property,
|
117 | operator: _constants.OPERATORS.IN,
|
118 | operand: [o2, o1To]
|
119 | };
|
120 | }
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | return decisionRule1;
|
126 | }), _defineProperty(_OPERATORS$IN, _constants.OPERATORS.LT, function (decisionRule1, decisionRule2) {
|
127 | var _decisionRule1$operan3 = _slicedToArray(decisionRule1.operand, 2),
|
128 | o1From = _decisionRule1$operan3[0],
|
129 | o1To = _decisionRule1$operan3[1];
|
130 |
|
131 | var o2 = decisionRule2.operand;
|
132 |
|
133 | var o1IsCyclic = o1From > o1To;
|
134 |
|
135 | if (o1IsCyclic) {
|
136 |
|
137 | throw new Error('Unable to reduce decision rules \'' + (0, _formatter.formatDecisionRules)([decisionRule1]) + '\' and \'' + (0, _formatter.formatDecisionRules)([decisionRule2]) + '\': the resulting rule is not fulfillable.');
|
138 | }
|
139 |
|
140 | if (o2 < o1From) {
|
141 |
|
142 |
|
143 |
|
144 | throw new Error('Unable to reduce decision rules \'' + (0, _formatter.formatDecisionRules)([decisionRule1]) + '\' and \'' + (0, _formatter.formatDecisionRules)([decisionRule2]) + '\': the resulting rule is not fulfillable.');
|
145 | }
|
146 |
|
147 | if (o2 >= o1From && o2 < o1To) {
|
148 |
|
149 |
|
150 |
|
151 | return {
|
152 | property: decisionRule1.property,
|
153 | operator: _constants.OPERATORS.IN,
|
154 | operand: [o1From, o2]
|
155 | };
|
156 | }
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | return decisionRule1;
|
162 | }), _OPERATORS$IN)), _defineProperty(_REDUCER_FROM_DECISIO, _constants.OPERATORS.GTE, (_OPERATORS$GTE = {}, _defineProperty(_OPERATORS$GTE, _constants.OPERATORS.IN, function (decisionRule1, decisionRule2) {
|
163 | return REDUCER_FROM_DECISION_RULE[_constants.OPERATORS.IN][_constants.OPERATORS.GTE](decisionRule2, decisionRule1);
|
164 | }), _defineProperty(_OPERATORS$GTE, _constants.OPERATORS.GTE, function (decisionRule1, decisionRule2) {
|
165 | return {
|
166 | property: decisionRule1.property,
|
167 | operator: _constants.OPERATORS.GTE,
|
168 | operand: Math.max(decisionRule1.operand, decisionRule2.operand)
|
169 | };
|
170 | }), _defineProperty(_OPERATORS$GTE, _constants.OPERATORS.LT, function (decisionRule1, decisionRule2) {
|
171 | var newLowerBound = decisionRule1.operand;
|
172 | var newUpperBound = decisionRule2.operand;
|
173 | if (newUpperBound < newLowerBound) {
|
174 | throw new Error('Unable to reduce decision rules \'' + (0, _formatter.formatDecisionRules)([decisionRule1]) + '\' and \'' + (0, _formatter.formatDecisionRules)([decisionRule2]) + '\': the resulting rule is not fulfillable.');
|
175 | }
|
176 | return {
|
177 | property: decisionRule1.property,
|
178 | operator: _constants.OPERATORS.IN,
|
179 | operand: [newLowerBound, newUpperBound]
|
180 | };
|
181 | }), _OPERATORS$GTE)), _defineProperty(_REDUCER_FROM_DECISIO, _constants.OPERATORS.LT, (_OPERATORS$LT = {}, _defineProperty(_OPERATORS$LT, _constants.OPERATORS.IN, function (decisionRule1, decisionRule2) {
|
182 | return REDUCER_FROM_DECISION_RULE[_constants.OPERATORS.IN][_constants.OPERATORS.LT](decisionRule2, decisionRule1);
|
183 | }), _defineProperty(_OPERATORS$LT, _constants.OPERATORS.GTE, function (decisionRule1, decisionRule2) {
|
184 | return REDUCER_FROM_DECISION_RULE[_constants.OPERATORS.GTE][_constants.OPERATORS.LT](decisionRule2, decisionRule1);
|
185 | }), _defineProperty(_OPERATORS$LT, _constants.OPERATORS.LT, function (decisionRule1, decisionRule2) {
|
186 | return {
|
187 | property: decisionRule1.property,
|
188 | operator: _constants.OPERATORS.LT,
|
189 | operand: Math.min(decisionRule1.operand, decisionRule2.operand)
|
190 | };
|
191 | }), _OPERATORS$LT)), _REDUCER_FROM_DECISIO);
|
192 |
|
193 | function decisionRuleReducer(decisionRule1, decisionRule2) {
|
194 | if (!decisionRule1 || !decisionRule2) {
|
195 | return decisionRule1 || decisionRule2;
|
196 | }
|
197 | var reducer = REDUCER_FROM_DECISION_RULE[decisionRule1.operator][decisionRule2.operator];
|
198 | if (!reducer) {
|
199 | throw new Error('Unable to reduce decision rules \'' + (0, _formatter.formatDecisionRules)([decisionRule1]) + '\' and \'' + (0, _formatter.formatDecisionRules)([decisionRule2]) + '\': incompatible operators.');
|
200 | }
|
201 | return reducer(decisionRule1, decisionRule2);
|
202 | }
|
203 |
|
204 | function reduceDecisionRules(decisionRules) {
|
205 | var properties = _lodash2.default.uniq(_lodash2.default.map(decisionRules, function (_ref) {
|
206 | var property = _ref.property;
|
207 | return property;
|
208 | }));
|
209 | return _lodash2.default.map(properties, function (currentPropery) {
|
210 | return (0, _lodash2.default)(decisionRules).filter(function (_ref2) {
|
211 | var property = _ref2.property;
|
212 | return property == currentPropery;
|
213 | }).reduce(decisionRuleReducer, undefined);
|
214 | });
|
215 | } |
\ | No newline at end of file |