Skip to content

Commit

Permalink
Enable session.halt()
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Feng committed Feb 23, 2015
1 parent d606e67 commit a510303
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
16 changes: 15 additions & 1 deletion lib/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module.exports = declare(EventEmitter, {
this.agenda.on("fire", bind(this, "emit", "fire"));
this.agenda.on("focused", bind(this, "emit", "focused"));
this.rootNode = new nodes.RootNode(this.workingMemory, this.agenda);
extd.bindAll(this, "halt", "assert", "retract", "modify", "focus", "emit", "getFacts");
extd.bindAll(this, "halt", "assert", "retract", "modify", "focus",
"emit", "getFacts", "getFact");
},

getFacts: function (Type) {
Expand All @@ -40,16 +41,29 @@ module.exports = declare(EventEmitter, {
return ret;
},

getFact: function (Type) {
var ret;
if (Type) {
ret = this.workingMemory.getFactsByType(Type);
} else {
ret = this.workingMemory.getFacts();
}
return ret && ret[0];
},

focus: function (focused) {
this.agenda.setFocus(focused);
return this;
},

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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"author": "Doug Martin (http://c2fo.com)",
"main": "index.js",
"scripts": {
"test": "it -r dot",
"test": "it -r spec",
"create-doc": "rm -rf docs/* && coddoc -d ./lib -f multi-html"
},
"directories": {
Expand Down
2 changes: 2 additions & 0 deletions test/flow.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
require('./flow/index');
1 change: 1 addition & 0 deletions test/flow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require("./events.test");
require("./exists.test");
require("./from.test");
require("./matchUntil.halt");
require("./match.halt");
require("./not.test");
require("./or.test");
require("./rule.test");
Expand Down
36 changes: 36 additions & 0 deletions test/flow/match.halt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"use strict";
var it = require("it"),
nools = require("../../"),
assert = require("assert");

it.describe("#matchHalt", function (it) {

function Count(c) {
this.count = c;
}

var session, flow = nools.flow("Match with halt Flow", function (flow) {

flow.rule("Stop", [Count, "c", "c.count == 6"], function () {
this.halt();
});

flow.rule("Inc", [Count, "c"], function (facts) {
facts.c.count++;
this.modify(facts.c);
});

});

it.beforeEach(function () {
session = flow.getSession(new Count(0));
});

it.should("stop match with halt", function () {
return session.match().then(function (err) {
assert.isUndefinedOrNull(err);
assert.equal(session.getFacts(Count)[0].count, 6);
});

});
});

0 comments on commit a510303

Please sign in to comment.