1 // Copyright (C) 2006-2019 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)
117 this->required("id",attr);
118 for (int i = 0; attr[i]; i += 2)
120 if(std::string(attr[i]) == "id")id(atoi(attr[i+1]));
123 void casetypeParser::pre ()
128 void casetypeParser::id (const int& n)
130 DEBTRACE( "case_id: " << n )
132 //store this level id
133 _idStack.push_back(_id);
134 //store this level name
135 std::stringstream temp;
136 if (_id <0) temp << "m" << -_id << "_";
137 else temp << "p" << _id << "_";
138 std::string fullname=currentProc->names.back()+temp.str();
139 DEBTRACE( "case_fullname: " << fullname )
140 currentProc->names.push_back(fullname);
142 void casetypeParser::property (const myprop& prop)
144 DEBTRACE( "property_set: " << prop._name << prop._value )
146 void casetypeParser::inline_ (ENGINE::InlineNode* const& n)
149 std::string fullname=currentProc->names.back()+ n->getName();
150 currentProc->nodeMap[fullname]=n;
151 currentProc->inlineMap[fullname]=n;
153 void casetypeParser::sinline (ENGINE::ServiceInlineNode* const& n)
156 std::string fullname=currentProc->names.back()+ n->getName();
157 currentProc->nodeMap[fullname]=n;
158 currentProc->serviceMap[fullname]=n;
160 void casetypeParser::service (ENGINE::ServiceNode* const& n)
163 std::string fullname=currentProc->names.back()+ n->getName();
164 currentProc->nodeMap[fullname]=n;
165 currentProc->serviceMap[fullname]=n;
167 void casetypeParser::server (ENGINE::ServerNode* const& n)
170 std::string fullname=currentProc->names.back()+ n->getName();
171 currentProc->nodeMap[fullname]=n;
172 currentProc->inlineMap[fullname]=n;
174 void casetypeParser::remote (ENGINE::InlineNode* const& n)
177 std::string fullname=currentProc->names.back()+ n->getName();
178 currentProc->nodeMap[fullname]=n;
179 currentProc->inlineMap[fullname]=n;
181 void casetypeParser::node (ENGINE::InlineNode* const& n)
184 std::string fullname=currentProc->names.back()+ n->getName();
185 currentProc->nodeMap[fullname]=n;
186 currentProc->inlineMap[fullname]=n;
188 void casetypeParser::forloop (ENGINE::ForLoop* const& n)
191 std::string fullname=currentProc->names.back()+ n->getName();
192 currentProc->nodeMap[fullname]=n;
194 void casetypeParser::foreach (ENGINE::ForEachLoop* const& n)
197 std::string fullname=currentProc->names.back()+ n->getName();
198 currentProc->nodeMap[fullname]=n;
199 fullname += ".splitter";
200 currentProc->nodeMap[fullname]=n->getChildByShortName("splitter");
202 void casetypeParser::optimizer (ENGINE::OptimizerLoop* const& n)
205 std::string fullname=currentProc->names.back()+ n->getName();
206 currentProc->nodeMap[fullname]=n;
207 //fullname += ".splitter";
208 //currentProc->nodeMap[fullname]=n->getChildByShortName("splitter");
210 void casetypeParser::while_ (ENGINE::WhileLoop* const& n)
213 std::string fullname=currentProc->names.back()+ n->getName();
214 currentProc->nodeMap[fullname]=n;
216 void casetypeParser::switch_ (ENGINE::Switch* const& n)
219 std::string fullname=currentProc->names.back()+ n->getName();
220 currentProc->nodeMap[fullname]=n;
222 void casetypeParser::bloc (ENGINE::Bloc* const& n)
225 std::string fullname=currentProc->names.back()+ n->getName();
226 currentProc->nodeMap[fullname]=n;
228 std::pair<int,ENGINE::Node*> casetypeParser::post()
230 DEBTRACE( "case_post" )
231 minchoice(switch_t3,1);
232 //get back this level id
235 //pop back this level name
236 currentProc->names.pop_back();
237 return std::pair<int,ENGINE::Node*>(_id,_cnode);
240 void defaultcasetypeParser::buildAttr(const XML_Char** attr)
244 for (int i = 0; attr[i]; i += 2)
246 DEBTRACE( attr[i] << "=" << attr[i + 1] )
249 void defaultcasetypeParser::pre ()
253 //store this level id
254 _idStack.push_back(_id);
255 //store this level name
256 std::string fullname=currentProc->names.back()+"default_";
257 DEBTRACE( "case_fullname: " << fullname )
258 currentProc->names.push_back(fullname);
261 void switchtypeParser::buildAttr(const XML_Char** attr)
265 this->required("name",attr);
266 for (int i = 0; attr[i]; i += 2)
268 if(std::string(attr[i]) == "name")name(attr[i+1]);
269 if(std::string(attr[i]) == "state")state(attr[i+1]);
270 if(std::string(attr[i]) == "select")select(atoi(attr[i+1]));
273 void switchtypeParser::pre (){_state="";}
274 void switchtypeParser::case_ (const std::pair<int,ENGINE::Node*>& p)
276 ENGINE::Switch* s=_cnodes.back();
277 s->edSetNode(p.first,p.second);
279 void switchtypeParser::default_ (const std::pair<int,ENGINE::Node*>& p)
281 ENGINE::Switch* s=_cnodes.back();
282 s->edSetDefaultNode(p.second);
284 void switchtypeParser::name (const std::string& name)
287 std::string fullname=currentProc->names.back()+name;
288 DEBTRACE( "switch_fullname: " << fullname )
289 s=theRuntime->createSwitch(name);
290 _cnodes.push_back(s);
291 currentProc->names.push_back(fullname+'.');
293 void switchtypeParser::state (const std::string& state)
295 //state is an attribute (no order). It can be defined before name
297 ENGINE::Switch* s=_cnodes.back();
298 if(_state == "disabled")
300 DEBTRACE( "Switch disabled: " << s->getName())
301 s->exDisabledState();
304 void switchtypeParser::select (const int& s)
306 //select is an attribute
307 ENGINE::Switch* sw=_cnodes.back();
308 ENGINE::InputPort *p=sw->edGetConditionPort();
311 ENGINE::Switch* switchtypeParser::post ()
313 DEBTRACE( "switch_post: " )
314 ENGINE::Switch* sw=_cnodes.back();
315 //pop back current level name and node
317 currentProc->names.pop_back();
321 void switchtypeParser::onStart(const XML_Char* el, const XML_Char** attr)
323 DEBTRACE( "switchtypeParser::onStart: " << el )
324 std::string element(el);
325 this->maxcount("default",1,element);
326 parser* pp=&parser::main_parser;
327 if(element == "case")pp=&casetypeParser::caseParser;
328 else if(element == "default")pp=&defaultcasetypeParser::defaultcaseParser;
329 SetUserDataAndPush(pp);
335 void switchtypeParser::onEnd(const char *el,parser* child)
337 DEBTRACE( "switchtypeParser::onEnd: " << el )
338 std::string element(el);
339 if(element == "case")case_(((casetypeParser*)child)->post());
340 else if(element == "default")default_(((defaultcasetypeParser*)child)->post());
343 void casetypeParser::onStart(const XML_Char* el, const XML_Char** attr)
345 DEBTRACE( "casetypeParser::onStart: " << el )
346 std::string element(el);
347 this->maxcount("inline",1,element);
348 this->maxcount("sinline",1,element);
349 this->maxcount("service",1,element);
350 this->maxcount("server",1,element);
351 this->maxcount("remote",1,element);
352 this->maxcount("node",1,element);
353 this->maxcount("forloop",1,element);
354 this->maxcount("foreach",1,element);
355 this->maxcount("optimizer",1,element);
356 this->maxcount("while",1,element);
357 this->maxcount("switch",1,element);
358 this->maxcount("bloc",1,element);
359 this->maxchoice(switch_t3,1,element);
360 parser* pp=&parser::main_parser;
361 if(element == "property")pp=&propertytypeParser::propertyParser;
362 else if(element == "inline")pp=&inlinetypeParser<>::inlineParser;
363 else if(element == "sinline")pp=&sinlinetypeParser<>::sinlineParser;
364 else if(element == "service")pp=&servicetypeParser<>::serviceParser;
365 else if(element == "server")pp=&servertypeParser<>::serverParser;
366 else if(element == "remote")pp=&remotetypeParser<>::remoteParser;
367 else if(element == "node")pp=&nodetypeParser<>::nodeParser;
368 else if(element == "forloop")pp=&forlooptypeParser<>::forloopParser;
369 else if(element == "foreach")pp=&foreachlooptypeParser<>::foreachloopParser;
370 else if(element == "optimizer")pp=&optimizerlooptypeParser<>::optimizerloopParser;
371 else if(element == "while")pp=&whilelooptypeParser<>::whileloopParser;
372 else if(element == "switch")pp=&switchtypeParser::switchParser;
373 else if(element == "bloc")pp=&bloctypeParser<>::blocParser;
374 SetUserDataAndPush(pp);
380 void casetypeParser::onEnd(const char *el,parser* child)
382 DEBTRACE( "casetypeParser::onEnd: " << el )
383 std::string element(el);
384 if(element == "property")property(((propertytypeParser*)child)->post());
385 else if(element == "inline")inline_(((inlinetypeParser<>*)child)->post());
386 else if(element == "sinline")sinline(((sinlinetypeParser<>*)child)->post());
387 else if(element == "service")service(((servicetypeParser<>*)child)->post());
388 else if(element == "server")server(((servertypeParser<>*)child)->post());
389 else if(element == "remote")remote(((remotetypeParser<>*)child)->post());
390 else if(element == "node")node(((nodetypeParser<>*)child)->post());
391 else if(element == "forloop")forloop(((forlooptypeParser<>*)child)->post());
392 else if(element == "foreach")foreach(((foreachlooptypeParser<>*)child)->post());
393 else if(element == "optimizer")optimizer(((optimizerlooptypeParser<>*)child)->post());
394 else if(element == "while")while_(((whilelooptypeParser<>*)child)->post());
395 else if(element == "switch")switch_(((switchtypeParser*)child)->post());
396 else if(element == "bloc")bloc(((bloctypeParser<>*)child)->post());