Salome HOME
copy tag mergefrom_BR_V0_1_CC_Salome_04oct07
[modules/yacs.git] / src / engine / VisitorSaveState.cxx
1
2 #include "ElementaryNode.hxx"
3 #include "Bloc.hxx"
4 #include "Proc.hxx"
5 #include "ForEachLoop.hxx"
6 #include "Loop.hxx"
7 #include "ForLoop.hxx"
8 #include "WhileLoop.hxx"
9 #include "Switch.hxx"
10 #include "InputPort.hxx"
11 #include "InlineNode.hxx"
12 #include "ServiceNode.hxx"
13 #include "ServiceInlineNode.hxx"
14
15 #include "VisitorSaveState.hxx"
16
17 #include <iostream>
18 #include <string>
19
20 using namespace YACS::ENGINE;
21 using namespace std;
22
23 //#define _DEVDEBUG_
24 #include "YacsTrace.hxx"
25
26 VisitorSaveState::VisitorSaveState(ComposedNode *root): Visitor(root)
27 {
28   _nodeStateName[YACS::INITED] ="INITED";
29   _nodeStateName[YACS::TOLOAD] ="TOLOAD";
30   _nodeStateName[YACS::LOADED] ="LOADED";
31   _nodeStateName[YACS::TOACTIVATE] ="TOACTIVATE";
32   _nodeStateName[YACS::ACTIVATED] ="ACTIVATED";
33   _nodeStateName[YACS::DESACTIVATED] ="DESACTIVATED";
34   _nodeStateName[YACS::DONE] ="DONE";
35   _nodeStateName[YACS::SUSPENDED] ="SUSPENDED";
36   _nodeStateName[YACS::LOADFAILED] ="LOADFAILED";
37   _nodeStateName[YACS::EXECFAILED] ="EXECFAILED";
38   _nodeStateName[YACS::PAUSE] ="PAUSE";
39   _nodeStateName[YACS::INTERNALERR] ="INTERNALERR";
40   _nodeStateName[YACS::DISABLED] ="DISABLED";
41   _nodeStateName[YACS::FAILED] ="FAILED";
42   _nodeStateName[YACS::ERROR] ="ERROR";
43 }
44
45 VisitorSaveState::~VisitorSaveState()
46 {
47   if (_out)
48     {
49       _out << "</graphState>" << endl;
50       _out.close();
51     }
52 }
53
54 void VisitorSaveState::openFileDump(std::string xmlDump) throw(Exception)
55 {
56   _out.open(xmlDump.c_str(), ios::out);
57   if (!_out)
58     {
59       string what = "Impossible to open file for writing: " + xmlDump;
60       throw Exception(what);
61     }
62   _out << "<?xml version='1.0'?>" << endl;
63   _out << "<graphState>" << endl;
64 }
65
66 void VisitorSaveState::closeFileDump()
67 {
68   if (!_out) throw Exception("No file open for dump state");
69   _out << "</graphState>" << endl;
70   _out.close();
71 }
72
73 void VisitorSaveState::visitElementaryNode(ElementaryNode *node)
74 {
75   if (!_out) throw Exception("No file open for dump state");
76   string name = _root->getChildName(node);
77   DEBTRACE("VisitorSaveState::visitElementaryNode --- " << name);
78   _out << "  <node type='elementaryNode'>" << endl;
79   _out << "    <name>" << name << "</name>" << endl;
80   int nodeState = node->getState();
81   _out << "    <state>" << _nodeStateName[nodeState] << "</state>" << endl;
82
83   list<InputPort *> setOfInputPort = node->getSetOfInputPort();
84   list<InputPort *>::iterator iter;
85   for(iter = setOfInputPort.begin(); iter != setOfInputPort.end(); iter++)
86     {
87       _out << "    <inputPort>" << endl;
88       _out << "      <name>" << (*iter)->getName() << "</name>" << endl;
89       try
90         {
91           _out << "      ";
92           _out << (*iter)->dump();
93         }
94       catch (YACS::Exception &e)
95         {
96           DEBTRACE("caught YACS:Exception: " << e.what());
97           _out << "<value><error><![CDATA[" << e.what() << "]]></error></value>" << endl;
98         }
99       _out << "    </inputPort>" << endl;
100     }
101   _out << "  </node>" << endl;
102 }
103
104 void VisitorSaveState::visitBloc(Bloc *node)
105 {
106   node->ComposedNode::accept(this);
107   if (!_out) throw Exception("No file open for dump state");
108   string name = _root->getName();
109   if (static_cast<ComposedNode*>(node) != _root) name = _root->getChildName(node);
110   DEBTRACE("VisitorSaveState::visitBloc ------------- " << name);
111   _out << "  <node type='bloc'>" << endl;
112   _out << "    <name>" << name << "</name>" << endl;
113   _out << "    <state>" << _nodeStateName[node->getState()] << "</state>" << endl;
114
115   _out << "  </node>" << endl;
116 }
117
118 void VisitorSaveState::visitProc(Proc *node)
119 {
120   node->ComposedNode::accept(this);
121   if (!_out) throw Exception("No file open for dump state");
122   string name = _root->getName();
123   if (static_cast<ComposedNode*>(node) != _root) name = _root->getChildName(node);
124   DEBTRACE("VisitorSaveState::visitProc ------------- " << name);
125   _out << "  <node type='proc'>" << endl;
126   _out << "    <name>" << name << "</name>" << endl;
127   _out << "    <state>" << _nodeStateName[node->getState()] << "</state>" << endl;
128
129   _out << "  </node>" << endl;
130 }
131
132 void VisitorSaveState::visitForEachLoop(ForEachLoop *node)
133 {
134   node->ComposedNode::accept(this);
135   if (!_out) throw Exception("No file open for dump state");
136   string name = _root->getName();
137   if (static_cast<ComposedNode*>(node) != _root) name = _root->getChildName(node);
138   DEBTRACE("VisitorSaveState::visitForEachLoop ------ " << name);
139   _out << "  <node type='forEachLoop'>" << endl;
140   _out << "    <name>" << name << "</name>" << endl;
141   _out << "    <state>" << _nodeStateName[node->getState()] << "</state>" << endl;
142
143   _out << "  </node>" << endl;
144 }
145
146 void VisitorSaveState::visitLoop(Loop *node)
147 {
148   node->ComposedNode::accept(this);
149   if (!_out) throw Exception("No file open for dump state");
150   string name = _root->getName();
151   if (static_cast<ComposedNode*>(node) != _root) name = _root->getChildName(node);
152   DEBTRACE("VisitorSaveState::visitLoop ------------- " << name);
153   _out << "  <node type ='loop'>" << endl;
154   _out << "    <name>" << name << "</name>" << endl;
155   _out << "    <state>" << _nodeStateName[node->getState()] << "</state>" << endl;
156   _out << "    <nbdone>" << node->getNbOfTurns() << "</nbdone>" << endl;
157
158   _out << "  </node>" << endl;
159 }
160
161 void VisitorSaveState::visitForLoop(ForLoop *node)
162 {
163   node->ComposedNode::accept(this);
164   if (!_out) throw Exception("No file open for dump state");
165   string name = _root->getName();
166   if (static_cast<ComposedNode*>(node) != _root) name = _root->getChildName(node);
167   DEBTRACE("VisitorSaveState::visitForLoop ---------- " << name);
168   _out << "  <node type='forLoop'>" << endl;
169   _out << "    <name>" << name << "</name>" << endl;
170   _out << "    <state>" << _nodeStateName[node->getState()] << "</state>" << endl;
171   _out << "    <nbdone>" << node->getNbOfTurns() << "</nbdone>" << endl;
172   InputPort * ip = node->edGetNbOfTimesInputPort();
173   if (ip->isEmpty())
174     throw Exception("NbOfTimesInputPort in forLoop empty, case not handled yet...");
175   Any *val = static_cast<Any*>(ip->get());
176   int nsteps = val->getIntValue();
177   _out << "    <nsteps>" << nsteps << "</nsteps>" << endl;
178
179   _out << "  </node>" << endl;
180 }
181
182 void VisitorSaveState::visitWhileLoop(WhileLoop *node)
183 {
184   node->ComposedNode::accept(this);
185   if (!_out) throw Exception("No file open for dump state");
186   string name = _root->getName();
187   if (static_cast<ComposedNode*>(node) != _root) name = _root->getChildName(node);
188   DEBTRACE("VisitorSaveState::visitWhileLoop -------- " << name);
189   _out << "  <node type='whileLoop'>" << endl;
190   _out << "    <name>" << name << "</name>" << endl;
191   _out << "    <state>" << _nodeStateName[node->getState()] << "</state>" << endl;
192   _out << "    <nbdone>" << node->getNbOfTurns() << "</nbdone>" << endl;
193   InputPort * ip = node->edGetConditionPort();
194   if (ip->isEmpty())
195     throw Exception("condition in WhileLoop empty, case not handled yet...");
196   Any *val = static_cast<Any*>(ip->get());
197   bool condition = val->getBoolValue();
198   _out << "    <condition>" << condition << "</condition>" << endl;
199
200   _out << "  </node>" << endl;
201 }
202
203 void VisitorSaveState::visitSwitch(Switch *node)
204 {
205   node->ComposedNode::accept(this);
206   if (!_out) throw Exception("No file open for dump state");
207   string name = _root->getName();
208   if (static_cast<ComposedNode*>(node) != _root) name = _root->getChildName(node);
209   DEBTRACE("VisitorSaveState::visitSwitch ----------- " << name);
210   _out << "  <node type='switch'>" << endl;
211   _out << "    <name>" << name << "</name>" << endl;
212   _out << "    <state>" << _nodeStateName[node->getState()] << "</state>" << endl;
213   InputPort * ip = node->edGetConditionPort();
214   if (ip->isEmpty())
215     throw Exception("condition in switch empty, case not handled yet...");
216   Any *val = static_cast<Any*>(ip->get());
217   int condition = val->getIntValue();
218   _out << "    <condition>" << condition << "</condition>" << endl;
219  
220   _out << "  </node>" << endl;
221 }
222
223 void VisitorSaveState::visitInlineNode(InlineNode *node)
224 {
225   visitElementaryNode(node);
226 }
227
228 void VisitorSaveState::visitInlineFuncNode(InlineFuncNode *node)
229 {
230   visitElementaryNode(node);
231 }
232
233 void VisitorSaveState::visitServiceNode(ServiceNode *node)
234 {
235   visitElementaryNode(node);
236 }
237
238
239 void VisitorSaveState::visitServiceInlineNode(ServiceInlineNode *node)
240 {
241   visitElementaryNode(node);
242 }