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