-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.js
95 lines (82 loc) · 2.52 KB
/
models.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
function createRule(ruleString) {
// Basic rule parser
const operators = ['AND', 'OR'];
const regex = /(\w+)\s*(>|<|=)\s*([\w'"]+)|(\bAND\b|\bOR\b)/gi;
const stack = [];
const output = [];
let match;
while ((match = regex.exec(ruleString)) !== null) {
if (match[4]) {
while (stack.length && (stack[stack.length - 1] === 'AND' || match[4] === 'OR')) {
output.push(stack.pop());
}
stack.push(match[4]);
} else {
output.push({ type: 'operand', field: match[1], operator: match[2], value: match[3].replace(/['"]/g, '') });
}
}
while (stack.length) {
output.push(stack.pop());
}
return buildAst(output);
}
function buildAst(output) {
// Simplified AST builder
const stack = [];
output.forEach(token => {
if (token.type === 'operand') {
stack.push(token);
} else {
const right = stack.pop();
const left = stack.pop();
stack.push({ type: 'operator', operator: token, left, right });
}
});
return stack[0];
}
function combineRules(rules) {
// Combining rules into a single AST
return rules.reduce((combined, rule) => ({
type: 'operator',
operator: 'AND',
left: combined,
right: rule
}));
}
function evaluateRule(ast, data) {
console.log('Evaluating AST:', JSON.stringify(ast, null, 2));
console.log('Data:', data);
if (ast.type === 'operand') {
const { field, operator, value } = ast;
const fieldValue = data[field];
// Log for debugging
console.log(`Evaluating: ${fieldValue} ${operator} ${value}`);
switch (operator) {
case '>':
return Number(fieldValue) > Number(value);
case '<':
return Number(fieldValue) < Number(value);
case '=':
// Handle both string and numeric comparison
return fieldValue == value; // Use == to handle type coercion
default:
throw new Error('Invalid operator');
}
} else if (ast.type === 'operator') {
const { operator, left, right } = ast;
// Log for debugging
console.log(`Operator: ${operator}`);
console.log('Left:', JSON.stringify(left, null, 2));
console.log('Right:', JSON.stringify(right, null, 2));
if (operator === 'AND') {
return evaluateRule(left, data) && evaluateRule(right, data);
} else if (operator === 'OR') {
return evaluateRule(left, data) || evaluateRule(right, data);
} else {
throw new Error('Invalid operator');
}
} else {
throw new Error('Unknown node type');
}
}
module.exports = { createRule, combineRules, evaluateRule };