2 #ifndef _NODEPARSERS_HXX_
3 #define _NODEPARSERS_HXX_
5 #include "parserBase.hxx"
6 #include "containerParsers.hxx"
7 #include "dataParsers.hxx"
8 #include "portParsers.hxx"
9 #include "codeParsers.hxx"
10 #include "propertyParsers.hxx"
13 #include "TypeCode.hxx"
14 #include "InlineNode.hxx"
15 #include "ServiceNode.hxx"
16 #include "ServiceInlineNode.hxx"
17 #include "Exception.hxx"
18 #include "Runtime.hxx"
19 #include "OutputDataStreamPort.hxx"
20 #include "InputDataStreamPort.hxx"
21 #include "ComponentInstance.hxx"
22 #include "factory.hxx"
24 extern YACS::ENGINE::Proc* currentProc;
25 extern YACS::ENGINE::Runtime* theRuntime;
30 /*! \brief Class for node parser.
32 * This class is a base class for other parsers
34 template <class T=YACS::ENGINE::InlineNode*>
35 struct nodetypeParser: parser
37 static nodetypeParser<T> nodeParser;
39 virtual void onStart(const XML_Char* el, const XML_Char** attr)
41 DEBTRACE( "nodetypeParser::onStart: " << el )
42 std::string element(el);
43 parser* pp=&parser::main_parser;
44 this->SetUserDataAndPush(pp);
49 virtual void onEnd(const char *el,parser* child)
51 DEBTRACE( "nodetypeParser::onEnd: " << el )
52 std::string element(el);
54 virtual void buildAttr(const XML_Char** attr)
56 required("name",attr);
57 required("type",attr);
58 for (int i = 0; attr[i]; i += 2)
60 if(std::string(attr[i]) == "name")name(attr[i+1]);
61 if(std::string(attr[i]) == "state")state(attr[i+1]);
62 if(std::string(attr[i]) == "type")type(attr[i+1]);
70 virtual void name (const std::string& name)
72 DEBTRACE( "inline_name: " << name )
75 virtual void state (const std::string& name)
79 virtual void type (const std::string& name)
81 DEBTRACE( "node_type " << name )
84 virtual void property (const myprop& prop);
89 std::string _container;
93 template <class T> nodetypeParser<T> nodetypeParser<T>::nodeParser;
96 void nodetypeParser<T>::property (const myprop& prop)
98 _node->setProperty(prop._name,prop._value);
102 T nodetypeParser<T>::post()
108 YACS::ENGINE::InlineNode* nodetypeParser<YACS::ENGINE::InlineNode*>::post ()
110 std::string fullname = currentProc->names.back()+_type;
111 if(currentProc->inlineMap.count(_type) != 0)
113 //InlineNode type with absolute name found
114 YACS::ENGINE::InlineNode* n=currentProc->inlineMap[_type];
115 _node=n->cloneNode(_name);
117 else if(currentProc->inlineMap.count(fullname) != 0)
119 //InlineNode type with relative name found
120 YACS::ENGINE::InlineNode* n=currentProc->inlineMap[fullname];
121 _node=n->cloneNode(_name);
125 throw YACS::Exception("Unknown InlineNode type");
127 if(_state == "disabled")_node->exDisabledState();
128 DEBTRACE( "node_post " << _node->getName() )
132 static std::string t1[]={"script","function",""};
134 template <class T=YACS::ENGINE::InlineNode*>
135 struct inlinetypeParser:public nodetypeParser<T>
137 static inlinetypeParser<T> inlineParser;
139 virtual void onStart(const XML_Char* el, const XML_Char** attr);
140 virtual void onEnd(const char *el,parser* child)
142 DEBTRACE( "inlinetypeParser::onEnd: " << el )
143 std::string element(el);
144 if(element == "kind")kind(((stringtypeParser*)child)->post());
145 else if(element == "script")script(((codetypeParser*)child)->post());
146 else if(element == "function")function(((functypeParser*)child)->post());
147 else if(element == "property")this->property(((propertytypeParser*)child)->post());
148 else if(element == "inport") inport(((inporttypeParser<myinport>*)child)->post());
149 else if(element == "outport") outport(((outporttypeParser<myoutport>*)child)->post());
151 virtual void buildAttr(const XML_Char** attr)
153 this->required("name",attr);
154 for (int i = 0; attr[i]; i += 2)
156 if(std::string(attr[i]) == "name")this->name(attr[i+1]);
157 if(std::string(attr[i]) == "state")this->state(attr[i+1]);
167 virtual void kind (const std::string& name)
169 DEBTRACE( "inline_kind " << name )
173 virtual void script (const myfunc& f){}
174 virtual void function (const myfunc& f) {}
176 virtual void inport (const myinport& p)
178 DEBTRACE( "inline_inport: " << p._name <<":"<<p._type)
180 throw YACS::Exception("Node must be completely defined before defining its ports");
182 if(currentProc->typeMap.count(p._type)==0)
184 //Check if the typecode is defined in the runtime
185 YACS::ENGINE::TypeCode* t=theRuntime->getTypeCode(p._type);
188 std::string msg="Unknown InPort Type: ";
189 msg=msg+p._type+" for node: "+this->_node->getName()+" port name: "+p._name;
190 throw YACS::Exception(msg);
194 currentProc->typeMap[p._type]=t;
198 this->_node->edAddInputPort(p._name,currentProc->typeMap[p._type]);
200 virtual void outport (const myoutport& p)
202 DEBTRACE( "inline_outport: " << p._name <<":"<<p._type)
204 throw YACS::Exception("Node must be completely defined before defining its ports");
206 if(currentProc->typeMap.count(p._type)==0)
208 YACS::ENGINE::TypeCode* t=theRuntime->getTypeCode(p._type);
211 std::string msg="Unknown OutPort Type: ";
212 msg=msg+p._type+" for node: "+this->_node->getName()+" port name: "+p._name;
213 throw YACS::Exception(msg);
217 currentProc->typeMap[p._type]=t;
221 this->_node->edAddOutputPort(p._name,currentProc->typeMap[p._type]);
225 DEBTRACE( "inline_post " << this->_node->getName() )
226 if(this->_state == "disabled")this->_node->exDisabledState();
228 std::list<OutputPort *>::iterator iter;
229 std::list<OutputPort *> s=_node->getSetOfOutputPort();
230 for(iter=s.begin();iter!=s.end();iter++)
232 std::cerr << "port name: " << (*iter)->getName() << std::endl;
233 std::cerr << "port kind: " << (*iter)->edGetType()->kind() << std::endl;
241 template <class T> inlinetypeParser<T> inlinetypeParser<T>::inlineParser;
244 void inlinetypeParser<YACS::ENGINE::InlineNode*>::script (const myfunc& f)
246 DEBTRACE( "inline_script: " << f._code )
247 _node=theRuntime->createScriptNode(_kind,_name);
248 _node->setScript(f._code);
252 void inlinetypeParser<YACS::ENGINE::InlineNode*>::function (const myfunc& f)
254 DEBTRACE( "inline_function: " << f._code )
255 YACS::ENGINE::InlineFuncNode *fnode;
256 fnode=theRuntime->createFuncNode(_kind,_name);
257 fnode->setScript(f._code);
258 fnode->setFname(f._name);
262 /*! \brief Class for parsing ServiceInlineNode description
266 template <class T=YACS::ENGINE::ServiceInlineNode*>
267 struct sinlinetypeParser:public inlinetypeParser<T>
269 static sinlinetypeParser<T> sinlineParser;
271 virtual void onStart(const XML_Char* el, const XML_Char** attr);
272 virtual void onEnd(const char *el,parser* child)
274 DEBTRACE( "sinlinetypeParser::onEnd: " << el )
275 std::string element(el);
276 if(element == "kind")this->kind(((stringtypeParser*)child)->post());
277 else if(element == "function")this->function(((functypeParser*)child)->post());
278 else if(element == "load") load(((loadtypeParser*)child)->post());
279 else if(element == "property")this->property(((propertytypeParser*)child)->post());
280 else if(element == "inport") this->inport(((inporttypeParser<myinport>*)child)->post());
281 else if(element == "outport") this->outport(((outporttypeParser<myoutport>*)child)->post());
283 //virtual void service (const myfunc& f) {}
284 virtual void load (const loadon& l)
286 DEBTRACE( "sinline_load: " )
287 this->_container=l._container;
291 DEBTRACE( "sinline_post " << this->_node->getName() );
292 if(this->_state == "disabled")this->_node->exDisabledState();
294 if(currentProc->containerMap.count(this->_container) != 0)
296 this->_node->getComponent()->setContainer(currentProc->containerMap[this->_container]);
298 else if(this->_container == "")
300 if(currentProc->containerMap.count("DefaultContainer") != 0)
302 //a default container is defined : use it
303 this->_node->getComponent()->setContainer(currentProc->containerMap["DefaultContainer"]);
308 std::cerr << "WARNING: Unknown container " << this->_container << " ignored" << std::endl;
314 template <class T> sinlinetypeParser<T> sinlinetypeParser<T>::sinlineParser;
318 void inlinetypeParser<YACS::ENGINE::ServiceInlineNode*>::function (const myfunc& f)
320 DEBTRACE( "sinline_function: " << f._code )
321 YACS::ENGINE::ServiceInlineNode *fnode;
322 fnode=theRuntime->createSInlineNode(_kind,_name);
323 fnode->setScript(f._code);
324 fnode->setMethod(f._name);
325 fnode->setComponent(theRuntime->createComponentInstance("PyCompo","SalomePy"));
326 //fnode->setRef("PyCompo");
330 static std::string t2[]={"ref","node","component",""};
332 template <class T=YACS::ENGINE::ServiceNode*>
333 struct servicetypeParser:public inlinetypeParser<T>
335 static servicetypeParser<T> serviceParser;
337 virtual void onStart(const XML_Char* el, const XML_Char** attr);
338 virtual void onEnd(const char *el,parser* child)
340 DEBTRACE( "servicetypeParser::onEnd: " << el )
341 std::string element(el);
342 if(element == "kind")this->kind(((stringtypeParser*)child)->post());
343 else if(element == "ref") ref(((stringtypeParser*)child)->post());
344 else if(element == "component") component(((stringtypeParser*)child)->post());
345 else if(element == "node") node(((stringtypeParser*)child)->post());
346 else if(element == "method") method(((stringtypeParser*)child)->post());
347 else if(element == "load") load(((loadtypeParser*)child)->post());
348 else if(element == "property")this->property(((propertytypeParser*)child)->post());
349 else if(element == "inport") this->inport(((inporttypeParser<myinport>*)child)->post());
350 else if(element == "outport") this->outport(((outporttypeParser<myoutport>*)child)->post());
351 else if(element == "instream") instream(((inporttypeParser<myinport>*)child)->post());
352 else if(element == "outstream") outstream(((outporttypeParser<myoutport>*)child)->post());
354 virtual void ref (const std::string& name)
356 DEBTRACE( "service_ref: " << name )
357 this->_node=theRuntime->createRefNode(this->_kind,this->_name);
358 this->_node->setRef(name);
360 virtual void component (const std::string& name)
362 DEBTRACE( "service_component: " << name )
363 this->_node=theRuntime->createCompoNode(this->_kind,this->_name);
364 this->_node->setRef(name);
366 virtual void node (const std::string& name)
368 DEBTRACE( "service_node: " << name )
369 std::string fullname = currentProc->names.back()+name;
370 if(currentProc->serviceMap.count(name) != 0)
372 //ServiceNode with absolute name found
373 YACS::ENGINE::ServiceNode* n=currentProc->serviceMap[name];
374 this->_node =n->createNode(this->_name);
376 else if(currentProc->serviceMap.count(fullname) != 0)
378 //ServiceNode with relative name found
379 //TODO: must be a short name (possible only in the same context)
380 YACS::ENGINE::ServiceNode* n=currentProc->serviceMap[fullname];
381 this->_node =n->createNode(this->_name);
385 throw YACS::Exception("Unknown ServiceNode");
389 virtual void method (const std::string& name)
391 DEBTRACE( "service_method: " << name )
393 throw YACS::Exception("ServiceNode must be completely defined before defining its method");
396 this->logError("a service name must be a non empty string");
399 this->_node->setMethod(name);
402 virtual void load (const loadon& l)
404 DEBTRACE( "service_load: " );
405 this->_container=l._container;
408 virtual void instream (const myinport& p)
410 DEBTRACE( "service_instream" )
414 throw YACS::Exception("ServiceNode must be completely defined before defining its ports");
416 if(currentProc->typeMap.count(p._type)==0)
418 YACS::ENGINE::TypeCode* t=theRuntime->getTypeCode(p._type);
421 std::string msg="Unknown InStreamPort Type: ";
422 msg=msg+p._type+" for node: "+this->_node->getName()+" port name: "+p._name;
423 throw YACS::Exception(msg);
427 currentProc->typeMap[p._type]=t;
431 YACS::ENGINE::InputDataStreamPort* port;
432 port=this->_node->edAddInputDataStreamPort(p._name,currentProc->typeMap[p._type]);
433 // Set all properties for this port
434 std::map<std::string, std::string>::const_iterator pt;
435 for(pt=p._props.begin();pt!=p._props.end();pt++)
436 port->setProperty((*pt).first,(*pt).second);
438 virtual void outstream (const myoutport& p)
440 DEBTRACE( "service_outstream" )
444 throw YACS::Exception("ServiceNode must be completely defined before defining its ports");
446 if(currentProc->typeMap.count(p._type)==0)
448 YACS::ENGINE::TypeCode* t=theRuntime->getTypeCode(p._type);
451 std::string msg="Unknown OutStreamPort Type: ";
452 msg=msg+p._type+" for node: "+this->_node->getName()+" port name: "+p._name;
453 throw YACS::Exception(msg);
457 currentProc->typeMap[p._type]=t;
461 YACS::ENGINE::OutputDataStreamPort* port;
462 port=this->_node->edAddOutputDataStreamPort(p._name,currentProc->typeMap[p._type]);
463 // Set all properties for this port
464 std::map<std::string, std::string>::const_iterator pt;
465 for(pt=p._props.begin();pt!=p._props.end();pt++)
466 port->setProperty((*pt).first,(*pt).second);
470 DEBTRACE( "service_post " << this->_node->getName() )
471 this->mincount("method",1);
472 if(this->_state == "disabled")this->_node->exDisabledState();
474 if(currentProc->containerMap.count(this->_container) != 0)
476 this->_node->getComponent()->setContainer(currentProc->containerMap[this->_container]);
478 else if(this->_container == "")
480 if(currentProc->containerMap.count("DefaultContainer") != 0)
482 //a default container is defined : use it
483 this->_node->getComponent()->setContainer(currentProc->containerMap["DefaultContainer"]);
488 std::cerr << "WARNING: Unknown container " << this->_container << " ignored" << std::endl;
494 template <class T> servicetypeParser<T> servicetypeParser<T>::serviceParser;
502 void inlinetypeParser<T>::onStart(const XML_Char* el, const XML_Char** attr)
505 DEBTRACE( "inlinetypeParser::onStart: " << el )
506 std::string element(el);
507 parser* pp=&parser::main_parser;
508 this->maxcount("kind",1,element);
509 this->maxcount("script",1,element);
510 this->maxcount("function",1,element);
511 this->maxchoice(t1,1,element);
512 if(element == "kind")pp=&stringtypeParser::stringParser;
513 else if(element == "script")pp=&codetypeParser::codeParser;
514 else if(element == "function")pp=&functypeParser::funcParser;
515 else if(element == "property")pp=&propertytypeParser::propertyParser;
516 else if(element == "inport")pp=&inporttypeParser<>::inportParser;
517 else if(element == "outport")pp=&outporttypeParser<>::outportParser;
518 this->SetUserDataAndPush(pp);
525 void sinlinetypeParser<T>::onStart(const XML_Char* el, const XML_Char** attr)
527 DEBTRACE( "sinlinetypeParser::onStart: " << el )
528 std::string element(el);
529 parser* pp=&parser::main_parser;
530 this->maxcount("kind",1,element);
531 this->maxcount("function",1,element);
532 this->maxcount("load",1,element);
533 if(element == "kind")pp=&stringtypeParser::stringParser;
534 else if(element == "function")pp=&functypeParser::funcParser;
535 else if(element == "load")pp=&loadtypeParser::loadParser;
536 else if(element == "property")pp=&propertytypeParser::propertyParser;
537 else if(element == "inport")pp=&inporttypeParser<>::inportParser;
538 else if(element == "outport")pp=&outporttypeParser<>::outportParser;
539 this->SetUserDataAndPush(pp);
546 void servicetypeParser<T>::onStart(const XML_Char* el, const XML_Char** attr)
548 DEBTRACE( "servicetypeParser::onStart: " << el )
549 std::string element(el);
550 parser* pp=&parser::main_parser;
551 this->maxcount("kind",1,element);
552 this->maxcount("ref",1,element);
553 this->maxcount("node",1,element);
554 this->maxcount("component",1,element);
555 this->maxcount("method",1,element);
556 this->maxcount("load",1,element);
557 this->maxchoice(t2,1,element);
558 if(element == "kind")pp=&stringtypeParser::stringParser;
559 else if(element == "ref")pp=&stringtypeParser::stringParser;
560 else if(element == "component")pp=&stringtypeParser::stringParser;
561 else if(element == "node")pp=&stringtypeParser::stringParser;
562 else if(element == "method")pp=&stringtypeParser::stringParser;
563 else if(element == "load")pp=&loadtypeParser::loadParser;
564 else if(element == "property")pp=&propertytypeParser::propertyParser;
565 else if(element == "inport")pp=&inporttypeParser<>::inportParser;
566 else if(element == "outport")pp=&outporttypeParser<>::outportParser;
567 else if(element == "instream")pp=&inporttypeParser<>::inportParser;
568 else if(element == "outstream")pp=&outporttypeParser<>::outportParser;
569 this->SetUserDataAndPush(pp);