]> SALOME platform Git repositories - modules/yacs.git/blob - src/yacsloader/switchParsers.hxx
Salome HOME
merge from branch DEV tag mergeto_trunk_04apr08
[modules/yacs.git] / src / yacsloader / switchParsers.hxx
1
2 #ifndef _SWITCHPARSERS_HXX_
3 #define _SWITCHPARSERS_HXX_
4
5 #include "parserBase.hxx"
6 #include "propertyParsers.hxx"
7 #include "nodeParsers.hxx"
8
9 #include "Loop.hxx"
10 #include "ForLoop.hxx"
11 #include "ForEachLoop.hxx"
12 #include "WhileLoop.hxx"
13 #include "Switch.hxx"
14 #include "Bloc.hxx"
15 #include "Proc.hxx"
16 #include "InlineNode.hxx"
17 #include "ServiceNode.hxx"
18 #include "ServiceInlineNode.hxx"
19 #include "Runtime.hxx"
20
21 #include "factory.hxx"
22
23 #include <sstream>
24 #include <string>
25 #include <vector>
26 #include <map>
27
28 extern YACS::ENGINE::Proc* currentProc;
29 extern YACS::ENGINE::Runtime* theRuntime;
30
31 namespace YACS
32 {
33
34 struct casetypeParser:parser
35 {
36   static casetypeParser caseParser;
37   void onStart(const XML_Char* el, const XML_Char** attr);
38   void onEnd(const char *el,parser* child);
39   virtual void buildAttr(const XML_Char** attr);
40   virtual void pre ();
41   virtual void id (const int& n);
42   virtual void property (const myprop& prop);
43   virtual void inline_ (ENGINE::InlineNode* const& n);
44   virtual void sinline (ENGINE::ServiceInlineNode* const& n);
45   virtual void service (ENGINE::ServiceNode* const& n);
46   virtual void node (ENGINE::InlineNode* const& n);
47   virtual void forloop (ENGINE::ForLoop* const& n);
48   virtual void foreach (ENGINE::ForEachLoop* const& n);
49   virtual void while_ (ENGINE::WhileLoop* const& n);
50   virtual void switch_ (ENGINE::Switch* const& n);
51   virtual void bloc (ENGINE::Bloc* const& n);
52   virtual std::pair<int,ENGINE::Node*> post();
53   ENGINE::Node* _cnode;
54   int _id;
55   std::vector<int> _idStack;
56 };
57
58 struct defaultcasetypeParser:casetypeParser
59 {
60   static defaultcasetypeParser defaultcaseParser;
61   virtual void buildAttr(const XML_Char** attr);
62   virtual void pre ();
63 };
64
65 struct switchtypeParser:parser
66 {
67   static switchtypeParser switchParser;
68   void onStart(const XML_Char* el, const XML_Char** attr);
69   void onEnd(const char *el,parser* child);
70   virtual void buildAttr(const XML_Char** attr);
71   virtual void pre ();
72   virtual void case_ (const std::pair<int,ENGINE::Node*>& p);
73   virtual void default_ (const std::pair<int,ENGINE::Node*>& p);
74   virtual void name (const std::string& name);
75   virtual void state (const std::string& state);
76   virtual void select (const int& s);
77   virtual ENGINE::Switch* post ();
78   // stack to store switches in case of switch in switch
79   std::vector<ENGINE::Switch *> _cnodes;
80   std::string _state;
81 };
82
83 }
84
85 #include "loopParsers.hxx"
86
87 namespace YACS
88 {
89
90   static std::string switch_t3[]={"inline","sinline","service","node","forloop","foreach","while","switch","bloc",""};
91
92   void casetypeParser::buildAttr(const XML_Char** attr)
93     {
94       this->required("id",attr);
95       for (int i = 0; attr[i]; i += 2) 
96         {
97           if(std::string(attr[i]) == "id")id(atoi(attr[i+1]));
98         }
99     }
100   void casetypeParser::pre ()
101     {
102       _cnode=0;
103       _id=0;
104     }
105   void casetypeParser::id (const int& n)
106     {
107       DEBTRACE( "case_id: " << n )             
108       _id=n;
109       //store this level id
110       _idStack.push_back(_id);
111       //store this level name
112       std::stringstream temp;
113       if (_id <0) temp << "m" << -_id << "_";
114       else temp << "p" << _id << "_";
115       std::string fullname=currentProc->names.back()+temp.str();
116       DEBTRACE( "case_fullname: " << fullname )             
117       currentProc->names.push_back(fullname);
118     }
119   void casetypeParser::property (const myprop& prop)
120     {
121       DEBTRACE( "property_set: " << prop._name << prop._value )             
122     }
123   void casetypeParser::inline_ (ENGINE::InlineNode* const& n)
124     {
125       _cnode=n;
126       std::string fullname=currentProc->names.back()+ n->getName();
127       currentProc->nodeMap[fullname]=n;
128       currentProc->inlineMap[fullname]=n;
129     }
130   void casetypeParser::sinline (ENGINE::ServiceInlineNode* const& n)
131     {
132       _cnode=n;
133       std::string fullname=currentProc->names.back()+ n->getName();
134       currentProc->nodeMap[fullname]=n;
135       currentProc->serviceMap[fullname]=n;
136     }
137   void casetypeParser::service (ENGINE::ServiceNode* const& n)
138     {
139       _cnode=n;
140       std::string fullname=currentProc->names.back()+ n->getName();
141       currentProc->nodeMap[fullname]=n;
142       currentProc->serviceMap[fullname]=n;
143     }
144   void casetypeParser::node (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::forloop (ENGINE::ForLoop* const& n)
152     {
153       _cnode=n;
154       std::string fullname=currentProc->names.back()+ n->getName();
155       currentProc->nodeMap[fullname]=n;
156     }
157   void casetypeParser::foreach (ENGINE::ForEachLoop* const& n)
158     {
159       _cnode=n;
160       std::string fullname=currentProc->names.back()+ n->getName();
161       currentProc->nodeMap[fullname]=n;
162       fullname += ".splitter";
163       currentProc->nodeMap[fullname]=n->getChildByShortName("splitter");
164     }
165   void casetypeParser::while_ (ENGINE::WhileLoop* const& n)
166     {
167       _cnode=n;
168       std::string fullname=currentProc->names.back()+ n->getName();
169       currentProc->nodeMap[fullname]=n;
170     }
171   void casetypeParser::switch_ (ENGINE::Switch* const& n)
172     {
173       _cnode=n;
174       std::string fullname=currentProc->names.back()+ n->getName();
175       currentProc->nodeMap[fullname]=n;
176     }
177   void casetypeParser::bloc (ENGINE::Bloc* const& n)
178     {
179       _cnode=n;
180       std::string fullname=currentProc->names.back()+ n->getName();
181       currentProc->nodeMap[fullname]=n;
182     }
183   std::pair<int,ENGINE::Node*> casetypeParser::post()
184     {
185       DEBTRACE( "case_post" )             
186       minchoice(switch_t3,1);
187       //get back this level id
188       _id=_idStack.back();
189       _idStack.pop_back();
190       //pop back this level name
191       currentProc->names.pop_back();
192       return std::pair<int,ENGINE::Node*>(_id,_cnode);
193     }
194
195   void defaultcasetypeParser::buildAttr(const XML_Char** attr)
196     {
197       for (int i = 0; attr[i]; i += 2) 
198       {
199         DEBTRACE( attr[i] << "=" << attr[i + 1] )             
200       }
201     }
202   void defaultcasetypeParser::pre ()
203     {
204       _id=0;
205       _cnode=0;
206       //store this level id
207       _idStack.push_back(_id);
208       //store this level name
209       std::string fullname=currentProc->names.back()+"default_";
210       DEBTRACE( "case_fullname: " << fullname )             
211       currentProc->names.push_back(fullname);
212     }
213
214   void switchtypeParser::buildAttr(const XML_Char** attr)
215     {
216       this->required("name",attr);
217       for (int i = 0; attr[i]; i += 2) 
218         {
219           if(std::string(attr[i]) == "name")name(attr[i+1]);
220           if(std::string(attr[i]) == "state")state(attr[i+1]);
221           if(std::string(attr[i]) == "select")select(atoi(attr[i+1]));
222         }
223     }
224   void switchtypeParser::pre (){_state="";}
225   void switchtypeParser::case_ (const std::pair<int,ENGINE::Node*>& p)
226     {
227       ENGINE::Switch* s=_cnodes.back();
228       s->edSetNode(p.first,p.second);
229     }
230   void switchtypeParser::default_ (const std::pair<int,ENGINE::Node*>& p)
231     {
232       ENGINE::Switch* s=_cnodes.back();
233       s->edSetDefaultNode(p.second);
234     }
235   void switchtypeParser::name (const std::string& name)
236     {
237       ENGINE::Switch* s;
238       std::string fullname=currentProc->names.back()+name;
239       DEBTRACE( "switch_fullname: " << fullname )             
240       s=theRuntime->createSwitch(name);
241       _cnodes.push_back(s);
242       currentProc->names.push_back(fullname+'.');
243     }
244   void switchtypeParser::state (const std::string& state)
245     {
246       //state is an attribute (no order). It can be defined before name
247       //To be improved
248       ENGINE::Switch* s=_cnodes.back();
249       if(_state == "disabled")
250         {
251           DEBTRACE( "Switch disabled: " << s->getName())             
252           s->exDisabledState();
253         }
254     }
255   void switchtypeParser::select (const int& s)
256     {
257       //select is an attribute
258       ENGINE::Switch* sw=_cnodes.back();
259       ENGINE::InputPort *p=sw->edGetConditionPort();
260       p->edInit(s);
261     }
262   ENGINE::Switch* switchtypeParser::post ()
263     {
264       DEBTRACE( "switch_post: " )             
265       ENGINE::Switch* sw=_cnodes.back();
266       //pop back current level name and node
267       _cnodes.pop_back();
268       currentProc->names.pop_back();
269       return sw;
270     }
271
272 void switchtypeParser::onStart(const XML_Char* el, const XML_Char** attr)
273 {
274   DEBTRACE( "switchtypeParser::onStart: " << el )
275   std::string element(el);
276   this->maxcount("default",1,element);
277   parser* pp=&parser::main_parser;
278   if(element == "case")pp=&casetypeParser::caseParser;
279   else if(element == "default")pp=&defaultcasetypeParser::defaultcaseParser;
280   SetUserDataAndPush(pp);
281   pp->init();
282   pp->pre();
283   pp->buildAttr(attr);
284 }
285
286 void switchtypeParser::onEnd(const char *el,parser* child)
287 {
288   DEBTRACE( "switchtypeParser::onEnd: " << el )
289   std::string element(el);
290   if(element == "case")case_(((casetypeParser*)child)->post());
291   else if(element == "default")default_(((defaultcasetypeParser*)child)->post());
292 }
293
294 void casetypeParser::onStart(const XML_Char* el, const XML_Char** attr)
295 {
296   DEBTRACE( "casetypeParser::onStart: " << el )
297   std::string element(el);
298   this->maxcount("inline",1,element);
299   this->maxcount("sinline",1,element);
300   this->maxcount("service",1,element);
301   this->maxcount("node",1,element);
302   this->maxcount("forloop",1,element);
303   this->maxcount("foreach",1,element);
304   this->maxcount("while",1,element);
305   this->maxcount("switch",1,element);
306   this->maxcount("bloc",1,element);
307   this->maxchoice(switch_t3,1,element);
308   parser* pp=&parser::main_parser;
309   if(element == "property")pp=&propertytypeParser::propertyParser;
310   else if(element == "inline")pp=&inlinetypeParser<>::inlineParser;
311   else if(element == "sinline")pp=&sinlinetypeParser<>::sinlineParser;
312   else if(element == "service")pp=&servicetypeParser<>::serviceParser;
313   else if(element == "node")pp=&nodetypeParser<>::nodeParser;
314   else if(element == "forloop")pp=&forlooptypeParser<>::forloopParser;
315   else if(element == "foreach")pp=&foreachlooptypeParser<>::foreachloopParser;
316   else if(element == "while")pp=&whilelooptypeParser<>::whileloopParser;
317   else if(element == "switch")pp=&switchtypeParser::switchParser;
318   else if(element == "bloc")pp=&bloctypeParser<>::blocParser;
319   SetUserDataAndPush(pp);
320   pp->init();
321   pp->pre();
322   pp->buildAttr(attr);
323 }
324
325 void casetypeParser::onEnd(const char *el,parser* child)
326 {
327   DEBTRACE( "casetypeParser::onEnd: " << el )
328   std::string element(el);
329   if(element == "property")property(((propertytypeParser*)child)->post());
330   else if(element == "inline")inline_(((inlinetypeParser<>*)child)->post());
331   else if(element == "sinline")sinline(((sinlinetypeParser<>*)child)->post());
332   else if(element == "service")service(((servicetypeParser<>*)child)->post());
333   else if(element == "node")node(((nodetypeParser<>*)child)->post());
334   else if(element == "forloop")forloop(((forlooptypeParser<>*)child)->post());
335   else if(element == "foreach")foreach(((foreachlooptypeParser<>*)child)->post());
336   else if(element == "while")while_(((whilelooptypeParser<>*)child)->post());
337   else if(element == "switch")switch_(((switchtypeParser*)child)->post());
338   else if(element == "bloc")bloc(((bloctypeParser<>*)child)->post());
339 }
340
341 }
342
343 #endif