@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
@@ -48,7 +48,6 @@ test(' rand int 3', async () => {
48
48
  );
49
49
  let num: number = (await rand.execute(fep)).allResults()[0].getResult().get('value');
50
50
 
51
- console.log(num);
52
51
  expect(num).toBeLessThanOrEqual(max);
53
52
  expect(num).toBeGreaterThanOrEqual(min);
54
53
  });
@@ -1,4 +1,4 @@
1
- import { HybridRepository } from '../../../../../src';
1
+ import { AdditionalPropertiesType, HybridRepository } from '../../../../../src';
2
2
  import { Schema } from '../../../../../src/engine/json/schema/Schema';
3
3
  import { SchemaType } from '../../../../../src/engine/json/schema/type/SchemaType';
4
4
  import { TypeUtil } from '../../../../../src/engine/json/schema/type/TypeUtil';
@@ -26,6 +26,56 @@ test('Schema Validator Test 1', () => {
26
26
  // expect(SchemaValidator.validate([], schema, repo, 2.5)).toThrowError(new SchemaValidationException('', '2.5 is not a number of type Integer'));
27
27
  });
28
28
 
29
+ test('Schema validation when ref of ref', () => {
30
+ const locationSchema = Schema.from({
31
+ name: 'Location',
32
+ namespace: 'Test',
33
+ type: 'Object',
34
+ properties: {
35
+ url: { name: 'url', type: 'String' },
36
+ },
37
+ required: ['url'],
38
+ });
39
+
40
+ const urlParamsSchema = Schema.ofObject('UrlParameters')
41
+ .setNamespace('Test')
42
+ .setAdditionalProperties(
43
+ new AdditionalPropertiesType().setSchemaValue(Schema.ofRef(`Test.Location`)),
44
+ )
45
+ .setDefaultValue({});
46
+
47
+ const testSchema = Schema.ofObject('TestSchema')
48
+ .setNamespace('Test')
49
+ .setAdditionalProperties(
50
+ new AdditionalPropertiesType().setSchemaValue(Schema.ofRef(`Test.UrlParameters`)),
51
+ )
52
+ .setDefaultValue({});
53
+
54
+ const schemaMap = new Map([
55
+ ['Location', locationSchema],
56
+ ['UrlParameters', urlParamsSchema],
57
+ ['TestSchema', testSchema],
58
+ ]);
59
+
60
+ const repo = new HybridRepository<Schema>(
61
+ {
62
+ find(namespace: string, name: string): Schema | undefined {
63
+ if (namespace !== 'Test') return undefined;
64
+ return schemaMap.get(name);
65
+ },
66
+ },
67
+ new KIRunSchemaRepository(),
68
+ );
69
+ const obj = { url: 'http://xxxxxx.com' };
70
+ const queryParams = {
71
+ obj: { obj: { url: 'http://xxxxxx.com' } },
72
+ };
73
+
74
+ expect(
75
+ SchemaValidator.validate(undefined, Schema.ofRef('Test.TestSchema'), repo, queryParams),
76
+ ).toBe(queryParams);
77
+ });
78
+
29
79
  test('Schema Validator Test 2', () => {
30
80
  const locationSchema = Schema.from({
31
81
  name: 'Location',
@@ -20,6 +20,7 @@ import { AbstractFunction } from '../../../src/engine/function/AbstractFunction'
20
20
  import { FunctionOutput } from '../../../src/engine/model/FunctionOutput';
21
21
  import { HybridRepository } from '../../../src/engine/HybridRepository';
22
22
  import { Function } from '../../../src/engine/function/Function';
23
+ import { MapUtil } from '../../../src';
23
24
 
24
25
  test('KIRuntime Test 1', async () => {
25
26
  let start = new Date().getTime();
@@ -50,8 +51,8 @@ test('KIRuntime Test 1', async () => {
50
51
  create.getName(),
51
52
  ).setParameterMap(
52
53
  new Map([
53
- ['name', [ParameterReference.ofValue('a')]],
54
- ['schema', [ParameterReference.ofValue(arrayOfIntegerSchema)]],
54
+ ['name', MapUtil.ofEntriesArray(ParameterReference.ofValue('a'))],
55
+ ['schema', MapUtil.ofEntriesArray(ParameterReference.ofValue(arrayOfIntegerSchema))],
55
56
  ]),
56
57
  );
57
58
 
@@ -59,11 +60,11 @@ test('KIRuntime Test 1', async () => {
59
60
  var loop = new Statement('loop', rangeLoop.getNamespace(), rangeLoop.getName())
60
61
  .setParameterMap(
61
62
  new Map([
62
- ['from', [ParameterReference.ofValue(0)]],
63
- ['to', [ParameterReference.ofExpression('Arguments.Count')]],
63
+ ['from', MapUtil.ofEntriesArray(ParameterReference.ofValue(0))],
64
+ ['to', MapUtil.ofEntriesArray(ParameterReference.ofExpression('Arguments.Count'))],
64
65
  ]),
65
66
  )
66
- .setDependentStatements(['Steps.createArray.output']);
67
+ .setDependentStatements(MapUtil.of('Steps.createArray.output', true));
67
68
 
68
69
  var resultObj = { name: 'result', value: { isExpression: true, value: 'Context.a' } };
69
70
 
@@ -71,11 +72,11 @@ test('KIRuntime Test 1', async () => {
71
72
  var outputGenerate = new Statement('outputStep', generate.getNamespace(), generate.getName())
72
73
  .setParameterMap(
73
74
  new Map([
74
- ['eventName', [ParameterReference.ofValue('output')]],
75
- ['results', [ParameterReference.ofValue(resultObj)]],
75
+ ['eventName', MapUtil.ofEntriesArray(ParameterReference.ofValue('output'))],
76
+ ['results', MapUtil.ofEntriesArray(ParameterReference.ofValue(resultObj))],
76
77
  ]),
77
78
  )
78
- .setDependentStatements(['Steps.loop.output']);
79
+ .setDependentStatements(MapUtil.of('Steps.loop.output', true));
79
80
 
80
81
  var ifFunction = new If().getSignature();
81
82
  var ifStep = new Statement(
@@ -86,11 +87,11 @@ test('KIRuntime Test 1', async () => {
86
87
  new Map([
87
88
  [
88
89
  'condition',
89
- [
90
+ MapUtil.ofEntriesArray(
90
91
  ParameterReference.ofExpression(
91
92
  'Steps.loop.iteration.index = 0 or Steps.loop.iteration.index = 1',
92
93
  ),
93
- ],
94
+ ),
94
95
  ],
95
96
  ]),
96
97
  );
@@ -99,26 +100,41 @@ test('KIRuntime Test 1', async () => {
99
100
  var set1 = new Statement('setOnTrue', set.getNamespace(), set.getName())
100
101
  .setParameterMap(
101
102
  new Map([
102
- ['name', [ParameterReference.ofValue('Context.a[Steps.loop.iteration.index]')]],
103
- ['value', [ParameterReference.ofExpression('Steps.loop.iteration.index')]],
103
+ [
104
+ 'name',
105
+ MapUtil.ofEntriesArray(
106
+ ParameterReference.ofValue('Context.a[Steps.loop.iteration.index]'),
107
+ ),
108
+ ],
109
+ [
110
+ 'value',
111
+ MapUtil.ofEntriesArray(
112
+ ParameterReference.ofExpression('Steps.loop.iteration.index'),
113
+ ),
114
+ ],
104
115
  ]),
105
116
  )
106
- .setDependentStatements(['Steps.if.true']);
117
+ .setDependentStatements(MapUtil.of('Steps.if.true', true));
107
118
  var set2 = new Statement('setOnFalse', set.getNamespace(), set.getName())
108
119
  .setParameterMap(
109
120
  new Map([
110
- ['name', [ParameterReference.ofValue('Context.a[Steps.loop.iteration.index]')]],
121
+ [
122
+ 'name',
123
+ MapUtil.ofEntriesArray(
124
+ ParameterReference.ofValue('Context.a[Steps.loop.iteration.index]'),
125
+ ),
126
+ ],
111
127
  [
112
128
  'value',
113
- [
129
+ MapUtil.ofEntriesArray(
114
130
  ParameterReference.ofExpression(
115
131
  'Context.a[Steps.loop.iteration.index - 1] + Context.a[Steps.loop.iteration.index - 2]',
116
132
  ),
117
- ],
133
+ ),
118
134
  ],
119
135
  ]),
120
136
  )
121
- .setDependentStatements(['Steps.if.false']);
137
+ .setDependentStatements(MapUtil.of('Steps.if.false', true));
122
138
 
123
139
  start = new Date().getTime();
124
140
  let out: EventResult[] = (
@@ -178,7 +194,12 @@ test('KIRuntime Test 2', async () => {
178
194
  Statement.ofEntry(
179
195
  new Statement('first', Namespaces.MATH, 'Absolute').setParameterMap(
180
196
  new Map([
181
- ['value', [ParameterReference.ofExpression('Arguments.Value')]],
197
+ [
198
+ 'value',
199
+ MapUtil.ofEntriesArray(
200
+ ParameterReference.ofExpression('Arguments.Value'),
201
+ ),
202
+ ],
182
203
  ]),
183
204
  ),
184
205
  ),
@@ -189,8 +210,14 @@ test('KIRuntime Test 2', async () => {
189
210
  genEvent.getName(),
190
211
  ).setParameterMap(
191
212
  new Map([
192
- ['eventName', [ParameterReference.ofValue('output')]],
193
- ['results', [ParameterReference.ofValue(resultObj)]],
213
+ [
214
+ 'eventName',
215
+ MapUtil.ofEntriesArray(ParameterReference.ofValue('output')),
216
+ ],
217
+ [
218
+ 'results',
219
+ MapUtil.ofEntriesArray(ParameterReference.ofValue(resultObj)),
220
+ ],
194
221
  ]),
195
222
  ),
196
223
  ),
@@ -226,7 +253,12 @@ test('KIRuntime Test 3', async () => {
226
253
  Statement.ofEntry(
227
254
  new Statement('first', Namespaces.MATH, 'CubeRoot').setParameterMap(
228
255
  new Map([
229
- ['value', [ParameterReference.ofExpression('Arguments.Value')]],
256
+ [
257
+ 'value',
258
+ MapUtil.ofEntriesArray(
259
+ ParameterReference.ofExpression('Arguments.Value'),
260
+ ),
261
+ ],
230
262
  ]),
231
263
  ),
232
264
  ),
@@ -237,8 +269,14 @@ test('KIRuntime Test 3', async () => {
237
269
  genEvent.getName(),
238
270
  ).setParameterMap(
239
271
  new Map([
240
- ['eventName', [ParameterReference.ofValue('output')]],
241
- ['results', [ParameterReference.ofValue(resultObj)]],
272
+ [
273
+ 'eventName',
274
+ MapUtil.ofEntriesArray(ParameterReference.ofValue('output')),
275
+ ],
276
+ [
277
+ 'results',
278
+ MapUtil.ofEntriesArray(ParameterReference.ofValue(resultObj)),
279
+ ],
242
280
  ]),
243
281
  ),
244
282
  ),
@@ -326,7 +364,12 @@ test('KIRuntime Test 4', async () => {
326
364
  'asdf',
327
365
  ).setParameterMap(
328
366
  new Map([
329
- ['value', [ParameterReference.ofExpression('Arguments.Value')]],
367
+ [
368
+ 'value',
369
+ MapUtil.ofEntriesArray(
370
+ ParameterReference.ofExpression('Arguments.Value'),
371
+ ),
372
+ ],
330
373
  ]),
331
374
  ),
332
375
  ),
@@ -337,8 +380,14 @@ test('KIRuntime Test 4', async () => {
337
380
  genEvent.getName(),
338
381
  ).setParameterMap(
339
382
  new Map([
340
- ['eventName', [ParameterReference.ofValue('output')]],
341
- ['results', [ParameterReference.ofValue(resultObj)]],
383
+ [
384
+ 'eventName',
385
+ MapUtil.ofEntriesArray(ParameterReference.ofValue('output')),
386
+ ],
387
+ [
388
+ 'results',
389
+ MapUtil.ofEntriesArray(ParameterReference.ofValue(resultObj)),
390
+ ],
342
391
  ]),
343
392
  ),
344
393
  ),
@@ -42,11 +42,11 @@ test('KIRuntime With Definition 1', async () => {
42
42
  namespace: Namespaces.MATH,
43
43
  name: 'Add',
44
44
  parameterMap: {
45
- value: [
46
- { type: 'EXPRESSION', expression: 'Arguments.a' },
47
- { type: 'EXPRESSION', expression: '10 + 1' },
48
- { type: 'EXPRESSION', expression: 'Arguments.c' },
49
- ],
45
+ value: {
46
+ one: { key: 'one', type: 'EXPRESSION', expression: 'Arguments.a' },
47
+ two: { key: 'two', type: 'EXPRESSION', expression: '10 + 1' },
48
+ three: { key: 'three', type: 'EXPRESSION', expression: 'Arguments.c' },
49
+ },
50
50
  },
51
51
  },
52
52
  genOutput: {
@@ -54,16 +54,17 @@ test('KIRuntime With Definition 1', async () => {
54
54
  namespace: Namespaces.SYSTEM,
55
55
  name: 'GenerateEvent',
56
56
  parameterMap: {
57
- eventName: [{ type: 'VALUE', value: 'output' }],
58
- results: [
59
- {
57
+ eventName: { one: { key: 'one', type: 'VALUE', value: 'output' } },
58
+ results: {
59
+ one: {
60
+ key: 'one',
60
61
  type: 'VALUE',
61
62
  value: {
62
63
  name: 'additionResult',
63
64
  value: { isExpression: true, value: 'Steps.add.output.value' },
64
65
  },
65
66
  },
66
- ],
67
+ },
67
68
  },
68
69
  },
69
70
  },
@@ -86,3 +87,40 @@ test('KIRuntime With Definition 1', async () => {
86
87
 
87
88
  expect(result.allResults()[0].getResult().get('additionResult')).toBe(31);
88
89
  });
90
+
91
+ test('KIRuntime With Definition 2', async () => {
92
+ var def = {
93
+ name: 'checkWithNoParamsOrEvents',
94
+ namespace: 'UIApp',
95
+ steps: {
96
+ add: {
97
+ statementName: 'add',
98
+ namespace: Namespaces.MATH,
99
+ name: 'Add',
100
+ parameterMap: {
101
+ value: {
102
+ one: { key: 'one', type: 'VALUE', value: 2 },
103
+ two: { key: 'two', type: 'VALUE', value: 5 },
104
+ },
105
+ },
106
+ },
107
+ genOutput: {
108
+ statementName: 'genOutput',
109
+ namespace: Namespaces.SYSTEM,
110
+ name: 'GenerateEvent',
111
+ dependentStatements: { 'Steps.add.output': true },
112
+ },
113
+ },
114
+ };
115
+
116
+ const fd = FunctionDefinition.from(def);
117
+
118
+ let result = await new KIRuntime(fd).execute(
119
+ new FunctionExecutionParameters(
120
+ new KIRunFunctionRepository(),
121
+ new KIRunSchemaRepository(),
122
+ ).setArguments(new Map()),
123
+ );
124
+
125
+ expect(result.allResults()[0].getResult()).toMatchObject({});
126
+ });
@@ -0,0 +1,42 @@
1
+ import {
2
+ ArgumentsTokenValueExtractor,
3
+ Expression,
4
+ MapUtil,
5
+ TokenValueExtractor,
6
+ } from '../../../../src';
7
+ import { ExpressionEvaluator } from '../../../../src/engine/runtime/expression/ExpressionEvaluator';
8
+
9
+ test('Expression with Ternary Operator - 1 ', () => {
10
+ var exp = new Expression('a > 10 ? a - 2 : a + 3'.replace(' ', ''));
11
+ expect(exp.toString()).toBe('((a>10)?(a-2):(a+3))');
12
+
13
+ exp = new Expression('a > 10 ? a - 2 : a + 3');
14
+ expect(exp.toString()).toBe('((a>10)?(a-2):(a+3))');
15
+
16
+ exp = new Expression('a > 10 ? a > 15 ? a + 2 : a - 2 : a + 3');
17
+ expect(exp.toString()).toBe('((a>10)?((a>15)?(a+2):(a-2)):(a+3))');
18
+ });
19
+
20
+ test('Expression Evaluation with Ternary Operator - 1 ', () => {
21
+ let x = { a: 2, b: [true, false], c: { x: 'Arguments.b2' } };
22
+ let atv: ArgumentsTokenValueExtractor = new ArgumentsTokenValueExtractor(
23
+ new Map<string, any>([
24
+ ['a', 'kirun '],
25
+ ['b', 2],
26
+ ['b1', 4],
27
+ ['b2', 4],
28
+ ['c', x],
29
+ ['d', 'c'],
30
+ ]),
31
+ );
32
+ let valuesMap: Map<string, TokenValueExtractor> = MapUtil.of(atv.getPrefix(), atv);
33
+
34
+ var ev = new ExpressionEvaluator('Arguments.e = null ? Arguments.c.a : 3 ');
35
+ expect(ev.evaluate(valuesMap)).toBe(2);
36
+
37
+ ev = new ExpressionEvaluator('Arguments.f ? Arguments.c.a : 3 ');
38
+ expect(ev.evaluate(valuesMap)).toBe(3);
39
+
40
+ ev = new ExpressionEvaluator('Arguments.e = null ? Arguments.c : 3 ');
41
+ expect(ev.evaluate(valuesMap)).toMatchObject(x);
42
+ });