@fincity/kirun-js 1.3.1 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/engine/json/schema/validator/SchemaValidatorTest.ts +51 -1
- package/__tests__/engine/runtime/KIRuntimeWithDefinitionTest.ts +37 -0
- package/__tests__/engine/runtime/expression/ExpressionEvaluatorTernaryOperatorTest.ts +42 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/module.js +1 -1
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts +34 -19
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/engine/function/AbstractFunction.ts +12 -5
- package/src/engine/function/system/GenerateEvent.ts +1 -2
- package/src/engine/function/system/string/Reverse.ts +1 -1
- package/src/engine/json/schema/Schema.ts +119 -2
- package/src/engine/json/schema/SchemaUtil.ts +1 -1
- package/src/engine/json/schema/array/ArraySchemaType.ts +6 -0
- package/src/engine/json/schema/type/MultipleType.ts +5 -2
- package/src/engine/json/schema/type/SingleType.ts +4 -2
- package/src/engine/json/schema/validator/ObjectValidator.ts +2 -3
- package/src/engine/model/AbstractStatement.ts +10 -0
- package/src/engine/model/Event.ts +12 -5
- package/src/engine/model/FunctionDefinition.ts +1 -2
- package/src/engine/model/FunctionSignature.ts +14 -4
- package/src/engine/model/Parameter.ts +13 -3
- package/src/engine/model/ParameterReference.ts +12 -3
- package/src/engine/model/Position.ts +1 -1
- package/src/engine/model/Statement.ts +33 -7
- package/src/engine/model/StatementGroup.ts +22 -2
- package/src/engine/runtime/KIRuntime.ts +3 -2
- package/src/engine/runtime/expression/Expression.ts +120 -0
- package/src/engine/runtime/expression/ExpressionEvaluator.ts +26 -0
- package/src/engine/runtime/expression/Operation.ts +10 -0
- package/src/engine/runtime/expression/operators/ternary/ConditionalTernaryOperator.ts +7 -0
- package/src/engine/runtime/expression/operators/ternary/TernaryOperator.ts +15 -0
- package/src/engine/runtime/expression/operators/ternary/index.ts +1 -0
- package/src/index.ts +1 -1
- package/src/engine/json/schema/object/AdditionalPropertiesType.ts +0 -32
|
@@ -4,9 +4,12 @@ import { Type } from './Type';
|
|
|
4
4
|
export class MultipleType extends Type {
|
|
5
5
|
private type: Set<SchemaType>;
|
|
6
6
|
|
|
7
|
-
constructor(type: Set<SchemaType>) {
|
|
7
|
+
public constructor(type: Set<SchemaType> | MultipleType) {
|
|
8
8
|
super();
|
|
9
|
-
|
|
9
|
+
|
|
10
|
+
if (type instanceof MultipleType)
|
|
11
|
+
this.type = new Set(Array.from((type as MultipleType).type));
|
|
12
|
+
else this.type = new Set(Array.from(type));
|
|
10
13
|
}
|
|
11
14
|
|
|
12
15
|
public getType(): Set<SchemaType> {
|
|
@@ -4,9 +4,11 @@ import { Type } from './Type';
|
|
|
4
4
|
export class SingleType extends Type {
|
|
5
5
|
private type: SchemaType;
|
|
6
6
|
|
|
7
|
-
constructor(type: SchemaType) {
|
|
7
|
+
public constructor(type: SchemaType | SingleType) {
|
|
8
8
|
super();
|
|
9
|
-
|
|
9
|
+
|
|
10
|
+
if (type instanceof SingleType) this.type = (type as SingleType).type;
|
|
11
|
+
else this.type = type as SchemaType;
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
public getType(): SchemaType {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Repository } from '../../../Repository';
|
|
2
2
|
import { isNullValue } from '../../../util/NullCheck';
|
|
3
|
-
import { AdditionalPropertiesType } from '../
|
|
4
|
-
import { Schema } from '../Schema';
|
|
3
|
+
import { AdditionalPropertiesType, Schema } from '../Schema';
|
|
5
4
|
import { SchemaValidationException } from './exception/SchemaValidationException';
|
|
6
5
|
import { SchemaValidator } from './SchemaValidator';
|
|
7
6
|
|
|
@@ -100,7 +99,7 @@ export class ObjectValidator {
|
|
|
100
99
|
newParents,
|
|
101
100
|
apt.getSchemaValue(),
|
|
102
101
|
repository,
|
|
103
|
-
jsonObject
|
|
102
|
+
jsonObject[key],
|
|
104
103
|
);
|
|
105
104
|
jsonObject[key] = element;
|
|
106
105
|
}
|
|
@@ -6,6 +6,16 @@ export class AbstractStatement {
|
|
|
6
6
|
private position?: Position;
|
|
7
7
|
private override: boolean = false;
|
|
8
8
|
|
|
9
|
+
public constructor(ast?: AbstractStatement) {
|
|
10
|
+
if (!ast) return;
|
|
11
|
+
this.comment = ast.comment;
|
|
12
|
+
this.description = ast.description;
|
|
13
|
+
this.position = ast.position
|
|
14
|
+
? new Position(ast.position.getLeft(), ast.position.getTop())
|
|
15
|
+
: undefined;
|
|
16
|
+
this.override = ast.override;
|
|
17
|
+
}
|
|
18
|
+
|
|
9
19
|
public getComment(): string | undefined {
|
|
10
20
|
return this.comment;
|
|
11
21
|
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { AdditionalPropertiesType } from '../json/schema/
|
|
2
|
-
import { Schema } from '../json/schema/Schema';
|
|
1
|
+
import { AdditionalPropertiesType, Schema } from '../json/schema/Schema';
|
|
3
2
|
import { SchemaType } from '../json/schema/type/SchemaType';
|
|
4
3
|
import { TypeUtil } from '../json/schema/type/TypeUtil';
|
|
5
4
|
import { SchemaReferenceException } from '../json/schema/validator/exception/SchemaReferenceException';
|
|
@@ -30,9 +29,17 @@ export class Event {
|
|
|
30
29
|
private name: string;
|
|
31
30
|
private parameters: Map<string, Schema>;
|
|
32
31
|
|
|
33
|
-
constructor(
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
constructor(evn: string | Event, parameters?: Map<string, Schema>) {
|
|
33
|
+
if (evn instanceof Event) {
|
|
34
|
+
this.name = evn.name;
|
|
35
|
+
this.parameters = new Map(
|
|
36
|
+
Array.from(evn.parameters.entries()).map((e) => [e[0], new Schema(e[1])]),
|
|
37
|
+
);
|
|
38
|
+
} else {
|
|
39
|
+
this.name = evn;
|
|
40
|
+
if (!parameters) throw new Error('Unknown constructor format');
|
|
41
|
+
this.parameters = parameters;
|
|
42
|
+
}
|
|
36
43
|
}
|
|
37
44
|
|
|
38
45
|
public getName(): string {
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { AdditionalPropertiesType } from '../json/schema/
|
|
2
|
-
import { Schema } from '../json/schema/Schema';
|
|
1
|
+
import { AdditionalPropertiesType, Schema } from '../json/schema/Schema';
|
|
3
2
|
import { Namespaces } from '../namespaces/Namespaces';
|
|
4
3
|
import { Event } from './Event';
|
|
5
4
|
import { FunctionSignature } from './FunctionSignature';
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { AdditionalPropertiesType } from '../json/schema/
|
|
2
|
-
import { Schema } from '../json/schema/Schema';
|
|
1
|
+
import { AdditionalPropertiesType, Schema } from '../json/schema/Schema';
|
|
3
2
|
import { Namespaces } from '../namespaces/Namespaces';
|
|
4
3
|
import { Event } from './Event';
|
|
5
4
|
import { Parameter } from './Parameter';
|
|
@@ -32,8 +31,19 @@ export class FunctionSignature {
|
|
|
32
31
|
private parameters: Map<string, Parameter> = new Map();
|
|
33
32
|
private events: Map<string, Event> = new Map();
|
|
34
33
|
|
|
35
|
-
constructor(
|
|
36
|
-
|
|
34
|
+
constructor(value: string | FunctionSignature) {
|
|
35
|
+
if (value instanceof FunctionSignature) {
|
|
36
|
+
this.name = value.name;
|
|
37
|
+
this.namespace = value.namespace;
|
|
38
|
+
this.parameters = new Map(
|
|
39
|
+
Array.from(value.parameters.entries()).map((e) => [e[0], new Parameter(e[1])]),
|
|
40
|
+
);
|
|
41
|
+
this.events = new Map(
|
|
42
|
+
Array.from(value.events.entries()).map((e) => [e[0], new Event(e[1])]),
|
|
43
|
+
);
|
|
44
|
+
} else {
|
|
45
|
+
this.name = value;
|
|
46
|
+
}
|
|
37
47
|
}
|
|
38
48
|
|
|
39
49
|
public getNamespace(): string {
|
|
@@ -43,9 +43,19 @@ export class Parameter {
|
|
|
43
43
|
private variableArgument: boolean = false;
|
|
44
44
|
private type: ParameterType = ParameterType.EXPRESSION;
|
|
45
45
|
|
|
46
|
-
constructor(
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
constructor(pn: string | Parameter, schema?: Schema) {
|
|
47
|
+
if (pn instanceof Parameter) {
|
|
48
|
+
this.schema = new Schema(pn.schema);
|
|
49
|
+
this.parameterName = pn.parameterName;
|
|
50
|
+
this.variableArgument = pn.variableArgument;
|
|
51
|
+
this.type = pn.type;
|
|
52
|
+
} else {
|
|
53
|
+
if (!schema) {
|
|
54
|
+
throw new Error('Unknown constructor signature');
|
|
55
|
+
}
|
|
56
|
+
this.schema = schema;
|
|
57
|
+
this.parameterName = pn;
|
|
58
|
+
}
|
|
49
59
|
}
|
|
50
60
|
|
|
51
61
|
public getSchema(): Schema {
|
|
@@ -2,6 +2,7 @@ import { Schema } from '../json/schema/Schema';
|
|
|
2
2
|
import { SchemaType } from '../json/schema/type/SchemaType';
|
|
3
3
|
import { TypeUtil } from '../json/schema/type/TypeUtil';
|
|
4
4
|
import { Namespaces } from '../namespaces/Namespaces';
|
|
5
|
+
import { isNullValue } from '../util/NullCheck';
|
|
5
6
|
import UUID from '../util/UUID';
|
|
6
7
|
import { ParameterReferenceType } from './ParameterReferenceType';
|
|
7
8
|
|
|
@@ -25,9 +26,17 @@ export class ParameterReference {
|
|
|
25
26
|
private value: any;
|
|
26
27
|
private expression?: string;
|
|
27
28
|
|
|
28
|
-
constructor(type: ParameterReferenceType) {
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
constructor(type: ParameterReferenceType | ParameterReference) {
|
|
30
|
+
if (type instanceof ParameterReference) {
|
|
31
|
+
let pv = type as ParameterReference;
|
|
32
|
+
this.key = pv.key;
|
|
33
|
+
this.type = pv.type;
|
|
34
|
+
this.value = isNullValue(pv.value) ? undefined : JSON.parse(JSON.stringify(pv.value));
|
|
35
|
+
this.expression = pv.expression;
|
|
36
|
+
} else {
|
|
37
|
+
this.type = type as ParameterReferenceType;
|
|
38
|
+
this.key = UUID();
|
|
39
|
+
}
|
|
31
40
|
}
|
|
32
41
|
|
|
33
42
|
public getType(): ParameterReferenceType {
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { AdditionalPropertiesType } from '../json/schema/
|
|
2
|
-
import { Schema } from '../json/schema/Schema';
|
|
1
|
+
import { AdditionalPropertiesType, Schema } from '../json/schema/Schema';
|
|
3
2
|
import { SchemaType } from '../json/schema/type/SchemaType';
|
|
4
3
|
import { TypeUtil } from '../json/schema/type/TypeUtil';
|
|
5
4
|
import { Namespaces } from '../namespaces/Namespaces';
|
|
@@ -49,11 +48,38 @@ export class Statement extends AbstractStatement {
|
|
|
49
48
|
private parameterMap?: Map<string, Map<string, ParameterReference>>;
|
|
50
49
|
private dependentStatements?: Map<string, boolean>;
|
|
51
50
|
|
|
52
|
-
public constructor(
|
|
53
|
-
super();
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
51
|
+
public constructor(sn: string | Statement, namespace?: string, name?: string) {
|
|
52
|
+
super(sn instanceof Statement ? (sn as Statement) : undefined);
|
|
53
|
+
|
|
54
|
+
if (sn instanceof Statement) {
|
|
55
|
+
let x = sn as Statement;
|
|
56
|
+
this.statementName = x.statementName;
|
|
57
|
+
this.name = x.name;
|
|
58
|
+
this.namespace = x.namespace;
|
|
59
|
+
if (x.parameterMap)
|
|
60
|
+
this.parameterMap = new Map(
|
|
61
|
+
Array.from(x.parameterMap.entries()).map((f) => [
|
|
62
|
+
f[0],
|
|
63
|
+
new Map(
|
|
64
|
+
Array.from(f[1].entries()).map((e) => [
|
|
65
|
+
e[0],
|
|
66
|
+
new ParameterReference(e[1]),
|
|
67
|
+
]),
|
|
68
|
+
),
|
|
69
|
+
]),
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
if (x.dependentStatements) {
|
|
73
|
+
this.dependentStatements = new Map(Array.from(x.dependentStatements.entries()));
|
|
74
|
+
}
|
|
75
|
+
} else {
|
|
76
|
+
this.statementName = sn;
|
|
77
|
+
if (!name || !namespace) {
|
|
78
|
+
throw new Error('Unknown constructor');
|
|
79
|
+
}
|
|
80
|
+
this.namespace = namespace;
|
|
81
|
+
this.name = name;
|
|
82
|
+
}
|
|
57
83
|
}
|
|
58
84
|
|
|
59
85
|
public getStatementName(): string {
|
|
@@ -4,6 +4,7 @@ import { TypeUtil } from '../json/schema/type/TypeUtil';
|
|
|
4
4
|
import { Namespaces } from '../namespaces/Namespaces';
|
|
5
5
|
import { AbstractStatement } from './AbstractStatement';
|
|
6
6
|
import { Position } from './Position';
|
|
7
|
+
import { Statement } from './Statement';
|
|
7
8
|
|
|
8
9
|
export class StatementGroup extends AbstractStatement {
|
|
9
10
|
private static readonly SCHEMA_NAME: string = 'StatementGroup';
|
|
@@ -21,10 +22,12 @@ export class StatementGroup extends AbstractStatement {
|
|
|
21
22
|
);
|
|
22
23
|
|
|
23
24
|
private statementGroupName: string;
|
|
25
|
+
private statements: Map<string, boolean>;
|
|
24
26
|
|
|
25
|
-
constructor(statementGroupName: string) {
|
|
27
|
+
constructor(statementGroupName: string, statements: Map<string, boolean> = new Map()) {
|
|
26
28
|
super();
|
|
27
29
|
this.statementGroupName = statementGroupName;
|
|
30
|
+
this.statements = statements;
|
|
28
31
|
}
|
|
29
32
|
|
|
30
33
|
public getStatementGroupName(): string {
|
|
@@ -35,8 +38,25 @@ export class StatementGroup extends AbstractStatement {
|
|
|
35
38
|
return this;
|
|
36
39
|
}
|
|
37
40
|
|
|
41
|
+
public getStatements(): Map<string, boolean> {
|
|
42
|
+
return this.statements;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public setStatements(statements: Map<string, boolean>): StatementGroup {
|
|
46
|
+
this.statements = statements;
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
|
|
38
50
|
public static from(json: any): StatementGroup {
|
|
39
|
-
return new StatementGroup(
|
|
51
|
+
return new StatementGroup(
|
|
52
|
+
json.statementGroupName,
|
|
53
|
+
new Map(
|
|
54
|
+
Object.entries(json.statements || {}).map(([k, v]) => [
|
|
55
|
+
k,
|
|
56
|
+
('' + v)?.toLowerCase() == 'true',
|
|
57
|
+
]),
|
|
58
|
+
),
|
|
59
|
+
)
|
|
40
60
|
.setPosition(Position.from(json.position))
|
|
41
61
|
.setComment(json.comment)
|
|
42
62
|
.setDescription(json.description) as StatementGroup;
|
|
@@ -485,7 +485,7 @@ export class KIRuntime extends AbstractFunction {
|
|
|
485
485
|
|
|
486
486
|
let refList: ParameterReference[] = Array.from(param[1]?.values() ?? []);
|
|
487
487
|
|
|
488
|
-
if (!refList.length) {
|
|
488
|
+
if (!refList.length && !p.isVariableArgument()) {
|
|
489
489
|
if (isNullValue(SchemaUtil.getDefaultValue(p.getSchema(), sRepo)))
|
|
490
490
|
se.addMessage(
|
|
491
491
|
StatementMessageType.ERROR,
|
|
@@ -500,7 +500,7 @@ export class KIRuntime extends AbstractFunction {
|
|
|
500
500
|
|
|
501
501
|
if (p.isVariableArgument()) {
|
|
502
502
|
for (let ref of refList) this.parameterReferenceValidation(se, p, ref, sRepo);
|
|
503
|
-
} else {
|
|
503
|
+
} else if (refList.length) {
|
|
504
504
|
let ref: ParameterReference = refList[0];
|
|
505
505
|
this.parameterReferenceValidation(se, p, ref, sRepo);
|
|
506
506
|
}
|
|
@@ -515,6 +515,7 @@ export class KIRuntime extends AbstractFunction {
|
|
|
515
515
|
|
|
516
516
|
if (paramSet.size) {
|
|
517
517
|
for (let param of Array.from(paramSet.values())) {
|
|
518
|
+
if (param.isVariableArgument()) continue;
|
|
518
519
|
if (isNullValue(SchemaUtil.getDefaultValue(param.getSchema(), sRepo)))
|
|
519
520
|
se.addMessage(
|
|
520
521
|
StatementMessageType.ERROR,
|
|
@@ -77,6 +77,33 @@ export class Expression extends ExpressionToken {
|
|
|
77
77
|
isPrevOp = result.getT2();
|
|
78
78
|
break;
|
|
79
79
|
}
|
|
80
|
+
case '?': {
|
|
81
|
+
if (
|
|
82
|
+
i + 1 < length &&
|
|
83
|
+
this.expression.charAt(i + 1) != '?' &&
|
|
84
|
+
i != 0 &&
|
|
85
|
+
this.expression.charAt(i - 1) != '?'
|
|
86
|
+
) {
|
|
87
|
+
i = this.processTernaryOperator(length, sb, buff, i, isPrevOp);
|
|
88
|
+
} else {
|
|
89
|
+
let result: Tuple2<number, boolean> = this.processOthers(
|
|
90
|
+
chr,
|
|
91
|
+
length,
|
|
92
|
+
sb,
|
|
93
|
+
buff,
|
|
94
|
+
i,
|
|
95
|
+
isPrevOp,
|
|
96
|
+
);
|
|
97
|
+
i = result.getT1();
|
|
98
|
+
isPrevOp = result.getT2();
|
|
99
|
+
if (isPrevOp && this.ops.peek() == Operation.ARRAY_OPERATOR) {
|
|
100
|
+
result = this.process(length, sb, i);
|
|
101
|
+
i = result.getT1();
|
|
102
|
+
isPrevOp = result.getT2();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
80
107
|
default:
|
|
81
108
|
let result: Tuple2<number, boolean> = this.processOthers(
|
|
82
109
|
chr,
|
|
@@ -188,6 +215,80 @@ export class Expression extends ExpressionToken {
|
|
|
188
215
|
return new Tuple2(i, false);
|
|
189
216
|
}
|
|
190
217
|
|
|
218
|
+
private processTernaryOperator(
|
|
219
|
+
length: number,
|
|
220
|
+
sb: StringBuilder,
|
|
221
|
+
buff: string,
|
|
222
|
+
i: number,
|
|
223
|
+
isPrevOp: boolean,
|
|
224
|
+
): number {
|
|
225
|
+
if (isPrevOp) {
|
|
226
|
+
throw new ExpressionEvaluationException(
|
|
227
|
+
this.expression,
|
|
228
|
+
'Ternary operator is followed by an operator',
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (buff.trim() != '') {
|
|
233
|
+
this.tokens.push(new Expression(buff));
|
|
234
|
+
sb.setLength(0);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
++i;
|
|
238
|
+
let cnt: number = 1;
|
|
239
|
+
let inChr = '';
|
|
240
|
+
const start = i;
|
|
241
|
+
while (i < length && cnt > 0) {
|
|
242
|
+
inChr = this.expression.charAt(i);
|
|
243
|
+
if (inChr == '?') ++cnt;
|
|
244
|
+
else if (inChr == ':') --cnt;
|
|
245
|
+
++i;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (inChr != ':') {
|
|
249
|
+
throw new ExpressionEvaluationException(this.expression, "':' operater is missing");
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (i >= length) {
|
|
253
|
+
throw new ExpressionEvaluationException(
|
|
254
|
+
this.expression,
|
|
255
|
+
'Third part of the ternary expression is missing',
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
while (
|
|
260
|
+
!this.ops.isEmpty() &&
|
|
261
|
+
this.hasPrecedence(Operation.CONDITIONAL_TERNARY_OPERATOR, this.ops.peek())
|
|
262
|
+
) {
|
|
263
|
+
let prev: Operation = this.ops.pop();
|
|
264
|
+
|
|
265
|
+
if (Operation.UNARY_OPERATORS.has(prev)) {
|
|
266
|
+
const l: ExpressionToken = this.tokens.pop();
|
|
267
|
+
this.tokens.push(new Expression('', l, undefined, prev));
|
|
268
|
+
} else {
|
|
269
|
+
let r = this.tokens.pop();
|
|
270
|
+
let l = this.tokens.pop();
|
|
271
|
+
|
|
272
|
+
this.tokens.push(new Expression('', l, r, prev));
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
this.ops.push(Operation.CONDITIONAL_TERNARY_OPERATOR);
|
|
277
|
+
this.tokens.push(new Expression(this.expression.substring(start, i - 1)));
|
|
278
|
+
|
|
279
|
+
const secondExp: string = this.expression.substring(i);
|
|
280
|
+
if (secondExp.trim() === '') {
|
|
281
|
+
throw new ExpressionEvaluationException(
|
|
282
|
+
this.expression,
|
|
283
|
+
'Third part of the ternary expression is missing',
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
this.tokens.push(new Expression(secondExp));
|
|
288
|
+
|
|
289
|
+
return length - 1;
|
|
290
|
+
}
|
|
291
|
+
|
|
191
292
|
private processSubExpression(
|
|
192
293
|
length: number,
|
|
193
294
|
sb: StringBuilder,
|
|
@@ -329,6 +430,25 @@ export class Expression extends ExpressionToken {
|
|
|
329
430
|
)
|
|
330
431
|
.append(')');
|
|
331
432
|
ind++;
|
|
433
|
+
} else if (this.ops.get(i) == Operation.CONDITIONAL_TERNARY_OPERATOR) {
|
|
434
|
+
let temp: ExpressionToken = tokens[ind++];
|
|
435
|
+
sb.insert(
|
|
436
|
+
0,
|
|
437
|
+
temp instanceof Expression ? (temp as Expression).toString() : temp.toString(),
|
|
438
|
+
);
|
|
439
|
+
sb.insert(0, ':');
|
|
440
|
+
temp = tokens[ind++];
|
|
441
|
+
sb.insert(
|
|
442
|
+
0,
|
|
443
|
+
temp instanceof Expression ? (temp as Expression).toString() : temp.toString(),
|
|
444
|
+
);
|
|
445
|
+
sb.insert(0, '?');
|
|
446
|
+
temp = tokens[ind++];
|
|
447
|
+
sb.insert(
|
|
448
|
+
0,
|
|
449
|
+
temp instanceof Expression ? (temp as Expression).toString() : temp.toString(),
|
|
450
|
+
).append(')');
|
|
451
|
+
sb.insert(0, '(');
|
|
332
452
|
} else {
|
|
333
453
|
if (ind == 0) {
|
|
334
454
|
const temp: ExpressionToken = tokens[ind++];
|
|
@@ -40,6 +40,8 @@ import { UnaryOperator } from './operators/unary/UnaryOperator';
|
|
|
40
40
|
import { LiteralTokenValueExtractor } from './tokenextractor/LiteralTokenValueExtractor';
|
|
41
41
|
import { TokenValueExtractor } from './tokenextractor/TokenValueExtractor';
|
|
42
42
|
import { Tuple2 } from '../../util/Tuples';
|
|
43
|
+
import { ConditionalTernaryOperator } from './operators/ternary';
|
|
44
|
+
import { TernaryOperator } from './operators/ternary/TernaryOperator';
|
|
43
45
|
|
|
44
46
|
export class ExpressionEvaluator {
|
|
45
47
|
private static readonly UNARY_OPERATORS_MAP: Map<Operation, UnaryOperator> = new Map([
|
|
@@ -49,6 +51,10 @@ export class ExpressionEvaluator {
|
|
|
49
51
|
[Operation.UNARY_PLUS, new ArithmeticUnaryPlusOperator()],
|
|
50
52
|
]);
|
|
51
53
|
|
|
54
|
+
private static readonly TERNARY_OPERATORS_MAP: Map<Operation, TernaryOperator> = new Map([
|
|
55
|
+
[Operation.CONDITIONAL_TERNARY_OPERATOR, new ConditionalTernaryOperator()],
|
|
56
|
+
]);
|
|
57
|
+
|
|
52
58
|
private static readonly BINARY_OPERATORS_MAP: Map<Operation, BinaryOperator> = new Map([
|
|
53
59
|
[Operation.ADDITION, new ArithmeticAdditionOperator()],
|
|
54
60
|
[Operation.DIVISION, new ArithmeticDivisionOperator()],
|
|
@@ -195,6 +201,13 @@ export class ExpressionEvaluator {
|
|
|
195
201
|
operator == Operation.ARRAY_OPERATOR
|
|
196
202
|
) {
|
|
197
203
|
this.processObjectOrArrayOperator(valuesMap, ops, tokens, operator, token);
|
|
204
|
+
} else if (operator == Operation.CONDITIONAL_TERNARY_OPERATOR) {
|
|
205
|
+
const token2: ExpressionToken = tokens.pop();
|
|
206
|
+
const token3: ExpressionToken = tokens.pop();
|
|
207
|
+
var v1 = this.getValueFromToken(valuesMap, token3);
|
|
208
|
+
var v2 = this.getValueFromToken(valuesMap, token2);
|
|
209
|
+
var v3 = this.getValueFromToken(valuesMap, token);
|
|
210
|
+
tokens.push(this.applyTernaryOperation(operator, v1, v2, v3));
|
|
198
211
|
} else {
|
|
199
212
|
const token2: ExpressionToken = tokens.pop();
|
|
200
213
|
var v1 = this.getValueFromToken(valuesMap, token2);
|
|
@@ -295,6 +308,19 @@ export class ExpressionEvaluator {
|
|
|
295
308
|
}
|
|
296
309
|
}
|
|
297
310
|
|
|
311
|
+
private applyTernaryOperation(operator: Operation, v1: any, v2: any, v3: any): ExpressionToken {
|
|
312
|
+
let op: TernaryOperator | undefined =
|
|
313
|
+
ExpressionEvaluator.TERNARY_OPERATORS_MAP.get(operator);
|
|
314
|
+
|
|
315
|
+
if (!op)
|
|
316
|
+
throw new ExpressionEvaluationException(
|
|
317
|
+
this.expression,
|
|
318
|
+
StringFormatter.format('No operator found to evaluate $', this.getExpression()),
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
return new ExpressionTokenValue(operator.toString(), op.apply(v1, v2, v3));
|
|
322
|
+
}
|
|
323
|
+
|
|
298
324
|
private applyBinaryOperation(operator: Operation, v1: any, v2: any): ExpressionToken {
|
|
299
325
|
let typv1: string = typeof v1;
|
|
300
326
|
let typv2: string = typeof v2;
|
|
@@ -34,6 +34,8 @@ export class Operation {
|
|
|
34
34
|
|
|
35
35
|
public static readonly NULLISH_COALESCING_OPERATOR: Operation = new Operation('??');
|
|
36
36
|
|
|
37
|
+
public static readonly CONDITIONAL_TERNARY_OPERATOR: Operation = new Operation('?');
|
|
38
|
+
|
|
37
39
|
private static readonly VALUE_OF: Map<string, Operation> = new Map([
|
|
38
40
|
['MULTIPLICATION', Operation.MULTIPLICATION],
|
|
39
41
|
['DIVISION', Operation.DIVISION],
|
|
@@ -64,6 +66,7 @@ export class Operation {
|
|
|
64
66
|
['ARRAY_OPERATOR', Operation.ARRAY_OPERATOR],
|
|
65
67
|
['OBJECT_OPERATOR', Operation.OBJECT_OPERATOR],
|
|
66
68
|
['NULLISH_COALESCING_OPERATOR', Operation.NULLISH_COALESCING_OPERATOR],
|
|
69
|
+
['CONDITIONAL_TERNARY_OPERATOR', Operation.CONDITIONAL_TERNARY_OPERATOR],
|
|
67
70
|
]);
|
|
68
71
|
|
|
69
72
|
public static readonly UNARY_OPERATORS: Set<Operation> = new Set([
|
|
@@ -109,6 +112,10 @@ export class Operation {
|
|
|
109
112
|
Operation.BITWISE_XOR,
|
|
110
113
|
]);
|
|
111
114
|
|
|
115
|
+
public static readonly CONDITIONAL_OPERATORS: Set<Operation> = new Set([
|
|
116
|
+
Operation.CONDITIONAL_TERNARY_OPERATOR,
|
|
117
|
+
]);
|
|
118
|
+
|
|
112
119
|
public static readonly OPERATOR_PRIORITY: Map<Operation, number> = new Map([
|
|
113
120
|
[Operation.UNARY_PLUS, 1],
|
|
114
121
|
[Operation.UNARY_MINUS, 1],
|
|
@@ -137,6 +144,7 @@ export class Operation {
|
|
|
137
144
|
[Operation.AND, 10],
|
|
138
145
|
[Operation.OR, 11],
|
|
139
146
|
[Operation.NULLISH_COALESCING_OPERATOR, 11],
|
|
147
|
+
[Operation.CONDITIONAL_TERNARY_OPERATOR, 12],
|
|
140
148
|
]);
|
|
141
149
|
|
|
142
150
|
public static readonly OPERATORS: Set<string> = new Set(
|
|
@@ -146,6 +154,7 @@ export class Operation {
|
|
|
146
154
|
...Array.from(Operation.BITWISE_OPERATORS),
|
|
147
155
|
Operation.ARRAY_OPERATOR,
|
|
148
156
|
Operation.OBJECT_OPERATOR,
|
|
157
|
+
...Array.from(Operation.CONDITIONAL_OPERATORS),
|
|
149
158
|
].map((e) => e.getOperator()),
|
|
150
159
|
);
|
|
151
160
|
|
|
@@ -156,6 +165,7 @@ export class Operation {
|
|
|
156
165
|
...Array.from(Operation.BITWISE_OPERATORS),
|
|
157
166
|
Operation.ARRAY_OPERATOR,
|
|
158
167
|
Operation.OBJECT_OPERATOR,
|
|
168
|
+
...Array.from(Operation.CONDITIONAL_OPERATORS),
|
|
159
169
|
]
|
|
160
170
|
.filter((e) => !e.shouldBeWrappedInSpace())
|
|
161
171
|
.map((e) => e.getOperator()),
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ExecutionException } from '../../../../exception/ExecutionException';
|
|
2
|
+
import { isNullValue } from '../../../../util/NullCheck';
|
|
3
|
+
import { StringFormatter } from '../../../../util/string/StringFormatter';
|
|
4
|
+
import { Operation } from '../../Operation';
|
|
5
|
+
|
|
6
|
+
export abstract class TernaryOperator {
|
|
7
|
+
public abstract apply(t: any, u: any, v: any): any;
|
|
8
|
+
|
|
9
|
+
public nullCheck(e1: any, e2: any, e3: any, op: Operation): void {
|
|
10
|
+
if (isNullValue(e1) || isNullValue(e2) || isNullValue(e3))
|
|
11
|
+
throw new ExecutionException(
|
|
12
|
+
StringFormatter.format('$ cannot be applied to a null value', op.getOperatorName()),
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './ConditionalTernaryOperator';
|
package/src/index.ts
CHANGED
|
@@ -53,7 +53,6 @@ export * from './engine/json/schema/type/TypeUtil';
|
|
|
53
53
|
export * from './engine/json/schema/type/MultipleType';
|
|
54
54
|
export * from './engine/json/schema/type/Type';
|
|
55
55
|
export * from './engine/json/schema/type/SchemaType';
|
|
56
|
-
export * from './engine/json/schema/object/AdditionalPropertiesType';
|
|
57
56
|
export * from './engine/json/schema/SchemaUtil';
|
|
58
57
|
export * from './engine/json/schema/string/StringFormat';
|
|
59
58
|
export * from './engine/HybridRepository';
|
|
@@ -77,3 +76,4 @@ export * from './engine/exception/ExecutionException';
|
|
|
77
76
|
export * from './engine/exception/KIRuntimeException';
|
|
78
77
|
export * from './engine/runtime/expression/operators/unary';
|
|
79
78
|
export * from './engine/runtime/expression/operators/binary';
|
|
79
|
+
export * from './engine/runtime/expression/operators/ternary';
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { Schema } from '../Schema';
|
|
2
|
-
|
|
3
|
-
export class AdditionalPropertiesType {
|
|
4
|
-
private booleanValue?: boolean;
|
|
5
|
-
private schemaValue?: Schema;
|
|
6
|
-
|
|
7
|
-
public getBooleanValue(): boolean | undefined {
|
|
8
|
-
return this.booleanValue;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
public getSchemaValue(): Schema | undefined {
|
|
12
|
-
return this.schemaValue;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
public setBooleanValue(booleanValue: boolean): AdditionalPropertiesType {
|
|
16
|
-
this.booleanValue = booleanValue;
|
|
17
|
-
return this;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
public setSchemaValue(schemaValue: Schema): AdditionalPropertiesType {
|
|
21
|
-
this.schemaValue = schemaValue;
|
|
22
|
-
return this;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public static from(obj: any): AdditionalPropertiesType | undefined {
|
|
26
|
-
if (!obj) return undefined;
|
|
27
|
-
const ad = new AdditionalPropertiesType();
|
|
28
|
-
ad.booleanValue = obj.booleanValue;
|
|
29
|
-
ad.schemaValue = obj.schemaValue;
|
|
30
|
-
return ad;
|
|
31
|
-
}
|
|
32
|
-
}
|