Skip to content
Browse files

More changes for browser

* Changed extended configuration
* Added executable to parse nools source files
* Changed parse to return valid JSON
  • Loading branch information...
1 parent 5b1ba71 commit 82e7f865debaf018231d10a301ec1324f3a41dab @doug-martin committed Jan 15, 2013
View
33 bin/nools
@@ -0,0 +1,33 @@
+#!/usr/bin/env node
+
+
+var nools = require(".."),
+ path = require("path");
+
+/**
+ * Module dependencies.
+ */
+
+var program = require('commander');
+
+program
+ .version('0.0.1');
+
+
+program.command("compile")
+ .usage('[options] <file ...>')
+ .description("compile nools dsl")
+ .action(function (cmd) {
+ var files = Array.prototype.slice.call(arguments);
+ files.pop();
+ files.forEach(function (file) {
+ console.log(JSON.stringify(nools.parse(path.resolve(process.cwd(), file))));
+ });
+ });
+
+program.parse(process.argv);
+
+
+
+
+
View
6 examples/fibonacci.dsl.js
@@ -8,7 +8,7 @@
var Fibonacci = flow.getDefined("fibonacci"), Result = flow.getDefined("result");
var r1 = new Result(),
- session1 = flow.getSession(new Fibonacci({sequence:10}), r1),
+ session1 = flow.getSession(new Fibonacci({sequence: 10}), r1),
s1 = +(new Date());
session1.match().then(function () {
@@ -18,15 +18,15 @@
});
var r2 = new Result(),
- session2 = flow.getSession(new Fibonacci({sequence:150}), r2),
+ session2 = flow.getSession(new Fibonacci({sequence: 150}), r2),
s2 = +(new Date());
session2.match().then(function () {
console.log("%d [%dms]", r2.result, +(new Date()) - s2);
session2.dispose();
});
var r3 = new Result(),
- session3 = flow.getSession(new Fibonacci({sequence:1000}), r3),
+ session3 = flow.getSession(new Fibonacci({sequence: 1000}), r3),
s3 = +(new Date());
session3.match().then(function () {
console.log("%d [%dms]", r3.result, +(new Date()) - s3);
View
16 examples/fibonacci.js
@@ -14,26 +14,24 @@ var Result = function (result) {
var flow = nools.flow("Fibonacci Flow", function (flow) {
- flow.rule("Recurse", {priority:1}, [
+ flow.rule("Recurse", {priority: 1}, [
["not", Fibonacci, "f", "f.sequence == 1"],
[Fibonacci, "f1", "f1.sequence != 1"]
], function (facts) {
- var f2 = new Fibonacci(facts.f1.sequence - 1);
- console.log(f2.value);
- this.assert(f2);
+ this.assert(new Fibonacci(facts.f1.sequence - 1));
});
flow.rule("Bootstrap", [
Fibonacci, "f", "f.value == -1 && (f.sequence == 1 || f.sequence == 2)"
], function (facts) {
- var f = facts.f;
- f.value = 1;
- this.modify(f);
+ this.modify(facts.f, function () {
+ this.value = 1;
+ });
});
flow.rule("Calculate", [
- [Fibonacci, "f1", "f1.value != -1", {sequence:"s1"}],
- [Fibonacci, "f2", "f2.value != -1 && f2.sequence == s1 + 1", {sequence:"s2"}],
+ [Fibonacci, "f1", "f1.value != -1", {sequence: "s1"}],
+ [Fibonacci, "f2", "f2.value != -1 && f2.sequence == s1 + 1", {sequence: "s2"}],
[Fibonacci, "f3", "f3.value == -1 && f3.sequence == s2 + 1"],
[Result, "r"]
], function (facts) {
View
59 lib/compile.js
@@ -1,18 +1,16 @@
(function () {
"use strict";
- var extended = require("./extended"),
- fs = require("fs"),
- path = require("path"),
+ var extd = require("./extended"),
parser = require("./parser"),
constraintMatcher = require("./constraintMatcher.js"),
- array = extended.array,
- indexOf = array.indexOf,
- forEach = array.forEach,
- map = array.map,
- removeDuplicates = array.removeDuplicates,
- obj = extended.object,
+ indexOf = extd.indexOf,
+ forEach = extd.forEach,
+ map = extd.map,
+ removeDuplicates = extd.removeDuplicates,
+ obj = extd.hash,
keys = obj.keys,
- merge = obj.merge,
+ merge = extd.merge,
+ isString = extd.isString,
rules = require("./rule");
var modifiers = ["assert", "modify", "retract"];
@@ -38,13 +36,13 @@
declares.push("var " + i + "= flow." + i + ".bind(flow);");
}
});
- forEach(keys(defined), function (i) {
+ extd(defined).keys().forEach(function (i) {
if (action.indexOf(i) !== -1) {
declares.push("var " + i + "= defined." + i + ";");
}
});
- forEach(keys(scope), function (i) {
+ extd(scope).keys().forEach(function (i) {
if (action.indexOf(i) !== -1) {
declares.push("var " + i + "= scope." + i + ";");
}
@@ -65,7 +63,7 @@
var createRuleFromObject = (function () {
var __resolveRule = function (rule, identifiers, conditions, defined, name) {
var condition = [], definedClass = rule[0], alias = rule[1], constraint = rule[2], refs = rule[3];
- if (extended.isHash(constraint)) {
+ if (extd.isHash(constraint)) {
refs = constraint;
constraint = null;
}
@@ -82,7 +80,7 @@
identifiers.push(i);
});
}
- if (extended.isObject(refs)) {
+ if (extd.isObject(refs)) {
for (var j in refs) {
var ident = refs[j];
if (indexOf(identifiers, ident) === -1) {
@@ -94,7 +92,7 @@
};
return function (obj, defined, scope) {
var name = obj.name;
- if (extended.isEmpty(obj)) {
+ if (extd.isEmpty(obj)) {
throw new Error("Rule is empty");
}
var options = obj.options || {};
@@ -103,7 +101,7 @@
throw new Error("no rules defined for rule " + name);
}
var action = obj.action;
- if (extended.isUndefined(action)) {
+ if (extd.isUndefined(action)) {
throw new Error("No action was defined for rule " + name);
}
var conditions = [], identifiers = [];
@@ -160,6 +158,7 @@
var createDefined = (function () {
var _createDefined = function (options) {
+ options = isString(options) ? eval(options) : options;
var ret = options.hasOwnProperty("constructor") && "function" === typeof options.constructor ? options.constructor : function (opts) {
opts = opts || {};
for (var i in opts) {
@@ -181,35 +180,35 @@
};
})();
- exports.compile = function (file, options, cb, Container) {
- if (extended.isFunction(options)) {
+ exports.parse = function (src) {
+ //parse flow from file
+ return parser.parseRuleSet(src);
+
+ };
+
+ exports.compile = function (flowObj, options, cb, Container) {
+ if (extd.isFunction(options)) {
cb = options;
options = {};
} else {
options = options || {};
cb = null;
}
- var flowObj;
-
- //parse flow from file
- flowObj = parser.parseRuleSet(fs.readFileSync(file, "utf8"));
-
- var name = flowObj.name || options.name || path.basename(file, path.extname(file));
+ var name = flowObj.name || options.name;
//if !name throw an error
if (!name) {
throw new Error("Name must be present in JSON or options");
}
var flow = new Container(name);
- var defined = merge({Array: Array, String: String, Number: Number, Boolean: Boolean, RegExp: RegExp, Buffer: Buffer}, options.define || {});
+ var defined = merge({Array: Array, String: String, Number: Number, Boolean: Boolean, RegExp: RegExp}, options.define || {});
+ if (typeof Buffer !== "undefined") {
+ defined.Buffer = Buffer;
+ }
var scope = merge({}, options.scope);
forEach(flowObj.define, function (d) {
defined[d.name] = createDefined(d, flow);
});
- var scopeNames = map(flowObj.scope,function (s) {
- return s.name;
- }).concat(map(keys(scope), function (k) {
- return k;
- }));
+ var scopeNames = extd(flowObj.scope).pluck("name").union(extd(scope).keys()).value();
var definedNames = map(keys(defined), function (s) {
return s;
});
View
31 lib/constraint.js
@@ -1,8 +1,11 @@
"use strict";
-var extended = require("./extended"),
- filter = extended.array.filter,
- declare = require("declare.js"),
+var extd = require("./extended"),
+ object = extd.hash,
+ keys = object.keys,
+ forEach = extd.forEach,
+ filter = extd.filter,
+ declare = extd.declare,
constraintMatcher;
var Constraint = declare({
@@ -15,12 +18,12 @@ var Constraint = declare({
this.type = type;
this.constraint = constraint;
},
- assert: function () {
+ "assert": function () {
throw new Error("not implemented");
},
equal: function (constraint) {
- return extended.instanceOf(constraint, this._static) && this.get("alias") === constraint.get("alias") && constraintMatcher.equal(this.constraint, constraint.constraint);
+ return extd.instanceOf(constraint, this._static) && this.get("alias") === constraint.get("alias") && constraintMatcher.equal(this.constraint, constraint.constraint);
},
getters: {
@@ -39,12 +42,12 @@ Constraint.extend({
this._super(["object", type]);
},
- assert: function (param) {
+ "assert": function (param) {
return param instanceof this.constraint || param.constructor === this.constraint;
},
equal: function (constraint) {
- return extended.instanceOf(constraint, this._static) && this.constraint === constraint.constraint;
+ return extd.instanceOf(constraint, this._static) && this.constraint === constraint.constraint;
}
}
}).as(exports, "ObjectConstraint");
@@ -57,7 +60,7 @@ Constraint.extend({
this._matcher = constraintMatcher.getMatcher(constraint);
},
- assert: function (values) {
+ "assert": function (values) {
return this._matcher(values);
}
}
@@ -71,11 +74,11 @@ Constraint.extend({
},
equal: function (constraint) {
- return extended.instanceOf(constraint, this._static) && this.get("alias") === constraint.get("alias");
+ return extd.instanceOf(constraint, this._static) && this.get("alias") === constraint.get("alias");
},
- assert: function () {
+ "assert": function () {
return true;
}
}
@@ -89,7 +92,7 @@ Constraint.extend({
this._matcher = constraintMatcher.getMatcher(constraint);
},
- assert: function (values) {
+ "assert": function (values) {
return this._matcher(values);
},
@@ -123,12 +126,12 @@ Constraint.extend({
},
equal: function (constraint) {
- return extended.instanceOf(constraint, this._static) && this.get("alias") === constraint.get("alias") && extended.deepEqual(this.constraint, constraint.constraint);
+ return extd.instanceOf(constraint, this._static) && this.get("alias") === constraint.get("alias") && extd.deepEqual(this.constraint, constraint.constraint);
},
- assert: function (factHash) {
+ "assert": function (factHash) {
var fact = factHash[this.get("alias")], constraint = this.constraint;
- Object.keys(constraint).forEach(function (k) {
+ forEach(keys(constraint), function (k) {
var v = constraint[k];
factHash[v] = fact[k];
});
View
35 lib/constraintMatcher.js
@@ -1,15 +1,13 @@
"use strict";
-var extended = require("./extended"),
- isArray = extended.isArray,
- dt = extended.date,
- array = extended.array,
- forEach = array.forEach,
- some = array.some,
- map = array.map,
- indexOf = array.indexOf,
- isNumber = extended.isNumber,
- removeDups = array.removeDuplicates,
+var extd = require("./extended"),
+ isArray = extd.isArray,
+ forEach = extd.forEach,
+ some = extd.some,
+ map = extd.map,
+ indexOf = extd.indexOf,
+ isNumber = extd.isNumber,
+ removeDups = extd.removeDuplicates,
atoms = require("./constraint");
var definedFuncs = {
@@ -60,22 +58,22 @@ var definedFuncs = {
},
dateCmp: function (dt1, dt2) {
- return dt.compare(dt1, dt2);
+ return extd.compare(dt1, dt2);
}
};
forEach(["years", "days", "months", "hours", "minutes", "seconds"], function (k) {
- definedFuncs[k + "FromNow"] = dt[k + "FromNow"];
- definedFuncs[k + "Ago"] = dt[k + "Ago"];
+ definedFuncs[k + "FromNow"] = extd[k + "FromNow"];
+ definedFuncs[k + "Ago"] = extd[k + "Ago"];
});
forEach(["isArray", "isNumber", "isHash", "isObject", "isDate", "isBoolean", "isString", "isRegExp", "isNull", "isEmpty",
"isUndefined", "isDefined", "isUndefinedOrNull", "isPromiseLike", "isFunction", "deepEqual"], function (k) {
- var m = extended[k];
+ var m = extd[k];
definedFuncs[k] = function () {
- return m.apply(extended, arguments);
+ return m.apply(extd, arguments);
};
});
@@ -291,7 +289,7 @@ var lang = {
var toJs = exports.toJs = function (rule) {
var js = lang.parse(rule);
var vars = lang.getIdentifiers(rule);
- return ["(function(hash){", map(vars,function (v) {
+ return ["(function(){ return function jsMatcher(hash){", map(vars,function (v) {
var ret = ["var ", v, " = "];
if (definedFuncs.hasOwnProperty(v)) {
ret.push("definedFuncs['", v, "']");
@@ -300,7 +298,7 @@ var toJs = exports.toJs = function (rule) {
}
ret.push(";");
return ret.join("");
- }).join(""), " return !!(", js, ");", "})"].join("");
+ }).join(""), " return !!(", js, ");", "};})()"].join("");
};
exports.getMatcher = function (rule) {
@@ -321,6 +319,3 @@ exports.getIdentifiers = function (constraint) {
};
-
-
-
View
15 lib/extended.js
@@ -1,12 +1,15 @@
module.exports = require("extended")
.register(require("is-extended"))
- .register("array", require("array-extended"))
- .register("date", require("date-extended"))
- .register("object", require("object-extended"))
- .register("string", require("string-extended"))
+ .register(require("array-extended"))
+ .register(require("date-extended"))
+ .register(require("object-extended"))
+ .register(require("string-extended"))
.register(require("promise-extended"))
- .register("fn", require("function-extended"))
+ .register(require("function-extended"))
.register("HashTable", require("ht"))
- .expose(require("leafy"));
+ .register("declare", require("declare.js"))
+ .register(require("leafy"));
+
+
View
56 lib/index.js
@@ -600,15 +600,19 @@
"use strict";
-var extended = require("./extended"),
- declare = require("declare.js"),
- Promise = extended.Promise,
- when = extended.when,
- bind = extended.fn.bind,
+var extd = require("./extended"),
+ fs = require("fs"),
+ path = require("path"),
+ indexOf = extd.indexOf,
+ forEach = extd.forEach,
+ declare = extd.declare,
+ Promise = extd.Promise,
+ when = extd.when,
+ bind = extd.bind,
+ AVLTree = extd.AVLTree,
nodes = require("./nodes"),
EventEmitter = require("events").EventEmitter,
rule = require("./rule"),
- AVLTree = extended.AVLTree,
wm = require("./workingMemory"),
WorkingMemory = wm.WorkingMemory,
InitialFact = require("./pattern").InitialFact,
@@ -653,14 +657,14 @@ var FactHash = declare({
},
get: function (k) {
- return this.memoryValues[this.memory.indexOf(k)];
+ return this.memoryValues[indexOf(this.memory, k)];
},
remove: function (v) {
var facts = v.match.facts, j = facts.length - 1, mv = this.memoryValues, m = this.memory;
for (; j >= 0; j--) {
- var i = m.indexOf(facts[j].object);
- var arr = mv[i], index = arr.indexOf(v);
+ var i = indexOf(m, facts[j].object);
+ var arr = mv[i], index = indexOf(arr, v);
arr.splice(index, 1);
}
},
@@ -669,7 +673,7 @@ var FactHash = declare({
var facts = insert.match.facts, mv = this.memoryValues, m = this.memory;
var k = facts.length - 1;
for (; k >= 0; k--) {
- var o = facts[k].object, i = m.indexOf(o), arr = mv[i];
+ var o = facts[k].object, i = indexOf(m, o), arr = mv[i];
if (!arr) {
arr = mv[m.push(o) - 1] = [];
}
@@ -910,7 +914,7 @@ var FlowContainer = declare({
getSession: function () {
var flow = new Flow(this.name);
- this.__rules.forEach(function (rule) {
+ forEach(this.__rules, function (rule) {
flow.rule(rule);
});
flow.assert(new InitialFact());
@@ -930,14 +934,40 @@ var FlowContainer = declare({
}).as(exports, "Flow");
+function isNoolsFile(file) {
+ return file.match(/\.nools$/);
+}
+
+function parse(source) {
+ var ret;
+ if (isNoolsFile(source)) {
+ ret = compile.parse(fs.readFileSync(source, "utf8"));
+ } else {
+ ret = compile.parse(source);
+ }
+ return ret;
+}
+
+
exports.flow = function (name, cb) {
return new FlowContainer(name, cb);
};
exports.compile = function (file, options, cb) {
- return compile.compile(file, options, cb, FlowContainer);
-
+ if (extd.isFunction(options)) {
+ cb = options;
+ options = {};
+ } else {
+ options = options || {};
+ cb = null;
+ }
+ options.name = options.name || isNoolsFile(file) ? path.basename(file, path.extname(file)) : null;
+ if (!options.name) {
+ throw new Error("Name required when compiling nools source");
+ }
+ return compile.compile(parse(file), options, cb, FlowContainer);
};
+exports.parse = parse;
View
11 lib/matchResult.js
@@ -1,10 +1,9 @@
"use strict";
-var declare = require("declare.js"),
- extended = require("./extended"),
- merge = extended.object.merge,
- array = extended.array,
- union = array.union,
- map = array.map;
+var extd = require("./extended"),
+ declare = extd.declare,
+ merge = extd.merge,
+ union = extd.union,
+ map = extd.map;
declare({
instance: {
View
37 lib/nodes.js
@@ -1,13 +1,15 @@
"use strict";
-var extended = require("./extended"),
- array = extended.array,
- removeDuplicates = array.removeDuplicates,
- forEach = array.forEach,
- some = array.some,
- flatten = array.flatten,
- intersect = array.intersect,
- declare = require("declare.js"),
- HashTable = extended.HashTable,
+var extd = require("./extended"),
+ bind = extd.bind,
+ removeDuplicates = extd.removeDuplicates,
+ forEach = extd.forEach,
+ some = extd.some,
+ flatten = extd.flatten,
+ intersect = extd.intersect,
+ declare = extd.declare,
+ object = extd.hash,
+ keys = object.keys,
+ HashTable = extd.HashTable,
MatchResult = require("./matchResult"),
pattern = require("./pattern.js"),
ObjectPattern = pattern.ObjectPattern,
@@ -18,7 +20,6 @@ var extended = require("./extended"),
HashConstraint = constraints.HashConstraint,
ReferenceConstraint = constraints.ReferenceConstraint;
-
var Node = declare({
instance: {
constructor: function () {
@@ -29,8 +30,8 @@ var Node = declare({
resolve: function (mr1, mr2) {
var mr1FactHash = mr1.factHash, mr2FactHash = mr2.factHash;
- var fhKeys1 = Object.keys(mr1FactHash),
- fhKeys2 = Object.keys(mr2FactHash);
+ var fhKeys1 = keys(mr1FactHash),
+ fhKeys2 = keys(mr2FactHash);
var i = fhKeys1.length - 1, j = fhKeys2.length - 1;
if (i !== j) {
return false;
@@ -190,7 +191,7 @@ var PropertyNode = AlphaNode.extend({
},
toString: function () {
- return "Property Node" + this._super();
+ return "Property Node" + this._super(arguments);
}
}
});
@@ -236,7 +237,7 @@ var ReferenceNode = AlphaNode.extend({
}
},
toString: function () {
- return "Reference Node" + this._super();
+ return "Reference Node" + this._super(arguments);
}
}
});
@@ -254,7 +255,7 @@ var EqualityNode = AlphaNode.extend({
},
toString: function () {
- return "Equality Node" + this._super();
+ return "Equality Node" + this._super(arguments);
}
}
});
@@ -503,7 +504,7 @@ var NotNode = JoinNode.extend({
retractRight: function (fact) {
var rightMemory = this.rightMemory;
var rightContext = rightMemory.remove(fact.object);
- if (rightContext && !this.refNodes.length && rightMemory.isEmpty) {
+ if (rightContext && !this.refNodes.length && rightMemory.isEmpty()) {
var fl = flatten(this.leftMemory.values()), i = fl.length - 1;
for (; i >= 0; i--) {
var leftContext = fl[i];
@@ -517,7 +518,7 @@ var NotNode = JoinNode.extend({
assertLeft: function (context) {
this.__addToLeftMemory(context);
var rm = this.rightMemory;
- if (!this.refNodes.length && rm.isEmpty) {
+ if (!this.refNodes.length && rm.isEmpty()) {
this.propagateAssert(context);
} else {
var values = rm.values(), i = values.length - 1;
@@ -608,7 +609,7 @@ var TerminalNode = Node.extend({
},
retractResolve: function (match) {
- var resolve = this.resolve.bind(this);
+ var resolve = extd.bind(this, this.resolve);
this.agenda.retract(this, function (v) {
return resolve(v.match, match);
});
View
2 lib/parser/nools/tokens.js
@@ -133,7 +133,7 @@ module.exports = {
var body = utils.getTokensBetween(src, "{", "}", true).join("");
src = src.replace(body, "");
//should
- context.define.push({name: name, properties: eval("(" + body + ")")});
+ context.define.push({name: name, properties: "(" + body + ")"});
return src;
} else {
throw new Error("unexpected token : expected : '{' found : '" + utils.findNextToken(src) + "'");
View
10 lib/pattern.js
@@ -1,8 +1,8 @@
(function () {
"use strict";
- var extended = require("./extended"),
- forEach = extended.array.forEach,
- declare = require("declare.js"),
+ var extd = require("./extended"),
+ forEach = extd.forEach,
+ declare = extd.declare,
constraintMatcher = require("./constraintMatcher"),
constraint = require("./constraint");
@@ -23,7 +23,7 @@
var cnstrnt = new constraint.TrueConstraint();
this.constraints.push(cnstrnt);
}
- if (store && !extended.isEmpty(store)) {
+ if (store && !extd.isEmpty(store)) {
var atm = new constraint.HashConstraint(store);
this.constraints.push(atm);
}
@@ -44,7 +44,7 @@
ObjectPattern.extend().as(exports, "NotPattern");
- var CompositePattern = Pattern.extend({
+ Pattern.extend({
instance: {
constructor: function (left, right) {
View
34 lib/rule.js
@@ -1,19 +1,19 @@
"use strict";
-var extended = require("./extended"),
- Promise = extended.Promise,
- when = extended.when,
- declare = require("declare.js"),
+var extd = require("./extended"),
+ isArray = extd.isArray,
+ Promise = extd.Promise,
+ when = extd.when,
+ declare = extd.declare,
parser = require("./parser"),
pattern = require("./pattern"),
ObjectPattern = pattern.ObjectPattern,
NotPattern = pattern.NotPattern,
- CompositePattern = pattern.CompositePattern,
- array = extended.array;
+ CompositePattern = pattern.CompositePattern;
var InitialRule = declare({});
-var getParamTypeSwitch = extended
+var getParamTypeSwitch = extd
.switcher()
.isEq("string", function () {
return String;
@@ -45,7 +45,7 @@ var getParamTypeSwitch = extended
.switcher();
-var getParamType = extended
+var getParamType = extd
.switcher()
.isString(function (param) {
return getParamTypeSwitch(param.toLowerCase());
@@ -61,11 +61,11 @@ var getParamType = extended
})
.switcher();
-var parsePattern = extended
+var parsePattern = extd
.switcher()
.contains("or", function (condition) {
condition.shift();
- return array(condition).map(function (cond) {
+ return extd(condition).map(function (cond) {
return parsePattern(cond);
}).flatten().value();
})
@@ -114,14 +114,14 @@ var Rule = InitialRule.extend({
});
function createRule(name, options, conditions, cb) {
- if (extended.isArray(options)) {
+ if (extd.isArray(options)) {
cb = conditions;
conditions = options;
} else {
options = options || {};
}
- var isRules = conditions.every(function (cond) {
- return Array.isArray(cond);
+ var isRules = extd.every(conditions, function (cond) {
+ return isArray(cond);
});
if (isRules && conditions.length === 1) {
conditions = conditions[0];
@@ -138,18 +138,18 @@ function createRule(name, options, conditions, cb) {
}
patterns[i].push(patt);
} else {
- array.forEach(patterns, function (p) {
+ extd(patterns).forEach(function (p) {
p.push(patt);
});
}
};
var l = conditions.length, patterns = [];
for (var i = 0; i < l; i++) {
- array.forEach(parsePattern(conditions[i]), _mergePatterns);
+ extd.forEach(parsePattern(conditions[i]), _mergePatterns);
}
- rules = array.map(patterns, function (patterns) {
+ rules = extd.map(patterns, function (patterns) {
var compPat = null;
for (var i = 0; i < patterns.length; i++) {
if (compPat === null) {
@@ -161,7 +161,7 @@ function createRule(name, options, conditions, cb) {
return new Rule(name, options, compPat, cb);
});
} else {
- rules = array.map(parsePattern(conditions), function (cond) {
+ rules = extd.map(parsePattern(conditions), function (cond) {
return new Rule(name, options, cond, cb);
});
}
View
87 package.json
@@ -1,39 +1,52 @@
{
- "name": "nools",
- "description": "A rules engine for node",
- "version": "0.0.4",
- "keywords": [
- "rules",
- "flow",
- "rules engine",
- "rools",
- "drools",
- "async",
- "flow control"
- ],
- "repository": {
- "type": "git",
- "url": "git@github.com:C2FO/nools.git"
- },
- "dependencies": {
- "comb": "~0.2.0",
- "declare.js": "0.0.2"
- },
- "devDependencies": {
- "it": "~0.1.0",
- "jison": ">=0.3.2"
- },
- "homepage": "http://c2fo.github.com/nools",
- "author": "Doug Martin (http://c2fo.com)",
- "main": "index.js",
- "scripts": {
- "test": "it -r dot",
- "create-doc": "rm -rf docs/* && coddoc -f multi-html -d ./lib --dir ./docs"
- },
- "directories": {
- "lib": "lib"
- },
- "engines": {
- "node": ">= 0.6.1"
- }
+ "name": "nools",
+ "description": "A rules engine for node",
+ "version": "0.1.0",
+ "bin": {
+ "nools": "./bin/nools"
+ },
+ "keywords": [
+ "rules",
+ "flow",
+ "rules engine",
+ "rools",
+ "drools",
+ "async",
+ "flow control"
+ ],
+ "repository": {
+ "type": "git",
+ "url": "git@github.com:C2FO/nools.git"
+ },
+ "dependencies": {
+ "declare.js": "0.0.3",
+ "extended": "0.0.1",
+ "is-extended": "0.0.1",
+ "string-extended": "0.0.1",
+ "array-extended": "0.0.1",
+ "date-extended": "0.0.1",
+ "function-extended": "0.0.1",
+ "object-extended": "0.0.1",
+ "promise-extended": "0.0.1",
+ "leafy": "0.0.1",
+ "ht": "0.0.1",
+ "commander": "~1.1.1"
+ },
+ "devDependencies": {
+ "it": "~0.1.0",
+ "jison": ">=0.3.2"
+ },
+ "homepage": "http://c2fo.github.com/nools",
+ "author": "Doug Martin (http://c2fo.com)",
+ "main": "index.js",
+ "scripts": {
+ "test": "it -r dot",
+ "create-doc": "rm -rf docs/* && coddoc -f multi-html -d ./lib --dir ./docs"
+ },
+ "directories": {
+ "lib": "lib"
+ },
+ "engines": {
+ "node": ">= 0.6.1"
+ }
}
View
22 test/constraintMatcher.test.js
@@ -313,27 +313,27 @@ it.describe("constraint matcher",function (it) {
it.should("create js equvalent expression", function () {
assert.equal(constraintMatcher.toJs(parser.parseConstraint("isFalse(a)")),
- "(function(hash){var isFalse = definedFuncs['isFalse'];var a = hash['a']; return !!(isFalse(a));})");
+ "(function(){ return function jsMatcher(hash){var isFalse = definedFuncs['isFalse'];var a = hash['a']; return !!(isFalse(a));};})()");
assert.equal(constraintMatcher.toJs(parser.parseConstraint("isTrue(b)")),
- "(function(hash){var isTrue = definedFuncs['isTrue'];var b = hash['b']; return !!(isTrue(b));})");
+ "(function(){ return function jsMatcher(hash){var isTrue = definedFuncs['isTrue'];var b = hash['b']; return !!(isTrue(b));};})()");
assert.equal(constraintMatcher.toJs(parser.parseConstraint("isFalse(a) && isTrue(b)")),
- "(function(hash){var isFalse = definedFuncs['isFalse'];var a = hash['a'];var isTrue = definedFuncs['isTrue'];var b = hash['b']; return !!(isFalse(a) && isTrue(b));})");
+ "(function(){ return function jsMatcher(hash){var isFalse = definedFuncs['isFalse'];var a = hash['a'];var isTrue = definedFuncs['isTrue'];var b = hash['b']; return !!(isFalse(a) && isTrue(b));};})()");
assert.equal(constraintMatcher.toJs(parser.parseConstraint("isFalse(a) || isTrue(b)")),
- "(function(hash){var isFalse = definedFuncs['isFalse'];var a = hash['a'];var isTrue = definedFuncs['isTrue'];var b = hash['b']; return !!(isFalse(a) || isTrue(b));})");
+ "(function(){ return function jsMatcher(hash){var isFalse = definedFuncs['isFalse'];var a = hash['a'];var isTrue = definedFuncs['isTrue'];var b = hash['b']; return !!(isFalse(a) || isTrue(b));};})()");
assert.equal(constraintMatcher.toJs(parser.parseConstraint("isNumber(b) || isFalse(a) && b == 1")),
- "(function(hash){var isNumber = definedFuncs['isNumber'];var b = hash['b'];var isFalse = definedFuncs['isFalse'];var a = hash['a']; return !!(isNumber(b) || isFalse(a) && b === 1);})");
+ "(function(){ return function jsMatcher(hash){var isNumber = definedFuncs['isNumber'];var b = hash['b'];var isFalse = definedFuncs['isFalse'];var a = hash['a']; return !!(isNumber(b) || isFalse(a) && b === 1);};})()");
assert.equal(constraintMatcher.toJs(parser.parseConstraint("(isNumber(b) || isFalse(a)) && b == 1")),
- "(function(hash){var isNumber = definedFuncs['isNumber'];var b = hash['b'];var isFalse = definedFuncs['isFalse'];var a = hash['a']; return !!(isNumber(b) || isFalse(a) && b === 1);})");
+ "(function(){ return function jsMatcher(hash){var isNumber = definedFuncs['isNumber'];var b = hash['b'];var isFalse = definedFuncs['isFalse'];var a = hash['a']; return !!(isNumber(b) || isFalse(a) && b === 1);};})()");
assert.equal(constraintMatcher.toJs(parser.parseConstraint("(isNumber(b) || isFalse(a)) || b == 1")),
- "(function(hash){var isNumber = definedFuncs['isNumber'];var b = hash['b'];var isFalse = definedFuncs['isFalse'];var a = hash['a']; return !!(isNumber(b) || isFalse(a) || b === 1);})");
+ "(function(){ return function jsMatcher(hash){var isNumber = definedFuncs['isNumber'];var b = hash['b'];var isFalse = definedFuncs['isFalse'];var a = hash['a']; return !!(isNumber(b) || isFalse(a) || b === 1);};})()");
assert.equal(constraintMatcher.toJs(parser.parseConstraint("a.name == 'bob' && isFalse(a.flag) && b == 1")),
- "(function(hash){var a = hash['a'];var isFalse = definedFuncs['isFalse'];var b = hash['b']; return !!(a.name === 'bob' && isFalse(a.flag) && b === 1);})");
+ "(function(){ return function jsMatcher(hash){var a = hash['a'];var isFalse = definedFuncs['isFalse'];var b = hash['b']; return !!(a.name === 'bob' && isFalse(a.flag) && b === 1);};})()");
assert.equal(constraintMatcher.toJs(parser.parseConstraint("a.name.isSomething() && isFalse(a.flag) && b == 1")),
- "(function(hash){var a = hash['a'];var isFalse = definedFuncs['isFalse'];var b = hash['b']; return !!(a.name.isSomething() && isFalse(a.flag) && b === 1);})");
+ "(function(){ return function jsMatcher(hash){var a = hash['a'];var isFalse = definedFuncs['isFalse'];var b = hash['b']; return !!(a.name.isSomething() && isFalse(a.flag) && b === 1);};})()");
assert.equal(constraintMatcher.toJs(parser.parseConstraint("a.name like /hello$/")),
- "(function(hash){var a = hash['a']; return !!(/hello$/.test(a.name));})");
+ "(function(){ return function jsMatcher(hash){var a = hash['a']; return !!(/hello$/.test(a.name));};})()");
assert.equal(constraintMatcher.toJs(parser.parseConstraint("a.name in [a, b, c, 'BOB']")),
- "(function(hash){var a = hash['a'];var b = hash['b'];var c = hash['c']; return !!((function indexOf(arr, searchElement) { if (!isArray(arr)) { throw new TypeError(); } var t = Object(arr); var len = t.length >>> 0; if (len === 0) { return -1; } var n = 0; if (arguments.length > 2) { n = Number(arguments[2]); if (n !== n) { n = 0; } else if (n !== 0 && n !== Infinity && n !== -Infinity) { n = (n > 0 || -1) * floor(abs(n)); } } if (n >= len) { return -1; } var k = n >= 0 ? n : mathMax(len - abs(n), 0); for (; k < len; k++) { if (k in t && t[k] === searchElement) { return k; } } return -1; }([a,b,c,'BOB'],a.name)) != -1);})");
+ "(function(){ return function jsMatcher(hash){var a = hash['a'];var b = hash['b'];var c = hash['c']; return !!((function indexOf(arr, searchElement) { if (!isArray(arr)) { throw new TypeError(); } var t = Object(arr); var len = t.length >>> 0; if (len === 0) { return -1; } var n = 0; if (arguments.length > 2) { n = Number(arguments[2]); if (n !== n) { n = 0; } else if (n !== 0 && n !== Infinity && n !== -Infinity) { n = (n > 0 || -1) * floor(abs(n)); } } if (n >= len) { return -1; } var k = n >= 0 ? n : mathMax(len - abs(n), 0); for (; k < len; k++) { if (k in t && t[k] === searchElement) { return k; } } return -1; }([a,b,c,'BOB'],a.name)) != -1);};})()");
});
});
View
17 test/noolsParser.test.js
@@ -12,17 +12,24 @@ it.describe("nools dsl parser",function (it) {
define: [
{
name: "Test",
- properties: {
- myProp: "value"
- }
+ properties: "({myProp : 'value'})"
}
],
"rules": [],
"scope": []
});
parsed = noolsParser.parse("define Test {myFunc : function(){}}");
//have to test this way because deepEqual cannot include functions
- assert.isFunction(parsed.define[0].properties.myFunc);
+ assert.deepEqual(parsed, {
+ define: [
+ {
+ name: "Test",
+ properties: "({myFunc : function(){}})"
+ }
+ ],
+ "rules": [],
+ "scope": []
+ });
});
it.should("throw an error when the define block is missing a name", function () {
@@ -214,5 +221,5 @@ it.describe("nools dsl parser",function (it) {
});
});
-}).as(module);
+}).as(module).run();

0 comments on commit 82e7f86

Please sign in to comment.
Something went wrong with that request. Please try again.