Skip to content

Commit 3ca6e72

Browse files
committed
Merge pull request #95 from doug-martin/index-work
v0.2.3
2 parents 4b515ac + 8dd364c commit 3ca6e72

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+2274
-3028
lines changed

Gruntfile.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/*global module:false*/
22
module.exports = function (grunt) {
33
// Project configuration.
4+
var path = require("path"),
5+
child = require("child_process");
46
grunt.initConfig({
57
pkg: grunt.file.readJSON('package.json'),
68
jshint: {
@@ -48,9 +50,37 @@ module.exports = function (grunt) {
4850
});
4951

5052
// Default task.
51-
grunt.registerTask('default', ['jshint', 'it', 'browserify:nools', 'uglify:min']);
53+
grunt.registerTask('default', ['jshint', "compile-tests", 'it', 'browserify:nools', 'uglify:min']);
5254
grunt.loadNpmTasks('grunt-it');
5355
grunt.loadNpmTasks('grunt-contrib-jshint');
5456
grunt.loadNpmTasks('grunt-contrib-uglify');
5557
grunt.loadNpmTasks('grunt-browserify');
58+
59+
grunt.registerTask("compile-tests", "compiles all lest files", function () {
60+
var files = grunt.file.expand("./test/rules/*.nools"), count = files.length, done = this.async();
61+
62+
function counter(err) {
63+
if (err) {
64+
done(err);
65+
} else {
66+
count--;
67+
if (!count) {
68+
done();
69+
}
70+
}
71+
}
72+
73+
files.forEach(function (file) {
74+
var base = path.basename(file, ".nools"),
75+
out = path.resolve(path.dirname(file), base + "-compiled.js");
76+
child.exec(path.resolve(__dirname, "./bin/nools") + " compile " + file + " -l ../../ -n " + base + "-compiled", function (err, output) {
77+
if (!err) {
78+
grunt.file.write(out, output.toString());
79+
}
80+
counter(err);
81+
});
82+
});
83+
84+
85+
});
5686
};

benchmark/manners/benchmark.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
nools = require("../../index");
55

66
var flow = nools.compile(__dirname + "/manners.nools");
7-
var guests = data.load(flow).manners32;
7+
var guests = data.load(flow).manners64;
88
var session = flow.getSession.apply(flow, guests);
99
session.assert(new (flow.getDefined("count"))({value: 1}));
1010
var start = new Date();

bin/nools

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var nools = require(".."),
55
path = require("path"),
66
fs = require("fs"),
77
path = require("path"),
8+
stdout = process.stdout,
89
template = fs.readFileSync(path.join(__dirname, "assets", "compile_wrapper.tmpl"), "utf8"),
910
str = require("string-extended"),
1011
uglifyjs = require("uglify-js"),
@@ -42,7 +43,7 @@ program.command("compile")
4243
var noolsLocation = program["nools_location"] || "nools";
4344
files.forEach(function (file) {
4445
var name = program.name || path.basename(file, path.extname(file));
45-
console.log(uglify(str.format(template, { source: nools.transpile(path.resolve(process.cwd(), file), {name: name}), noolsLocation: noolsLocation}), program));
46+
stdout.write(uglify(str.format(template, { source: nools.transpile(path.resolve(process.cwd(), file), {name: name}), noolsLocation: noolsLocation}), program));
4647
});
4748
});
4849

docs/History.html

+10
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,16 @@
178178

179179

180180

181+
<h1>0.2.3</h1>
182+
<ul>
183+
<li>Added new <code>getFacts</code> method to allow for querying of facts currently in session. <a href="https://github.com/C2FO/nools/issues/52">#52</a>;</li>
184+
<li>Added indexing on comparison operators (i.e. <code>&gt;, &lt;, &gt;=, &lt;=</code>).</li>
185+
<li>Updated documentation.<ul>
186+
<li>Added new section about retrieving facts from a session.</li>
187+
<li>Created new section for async actions to address <a href="https://github.com/C2FO/nools/issues/94">#94</a></li>
188+
</ul>
189+
</li>
190+
</ul>
181191
<h1>0.2.2</h1>
182192
<ul>
183193
<li>Performance Upgrades<ul>

docs/examples/browser/rules/diagnosis.nools

+12-12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ define Treatment {
2222
}
2323

2424

