Skip to content

Commit 2e13972

Browse files
committed
Merge pull request #14 from heckj/cleanup
code cleanup
2 parents 82b551f + eb3fa08 commit 2e13972

File tree

3 files changed

+127
-131
lines changed

3 files changed

+127
-131
lines changed

.jshintrc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"nonew": true,
3+
"plusplus": true,
4+
"curly": true,
5+
"latedef": "nofunc",
6+
"unused": true,
7+
"noarg": true,
8+
"trailing": true,
9+
"indent": 2,
10+
"forin": true,
11+
"noempty": true,
12+
"node": true,
13+
"eqeqeq": true,
14+
"strict": true,
15+
"undef": true,
16+
"bitwise": true,
17+
"newcap": true,
18+
"immed": true,
19+
"camelcase": true,
20+
"nonbsp": true,
21+
"maxlen": 100,
22+
"freeze": true,
23+
24+
"globals": {
25+
"describe": true,
26+
"it": true,
27+
"before": true,
28+
"beforeEach": true,
29+
"after": true,
30+
"afterEach": true,
31+
"expect": true
32+
}
33+
}
34+

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44
*
55
*/
66

7-
module.exports = require('./lib/node-rules');
7+
(function() {
8+
'use strict';
9+
10+
module.exports = require('./lib/node-rules');
11+
}(module.exports));

lib/node-rules.js

Lines changed: 88 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,95 @@
1-
var _ = require('underscore');
1+
(function() {
2+
'use strict';
23

3-
exports.version = '2.2.0';
4+
var _ = require('underscore');
45

5-
function RuleEngine(rules) {
6-
7-
this.rules = rules;
8-
this.rules = this.rules.filter(function(a) {
9-
if(a.on == 1)
10-
return a;
11-
});
12-
13-
this.rules.sort(function(a,b) {
14-
return b.priority - a.priority;
15-
});
16-
return this;
17-
}
6+
exports.version = '2.2.1';
187

19-
RuleEngine.prototype.execute = function (fact,callback) {
20-
21-
//these new attributes has to be there in both last session and current session so that compare works perfectly.
22-
fact['process'] = false;
23-
fact['result'] = true;
24-
25-
var session = _.clone(fact)
26-
, last_session = _.clone(fact)
27-
, goal = false;
28-
29-
var _rules = this.rules;
30-
31-
(function doit(x) {
8+
function RuleEngine(rules) {
329

33-
if (x < _rules.length && session.process == false) {
10+
this.rules = rules;
11+
this.rules = this.rules.filter(function(a) {
12+
if(a.on === 1) {
13+
return a;
14+
}
15+
});
3416

35-
var outcome = true;
36-
37-
_rulelist = _.flatten([_rules[x].condition]);
38-
39-
(function looprules(y) {
40-
41-
42-
if(y < _rulelist.length) {
43-
44-
if(typeof _rulelist[y] === 'string')
45-
_rulelist[y] = eval('('+ _rulelist[y] + ')');
46-
47-
_rulelist[y].call({}, session,function(out){
48-
49-
outcome = outcome && out;
50-
process.nextTick(function(){
51-
return looprules(y+1);
52-
});
53-
54-
55-
});
56-
57-
58-
} else {
59-
60-
61-
if (outcome) {
62-
63-
_consequencelist = _.flatten([_rules[x].consequence]);
64-
65-
(function loopconsequence(z) {
66-
67-
if(z < _consequencelist.length) {
68-
69-
if(typeof _consequencelist[z] === 'string')
70-
_consequencelist[z] = eval('('+ _consequencelist[z] + ')');
71-
72-
_consequencelist[z].apply(session, [function() {
73-
74-
if (!_.isEqual(last_session,session)) {
75-
76-
last_session = _.clone(session);
77-
process.nextTick(function(){
78-
return doit(0);
79-
});
80-
81-
82-
83-
} else {
84-
85-
process.nextTick(function(){
86-
return loopconsequence(z+1);
87-
});
88-
89-
}
90-
91-
}]);
92-
93-
} else {
94-
95-
process.nextTick(function(){
96-
return doit(x+1);
97-
});
98-
99-
100-
}
101-
102-
103-
})(0);
104-
105-
} else {
106-
107-
process.nextTick(function(){
108-
return doit(x+1);
109-
});
110-
111-
112-
}
113-
114-
115-
}
116-
117-
118-
119-
120-
})(0);
121-
17+
this.rules.sort(function(a, b) {
18+
return b.priority - a.priority;
19+
});
20+
return this;
21+
}
12222

123-
} else {
124-
125-
process.nextTick(function(){
126-
return callback(session);
127-
});
128-
129-
130-
}
131-
132-
})(0);
133-
134-
135-
};
23+
RuleEngine.prototype.execute = function(fact, callback) {
13624

137-
module.exports = RuleEngine;
25+
//these new attributes have to be in both last session and current session to support
26+
// the compare function
27+
fact.process = false;
28+
fact.result = true;
29+
30+
var session = _.clone(fact);
31+
var lastSession = _.clone(fact);
32+
var _rules = this.rules;
33+
34+
(function doit(x) {
35+
36+
if (x < _rules.length && session.process === false) {
37+
38+
var outcome = true;
39+
var _rulelist = _.flatten([_rules[x].condition]);
40+
41+
(function looprules(y) {
42+
if(y < _rulelist.length) {
43+
if(typeof _rulelist[y] === 'string') {
44+
_rulelist[y] = eval('('+ _rulelist[y] + ')');
45+
}
46+
_rulelist[y].call({}, session, function(out) {
47+
outcome = outcome && out;
48+
process.nextTick(function(){
49+
return looprules(y+1);
50+
});
51+
});
52+
53+
} else {
54+
if (outcome) {
55+
var _consequencelist = _.flatten([_rules[x].consequence]);
56+
(function loopconsequence(z) {
57+
if(z < _consequencelist.length) {
58+
if(typeof _consequencelist[z] === 'string'){
59+
_consequencelist[z] = eval('('+ _consequencelist[z] + ')');
60+
}
61+
_consequencelist[z].apply(session, [function() {
62+
63+
if (!_.isEqual(lastSession,session)) {
64+
lastSession = _.clone(session);
65+
process.nextTick(function(){
66+
return doit(0);
67+
});
68+
} else {
69+
process.nextTick(function(){
70+
return loopconsequence(z+1);
71+
});
72+
}
73+
}]);
74+
} else {
75+
process.nextTick(function(){
76+
return doit(x+1);
77+
});
78+
}
79+
})(0);
80+
} else {
81+
process.nextTick(function(){
82+
return doit(x+1);
83+
});
84+
}
85+
}
86+
})(0);
87+
} else {
88+
process.nextTick(function(){
89+
return callback(session);
90+
});
91+
}
92+
})(0);
93+
};
94+
module.exports = RuleEngine;
95+
}(module.exports));

0 commit comments

Comments
 (0)