@media-quest/engine 0.0.22 → 0.0.24

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.
Files changed (51) hide show
  1. package/package.json +1 -1
  2. package/src/Delement/DElement.dto.ts +5 -5
  3. package/src/Delement/DElement.ts +88 -88
  4. package/src/Delement/DImg.ts +39 -39
  5. package/src/Delement/DStyle-utils.ts +616 -616
  6. package/src/Delement/DStyle.ts +165 -165
  7. package/src/Delement/DText.ts +13 -13
  8. package/src/Delement/Ddiv.ts +25 -25
  9. package/src/Delement/button-click-action.ts +35 -35
  10. package/src/Delement/css.spec.ts +36 -36
  11. package/src/Delement/css.ts +46 -46
  12. package/src/Delement/element-factory.ts +40 -40
  13. package/src/common/DMaybe.ts +46 -46
  14. package/src/common/DTimestamp.ts +20 -20
  15. package/src/common/DTmestamp.spec.ts +11 -11
  16. package/src/common/result.ts +41 -41
  17. package/src/engine/SchemaDto.ts +24 -24
  18. package/src/engine/SchemaEngine.ts +150 -150
  19. package/src/engine/SchemaResult.ts +10 -10
  20. package/src/engine/dplayer.spec.ts +91 -91
  21. package/src/engine/dplayer.ts +104 -104
  22. package/src/engine/history-que.spec.ts +67 -67
  23. package/src/engine/history-que.ts +17 -17
  24. package/src/engine/next-que.spec.ts +121 -121
  25. package/src/engine/next-que.ts +101 -101
  26. package/src/engine/page-que-ruleengine-action.ts +6 -6
  27. package/src/engine/scale.spec.ts +38 -38
  28. package/src/engine/scale.ts +70 -70
  29. package/src/events/mq-events.ts +63 -63
  30. package/src/page/Page.ts +182 -182
  31. package/src/page/media-player.ts +117 -117
  32. package/src/page/page-component.ts +113 -113
  33. package/src/page/page-result.ts +11 -11
  34. package/src/page/task-manager.ts +240 -240
  35. package/src/page/task-state.ts +55 -55
  36. package/src/page/task.ts +90 -90
  37. package/src/public-api.ts +26 -26
  38. package/src/rules/__test__/complex-condition.spec.ts +15 -15
  39. package/src/rules/__test__/conditon.spec.ts +124 -124
  40. package/src/rules/__test__/numeric-condition.spec.ts +84 -84
  41. package/src/rules/__test__/rule-engine.spec.ts +348 -348
  42. package/src/rules/__test__/rule-evaluation.spec.ts +140 -140
  43. package/src/rules/__test__/string-condition.spec.ts +41 -41
  44. package/src/rules/condition.ts +191 -191
  45. package/src/rules/fact.ts +18 -18
  46. package/src/rules/rule-engine.ts +45 -45
  47. package/src/rules/rule.ts +40 -40
  48. package/src/utils/DUtil.ts +116 -116
  49. package/src/utils/ID.spec.ts +39 -39
  50. package/src/utils/ID.ts +73 -73
  51. package/tsconfig.json +19 -19
