Permalink
Browse files

more lexer work

  • Loading branch information...
1 parent abf67c9 commit 854c36c647d543937a2590b21926c3c5353381a0 @doug-martin committed May 16, 2012
@@ -47,16 +47,15 @@ module.exports = exports = nools.flow("Manners", function (flow) {
lgn = facts.leftGuestName,
rightSeat = s.rightSeat,
seatId = facts.sid, countValue = count.value;
- var seating = new Seating(countValue, seatId, false, rightSeat, facts.seatingRightGuestName, rightSeat + 1, lgn);
- this.assert(seating);
- var path = new Path(countValue, rightSeat + 1, lgn);
- this.assert(path);
- var chosen = new Chosen(seatId, lgn, facts.rightGuestHobby);
- this.assert(chosen);
- ++count.value;
- this.modify(count);
- context.state = Context.MAKE_PATH;
- this.modify(context);
+ this.assert(new Seating(countValue, seatId, false, rightSeat, facts.seatingRightGuestName, rightSeat + 1, lgn));
+ this.assert(new Path(countValue, rightSeat + 1, lgn));
+ this.assert(new Chosen(seatId, lgn, facts.rightGuestHobby));
+ this.modify(count, function(){
+ this.value++;
+ });
+ this.modify(context, function(){
+ this.state = Context.MAKE_PATH
+ });
});
flow.rule("makePath", [
@@ -65,19 +64,20 @@ module.exports = exports = nools.flow("Manners", function (flow) {
[Path, "p", "p.id == seatingPid", {guestName:"pathGuestName", seat:"pathSeat"}],
["not", Path, "p2", "p2.id == seatingId && p2.guestName == pathGuestName"]
], function (facts) {
- var path = new Path(facts.seatingId, facts.pathSeat, facts.pathGuestName);
- this.assert(path);
+ this.assert(new Path(facts.seatingId, facts.pathSeat, facts.pathGuestName));
});
flow.rule("pathDone", [
[Context, "c", "c.state == " + Context.MAKE_PATH],
[Seating, "s", "s.path == false"]
], function (facts) {
- var c = facts.c, s = facts.s;
- s.path = true;
- this.modify(s);
- c.state = Context.CHECK_DONE;
- this.modify(c);
+ var s = facts.s;
+ this.modify(s, function(){
+ this.path = true;
+ });
+ this.modify(facts.c, function(){
+ this.state = Context.CHECK_DONE;
+ });
console.log("path Done : %s", s);
});
@@ -87,17 +87,17 @@ module.exports = exports = nools.flow("Manners", function (flow) {
[LastSeat, "ls", "true", {seat:"lastSeat"}],
[Seating, "s", "s.rightSeat == lastSeat"]
], function (facts) {
- var c = facts.c;
- c.state = Context.PRINT_RESULTS;
- this.modify(c);
+ this.modify(facts.c, function () {
+ this.state = Context.PRINT_RESULTS;
+ });
});
flow.rule("continue", [Context, "c", "c.state == " + Context.CHECK_DONE],
function (facts) {
- var c = facts.c;
- c.state = Context.ASSIGN_SEATS;
- this.modify(c);
+ this.modify(facts.c, function () {
+ this.state = Context.ASSIGN_SEATS;
+ });
});
flow.rule("allDone", [Context, "c", "c.state == " + Context.PRINT_RESULTS], function () {
@@ -1,10 +1,9 @@
var data = require("./data"),
nools = require("../../index");
-var flow = nools.parse(__dirname + "/manners.withDefined.json");
+var flow = nools.compile(__dirname + "/manners.nools");
var guests = data.load(flow.getDefined("guest"), flow.getDefined("lastSeat")).guests16;
var session = flow.getSession.apply(flow, guests);
-session.print();
session.assert(new (flow.getDefined("context"))({state:"start"}));
session.assert(new (flow.getDefined("count"))({value:1}));
var start = new Date();
@@ -0,0 +1,145 @@
+exports.load = function (Guest, LastSeat) {
+ return {
+ guests5:[
+ new Guest({name:"n1", sex:"m", hobby:"h1"}),
+ new Guest({name:"n2", sex:"f", hobby:"h1"}),
+ new Guest({name:"n2", sex:"f", hobby:"h3"}),
+ new Guest({name:"n3", sex:"m", hobby:"h3"}),
+ new Guest({name:"n4", sex:"m", hobby:"h1"}),
+ new Guest({name:"n4", sex:"f", hobby:"h2"}),
+ new Guest({name:"n4", sex:"f", hobby:"h3"}),
+ new Guest({name:"n5", sex:"f", hobby:"h2"}),
+ new Guest({name:"n5", sex:"f", hobby:"h1"}),
+ new LastSeat({seat:5})
+ ],
+
+ guests16:[
+ new Guest({name:"n1", sex:"f", hobby:"h3"}),
+ new Guest({name:"n1", sex:"f", hobby:"h1"}),
+ new Guest({name:"n1", sex:"f", hobby:"h2"}),
+ new Guest({name:"n2", sex:"f", hobby:"h3"}),
+ new Guest({name:"n2", sex:"f", hobby:"h2"}),
+ new Guest({name:"n3", sex:"m", hobby:"h1"}),
+ new Guest({name:"n3", sex:"m", hobby:"h3"}),
+ new Guest({name:"n4", sex:"m", hobby:"h2"}),
+ new Guest({name:"n4", sex:"m", hobby:"h1"}),
+ new Guest({name:"n5", sex:"m", hobby:"h2"}),
+ new Guest({name:"n5", sex:"m", hobby:"h3"}),
+ new Guest({name:"n6", sex:"m", hobby:"h2"}),
+ new Guest({name:"n6", sex:"m", hobby:"h1"}),
+ new Guest({name:"n7", sex:"f", hobby:"h2"}),
+ new Guest({name:"n7", sex:"f", hobby:"h1"}),
+ new Guest({name:"n7", sex:"f", hobby:"h3"}),
+ new Guest({name:"n8", sex:"f", hobby:"h3"}),
+ new Guest({name:"n8", sex:"f", hobby:"h2"}),
+ new Guest({name:"n9", sex:"f", hobby:"h1"}),
+ new Guest({name:"n9", sex:"f", hobby:"h3"}),
+ new Guest({name:"n9", sex:"f", hobby:"h2"}),
+ new Guest({name:"n10", sex:"m", hobby:"h2"}),
+ new Guest({name:"n10", sex:"m", hobby:"h3"}),
+ new Guest({name:"n11", sex:"m", hobby:"h3"}),
+ new Guest({name:"n11", sex:"m", hobby:"h2"}),
+ new Guest({name:"n11", sex:"m", hobby:"h1"}),
+ new Guest({name:"n12", sex:"m", hobby:"h3"}),
+ new Guest({name:"n12", sex:"m", hobby:"h1"}),
+ new Guest({name:"n13", sex:"m", hobby:"h2"}),
+ new Guest({name:"n13", sex:"m", hobby:"h3"}),
+ new Guest({name:"n13", sex:"m", hobby:"h1"}),
+ new Guest({name:"n14", sex:"f", hobby:"h3"}),
+ new Guest({name:"n14", sex:"f", hobby:"h1"}),
+ new Guest({name:"n15", sex:"f", hobby:"h3"}),
+ new Guest({name:"n15", sex:"f", hobby:"h2"}),
+ new Guest({name:"n15", sex:"f", hobby:"h1"}),
+ new Guest({name:"n16", sex:"f", hobby:"h3"}),
+ new Guest({name:"n16", sex:"f", hobby:"h2"}),
+ new Guest({name:"n16", sex:"f", hobby:"h1"}),
+ new LastSeat({seat:16})],
+
+ guests32:[
+ new Guest({name:"n1", sex:"m", hobby:"h1"}),
+ new Guest({name:"n1", sex:"m", hobby:"h3"}),
+ new Guest({name:"n2", sex:"f", hobby:"h3"}),
+ new Guest({name:"n2", sex:"f", hobby:"h2"}),
+ new Guest({name:"n2", sex:"f", hobby:"h1"}),
+ new Guest({name:"n3", sex:"f", hobby:"h1"}),
+ new Guest({name:"n3", sex:"f", hobby:"h2"}),
+ new Guest({name:"n4", sex:"f", hobby:"h3"}),
+ new Guest({name:"n4", sex:"f", hobby:"h1"}),
+ new Guest({name:"n5", sex:"f", hobby:"h1"}),
+ new Guest({name:"n5", sex:"f", hobby:"h2"}),
+ new Guest({name:"n6", sex:"m", hobby:"h1"}),
+ new Guest({name:"n6", sex:"m", hobby:"h2"}),
+ new Guest({name:"n6", sex:"m", hobby:"h3"}),
+ new Guest({name:"n7", sex:"f", hobby:"h2"}),
+ new Guest({name:"n7", sex:"f", hobby:"h1"}),
+ new Guest({name:"n7", sex:"f", hobby:"h3"}),
+ new Guest({name:"n8", sex:"f", hobby:"h1"}),
+ new Guest({name:"n8", sex:"f", hobby:"h3"}),
+ new Guest({name:"n8", sex:"f", hobby:"h2"}),
+ new Guest({name:"n9", sex:"f", hobby:"h1"}),
+ new Guest({name:"n9", sex:"f", hobby:"h3"}),
+ new Guest({name:"n9", sex:"f", hobby:"h2"}),
+ new Guest({name:"n10", sex:"m", hobby:"h2"}),
+ new Guest({name:"n10", sex:"m", hobby:"h1"}),
+ new Guest({name:"n11", sex:"m", hobby:"h2"}),
+ new Guest({name:"n11", sex:"m", hobby:"h1"}),
+ new Guest({name:"n12", sex:"m", hobby:"h3"}),
+ new Guest({name:"n12", sex:"m", hobby:"h2"}),
+ new Guest({name:"n13", sex:"m", hobby:"h1"}),
+ new Guest({name:"n13", sex:"m", hobby:"h3"}),
+ new Guest({name:"n14", sex:"m", hobby:"h3"}),
+ new Guest({name:"n14", sex:"m", hobby:"h2"}),
+ new Guest({name:"n15", sex:"f", hobby:"h2"}),
+ new Guest({name:"n15", sex:"f", hobby:"h1"}),
+ new Guest({name:"n15", sex:"f", hobby:"h3"}),
+ new Guest({name:"n16", sex:"f", hobby:"h3"}),
+ new Guest({name:"n16", sex:"f", hobby:"h2"}),
+ new Guest({name:"n16", sex:"f", hobby:"h1"}),
+ new Guest({name:"n17", sex:"m", hobby:"h3"}),
+ new Guest({name:"n17", sex:"m", hobby:"h2"}),
+ new Guest({name:"n18", sex:"f", hobby:"h2"}),
+ new Guest({name:"n18", sex:"f", hobby:"h1"}),
+ new Guest({name:"n19", sex:"f", hobby:"h1"}),
+ new Guest({name:"n19", sex:"f", hobby:"h2"}),
+ new Guest({name:"n19", sex:"f", hobby:"h3"}),
+ new Guest({name:"n20", sex:"f", hobby:"h1"}),
+ new Guest({name:"n20", sex:"f", hobby:"h2"}),
+ new Guest({name:"n20", sex:"f", hobby:"h3"}),
+ new Guest({name:"n21", sex:"m", hobby:"h2"}),
+ new Guest({name:"n21", sex:"m", hobby:"h3"}),
+ new Guest({name:"n21", sex:"m", hobby:"h1"}),
+ new Guest({name:"n22", sex:"f", hobby:"h1"}),
+ new Guest({name:"n22", sex:"f", hobby:"h2"}),
+ new Guest({name:"n22", sex:"f", hobby:"h3"}),
+ new Guest({name:"n23", sex:"f", hobby:"h3"}),
+ new Guest({name:"n23", sex:"f", hobby:"h1"}),
+ new Guest({name:"n23", sex:"f", hobby:"h2"}),
+ new Guest({name:"n24", sex:"m", hobby:"h1"}),
+ new Guest({name:"n24", sex:"m", hobby:"h3"}),
+ new Guest({name:"n25", sex:"f", hobby:"h3"}),
+ new Guest({name:"n25", sex:"f", hobby:"h2"}),
+ new Guest({name:"n25", sex:"f", hobby:"h1"}),
+ new Guest({name:"n26", sex:"f", hobby:"h3"}),
+ new Guest({name:"n26", sex:"f", hobby:"h2"}),
+ new Guest({name:"n26", sex:"f", hobby:"h1"}),
+ new Guest({name:"n27", sex:"m", hobby:"h3"}),
+ new Guest({name:"n27", sex:"m", hobby:"h1"}),
+ new Guest({name:"n27", sex:"m", hobby:"h2"}),
+ new Guest({name:"n28", sex:"m", hobby:"h3"}),
+ new Guest({name:"n28", sex:"m", hobby:"h1"}),
+ new Guest({name:"n29", sex:"m", hobby:"h3"}),
+ new Guest({name:"n29", sex:"m", hobby:"h2"}),
+ new Guest({name:"n29", sex:"m", hobby:"h1"}),
+ new Guest({name:"n30", sex:"m", hobby:"h2"}),
+ new Guest({name:"n30", sex:"m", hobby:"h1"}),
+ new Guest({name:"n30", sex:"m", hobby:"h3"}),
+ new Guest({name:"n31", sex:"m", hobby:"h2"}),
+ new Guest({name:"n31", sex:"m", hobby:"h1"}),
+ new Guest({name:"n32", sex:"m", hobby:"h1"}),
+ new Guest({name:"n32", sex:"m", hobby:"h3"}),
+ new Guest({name:"n32", sex:"m", hobby:"h2"}),
+ new LastSeat({seat:32})]
+ }
+};
+
+
@@ -23,7 +23,8 @@ define Count {
define Guest {
name : "",
- sex : "hobby",
+ sex : "",
+ hobby : "",
toString:function () {
return ["[Guest name=", this.name, ", sex=", this.sex, ", hobbies=", this.hobby, "]"].join("");
}
@@ -66,21 +67,23 @@ rule assignFirstSeat {
count : Count;
}
then {
- assert(new Seating({
- id : $count.value,
- pid : 0,
- path : true,
- leftSeat : 1,
- leftGuestName : $leftGuestName,
- rightSeat : 1,
- rightGuestName : $leftGuestName
- }));
- assert(new Path({id : $count.value, seat : 1, guestName : $leftGuestName}));
- modify($count, function(){
+ var seating = new Seating({
+ id : count.value,
+ pid : 0,
+ path : true,
+ leftSeat : 1,
+ leftGuestName : leftGuestName,
+ rightSeat : 1,
+ rightGuestName : leftGuestName
+ });
+ assert(seating);
+ var path = new Path({id : count.value, seat : 1, guestName : leftGuestName});
+ assert(path);
+ modify(count, function(){
this.value++
});
console.log('assign first seat : %s : %s', seating, path);
- modify($c, function(){
+ modify(c, function(){
this.state = 'assign';
});
}
@@ -99,30 +102,31 @@ rule findSeating {
}
then {
assert(new Seating({
- id : $count.value,
- pid : $sid,
- path : true,
- leftSeat : $s.rightSeat,
- leftGuestName : $seatingRightGuestName,
- rightSeat : $rightSeat + 1,
- rightGuestName : $leftGuestName
+ id : countValue,
+ pid : sid,
+ path : false,
+ leftSeat : s.rightSeat,
+ leftGuestName : seatingRightGuestName,
+ rightSeat : rightSeat + 1,
+ rightGuestName : leftGuestName
}));
- assert(new Path({id : countValue, seat : rightSeat + 1, guestName : leftGuestName}));
- assert(new Chosen({id : $sid, guestName : $leftGuestName, hobby : $rightGuestHobby}));
- modify($count, function(){this.value++;});
- modify($c, function(){this.state = 'make'});
+ var path = new Path({id : countValue, seat : rightSeat + 1, guestName : leftGuestName});
+ assert(path);
+ assert(new Chosen({id : sid, guestName : leftGuestName, hobby : rightGuestHobby}));
+ modify(count, function(){this.value++;});
+ modify(c, function(){this.state = 'make'});
}
}
rule makePath {
when {
c : Context c.state == 'make';
s : Seating s.path == false {id : sid, pid : seatingPid};
- p : Path p, p.id == seatingPid {guestName : pathGuestName, seat : pathSeat};
- not( p2 : Path p2.id == sid && p2.guestName == pathGuestName];
+ p : Path p.id == seatingPid {guestName : pathGuestName, seat : pathSeat};
+ not( p2 : Path p2.id == sid && p2.guestName == pathGuestName);
}
then {
- assert(new Path({id : $sid, seat : $pathSeat, guestName : $pathGuestName}));
+ assert(new Path({id : sid, seat : pathSeat, guestName : pathGuestName}));
}
}
@@ -132,13 +136,13 @@ rule pathDone {
s : Seating s.path == false;
}
then {
- modify($s, function(){
+ modify(s, function(){
this.path = true;
});
- modify($c, function(){
+ modify(c, function(){
this.state = 'check';
});
- console.log('path Done : %s', $s);
+ console.log('path Done : %s', s);
}
}
@@ -149,7 +153,7 @@ rule areWeDone {
s : Seating s.rightSeat == lastSeat;
}
then {
- modify($c, {this.state = 'print';})
+ modify(c, function(){this.state = 'print';})
}
}
@@ -158,7 +162,7 @@ rule continue {
c : Context c.state == 'check';
}
then {
- modify($c, {this.state = 'assign'});
+ modify(c, function(){this.state = 'assign'});
}
}
Oops, something went wrong.

0 comments on commit 854c36c

Please sign in to comment.