]> SALOME platform Git repositories - modules/yacs.git/blob - src/yacsloader/switchParsers.hxx
Salome HOME
808c95928e358a02f1d3ff205ad15e2a1942817c
[modules/yacs.git] / src / yacsloader / switchParsers.hxx
1 // Copyright (C) 2006-2014  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, or (at your option) any later version.
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
20 #ifndef _SWITCHPARSERS_HXX_
21 #define _SWITCHPARSERS_HXX_
22
23 #include "parserBase.hxx"
24 #include "propertyParsers.hxx"
25 #include "nodeParsers.hxx"
26
27 #include "Loop.hxx"
28 #include "ForLoop.hxx"
29 #include "ForEachLoop.hxx"
30 #include "WhileLoop.hxx"
31 #include "Switch.hxx"
32 #include "Bloc.hxx"
33 #include "Proc.hxx"
34 #include "InlineNode.hxx"
35 #include "ServiceNode.hxx"
36 #include "ServiceInlineNode.hxx"
37 #include "Runtime.hxx"
38
39 #include "factory.hxx"
40
41 #include <sstream>
42 #include <string>
43 #include <vector>
44 #include <map>
45
46 extern YACS::ENGINE::Proc* currentProc;
47 extern YACS::ENGINE::Runtime* theRuntime;
48
49 namespace YACS
50 {
51
52 struct casetypeParser:parser
53 {
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);
58   virtual void pre ();
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();
74   ENGINE::Node* _cnode;
75   int _id;
76   std::vector<int> _idStack;
77 };
78
79 struct defaultcasetypeParser:casetypeParser
80 {
81   static defaultcasetypeParser defaultcaseParser;
82   virtual void buildAttr(const XML_Char** attr);
83   virtual void pre ();
84 };
85
86 struct switchtypeParser:parser
87 {
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);
92   virtual void pre ();
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;
101   std::string _state;
102 };
103
104 }
105
106 #include "loopParsers.hxx"
107
108 namespace YACS
109 {
110
111   static std::string switch_t3[]={"inline","sinline","service","server", "remote", "node","forloop","foreach","optimizer","while","switch","bloc",""};
112
113   void casetypeParser::buildAttr(const XML_Char** attr)
114     {
115       this->required("id",attr);
116       for (int i = 0; attr[i]; i += 2) 
117         {
118           if(std::string(attr[i]) == "id")id(atoi(attr[i+1]));
119         }
120     }
121   void casetypeParser::pre ()
122     {
123       _cnode=0;
124       _id=0;
125     }
126   void casetypeParser::id (const int& n)
127     {
128       DEBTRACE( "case_id: " << n )             
129       _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);
139     }
140   void casetypeParser::property (const myprop& prop)
141     {
142       DEBTRACE( "property_set: " << prop._name << prop._value )             
143     }
144   void casetypeParser::inline_ (ENGINE::InlineNode* const& n)
145     {
146       _cnode=n;
147       std::string fullname=currentProc->names.back()+ n->getName();
148       currentProc->nodeMap[fullname]=n;
149       currentProc->inlineMap[fullname]=n;
150     }
151   void casetypeParser::sinline (ENGINE::ServiceInlineNode* const& n)
152     {
153       _cnode=n;
154       std::string fullname=currentProc->names.back()+ n->getName();
155       currentProc->nodeMap[fullname]=n;
156       currentProc->serviceMap[fullname]=n;
157     }
158   void casetypeParser::service (ENGINE::ServiceNode* const& n)
159     {
160       _cnode=n;
161       std::string fullname=currentProc->names.back()+ n->getName();
162       currentProc->nodeMap[fullname]=n;
163       currentProc->serviceMap[fullname]=n;
164     }
165   void casetypeParser::server (ENGINE::ServerNode* const& n)
166     {
167       _cnode=n;
168       std::string fullname=currentProc->names.back()+ n->getName();
169       currentProc->nodeMap[fullname]=n;
170       currentProc->inlineMap[fullname]=n;
171     }
172   void casetypeParser::remote (ENGINE::InlineNode* const& n)
173     {
174       _cnode=n;
175       std::string fullname=currentProc->names.back()+ n->getName();
176       currentProc->nodeMap[fullname]=n;
177       currentProc->inlineMap[fullname]=n;
178     }
179   void casetypeParser::node (ENGINE::InlineNode* const& n)
180     {
181       _cnode=n;
182       std::string fullname=currentProc->names.back()+ n->getName();
183       currentProc->nodeMap[fullname]=n;
184       currentProc->inlineMap[fullname]=n;
185     }
186   void casetypeParser::forloop (ENGINE::ForLoop* const& n)
187     {
188       _cnode=n;
189       std::string fullname=currentProc->names.back()+ n->getName();
190       currentProc->nodeMap[fullname]=n;
191     }
192   void casetypeParser::foreach (ENGINE::ForEachLoop* const& n)
193     {
194       _cnode=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");
199     }
200   void casetypeParser::optimizer (ENGINE::OptimizerLoop* const& n)
201     {
202       _cnode=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");
207     }
208   void casetypeParser::while_ (ENGINE::WhileLoop* const& n)
209     {
210       _cnode=n;
211       std::string fullname=currentProc->names.back()+ n->getName();
212       currentProc->nodeMap[fullname]=n;
213     }
214   void casetypeParser::switch_ (ENGINE::Switch* const& n)
215     {
216       _cnode=n;
217       std::string fullname=currentProc->names.back()+ n->getName();
218       currentProc->nodeMap[fullname]=n;
219     }
220   void casetypeParser::bloc (ENGINE::Bloc* const& n)
221     {
222       _cnode=n;
223       std::string fullname=currentProc->names.back()+ n->getName();
224       currentProc->nodeMap[fullname]=n;
225     }
226   std::pair<int,ENGINE::Node*> casetypeParser::post()
227     {
228       DEBTRACE( "case_post" )             
229       minchoice(switch_t3,1);
230       //get back this level id
231       _id=_idStack.back();
232       _idStack.pop_back();
233       //pop back this level name
234       currentProc->names.pop_back();
235       return std::pair<int,ENGINE::Node*>(_id,_cnode);
236     }
237
238   void defaultcasetypeParser::buildAttr(const XML_Char** attr)
239     {
240       for (int i = 0; attr[i]; i += 2) 
241       {
242         DEBTRACE( attr[i] << "=" << attr[i + 1] )             
243       }
244     }
245   void defaultcasetypeParser::pre ()
246     {
247       _id=0;
248       _cnode=0;
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);
255     }
256
257   void switchtypeParser::buildAttr(const XML_Char** attr)
258     {
259       this->required("name",attr);
260       for (int i = 0; attr[i]; i += 2) 
261         {
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]));
265         }
266     }
267   void switchtypeParser::pre (){_state="";}
268   void switchtypeParser::case_ (const std::pair<int,ENGINE::Node*>& p)
269     {
270       ENGINE::Switch* s=_cnodes.back();
271       s->edSetNode(p.first,p.second);
272     }
273   void switchtypeParser::default_ (const std::pair<int,ENGINE::Node*>& p)
274     {
275       ENGINE::Switch* s=_cnodes.back();
276       s->edSetDefaultNode(p.second);
277     }
278   void switchtypeParser::name (const std::string& name)
279     {
280       ENGINE::Switch* s;
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+'.');
286     }
287   void switchtypeParser::state (const std::string& state)
288     {
289       //state is an attribute (no order). It can be defined before name
290       //To be improved
291       ENGINE::Switch* s=_cnodes.back();
292       if(_state == "disabled")
293         {
294           DEBTRACE( "Switch disabled: " << s->getName())             
295           s->exDisabledState();
296         }
297     }
298   void switchtypeParser::select (const int& s)
299     {
300       //select is an attribute
301       ENGINE::Switch* sw=_cnodes.back();
302       ENGINE::InputPort *p=sw->edGetConditionPort();
303       p->edInit(s);
304     }
305   ENGINE::Switch* switchtypeParser::post ()
306     {
307       DEBTRACE( "switch_post: " )             
308       ENGINE::Switch* sw=_cnodes.back();
309       //pop back current level name and node
310       _cnodes.pop_back();
311       currentProc->names.pop_back();
312       return sw;
313     }
314
315 void switchtypeParser::onStart(const XML_Char* el, const XML_Char** attr)
316 {
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);
324   pp->init();
325   pp->pre();
326   pp->buildAttr(attr);
327 }
328
329 void switchtypeParser::onEnd(const char *el,parser* child)
330 {
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());
335 }
336
337 void casetypeParser::onStart(const XML_Char* el, const XML_Char** attr)
338 {
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);
369   pp->init();
370   pp->pre();
371   pp->buildAttr(attr);
372 }
373
374 void casetypeParser::onEnd(const char *el,parser* child)
375 {
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());
391 }
392
393 }
394
395 #endif