summaryrefslogtreecommitdiffstats
path: root/src/uscxml/transform/ChartToFSM.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/uscxml/transform/ChartToFSM.cpp')
-rw-r--r--src/uscxml/transform/ChartToFSM.cpp397
1 files changed, 221 insertions, 176 deletions
diff --git a/src/uscxml/transform/ChartToFSM.cpp b/src/uscxml/transform/ChartToFSM.cpp
index d962481..8b1725c 100644
--- a/src/uscxml/transform/ChartToFSM.cpp
+++ b/src/uscxml/transform/ChartToFSM.cpp
@@ -19,6 +19,8 @@
#include "uscxml/transform/ChartToFSM.h"
#include <DOM/io/Stream.hpp>
+#include <glog/logging.h>
+
#include <iostream>
#include "uscxml/UUID.h"
#include <math.h>
@@ -34,7 +36,9 @@ using namespace Arabica::XPath;
#define CREATE_TRANSIENT_STATE_WITH_CHILDS \
if (childs.size() > 0) { \
- Element<std::string> transientState = _flatDoc.createElementNS(xmlNs, "state"); \
+ Element<std::string> transientState = _flatDoc.createElementNS(_nsInfo.nsURL, "state"); \
+ _nsInfo.setPrefix(transientState);\
+ transientState.setAttribute("transient", "true"); \
for (int i = 0; i < childs.size(); i++) { \
Node<std::string> imported = _flatDoc.importNode(childs[i], true); \
transientState.appendChild(imported); \
@@ -43,19 +47,24 @@ if (childs.size() > 0) { \
} \
childs = NodeSet<std::string>();
-Document<std::string> ChartToFSM::flatten(const Document<std::string>& doc, const std::map<std::string, std::string>& nameSpaceInfo) {
- boost::shared_ptr<InterpreterImpl> flatInterpreter = boost::shared_ptr<InterpreterImpl>(new FlatteningInterpreter(doc));
- flatInterpreter->setNameSpaceInfo(nameSpaceInfo);
- flatInterpreter->interpret();
- return flatInterpreter->getDocument();
+Interpreter ChartToFSM::flatten(const Interpreter& other) {
+
+ // instantiate a new InterpreterImpl to do the flattening
+ boost::shared_ptr<InterpreterImpl> flattener = boost::shared_ptr<InterpreterImpl>(new FlatteningInterpreter(other.getDocument()));
+ flattener->setNameSpaceInfo(other.getNameSpaceInfo());
+ flattener->interpret();
+
+ // clone the flattener implementation to a new interpreter
+ Interpreter flat = Interpreter::fromClone(flattener);
+ return flat;
}
FlatteningInterpreter::FlatteningInterpreter(const Document<std::string>& doc) {
-
+
_currGlobalTransition = NULL;
-
- // kust copy given doc into _document an create _flatDoc for the FSM
+
+ // just copy given doc into _document an create _flatDoc for the FSM
DOMImplementation<std::string> domFactory = Arabica::SimpleDOM::DOMImplementation<std::string>::getDOMImplementation();
_document = domFactory.createDocument(doc.getNamespaceURI(), "", 0);
_flatDoc = domFactory.createDocument(doc.getNamespaceURI(), "", 0);
@@ -70,10 +79,24 @@ FlatteningInterpreter::FlatteningInterpreter(const Document<std::string>& doc) {
addMonitor(this);
}
-Document<std::string>& FlatteningInterpreter::getDocument() {
- std::cout << "######################" << std::endl;
- std::cout << _flatDoc << std::endl;
- std::cout << "######################" << std::endl;
+FlatteningInterpreter::~FlatteningInterpreter() {
+ std::map<std::string, GlobalState*>::iterator confIter = _globalConf.begin();
+ while(confIter != _globalConf.end()) {
+ std::map<std::string, GlobalTransition*>::iterator transIter = confIter->second->outgoing.begin();
+ while (transIter != confIter->second->outgoing.end()) {
+ delete transIter->second;
+ transIter++;
+ }
+ delete confIter->second;
+ confIter++;
+ }
+
+}
+
+Document<std::string> FlatteningInterpreter::getDocument() const {
+// std::cout << "######################" << std::endl;
+// std::cout << _flatDoc << std::endl;
+// std::cout << "######################" << std::endl;
return _flatDoc;
}
@@ -81,7 +104,7 @@ void FlatteningInterpreter::interpret() {
init();
setupIOProcessors();
-
+
// initialize the datamodel
std::string datamodelName;
if (datamodelName.length() == 0 && HAS_ATTR(_scxml, "datamodel"))
@@ -99,20 +122,19 @@ void FlatteningInterpreter::interpret() {
_dataModel = _factory->createDataModel("null", this);
}
if(datamodelName.length() > 0 && !_dataModel) {
- std::cout << "No datamodel for " << datamodelName << " registered";
- }
-
- if (_dataModel) {
- _dataModel.assign("_x.args", _cmdLineOptions);
+ LOG(ERROR) << "No datamodel for " << datamodelName << " registered";
}
-
+
_binding = (HAS_ATTR(_scxml, "binding") && iequals(ATTR(_scxml, "binding"), "late") ? LATE : EARLY);
-
+ // reset
+ _globalConf.clear();
+ _currGlobalTransition = NULL;
+
// very first state
_start = new GlobalState(_configuration, _alreadyEntered, _historyValue);
_globalConf[_start->stateId] = _start;
-
+
NodeSet<std::string> initialTransitions;
// enter initial configuration
@@ -120,9 +142,11 @@ void FlatteningInterpreter::interpret() {
initialStates = getInitialStates();
assert(initialStates.size() > 0);
for (int i = 0; i < initialStates.size(); i++) {
- Element<std::string> initialElem = _document.createElementNS(_nsURL, "initial");
+ Element<std::string> initialElem = _document.createElementNS(_nsInfo.nsURL, "initial");
+ _nsInfo.setPrefix(initialElem);
initialElem.setAttribute("generated", "true");
- Element<std::string> transitionElem = _document.createElementNS(_nsURL, "transition");
+ Element<std::string> transitionElem = _document.createElementNS(_nsInfo.nsURL, "transition");
+ _nsInfo.setPrefix(transitionElem);
transitionElem.setAttribute("target", ATTR(initialStates[i], "id"));
initialElem.appendChild(transitionElem);
_scxml.appendChild(initialElem);
@@ -130,8 +154,8 @@ void FlatteningInterpreter::interpret() {
}
labelTransitions();
weightTransitions();
-
- std::cout << _scxml << std::endl;
+
+// std::cout << _scxml << std::endl;
GlobalTransition* globalTransition = new GlobalTransition(initialTransitions, _dataModel);
_start->outgoing[globalTransition->transitionId] = globalTransition;
@@ -140,22 +164,22 @@ void FlatteningInterpreter::interpret() {
enterStates(initialTransitions);
explode();
-
+
#if 0
// print set of global configurations
for(std::map<std::string, GlobalState*>::iterator globalConfIter = _globalConf.begin();
- globalConfIter != _globalConf.end();
- globalConfIter++) {
+ globalConfIter != _globalConf.end();
+ globalConfIter++) {
std::cout << globalConfIter->first << std::endl;
}
std::cout << _globalConf.size() << std::endl;
#endif
-
+
createDocument();
}
void FlatteningInterpreter::executeContent(const Arabica::DOM::Node<std::string>& content, bool rethrow) {
- std::cout << content << std::endl;
+// std::cout << content << std::endl;
GlobalTransition::Action action;
if (false) {
@@ -199,51 +223,55 @@ void FlatteningInterpreter::cancelInvoke(const Arabica::DOM::Node<std::string>&
}
void FlatteningInterpreter::internalDoneSend(const Arabica::DOM::Node<std::string>& state) {
-
+
if (!isState(state))
return;
Arabica::DOM::Element<std::string> stateElem = (Arabica::DOM::Element<std::string>)state;
// if (parentIsScxmlState(state))
// return;
-
+
// std::cout << "internalDoneSend: " << state << std::endl;
-
+
// create onentry with a raise element
- Element<std::string> onentry = _flatDoc.createElement("onentry");
- Element<std::string> raise = _flatDoc.createElement("raise");
+ Element<std::string> onentry = _flatDoc.createElementNS(_nsInfo.nsURL, "onentry");
+ _nsInfo.setPrefix(onentry);
+
+ Element<std::string> raise = _flatDoc.createElementNS(_nsInfo.nsURL, "raise");
+ _nsInfo.setPrefix(raise);
+
onentry.appendChild(raise);
- Arabica::XPath::NodeSet<std::string> doneDatas = filterChildElements(_xmlNSPrefix + "donedata", stateElem);
+ Arabica::XPath::NodeSet<std::string> doneDatas = filterChildElements(_nsInfo.xmlNSPrefix + "donedata", stateElem);
if (doneDatas.size() > 0) {
Arabica::DOM::Node<std::string> doneData = doneDatas[0];
- Arabica::XPath::NodeSet<std::string> contents = filterChildElements(_xmlNSPrefix + "content", doneDatas[0]);
+ Arabica::XPath::NodeSet<std::string> contents = filterChildElements(_nsInfo.xmlNSPrefix + "content", doneDatas[0]);
if (contents.size() > 0) {
Node<std::string> imported = _flatDoc.importNode(contents[0], true);
raise.appendChild(imported);
}
- Arabica::XPath::NodeSet<std::string> params = filterChildElements(_xmlNSPrefix + "param", doneDatas[0]);
+ Arabica::XPath::NodeSet<std::string> params = filterChildElements(_nsInfo.xmlNSPrefix + "param", doneDatas[0]);
if (params.size() > 0) {
Node<std::string> imported = _flatDoc.importNode(params[0], true);
raise.appendChild(imported);
}
}
-
+
raise.setAttribute("event", "done.state." + ATTR(stateElem.getParentNode(), "id")); // parent?!
-
+
GlobalTransition::Action action;
action.onEntry = onentry;
-
+
_currGlobalTransition->actions.push_back(action);
-
+
}
static bool isSuperset(const GlobalTransition* t1, const GlobalTransition* t2) {
bool isSuperset = true;
-
+
if (t1->transitions.size() >= t2->transitions.size())
return false;
-
+
for (int i = 0; i < t1->transitions.size(); i++) {
if (!InterpreterImpl::isMember(t1->transitions[i], t2->transitions)) {
isSuperset = false;
@@ -251,7 +279,7 @@ static bool isSuperset(const GlobalTransition* t1, const GlobalTransition* t2) {
}
return isSuperset;
}
-
+
static NodeSet<std::string> filterChildEnabled(const NodeSet<std::string>& transitions) {
// drop any transition that is already enabled by a child
NodeSet<std::string> filteredTransitions;
@@ -269,7 +297,7 @@ static NodeSet<std::string> filterChildEnabled(const NodeSet<std::string>& trans
std::string eventDesc1 = ATTR(t1, "event");
std::string eventDesc2 = ATTR(t2, "event");
if (InterpreterImpl::nameMatch(eventDesc1, eventDesc2)) {
- std::cout << "Dropping " << t1 << std::endl << "for " << t2 << std::endl;
+// std::cout << "Dropping " << t1 << std::endl << "for " << t2 << std::endl;
goto SKIP_TRANSITION;
}
}
@@ -277,7 +305,8 @@ static NodeSet<std::string> filterChildEnabled(const NodeSet<std::string>& trans
}
}
filteredTransitions.push_back(t1);
- SKIP_TRANSITION:;
+SKIP_TRANSITION:
+ ;
}
return filteredTransitions;
}
@@ -286,14 +315,14 @@ static std::list<GlobalTransition*> sortTransitions(std::list<GlobalTransition*>
bool stable = false;
while(!stable) {
for (std::list<GlobalTransition*>::iterator outerIter = list.begin();
- outerIter != list.end();
- outerIter++) {
+ outerIter != list.end();
+ outerIter++) {
for (std::list<GlobalTransition*>::iterator innerIter = outerIter;
- innerIter != list.end();
- innerIter++) {
+ innerIter != list.end();
+ innerIter++) {
GlobalTransition* t1 = *outerIter;
GlobalTransition* t2 = *innerIter;
-
+
if (isSuperset(t1, t2)) {
// std::cout << "swapping " << t1->transitionId << " / " << t2->transitionId << std::endl;
std::swap(*innerIter, *outerIter);
@@ -313,7 +342,8 @@ static std::list<GlobalTransition*> sortTransitions(std::list<GlobalTransition*>
}
}
stable = true;
- NEXT_ITER:;
+NEXT_ITER:
+ ;
}
return list;
}
@@ -321,7 +351,7 @@ static std::list<GlobalTransition*> sortTransitions(std::list<GlobalTransition*>
void FlatteningInterpreter::explode() {
-
+
// save global configuration elements to restore after recursive explode
Arabica::XPath::NodeSet<std::string> configuration = _configuration;
Arabica::XPath::NodeSet<std::string> alreadyEntered = _alreadyEntered;
@@ -334,19 +364,19 @@ void FlatteningInterpreter::explode() {
if (_currGlobalTransition) {
_currGlobalTransition->destination = globalState->stateId;
globalState->incoming[_currGlobalTransition->transitionId] = _currGlobalTransition;
-
+
// if (!_currGlobalTransition->isEventless) {
- // if it was an eventful transition, add all invokers
- for (unsigned int i = 0; i < _statesToInvoke.size(); i++) {
- NodeSet<std::string> invokes = filterChildElements(_xmlNSPrefix + "invoke", _statesToInvoke[i]);
- for (unsigned int j = 0; j < invokes.size(); j++) {
- invoke(invokes[j]);
- }
+ // if it was an eventful transition, add all invokers
+ for (unsigned int i = 0; i < _statesToInvoke.size(); i++) {
+ NodeSet<std::string> invokes = filterChildElements(_nsInfo.xmlNSPrefix + "invoke", _statesToInvoke[i]);
+ for (unsigned int j = 0; j < invokes.size(); j++) {
+ invoke(invokes[j]);
}
- _statesToInvoke = NodeSet<std::string>();
+ }
+ _statesToInvoke = NodeSet<std::string>();
// }
}
-
+
if (_globalConf.find(globalState->stateId) != _globalConf.end()) {
delete globalState;
return; // we have already been here
@@ -354,8 +384,8 @@ void FlatteningInterpreter::explode() {
_globalConf[globalState->stateId] = globalState;
// get all transition elements from states in the current configuration
- NodeSet<std::string> allTransitions = filterChildElements(_xmlNSPrefix + "transition", configuration);
-
+ NodeSet<std::string> allTransitions = filterChildElements(_nsInfo.xmlNSPrefix + "transition", configuration);
+
/**
* From http://www.w3.org/TR/scxml/#SelectingTransitions
*
@@ -365,8 +395,8 @@ void FlatteningInterpreter::explode() {
* c) T lacks a 'cond' attribute or its 'cond' attribute evaluates to "true"
*
* A transition is enabled by NULL in atomic state S if
- * a) T lacks an 'event' attribute
- * b) T's source state is S or an ancestor of S
+ * a) T lacks an 'event' attribute
+ * b) T's source state is S or an ancestor of S
* c) T lacks an 'cond' attribute or its 'cond' attribute evaluates to "true"
*
* The _source state_ of a transition is the <state> or <parallel> element that it occurs in.
@@ -380,14 +410,14 @@ void FlatteningInterpreter::explode() {
*
* Two transitions T1 and T2 conflict in state configuration C if their exit sets in C have a non-null intersection.
* let transitions T1 and T2 conflict:
- * T1 is optimally enabled in atomic state S1
+ * T1 is optimally enabled in atomic state S1
* T2 is optimally enabled in atomic state S2
* S1 and S2 are both active
* T1 has a higher priority than T2 if:
* a) T1's source state is a descendant of T2's source state, or
* b) S1 precedes S2 in document order
*
- * The _optimal transition set_ enabled by event E in state configuration C is
+ * The _optimal transition set_ enabled by event E in state configuration C is
* the largest set of transitions such that:
* a) each transition in the set is optimally enabled by E in an atomic state in C
* b) no transition conflicts with another transition in the set
@@ -395,14 +425,14 @@ void FlatteningInterpreter::explode() {
*
* A _microstep_ consists of the execution of the transitions in an optimal enabled transition set
*
- * A _macrostep_ is a series of one or more microsteps ending in a configuration
+ * A _macrostep_ is a series of one or more microsteps ending in a configuration
* where the internal event queue is empty and no transitions are enabled by NULL
*/
-
-
+
+
if (allTransitions.size() == 0)
return; // no transitions
-
+
int nrElements = allTransitions.size();
int k = 0;
int* stack = (int*)malloc((nrElements + 1) * sizeof(int));
@@ -414,17 +444,17 @@ void FlatteningInterpreter::explode() {
while(1) {
// create the power set of all potential transitions
// see: http://www.programminglogic.com/powerset-algorithm-in-c/
-
- if (stack[k] < nrElements){
+
+ if (stack[k] < nrElements) {
stack[k+1] = stack[k] + 1;
k++;
}
-
+
else {
stack[k-1]++;
k--;
}
-
+
if (k==0)
break;
@@ -438,10 +468,10 @@ void FlatteningInterpreter::explode() {
// reduce to conflict-free subset
transitions = filterPreempted(transitions);
-
+
// algorithm can never reduce to empty set
assert(transitions.size() > 0);
-
+
// create a GlobalTransition object from the set
GlobalTransition* transition = new GlobalTransition(transitions, _dataModel);
if (!transition->isValid) {
@@ -452,7 +482,7 @@ void FlatteningInterpreter::explode() {
// two combinations might have projected onto the same conflict-free set
if (transitionSets.find(transition->transitionId) != transitionSets.end()) {
- std::cout << "skipping as projected onto existing conflict-free subset" << std::endl;
+// std::cout << "skipping as projected onto existing conflict-free subset" << std::endl;
delete transition;
continue;
}
@@ -475,7 +505,7 @@ void FlatteningInterpreter::explode() {
transition->firstElemPerLevel.push_back(lowestOrder);
transition->prioPerLevel.push_back(prioPerLevel);
}
-
+
#if 0
// calculate priority
transition->priority = 0;
@@ -495,25 +525,26 @@ void FlatteningInterpreter::explode() {
goto NEXT_DEPTH;
}
}
- NEXT_DEPTH:;
+NEXT_DEPTH:
+ ;
}
#endif
// remember this conflict-free set
- std::cout << "New conflict-free subset: " << transition->transitionId << ":" << transition->eventDesc << std::endl;
+// std::cout << "New conflict-free subset: " << transition->transitionId << ":" << transition->eventDesc << std::endl;
transitionSets[transition->transitionId] = transition;
}
// TODO: reduce and sort transition sets
std::list<GlobalTransition*> transitionList;
for(std::map<std::string, GlobalTransition*>::iterator transSetIter = transitionSets.begin();
- transSetIter != transitionSets.end();
- transSetIter++) {
+ transSetIter != transitionSets.end();
+ transSetIter++) {
transitionList.push_back(transSetIter->second);
}
-
+
for(std::list<GlobalTransition*>::iterator transListIter = transitionList.begin();
- transListIter != transitionList.end();
- transListIter++) {
+ transListIter != transitionList.end();
+ transListIter++) {
// add transition set to current global state
globalState->outgoing[(*transListIter)->transitionId] = *transListIter;
@@ -522,7 +553,7 @@ void FlatteningInterpreter::explode() {
_currGlobalTransition = *transListIter;
microstep((*transListIter)->transitions);
explode();
-
+
// reset state for next transition set
_configuration = configuration;
_alreadyEntered = alreadyEntered;
@@ -530,12 +561,13 @@ void FlatteningInterpreter::explode() {
}
}
-
+
void FlatteningInterpreter::createDocument() {
- std::string xmlNs = _nsURL;
Node<std::string> _origSCXML = _scxml;
-
- _scxml = _flatDoc.createElementNS(xmlNs, "scxml");
+
+ _scxml = _flatDoc.createElementNS(_nsInfo.nsURL, "scxml");
+ _nsInfo.setPrefix(_scxml);
+
_scxml.setAttribute("flat", "true");
_flatDoc.appendChild(_scxml);
@@ -546,16 +578,16 @@ void FlatteningInterpreter::createDocument() {
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
- datas = filterChildElements(_xmlNSPrefix + "datamodel", _origSCXML);
+ datas = filterChildElements(_nsInfo.xmlNSPrefix + "datamodel", _origSCXML);
} else {
// with early binding, copy all datamodel elements into scxml element
- datas = _xpath.evaluate("//" + _xpathPrefix + "datamodel", _origSCXML).asNodeSet();
+ datas = _xpath.evaluate("//" + _nsInfo.xpathPrefix + "datamodel", _origSCXML).asNodeSet();
}
for (int i = 0; i < datas.size(); i++) {
if (isInEmbeddedDocument(datas[i]))
@@ -564,25 +596,33 @@ void FlatteningInterpreter::createDocument() {
_scxml.appendChild(imported);
}
-
- NodeSet<std::string> scripts = filterChildElements(_xmlNSPrefix + "script", _origSCXML);
+
+ 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);
+ }
+
for (std::map<std::string, GlobalState*>::iterator confIter = _globalConf.begin();
- confIter != _globalConf.end();
- confIter++) {
- Node<std::string> state = globalStateToNode(confIter->second, xmlNs);
+ confIter != _globalConf.end();
+ confIter++) {
+ Node<std::string> state = globalStateToNode(confIter->second);
_scxml.appendChild(state);
}
// exit(0);
}
-Node<std::string> FlatteningInterpreter::globalStateToNode(GlobalState* globalState, const std::string& xmlNs) {
- Element<std::string> state = _flatDoc.createElementNS(xmlNs, "state");
+Node<std::string> FlatteningInterpreter::globalStateToNode(GlobalState* globalState) {
+ Element<std::string> state = _flatDoc.createElementNS(_nsInfo.nsURL, "state");
+ _nsInfo.setPrefix(state);
+
state.setAttribute("id", globalState->stateId);
if (globalState->isFinal)
@@ -590,20 +630,20 @@ Node<std::string> FlatteningInterpreter::globalStateToNode(GlobalState* globalSt
std::list<GlobalTransition*> transitionList;
for (std::map<std::string, GlobalTransition*>::iterator outIter = globalState->outgoing.begin();
- outIter != globalState->outgoing.end();
- outIter++) {
+ outIter != globalState->outgoing.end();
+ outIter++) {
transitionList.push_back(outIter->second);
}
transitionList = sortTransitions(transitionList);
-
- std::cout << "/////////////////" << std::endl;
+
+// std::cout << "/////////////////" << std::endl;
for (std::list<GlobalTransition*>::iterator outIter = transitionList.begin();
- outIter != transitionList.end();
- outIter++) {
- state.appendChild(globalTransitionToNode(*outIter, xmlNs));
+ outIter != transitionList.end();
+ outIter++) {
+ state.appendChild(globalTransitionToNode(*outIter));
}
- std::cout << "/////////////////" << std::endl;
+// std::cout << "/////////////////" << std::endl;
return state;
}
@@ -611,8 +651,9 @@ Node<std::string> FlatteningInterpreter::globalStateToNode(GlobalState* globalSt
/**
* Creates transient states for executable content as a side-effect
*/
-Node<std::string> FlatteningInterpreter::globalTransitionToNode(GlobalTransition* globalTransition, const std::string& xmlNs) {
- Element<std::string> transition = _flatDoc.createElementNS(xmlNs, "transition");
+Node<std::string> FlatteningInterpreter::globalTransitionToNode(GlobalTransition* globalTransition) {
+ Element<std::string> transition = _flatDoc.createElementNS(_nsInfo.nsURL, "transition");
+ _nsInfo.setPrefix(transition);
if (!globalTransition->isEventless) {
transition.setAttribute("event", globalTransition->eventDesc);
@@ -649,25 +690,26 @@ Node<std::string> FlatteningInterpreter::globalTransitionToNode(GlobalTransition
}
transition.setAttribute("prioPerLevel", nrSS.str());
#endif
-
+
transition.setAttribute("id", globalTransition->transitionId);
-
+
// std::cout << " firstPerLevel:" << feSS.str() << " " << globalTransition->transitionId << std::endl;
// std::cout << "event: " << globalTransition->eventDesc << " firstPerLevel:" << feSS.str() << " numberPerLevel:" << nrSS.str() << " prioPerLevel:" << prSS.str() << " " << globalTransition->transitionId << std::endl;
// std::cout << globalTransition->transitionId << std::endl;
-
+
NodeSet<std::string> transientStateChain;
// gather content for new transient state
NodeSet<std::string> childs;
-
+
// 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) {
- Element<std::string> onexit = _flatDoc.createElementNS(xmlNs, "onexit");
+ Element<std::string> onexit = _flatDoc.createElementNS(_nsInfo.nsURL, "onexit");
+ _nsInfo.setPrefix(onexit);
Node<std::string> child = actionIter->transition.getFirstChild();
while(child) {
Node<std::string> imported = _flatDoc.importNode(child, true);
@@ -700,7 +742,9 @@ Node<std::string> FlatteningInterpreter::globalTransitionToNode(GlobalTransition
}
if (actionIter->uninvoke) {
- Element<std::string> uninvokeElem = _flatDoc.createElementNS(xmlNs, "uninvoke");
+ 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"));
}
@@ -720,7 +764,7 @@ Node<std::string> FlatteningInterpreter::globalTransitionToNode(GlobalTransition
if (actionIter->entered) {
// 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(_xmlNSPrefix + "datamodel", actionIter->entered);
+ NodeSet<std::string> datamodel = filterChildElements(_nsInfo.xmlNSPrefix + "datamodel", actionIter->entered);
if (datamodel.size() > 0 && !isMember(actionIter->entered, _globalConf[globalTransition->source]->alreadyEnteredStates)) {
childs.push_back(datamodel);
}
@@ -740,43 +784,44 @@ Node<std::string> FlatteningInterpreter::globalTransitionToNode(GlobalTransition
}
CREATE_TRANSIENT_STATE_WITH_CHILDS
-
+
if (transientStateChain.size() > 0) {
for (int i = 0; i < transientStateChain.size(); i++) {
Element<std::string> transientStateElem = Element<std::string>(transientStateChain[i]);
transientStateElem.setAttribute("id", "transient-" + globalTransition->transitionId + "-" + globalTransition->source + "-" + toStr(i));
- Element<std::string> exitTransition = _flatDoc.createElementNS(xmlNs, "transition");
-
+ Element<std::string> exitTransition = _flatDoc.createElementNS(_nsInfo.nsURL, "transition");
+ _nsInfo.setPrefix(exitTransition);
+
if (i == transientStateChain.size() - 1) {
exitTransition.setAttribute("target", globalTransition->destination);
} else {
exitTransition.setAttribute("target", "transient-" + globalTransition->transitionId + "-" + globalTransition->source + "-" + toStr(i + 1));
}
transientStateElem.appendChild(exitTransition);
-
+
if (i == 0)
transition.setAttribute("target", transientStateElem.getAttribute("id"));
-
+
_scxml.appendChild(transientStateElem);
}
} else {
transition.setAttribute("target", globalTransition->destination);
}
-
+
return transition;
}
void FlatteningInterpreter::weightTransitions() {
maxDepth = 0;
maxOrder = 0;
-
+
int depth = 0;
Arabica::XPath::NodeSet<std::string> states = getChildStates(_scxml);
while(states.size() > 0) {
- NodeSet<std::string> transitions = filterChildElements(_xmlNSPrefix + "transition", states);
- NodeSet<std::string> initials = filterChildElements(_xmlNSPrefix + "initial", states);
- transitions.push_back(filterChildElements(_xmlNSPrefix + "transition", initials));
+ NodeSet<std::string> transitions = filterChildElements(_nsInfo.xmlNSPrefix + "transition", states);
+ NodeSet<std::string> initials = filterChildElements(_nsInfo.xmlNSPrefix + "initial", states);
+ transitions.push_back(filterChildElements(_nsInfo.xmlNSPrefix + "transition", initials));
for (int j = 0; j < transitions.size(); j++) {
if (depth > maxDepth)
@@ -791,24 +836,24 @@ void FlatteningInterpreter::weightTransitions() {
states = getChildStates(states);
}
}
-
+
void FlatteningInterpreter::labelTransitions() {
// put a unique id on each transition
Arabica::XPath::NodeSet<std::string> states = getAllStates();
states.push_back(_scxml);
for (int i = 0; i < states.size(); i++) {
std::string stateId = ATTR(states[i], "id");
- NodeSet<std::string> transitions = filterChildElements(_xmlNSPrefix + "transition", states[i]);
+ NodeSet<std::string> transitions = filterChildElements(_nsInfo.xmlNSPrefix + "transition", states[i]);
// some transitions are in the inital elements
- NodeSet<std::string> initials = filterChildElements(_xmlNSPrefix + "initial", states[i]);
- transitions.push_back(filterChildElements(_xmlNSPrefix + "transition", initials));
+ NodeSet<std::string> initials = filterChildElements(_nsInfo.xmlNSPrefix + "initial", states[i]);
+ transitions.push_back(filterChildElements(_nsInfo.xmlNSPrefix + "transition", initials));
for (int j = 0; j < transitions.size(); j++) {
Element<std::string> transition = Element<std::string>(transitions[j]);
transition.setAttribute("id", stateId + ":"+ toStr(j + 1));
}
}
}
-
+
void FlatteningInterpreter::beforeMicroStep(Interpreter interpreter) {
}
void FlatteningInterpreter::onStableConfiguration(Interpreter interpreter) {
@@ -828,31 +873,31 @@ void FlatteningInterpreter::beforeEnteringState(Interpreter interpreter, const A
void FlatteningInterpreter::beforeTakingTransition(Interpreter interpreter, const Arabica::DOM::Element<std::string>& transition, bool moreComing) {
}
-
+
GlobalState::GlobalState(const Arabica::XPath::NodeSet<std::string>& activeStates_,
- const Arabica::XPath::NodeSet<std::string>& alreadyEnteredStates_, // we need to remember for binding=late
- const std::map<std::string, Arabica::XPath::NodeSet<std::string> >& historyStates_) {
-
+ const Arabica::XPath::NodeSet<std::string>& alreadyEnteredStates_, // we need to remember for binding=late
+ const std::map<std::string, Arabica::XPath::NodeSet<std::string> >& historyStates_) {
+
// make copies and sort
activeStates = activeStates_;
alreadyEnteredStates = alreadyEnteredStates_;
historyStates = historyStates_;
isFinal = true; // is set to false if we contain a non-final state
-
+
// start state is not final
if (activeStates.size() == 0) {
isFinal = false;
}
-
+
// sort configuration
activeStates.to_document_order();
alreadyEnteredStates.to_document_order();
for(std::map<std::string, Arabica::XPath::NodeSet<std::string> >::iterator historyIter = historyStates.begin();
- historyIter != historyStates.end();
- historyIter++) {
+ historyIter != historyStates.end();
+ historyIter++) {
historyIter->second.to_document_order();
}
-
+
// create a unique identifier for a global configuration
std::ostringstream idSS;
idSS << "active-";
@@ -867,10 +912,10 @@ GlobalState::GlobalState(const Arabica::XPath::NodeSet<std::string>& activeState
idSS << ATTR(alreadyEnteredStates[i], "id") << "-";
}
idSS << ";";
-
+
for(std::map<std::string, Arabica::XPath::NodeSet<std::string> >::const_iterator histIter = historyStates.begin();
- histIter != historyStates.end();
- histIter++) {
+ histIter != historyStates.end();
+ histIter++) {
const Arabica::XPath::NodeSet<std::string>& histStates = histIter->second;
idSS << "history-";
idSS << histIter->first << "-";
@@ -878,7 +923,7 @@ GlobalState::GlobalState(const Arabica::XPath::NodeSet<std::string>& activeState
idSS << ATTR(histStates[i], "id") << "-";
}
}
-
+
stateId = idSS.str();
}
@@ -886,7 +931,7 @@ GlobalTransition::GlobalTransition(const Arabica::XPath::NodeSet<std::string>& t
transitions = transitionSet;
isValid = true;
isEventless = true;
-
+
#if 0
std::cout << "################" << std::endl;
for (int i = 0; i < transitions.size(); i++) {
@@ -894,14 +939,14 @@ GlobalTransition::GlobalTransition(const Arabica::XPath::NodeSet<std::string>& t
}
std::cout << "################" << std::endl;
#endif
-
+
std::list<std::string> conditions;
std::ostringstream setId; // also build id for subset
for (int i = 0; i < transitions.size(); i++) {
// get a unique string for the transition - we assume it is sorted
assert(HAS_ATTR(transitions[i], "id"));
setId << ATTR(transitions[i], "id") << "-";
-
+
// gather conditions while we are iterating anyway
if (HAS_ATTR(transitions[i], "cond")) {
conditions.push_back(ATTR(transitions[i], "cond"));
@@ -920,7 +965,7 @@ GlobalTransition::GlobalTransition(const Arabica::XPath::NodeSet<std::string>& t
bool foundEventLess = false;
bool foundWithTarget = false;
bool foundTargetLess = false;
-
+
for (int i = 0; i < transitions.size(); i++) {
if (HAS_ATTR(transitions[i], "eventexpr")) {
Event e("error.execution", Event::PLATFORM);
@@ -947,7 +992,7 @@ GlobalTransition::GlobalTransition(const Arabica::XPath::NodeSet<std::string>& t
}
}
-
+
// do not mix eventless and event transitions
if (foundEventLess && foundWithEvent) {
isValid = false;
@@ -962,20 +1007,20 @@ GlobalTransition::GlobalTransition(const Arabica::XPath::NodeSet<std::string>& t
isEventless = foundEventLess;
isTargetless = !foundWithTarget;
-
+
// is there a set of event names that would enable this conflict-free transition set?
if (foundWithEvent) {
// get the set of longest event descriptors that will enable this transition set
eventNames = getCommonEvents(transitions);
if (eventNames.size() == 0) {
- std::cout << "No event will activate this conflict-free subset" << std::endl;
+// LOG(INFO) << "No event will activate this conflict-free subset" << std::endl;
isValid = false;
return;
} else {
std::string seperator = "";
for (std::list<std::string>::iterator eventIter = eventNames.begin();
- eventIter != eventNames.end();
- eventIter++) {
+ eventIter != eventNames.end();
+ eventIter++) {
eventDesc += seperator + *eventIter;
seperator = " ";
}
@@ -983,35 +1028,35 @@ GlobalTransition::GlobalTransition(const Arabica::XPath::NodeSet<std::string>& t
if (eventDesc.size() == 0)
eventDesc = "*";
}
-
+
if (conditions.size() > 0) {
condition = dataModel.andExpressions(conditions);
if (condition.size() == 0) {
- std::cout << "Datamodel does not support to conjungate expressions!" << std::endl;
+ LOG(ERROR) << "Datamodel does not support to conjungate expressions!" << std::endl;
}
}
}
-
+
std::list<std::string> GlobalTransition::getCommonEvents(const NodeSet<std::string>& transitions) {
std::list<std::string> prefixes;
std::list<std::string> longestPrefixes;
-
+
for (int i = 0; i < transitions.size(); i++) {
// for every transition
std::list<std::string> eventNames = Interpreter::tokenizeIdRefs(ATTR(transitions[i], "event"));
-
+
for (std::list<std::string>::iterator eventNameIter = eventNames.begin();
- eventNameIter != eventNames.end();
- eventNameIter++) {
+ eventNameIter != eventNames.end();
+ eventNameIter++) {
// for every event descriptor
std::string eventName = *eventNameIter;
-
+
// remove trailing .*
if (eventName.find("*", eventName.size() - 1) != std::string::npos)
eventName = eventName.substr(0, eventName.size() - 1);
if (eventName.find(".", eventName.size() - 1) != std::string::npos)
eventName = eventName.substr(0, eventName.size() - 1);
-
+
bool isMatching = true;
for (int j = 0; j < transitions.size(); j++) {
// check if token would activate all other transitions
@@ -1027,20 +1072,20 @@ std::list<std::string> GlobalTransition::getCommonEvents(const NodeSet<std::stri
}
}
}
-
+
// from the set of event names, remove those that are prefixes
for (std::list<std::string>::iterator outerEventNameIter = prefixes.begin();
- outerEventNameIter != prefixes.end();
- outerEventNameIter++) {
+ outerEventNameIter != prefixes.end();
+ outerEventNameIter++) {
for (std::list<std::string>::iterator innerEventNameIter = prefixes.begin();
- innerEventNameIter != prefixes.end();
- innerEventNameIter++) {
+ innerEventNameIter != prefixes.end();
+ innerEventNameIter++) {
if (!iequals(*outerEventNameIter, *innerEventNameIter) && Interpreter::nameMatch(*outerEventNameIter, *innerEventNameIter)) {
goto IS_PREFIX;
}
}
longestPrefixes.push_back(*outerEventNameIter);
- IS_PREFIX:
+IS_PREFIX:
;
}
return longestPrefixes;