Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/yacs.git] / src / yacsloader / blocParsers.hxx
1 // Copyright (C) 2006-2012  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.
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 _BLOCPARSER_HXX_
21 #define _BLOCPARSER_HXX_
22
23 #include "parserBase.hxx"
24 #include "linkParsers.hxx"
25 #include "xmlrpcParsers.hxx"
26 #include "nodeParsers.hxx"
27 #include "inlineParsers.hxx"
28 #include "remoteParsers.hxx"
29 #include "serverParsers.hxx"
30 #include "sinlineParsers.hxx"
31 #include "serviceParsers.hxx"
32
33
34 #include "Proc.hxx"
35 #include "Logger.hxx"
36 #include "Runtime.hxx"
37 #include "ForLoop.hxx"
38 #include "ForEachLoop.hxx"
39 #include "OptimizerLoop.hxx"
40 #include "WhileLoop.hxx"
41 #include "Switch.hxx"
42 #include "Bloc.hxx"
43 #include "Exception.hxx"
44 #include "InlineNode.hxx"
45 #include "DataNode.hxx"
46 #include "ServiceNode.hxx"
47 #include "ServiceInlineNode.hxx"
48 #include "OutputDataStreamPort.hxx"
49 #include "InputDataStreamPort.hxx"
50
51 #include <vector>
52
53 extern YACS::ENGINE::Proc* currentProc;
54 extern YACS::ENGINE::Runtime* theRuntime;
55
56 namespace YACS
57 {
58
59 template <class T=YACS::ENGINE::Bloc*>
60 struct bloctypeParser:parser
61 {
62   static bloctypeParser<T> blocParser;
63
64   bloctypeParser():parser()
65   {
66     _orders["property"]=0;
67     _orders["inline"]=2;
68     _orders["service"]=2;
69     _orders["server"]=2;
70     _orders["remote"]=2;
71     _orders["sinline"]=2;
72     _orders["node"]=2;
73     _orders["datanode"]=2;
74     _orders["outnode"]=2;
75     _orders["forloop"]=2;
76     _orders["foreach"]=2;
77     _orders["optimizer"]=2;
78     _orders["while"]=2;
79     _orders["switch"]=2;
80     _orders["bloc"]=2;
81     _orders["control"]=3;
82     _orders["datalink"]=3;
83     _orders["stream"]=3;
84     _orders["parameter"]=3;
85   }
86   virtual void onStart(const XML_Char* el, const XML_Char** attr);
87   virtual void onEnd(const char *el,parser* child);
88   virtual void buildAttr(const XML_Char** attr)
89     {
90       this->required("name",attr);
91       for (int i = 0; attr[i]; i += 2) 
92         {
93           if(std::string(attr[i]) == "name")name(attr[i+1]);
94           if(std::string(attr[i]) == "state")state(attr[i+1]);
95         }
96     }
97   void name (const std::string& name)
98     {
99     }
100   virtual void state (const std::string& name){
101       DEBTRACE( "bloc_state: " << name )             
102       _state=name;
103       if(_state == "disabled")
104         {
105           DEBTRACE( "Bloc disabled: " << _bloc->getName())             
106           _bloc->exDisabledState();
107         }
108     }
109   virtual void property (const myprop& prop)
110     {
111       DEBTRACE( "property_set: " << prop._name << prop._value )             
112       _bloc->setProperty(prop._name,prop._value);
113     }
114   virtual void inline_ (YACS::ENGINE::InlineNode* const& n)
115     {
116       DEBTRACE( "bloc_pynode_set: " << n->getName() )             
117       _bloc->edAddChild(n);
118       std::string fullname = currentProc->names.back()+n->getName();
119       currentProc->nodeMap[fullname]=n;
120       currentProc->inlineMap[fullname]=n;
121     }
122   virtual void sinline (YACS::ENGINE::ServiceInlineNode* const& n)
123     {
124       DEBTRACE( "bloc_sinline: " << n->getName() )             
125       _bloc->edAddChild(n);
126       std::string fullname = currentProc->names.back()+n->getName();
127       currentProc->nodeMap[fullname]=n;
128       currentProc->serviceMap[fullname]=n;
129     }
130   virtual void service (YACS::ENGINE::ServiceNode* const& n)
131     {
132       DEBTRACE( "bloc_service_set: " << n->getName() )             
133       _bloc->edAddChild(n);
134       std::string fullname = currentProc->names.back()+n->getName();
135       currentProc->nodeMap[fullname]=n;
136       currentProc->serviceMap[fullname]=n;
137     }
138   virtual void remote (YACS::ENGINE::InlineNode* const& n)
139     {
140       DEBTRACE( "bloc_remote_set: " << n->getName() )             
141       _bloc->edAddChild(n);
142       std::string fullname = currentProc->names.back()+n->getName();
143       currentProc->nodeMap[fullname]=n;
144       currentProc->inlineMap[fullname]=n;
145     }
146   virtual void server (YACS::ENGINE::ServerNode* const& n)
147     {
148       DEBTRACE( "bloc_server_set: " << n->getName() )             
149       _bloc->edAddChild(n);
150       std::string fullname = currentProc->names.back()+n->getName();
151       currentProc->nodeMap[fullname]=n;
152       currentProc->inlineMap[fullname]=n;
153     }
154   virtual void node (YACS::ENGINE::InlineNode* const& n)
155     {
156       DEBTRACE( "bloc_node_set: " << n->getName() )             
157       _bloc->edAddChild(n);
158       std::string fullname = currentProc->names.back()+n->getName();
159       DEBTRACE( "bloc_node_set fullname = "  << fullname )             
160       currentProc->nodeMap[fullname]=n;
161       currentProc->inlineMap[fullname]=n;
162     }
163   virtual void forloop (YACS::ENGINE::ForLoop* const& b)
164     {
165       DEBTRACE( "bloc_forloop_set: " << b->getName() )             
166       _bloc->edAddChild(b);
167       std::string fullname = currentProc->names.back()+b->getName();
168       currentProc->nodeMap[fullname]=b;
169     }
170   virtual void optimizer (YACS::ENGINE::OptimizerLoop* const& b)
171     {
172       DEBTRACE( "bloc_optimizer_set: " << b->getName() );
173       _bloc->edAddChild(b);
174       std::string fullname = currentProc->names.back()+b->getName();
175       currentProc->nodeMap[fullname]=b;
176       //fullname += ".splitter";
177       //currentProc->nodeMap[fullname]=b->getChildByShortName("splitter");
178     }
179   virtual void foreach (YACS::ENGINE::ForEachLoop* const& b)
180     {
181       DEBTRACE( "bloc_foreach_set: " << b->getName() )             
182       _bloc->edAddChild(b);
183       std::string fullname = currentProc->names.back()+b->getName();
184       currentProc->nodeMap[fullname]=b;
185       fullname += ".splitter";
186       currentProc->nodeMap[fullname]=b->getChildByShortName("splitter");
187     }
188   virtual void while_ (YACS::ENGINE::WhileLoop* const& b)
189     {
190       DEBTRACE( "bloc_while_set: " << b->getName() )             
191       _bloc->edAddChild(b);
192       std::string fullname = currentProc->names.back()+b->getName();
193       currentProc->nodeMap[fullname]=b;
194     }
195   virtual void switch_ (YACS::ENGINE::Switch* const& b)
196     {
197       DEBTRACE( "bloc_switch_set: " << b->getName() )             
198       _bloc->edAddChild(b);
199       std::string fullname = currentProc->names.back()+b->getName();
200       currentProc->nodeMap[fullname]=b;
201     }
202   virtual void bloc (YACS::ENGINE::Bloc* const& b)
203     {
204       DEBTRACE( "bloc_bloc_set: " << b->getName() )             
205       _bloc->edAddChild(b);
206       std::string fullname=currentProc->names.back()+ b->getName();
207       currentProc->nodeMap[fullname]=b;
208     }
209   virtual void control (const mycontrol& l)
210     {
211       DEBTRACE( "bloc_control_set: " << l.fromnode() << " "<< l.tonode() )             
212       std::string msg;
213
214       if(currentProc->nodeMap.count(currentProc->names.back()+l.fromnode()) == 0)
215         {
216           msg="from node " + l.fromnode() + " does not exist in control link: ";
217           msg=msg+l.fromnode()+"->"+l.tonode();
218           msg=msg+ " context: "+currentProc->names.back();
219           this->logError(msg);
220           return;
221         }
222       if(currentProc->nodeMap.count(currentProc->names.back()+l.tonode()) == 0)
223         {
224           msg="to node " + l.tonode() + " does not exist in control link: ";
225           msg=msg+l.fromnode()+"->"+l.tonode();
226           msg=msg+ " context: "+currentProc->names.back();
227           this->logError(msg);
228           return;
229         }
230       // We only link local nodes
231       try
232         {
233           _bloc->edAddCFLink(currentProc->nodeMap[currentProc->names.back()+l.fromnode()],
234                              currentProc->nodeMap[currentProc->names.back()+l.tonode()]);
235         }
236       catch(YACS::Exception& e)
237         {
238           this->logError(e.what());
239         }
240     }
241   virtual void datalink (const mylink& l)
242     {
243       DEBTRACE( "bloc_datalink_set: "<<l.fromnode()<<"("<<l.fromport()<<")->"<<l.tonode()<<"("<<l.toport()<<")")
244       std::string msg;
245
246       //Try only relative name for from node
247       std::string fromname = currentProc->names.back()+l.fromnode();
248       if(currentProc->nodeMap.count(fromname) == 0)
249         {
250           msg="from node " + l.fromnode() + " does not exist in data link: ";
251           msg=msg+l.fromnode()+"("+l.fromport()+")->"+l.tonode()+"("+l.toport()+")";
252           this->logError(msg);
253           return;
254         }
255       //Try relative name for to node and then absolute one
256       std::string toname = currentProc->names.back()+l.tonode();
257       if(currentProc->nodeMap.count(toname) == 0)
258         {
259           //It's not a relative name. Try an absolute one (I think it's not possible)
260           toname=l.tonode();
261           if(currentProc->nodeMap.count(toname) == 0)
262             {
263               // The TO node does not exist -> error
264               msg="to node " + l.tonode() + " does not exist in data link: ";
265               msg=msg+l.fromnode()+"("+l.fromport()+")->"+l.tonode()+"("+l.toport()+")";
266               this->logError(msg);
267               return;
268             }
269         }
270       // We only link local node and other nodes (relative or absolute name in this order)
271       DEBTRACE(fromname <<":"<<l.fromport()<<toname<<":"<<l.toport());
272       try
273         {
274           if (l.withControl())
275             _bloc->edAddDFLink(currentProc->nodeMap[fromname]->getOutputPort(l.fromport()),
276                                currentProc->nodeMap[toname]->getInputPort(l.toport()));
277           else
278             _bloc->edAddLink(currentProc->nodeMap[fromname]->getOutputPort(l.fromport()),
279                              currentProc->nodeMap[toname]->getInputPort(l.toport()));
280         }
281       catch(YACS::Exception& e)
282         {
283           this->logError(e.what());
284         }
285     }
286   virtual void stream (const mystream& l)
287     {
288       DEBTRACE( "bloc_stream_set: " << l.fromnode() << l.fromport() << l.tonode() << l.toport() )
289       std::string msg;
290       std::string fromname = currentProc->names.back()+l.fromnode();
291       std::string toname = currentProc->names.back()+l.tonode();
292       //only relative names
293       if(currentProc->nodeMap.count(fromname) == 0)
294         {
295             msg="from node " + l.fromnode() + " does not exist in stream link: ";
296             msg=msg+l.fromnode()+"("+l.fromport()+")->"+l.tonode()+"("+l.toport()+")";
297             this->logError(msg);
298             return;
299         }
300       if(currentProc->nodeMap.count(toname) == 0)
301         {
302             msg="to node " + l.tonode() + " does not exist in stream link: ";
303             msg=msg+l.fromnode()+"("+l.fromport()+")->"+l.tonode()+"("+l.toport()+")";
304             this->logError(msg);
305             return;
306         }
307       YACS::ENGINE::OutputDataStreamPort* pout=currentProc->nodeMap[fromname]->getOutputDataStreamPort(l.fromport());
308       YACS::ENGINE::InputDataStreamPort* pin=currentProc->nodeMap[toname]->getInputDataStreamPort(l.toport());
309       _bloc->edAddLink(pout,pin);
310       // Set all properties for this link
311       std::map<std::string, std::string>::const_iterator pt;
312       for(pt=l._props.begin();pt!=l._props.end();pt++)
313         {
314           pin->setProperty((*pt).first,(*pt).second);
315           pout->setProperty((*pt).first,(*pt).second);
316         }
317     }
318   virtual void parameter (const myparam& param)
319     {
320       DEBTRACE( "++++++++++++++++++++Parameter+++++++++++++++++++++" );
321       std::string msg;
322       std::string toname = currentProc->names.back()+param._tonode;
323       if(currentProc->nodeMap.count(toname) == 0)
324         {
325           msg="to node " + param._tonode + " does not exist in parameter: ";
326           msg=msg+"->"+param._tonode+"("+param._toport+")";
327           this->logError(msg);
328           return;
329         }
330       YACS::ENGINE::InputPort* inport=currentProc->nodeMap[toname]->getInputPort(param._toport);
331       //We don't know the parameter type. So we try to initialize the port
332       //with the value. If it's not the right type, edInit throws an exception
333       try
334         {
335           inport->edInit("XML",param._value.c_str());
336         }
337       catch(YACS::Exception& e)
338         {
339           this->logError(e.what());
340         }
341       DEBTRACE( "++++++++++++++++++++End parameter+++++++++++++++++++++" );
342     }
343
344   virtual void preset (ENGINE::DataNode* const& n);
345   virtual void outnode (ENGINE::DataNode* const& n);
346
347   T post()
348     {
349       DEBTRACE( "bloc_post" )             
350       currentProc->names.pop_back();
351       T b=_bloc;
352       _blocs.pop_back();
353       if(_blocs.empty())
354         _bloc=NULL;
355       else
356         _bloc=_blocs.back();
357       return b;
358     }
359
360   T _bloc;
361   std::string _state;
362   std::vector<YACS::ENGINE::Bloc *> _blocs;
363 };
364
365 template <class T> bloctypeParser<T> bloctypeParser<T>::blocParser;
366
367 template <class T>
368 void bloctypeParser<T>::preset (ENGINE::DataNode* const& n)
369 {
370   DEBTRACE("bloc_preset_set: " << n->getName() );
371   _bloc->edAddChild(n);
372   std::string fullname = currentProc->names.back()+n->getName();
373   currentProc->nodeMap[fullname]=n;
374 }
375
376 template <class T>
377 void bloctypeParser<T>::outnode (ENGINE::DataNode* const& n)
378 {
379   DEBTRACE("bloc_outnode: " << n->getName() );
380   _bloc->edAddChild(n);
381   std::string fullname = currentProc->names.back()+n->getName();
382   currentProc->nodeMap[fullname]=n;
383 }
384
385 template <>
386 void bloctypeParser<YACS::ENGINE::Bloc*>::name (const std::string& name)
387 {
388   DEBTRACE( "bloc_name: " << name )             
389   std::string fullname=currentProc->names.back()+name;
390   _bloc=theRuntime->createBloc(name);
391   _blocs.push_back(_bloc);
392   currentProc->names.push_back(fullname+'.');
393 }
394
395 }
396
397 #include "loopParsers.hxx"
398 #include "switchParsers.hxx"
399 #include "presetParsers.hxx"
400 #include "outputParsers.hxx"
401
402 namespace YACS
403 {
404
405 template <class T>
406 void bloctypeParser<T>::onStart(const XML_Char* el, const XML_Char** attr)
407 {
408   DEBTRACE( "bloctypeParser::onStart: " << el )             
409   std::string element(el);
410   checkOrder(element);
411   parser* pp=&parser::main_parser;
412   if(element == "property")pp=&propertytypeParser::propertyParser;
413
414   else if(element == "inline")pp=&inlinetypeParser<>::inlineParser;
415   else if(element == "sinline")pp=&sinlinetypeParser<>::sinlineParser;
416   else if(element == "service")pp=&servicetypeParser<>::serviceParser;
417   else if(element == "server")pp=&servertypeParser<>::serverParser;
418   else if(element == "remote")pp=&remotetypeParser<>::remoteParser;
419   else if(element == "node")pp=&nodetypeParser<>::nodeParser;
420   else if(element == "datanode")pp=&presettypeParser<>::presetParser;
421   else if(element == "outnode")pp=&outnodetypeParser<>::outnodeParser;
422
423   else if(element == "bloc")pp=&bloctypeParser<>::blocParser;
424   else if(element == "forloop")pp=&forlooptypeParser<>::forloopParser;
425   else if(element == "foreach")pp=&foreachlooptypeParser<>::foreachloopParser;
426   else if(element == "optimizer")pp=&optimizerlooptypeParser<>::optimizerloopParser;
427   else if(element == "while")pp=&whilelooptypeParser<>::whileloopParser;
428   else if(element == "switch")pp=&switchtypeParser::switchParser;
429
430   else if(element == "control")pp=&controltypeParser<>::controlParser;
431   else if(element == "datalink")pp=&linktypeParser<>::linkParser;
432   else if(element == "stream")pp=&streamtypeParser<>::streamParser;
433   else if(element == "parameter")pp=&parametertypeParser::parameterParser;
434
435   this->SetUserDataAndPush(pp);
436   pp->init();
437   pp->pre();
438   pp->buildAttr(attr);
439 }
440
441 template <class T>
442 void bloctypeParser<T>::onEnd(const char *el,parser* child)
443 {
444   DEBTRACE( "bloctypeParser::onEnd: " << el )             
445   std::string element(el);
446   if(element == "property")property(((propertytypeParser*)child)->post());
447   else if(element == "inline")inline_(((inlinetypeParser<>*)child)->post());
448   else if(element == "sinline")sinline(((sinlinetypeParser<>*)child)->post());
449   else if(element == "service")service(((servicetypeParser<>*)child)->post());
450   else if(element == "server")server(((servertypeParser<>*)child)->post());
451   else if(element == "remote")remote(((remotetypeParser<>*)child)->post());
452   else if(element == "node")node(((nodetypeParser<>*)child)->post());
453   else if(element == "datanode")preset(((presettypeParser<>*)child)->post());
454   else if(element == "outnode")outnode(((outnodetypeParser<>*)child)->post());
455
456   else if(element == "bloc")bloc(((bloctypeParser<>*)child)->post());
457   else if(element == "forloop")forloop(((forlooptypeParser<>*)child)->post());
458   else if(element == "optimizer")optimizer(((optimizerlooptypeParser<>*)child)->post());
459   else if(element == "foreach")foreach(((foreachlooptypeParser<>*)child)->post());
460   else if(element == "while")while_(((whilelooptypeParser<>*)child)->post());
461   else if(element == "switch")switch_(((switchtypeParser*)child)->post());
462
463   else if(element == "control") control(((controltypeParser<>*)child)->post());
464   else if(element == "datalink") datalink(((linktypeParser<>*)child)->post());
465   else if(element == "stream") stream(((streamtypeParser<>*)child)->post());
466   else if(element == "parameter") parameter(((parametertypeParser*)child)->post());
467 }
468
469 }
470
471 #endif