@fincity/kirun-js 1.0.0

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 (217) hide show
  1. package/.prettierrc +9 -0
  2. package/__tests__/engine/function/system/array/AddFirstTest.ts +235 -0
  3. package/__tests__/engine/function/system/array/AddTest.ts +126 -0
  4. package/__tests__/engine/function/system/array/BinarySearchTest.ts +135 -0
  5. package/__tests__/engine/function/system/array/CompareTest.ts +48 -0
  6. package/__tests__/engine/function/system/array/CopyTest.ts +86 -0
  7. package/__tests__/engine/function/system/array/DeleteFirstTest.ts +103 -0
  8. package/__tests__/engine/function/system/array/DeleteFromTest.ts +232 -0
  9. package/__tests__/engine/function/system/array/DeleteLastTest.ts +103 -0
  10. package/__tests__/engine/function/system/array/DeleteTest.ts +110 -0
  11. package/__tests__/engine/function/system/array/DisjointTest.ts +184 -0
  12. package/__tests__/engine/function/system/array/Equals.ts +67 -0
  13. package/__tests__/engine/function/system/array/FillTest.ts +36 -0
  14. package/__tests__/engine/function/system/array/FrequencyTest.ts +97 -0
  15. package/__tests__/engine/function/system/array/IndexOfArrayTest.ts +347 -0
  16. package/__tests__/engine/function/system/array/IndexOfTest.ts +267 -0
  17. package/__tests__/engine/function/system/array/InsertTest.ts +229 -0
  18. package/__tests__/engine/function/system/array/LastIndexOfArrayTest.ts +213 -0
  19. package/__tests__/engine/function/system/array/LastIndexOfTest.ts +253 -0
  20. package/__tests__/engine/function/system/array/MaxTest.ts +98 -0
  21. package/__tests__/engine/function/system/array/MinTest.ts +99 -0
  22. package/__tests__/engine/function/system/array/MisMatchTest.ts +215 -0
  23. package/__tests__/engine/function/system/array/ReverseTest.ts +235 -0
  24. package/__tests__/engine/function/system/array/RotateTest.ts +137 -0
  25. package/__tests__/engine/function/system/array/ShuffleTest.ts +154 -0
  26. package/__tests__/engine/function/system/array/SortTest.ts +130 -0
  27. package/__tests__/engine/function/system/array/SubArrayTest.ts +236 -0
  28. package/__tests__/engine/function/system/math/AddTest.ts +12 -0
  29. package/__tests__/engine/function/system/string/ConcatenateTest.ts +46 -0
  30. package/__tests__/engine/function/system/string/DeleteForGivenLengthTest.ts +38 -0
  31. package/__tests__/engine/function/system/string/InsertAtGivenPositionTest.ts +53 -0
  32. package/__tests__/engine/function/system/string/PostPadTest.ts +54 -0
  33. package/__tests__/engine/function/system/string/PrePadTest.ts +54 -0
  34. package/__tests__/engine/function/system/string/RegionMatchesTest.ts +78 -0
  35. package/__tests__/engine/function/system/string/ReverseTest.ts +36 -0
  36. package/__tests__/engine/function/system/string/SplitTest.ts +61 -0
  37. package/__tests__/engine/function/system/string/StringFunctionRepoTest2.ts +126 -0
  38. package/__tests__/engine/function/system/string/StringFunctionRepoTest3.ts +54 -0
  39. package/__tests__/engine/function/system/string/StringFunctionRepositoryTest.ts +146 -0
  40. package/__tests__/engine/function/system/string/ToStringTest.ts +43 -0
  41. package/__tests__/engine/function/system/string/TrimToTest.ts +28 -0
  42. package/__tests__/engine/json/schema/validator/SchemaValidatorTest.ts +26 -0
  43. package/__tests__/engine/runtime/KIRuntimeTest.ts +351 -0
  44. package/__tests__/engine/runtime/expression/ExpressionEvaluationTest.ts +128 -0
  45. package/__tests__/engine/runtime/expression/ExpressionTest.ts +33 -0
  46. package/__tests__/engine/runtime/expression/tokenextractor/OutputMapTokenValueExtractorTest.ts +44 -0
  47. package/__tests__/engine/runtime/expression/tokenextractor/TokenValueExtractorTest.ts +72 -0
  48. package/__tests__/engine/util/LinkedListTest.ts +29 -0
  49. package/__tests__/engine/util/string/StringFormatterTest.ts +17 -0
  50. package/__tests__/indexTest.ts +33 -0
  51. package/dist/index.js +2 -0
  52. package/dist/index.js.map +1 -0
  53. package/dist/module.js +2 -0
  54. package/dist/module.js.map +1 -0
  55. package/dist/types.d.ts +430 -0
  56. package/dist/types.d.ts.map +1 -0
  57. package/package.json +54 -0
  58. package/src/engine/HybridRepository.ts +18 -0
  59. package/src/engine/Repository.ts +3 -0
  60. package/src/engine/constant/KIRunConstants.ts +7 -0
  61. package/src/engine/exception/ExecutionException.ts +12 -0
  62. package/src/engine/exception/KIRuntimeException.ts +12 -0
  63. package/src/engine/function/AbstractFunction.ts +76 -0
  64. package/src/engine/function/Function.ts +13 -0
  65. package/src/engine/function/system/GenerateEvent.ts +76 -0
  66. package/src/engine/function/system/If.ts +40 -0
  67. package/src/engine/function/system/array/AbstractArrayFunction.ts +158 -0
  68. package/src/engine/function/system/array/Add.ts +31 -0
  69. package/src/engine/function/system/array/AddFirst.ts +44 -0
  70. package/src/engine/function/system/array/BinarySearch.ts +66 -0
  71. package/src/engine/function/system/array/Compare.ts +150 -0
  72. package/src/engine/function/system/array/Copy.ts +56 -0
  73. package/src/engine/function/system/array/Delete.ts +63 -0
  74. package/src/engine/function/system/array/DeleteFirst.ts +22 -0
  75. package/src/engine/function/system/array/DeleteFrom.ts +51 -0
  76. package/src/engine/function/system/array/DeleteLast.ts +23 -0
  77. package/src/engine/function/system/array/Disjoint.ts +85 -0
  78. package/src/engine/function/system/array/Equals.ts +36 -0
  79. package/src/engine/function/system/array/Fill.ts +51 -0
  80. package/src/engine/function/system/array/Frequency.ts +68 -0
  81. package/src/engine/function/system/array/IndexOf.ts +56 -0
  82. package/src/engine/function/system/array/IndexOfArray.ts +67 -0
  83. package/src/engine/function/system/array/Insert.ts +48 -0
  84. package/src/engine/function/system/array/LastIndexOf.ts +62 -0
  85. package/src/engine/function/system/array/LastIndexOfArray.ts +70 -0
  86. package/src/engine/function/system/array/Max.ts +31 -0
  87. package/src/engine/function/system/array/Min.ts +32 -0
  88. package/src/engine/function/system/array/MisMatch.ts +66 -0
  89. package/src/engine/function/system/array/Reverse.ts +50 -0
  90. package/src/engine/function/system/array/Rotate.ts +43 -0
  91. package/src/engine/function/system/array/Shuffle.ts +31 -0
  92. package/src/engine/function/system/array/Sort.ts +70 -0
  93. package/src/engine/function/system/array/SubArray.ts +48 -0
  94. package/src/engine/function/system/context/Create.ts +73 -0
  95. package/src/engine/function/system/context/Get.ts +55 -0
  96. package/src/engine/function/system/context/SetFunction.ts +244 -0
  97. package/src/engine/function/system/loop/CountLoop.ts +55 -0
  98. package/src/engine/function/system/loop/RangeLoop.ts +120 -0
  99. package/src/engine/function/system/math/Add.ts +34 -0
  100. package/src/engine/function/system/math/GenericMathFunction.ts +80 -0
  101. package/src/engine/function/system/math/Hypotenuse.ts +57 -0
  102. package/src/engine/function/system/math/MathFunctionRepository.ts +63 -0
  103. package/src/engine/function/system/math/Maximum.ts +38 -0
  104. package/src/engine/function/system/math/Minimum.ts +38 -0
  105. package/src/engine/function/system/math/Random.ts +27 -0
  106. package/src/engine/function/system/string/AbstractStringFunction.ts +409 -0
  107. package/src/engine/function/system/string/Concatenate.ts +57 -0
  108. package/src/engine/function/system/string/DeleteForGivenLength.ts +82 -0
  109. package/src/engine/function/system/string/InsertAtGivenPosition.ts +72 -0
  110. package/src/engine/function/system/string/PostPad.ts +83 -0
  111. package/src/engine/function/system/string/PrePad.ts +73 -0
  112. package/src/engine/function/system/string/RegionMatches.ts +119 -0
  113. package/src/engine/function/system/string/ReplaceAtGivenPosition.ts +101 -0
  114. package/src/engine/function/system/string/Reverse.ts +62 -0
  115. package/src/engine/function/system/string/Split.ts +53 -0
  116. package/src/engine/function/system/string/StringFunctionRepository.ts +60 -0
  117. package/src/engine/function/system/string/ToString.ts +45 -0
  118. package/src/engine/function/system/string/TrimTo.ts +55 -0
  119. package/src/engine/json/JsonExpression.ts +11 -0
  120. package/src/engine/json/schema/Schema.ts +670 -0
  121. package/src/engine/json/schema/SchemaUtil.ts +145 -0
  122. package/src/engine/json/schema/array/ArraySchemaType.ts +44 -0
  123. package/src/engine/json/schema/object/AdditionalPropertiesType.ts +32 -0
  124. package/src/engine/json/schema/string/StringFormat.ts +7 -0
  125. package/src/engine/json/schema/type/MultipleType.ts +28 -0
  126. package/src/engine/json/schema/type/SchemaType.ts +11 -0
  127. package/src/engine/json/schema/type/SingleType.ts +23 -0
  128. package/src/engine/json/schema/type/Type.ts +6 -0
  129. package/src/engine/json/schema/type/TypeUtil.ts +25 -0
  130. package/src/engine/json/schema/validator/AnyOfAllOfOneOfValidator.ts +108 -0
  131. package/src/engine/json/schema/validator/ArrayValidator.ts +145 -0
  132. package/src/engine/json/schema/validator/BooleanValidator.ts +24 -0
  133. package/src/engine/json/schema/validator/NullValidator.ts +17 -0
  134. package/src/engine/json/schema/validator/NumberValidator.ts +115 -0
  135. package/src/engine/json/schema/validator/ObjectValidator.ts +184 -0
  136. package/src/engine/json/schema/validator/SchemaValidator.ts +141 -0
  137. package/src/engine/json/schema/validator/StringValidator.ts +97 -0
  138. package/src/engine/json/schema/validator/TypeValidator.ts +49 -0
  139. package/src/engine/json/schema/validator/exception/SchemaReferenceException.ts +19 -0
  140. package/src/engine/json/schema/validator/exception/SchemaValidationException.ts +22 -0
  141. package/src/engine/model/AbstractStatement.ts +29 -0
  142. package/src/engine/model/Argument.ts +36 -0
  143. package/src/engine/model/Event.ts +62 -0
  144. package/src/engine/model/EventResult.ts +34 -0
  145. package/src/engine/model/FunctionDefinition.ts +69 -0
  146. package/src/engine/model/FunctionOutput.ts +37 -0
  147. package/src/engine/model/FunctionOutputGenerator.ts +5 -0
  148. package/src/engine/model/FunctionSignature.ts +67 -0
  149. package/src/engine/model/Parameter.ts +97 -0
  150. package/src/engine/model/ParameterReference.ts +57 -0
  151. package/src/engine/model/ParameterReferenceType.ts +4 -0
  152. package/src/engine/model/ParameterType.ts +4 -0
  153. package/src/engine/model/Position.ts +40 -0
  154. package/src/engine/model/Statement.ts +101 -0
  155. package/src/engine/model/StatementGroup.ts +37 -0
  156. package/src/engine/namespaces/Namespaces.ts +11 -0
  157. package/src/engine/repository/KIRunFunctionRepository.ts +37 -0
  158. package/src/engine/repository/KIRunSchemaRepository.ts +24 -0
  159. package/src/engine/runtime/ContextElement.ts +28 -0
  160. package/src/engine/runtime/FunctionExecutionParameters.ts +74 -0
  161. package/src/engine/runtime/KIRuntime.ts +653 -0
  162. package/src/engine/runtime/StatementExecution.ts +61 -0
  163. package/src/engine/runtime/StatementMessage.ts +30 -0
  164. package/src/engine/runtime/StatementMessageType.ts +5 -0
  165. package/src/engine/runtime/expression/Expression.ts +330 -0
  166. package/src/engine/runtime/expression/ExpressionEvaluator.ts +305 -0
  167. package/src/engine/runtime/expression/ExpressionToken.ts +15 -0
  168. package/src/engine/runtime/expression/ExpressionTokenValue.ts +23 -0
  169. package/src/engine/runtime/expression/Operation.ts +190 -0
  170. package/src/engine/runtime/expression/exception/ExpressionEvaluationException.ts +15 -0
  171. package/src/engine/runtime/expression/operators/binary/ArithmeticAdditionOperator.ts +9 -0
  172. package/src/engine/runtime/expression/operators/binary/ArithmeticDivisionOperator.ts +9 -0
  173. package/src/engine/runtime/expression/operators/binary/ArithmeticInetgerDivisionOperator.ts +9 -0
  174. package/src/engine/runtime/expression/operators/binary/ArithmeticModulusOperator.ts +9 -0
  175. package/src/engine/runtime/expression/operators/binary/ArithmeticMultiplicationOperator.ts +9 -0
  176. package/src/engine/runtime/expression/operators/binary/ArithmeticSubtractionOperator.ts +9 -0
  177. package/src/engine/runtime/expression/operators/binary/ArrayOperator.ts +31 -0
  178. package/src/engine/runtime/expression/operators/binary/BinaryOperator.ts +15 -0
  179. package/src/engine/runtime/expression/operators/binary/BitwiseAndOperator.ts +9 -0
  180. package/src/engine/runtime/expression/operators/binary/BitwiseLeftShiftOperator.ts +9 -0
  181. package/src/engine/runtime/expression/operators/binary/BitwiseOrOperator.ts +9 -0
  182. package/src/engine/runtime/expression/operators/binary/BitwiseRightShiftOperator.ts +9 -0
  183. package/src/engine/runtime/expression/operators/binary/BitwiseUnsignedRightShiftOperator.ts +9 -0
  184. package/src/engine/runtime/expression/operators/binary/BitwiseXorOperator.ts +9 -0
  185. package/src/engine/runtime/expression/operators/binary/LogicalAndOperator.ts +25 -0
  186. package/src/engine/runtime/expression/operators/binary/LogicalEqualOperator.ts +13 -0
  187. package/src/engine/runtime/expression/operators/binary/LogicalGreaterThanEqualOperator.ts +24 -0
  188. package/src/engine/runtime/expression/operators/binary/LogicalGreaterThanOperator.ts +24 -0
  189. package/src/engine/runtime/expression/operators/binary/LogicalLessThanEqualOperator.ts +24 -0
  190. package/src/engine/runtime/expression/operators/binary/LogicalLessThanOperator.ts +24 -0
  191. package/src/engine/runtime/expression/operators/binary/LogicalNotEqualOperator.ts +13 -0
  192. package/src/engine/runtime/expression/operators/binary/LogicalOrOperator.ts +25 -0
  193. package/src/engine/runtime/expression/operators/binary/ObjectOperator.ts +24 -0
  194. package/src/engine/runtime/expression/operators/unary/ArithmeticUnaryMinusOperator.ts +13 -0
  195. package/src/engine/runtime/expression/operators/unary/ArithmeticUnaryPlusOperator.ts +13 -0
  196. package/src/engine/runtime/expression/operators/unary/BitwiseComplementOperator.ts +22 -0
  197. package/src/engine/runtime/expression/operators/unary/LogicalNotOperator.ts +22 -0
  198. package/src/engine/runtime/expression/operators/unary/UnaryOperator.ts +15 -0
  199. package/src/engine/runtime/expression/tokenextractor/LiteralTokenValueExtractor.ts +64 -0
  200. package/src/engine/runtime/expression/tokenextractor/TokenValueExtractor.ts +136 -0
  201. package/src/engine/runtime/graph/ExecutionGraph.ts +81 -0
  202. package/src/engine/runtime/graph/GraphVertex.ts +118 -0
  203. package/src/engine/runtime/graph/GraphVertexType.ts +4 -0
  204. package/src/engine/runtime/tokenextractor/ArgumentsTokenValueExtractor.ts +22 -0
  205. package/src/engine/runtime/tokenextractor/ContextTokenValueExtractor.ts +37 -0
  206. package/src/engine/runtime/tokenextractor/OutputMapTokenValueExtractor.ts +32 -0
  207. package/src/engine/util/ArrayUtil.ts +23 -0
  208. package/src/engine/util/LinkedList.ts +229 -0
  209. package/src/engine/util/MapUtil.ts +86 -0
  210. package/src/engine/util/NullCheck.ts +3 -0
  211. package/src/engine/util/Tuples.ts +43 -0
  212. package/src/engine/util/primitive/PrimitiveUtil.ts +143 -0
  213. package/src/engine/util/string/StringBuilder.ts +59 -0
  214. package/src/engine/util/string/StringFormatter.ts +25 -0
  215. package/src/engine/util/string/StringUtil.ts +48 -0
  216. package/src/index.ts +13 -0
  217. package/tsconfig.json +6 -0
