@media-quest/engine 0.0.25 → 0.0.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,351 +1,351 @@
1
- import { RuleEngine } from "../rule-engine";
2
- import { Rule } from "../rule";
3
- import { Fact } from "../fact";
4
- import { Condition } from "../condition";
5
- import { RuleActionPageQue } from "../../engine/page-que-ruleengine-action";
6
- import { DUtil } from "../../utils/DUtil";
7
- // import { PageID } from "../../utils/ID";
8
-
9
- const excludeById = (ids: string[]): RuleActionPageQue => {
10
- return {
11
- kind: "excludeByPageId",
12
- pageIds: ids,
13
- };
14
- };
15
-
16
- const excludeByTag = (id: string): RuleActionPageQue => ({ kind: "excludeByTag", tagIds: [id] });
17
-
18
- let engine = new RuleEngine();
19
- // const x = 'x';
20
- // const y = 'y';
21
-
22
- const xIs = (value: number): Fact.Numeric => ({
23
- referenceId: "x",
24
- referenceLabel: "x-label",
25
- label: "value-label x",
26
- value,
27
- kind: "numeric-fact",
28
- });
29
-
30
- const yIs = (value: number): Fact.Numeric => ({
31
- kind: "numeric-fact",
32
- referenceId: "y",
33
- referenceLabel: "y-label",
34
- label: "var-label y",
35
- // valueLabel: 'value-label y',
36
- value,
37
- });
38
-
39
- const xCondition = (operator: Condition.NumericOperator, value: number): Condition.Numeric => ({
40
- kind: "numeric-condition",
41
- referenceId: "x",
42
- referenceLabel: "",
43
- operator,
44
- value,
45
- valueLabel: "",
46
- });
47
-
48
- const trueIf_0_conditions: Condition.Numeric[] = [
49
- xCondition("eq", 0),
50
- xCondition("not-eq", 2),
51
- xCondition("less-then", 1),
52
- xCondition("less-then-inclusive", 0),
53
- xCondition("greater-then", -1),
54
- xCondition("greater-then-inclusive", 0),
55
- ];
56
-
57
- const falseIf_0_conditions: Condition.Numeric[] = [
58
- xCondition("eq", 10),
59
- xCondition("not-eq", 0),
60
- xCondition("less-then", -5),
61
- xCondition("less-then-inclusive", -1),
62
- xCondition("greater-then", 0),
63
- xCondition("greater-then-inclusive", 1),
64
- ];
65
-
66
- const trueIf_0_simple: Condition.Complex[] = [
67
- {
68
- kind: "complex-condition",
69
- name: "test-name",
70
- all: [...trueIf_0_conditions],
71
- some: [...trueIf_0_conditions, ...falseIf_0_conditions],
72
- },
73
- ];
74
-
75
- const falseIf_0_simple: Condition.Complex[] = [
76
- {
77
- kind: "complex-condition",
78
- name: "test-name",
79
- all: [...trueIf_0_conditions, ...falseIf_0_conditions],
80
- some: [...falseIf_0_conditions],
81
- },
82
- ];
83
-
84
- describe("Rule-engine spec", () => {
85
- beforeEach(() => {
86
- engine = new RuleEngine();
87
- });
88
-
89
- it("Empty rule => one error, no actions", () => {
90
- const rule: Rule<any, any> = {
91
- id: "id123",
92
- description: "",
93
- some: [],
94
- all: [],
95
- onFailure: [],
96
- onSuccess: [],
97
- };
98
- const result = engine.solveAll([rule], []);
99
- expect(result.matching.length).toBe(0);
100
- expect(result.errors.length).toBe(1);
101
- });
102
-
103
- it("Empty facts => no actions, no errors.", () => {
104
- const hideAs1 = excludeById([DUtil.randomObjectId()]);
105
- const rule: Rule<any, any> = {
106
- id: "id123",
107
- description: "",
108
- some: [],
109
- all: [...trueIf_0_conditions],
110
- onFailure: [],
111
- onSuccess: [hideAs1],
112
- };
113
- const facts = [yIs(1)];
114
- const rules = [rule];
115
- // engine.addRule(rule);
116
- // engine.addFact(yIs(1)); // X is now missing.
117
- const result = engine.solveAll(rules, facts);
118
-
119
- expect(result.matching.length).toBe(0);
120
- expect(result.errors.length).toBe(0);
121
- });
122
-
123
- it("and-rule 0=0 true -> 2 Actions in ruleMatch", () => {
124
- const rule: Rule<any, any> = {
125
- id: "id123",
126
- description: "",
127
- some: [],
128
- all: [...trueIf_0_conditions],
129
- onFailure: [],
130
- onSuccess: [excludeById([DUtil.randomObjectId(), DUtil.randomObjectId()])],
131
- };
132
- const f1 = xIs(0);
133
- const rules = [rule];
134
- const result = engine.solveAll(rules, [f1]);
135
- const firstMatch = result.matching[0];
136
- expect(firstMatch.actionList.length).toBe(1);
137
- expect(result.matching.length).toBe(1);
138
- expect(result.errors.length).toBe(0);
139
- });
140
-
141
- it("and-rule 0=1 false", () => {
142
- const facts = [xIs(1)];
143
- const rule: Rule<any, any> = {
144
- id: "id123",
145
-
146
- description: "",
147
- some: [],
148
- all: [xCondition("eq", 0)],
149
- onFailure: [],
150
- onSuccess: [excludeById([DUtil.randomObjectId(), DUtil.randomObjectId()])],
151
- };
152
-
153
- expect(engine.solve(rule, facts)).toEqual(false);
154
-
155
- const results = engine.solveAll([rule], facts);
156
- expect(results.matching.length).toBe(0);
157
- expect(results.errors.length).toBe(0);
158
- });
159
-
160
- it("One true some-rule gives true", () => {
161
- const facts = [xIs(6)];
162
- const rule: Rule<any, any> = {
163
- id: "id123",
164
- description: "",
165
- some: [xCondition("eq", 6)],
166
- all: [],
167
- onFailure: [],
168
- onSuccess: [excludeById([DUtil.randomObjectId()])],
169
- };
170
- expect(engine.solve(rule, facts)).toBe(true);
171
- const result = engine.solveAll([rule], facts);
172
- expect(result.matching.length).toBe(1);
173
- });
174
-
175
- it("One false some-rule gives false", () => {
176
- const facts = [xIs(6)];
177
- const rule: Rule<any, any> = {
178
- id: "id123",
179
-
180
- description: "",
181
- some: [xCondition("eq", 5)],
182
- all: [],
183
- onFailure: [],
184
- onSuccess: [],
185
- };
186
- expect(engine.solve(rule, facts)).toBe(false);
187
- });
188
-
189
- it("All true all-rules, and one true some-rule -> true", () => {
190
- const facts = [xIs(9)];
191
- const rule: Rule<any, any> = {
192
- id: "id123",
193
-
194
- description: "",
195
- some: [xCondition("eq", 5), xCondition("greater-then", 3)],
196
- all: [xCondition("greater-then", 5), xCondition("eq", 9)],
197
- onFailure: [],
198
- onSuccess: [],
199
- };
200
- expect(engine.solve(rule, facts)).toBe(true);
201
- });
202
-
203
- it("Can solve complex rule [some-2 -> true]", () => {
204
- const facts = [xIs(9)];
205
- const rule: Rule<any, any> = {
206
- id: "id123",
207
-
208
- description: "",
209
- some: [
210
- {
211
- kind: "complex-condition",
212
- name: "test-name",
213
- all: [],
214
- some: [xCondition("eq", 5), xCondition("greater-then", 3)],
215
- },
216
- ],
217
- all: [],
218
- onFailure: [],
219
- onSuccess: [excludeByTag("asdf")],
220
- };
221
- expect(engine.solve(rule, facts)).toBe(true);
222
- const result = engine.solveAll([rule], facts);
223
- expect(result.matching.length).toBe(1);
224
- });
225
- it("Can solve complex rule [some-1 -> false]", () => {
226
- const facts = [xIs(0)];
227
- const rule: Rule<any, any> = {
228
- id: "id123",
229
-
230
- description: "",
231
- some: [
232
- {
233
- kind: "complex-condition",
234
- name: "test-name",
235
- all: [],
236
- some: [xCondition("eq", 5)],
237
- },
238
- ],
239
- all: [],
240
- onFailure: [],
241
- onSuccess: [excludeByTag("xbj")],
242
- };
243
- expect(engine.solve(rule, facts)).toBe(false);
244
- const result = engine.solveAll([rule], facts);
245
- expect(result.matching.length).toBe(0);
246
- expect(result.errors.length).toBe(0);
247
- });
248
-
249
- it("Can solve complex rule [some-1-true -> true]", () => {
250
- const facts = [xIs(0)];
251
- const rule: Rule<any, any> = {
252
- id: "id123",
253
-
254
- description: "",
255
- some: [
256
- {
257
- kind: "complex-condition",
258
- name: "test-name",
259
- all: [],
260
- some: [xCondition("greater-then", -5)],
261
- },
262
- ],
263
- all: [],
264
- onFailure: [],
265
- onSuccess: [],
266
- };
267
- expect(engine.solve(rule, facts)).toBe(true);
268
- });
269
-
270
- it("Empty some (nested) returns false (not valid condition)", () => {
271
- const rule: Rule<any, any> = {
272
- id: "id123",
273
-
274
- description: "",
275
- some: [
276
- {
277
- kind: "complex-condition",
278
- name: "test-name",
279
- all: [],
280
- some: [],
281
- },
282
- ],
283
- all: [],
284
- onFailure: [],
285
- onSuccess: [],
286
- };
287
- expect(engine.solve(rule, [])).toBe(false);
288
- });
289
-
290
- it("Empty complex -> false", () => {
291
- const rule: Rule<any, any> = {
292
- id: "id123",
293
-
294
- description: "",
295
- some: [],
296
- all: [],
297
- onFailure: [],
298
- onSuccess: [],
299
- };
300
- expect(engine.solve(rule, [])).toBe(false);
301
- });
302
-
303
- it("Complex all 6 true conditions -> true", () => {
304
- const facts = [xIs(0)];
305
- const action = excludeById([DUtil.randomObjectId()]);
306
- const rule: Rule<any, any> = {
307
- id: "id123",
308
-
309
- description: "",
310
- some: [...falseIf_0_simple, ...trueIf_0_simple],
311
- all: [...trueIf_0_conditions, ...trueIf_0_simple],
312
- onFailure: [],
313
- onSuccess: [action],
314
- };
315
- // CAN SOLVE
316
- expect(engine.solve(rule, facts)).toBe(true);
317
- // engine.addRule(rule);
318
- // expect(engine.getRules().length).toBe(1);
319
-
320
- // const lastAction = engine.actionState?.lastAction as HidePageAction;
321
- // expect(lastAction?.pageId).toBe('as1');
322
- });
323
-
324
- it("True some, but false all -> true", () => {
325
- const facts = [xIs(0)];
326
- const rule: Rule<any, any> = {
327
- id: "id123",
328
-
329
- description: "",
330
- some: [...falseIf_0_simple, ...trueIf_0_simple],
331
- all: [...trueIf_0_conditions, ...trueIf_0_simple, ...falseIf_0_simple],
332
- onFailure: [],
333
- onSuccess: [],
334
- };
335
- expect(engine.solve(rule, facts)).toBe(false);
336
- });
337
-
338
- it("Empty some && true all -> true", () => {
339
- const facts = [xIs(0)];
340
- const rule: Rule<any, any> = {
341
- id: "id123",
342
-
343
- description: "",
344
- some: [],
345
- all: [...trueIf_0_conditions, ...trueIf_0_simple],
346
- onFailure: [],
347
- onSuccess: [],
348
- };
349
- expect(engine.solve(rule, facts)).toBe(true);
350
- });
351
- });
1
+ import { RuleEngine } from "../rule-engine";
2
+ import { Rule } from "../rule";
3
+ import { Fact } from "../fact";
4
+ import { Condition } from "../condition";
5
+ import { RuleActionPageQue } from "../../engine/page-que-ruleengine-action";
6
+ import { DUtil } from "../../utils/DUtil";
7
+ // import { PageID } from "../../utils/ID";
8
+
9
+ const excludeById = (ids: string[]): RuleActionPageQue => {
10
+ return {
11
+ kind: "excludeByPageId",
12
+ pageIds: ids,
13
+ };
14
+ };
15
+
16
+ const excludeByTag = (id: string): RuleActionPageQue => ({ kind: "excludeByTag", tagIds: [id] });
17
+
18
+ let engine = new RuleEngine();
19
+ // const x = 'x';
20
+ // const y = 'y';
21
+
22
+ const xIs = (value: number): Fact.Numeric => ({
23
+ referenceId: "x",
24
+ referenceLabel: "x-label",
25
+ label: "value-label x",
26
+ value,
27
+ kind: "numeric-fact",
28
+ });
29
+
30
+ const yIs = (value: number): Fact.Numeric => ({
31
+ kind: "numeric-fact",
32
+ referenceId: "y",
33
+ referenceLabel: "y-label",
34
+ label: "var-label y",
35
+ // valueLabel: 'value-label y',
36
+ value,
37
+ });
38
+
39
+ const xCondition = (operator: Condition.NumericOperator, value: number): Condition.Numeric => ({
40
+ kind: "numeric-condition",
41
+ referenceId: "x",
42
+ referenceLabel: "",
43
+ operator,
44
+ value,
45
+ valueLabel: "",
46
+ });
47
+
48
+ const trueIf_0_conditions: Condition.Numeric[] = [
49
+ xCondition("eq", 0),
50
+ xCondition("not-eq", 2),
51
+ xCondition("less-then", 1),
52
+ xCondition("less-then-inclusive", 0),
53
+ xCondition("greater-then", -1),
54
+ xCondition("greater-then-inclusive", 0),
55
+ ];
56
+
57
+ const falseIf_0_conditions: Condition.Numeric[] = [
58
+ xCondition("eq", 10),
59
+ xCondition("not-eq", 0),
60
+ xCondition("less-then", -5),
61
+ xCondition("less-then-inclusive", -1),
62
+ xCondition("greater-then", 0),
63
+ xCondition("greater-then-inclusive", 1),
64
+ ];
65
+
66
+ const trueIf_0_simple: Condition.Complex[] = [
67
+ {
68
+ kind: "complex-condition",
69
+ name: "test-name",
70
+ all: [...trueIf_0_conditions],
71
+ some: [...trueIf_0_conditions, ...falseIf_0_conditions],
72
+ },
73
+ ];
74
+
75
+ const falseIf_0_simple: Condition.Complex[] = [
76
+ {
77
+ kind: "complex-condition",
78
+ name: "test-name",
79
+ all: [...trueIf_0_conditions, ...falseIf_0_conditions],
80
+ some: [...falseIf_0_conditions],
81
+ },
82
+ ];
83
+
84
+ describe("Rule-engine spec", () => {
85
+ beforeEach(() => {
86
+ engine = new RuleEngine();
87
+ });
88
+
89
+ it("Empty rule => one error, no actions", () => {
90
+ const rule: Rule<any, any> = {
91
+ id: "id123",
92
+ description: "",
93
+ some: [],
94
+ all: [],
95
+ onFailure: [],
96
+ onSuccess: [],
97
+ };
98
+ const result = engine.solveAll([rule], []);
99
+ expect(result.matching.length).toBe(0);
100
+ expect(result.errors.length).toBe(1);
101
+ });
102
+
103
+ it("Empty facts => no actions, no errors.", () => {
104
+ const hideAs1 = excludeById([DUtil.randomObjectId()]);
105
+ const rule: Rule<any, any> = {
106
+ id: "id123",
107
+ description: "",
108
+ some: [],
109
+ all: [...trueIf_0_conditions],
110
+ onFailure: [],
111
+ onSuccess: [hideAs1],
112
+ };
113
+ const facts = [yIs(1)];
114
+ const rules = [rule];
115
+ // engine.addRule(rule);
116
+ // engine.addFact(yIs(1)); // X is now missing.
117
+ const result = engine.solveAll(rules, facts);
118
+
119
+ expect(result.matching.length).toBe(0);
120
+ expect(result.errors.length).toBe(0);
121
+ });
122
+
123
+ it("and-rule 0=0 true -> 2 Actions in ruleMatch", () => {
124
+ const rule: Rule<any, any> = {
125
+ id: "id123",
126
+ description: "",
127
+ some: [],
128
+ all: [...trueIf_0_conditions],
129
+ onFailure: [],
130
+ onSuccess: [excludeById([DUtil.randomObjectId(), DUtil.randomObjectId()])],
131
+ };
132
+ const f1 = xIs(0);
133
+ const rules = [rule];
134
+ const result = engine.solveAll(rules, [f1]);
135
+ const firstMatch = result.matching[0];
136
+ expect(firstMatch.actionList.length).toBe(1);
137
+ expect(result.matching.length).toBe(1);
138
+ expect(result.errors.length).toBe(0);
139
+ });
140
+
141
+ it("and-rule 0=1 false", () => {
142
+ const facts = [xIs(1)];
143
+ const rule: Rule<any, any> = {
144
+ id: "id123",
145
+
146
+ description: "",
147
+ some: [],
148
+ all: [xCondition("eq", 0)],
149
+ onFailure: [],
150
+ onSuccess: [excludeById([DUtil.randomObjectId(), DUtil.randomObjectId()])],
151
+ };
152
+
153
+ expect(engine.solve(rule, facts)).toEqual(false);
154
+
155
+ const results = engine.solveAll([rule], facts);
156
+ expect(results.matching.length).toBe(0);
157
+ expect(results.errors.length).toBe(0);
158
+ });
159
+
160
+ it("One true some-rule gives true", () => {
161
+ const facts = [xIs(6)];
162
+ const rule: Rule<any, any> = {
163
+ id: "id123",
164
+ description: "",
165
+ some: [xCondition("eq", 6)],
166
+ all: [],
167
+ onFailure: [],
168
+ onSuccess: [excludeById([DUtil.randomObjectId()])],
169
+ };
170
+ expect(engine.solve(rule, facts)).toBe(true);
171
+ const result = engine.solveAll([rule], facts);
172
+ expect(result.matching.length).toBe(1);
173
+ });
174
+
175
+ it("One false some-rule gives false", () => {
176
+ const facts = [xIs(6)];
177
+ const rule: Rule<any, any> = {
178
+ id: "id123",
179
+
180
+ description: "",
181
+ some: [xCondition("eq", 5)],
182
+ all: [],
183
+ onFailure: [],
184
+ onSuccess: [],
185
+ };
186
+ expect(engine.solve(rule, facts)).toBe(false);
187
+ });
188
+
189
+ it("All true all-rules, and one true some-rule -> true", () => {
190
+ const facts = [xIs(9)];
191
+ const rule: Rule<any, any> = {
192
+ id: "id123",
193
+
194
+ description: "",
195
+ some: [xCondition("eq", 5), xCondition("greater-then", 3)],
196
+ all: [xCondition("greater-then", 5), xCondition("eq", 9)],
197
+ onFailure: [],
198
+ onSuccess: [],
199
+ };
200
+ expect(engine.solve(rule, facts)).toBe(true);
201
+ });
202
+
203
+ it("Can solve complex rule [some-2 -> true]", () => {
204
+ const facts = [xIs(9)];
205
+ const rule: Rule<any, any> = {
206
+ id: "id123",
207
+
208
+ description: "",
209
+ some: [
210
+ {
211
+ kind: "complex-condition",
212
+ name: "test-name",
213
+ all: [],
214
+ some: [xCondition("eq", 5), xCondition("greater-then", 3)],
215
+ },
216
+ ],
217
+ all: [],
218
+ onFailure: [],
219
+ onSuccess: [excludeByTag("asdf")],
220
+ };
221
+ expect(engine.solve(rule, facts)).toBe(true);
222
+ const result = engine.solveAll([rule], facts);
223
+ expect(result.matching.length).toBe(1);
224
+ });
225
+ it("Can solve complex rule [some-1 -> false]", () => {
226
+ const facts = [xIs(0)];
227
+ const rule: Rule<any, any> = {
228
+ id: "id123",
229
+
230
+ description: "",
231
+ some: [
232
+ {
233
+ kind: "complex-condition",
234
+ name: "test-name",
235
+ all: [],
236
+ some: [xCondition("eq", 5)],
237
+ },
238
+ ],
239
+ all: [],
240
+ onFailure: [],
241
+ onSuccess: [excludeByTag("xbj")],
242
+ };
243
+ expect(engine.solve(rule, facts)).toBe(false);
244
+ const result = engine.solveAll([rule], facts);
245
+ expect(result.matching.length).toBe(0);
246
+ expect(result.errors.length).toBe(0);
247
+ });
248
+
249
+ it("Can solve complex rule [some-1-true -> true]", () => {
250
+ const facts = [xIs(0)];
251
+ const rule: Rule<any, any> = {
252
+ id: "id123",
253
+
254
+ description: "",
255
+ some: [
256
+ {
257
+ kind: "complex-condition",
258
+ name: "test-name",
259
+ all: [],
260
+ some: [xCondition("greater-then", -5)],
261
+ },
262
+ ],
263
+ all: [],
264
+ onFailure: [],
265
+ onSuccess: [],
266
+ };
267
+ expect(engine.solve(rule, facts)).toBe(true);
268
+ });
269
+
270
+ it("Empty some (nested) returns false (not valid condition)", () => {
271
+ const rule: Rule<any, any> = {
272
+ id: "id123",
273
+
274
+ description: "",
275
+ some: [
276
+ {
277
+ kind: "complex-condition",
278
+ name: "test-name",
279
+ all: [],
280
+ some: [],
281
+ },
282
+ ],
283
+ all: [],
284
+ onFailure: [],
285
+ onSuccess: [],
286
+ };
287
+ expect(engine.solve(rule, [])).toBe(false);
288
+ });
289
+
290
+ it("Empty complex -> false", () => {
291
+ const rule: Rule<any, any> = {
292
+ id: "id123",
293
+
294
+ description: "",
295
+ some: [],
296
+ all: [],
297
+ onFailure: [],
298
+ onSuccess: [],
299
+ };
300
+ expect(engine.solve(rule, [])).toBe(false);
301
+ });
302
+
303
+ it("Complex all 6 true conditions -> true", () => {
304
+ const facts = [xIs(0)];
305
+ const action = excludeById([DUtil.randomObjectId()]);
306
+ const rule: Rule<any, any> = {
307
+ id: "id123",
308
+
309
+ description: "",
310
+ some: [...falseIf_0_simple, ...trueIf_0_simple],
311
+ all: [...trueIf_0_conditions, ...trueIf_0_simple],
312
+ onFailure: [],
313
+ onSuccess: [action],
314
+ };
315
+ // CAN SOLVE
316
+ expect(engine.solve(rule, facts)).toBe(true);
317
+ // engine.addRule(rule);
318
+ // expect(engine.getRules().length).toBe(1);
319
+
320
+ // const lastAction = engine.actionState?.lastAction as HidePageAction;
321
+ // expect(lastAction?.pageId).toBe('as1');
322
+ });
323
+
324
+ it("True some, but false all -> true", () => {
325
+ const facts = [xIs(0)];
326
+ const rule: Rule<any, any> = {
327
+ id: "id123",
328
+
329
+ description: "",
330
+ some: [...falseIf_0_simple, ...trueIf_0_simple],
331
+ all: [...trueIf_0_conditions, ...trueIf_0_simple, ...falseIf_0_simple],
332
+ onFailure: [],
333
+ onSuccess: [],
334
+ };
335
+ expect(engine.solve(rule, facts)).toBe(false);
336
+ });
337
+
338
+ it("Empty some && true all -> true", () => {
339
+ const facts = [xIs(0)];
340
+ const rule: Rule<any, any> = {
341
+ id: "id123",
342
+
343
+ description: "",
344
+ some: [],
345
+ all: [...trueIf_0_conditions, ...trueIf_0_simple],
346
+ onFailure: [],
347
+ onSuccess: [],
348
+ };
349
+ expect(engine.solve(rule, facts)).toBe(true);
350
+ });
351
+ });