-
Notifications
You must be signed in to change notification settings - Fork 181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implements the extends keyword for types defined in the dsl. #165
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,10 @@ var createFunction = function (body, defined, scope, scopeNames, definedNames) { | |
|
||
var createDefined = (function () { | ||
|
||
var _createDefined = function (action, defined, scope) { | ||
var _createDefined = function (options, defined, scope) { | ||
var action = options.properties | ||
,proto, base; | ||
// | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. empty comment |
||
if (isString(action)) { | ||
var declares = []; | ||
extd(defined).keys().forEach(function (i) { | ||
|
@@ -57,7 +60,14 @@ var createDefined = (function () { | |
} | ||
} | ||
}; | ||
var proto = ret.prototype; | ||
if (options.extend) { | ||
base = defined[options.extend]; | ||
proto = Object.create(base.prototype); | ||
ret.prototype = proto; | ||
} | ||
else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
proto = ret.prototype; | ||
} | ||
for (var i in action) { | ||
proto[i] = action[i]; | ||
} | ||
|
@@ -66,7 +76,7 @@ var createDefined = (function () { | |
}; | ||
|
||
return function (options, defined, scope) { | ||
return _createDefined(options.properties, defined, scope); | ||
return _createDefined(options, defined, scope); | ||
}; | ||
})(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,26 +7,48 @@ var extd = require("../extended"), | |
constraintMatcher = require("../constraintMatcher"), | ||
parser = require("../parser"); | ||
|
||
function definedToJs(options) { | ||
function definedToJs(options, extend) { | ||
/*jshint evil:true*/ | ||
options = isString(options) ? new Function("return " + options + ";")() : options; | ||
var ret = ["(function(){"], value; | ||
|
||
var ret = ["(function(flow){"], value; | ||
if (options.hasOwnProperty("constructor") && "function" === typeof options.constructor) { | ||
ret.push("var Defined = " + options.constructor.toString() + ";"); | ||
} else { | ||
ret.push("var Defined = function(opts){ for(var i in opts){if(opts.hasOwnProperty(i)){this[i] = opts[i];}}};"); | ||
} | ||
ret.push("var proto = Defined.prototype;"); | ||
if (extend) { | ||
ret.push("var proto = Object.create(flow.getDefined('" + extend + "').prototype); proto.constructor = Defined; Defined.prototype = proto;"); | ||
} | ||
else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
ret.push("var proto = Defined.prototype;"); | ||
} | ||
|
||
for (var key in options) { | ||
if (options.hasOwnProperty(key)) { | ||
var str; | ||
if (options.hasOwnProperty(key) && key !== 'constructor') { | ||
value = options[key]; | ||
ret.push("proto." + key + " = " + (extd.isFunction(value) ? value.toString() : extd.format("%j", value)) + ";"); | ||
str = ("proto." + key + " = "); | ||
if (extd.isFunction(value)) { | ||
str += value.toString(); | ||
} | ||
else { | ||
if (value instanceof Array) { | ||
str += JSON.stringify(value); | ||
} | ||
else { | ||
str += extd.format("%j", value); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should change this to use |
||
str += ";" | ||
} | ||
ret.push(str); | ||
} | ||
} | ||
ret.push("return Defined;"); | ||
ret.push("}())"); | ||
return ret.join(""); | ||
ret.push("}(this))"); | ||
var sJoin = ret.join(""); | ||
debugger; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this debugger |
||
return sJoin; | ||
|
||
} | ||
|
||
|
@@ -148,7 +170,7 @@ exports.transpile = function (flowObj, options) { | |
ret.push(extd(flowObj.define || []).map(function (defined) { | ||
var name = defined.name; | ||
defined[name] = {}; | ||
return ["var", name, "= defined." + name, "= this.addDefined('" + name + "',", definedToJs(defined.properties) + ");"].join(" "); | ||
return ["var", name, "= defined." + name, "= this.addDefined('" + name + "',", definedToJs(defined.properties, defined.extend) + ");"].join(" "); | ||
}).value().join("\n")); | ||
ret.push(extd(flowObj.scope || []).map(function (s) { | ||
var name = s.name; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,6 +145,19 @@ To use the flow | |
var flow = nools.compile(__dirname + "/helloworld.nools"), | ||
Message = flow.getDefined("message"); | ||
``` | ||
# Type Declaration 'extends' | ||
Defined types in the DSL no support 'extends' keyword for inheritance. | ||
|
||
Any type present in the flow can be extended. Base types must be defined before extended types. | ||
|
||
``` | ||
define EncodedMessage extends Message { | ||
encoding: 'sha1' | ||
,constructor: function(message, encoding) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Formatting |
||
Message.call(this, message); // call superclass manually | ||
this.encoding = encoding | ||
} | ||
} | ||
|
||
### Flow Events | ||
|
||
|
@@ -341,7 +354,6 @@ session.getFacts(Number); //[1, 2]; | |
session.getFacts(String); //["A", "B"]; | ||
``` | ||
|
||
|
||
<a name="firing"></a> | ||
## Firing the rules | ||
|
||
|
@@ -1004,7 +1016,6 @@ flow1 | |
``` | ||
|
||
|
||
|
||
<a name="rule-scope"></a> | ||
### Scope | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -593,4 +593,26 @@ it.describe("Flow dsl", function (it) { | |
}); | ||
}); | ||
}); | ||
it.describe("extended Fact types", function (it) { | ||
var Person, Student, LongTermStudent, Count, session1Fired = {}, session1; | ||
var flow = nools.compile(resolve(__dirname, "./rules/extends.nools"), { | ||
name: 'extendsTest' | ||
}); | ||
Person = flow.getDefined('Person'); | ||
Student = flow.getDefined('Student'); | ||
LongTermStudent = flow.getDefined('LongTermStudent'); | ||
// | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. empty comments |
||
session1 = flow.getSession(new Person('Bob'), new Student('Sue', 'harvard'), new LongTermStudent('Jim', 'princeton', 1)).on('fire', function (name) { | ||
session1Fired[name] = session1Fired[name] || { cnt: 0 }; | ||
session1Fired[name].cnt++; | ||
}); | ||
// | ||
it.should(" fire rule(s) correctly according to type. each rule matching a single type ", function (next) { | ||
return session1.match().then(function () { | ||
assert(session1Fired['PersonTest'].cnt === 3); | ||
assert(session1Fired['StudentTest'].cnt === 2); | ||
assert(session1Fired['LongTermStudentTest'].cnt === 1); | ||
}) | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,8 @@ it.describe("nools dsl parser", function (it) { | |
define: [ | ||
{ | ||
name: "Test", | ||
properties: "({myProp : 'value'})" | ||
extend: null, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. formatting |
||
properties: "({myProp : 'value'})" | ||
} | ||
], | ||
"rules": [], | ||
|
@@ -28,6 +29,7 @@ it.describe("nools dsl parser", function (it) { | |
define: [ | ||
{ | ||
name: "Test", | ||
extend: null, | ||
properties: "({myFunc : function(){}})" | ||
} | ||
], | ||
|
@@ -183,6 +185,7 @@ it.describe("nools dsl parser", function (it) { | |
"define": [ | ||
{ | ||
"name": "Count", | ||
extend: null, | ||
"properties": "({ constructor: function(){ this.called = 0; } })" | ||
} | ||
], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
| ||
define Person { | ||
constructor: function(name){ | ||
this.name = name; | ||
} | ||
} | ||
|
||
define Student extends Person { | ||
constructor: function(name, school) { | ||
Person.call(this, name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spacing throughout this file |
||
this.school = school; | ||
} | ||
} | ||
|
||
define LongTermStudent extends Student { | ||
constructor: function(name, school, years) { | ||
Student.call(this, name, school); | ||
this.years = years; | ||
} | ||
} | ||
|
||
|
||
rule PersonTest { | ||
when { | ||
p: Person; | ||
} | ||
then { | ||
} | ||
} | ||
|
||
rule StudentTest { | ||
when { | ||
s: Student; | ||
} | ||
then { | ||
} | ||
} | ||
|
||
|
||
rule LongTermStudentTest { | ||
when { | ||
s: LongTermStudent; | ||
} | ||
then { | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comma on previous line