@@ -0,0 +1,76 @@
1
+ import { Schema } from '../json/schema/Schema';
2
+ import { SchemaValidator } from '../json/schema/validator/SchemaValidator';
3
+ import { Event } from '../model/Event';
4
+ import { FunctionOutput } from '../model/FunctionOutput';
5
+ import { FunctionSignature } from '../model/FunctionSignature';
6
+ import { Parameter } from '../model/Parameter';
7
+ import { FunctionExecutionParameters } from '../runtime/FunctionExecutionParameters';
8
+ import { isNullValue } from '../util/NullCheck';
9
+ import { Tuple2 } from '../util/Tuples';
10
+ import { Function } from './Function';
11
+
12
+ export abstract class AbstractFunction implements Function {
13
+ protected validateArguments(args: Map<string, any>): Map<string, any> {
14
+ return Array.from(this.getSignature().getParameters().entries())
15
+ .map((e) => {
16
+ let key: string = e[0];
17
+ let param: Parameter = e[1];
18
+ let jsonElement: any = args.get(e[0]);
19
+
20
+ if (isNullValue(jsonElement)) {
21
+ return new Tuple2(
22
+ key,
23
+ SchemaValidator.validate(
24
+ undefined,
25
+ param.getSchema(),
26
+ undefined,
27
+ undefined,
28
+ ),
29
+ );
30
+ }
31
+
32
+ if (!param?.isVariableArgument())
33
+ return new Tuple2(
34
+ key,
35
+ SchemaValidator.validate(
36
+ undefined,
37
+ param.getSchema(),
38
+ undefined,
39
+ jsonElement,
40
+ ),
41
+ );
42
+
43
+ let array: any[] | undefined = undefined;
44
+
45
+ if (Array.isArray(jsonElement)) array = jsonElement as any[];
46
+ else {
47
+ array = [];
48
+ array.push(jsonElement);
49
+ }
50
+
51
+ for (const je of array) {
52
+ SchemaValidator.validate(undefined, param.getSchema(), undefined, je);
53
+ }
54
+
55
+ return new Tuple2(key, jsonElement);
56
+ })
57
+ .reduce((a, c) => {
58
+ a.set(c.getT1(), c.getT2());
59
+ return a;
60
+ }, new Map<string, any>());
61
+ }
62
+
63
+ public execute(context: FunctionExecutionParameters): FunctionOutput {
64
+ context.setArguments(this.validateArguments(context.getArguments()??new Map()));
65
+ return this.internalExecute(context);
66
+ }
67
+
68
+ public getProbableEventSignature(
69
+ probableParameters: Map<string, Schema[]>,
70
+ ): Map<string, Event> {
71
+ return this.getSignature().getEvents();
72
+ }
73
+
74
+ protected abstract internalExecute(context: FunctionExecutionParameters): FunctionOutput;
75
+ public abstract getSignature(): FunctionSignature;
76
+ }
@@ -0,0 +1,13 @@
1
+ import { Schema } from '../json/schema/Schema';
2
+ import { Event } from '../model/Event';
3
+ import { FunctionOutput } from '../model/FunctionOutput';
4
+ import { FunctionSignature } from '../model/FunctionSignature';
5
+ import { FunctionExecutionParameters } from '../runtime/FunctionExecutionParameters';
6
+
7
+ export interface Function {
8
+ getSignature(): FunctionSignature;
9
+
10
+ getProbableEventSignature(probableParameters: Map<string, Schema[]>): Map<string, Event>;
11
+
12
+ execute(context: FunctionExecutionParameters): FunctionOutput;
13
+ }
@@ -0,0 +1,76 @@
1
+ import { KIRuntimeException } from '../../exception/KIRuntimeException';
2
+ import { Schema } from '../../json/schema/Schema';
3
+ import { Event } from '../../model/Event';
4
+ import { EventResult } from '../../model/EventResult';
5
+ import { FunctionOutput } from '../../model/FunctionOutput';
6
+ import { FunctionSignature } from '../../model/FunctionSignature';
7
+ import { Parameter } from '../../model/Parameter';
8
+ import { Namespaces } from '../../namespaces/Namespaces';
9
+ import { ExpressionEvaluator } from '../../runtime/expression/ExpressionEvaluator';
10
+ import { FunctionExecutionParameters } from '../../runtime/FunctionExecutionParameters';
11
+ import { KIRuntime } from '../../runtime/KIRuntime';
12
+ import { isNullValue } from '../../util/NullCheck';
13
+ import { AbstractFunction } from '../AbstractFunction';
14
+
15
+ const VALUE = 'value';
16
+ const EVENT_NAME = 'eventName';
17
+ const RESULTS = 'results';
18
+
19
+ const SIGNATURE: FunctionSignature = new FunctionSignature('GenerateEvent')
20
+ .setNamespace(Namespaces.SYSTEM)
21
+ .setParameters(
22
+ new Map([
23
+ Parameter.ofEntry(EVENT_NAME, Schema.ofString(EVENT_NAME)),
24
+ Parameter.ofEntry(
25
+ RESULTS,
26
+ Schema.ofObject(RESULTS).setProperties(
27
+ new Map([
28
+ ['name', Schema.ofString('name')],
29
+ [VALUE, Parameter.EXPRESSION],
30
+ ]),
31
+ ),
32
+ true,
33
+ ),
34
+ ]),
35
+ )
36
+ .setEvents(new Map([Event.outputEventMapEntry(new Map())]));
37
+
38
+ interface ResultType {
39
+ name: string;
40
+ value: any;
41
+ }
42
+
43
+ export class GenerateEvent extends AbstractFunction {
44
+ public getSignature(): FunctionSignature {
45
+ return SIGNATURE;
46
+ }
47
+ protected internalExecute(context: FunctionExecutionParameters): FunctionOutput {
48
+ const events: Map<string, Map<string, any>[]> | undefined = context.getEvents();
49
+ const args: Map<string, any> | undefined = context.getArguments();
50
+
51
+ const eventName: string = args?.get(EVENT_NAME);
52
+
53
+ const map: Map<string, any> = context
54
+ ?.getArguments()
55
+ ?.get(RESULTS)
56
+ .map((e: ResultType) => {
57
+ let je: any = e[VALUE];
58
+
59
+ if (isNullValue(je)) throw new KIRuntimeException('Expect a value object');
60
+
61
+ let v: any = je.value;
62
+ if (je.isExpression)
63
+ v = new ExpressionEvaluator(v).evaluate(context.getValuesMap());
64
+ return [e.name, v];
65
+ })
66
+ .reduce((a: Map<string, any>, c: [string, any]) => {
67
+ a.set(c[0], c[1]);
68
+ return a;
69
+ }, new Map());
70
+
71
+ if (!events?.has(eventName)) events?.set(eventName, []);
72
+ events?.get(eventName)?.push(map);
73
+
74
+ return new FunctionOutput([EventResult.outputOf(new Map())]);
75
+ }
76
+ }
@@ -0,0 +1,40 @@
1
+ import { Schema } from '../../json/schema/Schema';
2
+ import { SchemaType } from '../../json/schema/type/SchemaType';
3
+ import { Event } from '../../model/Event';
4
+ import { EventResult } from '../../model/EventResult';
5
+ import { FunctionOutput } from '../../model/FunctionOutput';
6
+ import { FunctionSignature } from '../../model/FunctionSignature';
7
+ import { Parameter } from '../../model/Parameter';
8
+ import { Namespaces } from '../../namespaces/Namespaces';
9
+ import { FunctionExecutionParameters } from '../../runtime/FunctionExecutionParameters';
10
+ import { AbstractFunction } from '../AbstractFunction';
11
+
12
+ export class If extends AbstractFunction {
13
+ private static readonly CONDITION: string = 'condition';
14
+
15
+ private static readonly SIGNATURE: FunctionSignature = new FunctionSignature('If')
16
+ .setNamespace(Namespaces.SYSTEM)
17
+ .setParameters(
18
+ new Map([Parameter.ofEntry(If.CONDITION, Schema.of(If.CONDITION, SchemaType.BOOLEAN))]),
19
+ )
20
+ .setEvents(
21
+ new Map([
22
+ Event.eventMapEntry(Event.TRUE, new Map()),
23
+ Event.eventMapEntry(Event.FALSE, new Map()),
24
+ Event.outputEventMapEntry(new Map()),
25
+ ]),
26
+ );
27
+
28
+ public getSignature(): FunctionSignature {
29
+ return If.SIGNATURE;
30
+ }
31
+
32
+ protected internalExecute(context: FunctionExecutionParameters): FunctionOutput {
33
+ var condition = context.getArguments()?.get(If.CONDITION);
34
+
35
+ return new FunctionOutput([
36
+ EventResult.of(condition ? Event.TRUE : Event.FALSE, new Map()),
37
+ EventResult.outputOf(new Map()),
38
+ ]);
39
+ }
40
+ }
@@ -0,0 +1,158 @@
1
+ import { Schema } from '../../../json/schema/Schema';
2
+ import { SchemaType } from '../../../json/schema/type/SchemaType';
3
+ import { TypeUtil } from '../../../json/schema/type/TypeUtil';
4
+ import { Event } from '../../../model/Event';
5
+ import { FunctionSignature } from '../../../model/FunctionSignature';
6
+ import { Parameter } from '../../../model/Parameter';
7
+ import { Namespaces } from '../../../namespaces/Namespaces';
8
+ import { MapUtil } from '../../../util/MapUtil';
9
+ import { AbstractFunction } from '../../AbstractFunction';
10
+
11
+ export abstract class AbstractArrayFunction extends AbstractFunction {
12
+ public static readonly EVENT_INDEX_NAME: string = 'index';
13
+ public static readonly EVENT_RESULT_NAME: string = 'result';
14
+
15
+ public static readonly EVENT_INDEX: Event = new Event(
16
+ Event.OUTPUT,
17
+ MapUtil.of(
18
+ AbstractArrayFunction.EVENT_INDEX_NAME,
19
+ Schema.ofInteger(AbstractArrayFunction.EVENT_INDEX_NAME),
20
+ ),
21
+ );
22
+
23
+ public static readonly EVENT_RESULT_INTEGER: Event = new Event(
24
+ Event.OUTPUT,
25
+ MapUtil.of(
26
+ AbstractArrayFunction.EVENT_RESULT_NAME,
27
+ Schema.ofInteger(AbstractArrayFunction.EVENT_RESULT_NAME),
28
+ ),
29
+ );
30
+
31
+ public static readonly EVENT_RESULT_BOOLEAN: Event = new Event(
32
+ Event.OUTPUT,
33
+ MapUtil.of(
34
+ AbstractArrayFunction.EVENT_RESULT_NAME,
35
+ Schema.ofBoolean(AbstractArrayFunction.EVENT_RESULT_NAME),
36
+ ),
37
+ );
38
+
39
+ public static readonly EVENT_RESULT_ARRAY: Event = new Event(
40
+ Event.OUTPUT,
41
+ MapUtil.of(
42
+ AbstractArrayFunction.EVENT_RESULT_NAME,
43
+ Schema.ofArray(
44
+ AbstractArrayFunction.EVENT_RESULT_NAME,
45
+ Schema.ofAny(AbstractArrayFunction.EVENT_RESULT_NAME),
46
+ ),
47
+ ),
48
+ );
49
+
50
+ public static readonly EVENT_RESULT_EMPTY: Event = new Event(Event.OUTPUT, MapUtil.of());
51
+
52
+ public static readonly EVENT_RESULT_ANY: Event = new Event(
53
+ Event.OUTPUT,
54
+ MapUtil.of(this.EVENT_RESULT_NAME, Schema.ofAny(this.EVENT_RESULT_NAME)),
55
+ );
56
+
57
+ public static readonly PARAMETER_INT_LENGTH: Parameter = Parameter.of(
58
+ 'length',
59
+ Schema.ofInteger('length').setDefaultValue(-1),
60
+ );
61
+ public static readonly PARAMETER_ARRAY_FIND: Parameter = Parameter.of(
62
+ 'find',
63
+ Schema.ofArray('eachFind', Schema.ofAny('eachFind')),
64
+ );
65
+ public static readonly PARAMETER_INT_SOURCE_FROM: Parameter = Parameter.of(
66
+ 'srcFrom',
67
+ Schema.ofInteger('srcFrom').setDefaultValue(0).setMinimum(0),
68
+ );
69
+ public static readonly PARAMETER_INT_SECOND_SOURCE_FROM: Parameter = Parameter.of(
70
+ 'secondSrcFrom',
71
+ Schema.ofInteger('secondSrcFrom').setDefaultValue(0),
72
+ );
73
+ public static readonly PARAMETER_INT_FIND_FROM: Parameter = Parameter.of(
74
+ 'findFrom',
75
+ Schema.ofInteger('findFrom').setDefaultValue(0),
76
+ );
77
+
78
+ public static readonly PARAMETER_INT_OFFSET: Parameter = Parameter.of(
79
+ 'offset',
80
+ Schema.ofInteger('offset').setDefaultValue(0),
81
+ );
82
+
83
+ public static readonly PARAMETER_ROTATE_LENGTH: Parameter = Parameter.of(
84
+ 'rotateLength',
85
+ Schema.ofInteger('rotateLength').setDefaultValue(1).setMinimum(1),
86
+ );
87
+
88
+ public static readonly PARAMETER_BOOLEAN_ASCENDING: Parameter = Parameter.of(
89
+ 'ascending',
90
+ Schema.ofBoolean('ascending').setDefaultValue(false),
91
+ );
92
+
93
+ public static readonly PARAMETER_ARRAY_SOURCE: Parameter = Parameter.of(
94
+ 'source',
95
+ Schema.ofArray('eachSource', Schema.ofAny('eachSource')),
96
+ );
97
+
98
+ public static readonly PARAMETER_ARRAY_SECOND_SOURCE: Parameter = Parameter.of(
99
+ 'secondSource',
100
+ Schema.ofArray('eachSecondSource', Schema.ofAny('eachSecondSource')),
101
+ );
102
+
103
+ public static readonly PARAMETER_ARRAY_SOURCE_PRIMITIVE: Parameter = Parameter.of(
104
+ 'source',
105
+ Schema.ofArray(
106
+ 'eachSource',
107
+ new Schema()
108
+ .setName('eachSource')
109
+ .setType(
110
+ TypeUtil.of(
111
+ SchemaType.STRING,
112
+ SchemaType.NULL,
113
+ SchemaType.INTEGER,
114
+ SchemaType.FLOAT,
115
+ SchemaType.DOUBLE,
116
+ SchemaType.LONG,
117
+ ),
118
+ ),
119
+ ),
120
+ );
121
+
122
+ public static readonly PARAMETER_BOOLEAN_DEEP_COPY: Parameter = Parameter.of(
123
+ 'deepCopy',
124
+ Schema.ofBoolean('deepCopy').setDefaultValue(true),
125
+ );
126
+ public static readonly PARAMETER_ANY: Parameter = Parameter.of(
127
+ 'element',
128
+ Schema.ofAny('element'),
129
+ );
130
+
131
+ public static readonly PARAMETER_ANY_NOT_NULL: Parameter = Parameter.of(
132
+ 'elementObject',
133
+ Schema.ofAnyNotNull('elementObject'),
134
+ );
135
+
136
+ public static readonly PARAMETER_ARRAY_RESULT: Parameter = Parameter.of(
137
+ AbstractArrayFunction.EVENT_RESULT_NAME,
138
+ Schema.ofArray('eachResult', Schema.ofAny('eachResult')),
139
+ );
140
+
141
+ private signature: FunctionSignature;
142
+
143
+ protected constructor(functionName: string, parameters: Parameter[], event: Event) {
144
+ super();
145
+
146
+ const paramMap: Map<string, Parameter> = new Map();
147
+ for (const param of parameters) paramMap.set(param.getParameterName(), param);
148
+
149
+ this.signature = new FunctionSignature(functionName)
150
+ .setNamespace(Namespaces.SYSTEM_ARRAY)
151
+ .setParameters(paramMap)
152
+ .setEvents(MapUtil.of(event.getName(), event));
153
+ }
154
+
155
+ public getSignature(): FunctionSignature {
156
+ return this.signature;
157
+ }
158
+ }
@@ -0,0 +1,31 @@
1
+ import { EventResult } from '../../../model/EventResult';
2
+ import { FunctionOutput } from '../../../model/FunctionOutput';
3
+ import { FunctionExecutionParameters } from '../../../runtime/FunctionExecutionParameters';
4
+ import { AbstractArrayFunction } from './AbstractArrayFunction';
5
+
6
+ export class Add extends AbstractArrayFunction {
7
+ public constructor() {
8
+ super(
9
+ 'Add',
10
+ [
11
+ AbstractArrayFunction.PARAMETER_ARRAY_SOURCE,
12
+ AbstractArrayFunction.PARAMETER_ARRAY_SECOND_SOURCE,
13
+ ],
14
+ AbstractArrayFunction.EVENT_RESULT_EMPTY,
15
+ );
16
+ }
17
+
18
+ protected internalExecute(context: FunctionExecutionParameters): FunctionOutput {
19
+ let source: any[] = context
20
+ ?.getArguments()
21
+ ?.get(AbstractArrayFunction.PARAMETER_ARRAY_SOURCE.getParameterName());
22
+
23
+ let secondSource: any[] = context
24
+ ?.getArguments()
25
+ ?.get(AbstractArrayFunction.PARAMETER_ARRAY_SECOND_SOURCE.getParameterName());
26
+
27
+ source.splice(source.length, 0, ...secondSource);
28
+
29
+ return new FunctionOutput([EventResult.outputOf(new Map([]))]);
30
+ }
31
+ }
@@ -0,0 +1,44 @@
1
+ import { EventResult } from '../../../model/EventResult';
2
+ import { FunctionOutput } from '../../../model/FunctionOutput';
3
+ import { FunctionExecutionParameters } from '../../../runtime/FunctionExecutionParameters';
4
+ import { AbstractArrayFunction } from './AbstractArrayFunction';
5
+
6
+ export class AddFirst extends AbstractArrayFunction {
7
+ public constructor() {
8
+ super(
9
+ 'AddFirst',
10
+ [
11
+ AbstractArrayFunction.PARAMETER_ARRAY_SOURCE,
12
+ AbstractArrayFunction.PARAMETER_ANY_NOT_NULL,
13
+ ],
14
+ AbstractArrayFunction.EVENT_RESULT_EMPTY,
15
+ );
16
+ }
17
+
18
+ protected internalExecute(context: FunctionExecutionParameters): FunctionOutput {
19
+ let source: any[] = context
20
+ ?.getArguments()
21
+ ?.get(AbstractArrayFunction.PARAMETER_ARRAY_SOURCE.getParameterName());
22
+
23
+ let input: any = context
24
+ ?.getArguments()
25
+ ?.get(AbstractArrayFunction.PARAMETER_ANY_NOT_NULL.getParameterName());
26
+
27
+ if (source.length == 0) {
28
+ source.push(input);
29
+ return new FunctionOutput([EventResult.outputOf(new Map([]))]);
30
+ }
31
+
32
+ source.push(input);
33
+
34
+ let len: number = source.length - 1;
35
+
36
+ while (len > 0) {
37
+ let temp: any = source[len - 1];
38
+ source[len - 1] = source[len];
39
+ source[len--] = temp;
40
+ }
41
+
42
+ return new FunctionOutput([EventResult.outputOf(new Map([]))]);
43
+ }
44
+ }
@@ -0,0 +1,66 @@
1
+ import { KIRuntimeException } from '../../../exception/KIRuntimeException';
2
+ import { EventResult } from '../../../model/EventResult';
3
+ import { FunctionOutput } from '../../../model/FunctionOutput';
4
+ import { FunctionExecutionParameters } from '../../../runtime/FunctionExecutionParameters';
5
+ import { PrimitiveUtil } from '../../../util/primitive/PrimitiveUtil';
6
+ import { AbstractArrayFunction } from './AbstractArrayFunction';
7
+
8
+ export class BinarySearch extends AbstractArrayFunction {
9
+ public constructor() {
10
+ super(
11
+ 'BinarySearch',
12
+ [
13
+ BinarySearch.PARAMETER_ARRAY_SOURCE,
14
+ BinarySearch.PARAMETER_INT_SOURCE_FROM,
15
+ BinarySearch.PARAMETER_ANY_NOT_NULL,
16
+ BinarySearch.PARAMETER_INT_LENGTH,
17
+ ],
18
+ BinarySearch.EVENT_INDEX,
19
+ );
20
+ }
21
+
22
+ protected internalExecute(context: FunctionExecutionParameters): FunctionOutput {
23
+ let source: any[] = context
24
+ ?.getArguments()
25
+ ?.get(BinarySearch.PARAMETER_ARRAY_SOURCE.getParameterName());
26
+
27
+ let start: number = context
28
+ ?.getArguments()
29
+ ?.get(BinarySearch.PARAMETER_INT_SOURCE_FROM.getParameterName());
30
+
31
+ let find: any = context
32
+ ?.getArguments()
33
+ ?.get(BinarySearch.PARAMETER_ANY_NOT_NULL.getParameterName());
34
+
35
+ let end: number = context
36
+ ?.getArguments()
37
+ ?.get(BinarySearch.PARAMETER_INT_LENGTH.getParameterName());
38
+
39
+ if (source.length == 0 || start < 0 || start > source.length)
40
+ throw new KIRuntimeException('Search source array cannot be empty');
41
+
42
+ if (end == -1) end = source.length - start;
43
+
44
+ end = start + end;
45
+
46
+ if (end > source.length)
47
+ throw new KIRuntimeException(
48
+ 'End point for array cannot be more than the size of the source array',
49
+ );
50
+
51
+ let index: number = -1;
52
+
53
+ while (start <= end) {
54
+ let mid: number = Math.floor((start + end) / 2);
55
+ if (PrimitiveUtil.compare(source[mid], find) == 0) {
56
+ index = mid;
57
+ break;
58
+ } else if (PrimitiveUtil.compare(source[mid], find) > 0) end = mid - 1;
59
+ else start = mid + 1;
60
+ }
61
+
62
+ return new FunctionOutput([
63
+ EventResult.outputOf(new Map([[BinarySearch.EVENT_INDEX.getName(), index]])),
64
+ ]);
65
+ }
66
+ }
@@ -0,0 +1,150 @@
1
+ import { KIRuntimeException } from '../../../exception/KIRuntimeException';
2
+ import { EventResult } from '../../../model/EventResult';
3
+ import { FunctionOutput } from '../../../model/FunctionOutput';
4
+ import { FunctionExecutionParameters } from '../../../runtime/FunctionExecutionParameters';
5
+ import { ArrayUtil } from '../../../util/ArrayUtil';
6
+ import { MapUtil } from '../../../util/MapUtil';
7
+ import { isNullValue } from '../../../util/NullCheck';
8
+ import { StringFormatter } from '../../../util/string/StringFormatter';
9
+ import { AbstractArrayFunction } from './AbstractArrayFunction';
10
+
11
+ export class Compare extends AbstractArrayFunction {
12
+ public constructor() {
13
+ super(
14
+ 'Compare',
15
+ ArrayUtil.of(
16
+ Compare.PARAMETER_ARRAY_SOURCE,
17
+ Compare.PARAMETER_INT_SOURCE_FROM,
18
+ Compare.PARAMETER_ARRAY_FIND,
19
+ Compare.PARAMETER_INT_FIND_FROM,
20
+ Compare.PARAMETER_INT_LENGTH,
21
+ ),
22
+ Compare.EVENT_RESULT_INTEGER,
23
+ );
24
+ }
25
+
26
+ protected internalExecute(context: FunctionExecutionParameters): FunctionOutput {
27
+ var source = context
28
+ ?.getArguments()
29
+ ?.get(Compare.PARAMETER_ARRAY_SOURCE.getParameterName());
30
+ var srcfrom = context
31
+ ?.getArguments()
32
+ ?.get(Compare.PARAMETER_INT_SOURCE_FROM.getParameterName());
33
+ var find = context?.getArguments()?.get(Compare.PARAMETER_ARRAY_FIND.getParameterName());
34
+ var findfrom = context
35
+ ?.getArguments()
36
+ ?.get(Compare.PARAMETER_INT_FIND_FROM.getParameterName());
37
+ var length = context?.getArguments()?.get(Compare.PARAMETER_INT_LENGTH.getParameterName());
38
+
39
+ if (source.length == 0) {
40
+ throw new KIRuntimeException('Compare source array cannot be empty');
41
+ }
42
+
43
+ if (find.length == 0) {
44
+ throw new KIRuntimeException('Compare find array cannot be empty');
45
+ }
46
+
47
+ if (length == -1) length = source.length - srcfrom;
48
+
49
+ if (srcfrom + length > source.length)
50
+ throw new KIRuntimeException(
51
+ StringFormatter.format(
52
+ 'Source array size $ is less than comparing size $',
53
+ source.length,
54
+ srcfrom + length,
55
+ ),
56
+ );
57
+
58
+ if (findfrom + length > find.length)
59
+ throw new KIRuntimeException(
60
+ StringFormatter.format(
61
+ 'Find array size $ is less than comparing size $',
62
+ find.length,
63
+ findfrom + length,
64
+ ),
65
+ );
66
+
67
+ return new FunctionOutput(
68
+ ArrayUtil.of(
69
+ EventResult.outputOf(
70
+ MapUtil.of(
71
+ Compare.EVENT_RESULT_NAME,
72
+ this.compare(
73
+ source,
74
+ srcfrom,
75
+ srcfrom + length,
76
+ find,
77
+ findfrom,
78
+ findfrom + length,
79
+ ),
80
+ ),
81
+ ),
82
+ ),
83
+ );
84
+ }
85
+
86
+ public compare(
87
+ source: any[],
88
+ srcfrom: number,
89
+ srcto: number,
90
+ find: any[],
91
+ findfrom: number,
92
+ findto: number,
93
+ ): number {
94
+ if (srcto < srcfrom) {
95
+ let x: number = srcfrom;
96
+ srcfrom = srcto;
97
+ srcto = x;
98
+ }
99
+
100
+ if (findto < findfrom) {
101
+ let x: number = findfrom;
102
+ findfrom = findto;
103
+ findto = x;
104
+ }
105
+
106
+ if (srcto - srcfrom != findto - findfrom) {
107
+ throw new KIRuntimeException(
108
+ StringFormatter.format(
109
+ 'Cannot compare uneven arrays from $ to $ in source array with $ to $ in find array',
110
+ srcto,
111
+ srcfrom,
112
+ findto,
113
+ findfrom,
114
+ ),
115
+ );
116
+ }
117
+
118
+ for (let i = srcfrom, j = findfrom; i < srcto; i++, j++) {
119
+ let x: number = 1;
120
+
121
+ if (isNullValue(source[i]) || isNullValue(find[j])) {
122
+ let s: boolean = isNullValue(source[i]);
123
+ let f: boolean = isNullValue(find[j]);
124
+
125
+ if (s == f) x = 0;
126
+ else if (s) x = -1;
127
+ } else {
128
+ let typs: string = typeof source[i];
129
+ let typf: string = typeof find[j];
130
+
131
+ if (typs === 'object' || typf === 'object') {
132
+ x = 1;
133
+ } else if (typs === 'string' || typf === 'string') {
134
+ let s = '' + source[i];
135
+ let f = '' + find[j];
136
+ if (s === f) x = 0;
137
+ else if (s < f) x = -1;
138
+ } else if (typs === 'boolean' || typf === 'boolean') {
139
+ x = typs == typf ? 0 : 1;
140
+ } else if (typs === 'number' && typf === 'number') {
141
+ x = source[i] - find[j];
142
+ }
143
+ }
144
+
145
+ if (x != 0) return x;
146
+ }
147
+
148
+ return 0;
149
+ }
150
+ }