-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from heckj/cleanup
code cleanup
- Loading branch information
Showing
3 changed files
with
127 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"nonew": true, | ||
"plusplus": true, | ||
"curly": true, | ||
"latedef": "nofunc", | ||
"unused": true, | ||
"noarg": true, | ||
"trailing": true, | ||
"indent": 2, | ||
"forin": true, | ||
"noempty": true, | ||
"node": true, | ||
"eqeqeq": true, | ||
"strict": true, | ||
"undef": true, | ||
"bitwise": true, | ||
"newcap": true, | ||
"immed": true, | ||
"camelcase": true, | ||
"nonbsp": true, | ||
"maxlen": 100, | ||
"freeze": true, | ||
|
||
"globals": { | ||
"describe": true, | ||
"it": true, | ||
"before": true, | ||
"beforeEach": true, | ||
"after": true, | ||
"afterEach": true, | ||
"expect": true | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,137 +1,95 @@ | ||
var _ = require('underscore'); | ||
(function() { | ||
'use strict'; | ||
|
||
exports.version = '2.2.0'; | ||
var _ = require('underscore'); | ||
|
||
function RuleEngine(rules) { | ||
|
||
this.rules = rules; | ||
this.rules = this.rules.filter(function(a) { | ||
if(a.on == 1) | ||
return a; | ||
}); | ||
|
||
this.rules.sort(function(a,b) { | ||
return b.priority - a.priority; | ||
}); | ||
return this; | ||
} | ||
exports.version = '2.2.1'; | ||
|
||
RuleEngine.prototype.execute = function (fact,callback) { | ||
|
||
//these new attributes has to be there in both last session and current session so that compare works perfectly. | ||
fact['process'] = false; | ||
fact['result'] = true; | ||
|
||
var session = _.clone(fact) | ||
, last_session = _.clone(fact) | ||
, goal = false; | ||
|
||
var _rules = this.rules; | ||
|
||
(function doit(x) { | ||
function RuleEngine(rules) { | ||
|
||
if (x < _rules.length && session.process == false) { | ||
this.rules = rules; | ||
this.rules = this.rules.filter(function(a) { | ||
if(a.on === 1) { | ||
return a; | ||
} | ||
}); | ||
|
||
var outcome = true; | ||
|
||
_rulelist = _.flatten([_rules[x].condition]); | ||
|
||
(function looprules(y) { | ||
|
||
|
||
if(y < _rulelist.length) { | ||
|
||
if(typeof _rulelist[y] === 'string') | ||
_rulelist[y] = eval('('+ _rulelist[y] + ')'); | ||
|
||
_rulelist[y].call({}, session,function(out){ | ||
|
||
outcome = outcome && out; | ||
process.nextTick(function(){ | ||
return looprules(y+1); | ||
}); | ||
|
||
|
||
}); | ||
|
||
|
||
} else { | ||
|
||
|
||
if (outcome) { | ||
|
||
_consequencelist = _.flatten([_rules[x].consequence]); | ||
|
||
(function loopconsequence(z) { | ||
|
||
if(z < _consequencelist.length) { | ||
|
||
if(typeof _consequencelist[z] === 'string') | ||
_consequencelist[z] = eval('('+ _consequencelist[z] + ')'); | ||
|
||
_consequencelist[z].apply(session, [function() { | ||
|
||
if (!_.isEqual(last_session,session)) { | ||
|
||
last_session = _.clone(session); | ||
process.nextTick(function(){ | ||
return doit(0); | ||
}); | ||
|
||
|
||
|
||
} else { | ||
|
||
process.nextTick(function(){ | ||
return loopconsequence(z+1); | ||
}); | ||
|
||
} | ||
|
||
}]); | ||
|
||
} else { | ||
|
||
process.nextTick(function(){ | ||
return doit(x+1); | ||
}); | ||
|
||
|
||
} | ||
|
||
|
||
})(0); | ||
|
||
} else { | ||
|
||
process.nextTick(function(){ | ||
return doit(x+1); | ||
}); | ||
|
||
|
||
} | ||
|
||
|
||
} | ||
|
||
|
||
|
||
|
||
})(0); | ||
|
||
this.rules.sort(function(a, b) { | ||
return b.priority - a.priority; | ||
}); | ||
return this; | ||
} | ||
|
||
} else { | ||
|
||
process.nextTick(function(){ | ||
return callback(session); | ||
}); | ||
|
||
|
||
} | ||
|
||
})(0); | ||
|
||
|
||
}; | ||
RuleEngine.prototype.execute = function(fact, callback) { | ||
|
||
module.exports = RuleEngine; | ||
//these new attributes have to be in both last session and current session to support | ||
// the compare function | ||
fact.process = false; | ||
fact.result = true; | ||
|
||
var session = _.clone(fact); | ||
var lastSession = _.clone(fact); | ||
var _rules = this.rules; | ||
|
||
(function doit(x) { | ||
|
||
if (x < _rules.length && session.process === false) { | ||
|
||
var outcome = true; | ||
var _rulelist = _.flatten([_rules[x].condition]); | ||
|
||
(function looprules(y) { | ||
if(y < _rulelist.length) { | ||
if(typeof _rulelist[y] === 'string') { | ||
_rulelist[y] = eval('('+ _rulelist[y] + ')'); | ||
} | ||
_rulelist[y].call({}, session, function(out) { | ||
outcome = outcome && out; | ||
process.nextTick(function(){ | ||
return looprules(y+1); | ||
}); | ||
}); | ||
|
||
} else { | ||
if (outcome) { | ||
var _consequencelist = _.flatten([_rules[x].consequence]); | ||
(function loopconsequence(z) { | ||
if(z < _consequencelist.length) { | ||
if(typeof _consequencelist[z] === 'string'){ | ||
_consequencelist[z] = eval('('+ _consequencelist[z] + ')'); | ||
} | ||
_consequencelist[z].apply(session, [function() { | ||
|
||
if (!_.isEqual(lastSession,session)) { | ||
lastSession = _.clone(session); | ||
process.nextTick(function(){ | ||
return doit(0); | ||
}); | ||
} else { | ||
process.nextTick(function(){ | ||
return loopconsequence(z+1); | ||
}); | ||
} | ||
}]); | ||
} else { | ||
process.nextTick(function(){ | ||
return doit(x+1); | ||
}); | ||
} | ||
})(0); | ||
} else { | ||
process.nextTick(function(){ | ||
return doit(x+1); | ||
}); | ||
} | ||
} | ||
})(0); | ||
} else { | ||
process.nextTick(function(){ | ||
return callback(session); | ||
}); | ||
} | ||
})(0); | ||
}; | ||
module.exports = RuleEngine; | ||
}(module.exports)); |