@media-quest/builder 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.
- package/package.json +1 -1
- package/src/Builder-option.ts +66 -66
- package/src/Builder-page.spec.ts +320 -320
- package/src/Builder-page.ts +257 -257
- package/src/Builder-question.spec.ts +68 -68
- package/src/Builder-question.ts +101 -101
- package/src/Builder-schema.spec.ts +51 -0
- package/src/Builder-schema.ts +47 -15
- package/src/Builder-text.spec.ts +24 -24
- package/src/Builder-text.ts +57 -57
- package/src/BuilderMainImageDto.ts +7 -7
- package/src/BuilderMainText.ts +81 -81
- package/src/BuilderMainVideoDto.ts +10 -10
- package/src/BuilderObject.ts +61 -61
- package/src/BuilderTag.ts +97 -97
- package/src/builder-compiler.ts +14 -0
- package/src/codebook.ts +72 -72
- package/src/media-files.ts +28 -28
- package/src/primitives/page-prefix.ts +58 -58
- package/src/primitives/prefix.spec.ts +5 -5
- package/src/primitives/schema-prefix.ts +52 -52
- package/src/primitives/varID.ts +11 -11
- package/src/public-api.ts +3 -1
- package/src/rulebuilder/Builder-rule.spec.ts +322 -322
- package/src/rulebuilder/Builder-rule.ts +190 -190
- package/src/rulebuilder/RuleAction.ts +106 -106
- package/src/rulebuilder/RuleBuilder-test-utils.ts +316 -316
- package/src/rulebuilder/RuleInput.ts +44 -44
- package/src/rulebuilder/RuleVariable.ts +49 -49
- package/src/rulebuilder/SingleSelectItem.ts +135 -135
- package/src/rulebuilder/condition/Builder-condition-group.spec.ts +47 -47
- package/src/rulebuilder/condition/Builder-condition-group.ts +118 -118
- package/src/rulebuilder/condition/Builder-condition.spec.ts +195 -195
- package/src/rulebuilder/condition/Builder-condition.ts +208 -208
- package/src/rulebuilder/condition/Builder-operator.spec.ts +9 -9
- package/src/rulebuilder/condition/Builder-operator.ts +31 -31
- package/src/rulebuilder/index.ts +22 -22
- package/src/rulebuilder/jump-to-action-manager.ts +33 -33
- package/src/rulebuilder/multi-select-item.ts +73 -73
- package/src/rulebuilder/page-action-manager.ts +31 -31
- package/src/rulebuilder/rule2/Rule2.ts +211 -211
- package/src/rulebuilder/tag-action-manager.spec.ts +44 -44
- package/src/rulebuilder/tag-action-manager.ts +28 -28
- package/src/schema-config.ts +25 -25
- package/src/theme/AbstractThemeCompiler.ts +7 -7
- package/src/theme/IDefaultTheme.ts +226 -226
- package/src/theme/css-theme.ts +7 -7
- package/src/theme/default-theme-compiler.ts +358 -358
- package/src/theme/icon-urls.ts +29 -29
- package/src/theme/theme-utils.ts +57 -57
- package/src/theme/theme1.spec.ts +52 -52
- package/src/variable/mq-variable.spec.ts +146 -0
- package/src/{mq-variable.ts → variable/mq-variable.ts} +8 -1
- package/src/variable/sum-score.ts +138 -0
- package/tsconfig.json +15 -15
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1,73 +1,73 @@
|
|
|
1
|
-
import { ExcludeByPageAction, ExcludeByTagAction } from "./RuleAction";
|
|
2
|
-
|
|
3
|
-
export abstract class MultiSelectItem<T> {
|
|
4
|
-
private readonly _isSelectedInitially: boolean;
|
|
5
|
-
private readonly _selectLabel;
|
|
6
|
-
private readonly _toolTip;
|
|
7
|
-
private readonly _searchString;
|
|
8
|
-
public isSelected: boolean;
|
|
9
|
-
get selectLabel(): string {
|
|
10
|
-
return this._selectLabel;
|
|
11
|
-
}
|
|
12
|
-
get tooltip() {
|
|
13
|
-
return this._toolTip;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
get searchString() {
|
|
17
|
-
return this._searchString;
|
|
18
|
-
}
|
|
19
|
-
protected constructor(
|
|
20
|
-
readonly data: T,
|
|
21
|
-
isSelected: boolean,
|
|
22
|
-
) {
|
|
23
|
-
this._isSelectedInitially = isSelected;
|
|
24
|
-
this.isSelected = isSelected;
|
|
25
|
-
this._searchString = this.getSearchString();
|
|
26
|
-
this._toolTip = this.getTooltip();
|
|
27
|
-
this._selectLabel = this.getSelectLabel();
|
|
28
|
-
}
|
|
29
|
-
protected abstract getSelectLabel(): string;
|
|
30
|
-
protected abstract getTooltip(): string;
|
|
31
|
-
protected abstract getSearchString(): string;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export class ExcludeByTagSelectItem extends MultiSelectItem<ExcludeByTagAction> {
|
|
35
|
-
public static readonly create = (tagData: ExcludeByTagAction, isSelected: boolean) => {
|
|
36
|
-
return new ExcludeByTagSelectItem(tagData, isSelected);
|
|
37
|
-
};
|
|
38
|
-
protected constructor(data: ExcludeByTagAction, isSelected: boolean) {
|
|
39
|
-
super(data, isSelected);
|
|
40
|
-
}
|
|
41
|
-
protected getSearchString(): string {
|
|
42
|
-
return this.data.tag;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
protected getSelectLabel(): string {
|
|
46
|
-
return this.data.tag + " (" + this.data.pageCount + ")";
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
protected getTooltip(): string {
|
|
50
|
-
return this.data.tag + " (used in " + this.data.pageCount + " pages)";
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export class ExcludeByPageIdSelectItem extends MultiSelectItem<ExcludeByPageAction> {
|
|
55
|
-
public static create = (ruleActionPage: ExcludeByPageAction, isSelected: boolean) => {
|
|
56
|
-
return new ExcludeByPageIdSelectItem(ruleActionPage, isSelected);
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
protected constructor(data: ExcludeByPageAction, isSelected: boolean) {
|
|
60
|
-
super(data, isSelected);
|
|
61
|
-
}
|
|
62
|
-
protected getSearchString(): string {
|
|
63
|
-
return this.data.pagePrefix + this.data.mainText;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
protected getSelectLabel(): string {
|
|
67
|
-
return this.data.pagePrefix + " (" + this.data.pageNumber + ")";
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
protected getTooltip(): string {
|
|
71
|
-
return this.data.mainText;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
1
|
+
import { ExcludeByPageAction, ExcludeByTagAction } from "./RuleAction";
|
|
2
|
+
|
|
3
|
+
export abstract class MultiSelectItem<T> {
|
|
4
|
+
private readonly _isSelectedInitially: boolean;
|
|
5
|
+
private readonly _selectLabel;
|
|
6
|
+
private readonly _toolTip;
|
|
7
|
+
private readonly _searchString;
|
|
8
|
+
public isSelected: boolean;
|
|
9
|
+
get selectLabel(): string {
|
|
10
|
+
return this._selectLabel;
|
|
11
|
+
}
|
|
12
|
+
get tooltip() {
|
|
13
|
+
return this._toolTip;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
get searchString() {
|
|
17
|
+
return this._searchString;
|
|
18
|
+
}
|
|
19
|
+
protected constructor(
|
|
20
|
+
readonly data: T,
|
|
21
|
+
isSelected: boolean,
|
|
22
|
+
) {
|
|
23
|
+
this._isSelectedInitially = isSelected;
|
|
24
|
+
this.isSelected = isSelected;
|
|
25
|
+
this._searchString = this.getSearchString();
|
|
26
|
+
this._toolTip = this.getTooltip();
|
|
27
|
+
this._selectLabel = this.getSelectLabel();
|
|
28
|
+
}
|
|
29
|
+
protected abstract getSelectLabel(): string;
|
|
30
|
+
protected abstract getTooltip(): string;
|
|
31
|
+
protected abstract getSearchString(): string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export class ExcludeByTagSelectItem extends MultiSelectItem<ExcludeByTagAction> {
|
|
35
|
+
public static readonly create = (tagData: ExcludeByTagAction, isSelected: boolean) => {
|
|
36
|
+
return new ExcludeByTagSelectItem(tagData, isSelected);
|
|
37
|
+
};
|
|
38
|
+
protected constructor(data: ExcludeByTagAction, isSelected: boolean) {
|
|
39
|
+
super(data, isSelected);
|
|
40
|
+
}
|
|
41
|
+
protected getSearchString(): string {
|
|
42
|
+
return this.data.tag;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
protected getSelectLabel(): string {
|
|
46
|
+
return this.data.tag + " (" + this.data.pageCount + ")";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
protected getTooltip(): string {
|
|
50
|
+
return this.data.tag + " (used in " + this.data.pageCount + " pages)";
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export class ExcludeByPageIdSelectItem extends MultiSelectItem<ExcludeByPageAction> {
|
|
55
|
+
public static create = (ruleActionPage: ExcludeByPageAction, isSelected: boolean) => {
|
|
56
|
+
return new ExcludeByPageIdSelectItem(ruleActionPage, isSelected);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
protected constructor(data: ExcludeByPageAction, isSelected: boolean) {
|
|
60
|
+
super(data, isSelected);
|
|
61
|
+
}
|
|
62
|
+
protected getSearchString(): string {
|
|
63
|
+
return this.data.pagePrefix + this.data.mainText;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
protected getSelectLabel(): string {
|
|
67
|
+
return this.data.pagePrefix + " (" + this.data.pageNumber + ")";
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
protected getTooltip(): string {
|
|
71
|
+
return this.data.mainText;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import type { RuleInput } from "./RuleInput";
|
|
2
|
-
import { ExcludeByPageIdSelectItem } from "./multi-select-item";
|
|
3
|
-
import { ExcludeByPageAction } from "./RuleAction";
|
|
4
|
-
import { PageID } from "@media-quest/engine";
|
|
5
|
-
|
|
6
|
-
export class PageActionManager {
|
|
7
|
-
private readonly _initialSelection: Set<string>;
|
|
8
|
-
readonly selectItems: ReadonlyArray<ExcludeByPageIdSelectItem>;
|
|
9
|
-
|
|
10
|
-
constructor(
|
|
11
|
-
readonly validOptions: RuleInput["_pageIdActions"],
|
|
12
|
-
readonly initialSelection: ReadonlyArray<string>,
|
|
13
|
-
) {
|
|
14
|
-
this._initialSelection = new Set([...initialSelection]);
|
|
15
|
-
this.selectItems = validOptions.map((opt) => {
|
|
16
|
-
const isSelected = this._initialSelection.has(opt.pageId);
|
|
17
|
-
return ExcludeByPageIdSelectItem.create(opt, isSelected);
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
getCurrentSelection(): ReadonlyArray<PageID> {
|
|
22
|
-
const selected = this.selectItems.filter((item) => item.isSelected).map((itm) => itm.data.pageId);
|
|
23
|
-
return selected;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
getEngineAction(): ReadonlyArray<ExcludeByPageAction> {
|
|
27
|
-
const selectItems = this.selectItems.filter((item) => item.isSelected);
|
|
28
|
-
const actions = selectItems.map((item) => item.data);
|
|
29
|
-
return [...actions];
|
|
30
|
-
}
|
|
31
|
-
}
|
|
1
|
+
import type { RuleInput } from "./RuleInput";
|
|
2
|
+
import { ExcludeByPageIdSelectItem } from "./multi-select-item";
|
|
3
|
+
import { ExcludeByPageAction } from "./RuleAction";
|
|
4
|
+
import { PageID } from "@media-quest/engine";
|
|
5
|
+
|
|
6
|
+
export class PageActionManager {
|
|
7
|
+
private readonly _initialSelection: Set<string>;
|
|
8
|
+
readonly selectItems: ReadonlyArray<ExcludeByPageIdSelectItem>;
|
|
9
|
+
|
|
10
|
+
constructor(
|
|
11
|
+
readonly validOptions: RuleInput["_pageIdActions"],
|
|
12
|
+
readonly initialSelection: ReadonlyArray<string>,
|
|
13
|
+
) {
|
|
14
|
+
this._initialSelection = new Set([...initialSelection]);
|
|
15
|
+
this.selectItems = validOptions.map((opt) => {
|
|
16
|
+
const isSelected = this._initialSelection.has(opt.pageId);
|
|
17
|
+
return ExcludeByPageIdSelectItem.create(opt, isSelected);
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
getCurrentSelection(): ReadonlyArray<PageID> {
|
|
22
|
+
const selected = this.selectItems.filter((item) => item.isSelected).map((itm) => itm.data.pageId);
|
|
23
|
+
return selected;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
getEngineAction(): ReadonlyArray<ExcludeByPageAction> {
|
|
27
|
+
const selectItems = this.selectItems.filter((item) => item.isSelected);
|
|
28
|
+
const actions = selectItems.map((item) => item.data);
|
|
29
|
+
return [...actions];
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -1,211 +1,211 @@
|
|
|
1
|
-
import { BuilderOperator } from "../condition/Builder-operator";
|
|
2
|
-
import { BuilderConditionDto } from "../condition/Builder-condition";
|
|
3
|
-
import { BuilderConditionGroupDto, ConditionGroupType } from "../condition/Builder-condition-group";
|
|
4
|
-
import { BuilderRuleDto } from "../Builder-rule";
|
|
5
|
-
|
|
6
|
-
type SolveErrorReason =
|
|
7
|
-
| "INVALID_FACT_TYPE"
|
|
8
|
-
| "INVALID_OPERATOR"
|
|
9
|
-
| "INVALID_VALUE"
|
|
10
|
-
| "INVALID_VARIABLE"
|
|
11
|
-
| "UNIMPLEMENTED_VARIABLE_TYPE"
|
|
12
|
-
| "UNIMPLEMENTED_OPERATOR";
|
|
13
|
-
|
|
14
|
-
type TrueResult = { readonly type: "IS_TRUE" };
|
|
15
|
-
type FalseResult = { readonly type: "IS_FALSE" };
|
|
16
|
-
type MissingFactsResult = { readonly type: "MISSING_FACTS"; readonly missingVariables: ReadonlyArray<string> };
|
|
17
|
-
type ErrorResult = {
|
|
18
|
-
readonly type: "HAS_ERROR";
|
|
19
|
-
readonly reason: SolveErrorReason;
|
|
20
|
-
readonly data: Record<string, string>;
|
|
21
|
-
};
|
|
22
|
-
export type EvalResult = FalseResult | TrueResult | MissingFactsResult | ErrorResult;
|
|
23
|
-
|
|
24
|
-
export interface Fact2 {
|
|
25
|
-
readonly variableType: "numeric";
|
|
26
|
-
readonly value: number;
|
|
27
|
-
readonly valueLabel: string;
|
|
28
|
-
readonly variableId: string;
|
|
29
|
-
readonly variableLabel: string;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export class FactCollection {
|
|
33
|
-
public static isFact = (value: unknown): value is Fact2 => {
|
|
34
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
35
|
-
return false;
|
|
36
|
-
}
|
|
37
|
-
const fact = value as Partial<Fact2>;
|
|
38
|
-
if (typeof fact.variableId !== "string" || fact.variableId.length === 0) {
|
|
39
|
-
return false;
|
|
40
|
-
}
|
|
41
|
-
if (typeof fact.variableLabel !== "string") {
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
if (typeof fact.value !== "number") {
|
|
45
|
-
return false;
|
|
46
|
-
}
|
|
47
|
-
if (typeof fact.valueLabel !== "string") {
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
// NB: This is a temporary check until we have more variable types.
|
|
51
|
-
if (typeof fact.variableType !== "string" || fact.variableType !== "numeric") {
|
|
52
|
-
return false;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return true;
|
|
56
|
-
};
|
|
57
|
-
public static create = (facts: ReadonlyArray<Fact2>): FactCollection => {
|
|
58
|
-
return new FactCollection(facts);
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
private readonly _facts: ReadonlyArray<Fact2>;
|
|
62
|
-
|
|
63
|
-
private constructor(facts: ReadonlyArray<Fact2>) {
|
|
64
|
-
if (!Array.isArray(facts)) {
|
|
65
|
-
console.log("Invalid facts", facts);
|
|
66
|
-
this._facts = [];
|
|
67
|
-
} else {
|
|
68
|
-
this._facts = [...facts];
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
byId(variableId: string): Fact2 | false {
|
|
72
|
-
const result = this._facts.find((fact) => fact.variableId === variableId);
|
|
73
|
-
if (!result) {
|
|
74
|
-
return false;
|
|
75
|
-
}
|
|
76
|
-
return { ...result };
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
interface FactEvaluator {
|
|
80
|
-
isTrue(facts: FactCollection): boolean;
|
|
81
|
-
evaluate(facts: FactCollection): EvalResult;
|
|
82
|
-
}
|
|
83
|
-
interface IsValid {
|
|
84
|
-
isValid(): boolean;
|
|
85
|
-
}
|
|
86
|
-
export class Condition implements FactEvaluator, IsValid {
|
|
87
|
-
public static create = (dto: BuilderConditionDto): Condition => {
|
|
88
|
-
return new Condition(dto);
|
|
89
|
-
};
|
|
90
|
-
private constructor(private readonly dto: BuilderConditionDto) {}
|
|
91
|
-
isTrue(facts: FactCollection): boolean {
|
|
92
|
-
const dto = this.dto;
|
|
93
|
-
const op = dto.operator;
|
|
94
|
-
const value = dto.value;
|
|
95
|
-
const varId = dto.variableId;
|
|
96
|
-
if (!BuilderOperator.is(op)) {
|
|
97
|
-
return false;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (typeof value !== "number") {
|
|
101
|
-
return false;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const fact = facts.byId(this.dto.variableId);
|
|
105
|
-
|
|
106
|
-
if (!FactCollection.isFact(fact)) {
|
|
107
|
-
return false;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (fact.variableType !== "numeric" && typeof fact.value !== "number") {
|
|
111
|
-
return false;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
if (op === "equal") {
|
|
115
|
-
return fact.value === value;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (op === "notEqual") {
|
|
119
|
-
return fact.value !== value;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return false;
|
|
123
|
-
}
|
|
124
|
-
isValid(): boolean {
|
|
125
|
-
return false;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
evaluate(facts: FactCollection): EvalResult {
|
|
129
|
-
return { type: "HAS_ERROR", reason: "UNIMPLEMENTED_VARIABLE_TYPE", data: {} }; // TODO
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
export class ConditionGroup implements FactEvaluator, IsValid {
|
|
133
|
-
public static readonly create = (dto: BuilderConditionGroupDto): ConditionGroup => {
|
|
134
|
-
return new ConditionGroup(dto);
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
private readonly _conditions: ReadonlyArray<Condition>;
|
|
138
|
-
private constructor(private readonly dto: BuilderConditionGroupDto) {
|
|
139
|
-
this._conditions = dto.conditions.map(Condition.create);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
isTrue(facts: FactCollection): boolean {
|
|
143
|
-
const results = this._conditions.map((condition) => condition.isTrue(facts));
|
|
144
|
-
let trueCount = 0;
|
|
145
|
-
let falseCount = 0;
|
|
146
|
-
results.forEach((results) => {
|
|
147
|
-
if (results) {
|
|
148
|
-
trueCount++;
|
|
149
|
-
} else {
|
|
150
|
-
falseCount++;
|
|
151
|
-
}
|
|
152
|
-
});
|
|
153
|
-
if (trueCount === 0 || falseCount === 0) {
|
|
154
|
-
return false;
|
|
155
|
-
}
|
|
156
|
-
const type = this.dto.type;
|
|
157
|
-
|
|
158
|
-
if (type === "all" && trueCount === this._conditions.length) {
|
|
159
|
-
return true;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if (type === "any" && trueCount > 0) {
|
|
163
|
-
return true;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
const minLimit = this.dto.count;
|
|
167
|
-
if (type === "count" && typeof minLimit === "number" && trueCount >= minLimit) {
|
|
168
|
-
return true;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
return false;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
isValid(): boolean {
|
|
175
|
-
return true;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
evaluate(facts: FactCollection): EvalResult {
|
|
179
|
-
return { type: "HAS_ERROR", reason: "UNIMPLEMENTED_VARIABLE_TYPE", data: {} }; // TODO
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
export class Rule2 implements FactEvaluator {
|
|
183
|
-
readonly name: string;
|
|
184
|
-
private readonly _conditions: ReadonlyArray<Condition | ConditionGroup>;
|
|
185
|
-
public static readonly create = (dto: BuilderRuleDto): Rule2 => {
|
|
186
|
-
return new Rule2(dto);
|
|
187
|
-
};
|
|
188
|
-
private _count = -1;
|
|
189
|
-
constructor(private readonly dto: BuilderRuleDto) {
|
|
190
|
-
this.name = dto.name;
|
|
191
|
-
const conditions: Array<Condition | ConditionGroup> = [];
|
|
192
|
-
dto.conditions.forEach((condition) => {
|
|
193
|
-
if (condition.kind === "condition-group") {
|
|
194
|
-
conditions.push(ConditionGroup.create(condition));
|
|
195
|
-
} else if (condition.kind === "condition") {
|
|
196
|
-
conditions.push(Condition.create(condition));
|
|
197
|
-
} else {
|
|
198
|
-
console.log("Unknown condition", condition);
|
|
199
|
-
}
|
|
200
|
-
});
|
|
201
|
-
this._conditions = conditions;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
isTrue(facts: FactCollection): boolean {
|
|
205
|
-
return false;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
evaluate(facts: FactCollection): EvalResult {
|
|
209
|
-
return { type: "HAS_ERROR", reason: "UNIMPLEMENTED_VARIABLE_TYPE", data: {} }; // TODO
|
|
210
|
-
}
|
|
211
|
-
}
|
|
1
|
+
import { BuilderOperator } from "../condition/Builder-operator";
|
|
2
|
+
import { BuilderConditionDto } from "../condition/Builder-condition";
|
|
3
|
+
import { BuilderConditionGroupDto, ConditionGroupType } from "../condition/Builder-condition-group";
|
|
4
|
+
import { BuilderRuleDto } from "../Builder-rule";
|
|
5
|
+
|
|
6
|
+
type SolveErrorReason =
|
|
7
|
+
| "INVALID_FACT_TYPE"
|
|
8
|
+
| "INVALID_OPERATOR"
|
|
9
|
+
| "INVALID_VALUE"
|
|
10
|
+
| "INVALID_VARIABLE"
|
|
11
|
+
| "UNIMPLEMENTED_VARIABLE_TYPE"
|
|
12
|
+
| "UNIMPLEMENTED_OPERATOR";
|
|
13
|
+
|
|
14
|
+
type TrueResult = { readonly type: "IS_TRUE" };
|
|
15
|
+
type FalseResult = { readonly type: "IS_FALSE" };
|
|
16
|
+
type MissingFactsResult = { readonly type: "MISSING_FACTS"; readonly missingVariables: ReadonlyArray<string> };
|
|
17
|
+
type ErrorResult = {
|
|
18
|
+
readonly type: "HAS_ERROR";
|
|
19
|
+
readonly reason: SolveErrorReason;
|
|
20
|
+
readonly data: Record<string, string>;
|
|
21
|
+
};
|
|
22
|
+
export type EvalResult = FalseResult | TrueResult | MissingFactsResult | ErrorResult;
|
|
23
|
+
|
|
24
|
+
export interface Fact2 {
|
|
25
|
+
readonly variableType: "numeric";
|
|
26
|
+
readonly value: number;
|
|
27
|
+
readonly valueLabel: string;
|
|
28
|
+
readonly variableId: string;
|
|
29
|
+
readonly variableLabel: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export class FactCollection {
|
|
33
|
+
public static isFact = (value: unknown): value is Fact2 => {
|
|
34
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
const fact = value as Partial<Fact2>;
|
|
38
|
+
if (typeof fact.variableId !== "string" || fact.variableId.length === 0) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
if (typeof fact.variableLabel !== "string") {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
if (typeof fact.value !== "number") {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
if (typeof fact.valueLabel !== "string") {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
// NB: This is a temporary check until we have more variable types.
|
|
51
|
+
if (typeof fact.variableType !== "string" || fact.variableType !== "numeric") {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return true;
|
|
56
|
+
};
|
|
57
|
+
public static create = (facts: ReadonlyArray<Fact2>): FactCollection => {
|
|
58
|
+
return new FactCollection(facts);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
private readonly _facts: ReadonlyArray<Fact2>;
|
|
62
|
+
|
|
63
|
+
private constructor(facts: ReadonlyArray<Fact2>) {
|
|
64
|
+
if (!Array.isArray(facts)) {
|
|
65
|
+
console.log("Invalid facts", facts);
|
|
66
|
+
this._facts = [];
|
|
67
|
+
} else {
|
|
68
|
+
this._facts = [...facts];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
byId(variableId: string): Fact2 | false {
|
|
72
|
+
const result = this._facts.find((fact) => fact.variableId === variableId);
|
|
73
|
+
if (!result) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
return { ...result };
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
interface FactEvaluator {
|
|
80
|
+
isTrue(facts: FactCollection): boolean;
|
|
81
|
+
evaluate(facts: FactCollection): EvalResult;
|
|
82
|
+
}
|
|
83
|
+
interface IsValid {
|
|
84
|
+
isValid(): boolean;
|
|
85
|
+
}
|
|
86
|
+
export class Condition implements FactEvaluator, IsValid {
|
|
87
|
+
public static create = (dto: BuilderConditionDto): Condition => {
|
|
88
|
+
return new Condition(dto);
|
|
89
|
+
};
|
|
90
|
+
private constructor(private readonly dto: BuilderConditionDto) {}
|
|
91
|
+
isTrue(facts: FactCollection): boolean {
|
|
92
|
+
const dto = this.dto;
|
|
93
|
+
const op = dto.operator;
|
|
94
|
+
const value = dto.value;
|
|
95
|
+
const varId = dto.variableId;
|
|
96
|
+
if (!BuilderOperator.is(op)) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (typeof value !== "number") {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const fact = facts.byId(this.dto.variableId);
|
|
105
|
+
|
|
106
|
+
if (!FactCollection.isFact(fact)) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (fact.variableType !== "numeric" && typeof fact.value !== "number") {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (op === "equal") {
|
|
115
|
+
return fact.value === value;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (op === "notEqual") {
|
|
119
|
+
return fact.value !== value;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
isValid(): boolean {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
evaluate(facts: FactCollection): EvalResult {
|
|
129
|
+
return { type: "HAS_ERROR", reason: "UNIMPLEMENTED_VARIABLE_TYPE", data: {} }; // TODO
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
export class ConditionGroup implements FactEvaluator, IsValid {
|
|
133
|
+
public static readonly create = (dto: BuilderConditionGroupDto): ConditionGroup => {
|
|
134
|
+
return new ConditionGroup(dto);
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
private readonly _conditions: ReadonlyArray<Condition>;
|
|
138
|
+
private constructor(private readonly dto: BuilderConditionGroupDto) {
|
|
139
|
+
this._conditions = dto.conditions.map(Condition.create);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
isTrue(facts: FactCollection): boolean {
|
|
143
|
+
const results = this._conditions.map((condition) => condition.isTrue(facts));
|
|
144
|
+
let trueCount = 0;
|
|
145
|
+
let falseCount = 0;
|
|
146
|
+
results.forEach((results) => {
|
|
147
|
+
if (results) {
|
|
148
|
+
trueCount++;
|
|
149
|
+
} else {
|
|
150
|
+
falseCount++;
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
if (trueCount === 0 || falseCount === 0) {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
const type = this.dto.type;
|
|
157
|
+
|
|
158
|
+
if (type === "all" && trueCount === this._conditions.length) {
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (type === "any" && trueCount > 0) {
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const minLimit = this.dto.count;
|
|
167
|
+
if (type === "count" && typeof minLimit === "number" && trueCount >= minLimit) {
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
isValid(): boolean {
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
evaluate(facts: FactCollection): EvalResult {
|
|
179
|
+
return { type: "HAS_ERROR", reason: "UNIMPLEMENTED_VARIABLE_TYPE", data: {} }; // TODO
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
export class Rule2 implements FactEvaluator {
|
|
183
|
+
readonly name: string;
|
|
184
|
+
private readonly _conditions: ReadonlyArray<Condition | ConditionGroup>;
|
|
185
|
+
public static readonly create = (dto: BuilderRuleDto): Rule2 => {
|
|
186
|
+
return new Rule2(dto);
|
|
187
|
+
};
|
|
188
|
+
private _count = -1;
|
|
189
|
+
constructor(private readonly dto: BuilderRuleDto) {
|
|
190
|
+
this.name = dto.name;
|
|
191
|
+
const conditions: Array<Condition | ConditionGroup> = [];
|
|
192
|
+
dto.conditions.forEach((condition) => {
|
|
193
|
+
if (condition.kind === "condition-group") {
|
|
194
|
+
conditions.push(ConditionGroup.create(condition));
|
|
195
|
+
} else if (condition.kind === "condition") {
|
|
196
|
+
conditions.push(Condition.create(condition));
|
|
197
|
+
} else {
|
|
198
|
+
console.log("Unknown condition", condition);
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
this._conditions = conditions;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
isTrue(facts: FactCollection): boolean {
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
evaluate(facts: FactCollection): EvalResult {
|
|
209
|
+
return { type: "HAS_ERROR", reason: "UNIMPLEMENTED_VARIABLE_TYPE", data: {} }; // TODO
|
|
210
|
+
}
|
|
211
|
+
}
|