diff options
Diffstat (limited to 'src/uscxml/transform/ChartToFlatSCXML.cpp')
-rw-r--r-- | src/uscxml/transform/ChartToFlatSCXML.cpp | 134 |
1 files changed, 67 insertions, 67 deletions
diff --git a/src/uscxml/transform/ChartToFlatSCXML.cpp b/src/uscxml/transform/ChartToFlatSCXML.cpp index c554218..2905444 100644 --- a/src/uscxml/transform/ChartToFlatSCXML.cpp +++ b/src/uscxml/transform/ChartToFlatSCXML.cpp @@ -52,7 +52,7 @@ ChartToFlatSCXML::operator Interpreter() { return Interpreter::fromClone(shared_from_this()); } - + Transformer ChartToFlatSCXML::transform(const Interpreter& other) { return boost::shared_ptr<TransformerImpl>(new ChartToFlatSCXML(other)); } @@ -79,10 +79,10 @@ void ChartToFlatSCXML::writeTo(std::ostream& stream) { if (HAS_ATTR(element, "final-target")) element.removeAttribute("final-target"); } - + if (envVarIsTrue("USCXML_FLAT_FSM_METRICS_ONLY")) return; - + stream << _scxml; } @@ -90,7 +90,7 @@ void ChartToFlatSCXML::createDocument() { if (HAS_ATTR(_scxml, "flat") && stringIsTrue(ATTR(_scxml, "flat"))) return; - + { NodeSet<std::string> allElements = filterChildType(Node_base::ELEMENT_NODE, _scxml, true); size_t nrElements = 0; @@ -100,36 +100,36 @@ void ChartToFlatSCXML::createDocument() { } std::cerr << "Number of elements before flattening: " << nrElements + 1 << std::endl; } - + if (_start == NULL) interpret(); // only if not already flat! - + if (envVarIsTrue("USCXML_FLAT_FSM_METRICS_ONLY")) return; - + Element<std::string> _origSCXML = _scxml; - + _scxml = _flatDoc.createElementNS(_nsInfo.nsURL, "scxml"); _nsInfo.setPrefix(_scxml); - + _scxml.setAttribute("flat", "true"); _flatDoc.appendChild(_scxml); - + if (HAS_ATTR(_origSCXML, "datamodel")) { _scxml.setAttribute("datamodel", ATTR(_origSCXML, "datamodel")); } - + if (HAS_ATTR(_origSCXML, "name")) { _scxml.setAttribute("name", ATTR(_origSCXML, "name")); } - + if (HAS_ATTR(_origSCXML, "binding")) { _scxml.setAttribute("binding", ATTR(_origSCXML, "binding")); } - + _scxml.setAttribute("initial", _start->stateId); - + NodeSet<std::string> datas; if (_binding == InterpreterImpl::LATE) { // with late binding, just copy direct datamodel childs @@ -144,34 +144,34 @@ void ChartToFlatSCXML::createDocument() { Node<std::string> imported = _flatDoc.importNode(datas[i], true); _scxml.appendChild(imported); } - - + + NodeSet<std::string> scripts = filterChildElements(_nsInfo.xmlNSPrefix + "script", _origSCXML); for (int i = 0; i < scripts.size(); i++) { Node<std::string> imported = _flatDoc.importNode(scripts[i], true); _scxml.appendChild(imported); } - + NodeSet<std::string> comments = filterChildType(Node_base::COMMENT_NODE, _origSCXML); for (int i = 0; i < comments.size(); i++) { Node<std::string> imported = _flatDoc.importNode(comments[i], true); _scxml.appendChild(imported); } - + std::vector<std::pair<std::string,GlobalState*> > sortedStates; sortedStates.insert(sortedStates.begin(), _globalConf.begin(), _globalConf.end()); std::sort(sortedStates.begin(), sortedStates.end(), sortStatesByIndex); - + // int index = 0; // for (std::vector<Element<std::string> >::iterator transIter = indexedTransitions.begin(); transIter != indexedTransitions.end(); transIter++) { // const Element<std::string>& refTrans = *transIter; // std::cerr << index++ << ": " << refTrans << std::endl; // } // std::cerr << std::endl; - + for (std::vector<std::pair<std::string,GlobalState*> >::iterator confIter = sortedStates.begin(); - confIter != sortedStates.end(); - confIter++) { + confIter != sortedStates.end(); + confIter++) { appendGlobalStateNode(confIter->second); } @@ -181,7 +181,7 @@ void ChartToFlatSCXML::createDocument() { if (scxmls.size() > 0) { _scxml = Element<std::string>(scxmls[0]); } - + { NodeSet<std::string> allElements = filterChildType(Node_base::ELEMENT_NODE, _scxml, true); size_t nrElements = 0; @@ -191,29 +191,29 @@ void ChartToFlatSCXML::createDocument() { } std::cerr << "Number of elements after flattening: " << nrElements + 1 << std::endl; } - + } void ChartToFlatSCXML::appendGlobalStateNode(GlobalState* globalState) { Element<std::string> state = _flatDoc.createElementNS(_nsInfo.nsURL, "state"); _nsInfo.setPrefix(state); - + state.setAttribute("step", toStr(globalState->index)); state.setAttribute("id", globalState->stateId); - + if (globalState->isFinal) state.setAttribute("final", "true"); - + std::list<GlobalTransition*>& transitionList = globalState->sortedOutgoing; - + // apend here, for transient state chains to trail the state _scxml.appendChild(state); - + size_t index = 0; for (std::list<GlobalTransition*>::iterator outIter = transitionList.begin(); - outIter != transitionList.end(); - outIter++) { + outIter != transitionList.end(); + outIter++) { // (*outIter)->index = globalState->index + ":" + toStr(index); state.appendChild(globalTransitionToNode(*outIter)); index++; @@ -226,48 +226,48 @@ void ChartToFlatSCXML::appendGlobalStateNode(GlobalState* globalState) { Node<std::string> ChartToFlatSCXML::globalTransitionToNode(GlobalTransition* globalTransition) { Element<std::string> transition = _flatDoc.createElementNS(_nsInfo.nsURL, "transition"); _nsInfo.setPrefix(transition); - + // transition.setAttribute("ref", globalTransition->index); - + #if 1 transition.setAttribute("members", globalTransition->members); #endif // transition.setAttribute("priority", toStr(globalTransition->priority)); - + if (!globalTransition->isEventless) { transition.setAttribute("event", globalTransition->eventDesc); } - + if (globalTransition->condition.size() > 0) { transition.setAttribute("cond", globalTransition->condition); } - + if (globalTransition->destination.size() > 0) { transition.setAttribute("final-target", globalTransition->destination); } - + NodeSet<std::string> transientStateChain; - + // current active state set FlatStateIdentifier flatId(globalTransition->source); std::list<std::string> currActiveStates = flatId.getActive(); - + // std::cerr << "From " << globalTransition->source << " to " << globalTransition->destination << ":" << std::endl; - + // gather content for new transient state NodeSet<std::string> childs; - + // aggregated entering / exiting to avoid states without childs while still labeling std::list<Arabica::DOM::Comment<std::string> > pendingComments; - + // iterate all actions taken during the transition for (std::list<GlobalTransition::Action>::iterator actionIter = globalTransition->actions.begin(); - actionIter != globalTransition->actions.end(); - actionIter++) { - + actionIter != globalTransition->actions.end(); + actionIter++) { + if (actionIter->transition) { // DETAIL_EXEC_CONTENT(transition, actionIter); - + Element<std::string> onexit = _flatDoc.createElementNS(_nsInfo.nsURL, "onexit"); _nsInfo.setPrefix(onexit); Node<std::string> child = actionIter->transition.getFirstChild(); @@ -286,7 +286,7 @@ Node<std::string> ChartToFlatSCXML::globalTransitionToNode(GlobalTransition* glo if (HAS_ATTR(actionIter->transition, "target")) commentSS << " to target '" << ATTR(actionIter->transition, "target") << "'"; commentSS << " "; - + if (onexit.hasChildNodes()) { if (envVarIsTrue("USCXML_ANNOTATE_VERBOSE_COMMENTS")) childs.push_back(_flatDoc.createComment(commentSS.str())); @@ -295,16 +295,16 @@ Node<std::string> ChartToFlatSCXML::globalTransitionToNode(GlobalTransition* glo if (envVarIsTrue("USCXML_ANNOTATE_VERBOSE_COMMENTS")) pendingComments.push_back(_flatDoc.createComment(commentSS.str())); } - + continue; } - + if (actionIter->onExit) { // DETAIL_EXEC_CONTENT(onExit, actionIter); childs.push_back(actionIter->onExit); continue; } - + if (actionIter->onEntry) { // DETAIL_EXEC_CONTENT(onEntry, actionIter); childs.push_back(actionIter->onEntry); @@ -324,12 +324,12 @@ Node<std::string> ChartToFlatSCXML::globalTransitionToNode(GlobalTransition* glo childs.push_back(invokeElem); continue; } - + if (actionIter->uninvoke) { // DETAIL_EXEC_CONTENT(uninvoke, actionIter); Element<std::string> uninvokeElem = _flatDoc.createElementNS(_nsInfo.nsURL, "uninvoke"); _nsInfo.setPrefix(uninvokeElem); - + if (HAS_ATTR(actionIter->uninvoke, "type")) { uninvokeElem.setAttribute("type", ATTR(actionIter->uninvoke, "type")); } @@ -345,7 +345,7 @@ Node<std::string> ChartToFlatSCXML::globalTransitionToNode(GlobalTransition* glo childs.push_back(uninvokeElem); continue; } - + if (actionIter->exited) { currActiveStates.remove(ATTR_CAST(actionIter->exited, "id")); if (childs.size() > 0) { @@ -358,7 +358,7 @@ Node<std::string> ChartToFlatSCXML::globalTransitionToNode(GlobalTransition* glo pendingComments.push_back(_flatDoc.createComment(" Exiting " + ATTR_CAST(actionIter->exited, "id") + " ")); } } - + if (actionIter->entered) { if (childs.size() > 0) { if (envVarIsTrue("USCXML_ANNOTATE_VERBOSE_COMMENTS")) @@ -369,7 +369,7 @@ Node<std::string> ChartToFlatSCXML::globalTransitionToNode(GlobalTransition* glo pendingComments.push_back(_flatDoc.createComment(" Entering " + ATTR_CAST(actionIter->entered, "id") + " ")); } currActiveStates.push_back(ATTR_CAST(actionIter->entered, "id")); - + // we entered a new child - check if it has a datamodel and we entered for the first time if (_binding == InterpreterImpl::LATE) { NodeSet<std::string> datamodel = filterChildElements(_nsInfo.xmlNSPrefix + "datamodel", actionIter->entered); @@ -379,35 +379,35 @@ Node<std::string> ChartToFlatSCXML::globalTransitionToNode(GlobalTransition* glo } } } - + CREATE_TRANSIENT_STATE_WITH_CHILDS(FlatStateIdentifier::toStateId(currActiveStates)) - + if (transientStateChain.size() > 0) { Element<std::string> prevExitTransitionElem; - + for (int i = 0; i < transientStateChain.size(); i++) { Element<std::string> transientStateElem = Element<std::string>(transientStateChain[i]); transientStateElem.setAttribute("id", transientStateElem.getAttribute("id") + "-via-" + toStr(_lastTransientStateId++)); - + Element<std::string> exitTransition = _flatDoc.createElementNS(_nsInfo.nsURL, "transition"); _nsInfo.setPrefix(exitTransition); - + if (prevExitTransitionElem) { // point previous to this one prevExitTransitionElem.setAttribute("target", transientStateElem.getAttribute("id")); } else { // update globalTransition->source target } - + transientStateElem.appendChild(exitTransition); prevExitTransitionElem = exitTransition; - + if (i == 0) transition.setAttribute("target", transientStateElem.getAttribute("id")); - + _scxml.appendChild(transientStateElem); } - + // last one points to actual target assert(prevExitTransitionElem); prevExitTransitionElem.setAttribute("target", globalTransition->destination); @@ -415,11 +415,11 @@ Node<std::string> ChartToFlatSCXML::globalTransitionToNode(GlobalTransition* glo } else if (transientStateChain.size() == 1) { Element<std::string> transientStateElem = Element<std::string>(transientStateChain[0]); transientStateElem.setAttribute("onlyOne", "yes!"); - + Element<std::string> exitTransition = _flatDoc.createElementNS(_nsInfo.nsURL, "transition"); _nsInfo.setPrefix(exitTransition); exitTransition.setAttribute("target", globalTransition->destination); - + transientStateElem.appendChild(exitTransition); _scxml.appendChild(transientStateElem); @@ -428,7 +428,7 @@ Node<std::string> ChartToFlatSCXML::globalTransitionToNode(GlobalTransition* glo } else { transition.setAttribute("target", globalTransition->destination); } - + assert(HAS_ATTR_CAST(transition, "target")); return transition; } |