]> SALOME platform Git repositories - modules/yacs.git/blob - src/yacsloader/LoadState.cxx
Salome HOME
Work in progress save and load state.
[modules/yacs.git] / src / yacsloader / LoadState.cxx
1 // Copyright (C) 2006-2015  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "LoadState.hxx"
21 #include "Proc.hxx"
22 #include "Node.hxx"
23 #include "ForLoop.hxx"
24 #include "WhileLoop.hxx"
25 #include "Switch.hxx"
26 #include "InGate.hxx"
27 #include "Runtime.hxx"
28 #include "InputPort.hxx"
29 #include "ElementaryNode.hxx"
30 #include "ForEachLoop.hxx"
31 #include "Any.hxx"
32
33 #include <iostream>
34 #include <string>
35 #include <cstdlib>
36 #include <cstdarg>
37 #include <cassert>
38
39 //#define _DEVDEBUG_
40 #include "YacsTrace.hxx"
41
42 using namespace YACS::ENGINE;
43 using namespace std;
44
45 XMLReadState    stateParser::_state;
46 std::string     stateParser::_what;
47 std::stack<XMLReadState> stateParser::_stackState;
48 Proc* stateParser::_p;
49 Runtime* stateParser::_runtime;
50 std::map<std::string, YACS::StatesForNode> stateParser::_nodeStateValue;
51 std::map<std::string, YACS::StatesForNode> stateParser::_nodeStates;
52
53 // ----------------------------------------------------------------------------
54 void stateParser::setProc(Proc* p)
55 {
56   _p= p;
57 }
58
59 void stateParser::setRuntime(Runtime* runtime)
60 {
61   _runtime = runtime;
62 }
63
64 void stateParser::init(const xmlChar** p, xmlParserBase* father)
65 {
66   DEBTRACE("stateParser::init()");
67   _state = XMLNOCONTEXT;
68   _father = father;
69   _stackState.push(_state);
70   _nodeStateValue["READY"] =YACS::READY;
71   _nodeStateValue["TOLOAD"] =YACS::TOLOAD;
72   _nodeStateValue["LOADED"] =YACS::LOADED;
73   _nodeStateValue["TOACTIVATE"] =YACS::TOACTIVATE;
74   _nodeStateValue["ACTIVATED"] =YACS::ACTIVATED;
75   _nodeStateValue["DESACTIVATED"] =YACS::DESACTIVATED;
76   _nodeStateValue["DONE"] =YACS::DONE;
77   _nodeStateValue["SUSPENDED"] =YACS::SUSPENDED;
78   _nodeStateValue["LOADFAILED"] =YACS::LOADFAILED;
79   _nodeStateValue["EXECFAILED"] =YACS::EXECFAILED;
80   _nodeStateValue["PAUSE"] =YACS::PAUSE;
81   _nodeStateValue["INTERNALERR"] =YACS::INTERNALERR;
82   _nodeStateValue["DISABLED"] =YACS::DISABLED;
83   _nodeStateValue["FAILED"] =YACS::FAILED;
84   _nodeStateValue["ERROR"] =YACS::ERROR;
85 }
86
87
88 void stateParser::onStart (const XML_Char* elem, const xmlChar** p)
89 {
90   DEBTRACE("stateParser::onStart");
91   string element(elem);
92   stateParser *parser = 0;
93   if (element == "graphState") parser = new graphParser();
94   else
95     { 
96       _what = "expected <graphState>, got <" + element + ">";
97       _state = XMLFATALERROR;
98       stopParse(_what);
99     }
100   if (parser)
101     {
102       _stackParser.push(parser);
103       XML_SetUserData(_xmlParser, parser);
104       parser->init(p);
105     }
106 }
107
108
109 void stateParser::onEnd   (const XML_Char* name)
110 {
111   _stackState.pop();
112   _state = _stackState.top();
113   //cerr << "end " << name << " " << _stackParser.size() << " " << _state << endl;
114 }
115
116
117 void stateParser::charData(std::string data)
118 {
119   //cerr << "data " << data << endl;
120 }
121
122 // ----------------------------------------------------------------------------
123
124 void graphParser::init(const xmlChar** p, xmlParserBase* father)
125 {
126   //  DEBTRACE("graphParser::init()");
127   _state = XMLINGRAPH;
128   _father = father;
129   _stackState.push(_state);
130   if (p) getAttributes(p);
131 }
132
133 void graphParser::onStart (const XML_Char* elem, const xmlChar** p)
134 {
135   string element(elem);
136   stateParser *parser = 0;
137   if (element == "node") parser = new nodeParser();
138   else
139     { 
140       _what = "expected <node>, got <" + element + ">";
141       _state = XMLFATALERROR;
142       stopParse(_what);
143     }
144   if (parser)
145     {
146       _stackParser.push(parser);
147       XML_SetUserData(_xmlParser, parser);
148       parser->init(p, this);
149     }
150 }
151
152
153 void graphParser::onEnd   (const XML_Char* name)
154 {
155   std::map<std::string, YACS::StatesForNode>::const_iterator it;
156   for (it = _nodeStates.begin(); it != _nodeStates.end(); it++)
157     {
158       Node *node =0;
159       string nodeName = it->first;
160       DEBTRACE("nodeName = " << nodeName);
161       if(_p->getName() == nodeName)
162         node = _p;
163       else
164         node = _p->getChildByName(nodeName);
165
166       InGate* inGate = node->getInGate();
167       list<OutGate*> backlinks = inGate->getBackLinks();
168       for (list<OutGate*>::iterator io = backlinks.begin(); io != backlinks.end(); io++)
169         {
170           Node* fromNode = (*io)->getNode();
171           string fromName;
172           if (fromNode == _p) fromName = fromNode->getName();
173           else fromName = _p->getChildName(fromNode);
174           if (_nodeStates[fromName] == YACS::DONE)
175             {
176               DEBTRACE("   fromNode = " << fromName);
177               inGate->setPrecursorDone(*io);
178             }
179         }
180     }
181   stateParser::onEnd(name);
182 }
183
184 // ----------------------------------------------------------------------------
185
186 class outputParser: public stateParser
187 {
188 public:
189   virtual void init(const xmlChar** p, xmlParserBase* father=0)
190   {
191     //DEBTRACE("outputParser::init()");
192     _state = XMLNOCONTEXT;
193     _father = father;
194     YASSERT( dynamic_cast<nodeParser*> (father));
195     _stackState.push(_state);
196     if (p) getAttributes(p);
197   }
198   virtual void onStart (const XML_Char* elem, const xmlChar** p)
199   {
200     //DEBTRACE("outputParser::onStart" << elem);
201     string element(elem);
202     stateParser *parser = 0;
203     if      (element == "name")      parser = new attrParser();
204     else if (element == "value")     parser = new valueParser();
205     else
206     { 
207       _what = "expected name or value, got <" + element + ">";
208       _state = XMLFATALERROR;
209       stopParse(_what);
210     }
211     if (parser)
212     {
213       _stackParser.push(parser);
214       XML_SetUserData(_xmlParser, parser);
215       parser->init(p, this);
216     }
217   }
218   virtual void onEnd   (const XML_Char* name)
219   {
220     //DEBTRACE("outputParser::onEnd" << elem);
221     //DEBTRACE("portName: " << _mapAttrib["name"] << "value: " << _data );
222     stateParser::onEnd(name);
223   }
224   virtual void addData(std::string /*value*/)
225   {
226     //DEBTRACE("outputParser::addData" << elem);
227   }
228 };
229
230 // ----------------------------------------------------------------------------
231
232 void nodeParser::init(const xmlChar** p, xmlParserBase* father)
233 {
234   DEBTRACE("nodeParser::init()");
235   _state = XMLINNODE;
236   _father = father;
237   _stackState.push(_state);
238   if (p) getAttributes(p);
239 }
240
241
242 void nodeParser::onStart (const XML_Char* elem, const xmlChar** p)
243 {
244   DEBTRACE("nodeParser::onStart" << elem);
245   string element(elem);
246   stateParser *parser = 0;
247   if (element == "inputPort")      parser = new portParser();
248   else if (element == "name")      parser = new attrParser();
249   else if (element == "state")     parser = new attrParser();
250   else if (element == "nsteps")    parser = new attrParser();
251   else if (element == "nbdone")    parser = new attrParser();
252   else if (element == "condition") parser = new attrParser();
253   else if (element == "outputPort") parser = new outputParser();
254   else if (element == "loopOutputPort") parser = new loopPortParser();
255   else
256     { 
257       _what = "expected name, state or inputPort, got <" + element + ">";
258       _state = XMLFATALERROR;
259       stopParse(_what);
260     }
261   if (parser)
262     {
263       _stackParser.push(parser);
264       XML_SetUserData(_xmlParser, parser);
265       parser->init(p, this);
266     }
267 }
268
269 void nodeParser::onEnd   (const XML_Char* name)
270 {
271   string nodeName = _mapAttrib["name"];
272   string nodeType = _mapAttrib["type"];
273   string nodeState = _mapAttrib["state"];
274   DEBTRACE( "nodeName: " << nodeName << " nodeType: " << nodeType << " nodeState: " << nodeState );
275 //   for (std::map< std::string, Node * >::iterator it=_p->nodeMap.begin(); it != _p->nodeMap.end(); it++)
276 //     cerr << "nodeMap: " << it->first << endl;
277   _nodeStates[nodeName] = _nodeStateValue[nodeState];
278   Node *node =0;
279   if(_p->getName() == nodeName)
280     node=_p;
281   else 
282     node = _p->getChildByName(nodeName);
283
284   YASSERT(_nodeStateValue.find(nodeState) != _nodeStateValue.end());
285   YACS::ENGINE::StateLoader(node, _nodeStateValue[nodeState]);
286
287   if (nodeType == "forLoop")
288     {
289       if (_mapAttrib.find("nsteps") == _mapAttrib.end())
290         {
291           _what = "no attribute nsteps in forLoop " + _mapAttrib["name"];
292           _state = XMLFATALERROR;
293           stopParse(_what);
294         }
295       int nsteps =  atoi(_mapAttrib["nsteps"].c_str());
296
297       if (_mapAttrib.find("nbdone") == _mapAttrib.end())
298         {
299           _what = "no attribute nbdone in forLoop " + _mapAttrib["name"];
300           _state = XMLFATALERROR;
301           stopParse(_what);
302         }
303       int nbdone =  atoi(_mapAttrib["nbdone"].c_str());
304       DEBTRACE("nsteps = " << nsteps << ", nbdone = " << nbdone);
305
306       ForLoop* loop = dynamic_cast<ForLoop*>(node);
307       if (!loop)
308         {
309           _what = "node is not a ForLoop: " + _mapAttrib["name"];
310           _state = XMLFATALERROR;
311           stopParse(_what);
312         }
313       loop->edGetNbOfTimesInputPort()->edInit(nsteps);
314       YACS::ENGINE::NbDoneLoader(loop, nbdone);
315     }
316
317   else if (nodeType == "whileLoop")
318     {
319       if (_mapAttrib.find("nbdone") == _mapAttrib.end())
320         {
321           _what = "no attribute nbdone in forLoop " + _mapAttrib["name"];
322           _state = XMLFATALERROR;
323           stopParse(_what);
324         }
325       int nbdone =  atoi(_mapAttrib["nbdone"].c_str());
326
327       if (_mapAttrib.find("condition") == _mapAttrib.end())
328         {
329           _what = "no attribute condition in forLoop " + _mapAttrib["name"];
330           _state = XMLFATALERROR;
331           stopParse(_what);
332         }
333       bool condition =  atoi(_mapAttrib["condition"].c_str());
334       DEBTRACE("condition = " << condition << ", nbdone = " << nbdone);
335
336       WhileLoop* loop = dynamic_cast<WhileLoop*>(node);
337       if (!loop)
338         {
339           _what = "node is not a WhileLoop: " + _mapAttrib["name"];
340           _state = XMLFATALERROR;
341           stopParse(_what);
342         }
343       loop->edGetConditionPort()->edInit(condition);
344       YACS::ENGINE::NbDoneLoader(loop, nbdone);
345     }
346
347   else if (nodeType == "switch")
348     {
349       if (_mapAttrib.find("condition") == _mapAttrib.end())
350         {
351           _what = "no attribute condition in switch " + _mapAttrib["name"];
352           _state = XMLFATALERROR;
353           stopParse(_what);
354         }
355       int condition =  atoi(_mapAttrib["condition"].c_str());
356       DEBTRACE("condition = " << condition);
357
358       Switch* mySwitch = dynamic_cast<Switch*>(node);
359       if (!mySwitch)
360         {
361           _what = "node is not a Switch: " + _mapAttrib["name"];
362           _state = XMLFATALERROR;
363           stopParse(_what);
364         }
365       mySwitch->edGetConditionPort()->edInit(condition);
366     }
367
368   stateParser::onEnd(name);
369 }
370
371 // ----------------------------------------------------------------------------
372
373 void attrParser::init(const xmlChar** p, xmlParserBase* father)
374 {
375   DEBTRACE("attrParser::init()");
376   //_state = XMLINNODE;
377   _father = father;
378   _stackState.push(_state); // keep current state
379   if (p) getAttributes(p);
380 }
381
382
383 void attrParser::onStart (const XML_Char* elem, const xmlChar** p)
384 {
385   string element(elem);
386   _what = "expected nothing, got <" + element + ">";
387   _state = XMLFATALERROR;
388   stopParse(_what);
389 }
390
391 void attrParser::charData(std::string data)
392 {
393   _attrValue = data;
394 }
395
396 void attrParser::onEnd   (const XML_Char* name)
397 {
398   // cerr << "end attrParser " << name << " " << _stackParser.size() << endl;
399   YASSERT(_father);
400   _father->setAttribute((char*)name, _attrValue);
401   stateParser::onEnd(name);
402 }
403
404 // ----------------------------------------------------------------------------
405
406 void portParser::init(const xmlChar** p, xmlParserBase* father)
407 {
408   DEBTRACE("portParser::init()");
409   _state = XMLINPORT;
410   _father = father;
411   YASSERT( dynamic_cast<nodeParser*> (father));
412   _stackState.push(_state);
413   if (p) getAttributes(p);
414 }
415
416
417 void portParser::onStart (const XML_Char* elem, const xmlChar** p)
418 {
419   DEBTRACE("portParser::onStart" << elem);
420   string element(elem);
421   stateParser *parser = 0;
422   if      (element == "name")      parser = new attrParser();
423   else if (element == "value")     parser = new valueParser();
424   else
425     { 
426       _what = "expected name or value, got <" + element + ">";
427       _state = XMLFATALERROR;
428       stopParse(_what);
429     }
430   if (parser)
431     {
432       _stackParser.push(parser);
433       XML_SetUserData(_xmlParser, parser);
434       parser->init(p, this);
435     }
436 }
437
438 void portParser::addData(std::string value)
439 {
440   _data = value;
441 }
442
443 void portParser::onEnd   (const XML_Char* name)
444 {
445   DEBTRACE("portName: " << _mapAttrib["name"] << "value: " << _data );
446   string nodeName = _father->getAttribute("name");
447   string nodeType = _father->getAttribute("type");
448   Node *node = _p->getChildByName(nodeName);
449   if (nodeType == "elementaryNode")
450     {
451       ElementaryNode* eNode = dynamic_cast<ElementaryNode*>(node);
452       YASSERT(eNode);
453       InputPort *port = eNode->getInputPort(_mapAttrib["name"]);
454       if(_data != "")
455         port->edInit("XML",_data.c_str());
456     }
457   else if (nodeType == "forLoop")
458     {
459       string what="no way to set a port value on port " +  _mapAttrib["name"];
460       what += " in node " + nodeName + " of type " + nodeType;
461       throw Exception(what);
462     }
463   else if (nodeType == "whileLoop")
464     {
465       string what="no way to set a port value on port " +  _mapAttrib["name"];
466       what += " in node " + nodeName + " of type " + nodeType;
467       throw Exception(what);
468     }
469   else if (nodeType == "switch")
470     {
471       string what="no way to set a port value on port " +  _mapAttrib["name"];
472       what += " in node " + nodeName + " of type " + nodeType;
473       throw Exception(what);
474     }
475   else if (nodeType == "foreachLoop")
476     {
477       string what="no way to set a port value on port " +  _mapAttrib["name"];
478       what += " in node " + nodeName + " of type " + nodeType;
479       throw Exception(what);
480     }
481   else 
482     {
483       string what="no way to set a port value on port " +  _mapAttrib["name"];
484       what += " in node " + nodeName + " of type " + nodeType;
485       throw Exception(what);
486     }
487
488   stateParser::onEnd(name);
489 }
490
491 // ----------------------------------------------------------------------------
492
493 void valueParser::init(const xmlChar** p, xmlParserBase* father)
494 {
495   DEBTRACE("valueParser::init()");
496   _state = XMLINVALUE;
497   _father = father;
498   _stackState.push(_state);
499   if (p) getAttributes(p);
500 }
501
502
503 void valueParser::onStart (const XML_Char* elem, const xmlChar** p)
504 {
505   string element(elem);
506   DEBTRACE("value type " << element );
507   stateParser *parser = 0;
508   if      (element == "data")      parser = new dataParser();
509   else if (element == "array")     parser = new arrayParser();
510   else                             parser = new simpleTypeParser();
511   if (parser)
512     {
513       _stackParser.push(parser);
514       XML_SetUserData(_xmlParser, parser);
515       parser->init(p, this);
516     }
517 }
518
519 void valueParser::addData(std::string value)
520 {
521   _data = "<value>" + value + "</value>";
522 }
523
524 void valueParser::onEnd   (const XML_Char* name)
525 {
526   DEBTRACE( _data );
527   _father->addData(_data);
528   string elem = (char *) name;
529   //if (elem == "value" || elem == "data" || elem == "array")
530   stateParser::onEnd(name);
531   //else YASSERT(0); //DEBTRACE("valueParser::onEnd " << elem);
532 }
533
534 // ----------------------------------------------------------------------------
535
536 void arrayParser::init(const xmlChar** p, xmlParserBase* father)
537 {
538   DEBTRACE("arrayParser::init()");
539   _state = XMLINVALUE;
540   _father = father;
541   _stackState.push(_state);
542   if (p) getAttributes(p);
543 }
544
545
546 void arrayParser::onStart (const XML_Char* elem, const xmlChar** p)
547 {
548   string element(elem);
549   DEBTRACE("array type " << element);
550   stateParser *parser = 0;
551   if      (element == "data")      parser = new dataParser();
552   else
553     { 
554       _what = "expected data, got <" + element + ">";
555       _state = XMLFATALERROR;
556       stopParse(_what);
557     }
558   if (parser)
559     {
560       _stackParser.push(parser);
561       XML_SetUserData(_xmlParser, parser);
562       parser->init(p, this);
563     }
564 }
565
566 void arrayParser::addData(std::string value)
567 {
568   string val = "<array>" + value + "</array>";
569   _data = val;
570 }
571
572
573 void arrayParser::onEnd   (const XML_Char* name)
574 {
575   // cerr << "arrayParser::onEnd " << name << endl;
576   // cerr << _data << endl;
577   _father->addData(_data);
578   stateParser::onEnd(name);
579 }
580
581 // ----------------------------------------------------------------------------
582
583 void dataParser::init(const xmlChar** p, xmlParserBase* father)
584 {
585   DEBTRACE("dataParser::init()");
586   _state = XMLINVALUE;
587   _father = father;
588   _stackState.push(_state);
589   if (p) getAttributes(p);
590 }
591
592
593 void dataParser::onStart (const XML_Char* elem, const xmlChar** p)
594 {
595   string element(elem);
596   DEBTRACE("data type " << element );
597   stateParser *parser = 0;
598   if      (element == "value")      parser = new valueParser();
599   else
600     { 
601       _what = "expected value, got <" + element + ">";
602       _state = XMLFATALERROR;
603       stopParse(_what);
604     }
605   if (parser)
606     {
607       _stackParser.push(parser);
608       XML_SetUserData(_xmlParser, parser);
609       parser->init(p, this);
610     }
611 }
612
613 void dataParser::addData(std::string value)
614 {
615   _dataList.push_back(value);
616 }
617
618 void dataParser::onEnd   (const XML_Char* name)
619 {
620   // cerr << "dataParser::onEnd " << name << endl;
621   string val = "<data>";
622   while (!_dataList.empty())
623     {
624       val += _dataList.front();
625       _dataList.pop_front();
626     }
627   val += "</data>";
628   // cerr << val << endl;
629   _father->addData(val);
630   stateParser::onEnd(name);
631 }
632
633 // ----------------------------------------------------------------------------
634
635 void simpleTypeParser::init(const xmlChar** p, xmlParserBase* father)
636 {
637   DEBTRACE("simpleTypeParser::init()");
638   _state = XMLINVALUE;
639   _father = father;
640   _stackState.push(_state);
641   if (p) getAttributes(p);
642 }
643
644 void simpleTypeParser::onStart (const XML_Char* elem, const xmlChar** p)
645 {
646   string element(elem);
647   _what = "expected nothing, got <" + element + ">";
648   _state = XMLFATALERROR;
649   stopParse(_what);
650 }
651
652 void simpleTypeParser::onEnd   (const XML_Char* name)
653 {
654   string val = string("<") + (char*) name + ">" + _data + "</"  + (char*) name +">";
655   DEBTRACE( val );
656   _father->addData(val);
657   stateParser::onEnd(name);
658 }
659
660 void simpleTypeParser::charData(std::string data)
661 {
662   _data = _data + data;
663 }
664
665 // ----------------------------------------------------------------------------
666
667 void loopPortParser::init(const xmlChar** p, xmlParserBase* father)
668 {
669   DEBTRACE("loopPortParser::init()");
670   _state = XMLINPORT;
671   _father = father;
672   _stackState.push(_state);
673   if (p) getAttributes(p);
674 }
675
676 void loopPortParser::onStart(const XML_Char* elem, const xmlChar** p)
677 {
678   DEBTRACE("loopPortParser::onStart" << elem);
679   string element(elem);
680   stateParser *parser = 0;
681   if (element == "name")      parser = new attrParser();
682   else if (element == "sample") parser = new sampleParser();
683   else
684     { 
685       _what = "expected name or sample, got <" + element + ">";
686       _state = XMLFATALERROR;
687       stopParse(_what);
688     }
689   if (parser)
690     {
691       _stackParser.push(parser);
692       XML_SetUserData(_xmlParser, parser);
693       parser->init(p, this);
694     }
695 }
696
697 void loopPortParser::onEnd(const XML_Char* name)
698 {
699   string portName = _mapAttrib["name"];
700   string nodeName = _father->getAttribute("name");
701   string nodeType = _father->getAttribute("type");
702   Node *node = _p->getChildByName(nodeName);
703   if (nodeType != "forEachLoop")
704   {
705     _what = "loopOutputPort attribute is not valid for node <" + nodeName + ">";
706     _state = XMLFATALERROR;
707     stopParse(_what);
708   }
709 }
710
711 void loopPortParser::charData(std::string data)
712 {
713 }
714
715 // ----------------------------------------------------------------------------
716
717 void sampleParser::init(const xmlChar** p, xmlParserBase* father)
718 {
719   DEBTRACE("sampleParser::init()");
720   _state = XMLINPORT;
721   _father = father;
722   _stackState.push(_state);
723   if (p) getAttributes(p);
724 }
725
726 void sampleParser::onStart(const XML_Char* elem, const xmlChar** p)
727 {
728   DEBTRACE("sampleParser::onStart" << elem);
729   string element(elem);
730   stateParser *parser = 0;
731   if (element == "index")      parser = new attrParser();
732   else if (element == "value") parser = new valueParser();
733   else
734     { 
735       _what = "expected index or value, got <" + element + ">";
736       _state = XMLFATALERROR;
737       stopParse(_what);
738     }
739   if (parser)
740     {
741       _stackParser.push(parser);
742       XML_SetUserData(_xmlParser, parser);
743       parser->init(p, this);
744     }
745 }
746
747 void sampleParser::onEnd(const XML_Char* name)
748 {
749   if (_mapAttrib.find("index") == _mapAttrib.end())
750     {
751       _what = "no attribute index in sample ";
752       _state = XMLFATALERROR;
753       stopParse(_what);
754     }
755   int index =  atoi(_mapAttrib["index"].c_str());
756   Any * value;
757   value = xmlToAny();
758 }
759
760 void sampleParser::charData(std::string data)
761 {
762 }
763
764 Any* sampleParser::xmlToAny() throw(ConversionException)
765 {
766   xmlDocPtr doc;
767   xmlNodePtr cur;
768   YACS::ENGINE::Any *ob=NULL;
769   {
770     doc = xmlParseMemory(_data.c_str(), _data.length());
771     if (doc == NULL )
772       {
773         stringstream msg;
774         msg << "Problem in conversion: XML Document not parsed successfully ";
775         msg << " (" << __FILE__ << ":" << __LINE__ << ")";
776         throw ConversionException(msg.str());
777       }
778     cur = xmlDocGetRootElement(doc);
779     if (cur == NULL)
780       {
781         xmlFreeDoc(doc);
782         stringstream msg;
783         msg << "Problem in conversion: empty XML Document";
784         msg << " (" << __FILE__ << ":" << __LINE__ << ")";
785         throw ConversionException(msg.str());
786       }
787     while (cur != NULL)
788       {
789         if ((!xmlStrcmp(cur->name, (const xmlChar *)"value")))
790           {
791             ob=convertXmlNeutral(edGetType(),doc,cur);
792             break;
793           }
794         cur = cur->next;
795       }
796     xmlFreeDoc(doc);
797     if(ob==NULL)
798       {
799         stringstream msg;
800         msg << "Problem in conversion: incorrect XML value";
801         msg << " (" << __FILE__ << ":" << __LINE__ << ")";
802         throw ConversionException(msg.str());
803       }
804   }
805   return ob;
806 }
807
808 // ----------------------------------------------------------------------------
809
810 stateLoader::stateLoader(xmlParserBase* parser,
811                          YACS::ENGINE::Proc* p) : xmlReader(parser)
812 {
813   _runtime = getRuntime();
814   _p = p;
815 }
816
817 void stateLoader::parse(std::string xmlState)
818 {
819   DEBTRACE("stateLoader::parse");
820   stateParser *parser = dynamic_cast<stateParser*> (_rootParser);
821   parser->setProc(_p);
822   parser->setRuntime(_runtime);
823
824   xmlReader::parse(xmlState);
825
826   DEBTRACE(parser->_state);
827   switch (parser->_state)
828     {
829     case XMLNOCONTEXT:
830     case XMLDONE:
831       {
832         DEBTRACE("parse OK");
833         break;
834       }
835     case XMLFATALERROR:
836       {
837         string what = "Abort Parse: " + parser->_what;
838         throw Exception(what);
839         break;
840       }
841     default:
842       {
843         string what = "Abort Parse: unknown execution problem";
844         throw Exception(what);
845         break;
846       }
847     }
848 }
849
850 void YACS::ENGINE::loadState(YACS::ENGINE::Proc *p,const std::string& xmlStateFile)
851 {
852   DEBTRACE("YACS::ENGINE::loadState");
853   p->init();
854   p->exUpdateState();
855   stateParser* rootParser = new stateParser();
856   stateLoader myStateLoader(rootParser, p);
857   myStateLoader.parse(xmlStateFile);
858 }