@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,244 @@
1
+ import { ExecutionException } from '../../../exception/ExecutionException';
2
+ import { KIRuntimeException } from '../../../exception/KIRuntimeException';
3
+ import { Schema } from '../../../json/schema/Schema';
4
+ import { SchemaType } from '../../../json/schema/type/SchemaType';
5
+ import { TypeUtil } from '../../../json/schema/type/TypeUtil';
6
+ import { Event } from '../../../model/Event';
7
+ import { EventResult } from '../../../model/EventResult';
8
+ import { FunctionOutput } from '../../../model/FunctionOutput';
9
+ import { FunctionSignature } from '../../../model/FunctionSignature';
10
+ import { Parameter } from '../../../model/Parameter';
11
+ import { ParameterType } from '../../../model/ParameterType';
12
+ import { Namespaces } from '../../../namespaces/Namespaces';
13
+ import { ContextElement } from '../../../runtime/ContextElement';
14
+ import { Expression } from '../../../runtime/expression/Expression';
15
+ import { ExpressionEvaluator } from '../../../runtime/expression/ExpressionEvaluator';
16
+ import { ExpressionToken } from '../../../runtime/expression/ExpressionToken';
17
+ import { ExpressionTokenValue } from '../../../runtime/expression/ExpressionTokenValue';
18
+ import { Operation } from '../../../runtime/expression/Operation';
19
+ import { FunctionExecutionParameters } from '../../../runtime/FunctionExecutionParameters';
20
+ import { LinkedList } from '../../../util/LinkedList';
21
+ import { isNullValue } from '../../../util/NullCheck';
22
+ import { PrimitiveUtil } from '../../../util/primitive/PrimitiveUtil';
23
+ import { StringFormatter } from '../../../util/string/StringFormatter';
24
+ import { StringUtil } from '../../../util/string/StringUtil';
25
+ import { Tuple2 } from '../../../util/Tuples';
26
+ import { AbstractFunction } from '../../AbstractFunction';
27
+
28
+ const NAME = 'name';
29
+ const VALUE = 'value';
30
+ const SIGNATURE = new FunctionSignature('Set')
31
+ .setNamespace(Namespaces.SYSTEM_CTX)
32
+ .setParameters(
33
+ new Map([
34
+ Parameter.ofEntry(
35
+ NAME,
36
+ new Schema().setName(NAME).setType(TypeUtil.of(SchemaType.STRING)).setMinLength(1),
37
+ false,
38
+ ParameterType.CONSTANT,
39
+ ),
40
+ Parameter.ofEntry(VALUE, Schema.ofAny(VALUE)),
41
+ ]),
42
+ )
43
+ .setEvents(new Map([Event.outputEventMapEntry(new Map())]));
44
+
45
+ export class SetFunction extends AbstractFunction {
46
+ public getSignature(): FunctionSignature {
47
+ return SIGNATURE;
48
+ }
49
+
50
+ protected internalExecute(context: FunctionExecutionParameters): FunctionOutput {
51
+ let key: string = context?.getArguments()?.get(NAME);
52
+
53
+ if (StringUtil.isNullOrBlank(key)) {
54
+ throw new KIRuntimeException(
55
+ 'Empty string is not a valid name for the context element',
56
+ );
57
+ }
58
+
59
+ let value: any = context?.getArguments()?.get(VALUE);
60
+
61
+ const exp: Expression = new Expression(key);
62
+
63
+ const contextToken: ExpressionToken = exp.getTokens().peekLast();
64
+
65
+ if (
66
+ !contextToken.getExpression().startsWith('Context') ||
67
+ contextToken instanceof Expression ||
68
+ (contextToken instanceof ExpressionTokenValue &&
69
+ !(contextToken as ExpressionTokenValue)
70
+ .getElement()
71
+ .toString()
72
+ .startsWith('Context'))
73
+ ) {
74
+ throw new ExecutionException(
75
+ StringFormatter.format('The context path $ is not a valid path in context', key),
76
+ );
77
+ }
78
+
79
+ for (const op of exp.getOperations().toArray()) {
80
+ if (op == Operation.ARRAY_OPERATOR || op == Operation.OBJECT_OPERATOR) continue;
81
+
82
+ throw new ExecutionException(
83
+ StringFormatter.format(
84
+ 'Expected a reference to the context location, but found an expression $',
85
+ key,
86
+ ),
87
+ );
88
+ }
89
+
90
+ for (let i = 0; i < exp.getTokens().size(); i++) {
91
+ let ex = exp.getTokens().get(i);
92
+ if (ex instanceof Expression)
93
+ exp.getTokens().set(
94
+ i,
95
+ new ExpressionTokenValue(
96
+ key,
97
+ new ExpressionEvaluator(ex as Expression).evaluate(context.getValuesMap()),
98
+ ),
99
+ );
100
+ }
101
+
102
+ let tokens: LinkedList<ExpressionToken> = exp.getTokens();
103
+ tokens.removeLast();
104
+ let ops: LinkedList<Operation> = exp.getOperations();
105
+ ops.removeLast();
106
+ let ce: ContextElement | undefined = context
107
+ .getContext()
108
+ ?.get(tokens.removeLast().getExpression());
109
+
110
+ if (!ce) {
111
+ throw new KIRuntimeException(
112
+ StringFormatter.format("Context doesn't have any element with name '$' ", key),
113
+ );
114
+ }
115
+
116
+ if (ops.isEmpty()) {
117
+ ce.setElement(value);
118
+ return new FunctionOutput([EventResult.outputOf(new Map())]);
119
+ }
120
+
121
+ let el: any = ce.getElement();
122
+
123
+ while (ops.size() > 1) {
124
+ if (isNullValue(el)) {
125
+ throw new KIRuntimeException(
126
+ StringFormatter.format('Unable to set the context in the path $', key),
127
+ );
128
+ }
129
+
130
+ const op: Operation = ops.removeLast();
131
+ const token: ExpressionToken = tokens.removeLast();
132
+ if (op == Operation.OBJECT_OPERATOR) {
133
+ if (typeof el != 'object') {
134
+ throw new KIRuntimeException(
135
+ StringFormatter.format('$ has no object in the context', key),
136
+ );
137
+ }
138
+
139
+ let mem: string | undefined = undefined;
140
+ if (token instanceof ExpressionTokenValue)
141
+ mem = (token as ExpressionTokenValue).getTokenValue().getAsString();
142
+ else mem = token.getExpression();
143
+
144
+ el = el.getAsJsonObject().get(mem);
145
+ } else {
146
+ const je: any =
147
+ token instanceof ExpressionTokenValue
148
+ ? (token as ExpressionTokenValue).getElement()
149
+ : token.getExpression();
150
+
151
+ if (Array.isArray(je) || typeof je === 'object') {
152
+ throw new KIRuntimeException(
153
+ StringFormatter.format(
154
+ 'Cannot extract json with key $ from the object $',
155
+ je,
156
+ el,
157
+ ),
158
+ );
159
+ }
160
+
161
+ if (Array.isArray(el)) {
162
+ const prim: Tuple2<SchemaType, any> = PrimitiveUtil.findPrimitive(je);
163
+
164
+ if (prim.getT1() != SchemaType.INTEGER || prim.getT1() != SchemaType.LONG) {
165
+ throw new KIRuntimeException(
166
+ StringFormatter.format('Expecting a numerical index but found $', je),
167
+ );
168
+ }
169
+
170
+ let index = prim.getT2() as number;
171
+ if (index >= el.length)
172
+ throw new KIRuntimeException(
173
+ StringFormatter.format('Index out of bound while accessing $', key),
174
+ );
175
+ el = el[index];
176
+ } else {
177
+ const mem: string = je.getAsString();
178
+ el = el[mem];
179
+ }
180
+ }
181
+ }
182
+
183
+ if (el == null) {
184
+ throw new KIRuntimeException(
185
+ StringFormatter.format('Unable to set the context in the path $', key),
186
+ );
187
+ }
188
+
189
+ const op: Operation = ops.removeLast();
190
+ const token: ExpressionToken = tokens.removeLast();
191
+
192
+ // TODO: Here I need to validate the schema of the value I have to put in the
193
+ // context.
194
+
195
+ if (op == Operation.OBJECT_OPERATOR) {
196
+ if (typeof el == 'object') {
197
+ throw new KIRuntimeException(
198
+ StringFormatter.format('$ has no object in the context', key),
199
+ );
200
+ }
201
+
202
+ let mem: string;
203
+ if (token instanceof ExpressionTokenValue)
204
+ mem = (token as ExpressionTokenValue).getTokenValue();
205
+ else mem = token.getExpression();
206
+
207
+ el[mem] = value;
208
+ } else {
209
+ let je =
210
+ token instanceof ExpressionTokenValue
211
+ ? (token as ExpressionTokenValue).getElement()
212
+ : token.getExpression();
213
+
214
+ if (Array.isArray(je) || typeof je === 'object') {
215
+ throw new KIRuntimeException(
216
+ StringFormatter.format(
217
+ 'Cannot extract json with key $ from the object $',
218
+ je,
219
+ el,
220
+ ),
221
+ );
222
+ }
223
+
224
+ if (Array.isArray(el)) {
225
+ const prim: Tuple2<SchemaType, any> = PrimitiveUtil.findPrimitive(je);
226
+
227
+ if (prim.getT1() != SchemaType.INTEGER && prim.getT1() != SchemaType.LONG) {
228
+ throw new KIRuntimeException(
229
+ StringFormatter.format('Expecting a numerical index but found $', je),
230
+ );
231
+ }
232
+
233
+ let index = prim.getT2() as number;
234
+ while (index >= el.length) el.push(undefined);
235
+ el[index] = value;
236
+ } else {
237
+ let mem: string = je;
238
+ el[mem] = value;
239
+ }
240
+ }
241
+
242
+ return new FunctionOutput([EventResult.outputOf(new Map())]);
243
+ }
244
+ }
@@ -0,0 +1,55 @@
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
+ const COUNT = 'count';
13
+ const VALUE = 'value';
14
+ const INDEX = 'index';
15
+
16
+ const SIGNATURE = new FunctionSignature('CountLoop')
17
+ .setNamespace(Namespaces.SYSTEM_LOOP)
18
+ .setParameters(
19
+ new Map([
20
+ Parameter.ofEntry(COUNT, Schema.of(COUNT, SchemaType.INTEGER).setDefaultValue(1)),
21
+ ]),
22
+ )
23
+ .setEvents(
24
+ new Map([
25
+ Event.eventMapEntry(
26
+ Event.ITERATION,
27
+ new Map([[INDEX, Schema.of(INDEX, SchemaType.INTEGER)]]),
28
+ ),
29
+ Event.outputEventMapEntry(new Map([[VALUE, Schema.of(VALUE, SchemaType.INTEGER)]])),
30
+ ]),
31
+ );
32
+
33
+ export class CountLoop extends AbstractFunction {
34
+ public getSignature(): FunctionSignature {
35
+ return SIGNATURE;
36
+ }
37
+
38
+ protected internalExecute(context: FunctionExecutionParameters): FunctionOutput {
39
+ let count: number = context.getArguments()?.get(COUNT);
40
+ let current = 0;
41
+
42
+ return new FunctionOutput({
43
+ next(): EventResult {
44
+ if (current >= count) {
45
+ return EventResult.outputOf(new Map([[VALUE, current]]));
46
+ }
47
+
48
+ const eve = EventResult.of(Event.ITERATION, new Map([[INDEX, current]]));
49
+ ++current;
50
+
51
+ return eve;
52
+ },
53
+ });
54
+ }
55
+ }
@@ -0,0 +1,120 @@
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
+ const FROM = 'from';
13
+ const TO = 'to';
14
+ const STEP = 'step';
15
+ const VALUE = 'value';
16
+ const INDEX = 'index';
17
+
18
+ const SIGNATURE = new FunctionSignature('RangeLoop')
19
+ .setNamespace(Namespaces.SYSTEM_LOOP)
20
+ .setParameters(
21
+ new Map([
22
+ Parameter.ofEntry(
23
+ FROM,
24
+ Schema.of(
25
+ FROM,
26
+ SchemaType.INTEGER,
27
+ SchemaType.LONG,
28
+ SchemaType.FLOAT,
29
+ SchemaType.DOUBLE,
30
+ ).setDefaultValue(0),
31
+ ),
32
+ Parameter.ofEntry(
33
+ TO,
34
+ Schema.of(
35
+ TO,
36
+ SchemaType.INTEGER,
37
+ SchemaType.LONG,
38
+ SchemaType.FLOAT,
39
+ SchemaType.DOUBLE,
40
+ ).setDefaultValue(1),
41
+ ),
42
+ Parameter.ofEntry(
43
+ STEP,
44
+ Schema.of(
45
+ STEP,
46
+ SchemaType.INTEGER,
47
+ SchemaType.LONG,
48
+ SchemaType.FLOAT,
49
+ SchemaType.DOUBLE,
50
+ )
51
+ .setDefaultValue(1)
52
+ .setNot(new Schema().setConstant(0)),
53
+ ),
54
+ ]),
55
+ )
56
+ .setEvents(
57
+ new Map([
58
+ Event.eventMapEntry(
59
+ Event.ITERATION,
60
+ new Map([
61
+ [
62
+ INDEX,
63
+ Schema.of(
64
+ INDEX,
65
+ SchemaType.INTEGER,
66
+ SchemaType.LONG,
67
+ SchemaType.FLOAT,
68
+ SchemaType.DOUBLE,
69
+ ),
70
+ ],
71
+ ]),
72
+ ),
73
+ Event.outputEventMapEntry(
74
+ new Map([
75
+ [
76
+ VALUE,
77
+ Schema.of(
78
+ VALUE,
79
+ SchemaType.INTEGER,
80
+ SchemaType.LONG,
81
+ SchemaType.FLOAT,
82
+ SchemaType.DOUBLE,
83
+ ),
84
+ ],
85
+ ]),
86
+ ),
87
+ ]),
88
+ );
89
+
90
+ export class RangeLoop extends AbstractFunction {
91
+ public getSignature(): FunctionSignature {
92
+ return SIGNATURE;
93
+ }
94
+
95
+ protected internalExecute(context: FunctionExecutionParameters): FunctionOutput {
96
+ let from: number = context.getArguments()?.get(FROM);
97
+ let to: number = context.getArguments()?.get(TO);
98
+ let step: number = context.getArguments()?.get(STEP);
99
+
100
+ const forward = step > 0;
101
+ let current: number = from;
102
+ let done: boolean = false;
103
+
104
+ return new FunctionOutput({
105
+ next(): EventResult | undefined {
106
+ if (done) return undefined;
107
+
108
+ if ((forward && current >= to) || (!forward && current <= to)) {
109
+ done = true;
110
+ return EventResult.outputOf(new Map([[VALUE, current]]));
111
+ }
112
+
113
+ const eve = EventResult.of(Event.ITERATION, new Map([[INDEX, current]]));
114
+ current += step;
115
+
116
+ return eve;
117
+ },
118
+ });
119
+ }
120
+ }
@@ -0,0 +1,34 @@
1
+ import { Schema } from '../../../json/schema/Schema';
2
+ import { Event } from '../../../model/Event';
3
+ import { EventResult } from '../../../model/EventResult';
4
+ import { FunctionOutput } from '../../../model/FunctionOutput';
5
+ import { FunctionSignature } from '../../../model/FunctionSignature';
6
+ import { Parameter } from '../../../model/Parameter';
7
+ import { Namespaces } from '../../../namespaces/Namespaces';
8
+ import { FunctionExecutionParameters } from '../../../runtime/FunctionExecutionParameters';
9
+ import { AbstractFunction } from '../../AbstractFunction';
10
+
11
+ const VALUE = 'value';
12
+
13
+ const SIGNATURE = new FunctionSignature('Add')
14
+ .setNamespace(Namespaces.MATH)
15
+ .setParameters(
16
+ new Map([
17
+ [VALUE, new Parameter(VALUE, Schema.ofNumber(VALUE)).setVariableArgument(true)],
18
+ ]),
19
+ )
20
+ .setEvents(new Map([Event.outputEventMapEntry(new Map([[VALUE, Schema.ofNumber(VALUE)]]))]));
21
+
22
+ export class Add extends AbstractFunction {
23
+ public getSignature(): FunctionSignature {
24
+ return SIGNATURE;
25
+ }
26
+
27
+ protected internalExecute(context: FunctionExecutionParameters): FunctionOutput {
28
+ let nums: number[] = context.getArguments()?.get(VALUE);
29
+
30
+ return new FunctionOutput([
31
+ EventResult.outputOf(new Map([[VALUE, nums.reduce((a, c) => (a += c), 0)]])),
32
+ ]);
33
+ }
34
+ }
@@ -0,0 +1,80 @@
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 { EventResult } from '../../../model/EventResult';
6
+ import { FunctionOutput } from '../../../model/FunctionOutput';
7
+ import { FunctionSignature } from '../../../model/FunctionSignature';
8
+ import { Parameter } from '../../../model/Parameter';
9
+ import { Namespaces } from '../../../namespaces/Namespaces';
10
+ import { FunctionExecutionParameters } from '../../../runtime/FunctionExecutionParameters';
11
+ import { PrimitiveUtil } from '../../../util/primitive/PrimitiveUtil';
12
+ import { AbstractFunction } from '../../AbstractFunction';
13
+
14
+ const VALUE = 'value';
15
+ const VALUE1 = 'value1';
16
+ const VALUE2 = 'value2';
17
+
18
+ const paramFunctions = [
19
+ () => {
20
+ return new Map([
21
+ [VALUE,new Parameter(VALUE,Schema.ofNumber(VALUE))],
22
+ ]);
23
+ },
24
+ () => {
25
+ return new Map([
26
+ [VALUE1, new Parameter(VALUE1, Schema.ofNumber(VALUE1))],
27
+ [VALUE2, new Parameter(VALUE2, Schema.ofNumber(VALUE2))],
28
+ ]);
29
+ },
30
+ ];
31
+
32
+ export class GenericMathFunction extends AbstractFunction {
33
+ private signature: FunctionSignature;
34
+ private parametersNumber: number;
35
+ private mathFunction: (v1: number, v2?: number) => number;
36
+
37
+ public constructor(
38
+ functionName: string,
39
+ mathFunction: (v1: number, v2?: number) => number,
40
+ parametersNumber: number = 1,
41
+ ...returnType: SchemaType[]
42
+ ) {
43
+ super();
44
+ if (!returnType || !returnType.length) returnType = [SchemaType.DOUBLE];
45
+ this.parametersNumber = parametersNumber;
46
+ this.mathFunction = mathFunction;
47
+ this.signature = new FunctionSignature(functionName)
48
+ .setNamespace(Namespaces.MATH)
49
+ .setParameters(paramFunctions[parametersNumber - 1]())
50
+ .setEvents(
51
+ new Map([
52
+ Event.outputEventMapEntry(
53
+ new Map([
54
+ [
55
+ VALUE,
56
+ new Schema().setType(TypeUtil.of(...returnType)).setName(VALUE),
57
+ ],
58
+ ]),
59
+ ) as [string, Event],
60
+ ]),
61
+ );
62
+ }
63
+
64
+ public getSignature(): FunctionSignature {
65
+ return this.signature;
66
+ }
67
+
68
+ protected internalExecute(context: FunctionExecutionParameters): FunctionOutput {
69
+ let v1 = PrimitiveUtil.findPrimitiveNumberType(
70
+ context.getArguments()?.get(this.parametersNumber == 1 ? VALUE : VALUE1),
71
+ ).getT2();
72
+ let v2;
73
+ if (this.parametersNumber == 2)
74
+ v2 = PrimitiveUtil.findPrimitiveNumberType(context.getArguments()?.get(VALUE2)).getT2();
75
+
76
+ return new FunctionOutput([
77
+ EventResult.outputOf(new Map([[VALUE, this.mathFunction.call(this, v1, v2)]])),
78
+ ]);
79
+ }
80
+ }
@@ -0,0 +1,57 @@
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 { EventResult } from '../../../model/EventResult';
6
+ import { FunctionOutput } from '../../../model/FunctionOutput';
7
+ import { FunctionSignature } from '../../../model/FunctionSignature';
8
+ import { Parameter } from '../../../model/Parameter';
9
+ import { Namespaces } from '../../../namespaces/Namespaces';
10
+ import { FunctionExecutionParameters } from '../../../runtime/FunctionExecutionParameters';
11
+ import { AbstractFunction } from '../../AbstractFunction';
12
+
13
+ const VALUE = 'value';
14
+
15
+ export class Hypotenuse extends AbstractFunction {
16
+ private static readonly SIGNATURE: FunctionSignature = new FunctionSignature('Hypotenuse')
17
+ .setNamespace(Namespaces.MATH)
18
+ .setParameters(
19
+ new Map([
20
+ [
21
+ VALUE,
22
+ new Parameter(VALUE,Schema.ofNumber(VALUE))
23
+ .setVariableArgument(true),
24
+ ],
25
+ ]),
26
+ )
27
+ .setEvents(
28
+ new Map([
29
+ Event.outputEventMapEntry(
30
+ new Map([
31
+ [
32
+ VALUE,
33
+ new Schema().setType(TypeUtil.of(SchemaType.DOUBLE)).setName(VALUE),
34
+ ],
35
+ ]),
36
+ ) as [string, Event],
37
+ ]),
38
+ );
39
+
40
+ public constructor() {
41
+ super();
42
+ }
43
+
44
+ public getSignature(): FunctionSignature {
45
+ return Hypotenuse.SIGNATURE;
46
+ }
47
+
48
+ protected internalExecute(context: FunctionExecutionParameters): FunctionOutput {
49
+ let nums: number[] = context.getArguments()?.get(VALUE);
50
+
51
+ return new FunctionOutput([
52
+ EventResult.outputOf(
53
+ new Map([[VALUE, Math.sqrt(nums.reduce((a, c) => (a += c * c), 0))]]),
54
+ ),
55
+ ]);
56
+ }
57
+ }
@@ -0,0 +1,63 @@
1
+ import { SchemaType } from '../../../json/schema/type/SchemaType';
2
+ import { Namespaces } from '../../../namespaces/Namespaces';
3
+ import { Repository } from '../../../Repository';
4
+ import { Function } from '../../Function';
5
+ import { Add } from './Add';
6
+ import { GenericMathFunction } from './GenericMathFunction';
7
+ import { Hypotenuse } from './Hypotenuse';
8
+ import { Maximum } from './Maximum';
9
+ import { Minimum } from './Minimum';
10
+ import { Random } from './Random';
11
+
12
+ const functionObjectsIndex: any = {
13
+ Absolute: new GenericMathFunction(
14
+ 'Absolute',
15
+ (v) => Math.abs(v),
16
+ 1,
17
+ SchemaType.INTEGER,
18
+ SchemaType.LONG,
19
+ SchemaType.FLOAT,
20
+ SchemaType.DOUBLE,
21
+ ),
22
+ ACosine: new GenericMathFunction('ArcCosine', (v) => Math.acos(v)),
23
+ ASine: new GenericMathFunction('ArcSine', (v) => Math.asin(v)),
24
+ ATangent: new GenericMathFunction('ArcTangent', (v) => Math.atan(v)),
25
+ Ceiling: new GenericMathFunction('Ceiling', (v) => Math.ceil(v)),
26
+ Cosine: new GenericMathFunction('Cosine', (v) => Math.cos(v)),
27
+ CosineH: new GenericMathFunction('HyperbolicCosine', (v) => Math.cosh(v)),
28
+ CubeRoot: new GenericMathFunction('CubeRoot', (v) => Math.cbrt(v)),
29
+ Exponential: new GenericMathFunction('Exponential', (v) => Math.exp(v)),
30
+ Expm1: new GenericMathFunction('ExponentialMinus1', (v) => Math.expm1(v)),
31
+ Floor: new GenericMathFunction('Floor', (v) => Math.floor(v)),
32
+ Log: new GenericMathFunction('LogNatural', (v) => Math.log(v)),
33
+ Log10: new GenericMathFunction('Log10', (v) => Math.log10(v)),
34
+ Round: new GenericMathFunction(
35
+ 'Round',
36
+ (v) => Math.round(v),
37
+ 1,
38
+ SchemaType.INTEGER,
39
+ SchemaType.LONG,
40
+ ),
41
+ Sine: new GenericMathFunction('Sine', (v) => Math.sin(v)),
42
+ SineH: new GenericMathFunction('HyperbolicSine', (v) => Math.sinh(v)),
43
+ Tangent: new GenericMathFunction('Tangent', (v) => Math.tan(v)),
44
+ TangentH: new GenericMathFunction('HyperbolicTangent', (v) => Math.tanh(v)),
45
+ ToDegrees: new GenericMathFunction('ToDegrees', (v) => v * (Math.PI / 180)),
46
+ ToRadians: new GenericMathFunction('ToRadians', (v) => v * (180 / Math.PI)),
47
+ SquareRoot: new GenericMathFunction('SquareRoot', (v) => Math.sqrt(v)),
48
+ ArcTangent: new GenericMathFunction('ArcTangent2', (v1, v2) => Math.atan2(v1, v2!), 2),
49
+ Power: new GenericMathFunction('Power', (v1, v2) => Math.pow(v1, v2!), 2),
50
+ Add: new Add(),
51
+ Hypotenuse: new Hypotenuse(),
52
+ Maximum: new Maximum(),
53
+ Minimum: new Minimum(),
54
+ Random: new Random(),
55
+ };
56
+
57
+ export class MathFunctionRepository implements Repository<Function> {
58
+ find(namespace: string, name: string): Function | undefined {
59
+ if (namespace != Namespaces.MATH) return undefined;
60
+
61
+ return functionObjectsIndex[name];
62
+ }
63
+ }