@fincity/kirun-js 1.3.0 → 1.4.1

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 (40) hide show
  1. package/__tests__/engine/function/system/math/RandomIntTest.ts +0 -1
  2. package/__tests__/engine/json/schema/validator/SchemaValidatorTest.ts +51 -1
  3. package/__tests__/engine/runtime/KIRuntimeTest.ts +75 -26
  4. package/__tests__/engine/runtime/KIRuntimeWithDefinitionTest.ts +47 -9
  5. package/__tests__/engine/runtime/expression/ExpressionEvaluatorTernaryOperatorTest.ts +42 -0
  6. package/dist/index.js +1 -1
  7. package/dist/index.js.map +1 -1
  8. package/dist/module.js +1 -1
  9. package/dist/module.js.map +1 -1
  10. package/dist/types.d.ts +44 -26
  11. package/dist/types.d.ts.map +1 -1
  12. package/package.json +1 -1
  13. package/src/engine/function/AbstractFunction.ts +12 -5
  14. package/src/engine/function/system/GenerateEvent.ts +1 -2
  15. package/src/engine/function/system/string/Reverse.ts +1 -1
  16. package/src/engine/json/schema/Schema.ts +119 -2
  17. package/src/engine/json/schema/SchemaUtil.ts +1 -1
  18. package/src/engine/json/schema/array/ArraySchemaType.ts +6 -0
  19. package/src/engine/json/schema/type/MultipleType.ts +5 -2
  20. package/src/engine/json/schema/type/SingleType.ts +4 -2
  21. package/src/engine/json/schema/validator/ObjectValidator.ts +2 -3
  22. package/src/engine/model/AbstractStatement.ts +10 -0
  23. package/src/engine/model/Event.ts +12 -5
  24. package/src/engine/model/FunctionDefinition.ts +1 -2
  25. package/src/engine/model/FunctionSignature.ts +14 -4
  26. package/src/engine/model/Parameter.ts +13 -3
  27. package/src/engine/model/ParameterReference.ts +38 -11
  28. package/src/engine/model/Position.ts +1 -1
  29. package/src/engine/model/Statement.ts +58 -20
  30. package/src/engine/model/StatementGroup.ts +22 -2
  31. package/src/engine/runtime/KIRuntime.ts +7 -6
  32. package/src/engine/runtime/expression/Expression.ts +120 -0
  33. package/src/engine/runtime/expression/ExpressionEvaluator.ts +26 -0
  34. package/src/engine/runtime/expression/Operation.ts +10 -0
  35. package/src/engine/runtime/expression/operators/ternary/ConditionalTernaryOperator.ts +7 -0
  36. package/src/engine/runtime/expression/operators/ternary/TernaryOperator.ts +15 -0
  37. package/src/engine/runtime/expression/operators/ternary/index.ts +1 -0
  38. package/src/engine/util/MapUtil.ts +10 -0
  39. package/src/index.ts +1 -1
  40. package/src/engine/json/schema/object/AdditionalPropertiesType.ts +0 -32
@@ -40,6 +40,8 @@ import { UnaryOperator } from './operators/unary/UnaryOperator';
40
40
  import { LiteralTokenValueExtractor } from './tokenextractor/LiteralTokenValueExtractor';
41
41
  import { TokenValueExtractor } from './tokenextractor/TokenValueExtractor';
42
42
  import { Tuple2 } from '../../util/Tuples';
43
+ import { ConditionalTernaryOperator } from './operators/ternary';
44
+ import { TernaryOperator } from './operators/ternary/TernaryOperator';
43
45
 
