Skip to content

Commit

Permalink
Merge pull request #144 from doug-martin/master
Browse files Browse the repository at this point in the history
v0.4.0
  • Loading branch information
doug-martin committed Feb 26, 2015
2 parents 727f8f4 + ec2f2d8 commit 0b0663e
Show file tree
Hide file tree
Showing 29 changed files with 493 additions and 3,273 deletions.
2 changes: 1 addition & 1 deletion browser/nools.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
} else {
this.nools = nools;
}
}).call(window);
}).call(typeof window !== "undefined" ? window : this);
15 changes: 15 additions & 0 deletions docs/History.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,21 @@



<h1>0.4.0</h1>
<ul>
<li>Fix for issue <a href="https://github.com/C2FO/nools/issues/122">#122</a> referencing defined class within another defined class<ul>
<li>Also fixes accessing scoped functions within a defined class.</li>
</ul>
</li>
<li>Fix for issue <a href="https://github.com/C2FO/nools/issues/119">#119</a> window was removed from the nools.js file now it is called in the current scope of <code>this</code>.</li>
<li>Allow session.halt even for <code>match()</code> <a href="https://github.com/C2FO/nools/pull/143">#143</a> - <a href="https://github.com/raymondfeng">@raymondfeng</a><ul>
<li>Now if you call <code>halt()</code> even if you did not call <code>matchUntilHalt()</code></li>
</ul>
</li>
<li>Now you can use a function as a constraint (Only applies to rules defined programatically) <a href="https://github.com/C2FO/nools/pull/142">#142</a> - <a href="https://github.com/raymondfeng">@raymondfeng</a></li>
<li>You can now define types using scope <a href="https://github.com/C2FO/nools/pull/142">#142</a> - <a href="https://github.com/raymondfeng">@raymondfeng</a></li>
<li>Fix for issue, is the dsl you do not have to escape <code>\</code> characters <a href="https://github.com/C2FO/nools/issues/123">#123</a></li>
</ul>
<h1>0.3.0</h1>
<ul>
<li>Added new <code>===</code> and <code>!==</code> operators <a href="https://github.com/C2FO/nools/issues/110">#110</a></li>
Expand Down
1 change: 0 additions & 1 deletion docs/examples/browser/conways_2d.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ <h1>Conways Game Of Life 2D</h1>
<script type="text/javascript" src="src/common.js"></script>
<script type="text/javascript" src="src/patterns.js"></script>
<script type="text/javascript" src="src/cell.js"></script>
<script type="text/javascript" src="rules/conways.js"></script>
<script type="text/javascript">

