Permalink
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...
doug-martin committed Jan 15, 2013
1 parent 5b1ba71 commit 82e7f865debaf018231d10a301ec1324f3a41dab
View
@@ -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);
+
+
+
+
+
@@ -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
@@ -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
@@ -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
@@ -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];
});
Oops, something went wrong.

0 comments on commit 82e7f86

Please sign in to comment.