UNPKG

9.58 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _OPERATORS$IN, _OPERATORS$GTE, _OPERATORS$LT, _REDUCER_FROM_DECISIO;
8
9var _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
11exports.reduceDecisionRules = reduceDecisionRules;
12
13var _lodash = require('lodash');
14
15var _lodash2 = _interopRequireDefault(_lodash);
16
17var _formatter = require('./formatter');
18
19var _constants = require('./constants');
20
21function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
23function _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
25var 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 // o1 belongs to o2
52 // | o1 |
53 // | o2 |
54 return decisionRule1;
55 }
56
57 if (o2FromInO1 && o2ToInO1) {
58 // o2 belongs to o1
59 // | o1 |
60 // | o2 |
61 return decisionRule2;
62 }
63
64 if (o2FromInO1 && o1ToInO2) {
65 // overlap 1
66 // | o1 |
67 // | o2 |
68 return {
69 property: decisionRule1.property,
70 operator: _constants.OPERATORS.IN,
71 operand: [o2From, o1To]
72 };
73 }
74
75 if (o2ToInO1 && o1FromInO2) {
76 // overlap 2
77 // | o1 |
78 // | o2 |
79 return {
80 property: decisionRule1.property,
81 operator: _constants.OPERATORS.IN,
82 operand: [o1From, o2To]
83 };
84 }
85
86 // disjointed
87 // | o1 |
88 // | o2 |
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 // Cyclics makes no sense with single bound limits
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 // o2 after o1, disjointed
106 // | o1 |
107 // |o2
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 // o2 belongs to o1
113 // | o1 |
114 // |o2
115 return {
116 property: decisionRule1.property,
117 operator: _constants.OPERATORS.IN,
118 operand: [o2, o1To]
119 };
120 }
121
122 // o2 before o1
123 // | o1 |
124 // |o2
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 // Cyclics makes no sense with single bound limits
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 // o2 before o1, disjointed
142 // | o1 |
143 // o2|
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 // o2 belongs to o1
149 // | o1 |
150 // o2|
151 return {
152 property: decisionRule1.property,
153 operator: _constants.OPERATORS.IN,
154 operand: [o1From, o2]
155 };
156 }
157
158 // o2 after o1
159 // | o1 |
160 // o2|
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
193function 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
204function 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