1 // Copyright (C) 2006-2014 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, or (at your option) any later version.
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
20 #ifndef _SWITCHPARSERS_HXX_
21 #define _SWITCHPARSERS_HXX_
23 #include "parserBase.hxx"
24 #include "propertyParsers.hxx"
25 #include "nodeParsers.hxx"
28 #include "ForLoop.hxx"
29 #include "ForEachLoop.hxx"
30 #include "WhileLoop.hxx"
34 #include "InlineNode.hxx"
35 #include "ServiceNode.hxx"
36 #include "ServiceInlineNode.hxx"
37 #include "Runtime.hxx"
39 #include "factory.hxx"
46 extern YACS::ENGINE::Proc* currentProc;
47 extern YACS::ENGINE::Runtime* theRuntime;
52 struct casetypeParser:parser
54 static casetypeParser caseParser;
55 void onStart(const XML_Char* el, const XML_Char** attr);
56 void onEnd(const char *el,parser* child);
57 virtual void buildAttr(const XML_Char** attr);
59 virtual void id (const int& n);
60 virtual void property (const myprop& prop);
61 virtual void inline_ (ENGINE::InlineNode* const& n);
62 virtual void sinline (ENGINE::ServiceInlineNode* const& n);
63 virtual void service (ENGINE::ServiceNode* const& n);
64 virtual void server (ENGINE::ServerNode* const& n);
65 virtual void remote (ENGINE::InlineNode* const& n);
66 virtual void node (ENGINE::InlineNode* const& n);
67 virtual void forloop (ENGINE::ForLoop* const& n);
68 virtual void foreach (ENGINE::ForEachLoop* const& n);
69 virtual void optimizer (ENGINE::OptimizerLoop* const& n);
70 virtual void while_ (ENGINE::WhileLoop* const& n);
71 virtual void switch_ (ENGINE::Switch* const& n);
72 virtual void bloc (ENGINE::Bloc* const& n);
73 virtual std::pair<int,ENGINE::Node*> post();
76 std::vector<int> _idStack;
79 struct defaultcasetypeParser:casetypeParser
81 static defaultcasetypeParser defaultcaseParser;
82 virtual void buildAttr(const XML_Char** attr);
86 struct switchtypeParser:parser
88 static switchtypeParser switchParser;
89 void onStart(const XML_Char* el, const XML_Char** attr);
90 void onEnd(const char *el,parser* child);
91 virtual void buildAttr(const XML_Char** attr);
93 virtual void case_ (const std::pair<int,ENGINE::Node*>& p);
94 virtual void default_ (const std::pair<int,ENGINE::Node*>& p);
95 virtual void name (const std::string& name);
96 virtual void state (const std::string& state);
97 virtual void select (const int& s);
98 virtual ENGINE::Switch* post ();
99 // stack to store switches in case of switch in switch
100 std::vector<ENGINE::Switch *> _cnodes;
106 #include "loopParsers.hxx"
111 static std::string switch_t3[]={"inline","sinline","service","server", "remote", "node","forloop","foreach","optimizer","while","switch","bloc",""};
113 void casetypeParser::buildAttr(const XML_Char** attr)
115 this->required("id",attr);
116 for (int i = 0; attr[i]; i += 2)
118 if(std::string(attr[i]) == "id")id(atoi(attr[i+1]));
121 void casetypeParser::pre ()
126 void casetypeParser::id (const int& n)
128 DEBTRACE( "case_id: " << n )
130 //store this level id
131 _idStack.push_back(_id);
132 //store this level name
133 std::stringstream temp;
134 if (_id <0) temp << "m" << -_id << "_";
135 else temp << "p" << _id << "_";
136 std::string fullname=currentProc->names.back()+temp.str();
137 DEBTRACE( "case_fullname: " << fullname )
138 currentProc->names.push_back(fullname);
140 void casetypeParser::property (const myprop& prop)
142 DEBTRACE( "property_set: " << prop._name << prop._value )
144 void casetypeParser::inline_ (ENGINE::InlineNode* const& n)
147 std::string fullname=currentProc->names.back()+ n->getName();
148 currentProc->nodeMap[fullname]=n;
149 currentProc->inlineMap[fullname]=n;
151 void casetypeParser::sinline (ENGINE::ServiceInlineNode* const& n)
154 std::string fullname=currentProc->names.back()+ n->getName();
155 currentProc->nodeMap[fullname]=n;
156 currentProc->serviceMap[fullname]=n;
158 void casetypeParser::service (ENGINE::ServiceNode* const& n)
161 std::string fullname=currentProc->names.back()+ n->getName();
162 currentProc->nodeMap[fullname]=n;
163 currentProc->serviceMap[fullname]=n;
165 void casetypeParser::server (ENGINE::ServerNode* const& n)
168 std::string fullname=currentProc->names.back()+ n->getName();
169 currentProc->nodeMap[fullname]=n;
170 currentProc->inlineMap[fullname]=n;
172 void casetypeParser::remote (ENGINE::InlineNode* const& n)
175 std::string fullname=currentProc->names.back()+ n->getName();
176 currentProc->nodeMap[fullname]=n;
177 currentProc->inlineMap[fullname]=n;
179 void casetypeParser::node (ENGINE::InlineNode* const& n)
182 std::string fullname=currentProc->names.back()+ n->getName();
183 currentProc->nodeMap[fullname]=n;
184 currentProc->inlineMap[fullname]=n;
186 void casetypeParser::forloop (ENGINE::ForLoop* const& n)
189 std::string fullname=currentProc->names.back()+ n->getName();
190 currentProc->nodeMap[fullname]=n;
192 void casetypeParser::foreach (ENGINE::ForEachLoop* const& n)
195 std::string fullname=currentProc->names.back()+ n->getName();
196 currentProc->nodeMap[fullname]=n;
197 fullname += ".splitter";
198 currentProc->nodeMap[fullname]=n->getChildByShortName("splitter");
200 void casetypeParser::optimizer (ENGINE::OptimizerLoop* const& n)
203 std::string fullname=currentProc->names.back()+ n->getName();
204 currentProc->nodeMap[fullname]=n;
205 //fullname += ".splitter";
206 //currentProc->nodeMap[fullname]=n->getChildByShortName("splitter");
208 void casetypeParser::while_ (ENGINE::WhileLoop* const& n)
211 std::string fullname=currentProc->names.back()+ n->getName();
212 currentProc->nodeMap[fullname]=n;
214 void casetypeParser::switch_ (ENGINE::Switch* const& n)
217 std::string fullname=currentProc->names.back()+ n->getName();
218 currentProc->nodeMap[fullname]=n;
220 void casetypeParser::bloc (ENGINE::Bloc* const& n)
223 std::string fullname=currentProc->names.back()+ n->getName();
224 currentProc->nodeMap[fullname]=n;
226 std::pair<int,ENGINE::Node*> casetypeParser::post()
228 DEBTRACE( "case_post" )
229 minchoice(switch_t3,1);
230 //get back this level id
233 //pop back this level name
234 currentProc->names.pop_back();
235 return std::pair<int,ENGINE::Node*>(_id,_cnode);
238 void defaultcasetypeParser::buildAttr(const XML_Char** attr)
240 for (int i = 0; attr[i]; i += 2)
242 DEBTRACE( attr[i] << "=" << attr[i + 1] )
245 void defaultcasetypeParser::pre ()
249 //store this level id
250 _idStack.push_back(_id);
251 //store this level name
252 std::string fullname=currentProc->names.back()+"default_";
253 DEBTRACE( "case_fullname: " << fullname )
254 currentProc->names.push_back(fullname);
257 void switchtypeParser::buildAttr(const XML_Char** attr)
259 this->required("name",attr);
260 for (int i = 0; attr[i]; i += 2)
262 if(std::string(attr[i]) == "name")name(attr[i+1]);
263 if(std::string(attr[i]) == "state")state(attr[i+1]);
264 if(std::string(attr[i]) == "select")select(atoi(attr[i+1]));
267 void switchtypeParser::pre (){_state="";}
268 void switchtypeParser::case_ (const std::pair<int,ENGINE::Node*>& p)
270 ENGINE::Switch* s=_cnodes.back();
271 s->edSetNode(p.first,p.second);
273 void switchtypeParser::default_ (const std::pair<int,ENGINE::Node*>& p)
275 ENGINE::Switch* s=_cnodes.back();
276 s->edSetDefaultNode(p.second);
278 void switchtypeParser::name (const std::string& name)
281 std::string fullname=currentProc->names.back()+name;
282 DEBTRACE( "switch_fullname: " << fullname )
283 s=theRuntime->createSwitch(name);
284 _cnodes.push_back(s);
285 currentProc->names.push_back(fullname+'.');
287 void switchtypeParser::state (const std::string& state)
289 //state is an attribute (no order). It can be defined before name
291 ENGINE::Switch* s=_cnodes.back();
292 if(_state == "disabled")
294 DEBTRACE( "Switch disabled: " << s->getName())
295 s->exDisabledState();
298 void switchtypeParser::select (const int& s)
300 //select is an attribute
301 ENGINE::Switch* sw=_cnodes.back();
302 ENGINE::InputPort *p=sw->edGetConditionPort();
305 ENGINE::Switch* switchtypeParser::post ()
307 DEBTRACE( "switch_post: " )
308 ENGINE::Switch* sw=_cnodes.back();
309 //pop back current level name and node
311 currentProc->names.pop_back();
315 void switchtypeParser::onStart(const XML_Char* el, const XML_Char** attr)
317 DEBTRACE( "switchtypeParser::onStart: " << el )
318 std::string element(el);
319 this->maxcount("default",1,element);
320 parser* pp=&parser::main_parser;
321 if(element == "case")pp=&casetypeParser::caseParser;
322 else if(element == "default")pp=&defaultcasetypeParser::defaultcaseParser;
323 SetUserDataAndPush(pp);
329 void switchtypeParser::onEnd(const char *el,parser* child)
331 DEBTRACE( "switchtypeParser::onEnd: " << el )
332 std::string element(el);
333 if(element == "case")case_(((casetypeParser*)child)->post());
334 else if(element == "default")default_(((defaultcasetypeParser*)child)->post());
337 void casetypeParser::onStart(const XML_Char* el, const XML_Char** attr)
339 DEBTRACE( "casetypeParser::onStart: " << el )
340 std::string element(el);
341 this->maxcount("inline",1,element);
342 this->maxcount("sinline",1,element);
343 this->maxcount("service",1,element);
344 this->maxcount("server",1,element);
345 this->maxcount("remote",1,element);
346 this->maxcount("node",1,element);
347 this->maxcount("forloop",1,element);
348 this->maxcount("foreach",1,element);
349 this->maxcount("optimizer",1,element);
350 this->maxcount("while",1,element);
351 this->maxcount("switch",1,element);
352 this->maxcount("bloc",1,element);
353 this->maxchoice(switch_t3,1,element);
354 parser* pp=&parser::main_parser;
355 if(element == "property")pp=&propertytypeParser::propertyParser;
356 else if(element == "inline")pp=&inlinetypeParser<>::inlineParser;
357 else if(element == "sinline")pp=&sinlinetypeParser<>::sinlineParser;
358 else if(element == "service")pp=&servicetypeParser<>::serviceParser;
359 else if(element == "server")pp=&servertypeParser<>::serverParser;
360 else if(element == "remote")pp=&remotetypeParser<>::remoteParser;
361 else if(element == "node")pp=&nodetypeParser<>::nodeParser;
362 else if(element == "forloop")pp=&forlooptypeParser<>::forloopParser;
363 else if(element == "foreach")pp=&foreachlooptypeParser<>::foreachloopParser;
364 else if(element == "optimizer")pp=&optimizerlooptypeParser<>::optimizerloopParser;
365 else if(element == "while")pp=&whilelooptypeParser<>::whileloopParser;
366 else if(element == "switch")pp=&switchtypeParser::switchParser;
367 else if(element == "bloc")pp=&bloctypeParser<>::blocParser;
368 SetUserDataAndPush(pp);
374 void casetypeParser::onEnd(const char *el,parser* child)
376 DEBTRACE( "casetypeParser::onEnd: " << el )
377 std::string element(el);
378 if(element == "property")property(((propertytypeParser*)child)->post());
379 else if(element == "inline")inline_(((inlinetypeParser<>*)child)->post());
380 else if(element == "sinline")sinline(((sinlinetypeParser<>*)child)->post());
381 else if(element == "service")service(((servicetypeParser<>*)child)->post());
382 else if(element == "server")server(((servertypeParser<>*)child)->post());
383 else if(element == "remote")remote(((remotetypeParser<>*)child)->post());
384 else if(element == "node")node(((nodetypeParser<>*)child)->post());
385 else if(element == "forloop")forloop(((forlooptypeParser<>*)child)->post());
386 else if(element == "foreach")foreach(((foreachlooptypeParser<>*)child)->post());
387 else if(element == "optimizer")optimizer(((optimizerlooptypeParser<>*)child)->post());
388 else if(element == "while")while_(((whilelooptypeParser<>*)child)->post());
389 else if(element == "switch")switch_(((switchtypeParser*)child)->post());
390 else if(element == "bloc")bloc(((bloctypeParser<>*)child)->post());