@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.
- package/.prettierrc +9 -0
- package/__tests__/engine/function/system/array/AddFirstTest.ts +235 -0
- package/__tests__/engine/function/system/array/AddTest.ts +126 -0
- package/__tests__/engine/function/system/array/BinarySearchTest.ts +135 -0
- package/__tests__/engine/function/system/array/CompareTest.ts +48 -0
- package/__tests__/engine/function/system/array/CopyTest.ts +86 -0
- package/__tests__/engine/function/system/array/DeleteFirstTest.ts +103 -0
- package/__tests__/engine/function/system/array/DeleteFromTest.ts +232 -0
- package/__tests__/engine/function/system/array/DeleteLastTest.ts +103 -0
- package/__tests__/engine/function/system/array/DeleteTest.ts +110 -0
- package/__tests__/engine/function/system/array/DisjointTest.ts +184 -0
- package/__tests__/engine/function/system/array/Equals.ts +67 -0
- package/__tests__/engine/function/system/array/FillTest.ts +36 -0
- package/__tests__/engine/function/system/array/FrequencyTest.ts +97 -0
- package/__tests__/engine/function/system/array/IndexOfArrayTest.ts +347 -0
- package/__tests__/engine/function/system/array/IndexOfTest.ts +267 -0
- package/__tests__/engine/function/system/array/InsertTest.ts +229 -0
- package/__tests__/engine/function/system/array/LastIndexOfArrayTest.ts +213 -0
- package/__tests__/engine/function/system/array/LastIndexOfTest.ts +253 -0
- package/__tests__/engine/function/system/array/MaxTest.ts +98 -0
- package/__tests__/engine/function/system/array/MinTest.ts +99 -0
- package/__tests__/engine/function/system/array/MisMatchTest.ts +215 -0
- package/__tests__/engine/function/system/array/ReverseTest.ts +235 -0
- package/__tests__/engine/function/system/array/RotateTest.ts +137 -0
- package/__tests__/engine/function/system/array/ShuffleTest.ts +154 -0
- package/__tests__/engine/function/system/array/SortTest.ts +130 -0
- package/__tests__/engine/function/system/array/SubArrayTest.ts +236 -0
- package/__tests__/engine/function/system/math/AddTest.ts +12 -0
- package/__tests__/engine/function/system/string/ConcatenateTest.ts +46 -0
- package/__tests__/engine/function/system/string/DeleteForGivenLengthTest.ts +38 -0
- package/__tests__/engine/function/system/string/InsertAtGivenPositionTest.ts +53 -0
- package/__tests__/engine/function/system/string/PostPadTest.ts +54 -0
- package/__tests__/engine/function/system/string/PrePadTest.ts +54 -0
- package/__tests__/engine/function/system/string/RegionMatchesTest.ts +78 -0
- package/__tests__/engine/function/system/string/ReverseTest.ts +36 -0
- package/__tests__/engine/function/system/string/SplitTest.ts +61 -0
- package/__tests__/engine/function/system/string/StringFunctionRepoTest2.ts +126 -0
- package/__tests__/engine/function/system/string/StringFunctionRepoTest3.ts +54 -0
- package/__tests__/engine/function/system/string/StringFunctionRepositoryTest.ts +146 -0
- package/__tests__/engine/function/system/string/ToStringTest.ts +43 -0
- package/__tests__/engine/function/system/string/TrimToTest.ts +28 -0
- package/__tests__/engine/json/schema/validator/SchemaValidatorTest.ts +26 -0
- package/__tests__/engine/runtime/KIRuntimeTest.ts +351 -0
- package/__tests__/engine/runtime/expression/ExpressionEvaluationTest.ts +128 -0
- package/__tests__/engine/runtime/expression/ExpressionTest.ts +33 -0
- package/__tests__/engine/runtime/expression/tokenextractor/OutputMapTokenValueExtractorTest.ts +44 -0
- package/__tests__/engine/runtime/expression/tokenextractor/TokenValueExtractorTest.ts +72 -0
- package/__tests__/engine/util/LinkedListTest.ts +29 -0
- package/__tests__/engine/util/string/StringFormatterTest.ts +17 -0
- package/__tests__/indexTest.ts +33 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/module.js +2 -0
- package/dist/module.js.map +1 -0
- package/dist/types.d.ts +430 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +54 -0
- package/src/engine/HybridRepository.ts +18 -0
- package/src/engine/Repository.ts +3 -0
- package/src/engine/constant/KIRunConstants.ts +7 -0
- package/src/engine/exception/ExecutionException.ts +12 -0
- package/src/engine/exception/KIRuntimeException.ts +12 -0
- package/src/engine/function/AbstractFunction.ts +76 -0
- package/src/engine/function/Function.ts +13 -0
- package/src/engine/function/system/GenerateEvent.ts +76 -0
- package/src/engine/function/system/If.ts +40 -0
- package/src/engine/function/system/array/AbstractArrayFunction.ts +158 -0
- package/src/engine/function/system/array/Add.ts +31 -0
- package/src/engine/function/system/array/AddFirst.ts +44 -0
- package/src/engine/function/system/array/BinarySearch.ts +66 -0
- package/src/engine/function/system/array/Compare.ts +150 -0
- package/src/engine/function/system/array/Copy.ts +56 -0
- package/src/engine/function/system/array/Delete.ts +63 -0
- package/src/engine/function/system/array/DeleteFirst.ts +22 -0
- package/src/engine/function/system/array/DeleteFrom.ts +51 -0
- package/src/engine/function/system/array/DeleteLast.ts +23 -0
- package/src/engine/function/system/array/Disjoint.ts +85 -0
- package/src/engine/function/system/array/Equals.ts +36 -0
- package/src/engine/function/system/array/Fill.ts +51 -0
- package/src/engine/function/system/array/Frequency.ts +68 -0
- package/src/engine/function/system/array/IndexOf.ts +56 -0
- package/src/engine/function/system/array/IndexOfArray.ts +67 -0
- package/src/engine/function/system/array/Insert.ts +48 -0
- package/src/engine/function/system/array/LastIndexOf.ts +62 -0
- package/src/engine/function/system/array/LastIndexOfArray.ts +70 -0
- package/src/engine/function/system/array/Max.ts +31 -0
- package/src/engine/function/system/array/Min.ts +32 -0
- package/src/engine/function/system/array/MisMatch.ts +66 -0
- package/src/engine/function/system/array/Reverse.ts +50 -0
- package/src/engine/function/system/array/Rotate.ts +43 -0
- package/src/engine/function/system/array/Shuffle.ts +31 -0
- package/src/engine/function/system/array/Sort.ts +70 -0
- package/src/engine/function/system/array/SubArray.ts +48 -0
- package/src/engine/function/system/context/Create.ts +73 -0
- package/src/engine/function/system/context/Get.ts +55 -0
- package/src/engine/function/system/context/SetFunction.ts +244 -0
- package/src/engine/function/system/loop/CountLoop.ts +55 -0
- package/src/engine/function/system/loop/RangeLoop.ts +120 -0
- package/src/engine/function/system/math/Add.ts +34 -0
- package/src/engine/function/system/math/GenericMathFunction.ts +80 -0
- package/src/engine/function/system/math/Hypotenuse.ts +57 -0
- package/src/engine/function/system/math/MathFunctionRepository.ts +63 -0
- package/src/engine/function/system/math/Maximum.ts +38 -0
- package/src/engine/function/system/math/Minimum.ts +38 -0
- package/src/engine/function/system/math/Random.ts +27 -0
- package/src/engine/function/system/string/AbstractStringFunction.ts +409 -0
- package/src/engine/function/system/string/Concatenate.ts +57 -0
- package/src/engine/function/system/string/DeleteForGivenLength.ts +82 -0
- package/src/engine/function/system/string/InsertAtGivenPosition.ts +72 -0
- package/src/engine/function/system/string/PostPad.ts +83 -0
- package/src/engine/function/system/string/PrePad.ts +73 -0
- package/src/engine/function/system/string/RegionMatches.ts +119 -0
- package/src/engine/function/system/string/ReplaceAtGivenPosition.ts +101 -0
- package/src/engine/function/system/string/Reverse.ts +62 -0
- package/src/engine/function/system/string/Split.ts +53 -0
- package/src/engine/function/system/string/StringFunctionRepository.ts +60 -0
- package/src/engine/function/system/string/ToString.ts +45 -0
- package/src/engine/function/system/string/TrimTo.ts +55 -0
- package/src/engine/json/JsonExpression.ts +11 -0
- package/src/engine/json/schema/Schema.ts +670 -0
- package/src/engine/json/schema/SchemaUtil.ts +145 -0
- package/src/engine/json/schema/array/ArraySchemaType.ts +44 -0
- package/src/engine/json/schema/object/AdditionalPropertiesType.ts +32 -0
- package/src/engine/json/schema/string/StringFormat.ts +7 -0
- package/src/engine/json/schema/type/MultipleType.ts +28 -0
- package/src/engine/json/schema/type/SchemaType.ts +11 -0
- package/src/engine/json/schema/type/SingleType.ts +23 -0
- package/src/engine/json/schema/type/Type.ts +6 -0
- package/src/engine/json/schema/type/TypeUtil.ts +25 -0
- package/src/engine/json/schema/validator/AnyOfAllOfOneOfValidator.ts +108 -0
- package/src/engine/json/schema/validator/ArrayValidator.ts +145 -0
- package/src/engine/json/schema/validator/BooleanValidator.ts +24 -0
- package/src/engine/json/schema/validator/NullValidator.ts +17 -0
- package/src/engine/json/schema/validator/NumberValidator.ts +115 -0
- package/src/engine/json/schema/validator/ObjectValidator.ts +184 -0
- package/src/engine/json/schema/validator/SchemaValidator.ts +141 -0
- package/src/engine/json/schema/validator/StringValidator.ts +97 -0
- package/src/engine/json/schema/validator/TypeValidator.ts +49 -0
- package/src/engine/json/schema/validator/exception/SchemaReferenceException.ts +19 -0
- package/src/engine/json/schema/validator/exception/SchemaValidationException.ts +22 -0
- package/src/engine/model/AbstractStatement.ts +29 -0
- package/src/engine/model/Argument.ts +36 -0
- package/src/engine/model/Event.ts +62 -0
- package/src/engine/model/EventResult.ts +34 -0
- package/src/engine/model/FunctionDefinition.ts +69 -0
- package/src/engine/model/FunctionOutput.ts +37 -0
- package/src/engine/model/FunctionOutputGenerator.ts +5 -0
- package/src/engine/model/FunctionSignature.ts +67 -0
- package/src/engine/model/Parameter.ts +97 -0
- package/src/engine/model/ParameterReference.ts +57 -0
- package/src/engine/model/ParameterReferenceType.ts +4 -0
- package/src/engine/model/ParameterType.ts +4 -0
- package/src/engine/model/Position.ts +40 -0
- package/src/engine/model/Statement.ts +101 -0
- package/src/engine/model/StatementGroup.ts +37 -0
- package/src/engine/namespaces/Namespaces.ts +11 -0
- package/src/engine/repository/KIRunFunctionRepository.ts +37 -0
- package/src/engine/repository/KIRunSchemaRepository.ts +24 -0
- package/src/engine/runtime/ContextElement.ts +28 -0
- package/src/engine/runtime/FunctionExecutionParameters.ts +74 -0
- package/src/engine/runtime/KIRuntime.ts +653 -0
- package/src/engine/runtime/StatementExecution.ts +61 -0
- package/src/engine/runtime/StatementMessage.ts +30 -0
- package/src/engine/runtime/StatementMessageType.ts +5 -0
- package/src/engine/runtime/expression/Expression.ts +330 -0
- package/src/engine/runtime/expression/ExpressionEvaluator.ts +305 -0
- package/src/engine/runtime/expression/ExpressionToken.ts +15 -0
- package/src/engine/runtime/expression/ExpressionTokenValue.ts +23 -0
- package/src/engine/runtime/expression/Operation.ts +190 -0
- package/src/engine/runtime/expression/exception/ExpressionEvaluationException.ts +15 -0
- package/src/engine/runtime/expression/operators/binary/ArithmeticAdditionOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/ArithmeticDivisionOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/ArithmeticInetgerDivisionOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/ArithmeticModulusOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/ArithmeticMultiplicationOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/ArithmeticSubtractionOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/ArrayOperator.ts +31 -0
- package/src/engine/runtime/expression/operators/binary/BinaryOperator.ts +15 -0
- package/src/engine/runtime/expression/operators/binary/BitwiseAndOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/BitwiseLeftShiftOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/BitwiseOrOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/BitwiseRightShiftOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/BitwiseUnsignedRightShiftOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/BitwiseXorOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/LogicalAndOperator.ts +25 -0
- package/src/engine/runtime/expression/operators/binary/LogicalEqualOperator.ts +13 -0
- package/src/engine/runtime/expression/operators/binary/LogicalGreaterThanEqualOperator.ts +24 -0
- package/src/engine/runtime/expression/operators/binary/LogicalGreaterThanOperator.ts +24 -0
- package/src/engine/runtime/expression/operators/binary/LogicalLessThanEqualOperator.ts +24 -0
- package/src/engine/runtime/expression/operators/binary/LogicalLessThanOperator.ts +24 -0
- package/src/engine/runtime/expression/operators/binary/LogicalNotEqualOperator.ts +13 -0
- package/src/engine/runtime/expression/operators/binary/LogicalOrOperator.ts +25 -0
- package/src/engine/runtime/expression/operators/binary/ObjectOperator.ts +24 -0
- package/src/engine/runtime/expression/operators/unary/ArithmeticUnaryMinusOperator.ts +13 -0
- package/src/engine/runtime/expression/operators/unary/ArithmeticUnaryPlusOperator.ts +13 -0
- package/src/engine/runtime/expression/operators/unary/BitwiseComplementOperator.ts +22 -0
- package/src/engine/runtime/expression/operators/unary/LogicalNotOperator.ts +22 -0
- package/src/engine/runtime/expression/operators/unary/UnaryOperator.ts +15 -0
- package/src/engine/runtime/expression/tokenextractor/LiteralTokenValueExtractor.ts +64 -0
- package/src/engine/runtime/expression/tokenextractor/TokenValueExtractor.ts +136 -0
- package/src/engine/runtime/graph/ExecutionGraph.ts +81 -0
- package/src/engine/runtime/graph/GraphVertex.ts +118 -0
- package/src/engine/runtime/graph/GraphVertexType.ts +4 -0
- package/src/engine/runtime/tokenextractor/ArgumentsTokenValueExtractor.ts +22 -0
- package/src/engine/runtime/tokenextractor/ContextTokenValueExtractor.ts +37 -0
- package/src/engine/runtime/tokenextractor/OutputMapTokenValueExtractor.ts +32 -0
- package/src/engine/util/ArrayUtil.ts +23 -0
- package/src/engine/util/LinkedList.ts +229 -0
- package/src/engine/util/MapUtil.ts +86 -0
- package/src/engine/util/NullCheck.ts +3 -0
- package/src/engine/util/Tuples.ts +43 -0
- package/src/engine/util/primitive/PrimitiveUtil.ts +143 -0
- package/src/engine/util/string/StringBuilder.ts +59 -0
- package/src/engine/util/string/StringFormatter.ts +25 -0
- package/src/engine/util/string/StringUtil.ts +48 -0
- package/src/index.ts +13 -0
- package/tsconfig.json +6 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Statement } from '../model/Statement';
|
|
2
|
+
import { GraphVertexType } from './graph/GraphVertexType';
|
|
3
|
+
import { StatementMessage } from './StatementMessage';
|
|
4
|
+
import { StatementMessageType } from './StatementMessageType';
|
|
5
|
+
|
|
6
|
+
// @EqualsAndHashCode(exclude = { "messages", "dependencies" })
|
|
7
|
+
export class StatementExecution implements GraphVertexType<String> {
|
|
8
|
+
private statement: Statement;
|
|
9
|
+
private messages: StatementMessage[] = new Array();
|
|
10
|
+
private dependencies: Set<string> = new Set();
|
|
11
|
+
|
|
12
|
+
public constructor(statement: Statement) {
|
|
13
|
+
this.statement = statement;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public getStatement(): Statement {
|
|
17
|
+
return this.statement;
|
|
18
|
+
}
|
|
19
|
+
public setStatement(statement: Statement): StatementExecution {
|
|
20
|
+
this.statement = statement;
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
public getMessages(): StatementMessage[] {
|
|
24
|
+
return this.messages;
|
|
25
|
+
}
|
|
26
|
+
public setMessages(messages: StatementMessage[]): StatementExecution {
|
|
27
|
+
this.messages = messages;
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
public getDependencies(): Set<string> {
|
|
31
|
+
return this.dependencies;
|
|
32
|
+
}
|
|
33
|
+
public setDependencies(dependencies: Set<string>): StatementExecution {
|
|
34
|
+
this.dependencies = dependencies;
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public getUniqueKey(): string {
|
|
39
|
+
return this.statement.getStatementName();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public addMessage(type: StatementMessageType, message: string): void {
|
|
43
|
+
this.messages.push(new StatementMessage(type, message));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public addDependency(dependency: string): void {
|
|
47
|
+
this.dependencies.add(dependency);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public getDepenedencies(): Set<string> {
|
|
51
|
+
return this.dependencies;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public equals(obj: Object): boolean {
|
|
55
|
+
if (!(obj instanceof StatementExecution)) return false;
|
|
56
|
+
|
|
57
|
+
let se: StatementExecution = obj as StatementExecution;
|
|
58
|
+
|
|
59
|
+
return se.statement.equals(this.statement);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { StatementMessageType } from './StatementMessageType';
|
|
2
|
+
|
|
3
|
+
export class StatementMessage {
|
|
4
|
+
private messageType?: StatementMessageType;
|
|
5
|
+
private message?: string;
|
|
6
|
+
|
|
7
|
+
public constructor(messageType?: StatementMessageType, message?: string) {
|
|
8
|
+
this.message = message;
|
|
9
|
+
this.messageType = messageType;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
public getMessageType(): StatementMessageType | undefined {
|
|
13
|
+
return this.messageType;
|
|
14
|
+
}
|
|
15
|
+
public setMessageType(messageType: StatementMessageType): StatementMessage {
|
|
16
|
+
this.messageType = messageType;
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
19
|
+
public getMessage(): string | undefined {
|
|
20
|
+
return this.message;
|
|
21
|
+
}
|
|
22
|
+
public setMessage(message: string): StatementMessage {
|
|
23
|
+
this.message = message;
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public toString(): string {
|
|
28
|
+
return `${this.messageType} : ${this.message}`;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import { LinkedList } from '../../util/LinkedList';
|
|
2
|
+
import { StringBuilder } from '../../util/string/StringBuilder';
|
|
3
|
+
import { StringFormatter } from '../../util/string/StringFormatter';
|
|
4
|
+
import { StringUtil } from '../../util/string/StringUtil';
|
|
5
|
+
import { Tuple2 } from '../../util/Tuples';
|
|
6
|
+
import { ExpressionEvaluationException } from './exception/ExpressionEvaluationException';
|
|
7
|
+
import { ExpressionToken } from './ExpressionToken';
|
|
8
|
+
import { Operation } from './Operation';
|
|
9
|
+
|
|
10
|
+
export class Expression extends ExpressionToken {
|
|
11
|
+
// Data structure for storing tokens
|
|
12
|
+
private tokens: LinkedList<ExpressionToken> = new LinkedList();
|
|
13
|
+
// Data structure for storing operations
|
|
14
|
+
private ops: LinkedList<Operation> = new LinkedList();
|
|
15
|
+
|
|
16
|
+
public constructor(
|
|
17
|
+
expression?: string,
|
|
18
|
+
l?: ExpressionToken,
|
|
19
|
+
r?: ExpressionToken,
|
|
20
|
+
op?: Operation,
|
|
21
|
+
) {
|
|
22
|
+
super(expression ? expression : '');
|
|
23
|
+
if (l) this.tokens.push(l);
|
|
24
|
+
if (r) this.tokens.push(r);
|
|
25
|
+
if (op) this.ops.push(op);
|
|
26
|
+
this.evaluate();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public getTokens(): LinkedList<ExpressionToken> {
|
|
30
|
+
return this.tokens;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public getOperations(): LinkedList<Operation> {
|
|
34
|
+
return this.ops;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
private evaluate(): void {
|
|
38
|
+
const length: number = this.expression.length;
|
|
39
|
+
let chr: string = '';
|
|
40
|
+
|
|
41
|
+
let sb: StringBuilder = new StringBuilder('');
|
|
42
|
+
let buff: string | undefined = undefined;
|
|
43
|
+
let i: number = 0;
|
|
44
|
+
let isPrevOp: boolean = false;
|
|
45
|
+
|
|
46
|
+
while (i < length) {
|
|
47
|
+
chr = this.expression[i];
|
|
48
|
+
buff = sb.toString();
|
|
49
|
+
|
|
50
|
+
switch (chr) {
|
|
51
|
+
case ' ': {
|
|
52
|
+
isPrevOp = this.processTokenSepearator(sb, buff, isPrevOp);
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
case '(': {
|
|
56
|
+
i = this.processSubExpression(length, sb, buff, i, isPrevOp);
|
|
57
|
+
isPrevOp = false;
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
case ')': {
|
|
61
|
+
throw new ExpressionEvaluationException(
|
|
62
|
+
this.expression,
|
|
63
|
+
'Extra closing parenthesis found',
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
case ']': {
|
|
67
|
+
throw new ExpressionEvaluationException(
|
|
68
|
+
this.expression,
|
|
69
|
+
'Extra closing square bracket found',
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
default:
|
|
73
|
+
let result: Tuple2<number, boolean> = this.processOthers(
|
|
74
|
+
chr,
|
|
75
|
+
length,
|
|
76
|
+
sb,
|
|
77
|
+
buff,
|
|
78
|
+
i,
|
|
79
|
+
isPrevOp,
|
|
80
|
+
);
|
|
81
|
+
i = result.getT1();
|
|
82
|
+
isPrevOp = result.getT2();
|
|
83
|
+
if (isPrevOp && this.ops.peek() == Operation.ARRAY_OPERATOR) {
|
|
84
|
+
result = this.process(length, sb, i);
|
|
85
|
+
i = result.getT1();
|
|
86
|
+
isPrevOp = result.getT2();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
++i;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
buff = sb.toString();
|
|
94
|
+
if (!StringUtil.isNullOrBlank(buff)) {
|
|
95
|
+
if (Operation.OPERATORS.has(buff)) {
|
|
96
|
+
throw new ExpressionEvaluationException(
|
|
97
|
+
this.expression,
|
|
98
|
+
'Expression is ending with an operator',
|
|
99
|
+
);
|
|
100
|
+
} else {
|
|
101
|
+
this.tokens.push(new ExpressionToken(buff));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private process(length: number, sb: StringBuilder, i: number): Tuple2<number, boolean> {
|
|
107
|
+
let cnt: number = 1;
|
|
108
|
+
++i;
|
|
109
|
+
while (i < length && cnt != 0) {
|
|
110
|
+
let c: string = this.expression.charAt(i);
|
|
111
|
+
if (c == ']') --cnt;
|
|
112
|
+
else if (c == '[') ++cnt;
|
|
113
|
+
if (cnt != 0) {
|
|
114
|
+
sb.append(c);
|
|
115
|
+
i++;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
this.tokens.push(new Expression(sb.toString()));
|
|
119
|
+
sb.setLength(0);
|
|
120
|
+
|
|
121
|
+
return new Tuple2(i, false);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private processOthers(
|
|
125
|
+
chr: string,
|
|
126
|
+
length: number,
|
|
127
|
+
sb: StringBuilder,
|
|
128
|
+
buff: string,
|
|
129
|
+
i: number,
|
|
130
|
+
isPrevOp: boolean,
|
|
131
|
+
): Tuple2<number, boolean> {
|
|
132
|
+
let start: number = length - i;
|
|
133
|
+
start = start < Operation.BIGGEST_OPERATOR_SIZE ? start : Operation.BIGGEST_OPERATOR_SIZE;
|
|
134
|
+
|
|
135
|
+
for (let size = start; size > 0; size--) {
|
|
136
|
+
let op: string = this.expression.substring(i, i + size);
|
|
137
|
+
if (Operation.OPERATORS.has(op)) {
|
|
138
|
+
if (!StringUtil.isNullOrBlank(buff)) {
|
|
139
|
+
this.tokens.push(new ExpressionToken(buff));
|
|
140
|
+
isPrevOp = false;
|
|
141
|
+
}
|
|
142
|
+
this.checkUnaryOperator(
|
|
143
|
+
this.tokens,
|
|
144
|
+
this.ops,
|
|
145
|
+
Operation.OPERATION_VALUE_OF.get(op),
|
|
146
|
+
isPrevOp,
|
|
147
|
+
);
|
|
148
|
+
isPrevOp = true;
|
|
149
|
+
sb.setLength(0);
|
|
150
|
+
return new Tuple2(i + size - 1, isPrevOp);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
sb.append(chr);
|
|
155
|
+
return new Tuple2(i, false);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
private processSubExpression(
|
|
159
|
+
length: number,
|
|
160
|
+
sb: StringBuilder,
|
|
161
|
+
buff: string,
|
|
162
|
+
i: number,
|
|
163
|
+
isPrevOp: boolean,
|
|
164
|
+
): number {
|
|
165
|
+
if (Operation.OPERATORS.has(buff)) {
|
|
166
|
+
this.checkUnaryOperator(
|
|
167
|
+
this.tokens,
|
|
168
|
+
this.ops,
|
|
169
|
+
Operation.OPERATION_VALUE_OF.get(buff),
|
|
170
|
+
isPrevOp,
|
|
171
|
+
);
|
|
172
|
+
sb.setLength(0);
|
|
173
|
+
} else if (!StringUtil.isNullOrBlank(buff)) {
|
|
174
|
+
throw new ExpressionEvaluationException(
|
|
175
|
+
this.expression,
|
|
176
|
+
StringFormatter.format('Unkown token : $ found.', buff),
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
let cnt: number = 1;
|
|
181
|
+
const subExp: StringBuilder = new StringBuilder();
|
|
182
|
+
let inChr: string = this.expression.charAt(i);
|
|
183
|
+
i++;
|
|
184
|
+
while (i < length && cnt > 0) {
|
|
185
|
+
inChr = this.expression.charAt(i);
|
|
186
|
+
if (inChr == '(') cnt++;
|
|
187
|
+
else if (inChr == ')') cnt--;
|
|
188
|
+
if (cnt != 0) {
|
|
189
|
+
subExp.append(inChr);
|
|
190
|
+
i++;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (inChr != ')')
|
|
195
|
+
throw new ExpressionEvaluationException(
|
|
196
|
+
this.expression,
|
|
197
|
+
'Missing a closed parenthesis',
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
while (
|
|
201
|
+
subExp.length() > 2 &&
|
|
202
|
+
subExp.charAt(0) == '(' &&
|
|
203
|
+
subExp.charAt(subExp.length() - 1) == ')'
|
|
204
|
+
) {
|
|
205
|
+
subExp.deleteCharAt(0);
|
|
206
|
+
subExp.setLength(subExp.length() - 1);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
this.tokens.push(new Expression(subExp.toString().trim()));
|
|
210
|
+
return i;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private processTokenSepearator(sb: StringBuilder, buff: string, isPrevOp: boolean): boolean {
|
|
214
|
+
if (!StringUtil.isNullOrBlank(buff)) {
|
|
215
|
+
if (Operation.OPERATORS.has(buff)) {
|
|
216
|
+
this.checkUnaryOperator(
|
|
217
|
+
this.tokens,
|
|
218
|
+
this.ops,
|
|
219
|
+
Operation.OPERATION_VALUE_OF.get(buff),
|
|
220
|
+
isPrevOp,
|
|
221
|
+
);
|
|
222
|
+
isPrevOp = true;
|
|
223
|
+
} else {
|
|
224
|
+
this.tokens.push(new ExpressionToken(buff));
|
|
225
|
+
isPrevOp = false;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
sb.setLength(0);
|
|
229
|
+
|
|
230
|
+
return isPrevOp;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
private checkUnaryOperator(
|
|
234
|
+
tokens: LinkedList<ExpressionToken>,
|
|
235
|
+
ops: LinkedList<Operation>,
|
|
236
|
+
op: Operation | undefined,
|
|
237
|
+
isPrevOp: boolean,
|
|
238
|
+
): void {
|
|
239
|
+
if(!op) return;
|
|
240
|
+
if (isPrevOp || tokens.isEmpty()) {
|
|
241
|
+
if (Operation.UNARY_OPERATORS.has(op)){
|
|
242
|
+
const x = Operation.UNARY_MAP.get(op);
|
|
243
|
+
if(x)
|
|
244
|
+
ops.push(x);
|
|
245
|
+
}
|
|
246
|
+
else
|
|
247
|
+
throw new ExpressionEvaluationException(
|
|
248
|
+
this.expression,
|
|
249
|
+
StringFormatter.format('Extra operator $ found.', op),
|
|
250
|
+
);
|
|
251
|
+
} else {
|
|
252
|
+
while (!ops.isEmpty() && this.hasPrecedence(op, ops.peek())) {
|
|
253
|
+
let prev: Operation = ops.pop();
|
|
254
|
+
|
|
255
|
+
if (Operation.UNARY_OPERATORS.has(prev)) {
|
|
256
|
+
let l: ExpressionToken = tokens.pop();
|
|
257
|
+
tokens.push(new Expression('', l, undefined, prev));
|
|
258
|
+
} else {
|
|
259
|
+
let r: ExpressionToken = tokens.pop();
|
|
260
|
+
let l: ExpressionToken = tokens.pop();
|
|
261
|
+
|
|
262
|
+
tokens.push(new Expression('', l, r, prev));
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
ops.push(op);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
private hasPrecedence(op1: Operation, op2: Operation): boolean {
|
|
270
|
+
let pre1: number | undefined = Operation.OPERATOR_PRIORITY.get(op1);
|
|
271
|
+
let pre2: number | undefined = Operation.OPERATOR_PRIORITY.get(op2);
|
|
272
|
+
if(!pre1 || !pre2) {
|
|
273
|
+
throw new Error('Unknown operators provided');
|
|
274
|
+
}
|
|
275
|
+
return pre2 < pre1;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
public toString(): string {
|
|
279
|
+
if (this.ops.isEmpty()) {
|
|
280
|
+
if (this.tokens.size() == 1) return this.tokens.get(0).toString();
|
|
281
|
+
return 'Error: No tokens';
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
let sb: StringBuilder = new StringBuilder();
|
|
285
|
+
let ind: number = 0;
|
|
286
|
+
|
|
287
|
+
const ops: Operation[] = this.ops.toArray();
|
|
288
|
+
const tokens: ExpressionToken[] = this.tokens.toArray();
|
|
289
|
+
|
|
290
|
+
for (let i = 0; i < ops.length; i++) {
|
|
291
|
+
if (ops[i].getOperator().startsWith('UN: ')) {
|
|
292
|
+
sb.append('(')
|
|
293
|
+
.append(ops[i].getOperator().substring(4))
|
|
294
|
+
.append(
|
|
295
|
+
tokens[ind] instanceof Expression
|
|
296
|
+
? (tokens[ind] as Expression).toString()
|
|
297
|
+
: tokens[ind],
|
|
298
|
+
)
|
|
299
|
+
.append(')');
|
|
300
|
+
ind++;
|
|
301
|
+
} else {
|
|
302
|
+
if (ind == 0) {
|
|
303
|
+
const temp: ExpressionToken = tokens[ind++];
|
|
304
|
+
sb.insert(
|
|
305
|
+
0,
|
|
306
|
+
temp instanceof Expression
|
|
307
|
+
? (temp as Expression).toString()
|
|
308
|
+
: temp.toString(),
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
const temp: ExpressionToken = tokens[ind++];
|
|
312
|
+
sb.insert(0, ops[i].getOperator())
|
|
313
|
+
.insert(
|
|
314
|
+
0,
|
|
315
|
+
temp instanceof Expression
|
|
316
|
+
? (temp as Expression).toString()
|
|
317
|
+
: temp.toString(),
|
|
318
|
+
)
|
|
319
|
+
.insert(0, '(')
|
|
320
|
+
.append(')');
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return sb.toString();
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
public equals(o: Expression): boolean {
|
|
328
|
+
return this.expression == o.expression;
|
|
329
|
+
}
|
|
330
|
+
}
|