@media-quest/builder 0.0.3 → 0.0.5

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,20 +1,30 @@
1
1
  import type { RuleInput } from "./RuleInput";
2
2
  import { ExcludeByPageIdSelectItem } from "./multi-select-item";
3
+ import { ExcludeByPageAction } from "./RuleAction";
3
4
 
4
5
  export class PageActionManager {
5
- private readonly _initialSelection: Set<string>;
6
- readonly selectItems: ReadonlyArray<ExcludeByPageIdSelectItem>;
6
+ private readonly _initialSelection: Set<string>;
7
+ readonly selectItems: ReadonlyArray<ExcludeByPageIdSelectItem>;
7
8
 
8
- constructor(readonly validOptions: RuleInput["_pageIdActions"], readonly initialSelection: ReadonlyArray<string>) {
9
- this._initialSelection = new Set([...initialSelection]);
10
- this.selectItems = validOptions.map((opt) => {
11
- const isSelected = this._initialSelection.has(opt.pageId);
12
- return ExcludeByPageIdSelectItem.create(opt, isSelected);
13
- });
14
- }
9
+ constructor(
10
+ readonly validOptions: RuleInput["_pageIdActions"],
11
+ readonly initialSelection: ReadonlyArray<string>,
12
+ ) {
13
+ this._initialSelection = new Set([...initialSelection]);
14
+ this.selectItems = validOptions.map((opt) => {
15
+ const isSelected = this._initialSelection.has(opt.pageId);
16
+ return ExcludeByPageIdSelectItem.create(opt, isSelected);
17
+ });
18
+ }
15
19
 
16
- getCurrentSelection(): ReadonlyArray<string> {
17
- const selected = this.selectItems.filter((item) => item.isSelected).map((itm) => itm.data.pageId);
18
- return selected;
19
- }
20
+ getCurrentSelection(): ReadonlyArray<string> {
21
+ const selected = this.selectItems.filter((item) => item.isSelected).map((itm) => itm.data.pageId);
22
+ return selected;
23
+ }
24
+
25
+ getEngineAction(): ReadonlyArray<ExcludeByPageAction> {
26
+ const selectItems = this.selectItems.filter((item) => item.isSelected);
27
+ const actions = selectItems.map((item) => item.data);
28
+ return [...actions];
29
+ }
20
30
  }
@@ -0,0 +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,18 +1,28 @@
1
1
  import type { RuleInput } from "./RuleInput";
2
2
  import { ExcludeByTagSelectItem } from "./multi-select-item";
3
+ import { ExcludeByTagAction } from "./RuleAction";
3
4
 
4
5
  export class TagActionManager {
5
- private readonly _initialSelection: Set<string>;
6
- readonly selectItems: ReadonlyArray<ExcludeByTagSelectItem>;
7
- constructor(readonly validOptions: RuleInput["_tagActions"], readonly initialSelection: ReadonlyArray<string>) {
8
- this._initialSelection = new Set([...initialSelection]);
9
- this.selectItems = validOptions.map((opt) => {
10
- const isSelected = this._initialSelection.has(opt.tag);
11
- return ExcludeByTagSelectItem.create(opt, isSelected);
12
- });
13
- }
14
- getCurrentSelection(): ReadonlyArray<string> {
15
- const selected = this.selectItems.filter((item) => item.isSelected).map((itm) => itm.data.tag);
16
- return selected;
17
- }
6
+ private readonly _initialSelection: Set<string>;
7
+ readonly selectItems: ReadonlyArray<ExcludeByTagSelectItem>;
8
+ constructor(
9
+ readonly validOptions: RuleInput["_tagActions"],
10
+ readonly initialSelection: ReadonlyArray<string>,
11
+ ) {
12
+ this._initialSelection = new Set([...initialSelection]);
13
+ this.selectItems = validOptions.map((opt) => {
14
+ const isSelected = this._initialSelection.has(opt.tag);
15
+ return ExcludeByTagSelectItem.create(opt, isSelected);
16
+ });
17
+ }
18
+ getCurrentSelection(): ReadonlyArray<string> {
19
+ const selected = this.selectItems.filter((item) => item.isSelected).map((itm) => itm.data.tag);
20
+ return selected;
21
+ }
22
+
23
+ getEngineActions(): ReadonlyArray<ExcludeByTagAction> {
24
+ const selected = this.selectItems.filter((item) => item.isSelected);
25
+ const tagActions = selected.map((s) => s.data);
26
+ return [...tagActions];
27
+ }
18
28
  }
@@ -1,5 +1,6 @@
1
1
  import { AbstractThemeCompiler } from "./AbstractThemeCompiler";
2
2
  import type { BuilderSchemaDto } from "../Builder-schema";
3
+ import { BuilderSchema } from "../Builder-schema";
3
4
  import type { BuilderPageDto } from "../Builder-page";
4
5
  import { DStateProps } from "./standard-props";
