1 // Copyright (C) 2006-2016 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"
38 #include "YacsTrace.hxx"
40 using namespace YACS::ENGINE;
43 XMLReadState stateParser::_state;
44 std::string stateParser::_what;
45 std::stack<XMLReadState> stateParser::_stackState;
46 Proc* stateParser::_p;
47 Runtime* stateParser::_runtime;
48 std::map<std::string, YACS::StatesForNode> stateParser::_nodeStateValue;
49 std::map<std::string, YACS::StatesForNode> stateParser::_nodeStates;
51 // ----------------------------------------------------------------------------
52 void stateParser::setProc(Proc* p)
57 void stateParser::setRuntime(Runtime* runtime)
62 void stateParser::init(const xmlChar** p, xmlParserBase* father)
64 DEBTRACE("stateParser::init()");
65 _state = XMLNOCONTEXT;
67 _stackState.push(_state);
68 _nodeStateValue["READY"] =YACS::READY;
69 _nodeStateValue["TOLOAD"] =YACS::TOLOAD;
70 _nodeStateValue["LOADED"] =YACS::LOADED;
71 _nodeStateValue["TOACTIVATE"] =YACS::TOACTIVATE;
72 _nodeStateValue["ACTIVATED"] =YACS::ACTIVATED;
73 _nodeStateValue["DESACTIVATED"] =YACS::DESACTIVATED;
74 _nodeStateValue["DONE"] =YACS::DONE;
75 _nodeStateValue["SUSPENDED"] =YACS::SUSPENDED;
76 _nodeStateValue["LOADFAILED"] =YACS::LOADFAILED;
77 _nodeStateValue["EXECFAILED"] =YACS::EXECFAILED;
78 _nodeStateValue["PAUSE"] =YACS::PAUSE;
79 _nodeStateValue["INTERNALERR"] =YACS::INTERNALERR;
80 _nodeStateValue["DISABLED"] =YACS::DISABLED;
81 _nodeStateValue["FAILED"] =YACS::FAILED;
82 _nodeStateValue["ERROR"] =YACS::ERROR;
87 void stateParser::onStart (const XML_Char* elem, const xmlChar** p)
89 DEBTRACE("stateParser::onStart");
91 stateParser *parser = 0;
92 if (element == "graphState") parser = new graphParser();
95 _what = "expected <graphState>, got <" + element + ">";
96 _state = XMLFATALERROR;
101 _stackParser.push(parser);
102 XML_SetUserData(_xmlParser, parser);
108 void stateParser::onEnd (const XML_Char* name)
111 _state = _stackState.top();
112 //cerr << "end " << name << " " << _stackParser.size() << " " << _state << endl;
116 void stateParser::charData(std::string data)
118 //cerr << "data " << data << endl;
121 // ----------------------------------------------------------------------------
123 void graphParser::init(const xmlChar** p, xmlParserBase* father)
125 // DEBTRACE("graphParser::init()");
128 _stackState.push(_state);
129 if (p) getAttributes(p);
132 void graphParser::onStart (const XML_Char* elem, const xmlChar** p)
134 string element(elem);
135 stateParser *parser = 0;
136 if (element == "node") parser = new nodeParser();
139 _what = "expected <node>, got <" + element + ">";
140 _state = XMLFATALERROR;
145 _stackParser.push(parser);
146 XML_SetUserData(_xmlParser, parser);
147 parser->init(p, this);
152 void graphParser::onEnd (const XML_Char* name)
154 std::map<std::string, YACS::StatesForNode>::const_iterator it;
155 for (it = _nodeStates.begin(); it != _nodeStates.end(); it++)
158 string nodeName = it->first;
159 DEBTRACE("nodeName = " << nodeName);
160 if(_p->getName() == nodeName)
163 node = _p->getChildByName(nodeName);
165 InGate* inGate = node->getInGate();
166 list<OutGate*> backlinks = inGate->getBackLinks();
167 for (list<OutGate*>::iterator io = backlinks.begin(); io != backlinks.end(); io++)
169 Node* fromNode = (*io)->getNode();
171 if (fromNode == _p) fromName = fromNode->getName();
172 else fromName = _p->getChildName(fromNode);
173 if (_nodeStates[fromName] == YACS::DONE)
175 DEBTRACE(" fromNode = " << fromName);
176 inGate->setPrecursorDone(*io);
180 stateParser::onEnd(name);
183 // ----------------------------------------------------------------------------
185 class outputParser: public stateParser
188 virtual void init(const xmlChar** p, xmlParserBase* father=0)
190 //DEBTRACE("outputParser::init()");
191 _state = XMLNOCONTEXT;
193 YASSERT( dynamic_cast<nodeParser*> (father));
194 _stackState.push(_state);
195 if (p) getAttributes(p);
197 virtual void onStart (const XML_Char* elem, const xmlChar** p)
199 //DEBTRACE("outputParser::onStart" << elem);
200 string element(elem);
201 stateParser *parser = 0;
202 if (element == "name") parser = new attrParser();
203 else if (element == "value") parser = new valueParser();
206 _what = "expected name or value, got <" + element + ">";
207 _state = XMLFATALERROR;
212 _stackParser.push(parser);
213 XML_SetUserData(_xmlParser, parser);
214 parser->init(p, this);
217 virtual void onEnd (const XML_Char* name)
219 //DEBTRACE("outputParser::onEnd" << elem);
220 //DEBTRACE("portName: " << _mapAttrib["name"] << "value: " << _data );
221 stateParser::onEnd(name);
223 virtual void addData(std::string /*value*/)
225 //DEBTRACE("outputParser::addData" << elem);
229 // ----------------------------------------------------------------------------
231 void nodeParser::init(const xmlChar** p, xmlParserBase* father)
233 DEBTRACE("nodeParser::init()");
236 _stackState.push(_state);
237 if (p) getAttributes(p);
241 void nodeParser::onStart (const XML_Char* elem, const xmlChar** p)
243 DEBTRACE("nodeParser::onStart" << elem);
244 string element(elem);
245 stateParser *parser = 0;
246 if (element == "inputPort") parser = new portParser();
247 else if (element == "name") parser = new attrParser();
248 else if (element == "state") parser = new attrParser();
249 else if (element == "nsteps") parser = new attrParser();
250 else if (element == "nbdone") parser = new attrParser();
251 else if (element == "condition") parser = new attrParser();
252 else if (element == "outputPort") parser = new outputParser();
255 _what = "expected name, state or inputPort, got <" + element + ">";
256 _state = XMLFATALERROR;
261 _stackParser.push(parser);
262 XML_SetUserData(_xmlParser, parser);
263 parser->init(p, this);
267 void nodeParser::onEnd (const XML_Char* name)
269 string nodeName = _mapAttrib["name"];
270 string nodeType = _mapAttrib["type"];
271 string nodeState = _mapAttrib["state"];
272 DEBTRACE( "nodeName: " << nodeName << " nodeType: " << nodeType << " nodeState: " << nodeState );
273 // for (std::map< std::string, Node * >::iterator it=_p->nodeMap.begin(); it != _p->nodeMap.end(); it++)
274 // cerr << "nodeMap: " << it->first << endl;
275 _nodeStates[nodeName] = _nodeStateValue[nodeState];
277 if(_p->getName() == nodeName)
280 node = _p->getChildByName(nodeName);
282 YASSERT(_nodeStateValue.find(nodeState) != _nodeStateValue.end());
283 YACS::ENGINE::StateLoader(node, _nodeStateValue[nodeState]);
285 if (nodeType == "forLoop")
287 if (_mapAttrib.find("nsteps") == _mapAttrib.end())
289 _what = "no attribute nsteps in forLoop " + _mapAttrib["name"];
290 _state = XMLFATALERROR;
293 int nsteps = atoi(_mapAttrib["nsteps"].c_str());
295 if (_mapAttrib.find("nbdone") == _mapAttrib.end())
297 _what = "no attribute nbdone in forLoop " + _mapAttrib["name"];
298 _state = XMLFATALERROR;
301 int nbdone = atoi(_mapAttrib["nbdone"].c_str());
302 DEBTRACE("nsteps = " << nsteps << ", nbdone = " << nbdone);
304 ForLoop* loop = dynamic_cast<ForLoop*>(node);
307 _what = "node is not a ForLoop: " + _mapAttrib["name"];
308 _state = XMLFATALERROR;
311 loop->edGetNbOfTimesInputPort()->edInit(nsteps);
312 YACS::ENGINE::NbDoneLoader(loop, nbdone);
315 else if (nodeType == "whileLoop")
317 if (_mapAttrib.find("nbdone") == _mapAttrib.end())
319 _what = "no attribute nbdone in forLoop " + _mapAttrib["name"];
320 _state = XMLFATALERROR;
323 int nbdone = atoi(_mapAttrib["nbdone"].c_str());
325 if (_mapAttrib.find("condition") == _mapAttrib.end())
327 _what = "no attribute condition in forLoop " + _mapAttrib["name"];
328 _state = XMLFATALERROR;
331 bool condition = atoi(_mapAttrib["condition"].c_str());
332 DEBTRACE("condition = " << condition << ", nbdone = " << nbdone);
334 WhileLoop* loop = dynamic_cast<WhileLoop*>(node);
337 _what = "node is not a WhileLoop: " + _mapAttrib["name"];
338 _state = XMLFATALERROR;
341 loop->edGetConditionPort()->edInit(condition);
342 YACS::ENGINE::NbDoneLoader(loop, nbdone);
345 else if (nodeType == "switch")
347 if (_mapAttrib.find("condition") == _mapAttrib.end())
349 _what = "no attribute condition in switch " + _mapAttrib["name"];
350 _state = XMLFATALERROR;
353 int condition = atoi(_mapAttrib["condition"].c_str());
354 DEBTRACE("condition = " << condition);
356 Switch* mySwitch = dynamic_cast<Switch*>(node);
359 _what = "node is not a Switch: " + _mapAttrib["name"];
360 _state = XMLFATALERROR;
363 mySwitch->edGetConditionPort()->edInit(condition);
366 stateParser::onEnd(name);
369 // ----------------------------------------------------------------------------
371 void attrParser::init(const xmlChar** p, xmlParserBase* father)
373 DEBTRACE("attrParser::init()");
374 //_state = XMLINNODE;
376 _stackState.push(_state); // keep current state
377 if (p) getAttributes(p);
381 void attrParser::onStart (const XML_Char* elem, const xmlChar** p)
383 string element(elem);
384 _what = "expected nothing, got <" + element + ">";
385 _state = XMLFATALERROR;
389 void attrParser::charData(std::string data)
394 void attrParser::onEnd (const XML_Char* name)
396 // cerr << "end attrParser " << name << " " << _stackParser.size() << endl;
398 _father->setAttribute((char*)name, _attrValue);
399 stateParser::onEnd(name);
402 // ----------------------------------------------------------------------------
404 void portParser::init(const xmlChar** p, xmlParserBase* father)
406 DEBTRACE("portParser::init()");
409 YASSERT( dynamic_cast<nodeParser*> (father));
410 _stackState.push(_state);
411 if (p) getAttributes(p);
415 void portParser::onStart (const XML_Char* elem, const xmlChar** p)
417 DEBTRACE("portParser::onStart" << elem);
418 string element(elem);
419 stateParser *parser = 0;
420 if (element == "name") parser = new attrParser();
421 else if (element == "value") parser = new valueParser();
424 _what = "expected name or value, got <" + element + ">";
425 _state = XMLFATALERROR;
430 _stackParser.push(parser);
431 XML_SetUserData(_xmlParser, parser);
432 parser->init(p, this);
436 void portParser::addData(std::string value)
441 void portParser::onEnd (const XML_Char* name)
443 DEBTRACE("portName: " << _mapAttrib["name"] << "value: " << _data );
444 string nodeName = _father->getAttribute("name");
445 string nodeType = _father->getAttribute("type");
446 Node *node = _p->getChildByName(nodeName);
447 if (nodeType == "elementaryNode")
449 ElementaryNode* eNode = dynamic_cast<ElementaryNode*>(node);
451 InputPort *port = eNode->getInputPort(_mapAttrib["name"]);
453 port->edInit("XML",_data.c_str());
455 else if (nodeType == "forLoop")
457 string what="no way to set a port value on port " + _mapAttrib["name"];
458 what += " in node " + nodeName + " of type " + nodeType;
459 throw Exception(what);
461 else if (nodeType == "whileLoop")
463 string what="no way to set a port value on port " + _mapAttrib["name"];
464 what += " in node " + nodeName + " of type " + nodeType;
465 throw Exception(what);
467 else if (nodeType == "switch")
469 string what="no way to set a port value on port " + _mapAttrib["name"];
470 what += " in node " + nodeName + " of type " + nodeType;
471 throw Exception(what);
473 else if (nodeType == "foreachLoop")
475 string what="no way to set a port value on port " + _mapAttrib["name"];
476 what += " in node " + nodeName + " of type " + nodeType;
477 throw Exception(what);
481 string what="no way to set a port value on port " + _mapAttrib["name"];
482 what += " in node " + nodeName + " of type " + nodeType;
483 throw Exception(what);
486 stateParser::onEnd(name);
489 // ----------------------------------------------------------------------------
491 void valueParser::init(const xmlChar** p, xmlParserBase* father)
493 DEBTRACE("valueParser::init()");
496 _stackState.push(_state);
497 if (p) getAttributes(p);
501 void valueParser::onStart (const XML_Char* elem, const xmlChar** p)
503 string element(elem);
504 DEBTRACE("value type " << element );
505 stateParser *parser = 0;
506 if (element == "data") parser = new dataParser();
507 else if (element == "array") parser = new arrayParser();
508 else parser = new simpleTypeParser();
511 _stackParser.push(parser);
512 XML_SetUserData(_xmlParser, parser);
513 parser->init(p, this);
517 void valueParser::addData(std::string value)
519 _data = "<value>" + value + "</value>";
522 void valueParser::onEnd (const XML_Char* name)
525 _father->addData(_data);
526 string elem = (char *) name;
527 //if (elem == "value" || elem == "data" || elem == "array")
528 stateParser::onEnd(name);
529 //else YASSERT(0); //DEBTRACE("valueParser::onEnd " << elem);
532 // ----------------------------------------------------------------------------
534 void arrayParser::init(const xmlChar** p, xmlParserBase* father)
536 DEBTRACE("arrayParser::init()");
539 _stackState.push(_state);
540 if (p) getAttributes(p);
544 void arrayParser::onStart (const XML_Char* elem, const xmlChar** p)
546 string element(elem);
547 DEBTRACE("array type " << element);
548 stateParser *parser = 0;
549 if (element == "data") parser = new dataParser();
552 _what = "expected data, got <" + element + ">";
553 _state = XMLFATALERROR;
558 _stackParser.push(parser);
559 XML_SetUserData(_xmlParser, parser);
560 parser->init(p, this);
564 void arrayParser::addData(std::string value)
566 string val = "<array>" + value + "</array>";
571 void arrayParser::onEnd (const XML_Char* name)
573 // cerr << "arrayParser::onEnd " << name << endl;
574 // cerr << _data << endl;
575 _father->addData(_data);
576 stateParser::onEnd(name);
579 // ----------------------------------------------------------------------------
581 void dataParser::init(const xmlChar** p, xmlParserBase* father)
583 DEBTRACE("dataParser::init()");
586 _stackState.push(_state);
587 if (p) getAttributes(p);
591 void dataParser::onStart (const XML_Char* elem, const xmlChar** p)
593 string element(elem);
594 DEBTRACE("data type " << element );
595 stateParser *parser = 0;
596 if (element == "value") parser = new valueParser();
599 _what = "expected value, got <" + element + ">";
600 _state = XMLFATALERROR;
605 _stackParser.push(parser);
606 XML_SetUserData(_xmlParser, parser);
607 parser->init(p, this);
611 void dataParser::addData(std::string value)
613 _dataList.push_back(value);
616 void dataParser::onEnd (const XML_Char* name)
618 // cerr << "dataParser::onEnd " << name << endl;
619 string val = "<data>";
620 while (!_dataList.empty())
622 val += _dataList.front();
623 _dataList.pop_front();
626 // cerr << val << endl;
627 _father->addData(val);
628 stateParser::onEnd(name);
631 // ----------------------------------------------------------------------------
633 void simpleTypeParser::init(const xmlChar** p, xmlParserBase* father)
635 DEBTRACE("simpleTypeParser::init()");
638 _stackState.push(_state);
639 if (p) getAttributes(p);
642 void simpleTypeParser::onStart (const XML_Char* elem, const xmlChar** p)
644 string element(elem);
645 _what = "expected nothing, got <" + element + ">";
646 _state = XMLFATALERROR;
650 void simpleTypeParser::onEnd (const XML_Char* name)
652 string val = string("<") + (char*) name + ">" + _data + "</" + (char*) name +">";
654 _father->addData(val);
655 stateParser::onEnd(name);
658 void simpleTypeParser::charData(std::string data)
660 _data = _data + data;
665 // ----------------------------------------------------------------------------
667 stateLoader::stateLoader(xmlParserBase* parser,
668 YACS::ENGINE::Proc* p) : xmlReader(parser)
670 _runtime = getRuntime();
674 void stateLoader::parse(std::string xmlState)
676 DEBTRACE("stateLoader::parse");
677 stateParser *parser = dynamic_cast<stateParser*> (_rootParser);
679 parser->setRuntime(_runtime);
681 xmlReader::parse(xmlState);
683 DEBTRACE(parser->_state);
684 switch (parser->_state)
689 DEBTRACE("parse OK");
694 string what = "Abort Parse: " + parser->_what;
695 throw Exception(what);
700 string what = "Abort Parse: unknown execution problem";
701 throw Exception(what);
707 void YACS::ENGINE::loadState(YACS::ENGINE::Proc *p,const std::string& xmlStateFile)
709 DEBTRACE("YACS::ENGINE::loadState");
712 stateParser* rootParser = new stateParser();
713 stateLoader myStateLoader(rootParser, p);
714 myStateLoader.parse(xmlStateFile);