@@ -1,191 +1,191 @@
1
- import { Fact } from "./fact";
2
- import { DUtil } from "../utils/DUtil";
3
- export type Condition = Condition.String | Condition.Numeric | Condition.Complex;
4
-
5
- export namespace Condition {
6
- export type StringOperator = "eq" | "not-eq" | "longer-then" | "shorter-then";
7
-
8
- export type NumericOperator =
9
- | "eq"
10
- | "not-eq"
11
- | "greater-then"
12
- | "less-then"
13
- | "greater-then-inclusive"
14
- | "less-then-inclusive";
15
-
16
- export interface Numeric {
17
- readonly referenceId: string;
18
- readonly referenceLabel: string;
19
- readonly valueLabel: string;
20
- readonly kind: "numeric-condition";
21
- readonly operator: NumericOperator;
22
- readonly value: number;
23
- }
24
- export interface String {
25
- readonly referenceId: string;
26
- readonly referenceLabel: string;
27
- readonly valueLabel: string;
28
- readonly kind: "string-condition";
29
- readonly operator: StringOperator;
30
- readonly value: string;
31
- }
32
-
33
- export interface Complex {
34
- readonly kind: "complex-condition";
35
- readonly name: string;
36
- readonly all: ReadonlyArray<Condition.Simple>;
37
- readonly some: ReadonlyArray<Condition.Simple>;
38
- }
39
-
40
- export type Simple = Condition.String | Condition.Numeric;
41
-
42
- /**
43
- * An empty condition will evaluate to false,
44
- * @param condition: Condition.Any
45
- * @param facts
46
- */
47
- export const evaluate = (condition: Condition, facts: ReadonlyArray<Fact>) => {
48
- let result: boolean = false;
49
- switch (condition.kind) {
50
- case "string-condition":
51
- result = evaluateSimple(condition, facts);
52
- break;
53
- case "numeric-condition":
54
- result = evaluateSimple(condition, facts);
55
- break;
56
- case "complex-condition":
57
- result = evaluateComplex(condition, facts);
58
- break;
59
- default:
60
- const check: never = condition;
61
- }
62
- return result;
63
- };
64
-
65
- const evaluateComplex = (condition: Condition.Complex, facts: ReadonlyArray<Fact>): boolean => {
66
- if (condition.some.length === 0 && condition.all.length === 0) {
67
- return false;
68
- }
69
- const allSolved = condition.all.map((condition) => {
70
- return evaluateSimple(condition, facts);
71
- });
72
-
73
- const someEvaluated = condition.some.map((condition) => {
74
- return evaluateSimple(condition, facts);
75
- });
76
- const allResult = allSolved.every(DUtil.isTrue);
77
- const someResult = someEvaluated.length === 0 || someEvaluated.some(DUtil.isTrue);
78
- return someResult && allResult;
79
- };
80
-
81
- const evaluateSimple = (condition: Condition.Simple, facts: ReadonlyArray<Fact>): boolean => {
82
- const fact = facts.find((f) => f.referenceId === condition.referenceId);
83
- if (!fact) {
84
- return false;
85
- }
86
- let res = false;
87
- switch (condition.kind) {
88
- case "numeric-condition":
89
- if (fact.kind === "numeric-fact") {
90
- res = evaluateNumeric(condition, fact.value);
91
- }
92
- break;
93
- case "string-condition":
94
- if (fact.kind === "string-fact") {
95
- res = evaluateString(condition, fact.value);
96
- }
97
- break;
98
- default:
99
- const check: never = condition;
100
- }
101
- return res;
102
- };
103
-
104
- export const isEmpty = (complex: Complex) => {
105
- return complex.all.length === 0 && complex.some.length === 0;
106
- };
107
-
108
- const evaluateString = (condition: Readonly<Condition.String>, value: string): boolean => {
109
- const operator = condition.operator;
110
- let result = false;
111
- switch (operator) {
112
- case "eq":
113
- result = condition.value === value;
114
- break;
115
- case "not-eq":
116
- result = condition.value !== value;
117
- break;
118
- case "shorter-then":
119
- result = condition.value !== value;
120
- break;
121
- case "longer-then":
122
- result = condition.value !== value;
123
- break;
124
- default:
125
- const check: never = operator;
126
- }
127
- return result;
128
- };
129
-
130
- const evaluateNumeric = (condition: Numeric, value: number): boolean => {
131
- const op = condition.operator;
132
- const conditionValue = condition.value;
133
- let result = false;
134
- switch (op) {
135
- case "eq":
136
- result = value === conditionValue;
137
- break;
138
- case "not-eq":
139
- result = value !== conditionValue;
140
- break;
141
- case "greater-then":
142
- result = value > conditionValue;
143
- break;
144
- case "greater-then-inclusive":
145
- result = value >= conditionValue;
146
- break;
147
- case "less-then":
148
- result = value < conditionValue;
149
- break;
150
- case "less-then-inclusive":
151
- result = value <= conditionValue;
152
- break;
153
- default:
154
- const check: never = op;
155
- }
156
- return result;
157
- };
158
-
159
- const _getAllSimple = (condition: Condition): ReadonlyArray<Condition.Simple> => {
160
- const simple: Array<Condition.Simple> = [];
161
- switch (condition.kind) {
162
- case "complex-condition":
163
- simple.push(...condition.all);
164
- simple.push(...condition.some);
165
- break;
166
- case "numeric-condition":
167
- simple.push(condition);
168
- break;
169
- case "string-condition":
170
- simple.push(condition);
171
- break;
172
- default:
173
- DUtil.neverCheck(condition);
174
- }
175
- return simple;
176
- };
177
-
178
- export const getAllSimpleConditions = (
179
- condition: Condition | Array<Condition>
180
- ): ReadonlyArray<Condition.Simple> => {
181
- const simple: Array<Condition.Simple> = [];
182
- if (Array.isArray(condition)) {
183
- condition.forEach((c) => {
184
- simple.push(..._getAllSimple(c));
185
- });
186
- } else {
187
- simple.push(..._getAllSimple(condition));
188
- }
189
- return simple;
190
- };
191
- }
1
+ import { Fact } from "./fact";
2
+ import { DUtil } from "../utils/DUtil";
3
+ export type Condition = Condition.String | Condition.Numeric | Condition.Complex;
4
+
5
+ export namespace Condition {
6
+ export type StringOperator = "eq" | "not-eq" | "longer-then" | "shorter-then";
7
+
8
+ export type NumericOperator =
9
+ | "eq"
10
+ | "not-eq"
11
+ | "greater-then"
12
+ | "less-then"
13
+ | "greater-then-inclusive"
14
+ | "less-then-inclusive";
15
+
16
+ export interface Numeric {
17
+ readonly referenceId: string;
18
+ readonly referenceLabel: string;
19
+ readonly valueLabel: string;
20
+ readonly kind: "numeric-condition";
21
+ readonly operator: NumericOperator;
22
+ readonly value: number;
23
+ }
24
+ export interface String {
25
+ readonly referenceId: string;
26
+ readonly referenceLabel: string;
27
+ readonly valueLabel: string;
28
+ readonly kind: "string-condition";
29
+ readonly operator: StringOperator;
30
+ readonly value: string;
31
+ }
32
+
33
+ export interface Complex {
34
+ readonly kind: "complex-condition";
35
+ readonly name: string;
36
+ readonly all: ReadonlyArray<Condition.Simple>;
37
+ readonly some: ReadonlyArray<Condition.Simple>;
38
+ }
39
+
40
+ export type Simple = Condition.String | Condition.Numeric;
41
+
42
+ /**
43
+ * An empty condition will evaluate to false,
44
+ * @param condition: Condition.Any
45
+ * @param facts
46
+ */
47
+ export const evaluate = (condition: Condition, facts: ReadonlyArray<Fact>) => {
48
+ let result: boolean = false;
49
+ switch (condition.kind) {
50
+ case "string-condition":
51
+ result = evaluateSimple(condition, facts);
52
+ break;
53
+ case "numeric-condition":
54
+ result = evaluateSimple(condition, facts);
55
+ break;
56
+ case "complex-condition":
57
+ result = evaluateComplex(condition, facts);
58
+ break;
59
+ default:
60
+ const check: never = condition;
61
+ }
62
+ return result;
63
+ };
64
+
65
+ const evaluateComplex = (condition: Condition.Complex, facts: ReadonlyArray<Fact>): boolean => {
66
+ if (condition.some.length === 0 && condition.all.length === 0) {
67
+ return false;
68
+ }
69
+ const allSolved = condition.all.map((condition) => {
70
+ return evaluateSimple(condition, facts);
71
+ });
72
+
73
+ const someEvaluated = condition.some.map((condition) => {
74
+ return evaluateSimple(condition, facts);
75
+ });
76
+ const allResult = allSolved.every(DUtil.isTrue);
77
+ const someResult = someEvaluated.length === 0 || someEvaluated.some(DUtil.isTrue);
78
+ return someResult && allResult;
79
+ };
80
+
81
+ const evaluateSimple = (condition: Condition.Simple, facts: ReadonlyArray<Fact>): boolean => {
82
+ const fact = facts.find((f) => f.referenceId === condition.referenceId);
83
+ if (!fact) {
84
+ return false;
85
+ }
86
+ let res = false;
87
+ switch (condition.kind) {
88
+ case "numeric-condition":
89
+ if (fact.kind === "numeric-fact") {
90
+ res = evaluateNumeric(condition, fact.value);
91
+ }
92
+ break;
93
+ case "string-condition":
94
+ if (fact.kind === "string-fact") {
95
+ res = evaluateString(condition, fact.value);
96
+ }
97
+ break;
98
+ default:
99
+ const check: never = condition;
100
+ }
101
+ return res;
102
+ };
103
+
104
+ export const isEmpty = (complex: Complex) => {
105
+ return complex.all.length === 0 && complex.some.length === 0;
106
+ };
107
+
108
+ const evaluateString = (condition: Readonly<Condition.String>, value: string): boolean => {
109
+ const operator = condition.operator;
110
+ let result = false;
111
+ switch (operator) {
112
+ case "eq":
113
+ result = condition.value === value;
114
+ break;
115
+ case "not-eq":
116
+ result = condition.value !== value;
117
+ break;
118
+ case "shorter-then":
119
+ result = condition.value !== value;
120
+ break;
121
+ case "longer-then":
122
+ result = condition.value !== value;
123
+ break;
124
+ default:
125
+ const check: never = operator;
126
+ }
127
+ return result;
128
+ };
129
+
130
+ const evaluateNumeric = (condition: Numeric, value: number): boolean => {
131
+ const op = condition.operator;
132
+ const conditionValue = condition.value;
133
+ let result = false;
134
+ switch (op) {
135
+ case "eq":
136
+ result = value === conditionValue;
137
+ break;
138
+ case "not-eq":
139
+ result = value !== conditionValue;
140
+ break;
141
+ case "greater-then":
142
+ result = value > conditionValue;
143
+ break;
144
+ case "greater-then-inclusive":
145
+ result = value >= conditionValue;
146
+ break;
147
+ case "less-then":
148
+ result = value < conditionValue;
149
+ break;
150
+ case "less-then-inclusive":
151
+ result = value <= conditionValue;
152
+ break;
153
+ default:
154
+ const check: never = op;
155
+ }
156
+ return result;
157
+ };
158
+
159
+ const _getAllSimple = (condition: Condition): ReadonlyArray<Condition.Simple> => {
160
+ const simple: Array<Condition.Simple> = [];
161
+ switch (condition.kind) {
162
+ case "complex-condition":
163
+ simple.push(...condition.all);
164
+ simple.push(...condition.some);
165
+ break;
166
+ case "numeric-condition":
167
+ simple.push(condition);
168
+ break;
169
+ case "string-condition":
170
+ simple.push(condition);
171
+ break;
172
+ default:
173
+ DUtil.neverCheck(condition);
174
+ }
175
+ return simple;
176
+ };
177
+
178
+ export const getAllSimpleConditions = (
179
+ condition: Condition | Array<Condition>
180
+ ): ReadonlyArray<Condition.Simple> => {
181
+ const simple: Array<Condition.Simple> = [];
182
+ if (Array.isArray(condition)) {
183
+ condition.forEach((c) => {
184
+ simple.push(..._getAllSimple(c));
185
+ });
186
+ } else {
187
+ simple.push(..._getAllSimple(condition));
188
+ }
189
+ return simple;
190
+ };
191
+ }
package/src/rules/fact.ts CHANGED
@@ -1,18 +1,18 @@
1
- export type Fact = Fact.Numeric | Fact.String;
2
- export namespace Fact {
3
- export interface Numeric {
4
- readonly kind: "numeric-fact";
5
- readonly value: number;
6
- readonly label: string;
7
- readonly referenceId: string;
8
- readonly referenceLabel: string;
9
- }
10
-
11
- export interface String {
12
- readonly kind: "string-fact";
13
- readonly label: string;
14
- readonly value: string;
15
- readonly referenceId: string;
16
- readonly referenceLabel: string;
17
- }
18
- }
1
+ export type Fact = Fact.Numeric | Fact.String;
2
+ export namespace Fact {
3
+ export interface Numeric {
4
+ readonly kind: "numeric-fact";
5
+ readonly value: number;
6
+ readonly label: string;
7
+ readonly referenceId: string;
8
+ readonly referenceLabel: string;
9
+ }
10
+
11
+ export interface String {
12
+ readonly kind: "string-fact";
13
+ readonly label: string;
14
+ readonly value: string;
15
+ readonly referenceId: string;
16
+ readonly referenceLabel: string;
17
+ }
18
+ }
@@ -1,45 +1,45 @@
1
- import { Fact } from "./fact";
2
- import { Rule } from "./rule";
3
-
4
- export interface SolveResult<S, F> {
5
- matching: ReadonlyArray<Match<S, F>>;
6
- errors: ReadonlyArray<RuleEngineError>;
7
- }
8
-
9
- export interface Match<S, F> {
10
- readonly matchingRuleId: string;
11
- readonly ruleDescription: string;
12
- readonly actionList: ReadonlyArray<S> | ReadonlyArray<F>;
13
- }
14
-
15
- export interface RuleEngineError {
16
- readonly kind?: string;
17
- readonly message: string;
18
- }
19
-
20
- export class RuleEngine<S, F> {
21
- constructor() {}
22
-
23
- solveAll(rules: Rule<S, F>[], facts: Fact[]): SolveResult<S, F> {
24
- const errors: RuleEngineError[] = [];
25
- const matching: Match<S, F>[] = [];
26
- rules.forEach((rule) => {
27
- if (Rule.isEmpty(rule)) {
28
- errors.push({ message: "Empty rule: " + rule.id });
29
- } else if (Rule.solve(rule, facts)) {
30
- const match: Match<S, F> = {
31
- ruleDescription: rule.description,
32
- matchingRuleId: rule.id ?? "no-id-given",
33
- actionList: [...rule.onSuccess],
34
- };
35
- matching.push(match);
36
- }
37
- });
38
- return { matching, errors };
39
- }
40
-
41
- solve(rule: Rule<S, F>, facts: Fact[]): boolean {
42
- // TODO Validate, and Return result
43
- return Rule.solve(rule, facts);
44
- }
45
- }
1
+ import { Fact } from "./fact";
2
+ import { Rule } from "./rule";
3
+
4
+ export interface SolveResult<S, F> {
5
+ matching: ReadonlyArray<Match<S, F>>;
6
+ errors: ReadonlyArray<RuleEngineError>;
7
+ }
8
+
9
+ export interface Match<S, F> {
10
+ readonly matchingRuleId: string;
11
+ readonly ruleDescription: string;
12
+ readonly actionList: ReadonlyArray<S> | ReadonlyArray<F>;
13
+ }
14
+
15
+ export interface RuleEngineError {
16
+ readonly kind?: string;
17
+ readonly message: string;
18
+ }
19
+
20
+ export class RuleEngine<S, F> {
21
+ constructor() {}
22
+
23
+ solveAll(rules: Rule<S, F>[], facts: Fact[]): SolveResult<S, F> {
24
+ const errors: RuleEngineError[] = [];
25
+ const matching: Match<S, F>[] = [];
26
+ rules.forEach((rule) => {
27
+ if (Rule.isEmpty(rule)) {
28
+ errors.push({ message: "Empty rule: " + rule.id });
29
+ } else if (Rule.solve(rule, facts)) {
30
+ const match: Match<S, F> = {
31
+ ruleDescription: rule.description,
32
+ matchingRuleId: rule.id ?? "no-id-given",
33
+ actionList: [...rule.onSuccess],
34
+ };
35
+ matching.push(match);
36
+ }
37
+ });
38
+ return { matching, errors };
39
+ }
40
+
41
+ solve(rule: Rule<S, F>, facts: Fact[]): boolean {
42
+ // TODO Validate, and Return result
43
+ return Rule.solve(rule, facts);
44
+ }
45
+ }
package/src/rules/rule.ts CHANGED
@@ -1,40 +1,40 @@
1
- import { Condition } from "./condition";
2
- import { Fact } from "./fact";
3
- import { DUtil } from "../utils/DUtil";
4
-
5
- export interface Rule<OnSuccessAction, OnFailureAction> {
6
- readonly id?: string;
7
- readonly description: string;
8
- readonly all: ReadonlyArray<Condition>;
9
- readonly some: ReadonlyArray<Condition>;
10
- readonly onSuccess: ReadonlyArray<OnSuccessAction>;
11
- readonly onFailure: ReadonlyArray<OnFailureAction>;
12
- }
13
-
14
- export namespace Rule {
15
- /**
16
- * Validates that the rule is valid.
17
- * @param rule
18
- */
19
-
20
- export const isEmpty = (rule: Rule<any, any>): boolean => {
21
- const emptyConditions = rule.all.length === 0 && rule.some.length === 0;
22
- const emptyActions = rule.onSuccess.length === 0 && rule.onFailure.length === 0;
23
- return emptyConditions || emptyActions;
24
- };
25
-
26
- export const solve = (rule: Rule<any, any>, facts: ReadonlyArray<Fact>): boolean => {
27
- if (rule.some.length === 0 && rule.all.length === 0) {
28
- // TODO RETURN WARNING? OR LOGGING ?
29
- return false;
30
- }
31
-
32
- const someSolved = rule.some.map((condition) => Condition.evaluate(condition, facts));
33
-
34
- const someResult = someSolved.length === 0 || someSolved.some(DUtil.isTrue);
35
-
36
- const allSolved = rule.all.map((condition) => Condition.evaluate(condition, facts)).every(DUtil.isTrue);
37
-
38
- return allSolved && someResult;
39
- };
40
- }
1
+ import { Condition } from "./condition";
2
+ import { Fact } from "./fact";
3
+ import { DUtil } from "../utils/DUtil";
4
+
5
+ export interface Rule<OnSuccessAction, OnFailureAction> {
6
+ readonly id?: string;
7
+ readonly description: string;
8
+ readonly all: ReadonlyArray<Condition>;
9
+ readonly some: ReadonlyArray<Condition>;
10
+ readonly onSuccess: ReadonlyArray<OnSuccessAction>;
11
+ readonly onFailure: ReadonlyArray<OnFailureAction>;
12
+ }
13
+
14
+ export namespace Rule {
15
+ /**
16
+ * Validates that the rule is valid.
17
+ * @param rule
18
+ */
19
+
20
+ export const isEmpty = (rule: Rule<any, any>): boolean => {
21
+ const emptyConditions = rule.all.length === 0 && rule.some.length === 0;
22
+ const emptyActions = rule.onSuccess.length === 0 && rule.onFailure.length === 0;
23
+ return emptyConditions || emptyActions;
24
+ };
25
+
26
+ export const solve = (rule: Rule<any, any>, facts: ReadonlyArray<Fact>): boolean => {
27
+ if (rule.some.length === 0 && rule.all.length === 0) {
28
+ // TODO RETURN WARNING? OR LOGGING ?
29
+ return false;
30
+ }
31
+
32
+ const someSolved = rule.some.map((condition) => Condition.evaluate(condition, facts));
33
+
34
+ const someResult = someSolved.length === 0 || someSolved.some(DUtil.isTrue);
35
+
36
+ const allSolved = rule.all.map((condition) => Condition.evaluate(condition, facts)).every(DUtil.isTrue);
37
+
38
+ return allSolved && someResult;
39
+ };
40
+ }