Skip to content

Commit 0b0663e

Browse files
committed
Merge pull request #144 from doug-martin/master
v0.4.0
2 parents 727f8f4 + ec2f2d8 commit 0b0663e

29 files changed

+493
-3273
lines changed

browser/nools.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
} else {
2121
this.nools = nools;
2222
}
23-
}).call(window);
23+
}).call(typeof window !== "undefined" ? window : this);

docs/History.html

+15
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,21 @@
178178

179179

180180

181+
<h1>0.4.0</h1>
182+
<ul>
183+
<li>Fix for issue <a href="https://github.com/C2FO/nools/issues/122">#122</a> referencing defined class within another defined class<ul>
184+
<li>Also fixes accessing scoped functions within a defined class.</li>
185+
</ul>
186+
</li>
187+
<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>
188+
<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>
189+
<li>Now if you call <code>halt()</code> even if you did not call <code>matchUntilHalt()</code></li>
190+
</ul>
191+
</li>
192+
<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>
193+
<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>
194+
<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>
195+
</ul>
181196
<h1>0.3.0</h1>
182197
<ul>
183198
<li>Added new <code>===</code> and <code>!==</code> operators <a href="https://github.com/C2FO/nools/issues/110">#110</a></li>

docs/examples/browser/conways_2d.html

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ <h1>Conways Game Of Life 2D</h1>
3636
<script type="text/javascript" src="src/common.js"></script>
3737
<script type="text/javascript" src="src/patterns.js"></script>
3838
<script type="text/javascript" src="src/cell.js"></script>
39-
<script type="text/javascript" src="rules/conways.js"></script>
4039
<script type="text/javascript">
4140

