1 // Copyright (C) 2006-2008 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "ElementaryNode.hxx"
22 #include "ForEachLoop.hxx"
24 #include "ForLoop.hxx"
25 #include "WhileLoop.hxx"
27 #include "InputPort.hxx"
28 #include "InlineNode.hxx"
29 #include "ServiceNode.hxx"
30 #include "ServiceInlineNode.hxx"
31 #include "DataNode.hxx"
33 #include "VisitorSaveState.hxx"
38 using namespace YACS::ENGINE;
42 #include "YacsTrace.hxx"
44 VisitorSaveState::VisitorSaveState(ComposedNode *root): Visitor(root)
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";
63 VisitorSaveState::~VisitorSaveState()
67 _out << "</graphState>" << endl;
72 void VisitorSaveState::openFileDump(std::string xmlDump) throw(Exception)
74 _out.open(xmlDump.c_str(), ios::out);
77 string what = "Impossible to open file for writing: " + xmlDump;
78 throw Exception(what);
80 _out << "<?xml version='1.0'?>" << endl;
81 _out << "<graphState>" << endl;
84 void VisitorSaveState::closeFileDump()
86 if (!_out) throw Exception("No file open for dump state");
87 _out << "</graphState>" << endl;
91 void VisitorSaveState::visitElementaryNode(ElementaryNode *node)
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;
101 list<InputPort *> setOfInputPort = node->getSetOfInputPort();
102 list<InputPort *>::iterator iter;
103 for(iter = setOfInputPort.begin(); iter != setOfInputPort.end(); iter++)
105 _out << " <inputPort>" << endl;
106 _out << " <name>" << (*iter)->getName() << "</name>" << endl;
110 _out << (*iter)->dump();
112 catch (YACS::Exception &e)
114 DEBTRACE("caught YACS:Exception: " << e.what());
115 _out << "<value><error><![CDATA[" << e.what() << "]]></error></value>" << endl;
117 _out << " </inputPort>" << endl;
119 _out << " </node>" << endl;
122 void VisitorSaveState::visitBloc(Bloc *node)
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;
133 _out << " </node>" << endl;
136 void VisitorSaveState::visitProc(Proc *node)
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;
147 _out << " </node>" << endl;
150 void VisitorSaveState::visitForEachLoop(ForEachLoop *node)
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;
161 _out << " </node>" << endl;
164 void VisitorSaveState::visitLoop(Loop *node)
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;
176 _out << " </node>" << endl;
179 void VisitorSaveState::visitForLoop(ForLoop *node)
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();
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;
197 _out << " </node>" << endl;
200 void VisitorSaveState::visitWhileLoop(WhileLoop *node)
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();
213 throw Exception("condition in WhileLoop empty, case not handled yet...");
214 if ( ConditionInputPort* cip = dynamic_cast<ConditionInputPort*>(ip) )
216 bool condition = cip->getValue();
217 _out << " <condition>" << condition << "</condition>" << endl;
220 _out << " </node>" << endl;
223 void VisitorSaveState::visitSwitch(Switch *node)
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();
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;
240 _out << " </node>" << endl;
243 void VisitorSaveState::visitInlineNode(InlineNode *node)
245 visitElementaryNode(node);
248 void VisitorSaveState::visitInlineFuncNode(InlineFuncNode *node)
250 visitElementaryNode(node);
253 void VisitorSaveState::visitServiceNode(ServiceNode *node)
255 visitElementaryNode(node);
259 void VisitorSaveState::visitServiceInlineNode(ServiceInlineNode *node)
261 visitElementaryNode(node);
264 void VisitorSaveState::visitPresetNode(DataNode *node)
266 visitElementaryNode(node);
269 void VisitorSaveState::visitOutNode(DataNode *node)
271 visitElementaryNode(node);
274 void VisitorSaveState::visitStudyInNode(DataNode *node)
276 visitElementaryNode(node);
279 void VisitorSaveState::visitStudyOutNode(DataNode *node)
281 visitElementaryNode(node);