1 // Copyright (C) 2006-2008 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #ifndef _NODEPARSERS_HXX_
20 #define _NODEPARSERS_HXX_
22 #include "parserBase.hxx"
23 #include "containerParsers.hxx"
24 #include "dataParsers.hxx"
25 #include "portParsers.hxx"
26 #include "codeParsers.hxx"
27 #include "propertyParsers.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"
41 extern YACS::ENGINE::Proc* currentProc;
42 extern YACS::ENGINE::Runtime* theRuntime;
47 /*! \brief Class for node parser.
49 * This class is a base class for other parsers
51 template <class T=YACS::ENGINE::InlineNode*>
52 struct nodetypeParser: parser
54 static nodetypeParser<T> nodeParser;
56 virtual void onStart(const XML_Char* el, const XML_Char** attr)
58 DEBTRACE( "nodetypeParser::onStart: " << el )
59 std::string element(el);
60 parser* pp=&parser::main_parser;
61 this->SetUserDataAndPush(pp);
66 virtual void onEnd(const char *el,parser* child)
68 DEBTRACE( "nodetypeParser::onEnd: " << el )
69 std::string element(el);
71 virtual void buildAttr(const XML_Char** attr)
73 required("name",attr);
74 required("type",attr);
75 for (int i = 0; attr[i]; i += 2)
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]);
87 virtual void name (const std::string& name)
89 DEBTRACE( "inline_name: " << name )
92 virtual void state (const std::string& name)
96 virtual void type (const std::string& name)
98 DEBTRACE( "node_type " << name )
101 virtual void property (const myprop& prop);
106 std::string _container;
110 template <class T> nodetypeParser<T> nodetypeParser<T>::nodeParser;
113 void nodetypeParser<T>::property (const myprop& prop)
116 throw YACS::Exception("Node must be completely defined before setting its properties");
117 _node->setProperty(prop._name,prop._value);
121 T nodetypeParser<T>::post()
127 YACS::ENGINE::InlineNode* nodetypeParser<YACS::ENGINE::InlineNode*>::post ()
129 std::string fullname = currentProc->names.back()+_type;
130 if(currentProc->inlineMap.count(_type) != 0)
132 //InlineNode type with absolute name found
133 YACS::ENGINE::InlineNode* n=currentProc->inlineMap[_type];
134 _node=n->cloneNode(_name);
136 else if(currentProc->inlineMap.count(fullname) != 0)
138 //InlineNode type with relative name found
139 YACS::ENGINE::InlineNode* n=currentProc->inlineMap[fullname];
140 _node=n->cloneNode(_name);
144 throw YACS::Exception("Unknown InlineNode type");
146 if(_state == "disabled")_node->exDisabledState();
147 DEBTRACE( "node_post " << _node->getName() )
151 static std::string t1[]={"script","function",""};
153 template <class T=YACS::ENGINE::InlineNode*>
154 struct inlinetypeParser:public nodetypeParser<T>
156 static inlinetypeParser<T> inlineParser;
158 virtual void onStart(const XML_Char* el, const XML_Char** attr);
159 virtual void onEnd(const char *el,parser* child)
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());
170 virtual void buildAttr(const XML_Char** attr)
172 this->required("name",attr);
173 for (int i = 0; attr[i]; i += 2)
175 if(std::string(attr[i]) == "name")this->name(attr[i+1]);
176 if(std::string(attr[i]) == "state")this->state(attr[i+1]);
186 virtual void kind (const std::string& name)
188 DEBTRACE( "inline_kind " << name )
192 virtual void script (const myfunc& f){}
193 virtual void function (const myfunc& f) {}
195 virtual void inport (const myinport& p)
197 DEBTRACE( "inline_inport: " << p._name <<":"<<p._type)
199 throw YACS::Exception("Node must be completely defined before defining its ports");
201 if(currentProc->typeMap.count(p._type)==0)
203 //Check if the typecode is defined in the runtime
204 YACS::ENGINE::TypeCode* t=theRuntime->getTypeCode(p._type);
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);
213 currentProc->typeMap[p._type]=t;
217 this->_node->edAddInputPort(p._name,currentProc->typeMap[p._type]);
219 virtual void outport (const myoutport& p)
221 DEBTRACE( "inline_outport: " << p._name <<":"<<p._type)
223 throw YACS::Exception("Node must be completely defined before defining its ports");
225 if(currentProc->typeMap.count(p._type)==0)
227 YACS::ENGINE::TypeCode* t=theRuntime->getTypeCode(p._type);
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);
236 currentProc->typeMap[p._type]=t;
240 this->_node->edAddOutputPort(p._name,currentProc->typeMap[p._type]);
244 DEBTRACE( "inline_post " << this->_node->getName() )
245 if(this->_state == "disabled")this->_node->exDisabledState();
247 std::list<OutputPort *>::iterator iter;
248 std::list<OutputPort *> s=_node->getSetOfOutputPort();
249 for(iter=s.begin();iter!=s.end();iter++)
251 std::cerr << "port name: " << (*iter)->getName() << std::endl;
252 std::cerr << "port kind: " << (*iter)->edGetType()->kind() << std::endl;
260 template <class T> inlinetypeParser<T> inlinetypeParser<T>::inlineParser;
263 void inlinetypeParser<YACS::ENGINE::InlineNode*>::script (const myfunc& f)
265 DEBTRACE( "inline_script: " << f._code )
266 _node=theRuntime->createScriptNode(_kind,_name);
267 _node->setScript(f._code);
271 void inlinetypeParser<YACS::ENGINE::InlineNode*>::function (const myfunc& f)
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);
281 /*! \brief Class for parsing ServiceInlineNode description
285 template <class T=YACS::ENGINE::ServiceInlineNode*>
286 struct sinlinetypeParser:public inlinetypeParser<T>
288 static sinlinetypeParser<T> sinlineParser;
290 virtual void onStart(const XML_Char* el, const XML_Char** attr);
291 virtual void onEnd(const char *el,parser* child)
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());
302 //virtual void service (const myfunc& f) {}
303 virtual void load (const loadon& l)
305 DEBTRACE( "sinline_load: " )
306 this->_container=l._container;
310 DEBTRACE( "sinline_post " << this->_node->getName() );
311 if(this->_state == "disabled")this->_node->exDisabledState();
313 if(currentProc->containerMap.count(this->_container) != 0)
315 this->_node->getComponent()->setContainer(currentProc->containerMap[this->_container]);
317 else if(this->_container == "")
319 if(currentProc->containerMap.count("DefaultContainer") != 0)
321 //a default container is defined : use it
322 this->_node->getComponent()->setContainer(currentProc->containerMap["DefaultContainer"]);
327 std::cerr << "WARNING: Unknown container " << this->_container << " ignored" << std::endl;
333 template <class T> sinlinetypeParser<T> sinlinetypeParser<T>::sinlineParser;
337 void inlinetypeParser<YACS::ENGINE::ServiceInlineNode*>::function (const myfunc& f)
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");
349 static std::string t2[]={"ref","node","component",""};
351 template <class T=YACS::ENGINE::ServiceNode*>
352 struct servicetypeParser:public inlinetypeParser<T>
354 static servicetypeParser<T> serviceParser;
356 virtual void onStart(const XML_Char* el, const XML_Char** attr);
357 virtual void onEnd(const char *el,parser* child)
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());
373 virtual void ref (const std::string& name)
375 DEBTRACE( "service_ref: " << name )
376 this->_node=theRuntime->createRefNode(this->_kind,this->_name);
377 this->_node->setRef(name);
379 virtual void component (const std::string& name)
381 DEBTRACE( "service_component: " << name )
382 this->_node=theRuntime->createCompoNode(this->_kind,this->_name);
383 this->_node->setRef(name);
385 virtual void node (const std::string& name)
387 DEBTRACE( "service_node: " << name )
388 std::string fullname = currentProc->names.back()+name;
389 if(currentProc->serviceMap.count(name) != 0)
391 //ServiceNode with absolute name found
392 YACS::ENGINE::ServiceNode* n=currentProc->serviceMap[name];
393 this->_node =n->createNode(this->_name);
395 else if(currentProc->serviceMap.count(fullname) != 0)
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);
404 throw YACS::Exception("Unknown ServiceNode");
408 virtual void method (const std::string& name)
410 DEBTRACE( "service_method: " << name )
412 throw YACS::Exception("ServiceNode must be completely defined before defining its method");
415 this->logError("a service name must be a non empty string");
418 this->_node->setMethod(name);
421 virtual void load (const loadon& l)
423 DEBTRACE( "service_load: " );
424 this->_container=l._container;
427 virtual void instream (const myinport& p)
429 DEBTRACE( "service_instream" )
433 throw YACS::Exception("ServiceNode must be completely defined before defining its ports");
435 if(currentProc->typeMap.count(p._type)==0)
437 YACS::ENGINE::TypeCode* t=theRuntime->getTypeCode(p._type);
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);
446 currentProc->typeMap[p._type]=t;
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);
457 virtual void outstream (const myoutport& p)
459 DEBTRACE( "service_outstream" )
463 throw YACS::Exception("ServiceNode must be completely defined before defining its ports");
465 if(currentProc->typeMap.count(p._type)==0)
467 YACS::ENGINE::TypeCode* t=theRuntime->getTypeCode(p._type);
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);
476 currentProc->typeMap[p._type]=t;
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);
489 DEBTRACE( "service_post " << this->_node->getName() )
490 this->mincount("method",1);
491 if(this->_state == "disabled")this->_node->exDisabledState();
493 if(currentProc->containerMap.count(this->_container) != 0)
495 this->_node->getComponent()->setContainer(currentProc->containerMap[this->_container]);
497 else if(this->_container == "")
499 if(currentProc->containerMap.count("DefaultContainer") != 0)
501 //a default container is defined : use it
502 this->_node->getComponent()->setContainer(currentProc->containerMap["DefaultContainer"]);
507 std::cerr << "WARNING: Unknown container " << this->_container << " ignored" << std::endl;
513 template <class T> servicetypeParser<T> servicetypeParser<T>::serviceParser;
521 void inlinetypeParser<T>::onStart(const XML_Char* el, const XML_Char** attr)
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);
544 void sinlinetypeParser<T>::onStart(const XML_Char* el, const XML_Char** attr)
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);
565 void servicetypeParser<T>::onStart(const XML_Char* el, const XML_Char** attr)
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);