]> SALOME platform Git repositories - modules/yacs.git/blob - src/yacsloader/LoadState.cxx
Salome HOME
copy tag mergefrom_BR_V0_1_CC_Salome_04oct07
[modules/yacs.git] / src / yacsloader / LoadState.cxx
1
2 #include "LoadState.hxx"
3 #include "Proc.hxx"
4 #include "Node.hxx"
5 #include "ForLoop.hxx"
6 #include "WhileLoop.hxx"
7 #include "Switch.hxx"
8 #include "InGate.hxx"
9 #include "Runtime.hxx"
10 #include "InputPort.hxx"
11 #include "ElementaryNode.hxx"
12
13 #include <iostream>
14 #include <string>
15 #include <cstdlib>
16 #include <cstdarg>
17 #include <cassert>
18
19 //#define _DEVDEBUG_
20 #include "YacsTrace.hxx"
21
22 using namespace YACS::ENGINE;
23 using namespace std;
24
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;
32
33 // ----------------------------------------------------------------------------
34 void stateParser::setProc(Proc* p)
35 {
36   _p= p;
37 }
38
39 void stateParser::setRuntime(Runtime* runtime)
40 {
41   _runtime = runtime;
42 }
43
44 void stateParser::init(const xmlChar** p, xmlParserBase* father)
45 {
46   DEBTRACE("stateParser::init()");
47   _state = XMLNOCONTEXT;
48   _father = father;
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;
65 }
66
67
68 void stateParser::onStart (const XML_Char* elem, const xmlChar** p)
69 {
70   DEBTRACE("stateParser::onStart");
71   string element(elem);
72   stateParser *parser = 0;
73   if (element == "graphState") parser = new graphParser();
74   else
75     { 
76       _what = "expected <graphState>, got <" + element + ">";
77       _state = XMLFATALERROR;
78       stopParse(_what);
79     }
80   if (parser)
81     {
82       _stackParser.push(parser);
83       XML_SetUserData(_xmlParser, parser);
84       parser->init(p);
85     }
86 }
87
88
89 void stateParser::onEnd   (const XML_Char* name)
90 {
91   _stackState.pop();
92   _state = _stackState.top();
93   //cerr << "end " << name << " " << _stackParser.size() << " " << _state << endl;
94 }
95
96
97 void stateParser::charData(std::string data)
98 {
99   //cerr << "data " << data << endl;
100 }
101
102 // ----------------------------------------------------------------------------
103
104 void graphParser::init(const xmlChar** p, xmlParserBase* father)
105 {
106   //  DEBTRACE("graphParser::init()");
107   _state = XMLINGRAPH;
108   _father = father;
109   _stackState.push(_state);
110   if (p) getAttributes(p);
111 }
112
113 void graphParser::onStart (const XML_Char* elem, const xmlChar** p)
114 {
115   string element(elem);
116   stateParser *parser = 0;
117   if (element == "node") parser = new nodeParser();
118   else
119     { 
120       _what = "expected <node>, got <" + element + ">";
121       _state = XMLFATALERROR;
122       stopParse(_what);
123     }
124   if (parser)
125     {
126       _stackParser.push(parser);
127       XML_SetUserData(_xmlParser, parser);
128       parser->init(p, this);
129     }
130 }
131
132
133 void graphParser::onEnd   (const XML_Char* name)
134 {
135   std::map<std::string, YACS::StatesForNode>::const_iterator it;
136   for (it = _nodeStates.begin(); it != _nodeStates.end(); it++)
137     {
138       Node *node =0;
139       string nodeName = it->first;
140       DEBTRACE("nodeName = " << nodeName);
141       if ( _p->nodeMap.find(nodeName) != _p->nodeMap.end() )
142         node = _p->getChildByName(nodeName);
143       else
144         {
145           assert(_p->getName() == nodeName);
146           node = _p;
147         }
148       InGate* inGate = node->getInGate();
149       list<OutGate*> backlinks = inGate->getBackLinks();
150       for (list<OutGate*>::iterator io = backlinks.begin(); io != backlinks.end(); io++)
151         {
152           Node* fromNode = (*io)->getNode();
153           string fromName;
154           if (fromNode == _p) fromName = fromNode->getName();
155           else fromName = _p->getChildName(fromNode);
156           if (_nodeStates[fromName] == YACS::DONE)
157             {
158               DEBTRACE("   fromNode = " << fromName);
159               inGate->setPrecursorDone(*io);
160             }
161         }
162     }
163   stateParser::onEnd(name);
164 }
165
166 // ----------------------------------------------------------------------------
167
168 void nodeParser::init(const xmlChar** p, xmlParserBase* father)
169 {
170   //DEBTRACE("nodeParser::init()");
171   _state = XMLINNODE;
172   _father = father;
173   _stackState.push(_state);
174   if (p) getAttributes(p);
175 }
176
177
178 void nodeParser::onStart (const XML_Char* elem, const xmlChar** p)
179 {
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();
189   else
190     { 
191       _what = "expected name, state or inputPort, got <" + element + ">";
192       _state = XMLFATALERROR;
193       stopParse(_what);
194     }
195   if (parser)
196     {
197       _stackParser.push(parser);
198       XML_SetUserData(_xmlParser, parser);
199       parser->init(p, this);
200     }
201 }
202
203 void nodeParser::onEnd   (const XML_Char* name)
204 {
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];
214   Node *node =0;
215   if ( _p->nodeMap.find(nodeName) != _p->nodeMap.end() )
216     node = _p->getChildByName(nodeName);
217   else
218     {
219       assert(_p->getName() == nodeName);
220       node = _p;
221     }
222   assert(_nodeStateValue.find(nodeState) != _nodeStateValue.end());
223   YACS::ENGINE::StateLoader(node, _nodeStateValue[nodeState]);
224
225   if (nodeType == "forLoop")
226     {
227       if (_mapAttrib.find("nsteps") == _mapAttrib.end())
228         {
229           _what = "no attribute nsteps in forLoop " + _mapAttrib["name"];
230           _state = XMLFATALERROR;
231           stopParse(_what);
232         }
233       int nsteps =  atoi(_mapAttrib["nsteps"].c_str());
234
235       if (_mapAttrib.find("nbdone") == _mapAttrib.end())
236         {
237           _what = "no attribute nbdone in forLoop " + _mapAttrib["name"];
238           _state = XMLFATALERROR;
239           stopParse(_what);
240         }
241       int nbdone =  atoi(_mapAttrib["nbdone"].c_str());
242       DEBTRACE("nsteps = " << nsteps << ", nbdone = " << nbdone);
243
244       ForLoop* loop = dynamic_cast<ForLoop*>(node);
245       if (!loop)
246         {
247           _what = "node is not a ForLoop: " + _mapAttrib["name"];
248           _state = XMLFATALERROR;
249           stopParse(_what);
250         }
251       loop->edGetNbOfTimesInputPort()->edInit(nsteps);
252       YACS::ENGINE::NbDoneLoader(loop, nbdone);
253     }
254
255   else if (nodeType == "whileLoop")
256     {
257       if (_mapAttrib.find("nbdone") == _mapAttrib.end())
258         {
259           _what = "no attribute nbdone in forLoop " + _mapAttrib["name"];
260           _state = XMLFATALERROR;
261           stopParse(_what);
262         }
263       int nbdone =  atoi(_mapAttrib["nbdone"].c_str());
264
265       if (_mapAttrib.find("condition") == _mapAttrib.end())
266         {
267           _what = "no attribute condition in forLoop " + _mapAttrib["name"];
268           _state = XMLFATALERROR;
269           stopParse(_what);
270         }
271       bool condition =  atoi(_mapAttrib["condition"].c_str());
272       DEBTRACE("condition = " << condition << ", nbdone = " << nbdone);
273
274       WhileLoop* loop = dynamic_cast<WhileLoop*>(node);
275       if (!loop)
276         {
277           _what = "node is not a WhileLoop: " + _mapAttrib["name"];
278           _state = XMLFATALERROR;
279           stopParse(_what);
280         }
281       loop->edGetConditionPort()->edInit(condition);
282       YACS::ENGINE::NbDoneLoader(loop, nbdone);
283     }
284
285   else if (nodeType == "switch")
286     {
287       if (_mapAttrib.find("condition") == _mapAttrib.end())
288         {
289           _what = "no attribute condition in switch " + _mapAttrib["name"];
290           _state = XMLFATALERROR;
291           stopParse(_what);
292         }
293       int condition =  atoi(_mapAttrib["condition"].c_str());
294       DEBTRACE("condition = " << condition);
295
296       Switch* mySwitch = dynamic_cast<Switch*>(node);
297       if (!mySwitch)
298         {
299           _what = "node is not a Switch: " + _mapAttrib["name"];
300           _state = XMLFATALERROR;
301           stopParse(_what);
302         }
303       mySwitch->edGetConditionPort()->edInit(condition);
304     }
305
306   stateParser::onEnd(name);
307 }
308
309 // ----------------------------------------------------------------------------
310
311 void attrParser::init(const xmlChar** p, xmlParserBase* father)
312 {
313   //DEBTRACE("attrParser::init()");
314   //_state = XMLINNODE;
315   _father = father;
316   _stackState.push(_state); // keep current state
317   if (p) getAttributes(p);
318 }
319
320
321 void attrParser::onStart (const XML_Char* elem, const xmlChar** p)
322 {
323   string element(elem);
324   _what = "expected nothing, got <" + element + ">";
325   _state = XMLFATALERROR;
326   stopParse(_what);
327 }
328
329 void attrParser::charData(std::string data)
330 {
331   _attrValue = data;
332 }
333
334 void attrParser::onEnd   (const XML_Char* name)
335 {
336   // cerr << "end attrParser " << name << " " << _stackParser.size() << endl;
337   assert(_father);
338   _father->setAttribute((char*)name, _attrValue);
339   stateParser::onEnd(name);
340 }
341
342 // ----------------------------------------------------------------------------
343
344 void portParser::init(const xmlChar** p, xmlParserBase* father)
345 {
346   //  DEBTRACE("portParser::init()");
347   _state = XMLINPORT;
348   _father = father;
349   assert( dynamic_cast<nodeParser*> (father));
350   _stackState.push(_state);
351   if (p) getAttributes(p);
352 }
353
354
355 void portParser::onStart (const XML_Char* elem, const xmlChar** p)
356 {
357   string element(elem);
358   stateParser *parser = 0;
359   if      (element == "name")      parser = new attrParser();
360   else if (element == "value")     parser = new valueParser();
361   else
362     { 
363       _what = "expected name or value, got <" + element + ">";
364       _state = XMLFATALERROR;
365       stopParse(_what);
366     }
367   if (parser)
368     {
369       _stackParser.push(parser);
370       XML_SetUserData(_xmlParser, parser);
371       parser->init(p, this);
372     }
373 }
374
375 void portParser::addData(std::string value)
376 {
377   _data = value;
378 }
379
380 void portParser::onEnd   (const XML_Char* name)
381 {
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")
388     {
389       ElementaryNode* eNode = dynamic_cast<ElementaryNode*>(node);
390       assert(eNode);
391       InputPort *port = eNode->getInputPort(_mapAttrib["name"]);
392       port->edInit("XML",_data.c_str());
393     }
394   else if (nodeType == "forLoop")
395     {
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);
399     }
400   else if (nodeType == "whileLoop")
401     {
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);
405     }
406   else if (nodeType == "switch")
407     {
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);
411     }
412   else if (nodeType == "foreachLoop")
413     {
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);
417     }
418   else 
419     {
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);
423     }
424
425   stateParser::onEnd(name);
426 }
427
428 // ----------------------------------------------------------------------------
429
430 void valueParser::init(const xmlChar** p, xmlParserBase* father)
431 {
432   //  DEBTRACE("valueParser::init()");
433   _state = XMLINVALUE;
434   _father = father;
435   _stackState.push(_state);
436   if (p) getAttributes(p);
437 }
438
439
440 void valueParser::onStart (const XML_Char* elem, const xmlChar** p)
441 {
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();
448   if (parser)
449     {
450       _stackParser.push(parser);
451       XML_SetUserData(_xmlParser, parser);
452       parser->init(p, this);
453     }
454 }
455
456 void valueParser::addData(std::string value)
457 {
458   _data = "<value>" + value + "</value>";
459 }
460
461 void valueParser::onEnd   (const XML_Char* name)
462 {
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);
469 }
470
471 // ----------------------------------------------------------------------------
472
473 void arrayParser::init(const xmlChar** p, xmlParserBase* father)
474 {
475   // DEBTRACE("arrayParser::init()");
476   _state = XMLINVALUE;
477   _father = father;
478   _stackState.push(_state);
479   if (p) getAttributes(p);
480 }
481
482
483 void arrayParser::onStart (const XML_Char* elem, const xmlChar** p)
484 {
485   string element(elem);
486   // cerr << "array type " << element << endl;
487   stateParser *parser = 0;
488   if      (element == "data")      parser = new dataParser();
489   else
490     { 
491       _what = "expected data, got <" + element + ">";
492       _state = XMLFATALERROR;
493       stopParse(_what);
494     }
495   if (parser)
496     {
497       _stackParser.push(parser);
498       XML_SetUserData(_xmlParser, parser);
499       parser->init(p, this);
500     }
501 }
502
503 void arrayParser::addData(std::string value)
504 {
505   string val = "<array>" + value + "</array>";
506   _data = val;
507 }
508
509
510 void arrayParser::onEnd   (const XML_Char* name)
511 {
512   // cerr << "arrayParser::onEnd " << name << endl;
513   // cerr << _data << endl;
514   _father->addData(_data);
515   stateParser::onEnd(name);
516 }
517
518 // ----------------------------------------------------------------------------
519
520 void dataParser::init(const xmlChar** p, xmlParserBase* father)
521 {
522   // DEBTRACE("dataParser::init()");
523   _state = XMLINVALUE;
524   _father = father;
525   _stackState.push(_state);
526   if (p) getAttributes(p);
527 }
528
529
530 void dataParser::onStart (const XML_Char* elem, const xmlChar** p)
531 {
532   string element(elem);
533   // cerr << "data type " << element << endl;
534   stateParser *parser = 0;
535   if      (element == "value")      parser = new valueParser();
536   else
537     { 
538       _what = "expected value, got <" + element + ">";
539       _state = XMLFATALERROR;
540       stopParse(_what);
541     }
542   if (parser)
543     {
544       _stackParser.push(parser);
545       XML_SetUserData(_xmlParser, parser);
546       parser->init(p, this);
547     }
548 }
549
550 void dataParser::addData(std::string value)
551 {
552   _dataList.push_back(value);
553 }
554
555 void dataParser::onEnd   (const XML_Char* name)
556 {
557   // cerr << "dataParser::onEnd " << name << endl;
558   string val = "<data>";
559   while (!_dataList.empty())
560     {
561       val += _dataList.front();
562       _dataList.pop_front();
563     }
564   val += "</data>";
565   // cerr << val << endl;
566   _father->addData(val);
567   stateParser::onEnd(name);
568 }
569
570 // ----------------------------------------------------------------------------
571
572 void simpleTypeParser::init(const xmlChar** p, xmlParserBase* father)
573 {
574   //  DEBTRACE("simpleTypeParser::init()");
575   _state = XMLINVALUE;
576   _father = father;
577   _stackState.push(_state);
578   if (p) getAttributes(p);
579 }
580
581 void simpleTypeParser::onStart (const XML_Char* elem, const xmlChar** p)
582 {
583   string element(elem);
584   _what = "expected nothing, got <" + element + ">";
585   _state = XMLFATALERROR;
586   stopParse(_what);
587 }
588
589 void simpleTypeParser::onEnd   (const XML_Char* name)
590 {
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);
596 }
597
598 void simpleTypeParser::charData(std::string data)
599 {
600   // cerr << "simple data " << data << endl;
601   _data = data;
602 }
603
604
605
606 // ----------------------------------------------------------------------------
607
608 stateLoader::stateLoader(xmlParserBase* parser,
609                          YACS::ENGINE::Proc* p) : xmlReader(parser)
610 {
611   _runtime = getRuntime();
612   _p = p;
613 }
614
615 void stateLoader::parse(std::string xmlState)
616 {
617   stateParser *parser = dynamic_cast<stateParser*> (_rootParser);
618   parser->setProc(_p);
619   parser->setRuntime(_runtime);
620
621   xmlReader::parse(xmlState);
622
623   cerr << parser->_state << endl;
624   switch (parser->_state)
625     {
626     case XMLNOCONTEXT:
627     case XMLDONE:
628       {
629         cerr << "parse OK" << endl;
630         break;
631       }
632     case XMLFATALERROR:
633       {
634         string what = "Abort Parse: " + parser->_what;
635         throw Exception(what);
636         break;
637       }
638     default:
639       {
640         string what = "Abort Parse: unknown execution problem";
641         throw Exception(what);
642         break;
643       }
644     }
645 }
646