2 #include "LoadState.hxx"
6 #include "WhileLoop.hxx"
10 #include "InputPort.hxx"
11 #include "ElementaryNode.hxx"
20 #include "YacsTrace.hxx"
22 using namespace YACS::ENGINE;
25 XMLReadState stateParser::_state;
26 std::string stateParser::_what;
27 std::stack<XMLReadState> stateParser::_stackState;
28 Proc* stateParser::_p;
29 Runtime* stateParser::_runtime;
30 std::map<std::string, YACS::StatesForNode> stateParser::_nodeStateValue;
31 std::map<std::string, YACS::StatesForNode> stateParser::_nodeStates;
33 // ----------------------------------------------------------------------------
34 void stateParser::setProc(Proc* p)
39 void stateParser::setRuntime(Runtime* runtime)
44 void stateParser::init(const xmlChar** p, xmlParserBase* father)
46 DEBTRACE("stateParser::init()");
47 _state = XMLNOCONTEXT;
49 _stackState.push(_state);
50 _nodeStateValue["INITED"] =YACS::INITED;
51 _nodeStateValue["TOLOAD"] =YACS::TOLOAD;
52 _nodeStateValue["LOADED"] =YACS::LOADED;
53 _nodeStateValue["TOACTIVATE"] =YACS::TOACTIVATE;
54 _nodeStateValue["ACTIVATED"] =YACS::ACTIVATED;
55 _nodeStateValue["DESACTIVATED"] =YACS::DESACTIVATED;
56 _nodeStateValue["DONE"] =YACS::DONE;
57 _nodeStateValue["SUSPENDED"] =YACS::SUSPENDED;
58 _nodeStateValue["LOADFAILED"] =YACS::LOADFAILED;
59 _nodeStateValue["EXECFAILED"] =YACS::EXECFAILED;
60 _nodeStateValue["PAUSE"] =YACS::PAUSE;
61 _nodeStateValue["INTERNALERR"] =YACS::INTERNALERR;
62 _nodeStateValue["DISABLED"] =YACS::DISABLED;
63 _nodeStateValue["FAILED"] =YACS::FAILED;
64 _nodeStateValue["ERROR"] =YACS::ERROR;
68 void stateParser::onStart (const XML_Char* elem, const xmlChar** p)
70 DEBTRACE("stateParser::onStart");
72 stateParser *parser = 0;
73 if (element == "graphState") parser = new graphParser();
76 _what = "expected <graphState>, got <" + element + ">";
77 _state = XMLFATALERROR;
82 _stackParser.push(parser);
83 XML_SetUserData(_xmlParser, parser);
89 void stateParser::onEnd (const XML_Char* name)
92 _state = _stackState.top();
93 //cerr << "end " << name << " " << _stackParser.size() << " " << _state << endl;
97 void stateParser::charData(std::string data)
99 //cerr << "data " << data << endl;
102 // ----------------------------------------------------------------------------
104 void graphParser::init(const xmlChar** p, xmlParserBase* father)
106 // DEBTRACE("graphParser::init()");
109 _stackState.push(_state);
110 if (p) getAttributes(p);
113 void graphParser::onStart (const XML_Char* elem, const xmlChar** p)
115 string element(elem);
116 stateParser *parser = 0;
117 if (element == "node") parser = new nodeParser();
120 _what = "expected <node>, got <" + element + ">";
121 _state = XMLFATALERROR;
126 _stackParser.push(parser);
127 XML_SetUserData(_xmlParser, parser);
128 parser->init(p, this);
133 void graphParser::onEnd (const XML_Char* name)
135 std::map<std::string, YACS::StatesForNode>::const_iterator it;
136 for (it = _nodeStates.begin(); it != _nodeStates.end(); it++)
139 string nodeName = it->first;
140 DEBTRACE("nodeName = " << nodeName);
141 if ( _p->nodeMap.find(nodeName) != _p->nodeMap.end() )
142 node = _p->getChildByName(nodeName);
145 assert(_p->getName() == nodeName);
148 InGate* inGate = node->getInGate();
149 list<OutGate*> backlinks = inGate->getBackLinks();
150 for (list<OutGate*>::iterator io = backlinks.begin(); io != backlinks.end(); io++)
152 Node* fromNode = (*io)->getNode();
154 if (fromNode == _p) fromName = fromNode->getName();
155 else fromName = _p->getChildName(fromNode);
156 if (_nodeStates[fromName] == YACS::DONE)
158 DEBTRACE(" fromNode = " << fromName);
159 inGate->setPrecursorDone(*io);
163 stateParser::onEnd(name);
166 // ----------------------------------------------------------------------------
168 void nodeParser::init(const xmlChar** p, xmlParserBase* father)
170 //DEBTRACE("nodeParser::init()");
173 _stackState.push(_state);
174 if (p) getAttributes(p);
178 void nodeParser::onStart (const XML_Char* elem, const xmlChar** p)
180 string element(elem);
181 stateParser *parser = 0;
182 if (element == "inputPort") parser = new portParser();
183 else if (element == "name") parser = new attrParser();
184 else if (element == "state") parser = new attrParser();
185 else if (element == "nsteps") parser = new attrParser();
186 else if (element == "nbdone") parser = new attrParser();
187 else if (element == "condition") parser = new attrParser();
188 else if (element == "inputPort") parser = new portParser();
191 _what = "expected name, state or inputPort, got <" + element + ">";
192 _state = XMLFATALERROR;
197 _stackParser.push(parser);
198 XML_SetUserData(_xmlParser, parser);
199 parser->init(p, this);
203 void nodeParser::onEnd (const XML_Char* name)
205 string nodeName = _mapAttrib["name"];
206 string nodeType = _mapAttrib["type"];
207 string nodeState = _mapAttrib["state"];
208 cerr << "nodeName: " << nodeName \
209 << " nodeType: " << nodeType \
210 << " nodeState: " << nodeState << endl;
211 // for (std::map< std::string, Node * >::iterator it=_p->nodeMap.begin(); it != _p->nodeMap.end(); it++)
212 // cerr << "nodeMap: " << it->first << endl;
213 _nodeStates[nodeName] = _nodeStateValue[nodeState];
215 if ( _p->nodeMap.find(nodeName) != _p->nodeMap.end() )
216 node = _p->getChildByName(nodeName);
219 assert(_p->getName() == nodeName);
222 assert(_nodeStateValue.find(nodeState) != _nodeStateValue.end());
223 YACS::ENGINE::StateLoader(node, _nodeStateValue[nodeState]);
225 if (nodeType == "forLoop")
227 if (_mapAttrib.find("nsteps") == _mapAttrib.end())
229 _what = "no attribute nsteps in forLoop " + _mapAttrib["name"];
230 _state = XMLFATALERROR;
233 int nsteps = atoi(_mapAttrib["nsteps"].c_str());
235 if (_mapAttrib.find("nbdone") == _mapAttrib.end())
237 _what = "no attribute nbdone in forLoop " + _mapAttrib["name"];
238 _state = XMLFATALERROR;
241 int nbdone = atoi(_mapAttrib["nbdone"].c_str());
242 DEBTRACE("nsteps = " << nsteps << ", nbdone = " << nbdone);
244 ForLoop* loop = dynamic_cast<ForLoop*>(node);
247 _what = "node is not a ForLoop: " + _mapAttrib["name"];
248 _state = XMLFATALERROR;
251 loop->edGetNbOfTimesInputPort()->edInit(nsteps);
252 YACS::ENGINE::NbDoneLoader(loop, nbdone);
255 else if (nodeType == "whileLoop")
257 if (_mapAttrib.find("nbdone") == _mapAttrib.end())
259 _what = "no attribute nbdone in forLoop " + _mapAttrib["name"];
260 _state = XMLFATALERROR;
263 int nbdone = atoi(_mapAttrib["nbdone"].c_str());
265 if (_mapAttrib.find("condition") == _mapAttrib.end())
267 _what = "no attribute condition in forLoop " + _mapAttrib["name"];
268 _state = XMLFATALERROR;
271 bool condition = atoi(_mapAttrib["condition"].c_str());
272 DEBTRACE("condition = " << condition << ", nbdone = " << nbdone);
274 WhileLoop* loop = dynamic_cast<WhileLoop*>(node);
277 _what = "node is not a WhileLoop: " + _mapAttrib["name"];
278 _state = XMLFATALERROR;
281 loop->edGetConditionPort()->edInit(condition);
282 YACS::ENGINE::NbDoneLoader(loop, nbdone);
285 else if (nodeType == "switch")
287 if (_mapAttrib.find("condition") == _mapAttrib.end())
289 _what = "no attribute condition in switch " + _mapAttrib["name"];
290 _state = XMLFATALERROR;
293 int condition = atoi(_mapAttrib["condition"].c_str());
294 DEBTRACE("condition = " << condition);
296 Switch* mySwitch = dynamic_cast<Switch*>(node);
299 _what = "node is not a Switch: " + _mapAttrib["name"];
300 _state = XMLFATALERROR;
303 mySwitch->edGetConditionPort()->edInit(condition);
306 stateParser::onEnd(name);
309 // ----------------------------------------------------------------------------
311 void attrParser::init(const xmlChar** p, xmlParserBase* father)
313 //DEBTRACE("attrParser::init()");
314 //_state = XMLINNODE;
316 _stackState.push(_state); // keep current state
317 if (p) getAttributes(p);
321 void attrParser::onStart (const XML_Char* elem, const xmlChar** p)
323 string element(elem);
324 _what = "expected nothing, got <" + element + ">";
325 _state = XMLFATALERROR;
329 void attrParser::charData(std::string data)
334 void attrParser::onEnd (const XML_Char* name)
336 // cerr << "end attrParser " << name << " " << _stackParser.size() << endl;
338 _father->setAttribute((char*)name, _attrValue);
339 stateParser::onEnd(name);
342 // ----------------------------------------------------------------------------
344 void portParser::init(const xmlChar** p, xmlParserBase* father)
346 // DEBTRACE("portParser::init()");
349 assert( dynamic_cast<nodeParser*> (father));
350 _stackState.push(_state);
351 if (p) getAttributes(p);
355 void portParser::onStart (const XML_Char* elem, const xmlChar** p)
357 string element(elem);
358 stateParser *parser = 0;
359 if (element == "name") parser = new attrParser();
360 else if (element == "value") parser = new valueParser();
363 _what = "expected name or value, got <" + element + ">";
364 _state = XMLFATALERROR;
369 _stackParser.push(parser);
370 XML_SetUserData(_xmlParser, parser);
371 parser->init(p, this);
375 void portParser::addData(std::string value)
380 void portParser::onEnd (const XML_Char* name)
382 cerr << "portName: " << _mapAttrib["name"] << endl;
383 cerr << "value: " << _data << endl;
384 string nodeName = _father->getAttribute("name");
385 string nodeType = _father->getAttribute("type");
386 Node *node = _p->getChildByName(nodeName);
387 if (nodeType == "elementaryNode")
389 ElementaryNode* eNode = dynamic_cast<ElementaryNode*>(node);
391 InputPort *port = eNode->getInputPort(_mapAttrib["name"]);
392 port->edInit("XML",_data.c_str());
394 else if (nodeType == "forLoop")
396 string what="no way to set a port value on port " + _mapAttrib["name"];
397 what += " in node " + nodeName + " of type " + nodeType;
398 throw Exception(what);
400 else if (nodeType == "whileLoop")
402 string what="no way to set a port value on port " + _mapAttrib["name"];
403 what += " in node " + nodeName + " of type " + nodeType;
404 throw Exception(what);
406 else if (nodeType == "switch")
408 string what="no way to set a port value on port " + _mapAttrib["name"];
409 what += " in node " + nodeName + " of type " + nodeType;
410 throw Exception(what);
412 else if (nodeType == "foreachLoop")
414 string what="no way to set a port value on port " + _mapAttrib["name"];
415 what += " in node " + nodeName + " of type " + nodeType;
416 throw Exception(what);
420 string what="no way to set a port value on port " + _mapAttrib["name"];
421 what += " in node " + nodeName + " of type " + nodeType;
422 throw Exception(what);
425 stateParser::onEnd(name);
428 // ----------------------------------------------------------------------------
430 void valueParser::init(const xmlChar** p, xmlParserBase* father)
432 // DEBTRACE("valueParser::init()");
435 _stackState.push(_state);
436 if (p) getAttributes(p);
440 void valueParser::onStart (const XML_Char* elem, const xmlChar** p)
442 string element(elem);
443 // cerr << "value type " << element << endl;
444 stateParser *parser = 0;
445 if (element == "data") parser = new dataParser();
446 else if (element == "array") parser = new arrayParser();
447 else parser = new simpleTypeParser();
450 _stackParser.push(parser);
451 XML_SetUserData(_xmlParser, parser);
452 parser->init(p, this);
456 void valueParser::addData(std::string value)
458 _data = "<value>" + value + "</value>";
461 void valueParser::onEnd (const XML_Char* name)
463 // cerr << _data << endl;
464 _father->addData(_data);
465 string elem = (char *) name;
466 //if (elem == "value" || elem == "data" || elem == "array")
467 stateParser::onEnd(name);
468 //else assert(0); //DEBTRACE("valueParser::onEnd " << elem);
471 // ----------------------------------------------------------------------------
473 void arrayParser::init(const xmlChar** p, xmlParserBase* father)
475 // DEBTRACE("arrayParser::init()");
478 _stackState.push(_state);
479 if (p) getAttributes(p);
483 void arrayParser::onStart (const XML_Char* elem, const xmlChar** p)
485 string element(elem);
486 // cerr << "array type " << element << endl;
487 stateParser *parser = 0;
488 if (element == "data") parser = new dataParser();
491 _what = "expected data, got <" + element + ">";
492 _state = XMLFATALERROR;
497 _stackParser.push(parser);
498 XML_SetUserData(_xmlParser, parser);
499 parser->init(p, this);
503 void arrayParser::addData(std::string value)
505 string val = "<array>" + value + "</array>";
510 void arrayParser::onEnd (const XML_Char* name)
512 // cerr << "arrayParser::onEnd " << name << endl;
513 // cerr << _data << endl;
514 _father->addData(_data);
515 stateParser::onEnd(name);
518 // ----------------------------------------------------------------------------
520 void dataParser::init(const xmlChar** p, xmlParserBase* father)
522 // DEBTRACE("dataParser::init()");
525 _stackState.push(_state);
526 if (p) getAttributes(p);
530 void dataParser::onStart (const XML_Char* elem, const xmlChar** p)
532 string element(elem);
533 // cerr << "data type " << element << endl;
534 stateParser *parser = 0;
535 if (element == "value") parser = new valueParser();
538 _what = "expected value, got <" + element + ">";
539 _state = XMLFATALERROR;
544 _stackParser.push(parser);
545 XML_SetUserData(_xmlParser, parser);
546 parser->init(p, this);
550 void dataParser::addData(std::string value)
552 _dataList.push_back(value);
555 void dataParser::onEnd (const XML_Char* name)
557 // cerr << "dataParser::onEnd " << name << endl;
558 string val = "<data>";
559 while (!_dataList.empty())
561 val += _dataList.front();
562 _dataList.pop_front();
565 // cerr << val << endl;
566 _father->addData(val);
567 stateParser::onEnd(name);
570 // ----------------------------------------------------------------------------
572 void simpleTypeParser::init(const xmlChar** p, xmlParserBase* father)
574 // DEBTRACE("simpleTypeParser::init()");
577 _stackState.push(_state);
578 if (p) getAttributes(p);
581 void simpleTypeParser::onStart (const XML_Char* elem, const xmlChar** p)
583 string element(elem);
584 _what = "expected nothing, got <" + element + ">";
585 _state = XMLFATALERROR;
589 void simpleTypeParser::onEnd (const XML_Char* name)
591 // cerr << "simpleTypeParser::onEnd " << name << endl;
592 string val = string("<") + (char*) name + ">" + _data + "</" + (char*) name +">";
593 // cerr << val << endl;
594 _father->addData(val);
595 stateParser::onEnd(name);
598 void simpleTypeParser::charData(std::string data)
600 // cerr << "simple data " << data << endl;
606 // ----------------------------------------------------------------------------
608 stateLoader::stateLoader(xmlParserBase* parser,
609 YACS::ENGINE::Proc* p) : xmlReader(parser)
611 _runtime = getRuntime();
615 void stateLoader::parse(std::string xmlState)
617 stateParser *parser = dynamic_cast<stateParser*> (_rootParser);
619 parser->setRuntime(_runtime);
621 xmlReader::parse(xmlState);
623 cerr << parser->_state << endl;
624 switch (parser->_state)
629 cerr << "parse OK" << endl;
634 string what = "Abort Parse: " + parser->_what;
635 throw Exception(what);
640 string what = "Abort Parse: unknown execution problem";
641 throw Exception(what);