1 // Copyright (C) 2006-2016 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 _BLOCPARSER_HXX_
21 #define _BLOCPARSER_HXX_
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"
36 #include "Runtime.hxx"
37 #include "ForLoop.hxx"
38 #include "ForEachLoop.hxx"
39 #include "OptimizerLoop.hxx"
40 #include "WhileLoop.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"
53 extern YACS::ENGINE::Proc* currentProc;
54 extern YACS::ENGINE::Runtime* theRuntime;
59 template <class T=YACS::ENGINE::Bloc*>
60 struct bloctypeParser:parser
62 static bloctypeParser<T> blocParser;
64 bloctypeParser():parser()
66 _orders["property"]=0;
73 _orders["datanode"]=2;
77 _orders["optimizer"]=2;
82 _orders["datalink"]=3;
84 _orders["parameter"]=3;
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)
92 this->required("name",attr);
93 for (int i = 0; attr[i]; i += 2)
95 if(std::string(attr[i]) == "name")name(attr[i+1]);
96 if(std::string(attr[i]) == "state")state(attr[i+1]);
99 void name (const std::string& name)
102 virtual void state (const std::string& name){
103 DEBTRACE( "bloc_state: " << name )
105 if(_state == "disabled")
107 DEBTRACE( "Bloc disabled: " << _bloc->getName())
108 _bloc->exDisabledState();
111 virtual void property (const myprop& prop)
113 DEBTRACE( "property_set: " << prop._name << prop._value )
114 _bloc->setProperty(prop._name,prop._value);
116 virtual void inline_ (YACS::ENGINE::InlineNode* const& n)
118 DEBTRACE( "bloc_pynode_set: " << n->getName() )
119 _bloc->edAddChild(n);
120 std::string fullname = currentProc->names.back()+n->getName();
121 currentProc->nodeMap[fullname]=n;
122 currentProc->inlineMap[fullname]=n;
124 virtual void sinline (YACS::ENGINE::ServiceInlineNode* const& n)
126 DEBTRACE( "bloc_sinline: " << n->getName() )
127 _bloc->edAddChild(n);
128 std::string fullname = currentProc->names.back()+n->getName();
129 currentProc->nodeMap[fullname]=n;
130 currentProc->serviceMap[fullname]=n;
132 virtual void service (YACS::ENGINE::ServiceNode* const& n)
134 DEBTRACE( "bloc_service_set: " << n->getName() )
135 _bloc->edAddChild(n);
136 std::string fullname = currentProc->names.back()+n->getName();
137 currentProc->nodeMap[fullname]=n;
138 currentProc->serviceMap[fullname]=n;
140 virtual void remote (YACS::ENGINE::InlineNode* const& n)
142 DEBTRACE( "bloc_remote_set: " << n->getName() )
143 _bloc->edAddChild(n);
144 std::string fullname = currentProc->names.back()+n->getName();
145 currentProc->nodeMap[fullname]=n;
146 currentProc->inlineMap[fullname]=n;
148 virtual void server (YACS::ENGINE::ServerNode* const& n)
150 DEBTRACE( "bloc_server_set: " << n->getName() )
151 _bloc->edAddChild(n);
152 std::string fullname = currentProc->names.back()+n->getName();
153 currentProc->nodeMap[fullname]=n;
154 currentProc->inlineMap[fullname]=n;
156 virtual void node (YACS::ENGINE::InlineNode* const& n)
158 DEBTRACE( "bloc_node_set: " << n->getName() )
159 _bloc->edAddChild(n);
160 std::string fullname = currentProc->names.back()+n->getName();
161 DEBTRACE( "bloc_node_set fullname = " << fullname )
162 currentProc->nodeMap[fullname]=n;
163 currentProc->inlineMap[fullname]=n;
165 virtual void forloop (YACS::ENGINE::ForLoop* const& b)
167 DEBTRACE( "bloc_forloop_set: " << b->getName() )
168 _bloc->edAddChild(b);
169 std::string fullname = currentProc->names.back()+b->getName();
170 currentProc->nodeMap[fullname]=b;
172 virtual void optimizer (YACS::ENGINE::OptimizerLoop* const& b)
174 DEBTRACE( "bloc_optimizer_set: " << b->getName() );
175 _bloc->edAddChild(b);
176 std::string fullname = currentProc->names.back()+b->getName();
177 currentProc->nodeMap[fullname]=b;
178 //fullname += ".splitter";
179 //currentProc->nodeMap[fullname]=b->getChildByShortName("splitter");
181 virtual void foreach (YACS::ENGINE::ForEachLoop* const& b)
183 DEBTRACE( "bloc_foreach_set: " << b->getName() )
184 _bloc->edAddChild(b);
185 std::string fullname = currentProc->names.back()+b->getName();
186 currentProc->nodeMap[fullname]=b;
187 fullname += ".splitter";
188 currentProc->nodeMap[fullname]=b->getChildByShortName("splitter");
190 virtual void while_ (YACS::ENGINE::WhileLoop* const& b)
192 DEBTRACE( "bloc_while_set: " << b->getName() )
193 _bloc->edAddChild(b);
194 std::string fullname = currentProc->names.back()+b->getName();
195 currentProc->nodeMap[fullname]=b;
197 virtual void switch_ (YACS::ENGINE::Switch* const& b)
199 DEBTRACE( "bloc_switch_set: " << b->getName() )
200 _bloc->edAddChild(b);
201 std::string fullname = currentProc->names.back()+b->getName();
202 currentProc->nodeMap[fullname]=b;
204 virtual void bloc (YACS::ENGINE::Bloc* const& b)
206 DEBTRACE( "bloc_bloc_set: " << b->getName() )
207 _bloc->edAddChild(b);
208 std::string fullname=currentProc->names.back()+ b->getName();
209 currentProc->nodeMap[fullname]=b;
211 virtual void control (const mycontrol& l)
213 DEBTRACE( "bloc_control_set: " << l.fromnode() << " "<< l.tonode() )
216 if(currentProc->nodeMap.count(currentProc->names.back()+l.fromnode()) == 0)
218 msg="from node " + l.fromnode() + " does not exist in control link: ";
219 msg=msg+l.fromnode()+"->"+l.tonode();
220 msg=msg+ " context: "+currentProc->names.back();
224 if(currentProc->nodeMap.count(currentProc->names.back()+l.tonode()) == 0)
226 msg="to node " + l.tonode() + " does not exist in control link: ";
227 msg=msg+l.fromnode()+"->"+l.tonode();
228 msg=msg+ " context: "+currentProc->names.back();
232 // We only link local nodes
235 _bloc->edAddCFLink(currentProc->nodeMap[currentProc->names.back()+l.fromnode()],
236 currentProc->nodeMap[currentProc->names.back()+l.tonode()]);
238 catch(YACS::Exception& e)
240 this->logError(e.what());
243 virtual void datalink (const mylink& l)
245 DEBTRACE( "bloc_datalink_set: "<<l.fromnode()<<"("<<l.fromport()<<")->"<<l.tonode()<<"("<<l.toport()<<")")
248 //Try only relative name for from node
249 std::string fromname = currentProc->names.back()+l.fromnode();
250 if(currentProc->nodeMap.count(fromname) == 0)
252 msg="from node " + l.fromnode() + " does not exist in data link: ";
253 msg=msg+l.fromnode()+"("+l.fromport()+")->"+l.tonode()+"("+l.toport()+")";
257 //Try relative name for to node and then absolute one
258 std::string toname = currentProc->names.back()+l.tonode();
259 if(currentProc->nodeMap.count(toname) == 0)
261 //It's not a relative name. Try an absolute one (I think it's not possible)
263 if(currentProc->nodeMap.count(toname) == 0)
265 // The TO node does not exist -> error
266 msg="to node " + l.tonode() + " does not exist in data link: ";
267 msg=msg+l.fromnode()+"("+l.fromport()+")->"+l.tonode()+"("+l.toport()+")";
272 // We only link local node and other nodes (relative or absolute name in this order)
273 DEBTRACE(fromname <<":"<<l.fromport()<<toname<<":"<<l.toport());
277 _bloc->edAddDFLink(currentProc->nodeMap[fromname]->getOutputPort(l.fromport()),
278 currentProc->nodeMap[toname]->getInputPort(l.toport()));
280 _bloc->edAddLink(currentProc->nodeMap[fromname]->getOutputPort(l.fromport()),
281 currentProc->nodeMap[toname]->getInputPort(l.toport()));
283 catch(YACS::Exception& e)
285 this->logError(e.what());
288 virtual void stream (const mystream& l)
290 DEBTRACE( "bloc_stream_set: " << l.fromnode() << l.fromport() << l.tonode() << l.toport() )
292 std::string fromname = currentProc->names.back()+l.fromnode();
293 std::string toname = currentProc->names.back()+l.tonode();
294 //only relative names
295 if(currentProc->nodeMap.count(fromname) == 0)
297 msg="from node " + l.fromnode() + " does not exist in stream link: ";
298 msg=msg+l.fromnode()+"("+l.fromport()+")->"+l.tonode()+"("+l.toport()+")";
302 if(currentProc->nodeMap.count(toname) == 0)
304 msg="to node " + l.tonode() + " does not exist in stream link: ";
305 msg=msg+l.fromnode()+"("+l.fromport()+")->"+l.tonode()+"("+l.toport()+")";
309 YACS::ENGINE::OutputDataStreamPort* pout=currentProc->nodeMap[fromname]->getOutputDataStreamPort(l.fromport());
310 YACS::ENGINE::InputDataStreamPort* pin=currentProc->nodeMap[toname]->getInputDataStreamPort(l.toport());
311 _bloc->edAddLink(pout,pin);
312 // Set all properties for this link
313 std::map<std::string, std::string>::const_iterator pt;
314 for(pt=l._props.begin();pt!=l._props.end();pt++)
316 pin->setProperty((*pt).first,(*pt).second);
317 pout->setProperty((*pt).first,(*pt).second);
320 virtual void parameter (const myparam& param)
322 DEBTRACE( "++++++++++++++++++++Parameter+++++++++++++++++++++" );
324 std::string toname = currentProc->names.back()+param._tonode;
325 if(currentProc->nodeMap.count(toname) == 0)
327 msg="to node " + param._tonode + " does not exist in parameter: ";
328 msg=msg+"->"+param._tonode+"("+param._toport+")";
332 YACS::ENGINE::InputPort* inport=currentProc->nodeMap[toname]->getInputPort(param._toport);
333 //We don't know the parameter type. So we try to initialize the port
334 //with the value. If it's not the right type, edInit throws an exception
337 inport->edInit("XML",param._value.c_str());
339 catch(YACS::Exception& e)
341 this->logError(e.what());
343 DEBTRACE( "++++++++++++++++++++End parameter+++++++++++++++++++++" );
346 virtual void preset (ENGINE::DataNode* const& n);
347 virtual void outnode (ENGINE::DataNode* const& n);
351 DEBTRACE( "bloc_post" )
352 currentProc->names.pop_back();
364 std::vector<YACS::ENGINE::Bloc *> _blocs;
367 template <class T> bloctypeParser<T> bloctypeParser<T>::blocParser;
370 void bloctypeParser<T>::preset (ENGINE::DataNode* const& n)
372 DEBTRACE("bloc_preset_set: " << n->getName() );
373 _bloc->edAddChild(n);
374 std::string fullname = currentProc->names.back()+n->getName();
375 currentProc->nodeMap[fullname]=n;
379 void bloctypeParser<T>::outnode (ENGINE::DataNode* const& n)
381 DEBTRACE("bloc_outnode: " << n->getName() );
382 _bloc->edAddChild(n);
383 std::string fullname = currentProc->names.back()+n->getName();
384 currentProc->nodeMap[fullname]=n;
388 void bloctypeParser<YACS::ENGINE::Bloc*>::name (const std::string& name)
390 DEBTRACE( "bloc_name: " << name )
391 std::string fullname=currentProc->names.back()+name;
392 _bloc=theRuntime->createBloc(name);
393 _blocs.push_back(_bloc);
394 currentProc->names.push_back(fullname+'.');
399 #include "loopParsers.hxx"
400 #include "switchParsers.hxx"
401 #include "presetParsers.hxx"
402 #include "outputParsers.hxx"
408 void bloctypeParser<T>::onStart(const XML_Char* el, const XML_Char** attr)
410 DEBTRACE( "bloctypeParser::onStart: " << el )
411 std::string element(el);
413 parser* pp=&parser::main_parser;
414 if(element == "property")pp=&propertytypeParser::propertyParser;
416 else if(element == "inline")pp=&inlinetypeParser<>::inlineParser;
417 else if(element == "sinline")pp=&sinlinetypeParser<>::sinlineParser;
418 else if(element == "service")pp=&servicetypeParser<>::serviceParser;
419 else if(element == "server")pp=&servertypeParser<>::serverParser;
420 else if(element == "remote")pp=&remotetypeParser<>::remoteParser;
421 else if(element == "node")pp=&nodetypeParser<>::nodeParser;
422 else if(element == "datanode")pp=&presettypeParser<>::presetParser;
423 else if(element == "outnode")pp=&outnodetypeParser<>::outnodeParser;
425 else if(element == "bloc")pp=&bloctypeParser<>::blocParser;
426 else if(element == "forloop")pp=&forlooptypeParser<>::forloopParser;
427 else if(element == "foreach")pp=&foreachlooptypeParser<>::foreachloopParser;
428 else if(element == "optimizer")pp=&optimizerlooptypeParser<>::optimizerloopParser;
429 else if(element == "while")pp=&whilelooptypeParser<>::whileloopParser;
430 else if(element == "switch")pp=&switchtypeParser::switchParser;
432 else if(element == "control")pp=&controltypeParser<>::controlParser;
433 else if(element == "datalink")pp=&linktypeParser<>::linkParser;
434 else if(element == "stream")pp=&streamtypeParser<>::streamParser;
435 else if(element == "parameter")pp=¶metertypeParser::parameterParser;
437 this->SetUserDataAndPush(pp);
444 void bloctypeParser<T>::onEnd(const char *el,parser* child)
446 DEBTRACE( "bloctypeParser::onEnd: " << el )
447 std::string element(el);
448 if(element == "property")property(((propertytypeParser*)child)->post());
449 else if(element == "inline")inline_(((inlinetypeParser<>*)child)->post());
450 else if(element == "sinline")sinline(((sinlinetypeParser<>*)child)->post());
451 else if(element == "service")service(((servicetypeParser<>*)child)->post());
452 else if(element == "server")server(((servertypeParser<>*)child)->post());
453 else if(element == "remote")remote(((remotetypeParser<>*)child)->post());
454 else if(element == "node")node(((nodetypeParser<>*)child)->post());
455 else if(element == "datanode")preset(((presettypeParser<>*)child)->post());
456 else if(element == "outnode")outnode(((outnodetypeParser<>*)child)->post());
458 else if(element == "bloc")bloc(((bloctypeParser<>*)child)->post());
459 else if(element == "forloop")forloop(((forlooptypeParser<>*)child)->post());
460 else if(element == "optimizer")optimizer(((optimizerlooptypeParser<>*)child)->post());
461 else if(element == "foreach")foreach(((foreachlooptypeParser<>*)child)->post());
462 else if(element == "while")while_(((whilelooptypeParser<>*)child)->post());
463 else if(element == "switch")switch_(((switchtypeParser*)child)->post());
465 else if(element == "control") control(((controltypeParser<>*)child)->post());
466 else if(element == "datalink") datalink(((linktypeParser<>*)child)->post());
467 else if(element == "stream") stream(((streamtypeParser<>*)child)->post());
468 else if(element == "parameter") parameter(((parametertypeParser*)child)->post());