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 _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)
90 this->required("name",attr);
91 for (int i = 0; attr[i]; i += 2)
93 if(std::string(attr[i]) == "name")name(attr[i+1]);
94 if(std::string(attr[i]) == "state")state(attr[i+1]);
97 void name (const std::string& name)
100 virtual void state (const std::string& name){
101 DEBTRACE( "bloc_state: " << name )
103 if(_state == "disabled")
105 DEBTRACE( "Bloc disabled: " << _bloc->getName())
106 _bloc->exDisabledState();
109 virtual void property (const myprop& prop)
111 DEBTRACE( "property_set: " << prop._name << prop._value )
112 _bloc->setProperty(prop._name,prop._value);
114 virtual void inline_ (YACS::ENGINE::InlineNode* const& n)
116 DEBTRACE( "bloc_pynode_set: " << n->getName() )
117 _bloc->edAddChild(n);
118 std::string fullname = currentProc->names.back()+n->getName();
119 currentProc->nodeMap[fullname]=n;
120 currentProc->inlineMap[fullname]=n;
122 virtual void sinline (YACS::ENGINE::ServiceInlineNode* const& n)
124 DEBTRACE( "bloc_sinline: " << n->getName() )
125 _bloc->edAddChild(n);
126 std::string fullname = currentProc->names.back()+n->getName();
127 currentProc->nodeMap[fullname]=n;
128 currentProc->serviceMap[fullname]=n;
130 virtual void service (YACS::ENGINE::ServiceNode* const& n)
132 DEBTRACE( "bloc_service_set: " << n->getName() )
133 _bloc->edAddChild(n);
134 std::string fullname = currentProc->names.back()+n->getName();
135 currentProc->nodeMap[fullname]=n;
136 currentProc->serviceMap[fullname]=n;
138 virtual void remote (YACS::ENGINE::InlineNode* const& n)
140 DEBTRACE( "bloc_remote_set: " << n->getName() )
141 _bloc->edAddChild(n);
142 std::string fullname = currentProc->names.back()+n->getName();
143 currentProc->nodeMap[fullname]=n;
144 currentProc->inlineMap[fullname]=n;
146 virtual void server (YACS::ENGINE::ServerNode* const& n)
148 DEBTRACE( "bloc_server_set: " << n->getName() )
149 _bloc->edAddChild(n);
150 std::string fullname = currentProc->names.back()+n->getName();
151 currentProc->nodeMap[fullname]=n;
152 currentProc->inlineMap[fullname]=n;
154 virtual void node (YACS::ENGINE::InlineNode* const& n)
156 DEBTRACE( "bloc_node_set: " << n->getName() )
157 _bloc->edAddChild(n);
158 std::string fullname = currentProc->names.back()+n->getName();
159 DEBTRACE( "bloc_node_set fullname = " << fullname )
160 currentProc->nodeMap[fullname]=n;
161 currentProc->inlineMap[fullname]=n;
163 virtual void forloop (YACS::ENGINE::ForLoop* const& b)
165 DEBTRACE( "bloc_forloop_set: " << b->getName() )
166 _bloc->edAddChild(b);
167 std::string fullname = currentProc->names.back()+b->getName();
168 currentProc->nodeMap[fullname]=b;
170 virtual void optimizer (YACS::ENGINE::OptimizerLoop* const& b)
172 DEBTRACE( "bloc_optimizer_set: " << b->getName() );
173 _bloc->edAddChild(b);
174 std::string fullname = currentProc->names.back()+b->getName();
175 currentProc->nodeMap[fullname]=b;
176 //fullname += ".splitter";
177 //currentProc->nodeMap[fullname]=b->getChildByShortName("splitter");
179 virtual void foreach (YACS::ENGINE::ForEachLoop* const& b)
181 DEBTRACE( "bloc_foreach_set: " << b->getName() )
182 _bloc->edAddChild(b);
183 std::string fullname = currentProc->names.back()+b->getName();
184 currentProc->nodeMap[fullname]=b;
185 fullname += ".splitter";
186 currentProc->nodeMap[fullname]=b->getChildByShortName("splitter");
188 virtual void while_ (YACS::ENGINE::WhileLoop* const& b)
190 DEBTRACE( "bloc_while_set: " << b->getName() )
191 _bloc->edAddChild(b);
192 std::string fullname = currentProc->names.back()+b->getName();
193 currentProc->nodeMap[fullname]=b;
195 virtual void switch_ (YACS::ENGINE::Switch* const& b)
197 DEBTRACE( "bloc_switch_set: " << b->getName() )
198 _bloc->edAddChild(b);
199 std::string fullname = currentProc->names.back()+b->getName();
200 currentProc->nodeMap[fullname]=b;
202 virtual void bloc (YACS::ENGINE::Bloc* const& b)
204 DEBTRACE( "bloc_bloc_set: " << b->getName() )
205 _bloc->edAddChild(b);
206 std::string fullname=currentProc->names.back()+ b->getName();
207 currentProc->nodeMap[fullname]=b;
209 virtual void control (const mycontrol& l)
211 DEBTRACE( "bloc_control_set: " << l.fromnode() << " "<< l.tonode() )
214 if(currentProc->nodeMap.count(currentProc->names.back()+l.fromnode()) == 0)
216 msg="from node " + l.fromnode() + " does not exist in control link: ";
217 msg=msg+l.fromnode()+"->"+l.tonode();
218 msg=msg+ " context: "+currentProc->names.back();
222 if(currentProc->nodeMap.count(currentProc->names.back()+l.tonode()) == 0)
224 msg="to node " + l.tonode() + " does not exist in control link: ";
225 msg=msg+l.fromnode()+"->"+l.tonode();
226 msg=msg+ " context: "+currentProc->names.back();
230 // We only link local nodes
233 _bloc->edAddCFLink(currentProc->nodeMap[currentProc->names.back()+l.fromnode()],
234 currentProc->nodeMap[currentProc->names.back()+l.tonode()]);
236 catch(YACS::Exception& e)
238 this->logError(e.what());
241 virtual void datalink (const mylink& l)
243 DEBTRACE( "bloc_datalink_set: "<<l.fromnode()<<"("<<l.fromport()<<")->"<<l.tonode()<<"("<<l.toport()<<")")
246 //Try only relative name for from node
247 std::string fromname = currentProc->names.back()+l.fromnode();
248 if(currentProc->nodeMap.count(fromname) == 0)
250 msg="from node " + l.fromnode() + " does not exist in data link: ";
251 msg=msg+l.fromnode()+"("+l.fromport()+")->"+l.tonode()+"("+l.toport()+")";
255 //Try relative name for to node and then absolute one
256 std::string toname = currentProc->names.back()+l.tonode();
257 if(currentProc->nodeMap.count(toname) == 0)
259 //It's not a relative name. Try an absolute one (I think it's not possible)
261 if(currentProc->nodeMap.count(toname) == 0)
263 // The TO node does not exist -> error
264 msg="to node " + l.tonode() + " does not exist in data link: ";
265 msg=msg+l.fromnode()+"("+l.fromport()+")->"+l.tonode()+"("+l.toport()+")";
270 // We only link local node and other nodes (relative or absolute name in this order)
271 DEBTRACE(fromname <<":"<<l.fromport()<<toname<<":"<<l.toport());
275 _bloc->edAddDFLink(currentProc->nodeMap[fromname]->getOutputPort(l.fromport()),
276 currentProc->nodeMap[toname]->getInputPort(l.toport()));
278 _bloc->edAddLink(currentProc->nodeMap[fromname]->getOutputPort(l.fromport()),
279 currentProc->nodeMap[toname]->getInputPort(l.toport()));
281 catch(YACS::Exception& e)
283 this->logError(e.what());
286 virtual void stream (const mystream& l)
288 DEBTRACE( "bloc_stream_set: " << l.fromnode() << l.fromport() << l.tonode() << l.toport() )
290 std::string fromname = currentProc->names.back()+l.fromnode();
291 std::string toname = currentProc->names.back()+l.tonode();
292 //only relative names
293 if(currentProc->nodeMap.count(fromname) == 0)
295 msg="from node " + l.fromnode() + " does not exist in stream link: ";
296 msg=msg+l.fromnode()+"("+l.fromport()+")->"+l.tonode()+"("+l.toport()+")";
300 if(currentProc->nodeMap.count(toname) == 0)
302 msg="to node " + l.tonode() + " does not exist in stream link: ";
303 msg=msg+l.fromnode()+"("+l.fromport()+")->"+l.tonode()+"("+l.toport()+")";
307 YACS::ENGINE::OutputDataStreamPort* pout=currentProc->nodeMap[fromname]->getOutputDataStreamPort(l.fromport());
308 YACS::ENGINE::InputDataStreamPort* pin=currentProc->nodeMap[toname]->getInputDataStreamPort(l.toport());
309 _bloc->edAddLink(pout,pin);
310 // Set all properties for this link
311 std::map<std::string, std::string>::const_iterator pt;
312 for(pt=l._props.begin();pt!=l._props.end();pt++)
314 pin->setProperty((*pt).first,(*pt).second);
315 pout->setProperty((*pt).first,(*pt).second);
318 virtual void parameter (const myparam& param)
320 DEBTRACE( "++++++++++++++++++++Parameter+++++++++++++++++++++" );
322 std::string toname = currentProc->names.back()+param._tonode;
323 if(currentProc->nodeMap.count(toname) == 0)
325 msg="to node " + param._tonode + " does not exist in parameter: ";
326 msg=msg+"->"+param._tonode+"("+param._toport+")";
330 YACS::ENGINE::InputPort* inport=currentProc->nodeMap[toname]->getInputPort(param._toport);
331 //We don't know the parameter type. So we try to initialize the port
332 //with the value. If it's not the right type, edInit throws an exception
335 inport->edInit("XML",param._value.c_str());
337 catch(YACS::Exception& e)
339 this->logError(e.what());
341 DEBTRACE( "++++++++++++++++++++End parameter+++++++++++++++++++++" );
344 virtual void preset (ENGINE::DataNode* const& n);
345 virtual void outnode (ENGINE::DataNode* const& n);
349 DEBTRACE( "bloc_post" )
350 currentProc->names.pop_back();
362 std::vector<YACS::ENGINE::Bloc *> _blocs;
365 template <class T> bloctypeParser<T> bloctypeParser<T>::blocParser;
368 void bloctypeParser<T>::preset (ENGINE::DataNode* const& n)
370 DEBTRACE("bloc_preset_set: " << n->getName() );
371 _bloc->edAddChild(n);
372 std::string fullname = currentProc->names.back()+n->getName();
373 currentProc->nodeMap[fullname]=n;
377 void bloctypeParser<T>::outnode (ENGINE::DataNode* const& n)
379 DEBTRACE("bloc_outnode: " << n->getName() );
380 _bloc->edAddChild(n);
381 std::string fullname = currentProc->names.back()+n->getName();
382 currentProc->nodeMap[fullname]=n;
386 void bloctypeParser<YACS::ENGINE::Bloc*>::name (const std::string& name)
388 DEBTRACE( "bloc_name: " << name )
389 std::string fullname=currentProc->names.back()+name;
390 _bloc=theRuntime->createBloc(name);
391 _blocs.push_back(_bloc);
392 currentProc->names.push_back(fullname+'.');
397 #include "loopParsers.hxx"
398 #include "switchParsers.hxx"
399 #include "presetParsers.hxx"
400 #include "outputParsers.hxx"
406 void bloctypeParser<T>::onStart(const XML_Char* el, const XML_Char** attr)
408 DEBTRACE( "bloctypeParser::onStart: " << el )
409 std::string element(el);
411 parser* pp=&parser::main_parser;
412 if(element == "property")pp=&propertytypeParser::propertyParser;
414 else if(element == "inline")pp=&inlinetypeParser<>::inlineParser;
415 else if(element == "sinline")pp=&sinlinetypeParser<>::sinlineParser;
416 else if(element == "service")pp=&servicetypeParser<>::serviceParser;
417 else if(element == "server")pp=&servertypeParser<>::serverParser;
418 else if(element == "remote")pp=&remotetypeParser<>::remoteParser;
419 else if(element == "node")pp=&nodetypeParser<>::nodeParser;
420 else if(element == "datanode")pp=&presettypeParser<>::presetParser;
421 else if(element == "outnode")pp=&outnodetypeParser<>::outnodeParser;
423 else if(element == "bloc")pp=&bloctypeParser<>::blocParser;
424 else if(element == "forloop")pp=&forlooptypeParser<>::forloopParser;
425 else if(element == "foreach")pp=&foreachlooptypeParser<>::foreachloopParser;
426 else if(element == "optimizer")pp=&optimizerlooptypeParser<>::optimizerloopParser;
427 else if(element == "while")pp=&whilelooptypeParser<>::whileloopParser;
428 else if(element == "switch")pp=&switchtypeParser::switchParser;
430 else if(element == "control")pp=&controltypeParser<>::controlParser;
431 else if(element == "datalink")pp=&linktypeParser<>::linkParser;
432 else if(element == "stream")pp=&streamtypeParser<>::streamParser;
433 else if(element == "parameter")pp=¶metertypeParser::parameterParser;
435 this->SetUserDataAndPush(pp);
442 void bloctypeParser<T>::onEnd(const char *el,parser* child)
444 DEBTRACE( "bloctypeParser::onEnd: " << el )
445 std::string element(el);
446 if(element == "property")property(((propertytypeParser*)child)->post());
447 else if(element == "inline")inline_(((inlinetypeParser<>*)child)->post());
448 else if(element == "sinline")sinline(((sinlinetypeParser<>*)child)->post());
449 else if(element == "service")service(((servicetypeParser<>*)child)->post());
450 else if(element == "server")server(((servertypeParser<>*)child)->post());
451 else if(element == "remote")remote(((remotetypeParser<>*)child)->post());
452 else if(element == "node")node(((nodetypeParser<>*)child)->post());
453 else if(element == "datanode")preset(((presettypeParser<>*)child)->post());
454 else if(element == "outnode")outnode(((outnodetypeParser<>*)child)->post());
456 else if(element == "bloc")bloc(((bloctypeParser<>*)child)->post());
457 else if(element == "forloop")forloop(((forlooptypeParser<>*)child)->post());
458 else if(element == "optimizer")optimizer(((optimizerlooptypeParser<>*)child)->post());
459 else if(element == "foreach")foreach(((foreachlooptypeParser<>*)child)->post());
460 else if(element == "while")while_(((whilelooptypeParser<>*)child)->post());
461 else if(element == "switch")switch_(((switchtypeParser*)child)->post());
463 else if(element == "control") control(((controltypeParser<>*)child)->post());
464 else if(element == "datalink") datalink(((linktypeParser<>*)child)->post());
465 else if(element == "stream") stream(((streamtypeParser<>*)child)->post());
466 else if(element == "parameter") parameter(((parametertypeParser*)child)->post());