-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathExpression.ts
More file actions
105 lines (91 loc) · 2.64 KB
/
Expression.ts
File metadata and controls
105 lines (91 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const feel = require('js-feel/dist/feel');
const FEEL = feel;
export class Expression {
script;
ast; // ExpressionNode;
constructor(script) {
this.script = script;
}
get isCondition(): Boolean { return false; }
static load(json): Expression {
const inst = new Expression(json);
return inst;
}
async compile() {
this.ast = await parse(this.script);
// this.ast = parser.compile(this.script, this.isCondition);
}
getState() {
return this;
}
display() {
// this.rootNode.displayExpression();
}
async evaluate(data) {
if (!this.ast)
await this.compile();
if (this.ast) {
const result = await this.ast.build(data);
return result;
}
return null;
// const executor = new Executor(data);
//return executor.evaluateCondition(this.rootNode, null, false);
}
}
async function parse(script, options = {}) {
try {
const ast = await FEEL.parse(script, options);
return ast;
}
catch (exc) {
console.log("Error in parsing " + script);
console.log(exc.message);
return null;
}
}
export class Condition extends Expression {
get isCondition(): Boolean { return true; }
variableName;
constructor(script, variableName) {
super(script);
this.variableName = variableName;
}
async compile() {
this.ast = await parse(this.script, { startRule: 'SimpleUnaryTests' });
// this.ast = parser.compile(this.script, this.isCondition);
}
/*
const condition = await feel.parse(conditionScript, { startRule: 'SimpleUnaryTests' });
const funct = await condition.build(context, {}, 'input');
const out = funct(inputValue);
*/
async evaluate(data) {
let value, funct, out;
if (!this.ast)
await this.compile();
if (!this.ast)
return null;
try {
funct = await this.ast.build(data, {}, 'input');
value = getValue(data[this.variableName]);
out = funct(value);
return out;
}
catch (exc) {
console.log(`Error in evaluating '${this.script}' for value: '${value}'`);
console.log(`--- generated function '${funct.toString()}'`);
console.log(data);
console.log(exc.message);
return out;
}
//return executor.evaluateCondition(this.rootNode, value, true);
}
}
function getValue(value) {
let val = parseFloat(value);
if (!isNaN(val))
return val;
else
return value;
}