1 // Copyright (C) 2006-2014 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, or (at your option) any later version.
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
20 #ifndef _LOOPPARSER_HXX_
21 #define _LOOPPARSER_HXX_
23 #include "parserBase.hxx"
28 #include "Exception.hxx"
29 #include "InlineNode.hxx"
30 #include "ServiceNode.hxx"
31 #include "ServiceInlineNode.hxx"
32 #include "OutputDataStreamPort.hxx"
33 #include "InputDataStreamPort.hxx"
34 #include "ForLoop.hxx"
35 #include "WhileLoop.hxx"
37 #include "ForEachLoop.hxx"
38 #include "OptimizerLoop.hxx"
47 template <class T=ENGINE::Loop*>
48 struct looptypeParser:parser
50 virtual void onStart(const XML_Char* el, const XML_Char** attr);
51 virtual void onEnd(const char *el,parser* child);
52 virtual void buildAttr(const XML_Char** attr);
54 virtual void name (const std::string& name);
55 virtual void state (const std::string& name);
56 virtual void property (const myprop& prop);
57 virtual void inline_ (ENGINE::InlineNode* const& n);
58 virtual void sinline (ENGINE::ServiceInlineNode* const& n);
59 virtual void service (ENGINE::ServiceNode* const& n);
60 virtual void server (ENGINE::ServerNode* const& n);
61 virtual void remote (ENGINE::InlineNode* const& n);
62 virtual void node (ENGINE::InlineNode* const& n);
63 virtual void forloop (ENGINE::ForLoop* const& b);
64 virtual void foreach (ENGINE::ForEachLoop* const& b);
65 virtual void optimizer (ENGINE::OptimizerLoop* const& b);
66 virtual void while_ (ENGINE::WhileLoop* const& b);
67 virtual void switch_ (ENGINE::Switch* const& b);
68 virtual void bloc (ENGINE::Bloc* const& b);
69 virtual void datalink (const mylink& l);
72 std::vector<T> _cnodes;
79 // A loop can contain forloop, whileloop or foreachloop
80 // We must respect the order : definition of loop, then while, for, .. and then onStart, onEnd for loop !!!
82 static std::string t3[]={"inline","sinline","service","server", "remote", "node","forloop","foreach","optimizer","while","switch","bloc",""};
85 void looptypeParser<T>::buildAttr(const XML_Char** attr)
87 this->required("name",attr);
88 for (int i = 0; attr[i]; i += 2)
90 if(std::string(attr[i]) == "name")name(attr[i+1]);
91 if(std::string(attr[i]) == "state")state(attr[i+1]);
95 void looptypeParser<T>::pre ()
101 void looptypeParser<T>::name (const std::string& name)
103 DEBTRACE( "bloc_name: " << name );
106 void looptypeParser<T>::state (const std::string& name)
108 DEBTRACE( "bloc_state: " << name );
112 void looptypeParser<T>::property (const myprop& prop)
114 DEBTRACE( "property_set" << prop._name << prop._value );
118 void looptypeParser<T>::inline_ (ENGINE::InlineNode* const& n)
120 DEBTRACE( "loop_inline" << n->getName() );
121 _cnode->edSetNode(n);
122 std::string fullname=currentProc->names.back()+ n->getName();
123 currentProc->nodeMap[fullname]=n;
124 currentProc->inlineMap[fullname]=n;
127 void looptypeParser<T>::sinline (ENGINE::ServiceInlineNode* const& n)
129 DEBTRACE( "loop_sinline" << n->getName() )
130 _cnode->edSetNode(n);
131 std::string fullname=currentProc->names.back()+ n->getName();
132 currentProc->nodeMap[fullname]=n;
133 currentProc->serviceMap[fullname]=n;
136 void looptypeParser<T>::service (ENGINE::ServiceNode* const& n)
138 DEBTRACE( "loop_service" << n->getName() )
139 _cnode->edSetNode(n);
140 std::string fullname=currentProc->names.back()+ n->getName();
141 currentProc->nodeMap[fullname]=n;
142 currentProc->serviceMap[fullname]=n;
145 void looptypeParser<T>::server (ENGINE::ServerNode* const& n)
147 DEBTRACE( "loop_server" << n->getName() )
148 _cnode->edSetNode(n);
149 std::string fullname=currentProc->names.back()+ n->getName();
150 currentProc->nodeMap[fullname]=n;
151 currentProc->inlineMap[fullname]=n;
155 void looptypeParser<T>::remote (YACS::ENGINE::InlineNode* const& n)
157 DEBTRACE( "loop_remote: " << n->getName() )
158 _cnode->edSetNode(n);
159 std::string fullname = currentProc->names.back()+n->getName();
160 currentProc->nodeMap[fullname]=n;
161 currentProc->inlineMap[fullname]=n;
165 void looptypeParser<T>::node (ENGINE::InlineNode* const& n)
167 DEBTRACE( "loop_node" << n->getName() )
168 _cnode->edSetNode(n);
169 std::string fullname=currentProc->names.back()+ n->getName();
170 currentProc->nodeMap[fullname]=n;
171 currentProc->inlineMap[fullname]=n;
174 void looptypeParser<T>::forloop (ENGINE::ForLoop* const& b)
176 DEBTRACE( "loop_forloop" << b->getName() )
177 _cnode->edSetNode(b);
178 std::string fullname=currentProc->names.back()+ b->getName();
179 currentProc->nodeMap[fullname]=b;
182 void looptypeParser<T>::foreach (ENGINE::ForEachLoop* const& b)
184 DEBTRACE("loop_foreach" << b->getName())
185 _cnode->edSetNode(b);
186 std::string fullname=currentProc->names.back()+ b->getName();
187 currentProc->nodeMap[fullname]=b;
188 fullname += ".splitter";
189 currentProc->nodeMap[fullname]=b->getChildByShortName("splitter");
192 void looptypeParser<T>::optimizer (ENGINE::OptimizerLoop* const& b)
194 DEBTRACE( "loop_optimizer: " << b->getName() );
195 _cnode->edSetNode(b);
196 std::string fullname = currentProc->names.back()+b->getName();
197 currentProc->nodeMap[fullname]=b;
198 //fullname += ".splitter";
199 //currentProc->nodeMap[fullname]=b->getChildByShortName("splitter");
202 void looptypeParser<T>::while_ (ENGINE::WhileLoop* const& b)
204 DEBTRACE( "loop_while: " << b->getName() )
205 _cnode->edSetNode(b);
206 std::string fullname=currentProc->names.back()+ b->getName();
207 currentProc->nodeMap[fullname]=b;
210 void looptypeParser<T>::switch_ (ENGINE::Switch* const& b)
212 DEBTRACE( "loop_switch: " << b->getName() )
213 _cnode->edSetNode(b);
214 std::string fullname=currentProc->names.back()+ b->getName();
215 currentProc->nodeMap[fullname]=b;
218 void looptypeParser<T>::bloc (ENGINE::Bloc* const& b)
220 DEBTRACE( "loop_bloc " << b->getName() )
221 _cnode->edSetNode(b);
222 std::string fullname=currentProc->names.back()+ b->getName();
223 currentProc->nodeMap[fullname]=b;
227 void looptypeParser<T>::datalink (const mylink& l)
229 DEBTRACE( "loop_datalink: " << l.fromnode() << l.fromport() << l.tonode() << l.toport())
232 //Try only relative name for from node
233 std::string fromname = currentProc->names.back()+l.fromnode();
234 if(currentProc->nodeMap.count(fromname) == 0)
236 msg="from node " + l.fromnode() + " does not exist in data link: ";
237 msg=msg+l.fromnode()+"("+l.fromport()+")->"+l.tonode()+"("+l.toport()+")";
241 //Try relative name for to node and then absolute one
242 std::string toname = currentProc->names.back()+l.tonode();
243 if(currentProc->nodeMap.count(toname) == 0)
245 //It's not a relative name. Try an absolute one (I think it's not possible)
247 if(currentProc->nodeMap.count(toname) == 0)
249 // The TO node does not exist -> error
250 msg="to node " + l.tonode() + " does not exist in data link: ";
251 msg=msg+l.fromnode()+"("+l.fromport()+")->"+l.tonode()+"("+l.toport()+")";
256 // We only link local node and other nodes (relative or absolute name in this order)
257 DEBTRACE(fromname <<":"<<l.fromport()<<toname<<":"<<l.toport());
261 _cnode->edAddDFLink(currentProc->nodeMap[fromname]->getOutputPort(l.fromport()),
262 currentProc->nodeMap[toname]->getInputPort(l.toport()));
264 _cnode->edAddLink(currentProc->nodeMap[fromname]->getOutputPort(l.fromport()),
265 currentProc->nodeMap[toname]->getInputPort(l.toport()));
269 this->logError(e.what());
279 // While loop specialization
281 template <class T=ENGINE::WhileLoop*>
282 struct whilelooptypeParser:looptypeParser<T>
284 static whilelooptypeParser<T> whileloopParser;
285 virtual void name (const std::string& name);
289 template <class T> whilelooptypeParser<T> whilelooptypeParser<T>::whileloopParser;
292 void whilelooptypeParser<T>::name (const std::string& name)
294 DEBTRACE( "while_name: " << name )
295 std::string fullname=currentProc->names.back()+name;
296 this->_cnode=theRuntime->createWhileLoop(name);
297 currentProc->nodeMap[fullname]=this->_cnode;
298 this->_cnodes.push_back(this->_cnode);
299 currentProc->names.push_back(fullname+'.');
303 T whilelooptypeParser<T>::post()
305 DEBTRACE( "while_post" << this->_cnode->getName() )
306 ENGINE::InputPort *cond=this->_cnode->edGetConditionPort();
309 this->_cnodes.pop_back();
310 currentProc->names.pop_back();
311 this->_cnode=this->_cnodes.back();
321 // For loop specialization
323 template <class T=ENGINE::ForLoop*>
324 struct forlooptypeParser:looptypeParser<T>
326 static forlooptypeParser<T> forloopParser;
327 virtual void buildAttr(const XML_Char** attr);
328 virtual void name (const std::string& name);
329 virtual void nsteps (const int& n);
334 template <class T> forlooptypeParser<T> forlooptypeParser<T>::forloopParser;
337 void forlooptypeParser<T>::buildAttr(const XML_Char** attr)
339 this->required("name",attr);
340 for (int i = 0; attr[i]; i += 2)
342 if(std::string(attr[i]) == "name")name(attr[i+1]);
343 if(std::string(attr[i]) == "state")this->state(attr[i+1]);
344 if(std::string(attr[i]) == "nsteps")nsteps(atoi(attr[i+1]));
349 void forlooptypeParser<T>::name (const std::string& name)
351 DEBTRACE( "forloop_name: " << name );
352 std::string fullname=currentProc->names.back()+name;
353 this->_cnode=theRuntime->createForLoop(name);
354 currentProc->nodeMap[fullname]=this->_cnode;
355 this->_cnodes.push_back(this->_cnode);
356 currentProc->names.push_back(fullname+'.');
361 void forlooptypeParser<T>::nsteps (const int& n)
363 DEBTRACE( "forloop_nsteps: " << n )
365 throw Exception("Node name must be defined before nsteps");
366 ENGINE::InputPort *iNbTimes=this->_cnode->edGetNbOfTimesInputPort();
371 T forlooptypeParser<T>::post()
373 DEBTRACE( "forloop_post" )
375 this->_cnodes.pop_back();
376 currentProc->names.pop_back();
377 this->_cnode=this->_cnodes.back();
385 // pseudo composed node, used to store the init or finalize node of a DynParaLoop
386 class PseudoComposedNode
389 void edSetNode(ENGINE::Node * node) { _node = node; }
390 ENGINE::Node * getNode() { return _node; }
392 // Those two methods should never be called
393 bool edAddLink(ENGINE::OutPort *start, ENGINE::InPort *end) throw(Exception) { YASSERT(false); }
394 bool edAddDFLink(ENGINE::OutPort *start, ENGINE::InPort *end) throw(Exception) { YASSERT(false); }
397 ENGINE::Node * _node;
400 // pseudo composed node parser specialization for DynParaLoop init and finalize nodes
401 template <class T=PseudoComposedNode*>
402 struct pseudocomposednodetypeParser:looptypeParser<T>
404 static pseudocomposednodetypeParser<T> pseudocomposednodeParser;
406 virtual void buildAttr(const XML_Char** attr)
408 this->_cnode = new PseudoComposedNode();
409 this->_cnodes.push_back(this->_cnode);
414 DEBTRACE("pseudocomposednode_post" << this->_cnode->getNode()->getName())
416 this->_cnodes.pop_back();
417 if(this->_cnodes.size() == 0)
420 this->_cnode = this->_cnodes.back();
424 virtual void datalink(const mylink & l)
426 throw YACS::Exception("Unexpected datalink element in DynParaLoop init or finalize node");
431 template <class T> pseudocomposednodetypeParser<T> pseudocomposednodetypeParser<T>::pseudocomposednodeParser;
437 // dynparaloop specialization
439 template <class T=ENGINE::DynParaLoop*>
440 struct dynparalooptypeParser:looptypeParser<T>
442 virtual void onStart(const XML_Char* el, const XML_Char** attr)
444 DEBTRACE( "dynparalooptypeParser::onStart: " << el )
445 std::string element(el);
446 this->maxcount("initnode",1,element);
447 this->maxcount("finalizenode",1,element);
448 if (element == "initnode" || element == "finalizenode")
450 parser* pp = &pseudocomposednodetypeParser<>::pseudocomposednodeParser;
451 this->SetUserDataAndPush(pp);
458 this->looptypeParser<T>::onStart(el, attr);
462 virtual void onEnd(const char *el, parser* child)
464 DEBTRACE( "dynparalooptypeParser::onEnd: " << el )
465 std::string element(el);
466 if (element == "initnode") initnode(((pseudocomposednodetypeParser<>*)child)->post());
467 else if (element == "finalizenode") finalizenode(((pseudocomposednodetypeParser<>*)child)->post());
468 else this->looptypeParser<T>::onEnd(el, child);
471 virtual void initnode(PseudoComposedNode * const& n)
473 DEBTRACE( "dynparaloop_initnode: " << n->getNode()->getName() )
474 this->_cnode->edSetInitNode(n->getNode());
478 virtual void finalizenode(PseudoComposedNode * const& n)
480 DEBTRACE( "dynparaloop_finalizenode: " << n->getNode()->getName() )
481 this->_cnode->edSetFinalizeNode(n->getNode());
490 // Foreach loop specialization
492 template <class T=ENGINE::ForEachLoop*>
493 struct foreachlooptypeParser:dynparalooptypeParser<T>
495 static foreachlooptypeParser<T> foreachloopParser;
497 virtual void buildAttr(const XML_Char** attr)
499 this->required("name",attr);
500 this->required("type",attr);
501 for (int i = 0; attr[i]; i += 2)
503 if(std::string(attr[i]) == "name")name(attr[i+1]);
504 if(std::string(attr[i]) == "state")this->state(attr[i+1]);
505 if(std::string(attr[i]) == "nbranch")nbranch(atoi(attr[i+1]));
506 if(std::string(attr[i]) == "type")datatype(attr[i+1]);
513 this->looptypeParser<T>::pre();
515 virtual void name (const std::string& name)
517 DEBTRACE("foreach_name: " << name)
519 _fullname=currentProc->names.back()+name;
521 virtual void nbranch (const int& n)
523 DEBTRACE("foreach_nbranch: " << n )
526 virtual void datatype (const std::string& type)
528 DEBTRACE("foreach_datatype: "<< type)
531 virtual void postAttr()
533 if(currentProc->typeMap.count(_datatype)==0)
535 //Check if the typecode is defined in the runtime
536 YACS::ENGINE::TypeCode* t=theRuntime->getTypeCode(_datatype);
539 std::stringstream msg;
540 msg << "Type "<< _datatype <<" does not exist"<<" ("<<__FILE__<<":"<<__LINE__<< ")";
541 throw Exception(msg.str());
545 currentProc->typeMap[_datatype]=t;
549 this->_cnode=theRuntime->createForEachLoop(_name,currentProc->typeMap[_datatype]);
550 //set number of branches
551 if(_nbranch > 0)this->_cnode->edGetNbOfBranchesPort()->edInit(_nbranch);
552 this->_cnodes.push_back(this->_cnode);
553 currentProc->names.push_back(_fullname + '.');
557 DEBTRACE("foreach_post" << this->_cnode->getName())
559 this->_cnodes.pop_back();
560 currentProc->names.pop_back();
561 if(this->_cnodes.size() == 0)
564 this->_cnode=this->_cnodes.back();
568 std::string _fullname;
570 std::string _datatype;
573 template <class T> foreachlooptypeParser<T> foreachlooptypeParser<T>::foreachloopParser;
579 // optimizer loop specialization
581 template <class T=ENGINE::OptimizerLoop*>
582 struct optimizerlooptypeParser:dynparalooptypeParser<T>
584 static optimizerlooptypeParser<T> optimizerloopParser;
586 virtual void buildAttr(const XML_Char** attr)
588 this->required("name",attr);
589 this->required("lib",attr);
590 this->required("entry",attr);
591 for (int i = 0; attr[i]; i += 2)
593 if(std::string(attr[i]) == "name")name(attr[i+1]);
594 if(std::string(attr[i]) == "state")this->state(attr[i+1]);
595 if(std::string(attr[i]) == "nbranch")nbranch(atoi(attr[i+1]));
596 if(std::string(attr[i]) == "lib")lib(attr[i+1]);
597 if(std::string(attr[i]) == "entry")entry(attr[i+1]);
598 if(std::string(attr[i]) == "kind")kind(attr[i+1]);
605 this->looptypeParser<T>::pre();
607 virtual void name (const std::string& name)
609 DEBTRACE("optimizer_name: " << name)
611 _fullname=currentProc->names.back()+name;
613 virtual void lib (const std::string& name)
617 virtual void entry (const std::string& name)
621 virtual void nbranch (const int& n)
623 DEBTRACE("optimizer_nbranch: " << n )
626 virtual void kind (const std::string& name)
630 virtual void postAttr()
632 this->_cnode=theRuntime->createOptimizerLoop(_name,_lib,_entry,true,_kind, currentProc);
633 //set number of branches
634 if(_nbranch > 0)this->_cnode->edGetNbOfBranchesPort()->edInit(_nbranch);
635 this->_cnodes.push_back(this->_cnode);
636 currentProc->names.push_back(_fullname + '.');
640 DEBTRACE("optimizer_post" << this->_cnode->getName())
642 this->_cnodes.pop_back();
643 currentProc->names.pop_back();
644 if(this->_cnodes.size() == 0)
647 this->_cnode=this->_cnodes.back();
652 std::string _fullname;
660 template <class T> optimizerlooptypeParser<T> optimizerlooptypeParser<T>::optimizerloopParser;
664 #include "blocParsers.hxx"
665 #include "switchParsers.hxx"
671 void looptypeParser<T>::onStart(const XML_Char* el, const XML_Char** attr)
673 DEBTRACE( "looptypeParser::onStart: " << el )
674 std::string element(el);
675 this->maxcount("inline",1,element);
676 this->maxcount("sinline",1,element);
677 this->maxcount("service",1,element);
678 this->maxcount("server",1,element);
679 this->maxcount("remote",1,element);
680 this->maxcount("node",1,element);
681 this->maxcount("forloop",1,element);
682 this->maxcount("foreach",1,element);
683 this->maxcount("optimizer",1,element);
684 this->maxcount("while",1,element);
685 this->maxcount("switch",1,element);
686 this->maxcount("bloc",1,element);
687 this->maxchoice(t3,1,element);
688 parser* pp=&parser::main_parser;
689 if(element == "property")pp=&propertytypeParser::propertyParser;
690 else if(element == "inline")pp=&inlinetypeParser<>::inlineParser;
691 else if(element == "sinline")pp=&sinlinetypeParser<>::sinlineParser;
692 else if(element == "service")pp=&servicetypeParser<>::serviceParser;
693 else if(element == "server")pp=&servertypeParser<>::serverParser;
694 else if(element == "remote")pp=&remotetypeParser<>::remoteParser;
695 else if(element == "node")pp=&nodetypeParser<>::nodeParser;
697 else if(element == "forloop")pp=&forlooptypeParser<>::forloopParser;
698 else if(element == "foreach")pp=&foreachlooptypeParser<>::foreachloopParser;
699 else if(element == "optimizer")pp=&optimizerlooptypeParser<>::optimizerloopParser;
700 else if(element == "while")pp=&whilelooptypeParser<>::whileloopParser;
701 else if(element == "switch")pp=&switchtypeParser::switchParser;
702 else if(element == "bloc")pp=&bloctypeParser<>::blocParser;
703 else if(element == "datalink")pp=&linktypeParser<>::linkParser;
704 this->SetUserDataAndPush(pp);
711 void looptypeParser<T>::onEnd(const char *el,parser* child)
713 DEBTRACE( "looptypeParser::onEnd: " << el )
714 std::string element(el);
715 if(element == "property")property(((propertytypeParser*)child)->post());
716 else if(element == "inline")inline_(((inlinetypeParser<>*)child)->post());
717 else if(element == "sinline")sinline(((sinlinetypeParser<>*)child)->post());
718 else if(element == "service")service(((servicetypeParser<>*)child)->post());
719 else if(element == "server")server(((servertypeParser<>*)child)->post());
720 else if(element == "remote")remote(((remotetypeParser<>*)child)->post());
721 else if(element == "node")node(((nodetypeParser<>*)child)->post());
723 else if(element == "forloop")forloop(((forlooptypeParser<>*)child)->post());
724 else if(element == "foreach")foreach(((foreachlooptypeParser<>*)child)->post());
725 else if(element == "optimizer")optimizer(((optimizerlooptypeParser<>*)child)->post());
726 else if(element == "while")while_(((whilelooptypeParser<>*)child)->post());
727 else if(element == "switch")switch_(((switchtypeParser*)child)->post());
728 else if(element == "bloc")bloc(((bloctypeParser<>*)child)->post());
730 else if(element == "datalink") datalink(((linktypeParser<>*)child)->post());