5
6
  import { ThemeUtils } from "./theme-utils";
@@ -18,20 +19,43 @@ import {
18
19
  DVideoDto,
19
20
  Fact,
20
21
  PageDto,
22
+ PageQueCommand,
23
+ Rule,
21
24
  SchemaDto,
22
25
  } from "@media-quest/engine";
23
26
  import { AudioFile } from "../media-files";
27
+ import { BuilderRule } from "../rulebuilder";
24
28
 
25
29
  const U = DUtil;
26
30
  const generateElementId = () => U.randomString(32);
27
31
  export class DefaultThemeCompiler extends AbstractThemeCompiler<IDefaultTheme> {
28
32
  readonly name = "Ispe default theme.";
33
+ private readonly TAG = "[ DEFAULT_THEME_COMPILER ]: ";
29
34
  constructor() {
30
35
  super(DefaultTheme);
31
36
  }
32
37
 
38
+ private compileRules(source: BuilderSchemaDto): Rule<PageQueCommand, never>[] {
39
+ const builderSchema = BuilderSchema.fromJson(source);
40
+ const ruleInput = builderSchema.getRuleInput();
41
+ const pageQueRules: Rule<PageQueCommand, never>[] = [];
42
+ source.rules.forEach((rule) => {
43
+ const engineRule = BuilderRule.fromDto(rule, ruleInput).toEngineRule(source.prefix);
44
+ if (!Rule.isEmpty(engineRule)) {
45
+ pageQueRules.push(engineRule);
46
+ } else {
47
+ console.groupCollapsed(this.TAG, "Throws away empty rule.");
48
+ console.log(rule);
49
+ console.log(ruleInput);
50
+ console.groupEnd();
51
+ }
52
+ });
53
+ return pageQueRules;
54
+ }
55
+
33
56
  compile(source: BuilderSchemaDto): SchemaDto {
34
- const pages = source.pages.map((p) => this.compilePage(p, source.id));
57
+ const pages = source.pages.map((p) => this.compilePage(p, source.prefix));
58
+ const rules = this.compileRules(source);
35
59
  const dto: SchemaDto = {
36
60
  backgroundColor: source.backgroundColor,
37
61
  baseHeight: source.baseHeight,
@@ -41,7 +65,7 @@ export class DefaultThemeCompiler extends AbstractThemeCompiler<IDefaultTheme> {
41
65
  pages,
42
66
  predefinedFacts: [],
43
67
  prefix: source.prefix,
44
- rules: [],
68
+ rules,
45
69
  stateFromEvent: [
46
70
  {
47
71
  onEvent: "VIDEO_ENDED_EVENT",
@@ -92,8 +116,8 @@ export class DefaultThemeCompiler extends AbstractThemeCompiler<IDefaultTheme> {
92
116
  };
93
117
  return dto;
94
118
  }
95
- private compilePage(page: BuilderPageDto, _moduleId: string): PageDto {
96
- // console.log(moduleId);
119
+ private compilePage(page: BuilderPageDto, modulePrefix: string): PageDto {
120
+ // console.log(_moduleId);
97
121
  // const textElement
98
122
  const { nextButton, mainText, id, mainMedia, _type } = page;
99
123
  const elements: DElementDto[] = [];
@@ -110,7 +134,8 @@ export class DefaultThemeCompiler extends AbstractThemeCompiler<IDefaultTheme> {
110
134
  }
111
135
 
112
136
  if (_type === "question") {
113
- const { buttons, question } = this.compileQuestion(id, page);
137
+ const variableId = modulePrefix + "_" + page.prefix;
138
+ const { buttons, question } = this.compileQuestion(id, page, variableId);
114
139
  // console.log(question);
115
140
  elements.push(...buttons, question);
116
141
  }
@@ -306,6 +331,7 @@ export class DefaultThemeCompiler extends AbstractThemeCompiler<IDefaultTheme> {
306
331
  private compileQuestion(
307
332
  pageId: string,
308
333
  page: BuilderPageDto,
334
+ variableId: string,
309
335
  ): {
310
336
  question: DTextDto;
311
337
  buttons: DDivDto[];
@@ -327,7 +353,7 @@ export class DefaultThemeCompiler extends AbstractThemeCompiler<IDefaultTheme> {
327
353
  const buttons = q.options.map((o) => {
328
354
  const btns = this.compileButton(pageId, o, {
329
355
  kind: "response-button",
330
- questionId: "",
356
+ questionId: variableId,
331
357
  });
332
358
  return btns;
333
359
  });
package/tsconfig.json CHANGED
@@ -9,6 +9,6 @@
9
9
  "skipLibCheck": true,
10
10
  "forceConsistentCasingInFileNames": true,
11
11
  "rootDir": "./src",
12
- "outDir": "./dist",
13
- }
12
+ "outDir": "./dist"
13
+ },
14
14
  }