2 #include "ElementaryNode.hxx"
5 #include "ForEachLoop.hxx"
8 #include "WhileLoop.hxx"
10 #include "InputPort.hxx"
11 #include "InlineNode.hxx"
12 #include "ServiceNode.hxx"
13 #include "ServiceInlineNode.hxx"
15 #include "VisitorSaveState.hxx"
20 using namespace YACS::ENGINE;
24 #include "YacsTrace.hxx"
26 VisitorSaveState::VisitorSaveState(ComposedNode *root): Visitor(root)
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";
45 VisitorSaveState::~VisitorSaveState()
49 _out << "</graphState>" << endl;
54 void VisitorSaveState::openFileDump(std::string xmlDump) throw(Exception)
56 _out.open(xmlDump.c_str(), ios::out);
59 string what = "Impossible to open file for writing: " + xmlDump;
60 throw Exception(what);
62 _out << "<?xml version='1.0'?>" << endl;
63 _out << "<graphState>" << endl;
66 void VisitorSaveState::closeFileDump()
68 if (!_out) throw Exception("No file open for dump state");
69 _out << "</graphState>" << endl;
73 void VisitorSaveState::visitElementaryNode(ElementaryNode *node)
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;
83 list<InputPort *> setOfInputPort = node->getSetOfInputPort();
84 list<InputPort *>::iterator iter;
85 for(iter = setOfInputPort.begin(); iter != setOfInputPort.end(); iter++)
87 _out << " <inputPort>" << endl;
88 _out << " <name>" << (*iter)->getName() << "</name>" << endl;
92 _out << (*iter)->dump();
94 catch (YACS::Exception &e)
96 DEBTRACE("caught YACS:Exception: " << e.what());
97 _out << "<value><error><![CDATA[" << e.what() << "]]></error></value>" << endl;
99 _out << " </inputPort>" << endl;
101 _out << " </node>" << endl;
104 void VisitorSaveState::visitBloc(Bloc *node)
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;
115 _out << " </node>" << endl;
118 void VisitorSaveState::visitProc(Proc *node)
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;
129 _out << " </node>" << endl;
132 void VisitorSaveState::visitForEachLoop(ForEachLoop *node)
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;
143 _out << " </node>" << endl;
146 void VisitorSaveState::visitLoop(Loop *node)
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;
158 _out << " </node>" << endl;
161 void VisitorSaveState::visitForLoop(ForLoop *node)
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();
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;
179 _out << " </node>" << endl;
182 void VisitorSaveState::visitWhileLoop(WhileLoop *node)
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();
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;
200 _out << " </node>" << endl;
203 void VisitorSaveState::visitSwitch(Switch *node)
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();
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;
220 _out << " </node>" << endl;
223 void VisitorSaveState::visitInlineNode(InlineNode *node)
225 visitElementaryNode(node);
228 void VisitorSaveState::visitInlineFuncNode(InlineFuncNode *node)
230 visitElementaryNode(node);
233 void VisitorSaveState::visitServiceNode(ServiceNode *node)
235 visitElementaryNode(node);
239 void VisitorSaveState::visitServiceInlineNode(ServiceInlineNode *node)
241 visitElementaryNode(node);