44
46
  export class ExpressionEvaluator {
45
47
  private static readonly UNARY_OPERATORS_MAP: Map<Operation, UnaryOperator> = new Map([
@@ -49,6 +51,10 @@ export class ExpressionEvaluator {
49
51
  [Operation.UNARY_PLUS, new ArithmeticUnaryPlusOperator()],
50
52
  ]);
51
53
 
54
+ private static readonly TERNARY_OPERATORS_MAP: Map<Operation, TernaryOperator> = new Map([
55
+ [Operation.CONDITIONAL_TERNARY_OPERATOR, new ConditionalTernaryOperator()],
56
+ ]);
57
+
52
58
  private static readonly BINARY_OPERATORS_MAP: Map<Operation, BinaryOperator> = new Map([
53
59
  [Operation.ADDITION, new ArithmeticAdditionOperator()],
54
60
  [Operation.DIVISION, new ArithmeticDivisionOperator()],
@@ -195,6 +201,13 @@ export class ExpressionEvaluator {
195
201
  operator == Operation.ARRAY_OPERATOR
196
202
  ) {
197
203
  this.processObjectOrArrayOperator(valuesMap, ops, tokens, operator, token);
204
+ } else if (operator == Operation.CONDITIONAL_TERNARY_OPERATOR) {
205
+ const token2: ExpressionToken = tokens.pop();
206
+ const token3: ExpressionToken = tokens.pop();
207
+ var v1 = this.getValueFromToken(valuesMap, token3);
208
+ var v2 = this.getValueFromToken(valuesMap, token2);
209
+ var v3 = this.getValueFromToken(valuesMap, token);
210
+ tokens.push(this.applyTernaryOperation(operator, v1, v2, v3));
198
211
  } else {
199
212
  const token2: ExpressionToken = tokens.pop();
200
213
  var v1 = this.getValueFromToken(valuesMap, token2);
@@ -295,6 +308,19 @@ export class ExpressionEvaluator {
295
308
  }
296
309
  }
297
310
 
311
+ private applyTernaryOperation(operator: Operation, v1: any, v2: any, v3: any): ExpressionToken {
312
+ let op: TernaryOperator | undefined =
313
+ ExpressionEvaluator.TERNARY_OPERATORS_MAP.get(operator);
314
+
315
+ if (!op)
316
+ throw new ExpressionEvaluationException(
317
+ this.expression,
318
+ StringFormatter.format('No operator found to evaluate $', this.getExpression()),
319
+ );
320
+
321
+ return new ExpressionTokenValue(operator.toString(), op.apply(v1, v2, v3));
322
+ }
323
+
298
324
  private applyBinaryOperation(operator: Operation, v1: any, v2: any): ExpressionToken {
299
325
  let typv1: string = typeof v1;
300
326
  let typv2: string = typeof v2;
@@ -34,6 +34,8 @@ export class Operation {
34
34
 
35
35
  public static readonly NULLISH_COALESCING_OPERATOR: Operation = new Operation('??');
36
36
 
37
+ public static readonly CONDITIONAL_TERNARY_OPERATOR: Operation = new Operation('?');
38
+
37
39
  private static readonly VALUE_OF: Map<string, Operation> = new Map([
38
40
  ['MULTIPLICATION', Operation.MULTIPLICATION],
39
41
  ['DIVISION', Operation.DIVISION],
@@ -64,6 +66,7 @@ export class Operation {
64
66
  ['ARRAY_OPERATOR', Operation.ARRAY_OPERATOR],
65
67
  ['OBJECT_OPERATOR', Operation.OBJECT_OPERATOR],
66
68
  ['NULLISH_COALESCING_OPERATOR', Operation.NULLISH_COALESCING_OPERATOR],
69
+ ['CONDITIONAL_TERNARY_OPERATOR', Operation.CONDITIONAL_TERNARY_OPERATOR],
67
70
  ]);
68
71
 
69
72
  public static readonly UNARY_OPERATORS: Set<Operation> = new Set([
@@ -109,6 +112,10 @@ export class Operation {
109
112
  Operation.BITWISE_XOR,
110
113
  ]);
111
114
 
115
+ public static readonly CONDITIONAL_OPERATORS: Set<Operation> = new Set([
116
+ Operation.CONDITIONAL_TERNARY_OPERATOR,
117
+ ]);
118
+
112
119
  public static readonly OPERATOR_PRIORITY: Map<Operation, number> = new Map([
113
120
  [Operation.UNARY_PLUS, 1],
114
121
  [Operation.UNARY_MINUS, 1],
@@ -137,6 +144,7 @@ export class Operation {
137
144
  [Operation.AND, 10],
138
145
  [Operation.OR, 11],
139
146
  [Operation.NULLISH_COALESCING_OPERATOR, 11],
147
+ [Operation.CONDITIONAL_TERNARY_OPERATOR, 12],
140
148
  ]);
141
149
 
142
150
  public static readonly OPERATORS: Set<string> = new Set(
@@ -146,6 +154,7 @@ export class Operation {
146
154
  ...Array.from(Operation.BITWISE_OPERATORS),
147
155
  Operation.ARRAY_OPERATOR,
148
156
  Operation.OBJECT_OPERATOR,
157
+ ...Array.from(Operation.CONDITIONAL_OPERATORS),
149
158
  ].map((e) => e.getOperator()),
150
159
  );
151
160
 
@@ -156,6 +165,7 @@ export class Operation {
156
165
  ...Array.from(Operation.BITWISE_OPERATORS),
157
166
  Operation.ARRAY_OPERATOR,
158
167
  Operation.OBJECT_OPERATOR,
168
+ ...Array.from(Operation.CONDITIONAL_OPERATORS),
159
169
  ]
160
170
  .filter((e) => !e.shouldBeWrappedInSpace())
161
171
  .map((e) => e.getOperator()),
@@ -0,0 +1,7 @@
1
+ import { TernaryOperator } from './TernaryOperator';
2
+
3
+ export class ConditionalTernaryOperator extends TernaryOperator {
4
+ public apply(t: any, u: any, v: any): any {
5
+ return t ? u : v;
6
+ }
7
+ }
@@ -0,0 +1,15 @@
1
+ import { ExecutionException } from '../../../../exception/ExecutionException';
2
+ import { isNullValue } from '../../../../util/NullCheck';
3
+ import { StringFormatter } from '../../../../util/string/StringFormatter';
4
+ import { Operation } from '../../Operation';
5
+
6
+ export abstract class TernaryOperator {
7
+ public abstract apply(t: any, u: any, v: any): any;
8
+
9
+ public nullCheck(e1: any, e2: any, e3: any, op: Operation): void {
10
+ if (isNullValue(e1) || isNullValue(e2) || isNullValue(e3))
11
+ throw new ExecutionException(
12
+ StringFormatter.format('$ cannot be applied to a null value', op.getOperatorName()),
13
+ );
14
+ }
15
+ }
@@ -0,0 +1 @@
1
+ export * from './ConditionalTernaryOperator';
@@ -72,6 +72,16 @@ export class MapUtil {
72
72
  return map;
73
73
  }
74
74
 
75
+ public static ofEntriesArray<K, V>(...entry: [K, V][]): Map<K, V> {
76
+ const map: Map<K, V> = new Map();
77
+
78
+ for (let i = 0; i < entry.length; i++) {
79
+ map.set(entry[i][0], entry[i][1]);
80
+ }
81
+
82
+ return map;
83
+ }
84
+
75
85
  private constructor() {}
76
86
  }
77
87
 
package/src/index.ts CHANGED
@@ -53,7 +53,6 @@ export * from './engine/json/schema/type/TypeUtil';
53
53
  export * from './engine/json/schema/type/MultipleType';
54
54
  export * from './engine/json/schema/type/Type';
55
55
  export * from './engine/json/schema/type/SchemaType';
56
- export * from './engine/json/schema/object/AdditionalPropertiesType';
57
56
  export * from './engine/json/schema/SchemaUtil';
58
57
  export * from './engine/json/schema/string/StringFormat';
59
58
  export * from './engine/HybridRepository';
@@ -77,3 +76,4 @@ export * from './engine/exception/ExecutionException';
77
76
  export * from './engine/exception/KIRuntimeException';
78
77
  export * from './engine/runtime/expression/operators/unary';
79
78
  export * from './engine/runtime/expression/operators/binary';
79
+ export * from './engine/runtime/expression/operators/ternary';
@@ -1,32 +0,0 @@
1
- import { Schema } from '../Schema';
2
-
3
- export class AdditionalPropertiesType {
4
- private booleanValue?: boolean;
5
- private schemaValue?: Schema;
6
-
7
- public getBooleanValue(): boolean | undefined {
8
- return this.booleanValue;
9
- }
10
-
11
- public getSchemaValue(): Schema | undefined {
12
- return this.schemaValue;
13
- }
14
-
15
- public setBooleanValue(booleanValue: boolean): AdditionalPropertiesType {
16
- this.booleanValue = booleanValue;
17
- return this;
18
- }
19
-
20
- public setSchemaValue(schemaValue: Schema): AdditionalPropertiesType {
21
- this.schemaValue = schemaValue;
22
- return this;
23
- }
24
-
25
- public static from(obj: any): AdditionalPropertiesType | undefined {
26
- if (!obj) return undefined;
27
- const ad = new AdditionalPropertiesType();
28
- ad.booleanValue = obj.booleanValue;
29
- ad.schemaValue = obj.schemaValue;
30
- return ad;
31
- }
32
- }