1 // Copyright (C) 2006-2012 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
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 void nodeParser::init(const xmlChar** p, xmlParserBase* father)
186 DEBTRACE("nodeParser::init()");
189 _stackState.push(_state);
190 if (p) getAttributes(p);
194 void nodeParser::onStart (const XML_Char* elem, const xmlChar** p)
196 DEBTRACE("nodeParser::onStart" << elem);
197 string element(elem);
198 stateParser *parser = 0;
199 if (element == "inputPort") parser = new portParser();
200 else if (element == "name") parser = new attrParser();
201 else if (element == "state") parser = new attrParser();
202 else if (element == "nsteps") parser = new attrParser();
203 else if (element == "nbdone") parser = new attrParser();
204 else if (element == "condition") parser = new attrParser();
205 else if (element == "inputPort") parser = new portParser();
208 _what = "expected name, state or inputPort, got <" + element + ">";
209 _state = XMLFATALERROR;
214 _stackParser.push(parser);
215 XML_SetUserData(_xmlParser, parser);
216 parser->init(p, this);
220 void nodeParser::onEnd (const XML_Char* name)
222 string nodeName = _mapAttrib["name"];
223 string nodeType = _mapAttrib["type"];
224 string nodeState = _mapAttrib["state"];
225 DEBTRACE( "nodeName: " << nodeName << " nodeType: " << nodeType << " nodeState: " << nodeState );
226 // for (std::map< std::string, Node * >::iterator it=_p->nodeMap.begin(); it != _p->nodeMap.end(); it++)
227 // cerr << "nodeMap: " << it->first << endl;
228 _nodeStates[nodeName] = _nodeStateValue[nodeState];
230 if(_p->getName() == nodeName)
233 node = _p->getChildByName(nodeName);
235 YASSERT(_nodeStateValue.find(nodeState) != _nodeStateValue.end());
236 YACS::ENGINE::StateLoader(node, _nodeStateValue[nodeState]);
238 if (nodeType == "forLoop")
240 if (_mapAttrib.find("nsteps") == _mapAttrib.end())
242 _what = "no attribute nsteps in forLoop " + _mapAttrib["name"];
243 _state = XMLFATALERROR;
246 int nsteps = atoi(_mapAttrib["nsteps"].c_str());
248 if (_mapAttrib.find("nbdone") == _mapAttrib.end())
250 _what = "no attribute nbdone in forLoop " + _mapAttrib["name"];
251 _state = XMLFATALERROR;
254 int nbdone = atoi(_mapAttrib["nbdone"].c_str());
255 DEBTRACE("nsteps = " << nsteps << ", nbdone = " << nbdone);
257 ForLoop* loop = dynamic_cast<ForLoop*>(node);
260 _what = "node is not a ForLoop: " + _mapAttrib["name"];
261 _state = XMLFATALERROR;
264 loop->edGetNbOfTimesInputPort()->edInit(nsteps);
265 YACS::ENGINE::NbDoneLoader(loop, nbdone);
268 else if (nodeType == "whileLoop")
270 if (_mapAttrib.find("nbdone") == _mapAttrib.end())
272 _what = "no attribute nbdone in forLoop " + _mapAttrib["name"];
273 _state = XMLFATALERROR;
276 int nbdone = atoi(_mapAttrib["nbdone"].c_str());
278 if (_mapAttrib.find("condition") == _mapAttrib.end())
280 _what = "no attribute condition in forLoop " + _mapAttrib["name"];
281 _state = XMLFATALERROR;
284 bool condition = atoi(_mapAttrib["condition"].c_str());
285 DEBTRACE("condition = " << condition << ", nbdone = " << nbdone);
287 WhileLoop* loop = dynamic_cast<WhileLoop*>(node);
290 _what = "node is not a WhileLoop: " + _mapAttrib["name"];
291 _state = XMLFATALERROR;
294 loop->edGetConditionPort()->edInit(condition);
295 YACS::ENGINE::NbDoneLoader(loop, nbdone);
298 else if (nodeType == "switch")
300 if (_mapAttrib.find("condition") == _mapAttrib.end())
302 _what = "no attribute condition in switch " + _mapAttrib["name"];
303 _state = XMLFATALERROR;
306 int condition = atoi(_mapAttrib["condition"].c_str());
307 DEBTRACE("condition = " << condition);
309 Switch* mySwitch = dynamic_cast<Switch*>(node);
312 _what = "node is not a Switch: " + _mapAttrib["name"];
313 _state = XMLFATALERROR;
316 mySwitch->edGetConditionPort()->edInit(condition);
319 stateParser::onEnd(name);
322 // ----------------------------------------------------------------------------
324 void attrParser::init(const xmlChar** p, xmlParserBase* father)
326 DEBTRACE("attrParser::init()");
327 //_state = XMLINNODE;
329 _stackState.push(_state); // keep current state
330 if (p) getAttributes(p);
334 void attrParser::onStart (const XML_Char* elem, const xmlChar** p)
336 string element(elem);
337 _what = "expected nothing, got <" + element + ">";
338 _state = XMLFATALERROR;
342 void attrParser::charData(std::string data)
347 void attrParser::onEnd (const XML_Char* name)
349 // cerr << "end attrParser " << name << " " << _stackParser.size() << endl;
351 _father->setAttribute((char*)name, _attrValue);
352 stateParser::onEnd(name);
355 // ----------------------------------------------------------------------------
357 void portParser::init(const xmlChar** p, xmlParserBase* father)
359 DEBTRACE("portParser::init()");
362 YASSERT( dynamic_cast<nodeParser*> (father));
363 _stackState.push(_state);
364 if (p) getAttributes(p);
368 void portParser::onStart (const XML_Char* elem, const xmlChar** p)
370 DEBTRACE("portParser::onStart" << elem);
371 string element(elem);
372 stateParser *parser = 0;
373 if (element == "name") parser = new attrParser();
374 else if (element == "value") parser = new valueParser();
377 _what = "expected name or value, got <" + element + ">";
378 _state = XMLFATALERROR;
383 _stackParser.push(parser);
384 XML_SetUserData(_xmlParser, parser);
385 parser->init(p, this);
389 void portParser::addData(std::string value)
394 void portParser::onEnd (const XML_Char* name)
396 DEBTRACE("portName: " << _mapAttrib["name"] << "value: " << _data );
397 string nodeName = _father->getAttribute("name");
398 string nodeType = _father->getAttribute("type");
399 Node *node = _p->getChildByName(nodeName);
400 if (nodeType == "elementaryNode")
402 ElementaryNode* eNode = dynamic_cast<ElementaryNode*>(node);
404 InputPort *port = eNode->getInputPort(_mapAttrib["name"]);
406 port->edInit("XML",_data.c_str());
408 else if (nodeType == "forLoop")
410 string what="no way to set a port value on port " + _mapAttrib["name"];
411 what += " in node " + nodeName + " of type " + nodeType;
412 throw Exception(what);
414 else if (nodeType == "whileLoop")
416 string what="no way to set a port value on port " + _mapAttrib["name"];
417 what += " in node " + nodeName + " of type " + nodeType;
418 throw Exception(what);
420 else if (nodeType == "switch")
422 string what="no way to set a port value on port " + _mapAttrib["name"];
423 what += " in node " + nodeName + " of type " + nodeType;
424 throw Exception(what);
426 else if (nodeType == "foreachLoop")
428 string what="no way to set a port value on port " + _mapAttrib["name"];
429 what += " in node " + nodeName + " of type " + nodeType;
430 throw Exception(what);
434 string what="no way to set a port value on port " + _mapAttrib["name"];
435 what += " in node " + nodeName + " of type " + nodeType;
436 throw Exception(what);
439 stateParser::onEnd(name);
442 // ----------------------------------------------------------------------------
444 void valueParser::init(const xmlChar** p, xmlParserBase* father)
446 DEBTRACE("valueParser::init()");
449 _stackState.push(_state);
450 if (p) getAttributes(p);
454 void valueParser::onStart (const XML_Char* elem, const xmlChar** p)
456 string element(elem);
457 DEBTRACE("value type " << element );
458 stateParser *parser = 0;
459 if (element == "data") parser = new dataParser();
460 else if (element == "array") parser = new arrayParser();
461 else parser = new simpleTypeParser();
464 _stackParser.push(parser);
465 XML_SetUserData(_xmlParser, parser);
466 parser->init(p, this);
470 void valueParser::addData(std::string value)
472 _data = "<value>" + value + "</value>";
475 void valueParser::onEnd (const XML_Char* name)
478 _father->addData(_data);
479 string elem = (char *) name;
480 //if (elem == "value" || elem == "data" || elem == "array")
481 stateParser::onEnd(name);
482 //else YASSERT(0); //DEBTRACE("valueParser::onEnd " << elem);
485 // ----------------------------------------------------------------------------
487 void arrayParser::init(const xmlChar** p, xmlParserBase* father)
489 DEBTRACE("arrayParser::init()");
492 _stackState.push(_state);
493 if (p) getAttributes(p);
497 void arrayParser::onStart (const XML_Char* elem, const xmlChar** p)
499 string element(elem);
500 DEBTRACE("array type " << element);
501 stateParser *parser = 0;
502 if (element == "data") parser = new dataParser();
505 _what = "expected data, got <" + element + ">";
506 _state = XMLFATALERROR;
511 _stackParser.push(parser);
512 XML_SetUserData(_xmlParser, parser);
513 parser->init(p, this);
517 void arrayParser::addData(std::string value)
519 string val = "<array>" + value + "</array>";
524 void arrayParser::onEnd (const XML_Char* name)
526 // cerr << "arrayParser::onEnd " << name << endl;
527 // cerr << _data << endl;
528 _father->addData(_data);
529 stateParser::onEnd(name);
532 // ----------------------------------------------------------------------------
534 void dataParser::init(const xmlChar** p, xmlParserBase* father)
536 DEBTRACE("dataParser::init()");
539 _stackState.push(_state);
540 if (p) getAttributes(p);
544 void dataParser::onStart (const XML_Char* elem, const xmlChar** p)
546 string element(elem);
547 DEBTRACE("data type " << element );
548 stateParser *parser = 0;
549 if (element == "value") parser = new valueParser();
552 _what = "expected value, got <" + element + ">";
553 _state = XMLFATALERROR;
558 _stackParser.push(parser);
559 XML_SetUserData(_xmlParser, parser);
560 parser->init(p, this);
564 void dataParser::addData(std::string value)
566 _dataList.push_back(value);
569 void dataParser::onEnd (const XML_Char* name)
571 // cerr << "dataParser::onEnd " << name << endl;
572 string val = "<data>";
573 while (!_dataList.empty())
575 val += _dataList.front();
576 _dataList.pop_front();
579 // cerr << val << endl;
580 _father->addData(val);
581 stateParser::onEnd(name);
584 // ----------------------------------------------------------------------------
586 void simpleTypeParser::init(const xmlChar** p, xmlParserBase* father)
588 DEBTRACE("simpleTypeParser::init()");
591 _stackState.push(_state);
592 if (p) getAttributes(p);
595 void simpleTypeParser::onStart (const XML_Char* elem, const xmlChar** p)
597 string element(elem);
598 _what = "expected nothing, got <" + element + ">";
599 _state = XMLFATALERROR;
603 void simpleTypeParser::onEnd (const XML_Char* name)
605 string val = string("<") + (char*) name + ">" + _data + "</" + (char*) name +">";
607 _father->addData(val);
608 stateParser::onEnd(name);
611 void simpleTypeParser::charData(std::string data)
613 _data = _data + data;
618 // ----------------------------------------------------------------------------
620 stateLoader::stateLoader(xmlParserBase* parser,
621 YACS::ENGINE::Proc* p) : xmlReader(parser)
623 _runtime = getRuntime();
627 void stateLoader::parse(std::string xmlState)
629 DEBTRACE("stateLoader::parse");
630 stateParser *parser = dynamic_cast<stateParser*> (_rootParser);
632 parser->setRuntime(_runtime);
634 xmlReader::parse(xmlState);
636 DEBTRACE(parser->_state);
637 switch (parser->_state)
642 DEBTRACE("parse OK");
647 string what = "Abort Parse: " + parser->_what;
648 throw Exception(what);
653 string what = "Abort Parse: unknown execution problem";
654 throw Exception(what);
660 void YACS::ENGINE::loadState(YACS::ENGINE::Proc *p,const std::string& xmlStateFile)
662 DEBTRACE("YACS::ENGINE::loadState");
665 stateParser* rootParser = new stateParser();
666 stateLoader myStateLoader(rootParser, p);
667 myStateLoader.parse(xmlStateFile);