4241
(function () {

docs/index.html

+7-7
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ <h3>Programmatically</h3>
263263
var flow = nools.flow(&quot;Hello World&quot;, function (flow) {
264264

265265
//find any message that start with hello
266-
flow.rule(&quot;Hello&quot;, [Message, &quot;m&quot;, &quot;m.text =~ /^hello(\\s*world)?$/&quot;], function (facts) {
266+
flow.rule(&quot;Hello&quot;, [Message, &quot;m&quot;, &quot;m.text =~ /^hello\\sworld$/&quot;], function (facts) {
267267
facts.m.text = facts.m.text + &quot; goodbye&quot;;
268268
this.modify(facts.m);
269269
});
@@ -277,7 +277,7 @@ <h3>Programmatically</h3>
277277
<ul>
278278
<li>Hello<ul>
279279
<li>Requires a Message</li>
280-
<li>The messages&#39;s <code>text</code> must match the regular expression <code>/^hello(\\s*world)?$/</code></li>
280+
<li>The messages&#39;s <code>text</code> must match the regular expression <code>/^hello\\sworld$/</code></li>
281281
<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>
282282
</ul>
283283
</li>
@@ -395,7 +395,8 @@ <h3><code>nools.compile</code></h3>
395395
scope: {
396396
//the logger you want your flow to use.
397397
logger: logger
398-
}
398+
},
399+
name: &#39;person name is bob&#39;
399400
});</code></pre>
400401
<p><a name="session"></a></p>
401402
<h2>Working with a session</h2>
@@ -985,7 +986,7 @@ <h3>Scope</h3>
985986

986987
rule Hello {
987988
when {
988-
m : Message matches(m.text, /^hello(\\s*world)?$/);
989+
m : Message matches(m.text, /^hello\s*world)?$/);
989990
}
990991
then {
991992
modify(m, function(){
@@ -1004,7 +1005,7 @@ <h3>Scope</h3>
10041005
<p>Or you can pass in a custom function using the scope option in compile.</p>
10051006
<pre class='prettyprint linenums lang-js'><code>rule Hello {
10061007
when {
1007-
m : Message doesMatch(m.text, /^hello(\\s*world)?$/);
1008+
m : Message doesMatch(m.text, /^hello\sworld$/);
10081009
}
10091010
then {
10101011
modify(m, function(){
@@ -1415,7 +1416,7 @@ <h3>Example 1.</h3>
14151416

14161417
rule Hello {
14171418
when {
1418-
m : Message m.text =~ /^hello(\\s*world)?$/
1419+
m : Message m.text =~ /^hello\sworld$/
14191420
}
14201421
then {
14211422
modify(m, function(){
@@ -1443,7 +1444,6 @@ <h3>Example 1.</h3>
14431444
session = flow.getSession();
14441445
//assert your different messages
14451446
session.assert(new Message(&quot;goodbye&quot;));
1446-
session.assert(new Message(&quot;hello&quot;));
14471447
session.assert(new Message(&quot;hello world&quot;));
14481448
session.match();
14491449
}

docs/nools.js

+10-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/browser/conways_2d.html

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ <h1>Conways Game Of Life 2D</h1>
3636
<script type="text/javascript" src="src/common.js"></script>
3737
<script type="text/javascript" src="src/patterns.js"></script>
3838
<script type="text/javascript" src="src/cell.js"></script>
39-
<script type="text/javascript" src="rules/conways.js"></script>
4039
<script type="text/javascript">
4140

4241
(function () {

history.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# 0.4.0
2+
3+
* Fix for issue [#122](https://github.com/C2FO/nools/issues/122) referencing defined class within another defined class
4+
* Also fixes accessing scoped functions within a defined class.
5+
* 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`.
6+
* Allow session.halt even for `match()` [#143](https://github.com/C2FO/nools/pull/143) - [@raymondfeng](https://github.com/raymondfeng)
7+
* Now if you call `halt()` even if you did not call `matchUntilHalt()`
8+
* 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)
9+
* You can now define types using scope [#142](https://github.com/C2FO/nools/pull/142) - [@raymondfeng](https://github.com/raymondfeng)
10+
* Fix for issue, is the dsl you do not have to escape `\` characters [#123](https://github.com/C2FO/nools/issues/123)
11+
12+
113
# 0.3.0
214

315
* Added new `===` and `!==` operators [#110](https://github.com/C2FO/nools/issues/110)

lib/compile/common.js

+26-8
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,44 @@ var createFunction = function (body, defined, scope, scopeNames, definedNames) {
2929

3030
var createDefined = (function () {
3131

32-
var _createDefined = function (options) {
33-
options = isString(options) ? new Function("return " + options + ";")() : options;
34-
var ret = options.hasOwnProperty("constructor") && "function" === typeof options.constructor ? options.constructor : function (opts) {
32+
var _createDefined = function (action, defined, scope) {
33+
if (isString(action)) {
34+
var declares = [];
35+
extd(defined).keys().forEach(function (i) {
36+
if (action.indexOf(i) !== -1) {
37+
declares.push("var " + i + "= defined." + i + ";");
38+
}
39+
});
40+
41+
extd(scope).keys().forEach(function (i) {
42+
if (action.indexOf(i) !== -1) {
43+
declares.push("var " + i + "= function(){var prop = scope." + i + "; return __objToStr__.call(prop) === '[object Function]' ? prop.apply(void 0, arguments) : prop;};");
44+
}
45+
});
46+
if (declares.length) {
47+
declares.unshift("var __objToStr__ = Object.prototype.toString;");
48+
}
49+
action = [declares.join(""), "return ", action, ";"].join("");
50+
action = new Function("defined", "scope", action)(defined, scope);
51+
}
52+
var ret = action.hasOwnProperty("constructor") && "function" === typeof action.constructor ? action.constructor : function (opts) {
3553
opts = opts || {};
3654
for (var i in opts) {
37-
if (i in options) {
55+
if (i in action) {
3856
this[i] = opts[i];
3957
}
4058
}
4159
};
4260
var proto = ret.prototype;
43-
for (var i in options) {
44-
proto[i] = options[i];
61+
for (var i in action) {
62+
proto[i] = action[i];
4563
}
4664
return ret;
4765

4866
};
4967

50-
return function (options) {
51-
return _createDefined(options.properties);
68+
return function (options, defined, scope) {
69+
return _createDefined(options.properties, defined, scope);
5270
};
5371
})();
5472

lib/compile/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,13 @@ exports.compile = function (flowObj, options, cb, Container) {
163163
defined.Buffer = Buffer;
164164
}
165165
var scope = merge({console: console}, options.scope);
166+
//add the anything added to the scope as a property
167+
forEach(flowObj.scope, function (s) {
168+
scope[s.name] = true;
169+
});
166170
//add any defined classes in the parsed flowObj to defined
167171
forEach(flowObj.define, function (d) {
168-
defined[d.name] = createDefined(d);
172+
defined[d.name] = createDefined(d, defined, scope);
169173
});
170174

171175
//expose any defined classes to the flow.

lib/compile/transpile.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,13 @@ function constraintsToJs(constraint, identifiers) {
110110
ret.push('"', constraint.shift(), '", ');
111111
}
112112
identifiers.push(constraint[1]);
113-
ret.push(constraint[0], ', "' + constraint[1].replace(/"/g, "\\\"") + '"');
113+
ret.push(constraint[0], ', "' + constraint[1].replace(/\\/g, "\\\\").replace(/"/g, "\\\"") + '"');
114114
constraint.splice(0, 2);
115115
if (constraint.length) {
116116
//constraint
117117
var c = constraint.shift();
118118
if (extd.isString(c) && c) {
119-
ret.push(',"' + c.replace(/"/g, "\\\""), '"');
119+
ret.push(',"' + c.replace(/\\/g, "\\\\").replace(/"/g, "\\\""), '"');
120120
forEach(constraintMatcher.getIdentifiers(parser.parseConstraint(c)), function (i) {
121121
identifiers.push(i);
122122
});

lib/constraint.js

+22-20
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,15 @@ var ReferenceConstraint = Constraint.extend({
171171
}).as(exports, "ReferenceConstraint");
172172

173173

174-
ReferenceConstraint.extend({instance: {
175-
type: "reference_equality",
176-
op: "eq",
177-
getIndexableProperties: function () {
178-
return constraintMatcher.getIndexableProperties(this.constraint);
174+
ReferenceConstraint.extend({
175+
instance: {
176+
type: "reference_equality",
177+
op: "eq",
178+
getIndexableProperties: function () {
179+
return constraintMatcher.getIndexableProperties(this.constraint);
180+
}
179181
}
180-
}}).as(exports, "ReferenceEqualityConstraint")
182+
}).as(exports, "ReferenceEqualityConstraint")
181183
.extend({instance: {type: "reference_inequality", op: "neq"}}).as(exports, "ReferenceInequalityConstraint")
182184
.extend({instance: {type: "reference_gt", op: "gt"}}).as(exports, "ReferenceGTConstraint")
183185
.extend({instance: {type: "reference_gte", op: "gte"}}).as(exports, "ReferenceGTEConstraint")
@@ -237,21 +239,21 @@ Constraint.extend({
237239
}).as(exports, "FromConstraint");
238240

239241
Constraint.extend({
240-
instance: {
241-
constructor: function(func, options) {
242-
this.type = "custom";
243-
this.fn = func;
244-
this.options = options;
245-
},
246-
247-
equal: function(constraint) {
248-
return instanceOf(constraint, this._static) && this.fn === constraint.constraint;
249-
},
250-
251-
"assert": function(fact, fh) {
252-
return this.fn(fact, fh);
242+
instance: {
243+
constructor: function (func, options) {
244+
this.type = "custom";
245+
this.fn = func;
246+
this.options = options;
247+
},
248+
249+
equal: function (constraint) {
250+
return instanceOf(constraint, this._static) && this.fn === constraint.constraint;
251+
},
252+
253+
"assert": function (fact, fh) {
254+
return this.fn(fact, fh);
255+
}
253256
}
254-
}
255257
}).as(exports, "CustomConstraint");
256258

257259

lib/constraintMatcher.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,8 @@ exports.getSourceMatcher = function (rule, options, equality) {
458458
};
459459

460460
exports.toConstraints = function (constraint, options) {
461-
if(typeof constraint === 'function') {
462-
return [new atoms.CustomConstraint(constraint, options)];
461+
if (typeof constraint === 'function') {
462+
return [new atoms.CustomConstraint(constraint, options)];
463463
}
464464
//constraint.split("&&")
465465
return lang.toConstraints(constraint, options);

lib/flow.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,7 @@ module.exports = declare(EventEmitter, {
5757
},
5858

5959
halt: function () {
60-
var strategy = this.executionStrategy;
61-
/*
62-
if (strategy.matchUntilHalt) {
63-
strategy.halt();
64-
}
65-
*/
66-
strategy.halt();
60+
this.executionStrategy.halt();
6761
return this;
6862
},
6963

0 commit comments

Comments
 (0)