You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by jb...@apache.org on 2010/06/12 21:38:44 UTC
svn commit: r954089 - in /commons/sandbox/gsoc/2010/scxml-js/trunk: run.sh
runner.js src/scxml/cgf/SCXMLCompiler.js test/testScript.sh
Author: jbeard
Date: Sat Jun 12 19:38:44 2010
New Revision: 954089
URL: http://svn.apache.org/viewvc?rev=954089&view=rev
Log:
Intermediate commit. Currently hooking up js frontend.
Modified:
commons/sandbox/gsoc/2010/scxml-js/trunk/run.sh
commons/sandbox/gsoc/2010/scxml-js/trunk/runner.js
commons/sandbox/gsoc/2010/scxml-js/trunk/src/scxml/cgf/SCXMLCompiler.js
commons/sandbox/gsoc/2010/scxml-js/trunk/test/testScript.sh
Modified: commons/sandbox/gsoc/2010/scxml-js/trunk/run.sh
URL: http://svn.apache.org/viewvc/commons/sandbox/gsoc/2010/scxml-js/trunk/run.sh?rev=954089&r1=954088&r2=954089&view=diff
==============================================================================
--- commons/sandbox/gsoc/2010/scxml-js/trunk/run.sh (original)
+++ commons/sandbox/gsoc/2010/scxml-js/trunk/run.sh Sat Jun 12 19:38:44 2010
@@ -6,4 +6,4 @@
dn=`dirname $0`
abspath=`cd $dn; pwd`
-java -cp ./lib/java/js.jar:./lib/java/commons-cli.jar org.mozilla.javascript.tools.shell.Main -debug runner.js $abspath src/scxml/cgf/main $*
+java -cp ./lib/java/js.jar:./lib/java/commons-cli.jar:./lib/java/serializer.jar:./lib/java/xalan.jar:./lib/java/xercesImpl.jar:./lib/java/xml-apis.jar:./lib/java/commons-io-1.4.jar org.mozilla.javascript.tools.shell.Main -debug runner.js $abspath src/scxml/cgf/main $*
Modified: commons/sandbox/gsoc/2010/scxml-js/trunk/runner.js
URL: http://svn.apache.org/viewvc/commons/sandbox/gsoc/2010/scxml-js/trunk/runner.js?rev=954089&r1=954088&r2=954089&view=diff
==============================================================================
--- commons/sandbox/gsoc/2010/scxml-js/trunk/runner.js (original)
+++ commons/sandbox/gsoc/2010/scxml-js/trunk/runner.js Sat Jun 12 19:38:44 2010
@@ -77,6 +77,7 @@ if(arguments.length){
load(pathToRequireJsDir + "require.js");
load(pathToRequireJsDir + "require/rhino.js");
+ load(pathToRequireJsDir + "require/text.js");
//bootstrap require.js
require({
Modified: commons/sandbox/gsoc/2010/scxml-js/trunk/src/scxml/cgf/SCXMLCompiler.js
URL: http://svn.apache.org/viewvc/commons/sandbox/gsoc/2010/scxml-js/trunk/src/scxml/cgf/SCXMLCompiler.js?rev=954089&r1=954088&r2=954089&view=diff
==============================================================================
--- commons/sandbox/gsoc/2010/scxml-js/trunk/src/scxml/cgf/SCXMLCompiler.js (original)
+++ commons/sandbox/gsoc/2010/scxml-js/trunk/src/scxml/cgf/SCXMLCompiler.js Sat Jun 12 19:38:44 2010
@@ -26,109 +26,22 @@ code.
*/
require.def("src/scxml/cgf/SCXMLCompiler",
- ["src/scxml/cgf/util",
- "src/scxml/cgf/js/SwitchyardStatechartGenerator",
- "src/scxml/cgf/js/StateTableStatechartGenerator",
- "src/scxml/cgf/js/StatePatternStatechartGenerator",
- "src/scxml/cgf/util/file",
- "lib/js/beautify.js"],
+ ["src/scxml/cgf/xsltIRFilters",
+ "src/scxml/cgf/util/file",
+ "lib/js/beautify.js"],
- function(util,
- SwitchyardStatechartGenerator,
- StateTableStatechartGenerator,
- StatePatternStatechartGenerator,
- fileUtil){
-
- //set the default xml namespace
- msdlNs = "http://msdl.cs.mcgill.ca/";
- msdl = new Namespace(msdlNs);
+ function(xsltIRFilters,fileUtil){
- //default xml namespace = "http://www.w3.org/2005/07/scxml"
+ //import packages
+ importPackage(javax.xml.transform);
+ importPackage(javax.xml.transform.stream);
+ importPackage(javax.xml.transform.sax);
+ importPackage(Packages.org.apache.xml.serializer);
+ importPackage(Packages.org.xml.sax);
+ importPackage(Packages.org.xml.sax.helpers);
- function parseSCXML(scxmlString){
- return new XML(scxmlString.replace(/^<\?xml\s+version\s*=\s*(["'])[^\1]+\1[^?]*\?>/, "").replace(/^\s+|\s+$/g,"")); // bug 336551
- }
-
- function makeLegalLValue(name){
- return name.toString().replace(/[\.:]/g,"_");
- }
-
- function getOuterNodes(node){
- /*
- Returns a list representing the containment hierarchy of
- node. node is always the first element, and its outermost
- parent is the last.
- */
- var toReturn = [node];
- var currentNode = node;
- while(currentNode = currentNode.parent()){
- toReturn.push(currentNode);
- }
-
- return toReturn;
- }
-
-
- function getTransitionPath(allStates,startnode,endnode){
- /*
- Computes the states that must be exited and entered if the
- system is to make a transition from 'startnode' to 'endnode'.
- These ordered lists are returned in a tuple, (exitedNodes,
- enteredNodes).
- */
-
- //first get the ancestors of each node
- var startAncestors=getOuterNodes(startnode);
- var endAncestors=getOuterNodes(endnode);
-
- //now find the scope of the transition (lowest common proper ancestor)
- var LCA=null;
- while(!(startAncestors[startAncestors.length-1] === startnode ||
- endAncestors[endAncestors.length-1] === endnode ||
- startAncestors[startAncestors.length-1] !=
- endAncestors[endAncestors.length-1])){
- LCA=startAncestors.pop()
- endAncestors.pop()
- }
-
- /*
- the states to be exited are all proper descendants of the scope
- in which currentState resides the states to be entered are all
- the proper descendants of the scope in which the final basic
- state resides
- */
- var enterpath, exitpath;
- if(LCA){
- // last element (LCA) is not included in slice
- var currentnode;
- exitpath = [startnode];
- currentnode = startnode;
- while((currentnode = currentnode.parent())!=LCA){
- exitpath.push(currentnode);
- }
-
- enterpath = [endnode];
- currentnode = endnode;
-
- while((currentnode = currentnode.parent())!=LCA){
- enterpath.push(currentnode);
- }
-
- enterpath.reverse();
- }
-
- return [enterpath,exitpath,LCA];
- }
-
- //private and not changable by subclasses
- function computeDepth(state){
- var depthToReturn = 0;
- var currentNode = state;
- while(currentNode = currentNode.parent()){
- depthToReturn++;
- }
- return depthToReturn;
- }
+ //importPackage(Packages.org.apache.commons.io);
+ importClass(java.io.ByteArrayOutputStream);
/*
@@ -145,269 +58,48 @@ require.def("src/scxml/cgf/SCXMLCompiler
function compile(options){
if(!options.inFiles) return false;
- var toReturn = options.inFiles.map(function(xmlFile){
+ //do transforms
+ var tFactory = TransformerFactory.newInstance();
- if(options.ie){
- options.noMap = options.noForEach = options.noIndexOf = true;
- }
+ var filterList = xsltIRFilters.map(function(xsltDoc){
+ return tFactory.newXMLFilter(new DOMSource(xsltDoc));
+ });
- var scxmlRoot = parseSCXML(readFile(xmlFile));
+ var reader = XMLReaderFactory.createXMLReader();
- //do a bit of verification and normalization transformations
+ var prevFilter = null;
+ filterList.forEach(function(filter){
+ filter.setParent(prevFilter || reader);
+ prevFilter = filter;
+ })
+
+ var lastFilter = prevFilter;
+
+ var xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml");
+ xmlProps.setProperty("indent", "yes");
+ xmlProps.setProperty("standalone", "no");
+ var serializer = SerializerFactory.getSerializer(xmlProps);
- //make all names we will use for variables legal
- scxmlRoot..transition.(@event.length() && (@event = makeLegalLValue(@event)));
+ var toReturn = options.inFiles.map(function(xmlFile){
- //make sure root has an id
- scxmlRoot.@id = "Root"
-
- //normalize initial states
- util.foreach(scxmlRoot..*.(@initial.length() > 0),function(s){
- s.initial.transition.@target = s.@initial.toString();
- delete s.@initial;
- });
-
- //make sure all transitions have id's
- //var transitionid = 0;
- //scxml..transition.(!@id.length() && (@id = "transition_" + transitionid++));
-
- //we use both a dictionary and an array so that we can use both forEach and "in", which makes the code pretty
- var stateTagsArray = ["state", "parallel" , "final" , "initial" , "scxml" , "history" ];
- var stateTagsHash = {}
- stateTagsArray.forEach(function(s){stateTagsHash[s] = true});
-
- //precompute some stuff which we will need later
- var conf = {
- scxml : scxmlRoot,
- allStates : scxmlRoot + scxmlRoot..*.(localName() in stateTagsHash),
- states : scxmlRoot..state,
- parallels : scxmlRoot..parallel,
- finals : scxmlRoot.descendants("final"),
- initials : scxmlRoot..initial,
- transitions : scxmlRoot..transition,
- defaultTransitions : scxmlRoot..transition.(@event.length() == 0),
- nonDefaultTransitions : scxmlRoot..transition.(@event.length() > 0),
- name : scxmlRoot.@name,
- noForEach : options.noForEach,
- noIndexOf : options.noIndexOf,
- noMap : options.noMap,
- log : options.log
+ if(options.ie){
+ options.noMap = options.noForEach = options.noIndexOf = true;
}
- conf.stateTagsHash = stateTagsHash;
- conf.stateTagsArray = stateTagsArray;
+ var baos = new ByteArrayOutputStream();
- conf.basicStates = conf.allStates.(stateTagsArray.every(
- function(s){
- return descendants(s).length() == 0
- }));
-
- conf.compositeStates = conf.allStates.(stateTagsArray.some(
- function(s){
- return descendants(s).length() > 0
- }));
-
- conf.parallelRegions = conf.parallels.state;
-
- conf.parallelsAndDescendantStates = conf.parallels + conf.parallels..*.(localName() in stateTagsHash);
-
-
- //make sure all states have id's
- var idNum = 0;
- util.foreach(conf.allStates.(@id.length() == 0),function(s){
- while(true){
- //we want initial states to be named by a slightly different convention
- var newId = s.localName() == "initial" ?
- s.parent().@id + "_Initial" + idNum++ :
- s.localName() + idNum++;
- if(!conf.allStates.(@id == newId).length()){
- //he is unique
- s.@id = newId;
- break;
- }
- }
- });
-
- //very important transformation to allow the use of ortho regions:
- //we take transition target attribute, which can contain multiple space-delimited targets
- //and split it up into multiple elements stored as children under the transition under our
- //namespace.
- //This allows us to treat transition targets generally and uniformly in our compiler.
- util.foreach(conf.transitions,function(t){
- var targetIds = t.@target.toString().split(" ");
-
- targetIds.forEach(function(targetId){
- t.msdl::targets.msdl::target += <target xmlns={msdlNs}><targetState>{targetId}</targetState></target>;
- });
- });
-
-
- //change transitions that point to non-basic states to point instead to the initial states
- //note that we only change the transitions stored in our custom-namespaced elements, so this
- //is still legal SCXML
- util.foreach(conf.transitions,function(t){
- util.foreach(t.msdl::targets.msdl::target,function(targetNode){
- var target = conf.allStates.(@id == targetNode.msdl::targetState.text());
-
- //if he is a composite, update the transition to point to his initial state
- if(conf.compositeStates.contains(target)){
- targetNode.msdl::targetState.setChildren(target.initial.@id.toString());
- }
- });
- });
-
-
- //augment our AST to allow easy retrieval of enter and exit states from transitions
- util.foreach(conf.transitions,function(t){
-
- var startnode = t.parent();
- var exitpath, lca;
-
- util.foreach(t.msdl::targets.msdl::target,function(targetNode){
-
- var endnode = conf.allStates.(@id == targetNode.msdl::targetState.text());
-
- var enterpath;
- [enterpath, exitpath, lca] = getTransitionPath(conf.allStates,startnode,endnode);
-
- if(enterpath){
- enterpath.forEach(function(state){
- targetNode.msdl::enterpath.msdl::state += state.@id;
- });
- }
-
- });
-
- if(exitpath){
- exitpath.forEach(function(state){
- t.msdl::exitpath.msdl::state += state.@id;
- });
- }
-
- if(lca){
- t.msdl::lca = lca.@id;
- }
-
- });
-
-
- util.foreach(scxmlRoot.descendants("if"),function(ifNode){
- var toReturn = "";
-
- var ifNodeChildren = ifNode.*;
-
- ifNode.msdl::executableContent = "";
- var currentPartition = ifNode.msdl::executableContent;
-
- util.foreach(ifNodeChildren,function(node){
- if(node.localName() == "elseif" ||
- node.localName() == "else"){
- node.msdl::executableContent = "";
- currentPartition = node.msdl::executableContent;
- }else{
- currentPartition.appendChild(node.copy());
- }
- });
- });
-
-
- util.foreach(conf.allStates,function(state){
- if(conf.parallelRegions..*.contains(state)){
- state.@msdl::isParallelSubstate = true;
- };
- if(conf.basicStates.contains(state)){
- state.@msdl::isBasic = true;
- };
- });
-
- util.foreach(scxmlRoot..transition,function(transition){
-
- var sourceState = transition.parent();
-
- var targetId = transition.msdl::targets.msdl::target.msdl::targetState[0].toString();
- var transitionTarget = conf.allStates.(@id == targetId);
-
- //we use these funky-looking variables to avoid namespace collisions in the below query
- var $s = sourceState, $t = transitionTarget;
- if(!conf.parallelRegions.(
- descendants($s.localName()).contains($s) &&
- descendants($t.localName()).contains($t)).length()){
-
- transition.@msdl::exitsParallelRegion = true;
- }
-
-
- //set whether he is preempted
- if(conf.parallelsAndDescendantStates.contains(sourceState) &&
- !conf.parallelsAndDescendantStates.contains(transitionTarget)){
-
- transition.@msdl::isPreempted = true;
- }
-
- });
-
-
- return scxmlRoot;
-
- //do transforms
- importPackage(javax.xml.transform);
- importPackage(javax.xml.transform.stream);
- importClass(java.io.StringWriter);
-
- var outWriter = new StringWriter();
+ serializer.setOutputStream(baos);
+ lastFilter.setContentHandler(serializer.asContentHandler());
- //this stuff needs to get seriously cleaned up
+ lastFilter.parse(new InputSource(xmlFile));
- //write to temp file
- fileUtil.writeFile(scxmlRoot.toString(),"build/tmp.xml");
-
- var tFactory = TransformerFactory.newInstance();
- var transformer = tFactory.newTransformer(new StreamSource("./test/xslt/StatePatternStatechartGenerator.xsl"));
-
- //set parameters
- /*
- for(var paramName in conf){
- transformer.setParameter(paramName,conf[paramName]);
- }
- */
-
- transformer.transform(new StreamSource("build/tmp.xml"), new StreamResult(outWriter));
- var transformedJs = new String(outWriter.toString()); //convert him from Java string to js string for js_beautify
+ var transformedJs = String(new java.lang.String(baos.toByteArray()));
if(options.beautify){
- debugger;
transformedJs = js_beautify(transformedJs);
}
return transformedJs;
-
- /*
- var generatorConstructor;
- switch(options.backend){
- case "switch":
- generatorConstructor = SwitchyardStatechartGenerator;
- break;
- case "table":
- generatorConstructor = StateTableStatechartGenerator;
- break;
- case "state":
- generatorConstructor = StatePatternStatechartGenerator;
- break;
- default:
- print("Unknown backend specified: " + options.backend);
- }
-
- var scg = new generatorConstructor(conf);
- var sc = scg.genStatechart(conf);
-
- //print(conf.scxml);
- if(options.verbose) print(sc);
-
- //TODO: write to specified file
-
- //return generated statechart as a string
- return sc;
- */
});
if(toReturn.length == 1){
Modified: commons/sandbox/gsoc/2010/scxml-js/trunk/test/testScript.sh
URL: http://svn.apache.org/viewvc/commons/sandbox/gsoc/2010/scxml-js/trunk/test/testScript.sh?rev=954089&r1=954088&r2=954089&view=diff
==============================================================================
--- commons/sandbox/gsoc/2010/scxml-js/trunk/test/testScript.sh (original)
+++ commons/sandbox/gsoc/2010/scxml-js/trunk/test/testScript.sh Sat Jun 12 19:38:44 2010
@@ -7,4 +7,5 @@ xsltproc xslt/transformIf.xsl - | \
xsltproc xslt/appendStateInformation.xsl - | \
xsltproc xslt/appendBasicStateInformation.xsl - | \
xsltproc xslt/appendTransitionInformation.xsl - | \
-xmlindent
+xmlindent > tmp_IR.xml;
+xsltproc xslt/StatePatternStatechartGenerator.xsl tmp_IR.xml