1 // Copyright (C) 2006-2008 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "LoadState.hxx"
22 #include "ForLoop.hxx"
23 #include "WhileLoop.hxx"
26 #include "Runtime.hxx"
27 #include "InputPort.hxx"
28 #include "ElementaryNode.hxx"
37 #include "YacsTrace.hxx"
39 using namespace YACS::ENGINE;
42 XMLReadState stateParser::_state;
43 std::string stateParser::_what;
44 std::stack<XMLReadState> stateParser::_stackState;
45 Proc* stateParser::_p;
46 Runtime* stateParser::_runtime;
47 std::map<std::string, YACS::StatesForNode> stateParser::_nodeStateValue;
48 std::map<std::string, YACS::StatesForNode> stateParser::_nodeStates;
50 // ----------------------------------------------------------------------------
51 void stateParser::setProc(Proc* p)
56 void stateParser::setRuntime(Runtime* runtime)
61 void stateParser::init(const xmlChar** p, xmlParserBase* father)
63 DEBTRACE("stateParser::init()");
64 _state = XMLNOCONTEXT;
66 _stackState.push(_state);
67 _nodeStateValue["READY"] =YACS::READY;
68 _nodeStateValue["TOLOAD"] =YACS::TOLOAD;
69 _nodeStateValue["LOADED"] =YACS::LOADED;
70 _nodeStateValue["TOACTIVATE"] =YACS::TOACTIVATE;
71 _nodeStateValue["ACTIVATED"] =YACS::ACTIVATED;
72 _nodeStateValue["DESACTIVATED"] =YACS::DESACTIVATED;
73 _nodeStateValue["DONE"] =YACS::DONE;
74 _nodeStateValue["SUSPENDED"] =YACS::SUSPENDED;
75 _nodeStateValue["LOADFAILED"] =YACS::LOADFAILED;
76 _nodeStateValue["EXECFAILED"] =YACS::EXECFAILED;
77 _nodeStateValue["PAUSE"] =YACS::PAUSE;
78 _nodeStateValue["INTERNALERR"] =YACS::INTERNALERR;
79 _nodeStateValue["DISABLED"] =YACS::DISABLED;
80 _nodeStateValue["FAILED"] =YACS::FAILED;
81 _nodeStateValue["ERROR"] =YACS::ERROR;
85 void stateParser::onStart (const XML_Char* elem, const xmlChar** p)
87 DEBTRACE("stateParser::onStart");
89 stateParser *parser = 0;
90 if (element == "graphState") parser = new graphParser();
93 _what = "expected <graphState>, got <" + element + ">";
94 _state = XMLFATALERROR;
99 _stackParser.push(parser);
100 XML_SetUserData(_xmlParser, parser);
106 void stateParser::onEnd (const XML_Char* name)
109 _state = _stackState.top();
110 //cerr << "end " << name << " " << _stackParser.size() << " " << _state << endl;
114 void stateParser::charData(std::string data)
116 //cerr << "data " << data << endl;
119 // ----------------------------------------------------------------------------
121 void graphParser::init(const xmlChar** p, xmlParserBase* father)
123 // DEBTRACE("graphParser::init()");
126 _stackState.push(_state);
127 if (p) getAttributes(p);
130 void graphParser::onStart (const XML_Char* elem, const xmlChar** p)
132 string element(elem);
133 stateParser *parser = 0;
134 if (element == "node") parser = new nodeParser();
137 _what = "expected <node>, got <" + element + ">";
138 _state = XMLFATALERROR;
143 _stackParser.push(parser);
144 XML_SetUserData(_xmlParser, parser);
145 parser->init(p, this);
150 void graphParser::onEnd (const XML_Char* name)
152 std::map<std::string, YACS::StatesForNode>::const_iterator it;
153 for (it = _nodeStates.begin(); it != _nodeStates.end(); it++)
156 string nodeName = it->first;
157 DEBTRACE("nodeName = " << nodeName);
158 if(_p->getName() == nodeName)
161 node = _p->getChildByName(nodeName);
163 InGate* inGate = node->getInGate();
164 list<OutGate*> backlinks = inGate->getBackLinks();
165 for (list<OutGate*>::iterator io = backlinks.begin(); io != backlinks.end(); io++)
167 Node* fromNode = (*io)->getNode();
169 if (fromNode == _p) fromName = fromNode->getName();
170 else fromName = _p->getChildName(fromNode);
171 if (_nodeStates[fromName] == YACS::DONE)
173 DEBTRACE(" fromNode = " << fromName);
174 inGate->setPrecursorDone(*io);
178 stateParser::onEnd(name);
181 // ----------------------------------------------------------------------------
183 void nodeParser::init(const xmlChar** p, xmlParserBase* father)
185 DEBTRACE("nodeParser::init()");
188 _stackState.push(_state);
189 if (p) getAttributes(p);
193 void nodeParser::onStart (const XML_Char* elem, const xmlChar** p)
195 DEBTRACE("nodeParser::onStart" << elem);
196 string element(elem);
197 stateParser *parser = 0;
198 if (element == "inputPort") parser = new portParser();
199 else if (element == "name") parser = new attrParser();
200 else if (element == "state") parser = new attrParser();
201 else if (element == "nsteps") parser = new attrParser();
202 else if (element == "nbdone") parser = new attrParser();
203 else if (element == "condition") parser = new attrParser();
204 else if (element == "inputPort") parser = new portParser();
207 _what = "expected name, state or inputPort, got <" + element + ">";
208 _state = XMLFATALERROR;
213 _stackParser.push(parser);
214 XML_SetUserData(_xmlParser, parser);
215 parser->init(p, this);
219 void nodeParser::onEnd (const XML_Char* name)
221 string nodeName = _mapAttrib["name"];
222 string nodeType = _mapAttrib["type"];
223 string nodeState = _mapAttrib["state"];
224 DEBTRACE( "nodeName: " << nodeName << " nodeType: " << nodeType << " nodeState: " << nodeState );
225 // for (std::map< std::string, Node * >::iterator it=_p->nodeMap.begin(); it != _p->nodeMap.end(); it++)
226 // cerr << "nodeMap: " << it->first << endl;
227 _nodeStates[nodeName] = _nodeStateValue[nodeState];
229 if(_p->getName() == nodeName)
232 node = _p->getChildByName(nodeName);
234 assert(_nodeStateValue.find(nodeState) != _nodeStateValue.end());
235 YACS::ENGINE::StateLoader(node, _nodeStateValue[nodeState]);
237 if (nodeType == "forLoop")
239 if (_mapAttrib.find("nsteps") == _mapAttrib.end())
241 _what = "no attribute nsteps in forLoop " + _mapAttrib["name"];
242 _state = XMLFATALERROR;
245 int nsteps = atoi(_mapAttrib["nsteps"].c_str());
247 if (_mapAttrib.find("nbdone") == _mapAttrib.end())
249 _what = "no attribute nbdone in forLoop " + _mapAttrib["name"];
250 _state = XMLFATALERROR;
253 int nbdone = atoi(_mapAttrib["nbdone"].c_str());
254 DEBTRACE("nsteps = " << nsteps << ", nbdone = " << nbdone);
256 ForLoop* loop = dynamic_cast<ForLoop*>(node);
259 _what = "node is not a ForLoop: " + _mapAttrib["name"];
260 _state = XMLFATALERROR;
263 loop->edGetNbOfTimesInputPort()->edInit(nsteps);
264 YACS::ENGINE::NbDoneLoader(loop, nbdone);
267 else if (nodeType == "whileLoop")
269 if (_mapAttrib.find("nbdone") == _mapAttrib.end())
271 _what = "no attribute nbdone in forLoop " + _mapAttrib["name"];
272 _state = XMLFATALERROR;
275 int nbdone = atoi(_mapAttrib["nbdone"].c_str());
277 if (_mapAttrib.find("condition") == _mapAttrib.end())
279 _what = "no attribute condition in forLoop " + _mapAttrib["name"];
280 _state = XMLFATALERROR;
283 bool condition = atoi(_mapAttrib["condition"].c_str());
284 DEBTRACE("condition = " << condition << ", nbdone = " << nbdone);
286 WhileLoop* loop = dynamic_cast<WhileLoop*>(node);
289 _what = "node is not a WhileLoop: " + _mapAttrib["name"];
290 _state = XMLFATALERROR;
293 loop->edGetConditionPort()->edInit(condition);
294 YACS::ENGINE::NbDoneLoader(loop, nbdone);
297 else if (nodeType == "switch")
299 if (_mapAttrib.find("condition") == _mapAttrib.end())
301 _what = "no attribute condition in switch " + _mapAttrib["name"];
302 _state = XMLFATALERROR;
305 int condition = atoi(_mapAttrib["condition"].c_str());
306 DEBTRACE("condition = " << condition);
308 Switch* mySwitch = dynamic_cast<Switch*>(node);
311 _what = "node is not a Switch: " + _mapAttrib["name"];
312 _state = XMLFATALERROR;
315 mySwitch->edGetConditionPort()->edInit(condition);
318 stateParser::onEnd(name);
321 // ----------------------------------------------------------------------------
323 void attrParser::init(const xmlChar** p, xmlParserBase* father)
325 DEBTRACE("attrParser::init()");
326 //_state = XMLINNODE;
328 _stackState.push(_state); // keep current state
329 if (p) getAttributes(p);
333 void attrParser::onStart (const XML_Char* elem, const xmlChar** p)
335 string element(elem);
336 _what = "expected nothing, got <" + element + ">";
337 _state = XMLFATALERROR;
341 void attrParser::charData(std::string data)
346 void attrParser::onEnd (const XML_Char* name)
348 // cerr << "end attrParser " << name << " " << _stackParser.size() << endl;
350 _father->setAttribute((char*)name, _attrValue);
351 stateParser::onEnd(name);
354 // ----------------------------------------------------------------------------
356 void portParser::init(const xmlChar** p, xmlParserBase* father)
358 DEBTRACE("portParser::init()");
361 assert( dynamic_cast<nodeParser*> (father));
362 _stackState.push(_state);
363 if (p) getAttributes(p);
367 void portParser::onStart (const XML_Char* elem, const xmlChar** p)
369 DEBTRACE("portParser::onStart" << elem);
370 string element(elem);
371 stateParser *parser = 0;
372 if (element == "name") parser = new attrParser();
373 else if (element == "value") parser = new valueParser();
376 _what = "expected name or value, got <" + element + ">";
377 _state = XMLFATALERROR;
382 _stackParser.push(parser);
383 XML_SetUserData(_xmlParser, parser);
384 parser->init(p, this);
388 void portParser::addData(std::string value)
393 void portParser::onEnd (const XML_Char* name)
395 DEBTRACE("portName: " << _mapAttrib["name"] << "value: " << _data );
396 string nodeName = _father->getAttribute("name");
397 string nodeType = _father->getAttribute("type");
398 Node *node = _p->getChildByName(nodeName);
399 if (nodeType == "elementaryNode")
401 ElementaryNode* eNode = dynamic_cast<ElementaryNode*>(node);
403 InputPort *port = eNode->getInputPort(_mapAttrib["name"]);
405 port->edInit("XML",_data.c_str());
407 else if (nodeType == "forLoop")
409 string what="no way to set a port value on port " + _mapAttrib["name"];
410 what += " in node " + nodeName + " of type " + nodeType;
411 throw Exception(what);
413 else if (nodeType == "whileLoop")
415 string what="no way to set a port value on port " + _mapAttrib["name"];
416 what += " in node " + nodeName + " of type " + nodeType;
417 throw Exception(what);
419 else if (nodeType == "switch")
421 string what="no way to set a port value on port " + _mapAttrib["name"];
422 what += " in node " + nodeName + " of type " + nodeType;
423 throw Exception(what);
425 else if (nodeType == "foreachLoop")
427 string what="no way to set a port value on port " + _mapAttrib["name"];
428 what += " in node " + nodeName + " of type " + nodeType;
429 throw Exception(what);
433 string what="no way to set a port value on port " + _mapAttrib["name"];
434 what += " in node " + nodeName + " of type " + nodeType;
435 throw Exception(what);
438 stateParser::onEnd(name);
441 // ----------------------------------------------------------------------------
443 void valueParser::init(const xmlChar** p, xmlParserBase* father)
445 DEBTRACE("valueParser::init()");
448 _stackState.push(_state);
449 if (p) getAttributes(p);
453 void valueParser::onStart (const XML_Char* elem, const xmlChar** p)
455 string element(elem);
456 DEBTRACE("value type " << element );
457 stateParser *parser = 0;
458 if (element == "data") parser = new dataParser();
459 else if (element == "array") parser = new arrayParser();
460 else parser = new simpleTypeParser();
463 _stackParser.push(parser);
464 XML_SetUserData(_xmlParser, parser);
465 parser->init(p, this);
469 void valueParser::addData(std::string value)
471 _data = "<value>" + value + "</value>";
474 void valueParser::onEnd (const XML_Char* name)
477 _father->addData(_data);
478 string elem = (char *) name;
479 //if (elem == "value" || elem == "data" || elem == "array")
480 stateParser::onEnd(name);
481 //else assert(0); //DEBTRACE("valueParser::onEnd " << elem);
484 // ----------------------------------------------------------------------------
486 void arrayParser::init(const xmlChar** p, xmlParserBase* father)
488 DEBTRACE("arrayParser::init()");
491 _stackState.push(_state);
492 if (p) getAttributes(p);
496 void arrayParser::onStart (const XML_Char* elem, const xmlChar** p)
498 string element(elem);
499 DEBTRACE("array type " << element);
500 stateParser *parser = 0;
501 if (element == "data") parser = new dataParser();
504 _what = "expected data, got <" + element + ">";
505 _state = XMLFATALERROR;
510 _stackParser.push(parser);
511 XML_SetUserData(_xmlParser, parser);
512 parser->init(p, this);
516 void arrayParser::addData(std::string value)
518 string val = "<array>" + value + "</array>";
523 void arrayParser::onEnd (const XML_Char* name)
525 // cerr << "arrayParser::onEnd " << name << endl;
526 // cerr << _data << endl;
527 _father->addData(_data);
528 stateParser::onEnd(name);
531 // ----------------------------------------------------------------------------
533 void dataParser::init(const xmlChar** p, xmlParserBase* father)
535 DEBTRACE("dataParser::init()");
538 _stackState.push(_state);
539 if (p) getAttributes(p);
543 void dataParser::onStart (const XML_Char* elem, const xmlChar** p)
545 string element(elem);
546 DEBTRACE("data type " << element );
547 stateParser *parser = 0;
548 if (element == "value") parser = new valueParser();
551 _what = "expected value, got <" + element + ">";
552 _state = XMLFATALERROR;
557 _stackParser.push(parser);
558 XML_SetUserData(_xmlParser, parser);
559 parser->init(p, this);
563 void dataParser::addData(std::string value)
565 _dataList.push_back(value);
568 void dataParser::onEnd (const XML_Char* name)
570 // cerr << "dataParser::onEnd " << name << endl;
571 string val = "<data>";
572 while (!_dataList.empty())
574 val += _dataList.front();
575 _dataList.pop_front();
578 // cerr << val << endl;
579 _father->addData(val);
580 stateParser::onEnd(name);
583 // ----------------------------------------------------------------------------
585 void simpleTypeParser::init(const xmlChar** p, xmlParserBase* father)
587 DEBTRACE("simpleTypeParser::init()");
590 _stackState.push(_state);
591 if (p) getAttributes(p);
594 void simpleTypeParser::onStart (const XML_Char* elem, const xmlChar** p)
596 string element(elem);
597 _what = "expected nothing, got <" + element + ">";
598 _state = XMLFATALERROR;
602 void simpleTypeParser::onEnd (const XML_Char* name)
604 string val = string("<") + (char*) name + ">" + _data + "</" + (char*) name +">";
606 _father->addData(val);
607 stateParser::onEnd(name);
610 void simpleTypeParser::charData(std::string data)
612 _data = _data + data;
617 // ----------------------------------------------------------------------------
619 stateLoader::stateLoader(xmlParserBase* parser,
620 YACS::ENGINE::Proc* p) : xmlReader(parser)
622 _runtime = getRuntime();
626 void stateLoader::parse(std::string xmlState)
628 DEBTRACE("stateLoader::parse");
629 stateParser *parser = dynamic_cast<stateParser*> (_rootParser);
631 parser->setRuntime(_runtime);
633 xmlReader::parse(xmlState);
635 DEBTRACE(parser->_state);
636 switch (parser->_state)
641 DEBTRACE("parse OK");
646 string what = "Abort Parse: " + parser->_what;
647 throw Exception(what);
652 string what = "Abort Parse: unknown execution problem";
653 throw Exception(what);
659 void YACS::ENGINE::loadState(YACS::ENGINE::Proc *p,const std::string& xmlStateFile)
661 DEBTRACE("YACS::ENGINE::loadState");
664 stateParser* rootParser = new stateParser();
665 stateLoader myStateLoader(rootParser, p);
666 myStateLoader.parse(xmlStateFile);