-
Notifications
You must be signed in to change notification settings - Fork 2
/
Expression.js
113 lines (113 loc) · 3.89 KB
/
Expression.js
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
106
107
108
109
110
111
112
113
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Condition = exports.Expression = void 0;
const feel = require('js-feel/dist/feel');
const FEEL = feel;
class Expression {
constructor(script) {
this.script = script;
}
get isCondition() { return false; }
static load(json) {
const inst = new Expression(json);
return inst;
}
compile() {
return __awaiter(this, void 0, void 0, function* () {
this.ast = yield parse(this.script);
// this.ast = parser.compile(this.script, this.isCondition);
});
}
getState() {
return this;
}
display() {
// this.rootNode.displayExpression();
}
evaluate(data) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.ast)
yield this.compile();
if (this.ast) {
const result = yield this.ast.build(data);
return result;
}
return null;
// const executor = new Executor(data);
//return executor.evaluateCondition(this.rootNode, null, false);
});
}
}
exports.Expression = Expression;
function parse(script, options = {}) {
return __awaiter(this, void 0, void 0, function* () {
try {
const ast = yield FEEL.parse(script, options);
return ast;
}
catch (exc) {
console.log("Error in parsing " + script);
console.log(exc.message);
return null;
}
});
}
class Condition extends Expression {
constructor(script, variableName) {
super(script);
this.variableName = variableName;
}
get isCondition() { return true; }
compile() {
return __awaiter(this, void 0, void 0, function* () {
this.ast = yield 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);
*/
evaluate(data) {
return __awaiter(this, void 0, void 0, function* () {
let value, funct, out;
if (!this.ast)
yield this.compile();
if (!this.ast)
return null;
try {
funct = yield 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);
});
}
}
exports.Condition = Condition;
function getValue(value) {
let val = parseFloat(value);
if (!isNaN(val))
return val;
else
return value;
}
//# sourceMappingURL=Expression.js.map