Skip to content

Commit

Permalink
Merge pull request #14 from heckj/cleanup
Browse files Browse the repository at this point in the history
code cleanup
  • Loading branch information
munimkazia committed May 30, 2014
2 parents 82b551f + eb3fa08 commit 2e13972
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 131 deletions.
34 changes: 34 additions & 0 deletions .jshintrc
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
}
}

6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
*
*/

module.exports = require('./lib/node-rules');
(function() {
'use strict';

module.exports = require('./lib/node-rules');
}(module.exports));
218 changes: 88 additions & 130 deletions lib/node-rules.js
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));

0 comments on commit 2e13972

Please sign in to comment.