Permalink
Browse files

Created a single unified API for the browser, nodejs and Rhino, as we…

…ll as generic frontend that should load environment-specific frontends as needed. Also, used modified version of stitch to make sure that only environment-specific js bundles are built.
  • Loading branch information...
1 parent 65c20eb commit a453ca20e71ca656b844777cf27a78b01076b26c @jbeard4 committed May 31, 2012
View
@@ -1,6 +1,6 @@
-[submodule "lib/rhino/util/jsonml"]
- path = lib/rhino/util/jsonml
- url = git://github.com/jbeard4/jsonml.git
[submodule "test/scxml-test-framework"]
path = test/scxml-test-framework
url = git://github.com/jbeard4/scxml-test-framework.git
+[submodule "lib/external/jsonml"]
+ path = lib/external/jsonml
+ url = git://github.com/jbeard4/jsonml.git
View
@@ -1,4 +1,4 @@
-js = lib/core/scion.js \
+js = lib/scion.js \
lib/core/util/annotate-scxml-json.js \
lib/core/scxml/SCXML.js \
lib/core/scxml/default-transition-selector.js \
@@ -10,7 +10,7 @@ js = lib/core/scion.js \
lib/core/scxml/set/ArraySet.js
scion.js : $(js)
- node lib/build/stitch.js
+ node lib/browser/build/stitch.js
scion-min.js : scion.js
uglifyjs scion.js > scion-min.js
View
@@ -0,0 +1,53 @@
+//TODO: default send implementation
+//TODO: default script
+//TODO: anything else platform dependent in the interpreter
+
+//construct can take path, url, doc, or model
+
+var scxml = require('../core/scxml/SCXML'),
+ JsonML = require('../external/jsonml/jsonml-dom'),
+ annotator = require('../core/util/annotate-scxml-json'),
+ json2model = require('../core/scxml/json2model');
+
+function urlToModel(url,cb){
+ window.jQuery.get(url,function(doc){
+ cb(null,documentToModel(doc));
+ },"xml");
+}
+
+function documentStringToModel(s){
+ var dp = new window.DOMParser();
+ var doc = dp.parseFromString(s);
+
+ return documentToModel(doc);
+}
+
+function documentToModel(doc){
+ var arr = JsonML.parseDOM(doc);
+ var scxmlJson = arr[1];
+
+ var annotatedScxmlJson = annotator.transform(scxmlJson);
+
+ var model = json2model(annotatedScxmlJson);
+
+ return model;
+}
+
+//setup environment
+scxml.SimpleInterpreter.prototype._setTimeout = function(callback, timeout) {
+ return window.setTimeout(callback, timeout);
+};
+
+scxml.SimpleInterpreter.prototype._clearTimeout = function(timeoutId) {
+ return window.clearTimeout(timeoutId);
+};
+
+scxml.SimpleInterpreter.prototype._log = window.console.log || function(){};
+
+module.exports = {
+ pathToModel : urlToModel, //alias pathToModule to urlToModel for browser
+ urlToModel : urlToModel,
+ documentStringToModel : documentStringToModel,
+ documentToModel : documentToModel,
+ SCXML : scxml.SimpleInterpreter
+};
@@ -0,0 +1,14 @@
+var stitch = require('stitch');
+var fs = require('fs');
+
+var pkg = stitch.createPackage({
+ paths: ['lib/'],
+ excludes : ['lib/node','lib/rhino','lib/browser/build']
+});
+
+pkg.compile(function (err, source){
+ fs.writeFile('scion.js', source, function (err) {
+ if (err) throw err;
+ console.log('Compiled scion.js');
+ });
+});
View
@@ -1,6 +0,0 @@
-//this module provides a single point of access to all important user-facing modules in scion
-module.exports = {
- annotator : require('./util/annotate-scxml-json'),
- json2model : require('./scxml/json2model'),
- scxml : require('./scxml/SCXML')
-};
View
@@ -40,7 +40,7 @@ function SCXMLInterpreter(model, opts){
this.opts = opts;
//this.opts.printTrace = true;
- this.opts.log = this.opts.log || console.log; //rely on global console if this console is undefined
+ this.opts.log = this.opts.log || this._log; //rely on global console if this console is undefined
this.opts.StateIdSet = this.opts.StateIdSet || ArraySet;
this.opts.EventSet = this.opts.EventSet || ArraySet;
this.opts.TransitionPairSet = this.opts.TransitionPairSet || ArraySet;
@@ -61,7 +61,7 @@ SCXMLInterpreter.prototype = {
start : function() {
//perform big step without events to take all default transitions and reach stable initial state
- if (this.opts.printTrace) console.log("performing initial big step");
+ if (this.opts.printTrace) this._log("performing initial big step");
this._configuration.add(this.model.root.initial);
//eval top-level scripts
@@ -114,15 +114,15 @@ SCXMLInterpreter.prototype = {
_performSmallStep : function(eventSet, datamodelForNextStep) {
- if (this.opts.printTrace) console.log("selecting transitions with eventSet: ", eventSet);
+ if (this.opts.printTrace) this._log("selecting transitions with eventSet: ", eventSet);
var selectedTransitions = this._selectTransitions(eventSet, datamodelForNextStep);
- if (this.opts.printTrace) console.log("selected transitions: ", selectedTransitions);
+ if (this.opts.printTrace) this._log("selected transitions: ", selectedTransitions);
if (!selectedTransitions.isEmpty()) {
- if (this.opts.printTrace) console.log("sorted transitions: ", selectedTransitions);
+ if (this.opts.printTrace) this._log("sorted transitions: ", selectedTransitions);
//we only want to enter and exit states from transitions with targets
//filter out targetless transitions here - we will only use these to execute transition actions
@@ -136,19 +136,19 @@ SCXMLInterpreter.prototype = {
basicStatesEntered = enteredTuple[0],
statesEntered = enteredTuple[1];
- if (this.opts.printTrace) console.log("basicStatesExited ", basicStatesExited);
- if (this.opts.printTrace) console.log("basicStatesEntered ", basicStatesEntered);
- if (this.opts.printTrace) console.log("statesExited ", statesExited);
- if (this.opts.printTrace) console.log("statesEntered ", statesEntered);
+ if (this.opts.printTrace) this._log("basicStatesExited ", basicStatesExited);
+ if (this.opts.printTrace) this._log("basicStatesEntered ", basicStatesEntered);
+ if (this.opts.printTrace) this._log("statesExited ", statesExited);
+ if (this.opts.printTrace) this._log("statesEntered ", statesEntered);
var eventsToAddToInnerQueue = new this.opts.EventSet();
//update history states
- if (this.opts.printTrace) console.log("executing state exit actions");
+ if (this.opts.printTrace) this._log("executing state exit actions");
statesExited.forEach(function(state){
- if (this.opts.printTrace) console.log("exiting ", state);
+ if (this.opts.printTrace) this._log("exiting ", state);
//invoke listeners
this._listeners.forEach(function(l){
@@ -182,7 +182,7 @@ SCXMLInterpreter.prototype = {
return t1.documentOrder - t2.documentOrder;
});
- if (this.opts.printTrace) console.log("executing transitition actions");
+ if (this.opts.printTrace) this._log("executing transitition actions");
sortedTransitions.forEach(function(transition){
@@ -196,7 +196,7 @@ SCXMLInterpreter.prototype = {
},this);
},this);
- if (this.opts.printTrace) console.log("executing state enter actions");
+ if (this.opts.printTrace) this._log("executing state enter actions");
statesEntered.forEach(function(state){
@@ -209,33 +209,33 @@ SCXMLInterpreter.prototype = {
},this);
},this);
- if (this.opts.printTrace) console.log("updating configuration ");
- if (this.opts.printTrace) console.log("old configuration ", this._configuration);
+ if (this.opts.printTrace) this._log("updating configuration ");
+ if (this.opts.printTrace) this._log("old configuration ", this._configuration);
//update configuration by removing basic states exited, and adding basic states entered
this._configuration.difference(basicStatesExited);
this._configuration.union(basicStatesEntered);
- if (this.opts.printTrace) console.log("new configuration ", this._configuration);
+ if (this.opts.printTrace) this._log("new configuration ", this._configuration);
//add set of generated events to the innerEventQueue -> Event Lifelines: Next small-step
if (!eventsToAddToInnerQueue.isEmpty()) {
- if (this.opts.printTrace) console.log("adding triggered events to inner queue ", eventsToAddToInnerQueue);
+ if (this.opts.printTrace) this._log("adding triggered events to inner queue ", eventsToAddToInnerQueue);
this._innerEventQueue.push(eventsToAddToInnerQueue);
}
- if (this.opts.printTrace) console.log("updating datamodel for next small step :");
+ if (this.opts.printTrace) this._log("updating datamodel for next small step :");
//update the datamodel
for (var key in datamodelForNextStep) {
- if (this.opts.printTrace) console.log("key ", key);
+ if (this.opts.printTrace) this._log("key ", key);
if (key in this._datamodel) {
- if (this.opts.printTrace) console.log("old value ", this._datamodel[key]);
+ if (this.opts.printTrace) this._log("old value ", this._datamodel[key]);
} else {
- if (this.opts.printTrace) console.log("old value is null");
+ if (this.opts.printTrace) this._log("old value is null");
}
- if (this.opts.printTrace) console.log("new value ", datamodelForNextStep[key]);
+ if (this.opts.printTrace) this._log("new value ", datamodelForNextStep[key]);
this._datamodel[key] = datamodelForNextStep[key];
}
}
@@ -274,7 +274,7 @@ SCXMLInterpreter.prototype = {
switch (action.type) {
case "raise":
- if (this.opts.printTrace) console.log("sending event", action.event, "with content", action.contentexpr);
+ if (this.opts.printTrace) this._log("sending event", action.event, "with content", action.contentexpr);
eventsToAddToInnerQueue.add({ name: action.event });
break;
@@ -471,7 +471,7 @@ SCXMLInterpreter.prototype = {
var priorityEnabledTransitions = this._selectPriorityEnabledTransitions(enabledTransitions);
- if (this.opts.printTrace) console.log("priorityEnabledTransitions", priorityEnabledTransitions);
+ if (this.opts.printTrace) this._log("priorityEnabledTransitions", priorityEnabledTransitions);
return priorityEnabledTransitions;
},
@@ -485,10 +485,10 @@ SCXMLInterpreter.prototype = {
priorityEnabledTransitions.union(consistentTransitions);
- if (this.opts.printTrace) console.log("enabledTransitions", enabledTransitions);
- if (this.opts.printTrace) console.log("consistentTransitions", consistentTransitions);
- if (this.opts.printTrace) console.log("inconsistentTransitionsPairs", inconsistentTransitionsPairs);
- if (this.opts.printTrace) console.log("priorityEnabledTransitions", priorityEnabledTransitions);
+ if (this.opts.printTrace) this._log("enabledTransitions", enabledTransitions);
+ if (this.opts.printTrace) this._log("consistentTransitions", consistentTransitions);
+ if (this.opts.printTrace) this._log("inconsistentTransitionsPairs", inconsistentTransitionsPairs);
+ if (this.opts.printTrace) this._log("priorityEnabledTransitions", priorityEnabledTransitions);
while (!inconsistentTransitionsPairs.isEmpty()) {
enabledTransitions = new this.opts.TransitionSet(
@@ -500,10 +500,10 @@ SCXMLInterpreter.prototype = {
priorityEnabledTransitions.union(consistentTransitions);
- if (this.opts.printTrace) console.log("enabledTransitions", enabledTransitions);
- if (this.opts.printTrace) console.log("consistentTransitions", consistentTransitions);
- if (this.opts.printTrace) console.log("inconsistentTransitionsPairs", inconsistentTransitionsPairs);
- if (this.opts.printTrace) console.log("priorityEnabledTransitions", priorityEnabledTransitions);
+ if (this.opts.printTrace) this._log("enabledTransitions", enabledTransitions);
+ if (this.opts.printTrace) this._log("consistentTransitions", consistentTransitions);
+ if (this.opts.printTrace) this._log("inconsistentTransitionsPairs", inconsistentTransitionsPairs);
+ if (this.opts.printTrace) this._log("priorityEnabledTransitions", priorityEnabledTransitions);
}
return priorityEnabledTransitions;
@@ -514,7 +514,7 @@ SCXMLInterpreter.prototype = {
var inconsistentTransitionsPairs = new this.opts.TransitionPairSet();
var transitionList = transitions.iter();
- if (this.opts.printTrace) console.log("transitions", transitionList);
+ if (this.opts.printTrace) this._log("transitions", transitionList);
for(var i = 0; i < transitionList.length; i++){
for(var j = i+1; j < transitionList.length; j++){
@@ -542,8 +542,8 @@ SCXMLInterpreter.prototype = {
var isOrthogonal = this.opts.model.isOrthogonalTo(t1LCA, t2LCA);
if (this.opts.printTrace) {
- console.log("transition LCAs", t1LCA.id, t2LCA.id);
- console.log("transition LCAs are orthogonal?", isOrthogonal);
+ this._log("transition LCAs", t1LCA.id, t2LCA.id);
+ this._log("transition LCAs are orthogonal?", isOrthogonal);
}
return isOrthogonal;
@@ -620,64 +620,34 @@ SimpleInterpreter.prototype.gen = function(evtObjOrName,optionalData) {
SimpleInterpreter.prototype._send = function(event, options) {
var callback, timeoutId,
_this = this;
- if (this.opts.setTimeout) {
+ if (this._setTimeout) {
if (this.opts.printTrace) {
- console.log("sending event", event.name, "with content", event.data, "after delay", options.delay);
+ this._log("sending event", event.name, "with content", event.data, "after delay", options.delay);
}
callback = function() {
return _this.gen(event);
};
- timeoutId = this.opts.setTimeout(callback, options.delay);
+ timeoutId = this._setTimeout(callback, options.delay);
if (options.sendid) return this._timeoutMap[options.sendid] = timeoutId;
} else {
throw new Error("setTimeout function not set");
}
};
SimpleInterpreter.prototype._cancel = function(sendid){
- if (this.opts.clearTimeout) {
+ if (this._clearTimeout) {
if (sendid in this._timeoutMap) {
if (this.opts.printTrace) {
- console.log("cancelling ", sendid, " with timeout id ", this._timeoutMap[sendid]);
+ this._log("cancelling ", sendid, " with timeout id ", this._timeoutMap[sendid]);
}
- return this.opts.clearTimeout(this._timeoutMap[sendid]);
+ return this._clearTimeout(this._timeoutMap[sendid]);
}
} else {
throw new Error("clearTimeout function not set");
}
};
-function BrowserInterpreter(model, opts) {
- opts = opts || {};
- setupDefaultOpts(opts);
- if (!opts.setTimeout) {
- opts.setTimeout = function(callback, timeout) {
- return window.setTimeout(callback, timeout);
- };
- }
- if (!opts.clearTimeout) {
- opts.clearTimeout = function(timeoutId) {
- return window.clearTimeout(timeoutId);
- };
- }
-
- SimpleInterpreter.call(this,model,opts); //call super constructor
-}
-BrowserInterpreter.prototype = Object.create(SimpleInterpreter.prototype);
-
-function NodeInterpreter(model, opts) {
- opts = opts || {};
- setupDefaultOpts(opts);
- opts.setTimeout = setTimeout;
- opts.clearTimeout = clearTimeout;
-
- SimpleInterpreter.call(this,model,opts); //call super constructor
-}
-NodeInterpreter.prototype = Object.create(SimpleInterpreter.prototype);
-
module.exports = {
SCXMLInterpreter: SCXMLInterpreter,
- SimpleInterpreter: SimpleInterpreter,
- BrowserInterpreter: BrowserInterpreter,
- NodeInterpreter: NodeInterpreter
+ SimpleInterpreter: SimpleInterpreter
};
File renamed without changes.
Oops, something went wrong.

0 comments on commit a453ca2

Please sign in to comment.