]> SALOME platform Git repositories - modules/yacs.git/blob - src/yacsloader/nodeParsers.hxx
Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / yacsloader / nodeParsers.hxx
1 //  Copyright (C) 2006-2008  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 #ifndef _NODEPARSERS_HXX_
20 #define _NODEPARSERS_HXX_
21
22 #include "parserBase.hxx"
23 #include "containerParsers.hxx"
24 #include "dataParsers.hxx"
25 #include "portParsers.hxx"
26 #include "codeParsers.hxx"
27 #include "propertyParsers.hxx"
28
29 #include "Proc.hxx"
30 #include "TypeCode.hxx"
31 #include "InlineNode.hxx"
32 #include "ServiceNode.hxx"
33 #include "ServiceInlineNode.hxx"
34 #include "Exception.hxx"
35 #include "Runtime.hxx"
36 #include "OutputDataStreamPort.hxx"
37 #include "InputDataStreamPort.hxx"
38 #include "ComponentInstance.hxx"
39 #include "factory.hxx"
40
41 extern YACS::ENGINE::Proc* currentProc;
42 extern YACS::ENGINE::Runtime* theRuntime;
43
44 namespace YACS
45 {
46
47 /*! \brief Class for node parser.
48  *
49  *  This class is a base class for other parsers
50  */
51 template <class T=YACS::ENGINE::InlineNode*>
52 struct nodetypeParser: parser
53 {
54   static nodetypeParser<T> nodeParser;
55
56   virtual void onStart(const XML_Char* el, const XML_Char** attr)
57     {
58       DEBTRACE( "nodetypeParser::onStart: " << el )             
59       std::string element(el);
60       parser* pp=&parser::main_parser;
61       this->SetUserDataAndPush(pp);
62       pp->init();
63       pp->pre();
64       pp->buildAttr(attr);
65     }
66   virtual void onEnd(const char *el,parser* child)
67     {
68       DEBTRACE( "nodetypeParser::onEnd: " << el )             
69       std::string element(el);
70     }
71   virtual void buildAttr(const XML_Char** attr)
72     {
73       required("name",attr);
74       required("type",attr);
75       for (int i = 0; attr[i]; i += 2) 
76       {
77         if(std::string(attr[i]) == "name")name(attr[i+1]);
78         if(std::string(attr[i]) == "state")state(attr[i+1]);
79         if(std::string(attr[i]) == "type")type(attr[i+1]);
80       }
81     }
82   virtual void pre()
83     {
84       _node=0;
85       _container="";
86     }
87   virtual void name (const std::string& name)
88     {
89       DEBTRACE( "inline_name: " << name )             
90       _name=name;
91     }
92   virtual void state (const std::string& name)
93     {
94       _state=name;
95     }
96   virtual void type (const std::string& name)
97     {
98       DEBTRACE( "node_type " << name )             
99       _type=name;
100     }
101   virtual void property (const myprop& prop);
102   virtual T post();
103   std::string _type;
104   std::string _name;
105   std::string _state;
106   std::string _container;
107   T _node;
108 };
109
110 template <class T> nodetypeParser<T> nodetypeParser<T>::nodeParser;
111
112 template <class T>
113 void nodetypeParser<T>::property (const myprop& prop)
114 {
115    if(this->_node==0)
116      throw YACS::Exception("Node must be completely defined before setting its properties");
117   _node->setProperty(prop._name,prop._value);
118 }
119
120 template <class T>
121 T nodetypeParser<T>::post()
122 {
123   return _node;
124 }
125
126 template <>
127 YACS::ENGINE::InlineNode* nodetypeParser<YACS::ENGINE::InlineNode*>::post ()
128 {
129   std::string fullname = currentProc->names.back()+_type;
130   if(currentProc->inlineMap.count(_type) != 0)
131     {
132       //InlineNode type with absolute name found
133       YACS::ENGINE::InlineNode* n=currentProc->inlineMap[_type];
134       _node=n->cloneNode(_name);
135     }
136   else if(currentProc->inlineMap.count(fullname) != 0)
137     {
138       //InlineNode type with relative name found
139       YACS::ENGINE::InlineNode* n=currentProc->inlineMap[fullname];
140       _node=n->cloneNode(_name);
141     }
142   else
143     {
144       throw YACS::Exception("Unknown InlineNode type");
145     }
146   if(_state == "disabled")_node->exDisabledState();
147   DEBTRACE( "node_post " << _node->getName() )             
148   return _node;
149 }
150
151 static std::string t1[]={"script","function",""};
152
153 template <class T=YACS::ENGINE::InlineNode*>
154 struct inlinetypeParser:public nodetypeParser<T>
155 {
156   static inlinetypeParser<T> inlineParser;
157
158   virtual void onStart(const XML_Char* el, const XML_Char** attr);
159   virtual void onEnd(const char *el,parser* child)
160     {
161       DEBTRACE( "inlinetypeParser::onEnd: " << el )             
162       std::string element(el);
163       if(element == "kind")kind(((stringtypeParser*)child)->post());
164       else if(element == "script")script(((codetypeParser*)child)->post());
165       else if(element == "function")function(((functypeParser*)child)->post());
166       else if(element == "property")this->property(((propertytypeParser*)child)->post());
167       else if(element == "inport") inport(((inporttypeParser<myinport>*)child)->post());
168       else if(element == "outport") outport(((outporttypeParser<myoutport>*)child)->post());
169     }
170   virtual void buildAttr(const XML_Char** attr)
171     {
172       this->required("name",attr);
173       for (int i = 0; attr[i]; i += 2) 
174       {
175         if(std::string(attr[i]) == "name")this->name(attr[i+1]);
176         if(std::string(attr[i]) == "state")this->state(attr[i+1]);
177       }
178     }
179   virtual void pre ()
180     {
181       this->_node=0;
182       _kind="";
183       this->_state="";
184       this->_container="";
185     }
186   virtual void kind (const std::string& name)
187     {
188       DEBTRACE( "inline_kind " << name )             
189       _kind=name;
190     }
191
192   virtual void script (const myfunc& f){}
193   virtual void function (const myfunc& f) {}
194
195   virtual void inport (const myinport& p)
196     {
197       DEBTRACE( "inline_inport: " << p._name <<":"<<p._type)             
198       if(this->_node==0)
199         throw YACS::Exception("Node must be completely defined before defining its ports");
200
201       if(currentProc->typeMap.count(p._type)==0)
202       {
203         //Check if the typecode is defined in the runtime
204         YACS::ENGINE::TypeCode* t=theRuntime->getTypeCode(p._type);
205         if(t==0)
206         {
207           std::string msg="Unknown InPort Type: ";
208           msg=msg+p._type+" for node: "+this->_node->getName()+" port name: "+p._name;
209           throw YACS::Exception(msg);
210         }
211         else
212         {
213           currentProc->typeMap[p._type]=t;
214           t->incrRef();
215         }
216       }
217       this->_node->edAddInputPort(p._name,currentProc->typeMap[p._type]);
218     }
219   virtual void outport (const myoutport& p)
220     {
221       DEBTRACE( "inline_outport: " << p._name <<":"<<p._type)             
222       if(this->_node==0)
223         throw YACS::Exception("Node must be completely defined before defining its ports");
224
225       if(currentProc->typeMap.count(p._type)==0)
226       {
227         YACS::ENGINE::TypeCode* t=theRuntime->getTypeCode(p._type);
228         if(t==0)
229         {
230           std::string msg="Unknown OutPort Type: ";
231           msg=msg+p._type+" for node: "+this->_node->getName()+" port name: "+p._name;
232           throw YACS::Exception(msg);
233         }
234         else
235         {
236           currentProc->typeMap[p._type]=t;
237           t->incrRef();
238         }
239       }
240       this->_node->edAddOutputPort(p._name,currentProc->typeMap[p._type]);
241     }
242   virtual T post()
243     {
244       DEBTRACE( "inline_post " << this->_node->getName() )             
245       if(this->_state == "disabled")this->_node->exDisabledState();
246       /*
247       std::list<OutputPort *>::iterator iter;
248       std::list<OutputPort *> s=_node->getSetOfOutputPort();
249       for(iter=s.begin();iter!=s.end();iter++)
250         {
251           std::cerr << "port name: " << (*iter)->getName() << std::endl;
252           std::cerr << "port kind: " << (*iter)->edGetType()->kind() << std::endl;
253         }
254         */
255       return this->_node;
256     }
257   std::string _kind;
258 };
259
260 template <class T> inlinetypeParser<T> inlinetypeParser<T>::inlineParser;
261
262 template <>
263 void inlinetypeParser<YACS::ENGINE::InlineNode*>::script (const myfunc& f)
264 {
265   DEBTRACE( "inline_script: " << f._code )             
266   _node=theRuntime->createScriptNode(_kind,_name);
267   _node->setScript(f._code);
268 }
269
270 template <>
271 void inlinetypeParser<YACS::ENGINE::InlineNode*>::function (const myfunc& f)
272 {
273   DEBTRACE( "inline_function: " << f._code )             
274   YACS::ENGINE::InlineFuncNode *fnode;
275   fnode=theRuntime->createFuncNode(_kind,_name);
276   fnode->setScript(f._code);
277   fnode->setFname(f._name);
278   _node=fnode;
279 }
280
281 /*! \brief Class for parsing ServiceInlineNode description
282  *
283  *  
284  */
285 template <class T=YACS::ENGINE::ServiceInlineNode*>
286 struct sinlinetypeParser:public inlinetypeParser<T>
287 {
288   static sinlinetypeParser<T> sinlineParser;
289
290   virtual void onStart(const XML_Char* el, const XML_Char** attr);
291   virtual void onEnd(const char *el,parser* child)
292     {
293       DEBTRACE( "sinlinetypeParser::onEnd: " << el )             
294       std::string element(el);
295       if(element == "kind")this->kind(((stringtypeParser*)child)->post());
296       else if(element == "function")this->function(((functypeParser*)child)->post());
297       else if(element == "load") load(((loadtypeParser*)child)->post());
298       else if(element == "property")this->property(((propertytypeParser*)child)->post());
299       else if(element == "inport") this->inport(((inporttypeParser<myinport>*)child)->post());
300       else if(element == "outport") this->outport(((outporttypeParser<myoutport>*)child)->post());
301     }
302   //virtual void service (const myfunc& f) {}
303   virtual void load (const loadon& l)
304     {
305       DEBTRACE( "sinline_load: " )             
306       this->_container=l._container;
307     }
308   virtual T post()
309     {
310       DEBTRACE( "sinline_post " << this->_node->getName() );
311       if(this->_state == "disabled")this->_node->exDisabledState();
312
313       if(currentProc->containerMap.count(this->_container) != 0)
314         {
315           this->_node->getComponent()->setContainer(currentProc->containerMap[this->_container]);
316         }
317       else if(this->_container == "")
318         {
319           if(currentProc->containerMap.count("DefaultContainer") != 0)
320           {
321             //a default container is defined : use it
322             this->_node->getComponent()->setContainer(currentProc->containerMap["DefaultContainer"]);
323           }
324         }
325       else
326         {
327           std::cerr << "WARNING: Unknown container " << this->_container << " ignored" << std::endl;
328         }
329
330       return this->_node;
331     }
332 };
333 template <class T> sinlinetypeParser<T> sinlinetypeParser<T>::sinlineParser;
334
335 // sinline ????
336 template <>
337 void inlinetypeParser<YACS::ENGINE::ServiceInlineNode*>::function (const myfunc& f)
338 {
339   DEBTRACE( "sinline_function: " << f._code )             
340   YACS::ENGINE::ServiceInlineNode *fnode;
341   fnode=theRuntime->createSInlineNode(_kind,_name);
342   fnode->setScript(f._code);
343   fnode->setMethod(f._name);
344   fnode->setComponent(theRuntime->createComponentInstance("PyCompo","SalomePy"));
345   //fnode->setRef("PyCompo");
346   _node=fnode;
347 }
348
349 static std::string t2[]={"ref","node","component",""};
350
351 template <class T=YACS::ENGINE::ServiceNode*>
352 struct servicetypeParser:public inlinetypeParser<T>
353 {
354   static servicetypeParser<T> serviceParser;
355
356   virtual void onStart(const XML_Char* el, const XML_Char** attr);
357   virtual void onEnd(const char *el,parser* child)
358     {
359       DEBTRACE( "servicetypeParser::onEnd: " << el )             
360       std::string element(el);
361       if(element == "kind")this->kind(((stringtypeParser*)child)->post());
362       else if(element == "ref") ref(((stringtypeParser*)child)->post());
363       else if(element == "component") component(((stringtypeParser*)child)->post());
364       else if(element == "node") node(((stringtypeParser*)child)->post());
365       else if(element == "method") method(((stringtypeParser*)child)->post());
366       else if(element == "load") load(((loadtypeParser*)child)->post());
367       else if(element == "property")this->property(((propertytypeParser*)child)->post());
368       else if(element == "inport") this->inport(((inporttypeParser<myinport>*)child)->post());
369       else if(element == "outport") this->outport(((outporttypeParser<myoutport>*)child)->post());
370       else if(element == "instream") instream(((inporttypeParser<myinport>*)child)->post());
371       else if(element == "outstream") outstream(((outporttypeParser<myoutport>*)child)->post());
372     }
373   virtual void ref (const std::string& name)
374     {
375       DEBTRACE( "service_ref: " << name )             
376       this->_node=theRuntime->createRefNode(this->_kind,this->_name);
377       this->_node->setRef(name);
378     }
379   virtual void component (const std::string& name)
380     {
381       DEBTRACE( "service_component: " << name )             
382       this->_node=theRuntime->createCompoNode(this->_kind,this->_name);
383       this->_node->setRef(name);
384     }
385   virtual void node (const std::string& name)
386     {
387       DEBTRACE( "service_node: " << name )             
388       std::string fullname = currentProc->names.back()+name;
389       if(currentProc->serviceMap.count(name) != 0)
390         {
391           //ServiceNode with absolute name found
392           YACS::ENGINE::ServiceNode* n=currentProc->serviceMap[name];
393           this->_node =n->createNode(this->_name);
394         }
395       else if(currentProc->serviceMap.count(fullname) != 0)
396         {
397           //ServiceNode with relative name found
398           //TODO: must be a short name (possible only in the same context)
399           YACS::ENGINE::ServiceNode* n=currentProc->serviceMap[fullname];
400           this->_node =n->createNode(this->_name);
401         }
402       else
403         {
404           throw YACS::Exception("Unknown ServiceNode");
405         }
406     }
407
408   virtual void method (const std::string& name)
409     {
410       DEBTRACE( "service_method: " << name )             
411       if(this->_node==0)
412         throw YACS::Exception("ServiceNode must be completely defined before defining its method");
413       if(name == "")
414       {
415         this->logError("a service name must be a non empty string");
416         return;
417       }
418       this->_node->setMethod(name);
419     }
420
421   virtual void load (const loadon& l)
422     {
423       DEBTRACE( "service_load: " );
424       this->_container=l._container;
425     }
426
427   virtual void instream (const myinport& p)
428     {
429       DEBTRACE( "service_instream" )             
430       DEBTRACE( p._type )             
431       DEBTRACE( p._name )             
432       if(this->_node==0)
433         throw YACS::Exception("ServiceNode must be completely defined before defining its ports");
434
435       if(currentProc->typeMap.count(p._type)==0)
436       {
437         YACS::ENGINE::TypeCode* t=theRuntime->getTypeCode(p._type);
438         if(t==0)
439         {
440           std::string msg="Unknown InStreamPort Type: ";
441           msg=msg+p._type+" for node: "+this->_node->getName()+" port name: "+p._name;
442           throw YACS::Exception(msg);
443         }
444         else
445         {
446           currentProc->typeMap[p._type]=t;
447           t->incrRef();
448         }
449       }
450       YACS::ENGINE::InputDataStreamPort* port;
451       port=this->_node->edAddInputDataStreamPort(p._name,currentProc->typeMap[p._type]);
452       // Set all properties for this port
453       std::map<std::string, std::string>::const_iterator pt;
454       for(pt=p._props.begin();pt!=p._props.end();pt++)
455         port->setProperty((*pt).first,(*pt).second);
456     }
457   virtual void outstream (const myoutport& p)
458     {
459       DEBTRACE( "service_outstream" )             
460       DEBTRACE( p._type )             
461       DEBTRACE( p._name )             
462       if(this->_node==0)
463         throw YACS::Exception("ServiceNode must be completely defined before defining its ports");
464
465       if(currentProc->typeMap.count(p._type)==0)
466       {
467         YACS::ENGINE::TypeCode* t=theRuntime->getTypeCode(p._type);
468         if(t==0)
469         {
470           std::string msg="Unknown OutStreamPort Type: ";
471           msg=msg+p._type+" for node: "+this->_node->getName()+" port name: "+p._name;
472           throw YACS::Exception(msg);
473         }
474         else
475         {
476           currentProc->typeMap[p._type]=t;
477           t->incrRef();
478         }
479       }
480       YACS::ENGINE::OutputDataStreamPort* port;
481       port=this->_node->edAddOutputDataStreamPort(p._name,currentProc->typeMap[p._type]);
482       // Set all properties for this port
483       std::map<std::string, std::string>::const_iterator pt;
484       for(pt=p._props.begin();pt!=p._props.end();pt++)
485         port->setProperty((*pt).first,(*pt).second);
486     }
487   virtual T post()
488     {
489       DEBTRACE( "service_post " << this->_node->getName() )             
490       this->mincount("method",1);
491       if(this->_state == "disabled")this->_node->exDisabledState();
492
493       if(currentProc->containerMap.count(this->_container) != 0)
494         {
495           this->_node->getComponent()->setContainer(currentProc->containerMap[this->_container]);
496         }
497       else if(this->_container == "")
498         {
499           if(currentProc->containerMap.count("DefaultContainer") != 0)
500           {
501             //a default container is defined : use it
502             this->_node->getComponent()->setContainer(currentProc->containerMap["DefaultContainer"]);
503           }
504         }
505       else
506         {
507           std::cerr << "WARNING: Unknown container " << this->_container << " ignored" << std::endl;
508         }
509       return this->_node;
510     }
511 };
512
513 template <class T> servicetypeParser<T> servicetypeParser<T>::serviceParser;
514
515 }
516
517
518 namespace YACS
519 {
520 template <class T>
521 void inlinetypeParser<T>::onStart(const XML_Char* el, const XML_Char** attr)
522
523     {
524       DEBTRACE( "inlinetypeParser::onStart: " << el )
525       std::string element(el);
526       parser* pp=&parser::main_parser;
527       this->maxcount("kind",1,element);
528       this->maxcount("script",1,element);
529       this->maxcount("function",1,element);
530       this->maxchoice(t1,1,element);
531       if(element == "kind")pp=&stringtypeParser::stringParser;
532       else if(element == "script")pp=&codetypeParser::codeParser;
533       else if(element == "function")pp=&functypeParser::funcParser;
534       else if(element == "property")pp=&propertytypeParser::propertyParser;
535       else if(element == "inport")pp=&inporttypeParser<>::inportParser;
536       else if(element == "outport")pp=&outporttypeParser<>::outportParser;
537       this->SetUserDataAndPush(pp);
538       pp->init();
539       pp->pre();
540       pp->buildAttr(attr);
541     }
542
543 template <class T>
544 void sinlinetypeParser<T>::onStart(const XML_Char* el, const XML_Char** attr)
545     {
546       DEBTRACE( "sinlinetypeParser::onStart: " << el )
547       std::string element(el);
548       parser* pp=&parser::main_parser;
549       this->maxcount("kind",1,element);
550       this->maxcount("function",1,element);
551       this->maxcount("load",1,element);
552       if(element == "kind")pp=&stringtypeParser::stringParser;
553       else if(element == "function")pp=&functypeParser::funcParser;
554       else if(element == "load")pp=&loadtypeParser::loadParser;
555       else if(element == "property")pp=&propertytypeParser::propertyParser;
556       else if(element == "inport")pp=&inporttypeParser<>::inportParser;
557       else if(element == "outport")pp=&outporttypeParser<>::outportParser;
558       this->SetUserDataAndPush(pp);
559       pp->init();
560       pp->pre();
561       pp->buildAttr(attr);
562     }
563
564 template <class T>
565 void servicetypeParser<T>::onStart(const XML_Char* el, const XML_Char** attr)
566     {
567       DEBTRACE( "servicetypeParser::onStart: " << el )
568       std::string element(el);
569       parser* pp=&parser::main_parser;
570       this->maxcount("kind",1,element);
571       this->maxcount("ref",1,element);
572       this->maxcount("node",1,element);
573       this->maxcount("component",1,element);
574       this->maxcount("method",1,element);
575       this->maxcount("load",1,element);
576       this->maxchoice(t2,1,element);
577       if(element == "kind")pp=&stringtypeParser::stringParser;
578       else if(element == "ref")pp=&stringtypeParser::stringParser;
579       else if(element == "component")pp=&stringtypeParser::stringParser;
580       else if(element == "node")pp=&stringtypeParser::stringParser;
581       else if(element == "method")pp=&stringtypeParser::stringParser;
582       else if(element == "load")pp=&loadtypeParser::loadParser;
583       else if(element == "property")pp=&propertytypeParser::propertyParser;
584       else if(element == "inport")pp=&inporttypeParser<>::inportParser;
585       else if(element == "outport")pp=&outporttypeParser<>::outportParser;
586       else if(element == "instream")pp=&inporttypeParser<>::inportParser;
587       else if(element == "outstream")pp=&outporttypeParser<>::outportParser;
588       this->SetUserDataAndPush(pp);
589       pp->init();
590       pp->pre();
591       pp->buildAttr(attr);
592     }
593
594
595
596
597 }
598
599 #endif