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;
86 void stateParser::onStart (const XML_Char* elem, const xmlChar** p)
88 DEBTRACE("stateParser::onStart");
90 stateParser *parser = 0;
91 if (element == "graphState") parser = new graphParser();
94 _what = "expected <graphState>, got <" + element + ">";
95 _state = XMLFATALERROR;
100 _stackParser.push(parser);
101 XML_SetUserData(_xmlParser, parser);
107 void stateParser::onEnd (const XML_Char* name)
110 _state = _stackState.top();
111 //cerr << "end " << name << " " << _stackParser.size() << " " << _state << endl;
115 void stateParser::charData(std::string data)
117 //cerr << "data " << data << endl;
120 // ----------------------------------------------------------------------------
122 void graphParser::init(const xmlChar** p, xmlParserBase* father)
124 // DEBTRACE("graphParser::init()");
127 _stackState.push(_state);
128 if (p) getAttributes(p);
131 void graphParser::onStart (const XML_Char* elem, const xmlChar** p)
133 string element(elem);
134 stateParser *parser = 0;
135 if (element == "node") parser = new nodeParser();
138 _what = "expected <node>, got <" + element + ">";
139 _state = XMLFATALERROR;
144 _stackParser.push(parser);
145 XML_SetUserData(_xmlParser, parser);
146 parser->init(p, this);
151 void graphParser::onEnd (const XML_Char* name)
153 std::map<std::string, YACS::StatesForNode>::const_iterator it;
154 for (it = _nodeStates.begin(); it != _nodeStates.end(); it++)
157 string nodeName = it->first;
158 DEBTRACE("nodeName = " << nodeName);
159 if(_p->getName() == nodeName)
162 node = _p->getChildByName(nodeName);
164 InGate* inGate = node->getInGate();
165 list<OutGate*> backlinks = inGate->getBackLinks();
166 for (list<OutGate*>::iterator io = backlinks.begin(); io != backlinks.end(); io++)
168 Node* fromNode = (*io)->getNode();
170 if (fromNode == _p) fromName = fromNode->getName();
171 else fromName = _p->getChildName(fromNode);
172 if (_nodeStates[fromName] == YACS::DONE)
174 DEBTRACE(" fromNode = " << fromName);
175 inGate->setPrecursorDone(*io);
179 stateParser::onEnd(name);
182 // ----------------------------------------------------------------------------
184 class outputParser: public stateParser
187 virtual void init(const xmlChar** p, xmlParserBase* father=0)
189 //DEBTRACE("outputParser::init()");
190 _state = XMLNOCONTEXT;
192 YASSERT( dynamic_cast<nodeParser*> (father));
193 _stackState.push(_state);
194 if (p) getAttributes(p);
196 virtual void onStart (const XML_Char* elem, const xmlChar** p)
198 //DEBTRACE("outputParser::onStart" << elem);
199 string element(elem);
200 stateParser *parser = 0;
201 if (element == "name") parser = new attrParser();
202 else if (element == "value") parser = new valueParser();
205 _what = "expected name or value, got <" + element + ">";
206 _state = XMLFATALERROR;
211 _stackParser.push(parser);
212 XML_SetUserData(_xmlParser, parser);
213 parser->init(p, this);
216 virtual void onEnd (const XML_Char* name)
218 //DEBTRACE("outputParser::onEnd" << elem);
219 //DEBTRACE("portName: " << _mapAttrib["name"] << "value: " << _data );
220 stateParser::onEnd(name);
222 virtual void addData(std::string /*value*/)
224 //DEBTRACE("outputParser::addData" << elem);
228 // ----------------------------------------------------------------------------
230 void nodeParser::init(const xmlChar** p, xmlParserBase* father)
232 DEBTRACE("nodeParser::init()");
235 _stackState.push(_state);
236 if (p) getAttributes(p);
240 void nodeParser::onStart (const XML_Char* elem, const xmlChar** p)
242 DEBTRACE("nodeParser::onStart" << elem);
243 string element(elem);
244 stateParser *parser = 0;
245 if (element == "inputPort") parser = new portParser();
246 else if (element == "name") parser = new attrParser();
247 else if (element == "state") parser = new attrParser();
248 else if (element == "nsteps") parser = new attrParser();
249 else if (element == "nbdone") parser = new attrParser();
250 else if (element == "condition") parser = new attrParser();
251 else if (element == "outputPort") parser = new outputParser();
254 _what = "expected name, state or inputPort, got <" + element + ">";
255 _state = XMLFATALERROR;
260 _stackParser.push(parser);
261 XML_SetUserData(_xmlParser, parser);
262 parser->init(p, this);
266 void nodeParser::onEnd (const XML_Char* name)
268 string nodeName = _mapAttrib["name"];
269 string nodeType = _mapAttrib["type"];
270 string nodeState = _mapAttrib["state"];
271 DEBTRACE( "nodeName: " << nodeName << " nodeType: " << nodeType << " nodeState: " << nodeState );
272 // for (std::map< std::string, Node * >::iterator it=_p->nodeMap.begin(); it != _p->nodeMap.end(); it++)
273 // cerr << "nodeMap: " << it->first << endl;
274 _nodeStates[nodeName] = _nodeStateValue[nodeState];
276 if(_p->getName() == nodeName)
279 node = _p->getChildByName(nodeName);
281 YASSERT(_nodeStateValue.find(nodeState) != _nodeStateValue.end());
282 YACS::ENGINE::StateLoader(node, _nodeStateValue[nodeState]);
284 if (nodeType == "forLoop")
286 if (_mapAttrib.find("nsteps") == _mapAttrib.end())
288 _what = "no attribute nsteps in forLoop " + _mapAttrib["name"];
289 _state = XMLFATALERROR;
292 int nsteps = atoi(_mapAttrib["nsteps"].c_str());
294 if (_mapAttrib.find("nbdone") == _mapAttrib.end())
296 _what = "no attribute nbdone in forLoop " + _mapAttrib["name"];
297 _state = XMLFATALERROR;
300 int nbdone = atoi(_mapAttrib["nbdone"].c_str());
301 DEBTRACE("nsteps = " << nsteps << ", nbdone = " << nbdone);
303 ForLoop* loop = dynamic_cast<ForLoop*>(node);
306 _what = "node is not a ForLoop: " + _mapAttrib["name"];
307 _state = XMLFATALERROR;
310 loop->edGetNbOfTimesInputPort()->edInit(nsteps);
311 YACS::ENGINE::NbDoneLoader(loop, nbdone);
314 else if (nodeType == "whileLoop")
316 if (_mapAttrib.find("nbdone") == _mapAttrib.end())
318 _what = "no attribute nbdone in forLoop " + _mapAttrib["name"];
319 _state = XMLFATALERROR;
322 int nbdone = atoi(_mapAttrib["nbdone"].c_str());
324 if (_mapAttrib.find("condition") == _mapAttrib.end())
326 _what = "no attribute condition in forLoop " + _mapAttrib["name"];
327 _state = XMLFATALERROR;
330 bool condition = atoi(_mapAttrib["condition"].c_str());
331 DEBTRACE("condition = " << condition << ", nbdone = " << nbdone);
333 WhileLoop* loop = dynamic_cast<WhileLoop*>(node);
336 _what = "node is not a WhileLoop: " + _mapAttrib["name"];
337 _state = XMLFATALERROR;
340 loop->edGetConditionPort()->edInit(condition);
341 YACS::ENGINE::NbDoneLoader(loop, nbdone);
344 else if (nodeType == "switch")
346 if (_mapAttrib.find("condition") == _mapAttrib.end())
348 _what = "no attribute condition in switch " + _mapAttrib["name"];
349 _state = XMLFATALERROR;
352 int condition = atoi(_mapAttrib["condition"].c_str());
353 DEBTRACE("condition = " << condition);
355 Switch* mySwitch = dynamic_cast<Switch*>(node);
358 _what = "node is not a Switch: " + _mapAttrib["name"];
359 _state = XMLFATALERROR;
362 mySwitch->edGetConditionPort()->edInit(condition);
365 stateParser::onEnd(name);
368 // ----------------------------------------------------------------------------
370 void attrParser::init(const xmlChar** p, xmlParserBase* father)
372 DEBTRACE("attrParser::init()");
373 //_state = XMLINNODE;
375 _stackState.push(_state); // keep current state
376 if (p) getAttributes(p);
380 void attrParser::onStart (const XML_Char* elem, const xmlChar** p)
382 string element(elem);
383 _what = "expected nothing, got <" + element + ">";
384 _state = XMLFATALERROR;
388 void attrParser::charData(std::string data)
393 void attrParser::onEnd (const XML_Char* name)
395 // cerr << "end attrParser " << name << " " << _stackParser.size() << endl;
397 _father->setAttribute((char*)name, _attrValue);
398 stateParser::onEnd(name);
401 // ----------------------------------------------------------------------------
403 void portParser::init(const xmlChar** p, xmlParserBase* father)
405 DEBTRACE("portParser::init()");
408 YASSERT( dynamic_cast<nodeParser*> (father));
409 _stackState.push(_state);
410 if (p) getAttributes(p);
414 void portParser::onStart (const XML_Char* elem, const xmlChar** p)
416 DEBTRACE("portParser::onStart" << elem);
417 string element(elem);
418 stateParser *parser = 0;
419 if (element == "name") parser = new attrParser();
420 else if (element == "value") parser = new valueParser();
423 _what = "expected name or value, got <" + element + ">";
424 _state = XMLFATALERROR;
429 _stackParser.push(parser);
430 XML_SetUserData(_xmlParser, parser);
431 parser->init(p, this);
435 void portParser::addData(std::string value)
440 void portParser::onEnd (const XML_Char* name)
442 DEBTRACE("portName: " << _mapAttrib["name"] << "value: " << _data );
443 string nodeName = _father->getAttribute("name");
444 string nodeType = _father->getAttribute("type");
445 Node *node = _p->getChildByName(nodeName);
446 if (nodeType == "elementaryNode")
448 ElementaryNode* eNode = dynamic_cast<ElementaryNode*>(node);
450 InputPort *port = eNode->getInputPort(_mapAttrib["name"]);
452 port->edInit("XML",_data.c_str());
454 else if (nodeType == "forLoop")
456 string what="no way to set a port value on port " + _mapAttrib["name"];
457 what += " in node " + nodeName + " of type " + nodeType;
458 throw Exception(what);
460 else if (nodeType == "whileLoop")
462 string what="no way to set a port value on port " + _mapAttrib["name"];
463 what += " in node " + nodeName + " of type " + nodeType;
464 throw Exception(what);
466 else if (nodeType == "switch")
468 string what="no way to set a port value on port " + _mapAttrib["name"];
469 what += " in node " + nodeName + " of type " + nodeType;
470 throw Exception(what);
472 else if (nodeType == "foreachLoop")
474 string what="no way to set a port value on port " + _mapAttrib["name"];
475 what += " in node " + nodeName + " of type " + nodeType;
476 throw Exception(what);
480 string what="no way to set a port value on port " + _mapAttrib["name"];
481 what += " in node " + nodeName + " of type " + nodeType;
482 throw Exception(what);
485 stateParser::onEnd(name);
488 // ----------------------------------------------------------------------------
490 void valueParser::init(const xmlChar** p, xmlParserBase* father)
492 DEBTRACE("valueParser::init()");
495 _stackState.push(_state);
496 if (p) getAttributes(p);
500 void valueParser::onStart (const XML_Char* elem, const xmlChar** p)
502 string element(elem);
503 DEBTRACE("value type " << element );
504 stateParser *parser = 0;
505 if (element == "data") parser = new dataParser();
506 else if (element == "array") parser = new arrayParser();
507 else parser = new simpleTypeParser();
510 _stackParser.push(parser);
511 XML_SetUserData(_xmlParser, parser);
512 parser->init(p, this);
516 void valueParser::addData(std::string value)
518 _data = "<value>" + value + "</value>";
521 void valueParser::onEnd (const XML_Char* name)
524 _father->addData(_data);
525 string elem = (char *) name;
526 //if (elem == "value" || elem == "data" || elem == "array")
527 stateParser::onEnd(name);
528 //else YASSERT(0); //DEBTRACE("valueParser::onEnd " << elem);
531 // ----------------------------------------------------------------------------
533 void arrayParser::init(const xmlChar** p, xmlParserBase* father)
535 DEBTRACE("arrayParser::init()");
538 _stackState.push(_state);
539 if (p) getAttributes(p);
543 void arrayParser::onStart (const XML_Char* elem, const xmlChar** p)
545 string element(elem);
546 DEBTRACE("array type " << element);
547 stateParser *parser = 0;
548 if (element == "data") parser = new dataParser();
551 _what = "expected data, got <" + element + ">";
552 _state = XMLFATALERROR;
557 _stackParser.push(parser);
558 XML_SetUserData(_xmlParser, parser);
559 parser->init(p, this);
563 void arrayParser::addData(std::string value)
565 string val = "<array>" + value + "</array>";
570 void arrayParser::onEnd (const XML_Char* name)
572 // cerr << "arrayParser::onEnd " << name << endl;
573 // cerr << _data << endl;
574 _father->addData(_data);
575 stateParser::onEnd(name);
578 // ----------------------------------------------------------------------------
580 void dataParser::init(const xmlChar** p, xmlParserBase* father)
582 DEBTRACE("dataParser::init()");
585 _stackState.push(_state);
586 if (p) getAttributes(p);
590 void dataParser::onStart (const XML_Char* elem, const xmlChar** p)
592 string element(elem);
593 DEBTRACE("data type " << element );
594 stateParser *parser = 0;
595 if (element == "value") parser = new valueParser();
598 _what = "expected value, got <" + element + ">";
599 _state = XMLFATALERROR;
604 _stackParser.push(parser);
605 XML_SetUserData(_xmlParser, parser);
606 parser->init(p, this);
610 void dataParser::addData(std::string value)
612 _dataList.push_back(value);
615 void dataParser::onEnd (const XML_Char* name)
617 // cerr << "dataParser::onEnd " << name << endl;
618 string val = "<data>";
619 while (!_dataList.empty())
621 val += _dataList.front();
622 _dataList.pop_front();
625 // cerr << val << endl;
626 _father->addData(val);
627 stateParser::onEnd(name);
630 // ----------------------------------------------------------------------------
632 void simpleTypeParser::init(const xmlChar** p, xmlParserBase* father)
634 DEBTRACE("simpleTypeParser::init()");
637 _stackState.push(_state);
638 if (p) getAttributes(p);
641 void simpleTypeParser::onStart (const XML_Char* elem, const xmlChar** p)
643 string element(elem);
644 _what = "expected nothing, got <" + element + ">";
645 _state = XMLFATALERROR;
649 void simpleTypeParser::onEnd (const XML_Char* name)
651 string val = string("<") + (char*) name + ">" + _data + "</" + (char*) name +">";
653 _father->addData(val);
654 stateParser::onEnd(name);
657 void simpleTypeParser::charData(std::string data)
659 _data = _data + data;
664 // ----------------------------------------------------------------------------
666 stateLoader::stateLoader(xmlParserBase* parser,
667 YACS::ENGINE::Proc* p) : xmlReader(parser)
669 _runtime = getRuntime();
673 void stateLoader::parse(std::string xmlState)
675 DEBTRACE("stateLoader::parse");
676 stateParser *parser = dynamic_cast<stateParser*> (_rootParser);
678 parser->setRuntime(_runtime);
680 xmlReader::parse(xmlState);
682 DEBTRACE(parser->_state);
683 switch (parser->_state)
688 DEBTRACE("parse OK");
693 string what = "Abort Parse: " + parser->_what;
694 throw Exception(what);
699 string what = "Abort Parse: unknown execution problem";
700 throw Exception(what);
706 void YACS::ENGINE::loadState(YACS::ENGINE::Proc *p,const std::string& xmlStateFile)
708 DEBTRACE("YACS::ENGINE::loadState");
711 stateParser* rootParser = new stateParser();
712 stateLoader myStateLoader(rootParser, p);
713 myStateLoader.parse(xmlStateFile);