1 // Copyright (C) 2006-2015 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, or (at your option) any later version.
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
20 #include "LoadState.hxx"
23 #include "ForLoop.hxx"
24 #include "WhileLoop.hxx"
27 #include "Runtime.hxx"
28 #include "InputPort.hxx"
29 #include "ElementaryNode.hxx"
30 #include "ForEachLoop.hxx"
40 #include "YacsTrace.hxx"
42 using namespace YACS::ENGINE;
45 XMLReadState stateParser::_state;
46 std::string stateParser::_what;
47 std::stack<XMLReadState> stateParser::_stackState;
48 Proc* stateParser::_p;
49 Runtime* stateParser::_runtime;
50 std::map<std::string, YACS::StatesForNode> stateParser::_nodeStateValue;
51 std::map<std::string, YACS::StatesForNode> stateParser::_nodeStates;
53 // ----------------------------------------------------------------------------
54 void stateParser::setProc(Proc* p)
59 void stateParser::setRuntime(Runtime* runtime)
64 void stateParser::init(const xmlChar** p, xmlParserBase* father)
66 DEBTRACE("stateParser::init()");
67 _state = XMLNOCONTEXT;
69 _stackState.push(_state);
70 _nodeStateValue["READY"] =YACS::READY;
71 _nodeStateValue["TOLOAD"] =YACS::TOLOAD;
72 _nodeStateValue["LOADED"] =YACS::LOADED;
73 _nodeStateValue["TOACTIVATE"] =YACS::TOACTIVATE;
74 _nodeStateValue["ACTIVATED"] =YACS::ACTIVATED;
75 _nodeStateValue["DESACTIVATED"] =YACS::DESACTIVATED;
76 _nodeStateValue["DONE"] =YACS::DONE;
77 _nodeStateValue["SUSPENDED"] =YACS::SUSPENDED;
78 _nodeStateValue["LOADFAILED"] =YACS::LOADFAILED;
79 _nodeStateValue["EXECFAILED"] =YACS::EXECFAILED;
80 _nodeStateValue["PAUSE"] =YACS::PAUSE;
81 _nodeStateValue["INTERNALERR"] =YACS::INTERNALERR;
82 _nodeStateValue["DISABLED"] =YACS::DISABLED;
83 _nodeStateValue["FAILED"] =YACS::FAILED;
84 _nodeStateValue["ERROR"] =YACS::ERROR;
88 void stateParser::onStart (const XML_Char* elem, const xmlChar** p)
90 DEBTRACE("stateParser::onStart");
92 stateParser *parser = 0;
93 if (element == "graphState") parser = new graphParser();
96 _what = "expected <graphState>, got <" + element + ">";
97 _state = XMLFATALERROR;
102 _stackParser.push(parser);
103 XML_SetUserData(_xmlParser, parser);
109 void stateParser::onEnd (const XML_Char* name)
112 _state = _stackState.top();
113 //cerr << "end " << name << " " << _stackParser.size() << " " << _state << endl;
117 void stateParser::charData(std::string data)
119 //cerr << "data " << data << endl;
122 // ----------------------------------------------------------------------------
124 void graphParser::init(const xmlChar** p, xmlParserBase* father)
126 // DEBTRACE("graphParser::init()");
129 _stackState.push(_state);
130 if (p) getAttributes(p);
133 void graphParser::onStart (const XML_Char* elem, const xmlChar** p)
135 string element(elem);
136 stateParser *parser = 0;
137 if (element == "node") parser = new nodeParser();
140 _what = "expected <node>, got <" + element + ">";
141 _state = XMLFATALERROR;
146 _stackParser.push(parser);
147 XML_SetUserData(_xmlParser, parser);
148 parser->init(p, this);
153 void graphParser::onEnd (const XML_Char* name)
155 std::map<std::string, YACS::StatesForNode>::const_iterator it;
156 for (it = _nodeStates.begin(); it != _nodeStates.end(); it++)
159 string nodeName = it->first;
160 DEBTRACE("nodeName = " << nodeName);
161 if(_p->getName() == nodeName)
164 node = _p->getChildByName(nodeName);
166 InGate* inGate = node->getInGate();
167 list<OutGate*> backlinks = inGate->getBackLinks();
168 for (list<OutGate*>::iterator io = backlinks.begin(); io != backlinks.end(); io++)
170 Node* fromNode = (*io)->getNode();
172 if (fromNode == _p) fromName = fromNode->getName();
173 else fromName = _p->getChildName(fromNode);
174 if (_nodeStates[fromName] == YACS::DONE)
176 DEBTRACE(" fromNode = " << fromName);
177 inGate->setPrecursorDone(*io);
181 stateParser::onEnd(name);
184 // ----------------------------------------------------------------------------
186 class outputParser: public stateParser
189 virtual void init(const xmlChar** p, xmlParserBase* father=0)
191 //DEBTRACE("outputParser::init()");
192 _state = XMLNOCONTEXT;
194 YASSERT( dynamic_cast<nodeParser*> (father));
195 _stackState.push(_state);
196 if (p) getAttributes(p);
198 virtual void onStart (const XML_Char* elem, const xmlChar** p)
200 //DEBTRACE("outputParser::onStart" << elem);
201 string element(elem);
202 stateParser *parser = 0;
203 if (element == "name") parser = new attrParser();
204 else if (element == "value") parser = new valueParser();
207 _what = "expected name or value, got <" + element + ">";
208 _state = XMLFATALERROR;
213 _stackParser.push(parser);
214 XML_SetUserData(_xmlParser, parser);
215 parser->init(p, this);
218 virtual void onEnd (const XML_Char* name)
220 //DEBTRACE("outputParser::onEnd" << elem);
221 //DEBTRACE("portName: " << _mapAttrib["name"] << "value: " << _data );
222 stateParser::onEnd(name);
224 virtual void addData(std::string /*value*/)
226 //DEBTRACE("outputParser::addData" << elem);
230 // ----------------------------------------------------------------------------
232 void nodeParser::init(const xmlChar** p, xmlParserBase* father)
234 DEBTRACE("nodeParser::init()");
237 _stackState.push(_state);
238 if (p) getAttributes(p);
242 void nodeParser::onStart (const XML_Char* elem, const xmlChar** p)
244 DEBTRACE("nodeParser::onStart" << elem);
245 string element(elem);
246 stateParser *parser = 0;
247 if (element == "inputPort") parser = new portParser();
248 else if (element == "name") parser = new attrParser();
249 else if (element == "state") parser = new attrParser();
250 else if (element == "nsteps") parser = new attrParser();
251 else if (element == "nbdone") parser = new attrParser();
252 else if (element == "condition") parser = new attrParser();
253 else if (element == "outputPort") parser = new outputParser();
254 else if (element == "loopOutputPort") parser = new loopPortParser();
257 _what = "expected name, state or inputPort, got <" + element + ">";
258 _state = XMLFATALERROR;
263 _stackParser.push(parser);
264 XML_SetUserData(_xmlParser, parser);
265 parser->init(p, this);
269 void nodeParser::onEnd (const XML_Char* name)
271 string nodeName = _mapAttrib["name"];
272 string nodeType = _mapAttrib["type"];
273 string nodeState = _mapAttrib["state"];
274 DEBTRACE( "nodeName: " << nodeName << " nodeType: " << nodeType << " nodeState: " << nodeState );
275 // for (std::map< std::string, Node * >::iterator it=_p->nodeMap.begin(); it != _p->nodeMap.end(); it++)
276 // cerr << "nodeMap: " << it->first << endl;
277 _nodeStates[nodeName] = _nodeStateValue[nodeState];
279 if(_p->getName() == nodeName)
282 node = _p->getChildByName(nodeName);
284 YASSERT(_nodeStateValue.find(nodeState) != _nodeStateValue.end());
285 YACS::ENGINE::StateLoader(node, _nodeStateValue[nodeState]);
287 if (nodeType == "forLoop")
289 if (_mapAttrib.find("nsteps") == _mapAttrib.end())
291 _what = "no attribute nsteps in forLoop " + _mapAttrib["name"];
292 _state = XMLFATALERROR;
295 int nsteps = atoi(_mapAttrib["nsteps"].c_str());
297 if (_mapAttrib.find("nbdone") == _mapAttrib.end())
299 _what = "no attribute nbdone in forLoop " + _mapAttrib["name"];
300 _state = XMLFATALERROR;
303 int nbdone = atoi(_mapAttrib["nbdone"].c_str());
304 DEBTRACE("nsteps = " << nsteps << ", nbdone = " << nbdone);
306 ForLoop* loop = dynamic_cast<ForLoop*>(node);
309 _what = "node is not a ForLoop: " + _mapAttrib["name"];
310 _state = XMLFATALERROR;
313 loop->edGetNbOfTimesInputPort()->edInit(nsteps);
314 YACS::ENGINE::NbDoneLoader(loop, nbdone);
317 else if (nodeType == "whileLoop")
319 if (_mapAttrib.find("nbdone") == _mapAttrib.end())
321 _what = "no attribute nbdone in forLoop " + _mapAttrib["name"];
322 _state = XMLFATALERROR;
325 int nbdone = atoi(_mapAttrib["nbdone"].c_str());
327 if (_mapAttrib.find("condition") == _mapAttrib.end())
329 _what = "no attribute condition in forLoop " + _mapAttrib["name"];
330 _state = XMLFATALERROR;
333 bool condition = atoi(_mapAttrib["condition"].c_str());
334 DEBTRACE("condition = " << condition << ", nbdone = " << nbdone);
336 WhileLoop* loop = dynamic_cast<WhileLoop*>(node);
339 _what = "node is not a WhileLoop: " + _mapAttrib["name"];
340 _state = XMLFATALERROR;
343 loop->edGetConditionPort()->edInit(condition);
344 YACS::ENGINE::NbDoneLoader(loop, nbdone);
347 else if (nodeType == "switch")
349 if (_mapAttrib.find("condition") == _mapAttrib.end())
351 _what = "no attribute condition in switch " + _mapAttrib["name"];
352 _state = XMLFATALERROR;
355 int condition = atoi(_mapAttrib["condition"].c_str());
356 DEBTRACE("condition = " << condition);
358 Switch* mySwitch = dynamic_cast<Switch*>(node);
361 _what = "node is not a Switch: " + _mapAttrib["name"];
362 _state = XMLFATALERROR;
365 mySwitch->edGetConditionPort()->edInit(condition);
368 stateParser::onEnd(name);
371 // ----------------------------------------------------------------------------
373 void attrParser::init(const xmlChar** p, xmlParserBase* father)
375 DEBTRACE("attrParser::init()");
376 //_state = XMLINNODE;
378 _stackState.push(_state); // keep current state
379 if (p) getAttributes(p);
383 void attrParser::onStart (const XML_Char* elem, const xmlChar** p)
385 string element(elem);
386 _what = "expected nothing, got <" + element + ">";
387 _state = XMLFATALERROR;
391 void attrParser::charData(std::string data)
396 void attrParser::onEnd (const XML_Char* name)
398 // cerr << "end attrParser " << name << " " << _stackParser.size() << endl;
400 _father->setAttribute((char*)name, _attrValue);
401 stateParser::onEnd(name);
404 // ----------------------------------------------------------------------------
406 void portParser::init(const xmlChar** p, xmlParserBase* father)
408 DEBTRACE("portParser::init()");
411 YASSERT( dynamic_cast<nodeParser*> (father));
412 _stackState.push(_state);
413 if (p) getAttributes(p);
417 void portParser::onStart (const XML_Char* elem, const xmlChar** p)
419 DEBTRACE("portParser::onStart" << elem);
420 string element(elem);
421 stateParser *parser = 0;
422 if (element == "name") parser = new attrParser();
423 else if (element == "value") parser = new valueParser();
426 _what = "expected name or value, got <" + element + ">";
427 _state = XMLFATALERROR;
432 _stackParser.push(parser);
433 XML_SetUserData(_xmlParser, parser);
434 parser->init(p, this);
438 void portParser::addData(std::string value)
443 void portParser::onEnd (const XML_Char* name)
445 DEBTRACE("portName: " << _mapAttrib["name"] << "value: " << _data );
446 string nodeName = _father->getAttribute("name");
447 string nodeType = _father->getAttribute("type");
448 Node *node = _p->getChildByName(nodeName);
449 if (nodeType == "elementaryNode")
451 ElementaryNode* eNode = dynamic_cast<ElementaryNode*>(node);
453 InputPort *port = eNode->getInputPort(_mapAttrib["name"]);
455 port->edInit("XML",_data.c_str());
457 else if (nodeType == "forLoop")
459 string what="no way to set a port value on port " + _mapAttrib["name"];
460 what += " in node " + nodeName + " of type " + nodeType;
461 throw Exception(what);
463 else if (nodeType == "whileLoop")
465 string what="no way to set a port value on port " + _mapAttrib["name"];
466 what += " in node " + nodeName + " of type " + nodeType;
467 throw Exception(what);
469 else if (nodeType == "switch")
471 string what="no way to set a port value on port " + _mapAttrib["name"];
472 what += " in node " + nodeName + " of type " + nodeType;
473 throw Exception(what);
475 else if (nodeType == "foreachLoop")
477 string what="no way to set a port value on port " + _mapAttrib["name"];
478 what += " in node " + nodeName + " of type " + nodeType;
479 throw Exception(what);
483 string what="no way to set a port value on port " + _mapAttrib["name"];
484 what += " in node " + nodeName + " of type " + nodeType;
485 throw Exception(what);
488 stateParser::onEnd(name);
491 // ----------------------------------------------------------------------------
493 void valueParser::init(const xmlChar** p, xmlParserBase* father)
495 DEBTRACE("valueParser::init()");
498 _stackState.push(_state);
499 if (p) getAttributes(p);
503 void valueParser::onStart (const XML_Char* elem, const xmlChar** p)
505 string element(elem);
506 DEBTRACE("value type " << element );
507 stateParser *parser = 0;
508 if (element == "data") parser = new dataParser();
509 else if (element == "array") parser = new arrayParser();
510 else parser = new simpleTypeParser();
513 _stackParser.push(parser);
514 XML_SetUserData(_xmlParser, parser);
515 parser->init(p, this);
519 void valueParser::addData(std::string value)
521 _data = "<value>" + value + "</value>";
524 void valueParser::onEnd (const XML_Char* name)
527 _father->addData(_data);
528 string elem = (char *) name;
529 //if (elem == "value" || elem == "data" || elem == "array")
530 stateParser::onEnd(name);
531 //else YASSERT(0); //DEBTRACE("valueParser::onEnd " << elem);
534 // ----------------------------------------------------------------------------
536 void arrayParser::init(const xmlChar** p, xmlParserBase* father)
538 DEBTRACE("arrayParser::init()");
541 _stackState.push(_state);
542 if (p) getAttributes(p);
546 void arrayParser::onStart (const XML_Char* elem, const xmlChar** p)
548 string element(elem);
549 DEBTRACE("array type " << element);
550 stateParser *parser = 0;
551 if (element == "data") parser = new dataParser();
554 _what = "expected data, got <" + element + ">";
555 _state = XMLFATALERROR;
560 _stackParser.push(parser);
561 XML_SetUserData(_xmlParser, parser);
562 parser->init(p, this);
566 void arrayParser::addData(std::string value)
568 string val = "<array>" + value + "</array>";
573 void arrayParser::onEnd (const XML_Char* name)
575 // cerr << "arrayParser::onEnd " << name << endl;
576 // cerr << _data << endl;
577 _father->addData(_data);
578 stateParser::onEnd(name);
581 // ----------------------------------------------------------------------------
583 void dataParser::init(const xmlChar** p, xmlParserBase* father)
585 DEBTRACE("dataParser::init()");
588 _stackState.push(_state);
589 if (p) getAttributes(p);
593 void dataParser::onStart (const XML_Char* elem, const xmlChar** p)
595 string element(elem);
596 DEBTRACE("data type " << element );
597 stateParser *parser = 0;
598 if (element == "value") parser = new valueParser();
601 _what = "expected value, got <" + element + ">";
602 _state = XMLFATALERROR;
607 _stackParser.push(parser);
608 XML_SetUserData(_xmlParser, parser);
609 parser->init(p, this);
613 void dataParser::addData(std::string value)
615 _dataList.push_back(value);
618 void dataParser::onEnd (const XML_Char* name)
620 // cerr << "dataParser::onEnd " << name << endl;
621 string val = "<data>";
622 while (!_dataList.empty())
624 val += _dataList.front();
625 _dataList.pop_front();
628 // cerr << val << endl;
629 _father->addData(val);
630 stateParser::onEnd(name);
633 // ----------------------------------------------------------------------------
635 void simpleTypeParser::init(const xmlChar** p, xmlParserBase* father)
637 DEBTRACE("simpleTypeParser::init()");
640 _stackState.push(_state);
641 if (p) getAttributes(p);
644 void simpleTypeParser::onStart (const XML_Char* elem, const xmlChar** p)
646 string element(elem);
647 _what = "expected nothing, got <" + element + ">";
648 _state = XMLFATALERROR;
652 void simpleTypeParser::onEnd (const XML_Char* name)
654 string val = string("<") + (char*) name + ">" + _data + "</" + (char*) name +">";
656 _father->addData(val);
657 stateParser::onEnd(name);
660 void simpleTypeParser::charData(std::string data)
662 _data = _data + data;
665 // ----------------------------------------------------------------------------
667 void loopPortParser::init(const xmlChar** p, xmlParserBase* father)
669 DEBTRACE("loopPortParser::init()");
672 _stackState.push(_state);
673 if (p) getAttributes(p);
676 void loopPortParser::onStart(const XML_Char* elem, const xmlChar** p)
678 DEBTRACE("loopPortParser::onStart" << elem);
679 string element(elem);
680 stateParser *parser = 0;
681 if (element == "name") parser = new attrParser();
682 else if (element == "sample") parser = new sampleParser();
685 _what = "expected name or sample, got <" + element + ">";
686 _state = XMLFATALERROR;
691 _stackParser.push(parser);
692 XML_SetUserData(_xmlParser, parser);
693 parser->init(p, this);
697 void loopPortParser::onEnd(const XML_Char* name)
699 string portName = _mapAttrib["name"];
700 string nodeName = _father->getAttribute("name");
701 string nodeType = _father->getAttribute("type");
702 Node *node = _p->getChildByName(nodeName);
703 if (nodeType != "forEachLoop")
705 _what = "loopOutputPort attribute is not valid for node <" + nodeName + ">";
706 _state = XMLFATALERROR;
711 void loopPortParser::charData(std::string data)
715 // ----------------------------------------------------------------------------
717 void sampleParser::init(const xmlChar** p, xmlParserBase* father)
719 DEBTRACE("sampleParser::init()");
722 _stackState.push(_state);
723 if (p) getAttributes(p);
726 void sampleParser::onStart(const XML_Char* elem, const xmlChar** p)
728 DEBTRACE("sampleParser::onStart" << elem);
729 string element(elem);
730 stateParser *parser = 0;
731 if (element == "index") parser = new attrParser();
732 else if (element == "value") parser = new valueParser();
735 _what = "expected index or value, got <" + element + ">";
736 _state = XMLFATALERROR;
741 _stackParser.push(parser);
742 XML_SetUserData(_xmlParser, parser);
743 parser->init(p, this);
747 void sampleParser::onEnd(const XML_Char* name)
749 if (_mapAttrib.find("index") == _mapAttrib.end())
751 _what = "no attribute index in sample ";
752 _state = XMLFATALERROR;
755 int index = atoi(_mapAttrib["index"].c_str());
760 void sampleParser::charData(std::string data)
764 Any* sampleParser::xmlToAny() throw(ConversionException)
768 YACS::ENGINE::Any *ob=NULL;
770 doc = xmlParseMemory(_data.c_str(), _data.length());
774 msg << "Problem in conversion: XML Document not parsed successfully ";
775 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
776 throw ConversionException(msg.str());
778 cur = xmlDocGetRootElement(doc);
783 msg << "Problem in conversion: empty XML Document";
784 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
785 throw ConversionException(msg.str());
789 if ((!xmlStrcmp(cur->name, (const xmlChar *)"value")))
791 ob=convertXmlNeutral(edGetType(),doc,cur);
800 msg << "Problem in conversion: incorrect XML value";
801 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
802 throw ConversionException(msg.str());
808 // ----------------------------------------------------------------------------
810 stateLoader::stateLoader(xmlParserBase* parser,
811 YACS::ENGINE::Proc* p) : xmlReader(parser)
813 _runtime = getRuntime();
817 void stateLoader::parse(std::string xmlState)
819 DEBTRACE("stateLoader::parse");
820 stateParser *parser = dynamic_cast<stateParser*> (_rootParser);
822 parser->setRuntime(_runtime);
824 xmlReader::parse(xmlState);
826 DEBTRACE(parser->_state);
827 switch (parser->_state)
832 DEBTRACE("parse OK");
837 string what = "Abort Parse: " + parser->_what;
838 throw Exception(what);
843 string what = "Abort Parse: unknown execution problem";
844 throw Exception(what);
850 void YACS::ENGINE::loadState(YACS::ENGINE::Proc *p,const std::string& xmlStateFile)
852 DEBTRACE("YACS::ENGINE::loadState");
855 stateParser* rootParser = new stateParser();
856 stateLoader myStateLoader(rootParser, p);
857 myStateLoader.parse(xmlStateFile);