-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDecisionTable.js
More file actions
171 lines (171 loc) · 6.25 KB
/
DecisionTable.js
File metadata and controls
171 lines (171 loc) · 6.25 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"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.DecisionTable = exports.DTVariable = exports.DTOutput = exports.HIT_POLICY = void 0;
const Rule_1 = require("./Rule");
const fs = require('fs');
var HIT_POLICY;
(function (HIT_POLICY) {
HIT_POLICY["Unique"] = "Unique";
HIT_POLICY["Any"] = "Any";
HIT_POLICY["First"] = "First";
HIT_POLICY["RuleOrder"] = "Order";
HIT_POLICY["Collect"] = "Collect+";
})(HIT_POLICY = exports.HIT_POLICY || (exports.HIT_POLICY = {}));
class DTOutput {
constructor() {
this.input = {};
this.rules = [];
this.successCount = 0;
this.actions = {};
}
}
exports.DTOutput = DTOutput;
class DTVariable {
}
exports.DTVariable = DTVariable;
class DecisionTable {
constructor({ name, conditionVars, actionVars, rules, hitPolicy }) {
this.name = name;
const condCount = conditionVars.length;
const actioCount = actionVars.length;
this.conditionVars = conditionVars;
this.actionVars = actionVars;
this.hitPolicy = hitPolicy;
let id = 1;
this.rules = [];
rules.forEach(rule => {
this.rules.push(new Rule_1.Rule(rule[0], rule.slice(1, condCount + 1), rule.slice(condCount + 1), conditionVars, actionVars));
});
}
processAll() {
if (this.hitPolicy == 'Collect+')
return true;
else
return false;
}
/**
* Execute a DT on the fly, passing multiple records
* used for WebAPI
* @param dtDefinition
* @param data
*/
static execute(dtDefinition, data) {
return __awaiter(this, void 0, void 0, function* () {
const dt = new DecisionTable(dtDefinition);
const response = [];
let i;
for (i = 0; i < data.length; i++) {
const record = data[i];
response.push(yield dt.evaluate(record));
}
return { decisionTable: dt, results: response };
});
}
compile() {
const image = { rules: [] };
this.rules.forEach(rule => {
image.rules.push(rule.compile());
});
return image;
}
evaluate(data) {
return __awaiter(this, void 0, void 0, function* () {
const output = new DTOutput(); //= { _results: [], _success: 0 };
var r = 0;
output.input = data;
const rules = this.rules;
let ret;
for (r = 0; r < rules.length; r++) {
let rule = rules[r];
const result = { ruleId: rule.id };
ret = yield rule.evaluate(data, result);
if (ret) {
// console.log(" Rule #" + rule.id + " has returned");
output.successCount++;
output.rules.push(result);
if (!this.processAll)
break;
}
else {
// console.log(" Rule #" + rule.id + " has failed on " + result['failedCondition'] +" condition");
output.rules.push(result);
}
}
//console.log(results);
return yield this.processResults(output);
//console.log("**No Rule has returned**");
});
}
processResults(output) {
let operation = (this.hitPolicy == 'Collect+') ? '+' : '';
let i;
for (i = 0; i < output.rules.length; i++) {
const result = output.rules[i];
if (result.output) {
// console.log("output:");
// console.log(result.output)
this.actionVars.forEach(action => {
switch (operation) {
case '':
output.actions[action.name] = result.output[action.name];
break;
case '+':
if (output.actions[action.name])
output.actions[action.name] += result.output[action.name];
else
output.actions[action.name] = result.output[action.name];
break;
}
});
if (!this.processAll) {
return output;
}
}
}
// console.log(output);
return output;
}
save(fileName) {
fs.writeFileSync(fileName, this.asJson(), function (err) { });
}
static load(fileName) {
const json = fs.readFileSync(fileName, { encoding: 'utf8', flag: 'r' });
// console.log(json);
return new DecisionTable(JSON.parse(json));
}
asJson() {
const rules = [];
this.rules.forEach(rule => {
// console.log(rule.asJson());
rules.push(rule.asJson());
});
const obj = {
name: this.name,
hitPolicy: this.hitPolicy,
conditionVars: this.conditionVars,
actionVars: this.actionVars,
rules: rules
};
// console.log(obj);
return JSON.stringify(obj, null, 2);
}
}
exports.DecisionTable = DecisionTable;
function trimParam(param) {
if (param.startsWith('"') && param.endsWith('"'))
return param.substring(1, param.length - 1);
if (param.startsWith("'") && param.endsWith("'"))
return param.substring(1, param.length - 1);
else
return param.trim();
}
//# sourceMappingURL=DecisionTable.js.map