(function () {
Expand Down
14 changes: 7 additions & 7 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ <h3>Programmatically</h3>
var flow = nools.flow(&quot;Hello World&quot;, function (flow) {

//find any message that start with hello
flow.rule(&quot;Hello&quot;, [Message, &quot;m&quot;, &quot;m.text =~ /^hello(\\s*world)?$/&quot;], function (facts) {
flow.rule(&quot;Hello&quot;, [Message, &quot;m&quot;, &quot;m.text =~ /^hello\\sworld$/&quot;], function (facts) {
facts.m.text = facts.m.text + &quot; goodbye&quot;;
this.modify(facts.m);
});
Expand All @@ -277,7 +277,7 @@ <h3>Programmatically</h3>
<ul>
<li>Hello<ul>
<li>Requires a Message</li>
<li>The messages&#39;s <code>text</code> must match the regular expression <code>/^hello(\\s*world)?$/</code></li>
<li>The messages&#39;s <code>text</code> must match the regular expression <code>/^hello\\sworld$/</code></li>
<li>When matched the message&#39;s <code>text</code> is modified and then we let the engine know that we modified the message.</li>
</ul>
</li>
Expand Down Expand Up @@ -395,7 +395,8 @@ <h3><code>nools.compile</code></h3>
scope: {
//the logger you want your flow to use.
logger: logger
}
},
name: &#39;person name is bob&#39;
});</code></pre>
<p><a name="session"></a></p>
<h2>Working with a session</h2>
Expand Down Expand Up @@ -985,7 +986,7 @@ <h3>Scope</h3>

rule Hello {
when {
m : Message matches(m.text, /^hello(\\s*world)?$/);
m : Message matches(m.text, /^hello\s*world)?$/);
}
then {
modify(m, function(){
Expand All @@ -1004,7 +1005,7 @@ <h3>Scope</h3>
<p>Or you can pass in a custom function using the scope option in compile.</p>
<pre class='prettyprint linenums lang-js'><code>rule Hello {
when {
m : Message doesMatch(m.text, /^hello(\\s*world)?$/);
m : Message doesMatch(m.text, /^hello\sworld$/);
}
then {
modify(m, function(){
Expand Down Expand Up @@ -1415,7 +1416,7 @@ <h3>Example 1.</h3>

rule Hello {
when {
m : Message m.text =~ /^hello(\\s*world)?$/
m : Message m.text =~ /^hello\sworld$/
}
then {
modify(m, function(){
Expand Down Expand Up @@ -1443,7 +1444,6 @@ <h3>Example 1.</h3>
session = flow.getSession();
//assert your different messages
session.assert(new Message(&quot;goodbye&quot;));
session.assert(new Message(&quot;hello&quot;));
session.assert(new Message(&quot;hello world&quot;));
session.match();
}
Expand Down
20 changes: 10 additions & 10 deletions docs/nools.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion examples/browser/conways_2d.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ <h1>Conways Game Of Life 2D</h1>
<script type="text/javascript" src="src/common.js"></script>
<script type="text/javascript" src="src/patterns.js"></script>
<script type="text/javascript" src="src/cell.js"></script>
<script type="text/javascript" src="rules/conways.js"></script>
<script type="text/javascript">

(function () {
Expand Down
12 changes: 12 additions & 0 deletions history.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# 0.4.0

* Fix for issue [#122](https://github.com/C2FO/nools/issues/122) referencing defined class within another defined class
* Also fixes accessing scoped functions within a defined class.
* Fix for issue [#119](https://github.com/C2FO/nools/issues/119) window was removed from the nools.js file now it is called in the current scope of `this`.
* Allow session.halt even for `match()` [#143](https://github.com/C2FO/nools/pull/143) - [@raymondfeng](https://github.com/raymondfeng)
* Now if you call `halt()` even if you did not call `matchUntilHalt()`
* Now you can use a function as a constraint (Only applies to rules defined programatically) [#142](https://github.com/C2FO/nools/pull/142) - [@raymondfeng](https://github.com/raymondfeng)
* You can now define types using scope [#142](https://github.com/C2FO/nools/pull/142) - [@raymondfeng](https://github.com/raymondfeng)
* Fix for issue, is the dsl you do not have to escape `\` characters [#123](https://github.com/C2FO/nools/issues/123)


# 0.3.0

* Added new `===` and `!==` operators [#110](https://github.com/C2FO/nools/issues/110)
Expand Down
34 changes: 26 additions & 8 deletions lib/compile/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,44 @@ var createFunction = function (body, defined, scope, scopeNames, definedNames) {

var createDefined = (function () {

var _createDefined = function (options) {
options = isString(options) ? new Function("return " + options + ";")() : options;
var ret = options.hasOwnProperty("constructor") && "function" === typeof options.constructor ? options.constructor : function (opts) {
var _createDefined = function (action, defined, scope) {
if (isString(action)) {
var declares = [];
extd(defined).keys().forEach(function (i) {
if (action.indexOf(i) !== -1) {
declares.push("var " + i + "= defined." + i + ";");
}
});

extd(scope).keys().forEach(function (i) {
if (action.indexOf(i) !== -1) {
declares.push("var " + i + "= function(){var prop = scope." + i + "; return __objToStr__.call(prop) === '[object Function]' ? prop.apply(void 0, arguments) : prop;};");
}
});
if (declares.length) {
declares.unshift("var __objToStr__ = Object.prototype.toString;");
}
action = [declares.join(""), "return ", action, ";"].join("");
action = new Function("defined", "scope", action)(defined, scope);
}
var ret = action.hasOwnProperty("constructor") && "function" === typeof action.constructor ? action.constructor : function (opts) {
opts = opts || {};
for (var i in opts) {
if (i in options) {
if (i in action) {
this[i] = opts[i];
}
}
};
var proto = ret.prototype;
for (var i in options) {
proto[i] = options[i];
for (var i in action) {
proto[i] = action[i];
}
return ret;

};

return function (options) {
return _createDefined(options.properties);
return function (options, defined, scope) {
return _createDefined(options.properties, defined, scope);
};
})();

Expand Down
6 changes: 5 additions & 1 deletion lib/compile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,13 @@ exports.compile = function (flowObj, options, cb, Container) {
defined.Buffer = Buffer;
}
var scope = merge({console: console}, options.scope);
//add the anything added to the scope as a property
forEach(flowObj.scope, function (s) {
scope[s.name] = true;
});
//add any defined classes in the parsed flowObj to defined
forEach(flowObj.define, function (d) {
defined[d.name] = createDefined(d);
defined[d.name] = createDefined(d, defined, scope);
});

//expose any defined classes to the flow.
Expand Down
4 changes: 2 additions & 2 deletions lib/compile/transpile.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ function constraintsToJs(constraint, identifiers) {
ret.push('"', constraint.shift(), '", ');
}
identifiers.push(constraint[1]);
ret.push(constraint[0], ', "' + constraint[1].replace(/"/g, "\\\"") + '"');
ret.push(constraint[0], ', "' + constraint[1].replace(/\\/g, "\\\\").replace(/"/g, "\\\"") + '"');
constraint.splice(0, 2);
if (constraint.length) {
//constraint
var c = constraint.shift();
if (extd.isString(c) && c) {
ret.push(',"' + c.replace(/"/g, "\\\""), '"');
ret.push(',"' + c.replace(/\\/g, "\\\\").replace(/"/g, "\\\""), '"');
forEach(constraintMatcher.getIdentifiers(parser.parseConstraint(c)), function (i) {
identifiers.push(i);
});
Expand Down
42 changes: 22 additions & 20 deletions lib/constraint.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,15 @@ var ReferenceConstraint = Constraint.extend({
}).as(exports, "ReferenceConstraint");


ReferenceConstraint.extend({instance: {
type: "reference_equality",
op: "eq",
getIndexableProperties: function () {
return constraintMatcher.getIndexableProperties(this.constraint);
ReferenceConstraint.extend({
instance: {
type: "reference_equality",
op: "eq",
getIndexableProperties: function () {
return constraintMatcher.getIndexableProperties(this.constraint);
}
}
}}).as(exports, "ReferenceEqualityConstraint")
}).as(exports, "ReferenceEqualityConstraint")
.extend({instance: {type: "reference_inequality", op: "neq"}}).as(exports, "ReferenceInequalityConstraint")
.extend({instance: {type: "reference_gt", op: "gt"}}).as(exports, "ReferenceGTConstraint")
.extend({instance: {type: "reference_gte", op: "gte"}}).as(exports, "ReferenceGTEConstraint")
Expand Down Expand Up @@ -237,21 +239,21 @@ Constraint.extend({
}).as(exports, "FromConstraint");

Constraint.extend({
instance: {
constructor: function(func, options) {
this.type = "custom";
this.fn = func;
this.options = options;
},

equal: function(constraint) {
return instanceOf(constraint, this._static) && this.fn === constraint.constraint;
},

"assert": function(fact, fh) {
return this.fn(fact, fh);
instance: {
constructor: function (func, options) {
this.type = "custom";
this.fn = func;
this.options = options;
},

equal: function (constraint) {
return instanceOf(constraint, this._static) && this.fn === constraint.constraint;
},

"assert": function (fact, fh) {
return this.fn(fact, fh);
}
}
}
}).as(exports, "CustomConstraint");


4 changes: 2 additions & 2 deletions lib/constraintMatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,8 @@ exports.getSourceMatcher = function (rule, options, equality) {
};

exports.toConstraints = function (constraint, options) {
if(typeof constraint === 'function') {
return [new atoms.CustomConstraint(constraint, options)];
if (typeof constraint === 'function') {
return [new atoms.CustomConstraint(constraint, options)];
}
//constraint.split("&&")
return lang.toConstraints(constraint, options);
Expand Down
8 changes: 1 addition & 7 deletions lib/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,7 @@ module.exports = declare(EventEmitter, {
},

halt: function () {
var strategy = this.executionStrategy;
/*
if (strategy.matchUntilHalt) {
strategy.halt();
}
*/
strategy.halt();
this.executionStrategy.halt();
return this;
},

Expand Down
Loading

0 comments on commit 0b0663e

Please sign in to comment.