Permalink
Browse files

code cleanup

  • Loading branch information...
1 parent 0d0816d commit bf2d144a740c97c2c291c45b1b857b616c46224d @doug-martin doug-martin committed Sep 6, 2012
Showing with 26 additions and 24 deletions.
  1. +5 −0 examples/fibonacci.dsl.js
  2. +7 −7 lib/constraint.js
  3. +0 −1 lib/constraintMatcher.js
  4. +2 −3 lib/index.js
  5. +7 −7 lib/nodes.js
  6. +1 −1 lib/rule.js
  7. +1 −1 readme.md
  8. +1 −0 test/noolsParser.test.js
  9. +2 −3 test/parser.test.js
  10. +0 −1 test/rules/orRule.nools
@@ -1,18 +1,23 @@
(function () {
"use strict";
+ var profiler = require("profiler");
+
var nools = require("../index");
var flow = nools.compile(__dirname + "/fibonacci.nools");
var Fibonacci = flow.getDefined("fibonacci"), Result = flow.getDefined("result");
+ profiler.resume();
var r1 = new Result(),
session1 = flow.getSession(new Fibonacci({sequence:10}), r1),
s1 = +(new Date());
session1.match().then(function () {
+
console.log("%d [%dms]", r1.result, +(new Date()) - s1);
session1.dispose();
+ profiler.pause();
});
var r2 = new Result(),
View
@@ -14,7 +14,7 @@
this.constraint = constraint;
},
assert:function () {
- throw "not implemented";
+ throw new Error("not implemented");
},
equal:function (constraint) {
@@ -107,7 +107,7 @@
this._alias = alias;
this.vars = constraintMatcher.getIdentifiers(this.constraint).filter(function (v) {
return v !== alias;
- }, this);
+ });
}
}
}
@@ -122,15 +122,15 @@
},
equal:function (constraint) {
- return comb.isInstanceOf(constraint, this._static) && this.alias === constraint.alias && JSON.stringify(this.constraint) === JSON.stringify(constraint.constraint);
+ return comb.isInstanceOf(constraint, this._static) && this.alias === constraint.alias && comb.deepEqual(this.constraint, constraint.constraint);
},
assert:function (factHash) {
- var fact = factHash[this.alias];
- Object.keys(this.constraint).forEach(function (k) {
- var v = this.constraint[k];
+ var fact = factHash[this.alias], constraint = this.constraint;
+ Object.keys(constraint).forEach(function (k) {
+ var v = constraint[k];
factHash[v] = fact[k];
- }, this);
+ });
return true;
},
@@ -283,7 +283,6 @@
var toJs = exports.toJs = function (rule) {
var js = lang.parse(rule);
var vars = lang.getIdentifiers(rule);
-
return ["(function(hash){", vars.map(function(v){
var ret = ["var ", v, " = "];
if(definedFuncs.hasOwnProperty(v)){
View
@@ -43,7 +43,6 @@
};
var FactHash = comb.define(null, {
-
instance:{
constructor:function (def) {
this.memory = [];
@@ -230,13 +229,13 @@
var activation = agenda.pop();
activation.used = true;
flow.emit("fire", activation.rule.name, activation.match.factHash);
- when(activation.rule.fire(flow, activation.match), function () {
+ when(activation.rule.fire(flow, activation.match)).then(function () {
if (flow.__wmAltered) {
rootNode.incrementCounter();
flow.__wmAltered = false;
}
process.nextTick(fire);
- }, comb.hitch(ret, "errback"));
+ }, ret);
} else {
ret.callback();
}
View
@@ -126,7 +126,7 @@
return n;
}
}
- this.constraints.push(node);
+ constraints.push(node);
return node;
},
@@ -139,7 +139,7 @@
return n;
}
}
- this.typeNodes.push(ret);
+ constraints.push(ret);
return ret;
},
@@ -165,13 +165,13 @@
__createJoinNode:function (pattern, outNode, side) {
var joinNode;
- if (isInstanceOf(pattern.rightPattern, NotPattern)) {
+ if (pattern.rightPattern instanceof NotPattern) {
joinNode = new NotNode();
} else {
joinNode = new JoinNode();
}
var parentNode = joinNode;
- if (isInstanceOf(outNode, JoinNode)) {
+ if (outNode instanceof JoinNode) {
var adapterNode = this.__createAdapterNode(side);
parentNode.addOutNode(adapterNode, pattern);
parentNode = adapterNode;
@@ -181,12 +181,12 @@
},
__addToNetwork:function (pattern, outNode, side) {
- if (isInstanceOf(pattern, ObjectPattern)) {
- if (isInstanceOf(pattern, NotPattern) && (!side || side === "left")) {
+ if (pattern instanceof ObjectPattern) {
+ if (pattern instanceof NotPattern && (!side || side === "left")) {
return this.__addToNetwork(new CompositePattern(new InitialFactPattern(), pattern), outNode, side);
}
return this.__createAlphaNode(pattern, outNode, side);
- } else if (isInstanceOf(pattern, CompositePattern)) {
+ } else if (pattern instanceof CompositePattern) {
var joinNode = this.__createJoinNode(pattern, outNode, side);
this.__addToNetwork(pattern.leftPattern, joinNode, "left");
this.__addToNetwork(pattern.rightPattern, joinNode, "right");
View
@@ -36,7 +36,7 @@
fire:function (flow, match) {
var ret = new comb.Promise(), cb = this.cb;
if (cb.length === 3) {
- this.cb.call(flow, match.factHash, flow, hitch(ret, "classic"));
+ this.cb.call(flow, match.factHash, flow, ret.classic.bind(ret));
} else {
return comb.when(this.cb.call(flow, match.factHash, flow));
}
View
@@ -67,7 +67,7 @@ In the above flow definition 2 rules were defined
* The messages's message must match the regular expression "/.*goodbye$/"(anything that ends in goodbye)
* When matched the resulting message is logged.
-###Nools
+###Nools DSL
You may also use the `nools` rules language to define your rules
@@ -43,6 +43,7 @@
});
it.describe("parsing function", function (it) {
+
it.should("parse a function statement", function () {
var parsed = noolsParser.parse("function myFunc(a, b) {return a + b}");
assert.lengthOf(parsed.rules, 0);
View
@@ -76,12 +76,12 @@
]);
assert.deepEqual(parser.parseConstraint("a =~ /^hello$/"), [
['a', null, 'identifier'],
- [/hello/, null, 'regexp'],
+ [/^hello$/, null, 'regexp'],
'like'
]);
assert.deepEqual(parser.parseConstraint("a like /^hello$/"), [
['a', null, 'identifier'],
- [/hello/, null, 'regexp'],
+ [/^hello$/, null, 'regexp'],
'like'
]);
@@ -808,6 +808,5 @@
]);
});
-
});
})();
@@ -7,7 +7,6 @@ rule hello {
count : Count
}
then {
- console.log("called");
count.called++;
}
}

0 comments on commit bf2d144

Please sign in to comment.