25+
rule Cold {
26+
when {
27+
p : Patient p.fever in ['mild', 'high'] {name : n};
28+
not(d : Diagnosis d.name == n && d.diagnosis == 'flu');
29+
}
30+
then {
31+
var d = new Diagnosis({name : n, diagnosis : "cold"});
32+
emit("diagnosis", d);
33+
assert(d);
34+
}
35+
}
36+
2537
rule Measels {
2638
when {
2739
p : Patient p.fever == 'high' && p.spots == true && p.innoculated == true {name : n};
@@ -68,18 +80,6 @@ rule Flu {
6880
}
6981
}
7082

71-
rule Cold {
72-
when {
73-
p : Patient p.fever in ['mild', 'high'] {name : n};
74-
not(d : Diagnosis d.name == n && d.diagnosis == 'flu');
75-
}
76-
then {
77-
var d = new Diagnosis({name : n, diagnosis : "cold"});
78-
emit("diagnosis", d);
79-
assert(d);
80-
}
81-
}
82-
8383
rule Healthy {
8484
when {
8585
p: Patient {name : n};

docs/examples/browser/src/conway_3d.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
container.css({width: innerWidth + "px", height: innerHeight + "px"});
2222

2323
var camera = this.camera = new THREE.PerspectiveCamera(100, innerWidth / innerHeight, 1, 10000);
24-
camera.position.set(0.01,(dimension || this.defaultDimensions) * 25, 0.01);
24+
camera.position.set(0.01, (dimension || this.defaultDimensions) * 25, 0.01);
2525

2626

2727
var controls = this.controls = new THREE.OrbitControls(camera, container[0]);
@@ -77,7 +77,6 @@
7777
__disposeAndReset: function () {
7878
this.session.dispose();
7979
if (!this.stopped) {
80-
console.profileEnd();
8180
this.reset();
8281
this.__matchPromise = null;
8382
return this.run();
@@ -89,7 +88,6 @@
8988
var session;
9089
if (this.stopped && !this.__matchPromise) {
9190
this.stopped = false;
92-
console.profile();
9391
this.__matchPromise = this.statsListener.listen(this.session = this.flow
9492
.getSession(this.cells))
9593
.focus("populate")

docs/examples/browser/waltzDb.html

-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ <h1 class="inline">WaltzDB Benchmark</h1>
7171

7272
function run() {
7373
stop();
74-
console.profile();
7574
var data = loadData(flow, dataset);
7675
session = statsListener.listen(flow.getSession());
7776
for (var i = 0, l = data.length; i < l; i++) {
@@ -84,7 +83,6 @@ <h1 class="inline">WaltzDB Benchmark</h1>
8483
resultsContainer.scrollTop(resultsContainer.prop("scrollHeight"));
8584
})
8685
.match().then(function () {
87-
console.profileEnd();
8886
console.log("done");
8987
}, function (e) {
9088
console.log(e.stack);

docs/index.html

+43-12
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ <h1>Usage</h1>
195195
<li><a href="#facts-assert">Assert</a></li>
196196
<li><a href="#facts-retract">Retract</a></li>
197197
<li><a href="#facts-modify">Modify</a></li>
198+
<li><a href="#get-facts">Retrieving Facts</a></li>
198199
</ul>
199200
</li>
200201
<li><a href="#firing">Firing</a> </li>
@@ -221,7 +222,10 @@ <h1>Usage</h1>
221222
<li><a href="#exists-constraint">Exists</a></li>
222223
</ul>
223224
</li>
224-
<li><a href="#action">Actions</a></li>
225+
<li><a href="#action">Actions</a><ul>
226+
<li><a href="#action-async">Async Actions</a></li>
227+
</ul>
228+
</li>
225229
<li><a href="#globals">Globals</a></li>
226230
<li><a href="#import">Import</a></li>
227231
</ul>
@@ -434,6 +438,23 @@ <h3>Modify</h3>
434438

435439
session.modify(m);</code></pre>
436440
<p><strong>Note</strong> <code>modify</code> is typically used during the execution of the rules.</p>
441+
<p><a name="get-facts"></a></p>
442+
<h3>Retrieving Facts</h3>
443+
<p>To get a list of facts currently in the session you can use the <code>getFacts()</code> method exposed on a session.</p>
444+
<pre class='prettyprint linenums lang-js'><code class="lang-javascript">session.assert(1);
445+
session.assert(&quot;A&quot;);
446+
session.assert(&quot;B&quot;);
447+
session.assert(2);
448+
449+
session.getFacts(); //[1, &quot;A&quot;, &quot;B&quot;, 2];</code></pre>
450+
<p>You may also pass in a <code>Type</code> to <code>getFacts</code> which will return facts only of the given type.</p>
451+
<pre class='prettyprint linenums lang-js'><code class="lang-javascript">session.assert(1);
452+
session.assert(&quot;A&quot;);
453+
session.assert(&quot;B&quot;);
454+
session.assert(2);
455+
456+
session.getFacts(Number); //[1, 2];
457+
session.getFacts(String); //[&quot;A&quot;, &quot;B&quot;];</code></pre>
437458
<p><a name="firing"></a></p>
438459
<h2>Firing the rules</h2>
439460
<p>When you get a session from a <strong>flow</strong> no rules will be fired until the <strong>match</strong> method is called.</p>
@@ -1250,8 +1271,17 @@ <h3>Action</h3>
12501271
session.modify(f3);
12511272
session.retract(f1);
12521273
}</code></pre>
1253-
<p>If you have an async action that needs to take place an optional third argument can be passed in which is a function
1254-
to be called when the action is completed.</p>
1274+
<p>To define the actions with the nools DSL</p>
1275+
<pre class='prettyprint linenums lang-js'><code>then {
1276+
modify(f3, function(){
1277+
this.value = f1.value + f2.value;
1278+
});
1279+
retract(f1);
1280+
}</code></pre>
1281+
<p>For rules defined using the rules language nools will automatically determine what parameters need to be passed in based on what is referenced in the action.</p>
1282+
<p><a name="action-async"></a></p>
1283+
<h3>Async Actions</h3>
1284+
<p>If your action is async you can use the third argument which should called when the action is completed.</p>
12551285
<pre class='prettyprint linenums lang-js'><code class="lang-javascript">function (facts, engine, next) {
12561286
//some async action
12571287
process.nextTick(function(){
@@ -1261,17 +1291,18 @@ <h3>Action</h3>
12611291
engine.modify(f3);
12621292
engine.retract(f1);
12631293
next();
1264-
})
1294+
});
12651295
}</code></pre>
1266-
<p>If any arguments are passed into next it is assumed there was an error and the session will error out.</p>
1267-
<p>To define the action with the nools DSL</p>
1268-
<pre class='prettyprint linenums lang-js'><code>then {
1269-
modify(f3, function(){
1270-
this.value = f1.value + f2.value;
1271-
});
1272-
retract(f1);
1296+
<p>If an error occurs you can pass the error as the first argument to <code>next</code>.</p>
1297+
<pre class='prettyprint linenums lang-js'><code class="lang-javascript">then{
1298+
saveToDatabase(user, function(err){
1299+
next(new Error(&quot;Something went BOOM!&quot;));
1300+
});
1301+
}</code></pre>
1302+
<p>If you are using a <a href="http://promises-aplus.github.io/promises-spec/"><code>Promises/A+</code></a> compliant library you can just return a promise from your action and <code>nools</code> will wait for the promise to resolve before continuing.</p>
1303+
<pre class='prettyprint linenums lang-js'><code class="lang-javascript">then{
1304+
return saveToDatabase(user); // assume saveToDatabase returns a promise
12731305
}</code></pre>
1274-
<p>For rules defined using the rules language nools will automatically determine what parameters need to be passed in based on what is referenced in the action.</p>
12751306
<p><a name="globals"></a></p>
12761307
<h3>Globals</h3>
12771308
<p>Globals are accessible through the current working scope of rules defined in a <code>dsl</code>, very similar to using the <code>scope</code> option when compiling.</p>

docs/nools.js

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

examples/browser/rules/diagnosis.nools

+12-12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ define Treatment {
2222
}
2323

2424

25+
rule Cold {
26+
when {
27+
p : Patient p.fever in ['mild', 'high'] {name : n};
28+
not(d : Diagnosis d.name == n && d.diagnosis == 'flu');
29+
}
30+
then {
31+
var d = new Diagnosis({name : n, diagnosis : "cold"});
32+
emit("diagnosis", d);
33+
assert(d);
34+
}
35+
}
36+
2537
rule Measels {
2638
when {
2739
p : Patient p.fever == 'high' && p.spots == true && p.innoculated == true {name : n};
@@ -68,18 +80,6 @@ rule Flu {
6880
}
6981
}
7082

71-
rule Cold {
72-
when {
73-
p : Patient p.fever in ['mild', 'high'] {name : n};
74-
not(d : Diagnosis d.name == n && d.diagnosis == 'flu');
75-
}
76-
then {
77-
var d = new Diagnosis({name : n, diagnosis : "cold"});
78-
emit("diagnosis", d);
79-
assert(d);
80-
}
81-
}
82-
8383
rule Healthy {
8484
when {
8585
p: Patient {name : n};

examples/browser/src/conway_3d.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
container.css({width: innerWidth + "px", height: innerHeight + "px"});
2222

2323
var camera = this.camera = new THREE.PerspectiveCamera(100, innerWidth / innerHeight, 1, 10000);
24-
camera.position.set(0.01,(dimension || this.defaultDimensions) * 25, 0.01);
24+
camera.position.set(0.01, (dimension || this.defaultDimensions) * 25, 0.01);
2525

2626

2727
var controls = this.controls = new THREE.OrbitControls(camera, container[0]);
@@ -77,7 +77,6 @@
7777
__disposeAndReset: function () {
7878
this.session.dispose();
7979
if (!this.stopped) {
80-
console.profileEnd();
8180
this.reset();
8281
this.__matchPromise = null;
8382
return this.run();
@@ -89,7 +88,6 @@
8988
var session;
9089
if (this.stopped && !this.__matchPromise) {
9190
this.stopped = false;
92-
console.profile();
9391
this.__matchPromise = this.statsListener.listen(this.session = this.flow
9492
.getSession(this.cells))
9593
.focus("populate")

examples/browser/waltzDb.html

-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ <h1 class="inline">WaltzDB Benchmark</h1>
7171

7272
function run() {
7373
stop();
74-
console.profile();
7574
var data = loadData(flow, dataset);
7675
session = statsListener.listen(flow.getSession());
7776
for (var i = 0, l = data.length; i < l; i++) {
@@ -84,7 +83,6 @@ <h1 class="inline">WaltzDB Benchmark</h1>
8483
resultsContainer.scrollTop(resultsContainer.prop("scrollHeight"));
8584
})
8685
.match().then(function () {
87-
console.profileEnd();
8886
console.log("done");
8987
}, function (e) {
9088
console.log(e.stack);

examples/counter/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ var flow = nools.compile(require.resolve("./counter.nools"));
55
flow.getSession().matchUntilHalt().then(function () {
66
//halt finally invoked
77
console.log("done");
8-
}, function () {
8+
}, function (err) {
99
console.log(err);
1010
});

examples/sudoku/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ var hard4 = [
9292

9393
var sud = new sudoku.Sudoku(flow);
9494
var repl = require("repl");
95-
sud.setCellValues(hard4).then(function () {
95+
sud.setCellValues(hard3).then(function () {
9696
var sudokuRepl = repl.start("sudoku>>");
9797
sudokuRepl.context.print = sud.dumpGrid.bind(sud);
9898
sudokuRepl.context.step = function () {

history.md

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
#0.2.3
2+
3+
* Added new `getFacts` method to allow for querying of facts currently in session. [#52](https://github.com/C2FO/nools/issues/52);
4+
* Added indexing on comparison operators (i.e. `>, <, >=, <=`).
5+
* Updated documentation.
6+
* Added new section about retrieving facts from a session.
7+
* Created new section for async actions to address [#94](https://github.com/C2FO/nools/issues/94)
8+
9+
110
#0.2.2
211

312
* Performance Upgrades

lib/agenda.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var extd = require("./extended"),
33
declare = extd.declare,
44
AVLTree = extd.AVLTree,
55
LinkedList = extd.LinkedList,
6+
isPromise = extd.isPromiseLike,
67
EventEmitter = require("events").EventEmitter;
78

89

@@ -110,7 +111,7 @@ module.exports = declare(EventEmitter, {
110111
var activation = this.pop();
111112
this.emit("fire", activation.rule.name, activation.match.factHash);
112113
var fired = activation.rule.fire(this.flow, activation.match);
113-
if (extd.isPromiseLike(fired)) {
114+
if (isPromise(fired)) {
114115
ret = fired.then(function () {
115116
//return true if an activation fired
116117
return true;

lib/compile/common.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var extd = require("../extended"),
44
forEach = extd.forEach,
55
isString = extd.isString;
66

7-
exports.modifiers = ["assert", "modify", "retract", "emit", "halt", "focus"];
7+
exports.modifiers = ["assert", "modify", "retract", "emit", "halt", "focus", "getFacts"];
88

99
var createFunction = function (body, defined, scope, scopeNames, definedNames) {
1010
var declares = [];

0 commit comments

Comments
 (0)