Skip to content

Commit 33a19db

Browse files
dynamic binding experiment. rule with closures bound are commented in test
1 parent d788880 commit 33a19db

File tree

2 files changed

+44
-36
lines changed

2 files changed

+44
-36
lines changed

lib/node-rules.js

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,32 @@
2626

2727
RuleEngine.prototype.register = function(rules) {
2828

29+
if(typeof(rules) == "string") {
30+
31+
rules = JSON.parse(rules);
32+
33+
}
34+
35+
2936
if(rules instanceof Array) {
3037

38+
rules = rules.map(function(rule){
39+
40+
rule.condition = eval("("+rule.condition+")");
41+
return rule;
42+
43+
});
44+
3145
this.rules = this.rules.concat(rules);
3246

33-
} else if(typeof(rules) != "undefined") {
47+
} else if(rules !== null && typeof(rules) == "object") {
48+
49+
rules.condition = eval("("+rules.condition+")");
3450

3551
this.rules.push(rules);
3652

3753
}
54+
3855
this.sync();
3956

4057
};
@@ -59,16 +76,18 @@
5976

6077
//these new attributes have to be in both last session and current session to support
6178
// the compare function
62-
fact.process = false;
79+
fact.complete = false;
6380
fact.result = true;
6481

6582
var session = _.clone(fact);
6683
var lastSession = _.clone(fact);
6784
var _rules = this.activeRules;
6885

86+
87+
6988
(function doit(x) {
7089

71-
if (x < _rules.length && session.process === false) {
90+
if (x < _rules.length && session.complete === false) {
7291

7392
var _rule = _rules[x].condition;
7493

@@ -81,14 +100,18 @@
81100
_consequence.call(session, function() {
82101

83102
if (!_.isEqual(lastSession,session)) {
103+
84104
lastSession = _.clone(session);
85105
process.nextTick(function(){
86106
return doit(0);
87107
});
108+
88109
} else {
110+
89111
process.nextTick(function(){
90112
return doit(x+1);
91113
});
114+
92115
}
93116

94117
});
@@ -113,18 +136,18 @@
113136
})(0);
114137
};
115138

116-
RuleEngine.prototype.findRules = function(condition) {
139+
RuleEngine.prototype.findRules = function(filter) {
117140

118-
var find = _.matches(condition);
141+
var find = _.matches(filter);
119142
return _.filter(this.rules, find);
120143

121144
}
122145

123-
RuleEngine.prototype.turn = function(state,condition) {
146+
RuleEngine.prototype.turn = function(state,filter) {
124147

125148
var state = (state==="on" || state==="ON") ? true : false;
126149

127-
var rules = this.findRules(condition);
150+
var rules = this.findRules(filter);
128151

129152
for(var i=0,j=rules.length; i<j; i++) {
130153

@@ -137,10 +160,10 @@
137160
}
138161

139162

140-
RuleEngine.prototype.prioritize = function(priority,condition) {
163+
RuleEngine.prototype.prioritize = function(priority,filter) {
141164

142165
priority = parseInt(priority,10);
143-
var rules = this.findRules(condition);
166+
var rules = this.findRules(filter);
144167

145168
for(var i=0,j=rules.length; i<j; i++) {
146169

@@ -179,21 +202,6 @@
179202

180203
this.init();
181204

182-
if(rules instanceof Array) {
183-
184-
rules = rules.map(function(rule){
185-
186-
rule.condition = eval("("+rule.condition+")");
187-
return rule;
188-
189-
});
190-
191-
} else if(typeof(rules) != "undefined") {
192-
193-
rules.condition = eval("("+rules.condition+")");
194-
195-
}
196-
197205
this.register(rules);
198206

199207
};

test/rules.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var rules = [
4242
function(cb) {
4343
console.log("Rule 1 matched for "+this.name+": blocks transactions below value 500. Rejecting payment.");
4444
this.result = false;
45-
this.process = true;
45+
this.complete = true;
4646
cb();
4747
}
4848
},
@@ -60,7 +60,7 @@ var rules = [
6060
function(cb) {
6161
console.log("Rule 2 matched for "+this.name+": if the users credibility value is more, then avoid checking further. Accepting payment. ");
6262
this.result = true;
63-
this.process = true;
63+
this.complete = true;
6464
cb();
6565
}
6666
},
@@ -78,7 +78,7 @@ var rules = [
7878
function(cb) {
7979
console.log("Rule 3 matched for "+this.name+": filter American Express credit cards for payment above 10000. Rejecting payment.");
8080
this.result = false;
81-
this.process = true;
81+
this.complete = true;
8282
cb();
8383
}
8484
},
@@ -99,7 +99,7 @@ var rules = [
9999

100100
console.log("Rule 4 matched for "+this.name+": reject the payment if the payment type belong to cash card. Rejecting payment.");
101101
this.result = false;
102-
this.process = true;
102+
this.complete = true;
103103
cb();
104104

105105
}
@@ -118,7 +118,7 @@ var rules = [
118118
function(cb) {
119119
console.log("Rule 5 matched for "+this.name+": reject the payment if the payment above 10000 and customer type is guest. Rejecting payment.");
120120
this.result = false;
121-
this.process = true;
121+
this.complete = true;
122122
cb();
123123
}
124124
},
@@ -134,7 +134,7 @@ var rules = [
134134
},
135135
"consequence":
136136
function(cb) {
137-
console.log("Rule 6 matched for "+this.name+": support rule written for blocking payment above 10000 from guests. Process left to chain with rule 6.");
137+
console.log("Rule 6 matched for "+this.name+": support rule written for blocking payment above 10000 from guests. complete left to chain with rule 6.");
138138
this.customerType = "guest";
139139
cb();
140140
}
@@ -153,7 +153,7 @@ var rules = [
153153
function(cb) {
154154
console.log("Rule 7 matched for "+this.name+": turn on this rule to block the payment from a specific app. Reject Paymant.");
155155
this.result = false;
156-
this.process = true;
156+
this.complete = true;
157157
cb();
158158
}
159159
},
@@ -171,7 +171,7 @@ var rules = [
171171
function(cb) {
172172
console.log("Rule 8 matched for "+this.name+": if the event is top priority event, then do further checks else leave. Accept payment as low priority event.");
173173
this.result = true;
174-
this.process = true;
174+
this.complete = true;
175175
cb();
176176
}
177177
},
@@ -199,11 +199,11 @@ var rules = [
199199
function(cb) {
200200
console.log("Rule 9 matched for "+this.name+": if the ip fall in the given list of formats, then block the transaction. Rejecting payment.");
201201
this.result = false;
202-
this.process = true;
202+
this.complete = true;
203203
cb();
204204
}
205205
},
206-
/**** Rule 10 ****/
206+
/**** Rule 10 * fails
207207
{
208208
"name" : "check if user's name is blacklisted in db",
209209
"description" : "if the user's name is found then block transaction.",
@@ -220,11 +220,11 @@ var rules = [
220220
221221
console.log("Rule 10 matched for "+this.name+": if the user is malicious, then block the transaction. Rejecting payment.");
222222
this.result = false;
223-
this.process = true;
223+
this.complete = true;
224224
cb();
225225
226226
}
227-
}
227+
}****/
228228
];
229229

230230

0 commit comments

Comments
 (0)