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
21 #include "ElementaryNode.hxx"
22 #include "Runtime.hxx"
23 #include "Container.hxx"
24 #include "ComponentInstance.hxx"
25 #include "InputPort.hxx"
26 #include "OutputPort.hxx"
27 #include "TypeCode.hxx"
29 #include "Visitor.hxx"
30 #include "VisitorSaveSchema.hxx"
31 #include "VisitorSaveState.hxx"
36 #include "YacsTrace.hxx"
39 using namespace YACS::ENGINE;
41 /*! \class YACS::ENGINE::Proc
42 * \brief Base class for all schema objects.
44 * This is an abstract class that must be specialized in runtime.
48 Proc::Proc(const std::string& name):Bloc(name),_edition(false),_compoinstctr(0)
50 Runtime *theRuntime=getRuntime();
51 DEBTRACE("theRuntime->_tc_double->ref: " << theRuntime->_tc_double->getRefCnt());
52 DEBTRACE("theRuntime->_tc_int->ref: " << theRuntime->_tc_int->getRefCnt());
53 DEBTRACE("theRuntime->_tc_string->ref: " << theRuntime->_tc_string->getRefCnt());
54 DEBTRACE("theRuntime->_tc_bool->ref: " << theRuntime->_tc_bool->getRefCnt());
55 DEBTRACE("theRuntime->_tc_file->ref: " << theRuntime->_tc_file->getRefCnt());
56 theRuntime->_tc_double->incrRef();
57 theRuntime->_tc_string->incrRef();
58 theRuntime->_tc_int->incrRef();
59 theRuntime->_tc_bool->incrRef();
60 theRuntime->_tc_file->incrRef();
61 typeMap["double"]=theRuntime->_tc_double;
62 typeMap["string"]=theRuntime->_tc_string;
63 typeMap["int"]=theRuntime->_tc_int;
64 typeMap["bool"]=theRuntime->_tc_bool;
65 typeMap["file"]=theRuntime->_tc_file;
70 DEBTRACE("Proc::~Proc");
71 //for the moment all nodes are owned, so no need to manage their destruction
72 //nodeMap, inlineMap, serviceMap will be cleared automatically
73 //but we need to destroy TypeCodes
74 std::map<std::string, TypeCode *>::iterator pt;
75 for(pt=typeMap.begin();pt!=typeMap.end();pt++)
76 ((*pt).second)->decrRef();
78 //get rid of containers in container map
79 std::map<std::string, Container*>::const_iterator it;
80 for(it=containerMap.begin();it!=containerMap.end();it++)
81 ((*it).second)->decrRef();
83 //get rid of loggers in logger map
84 std::map<std::string, Logger*>::const_iterator lt;
85 for(lt=_loggers.begin();lt!=_loggers.end();lt++)
89 void Proc::writeDot(std::ostream &os) const
91 os << "digraph " << getQualifiedName() << " {\n" ;
92 os << "node [ style=\"filled\" ];\n" ;
93 os << "compound=true;";
94 os << "states [label=< <TABLE> <TR> <TD BGCOLOR=\"pink\" > Ready</TD> <TD BGCOLOR=\"magenta\" > Toload</TD> </TR> <TR> <TD BGCOLOR=\"magenta\" > Loaded</TD> <TD BGCOLOR=\"purple\" > Toactivate</TD> </TR> <TR> <TD BGCOLOR=\"blue\" > Activated</TD> <TD BGCOLOR=\"green\" > Done</TD> </TR> <TR> <TD BGCOLOR=\"red\" > Error</TD> <TD BGCOLOR=\"orange\" > Failed</TD> </TR> <TR> <TD BGCOLOR=\"grey\" > Disabled</TD> <TD BGCOLOR=\"white\" > Pause</TD> </TR> </TABLE>> \n shape = plaintext \n style = invis \n ];\n";
100 std::ostream& operator<< (std::ostream& os, const Proc& p)
106 TypeCode *Proc::createType(const std::string& name, const std::string& kind)
110 t=getRuntime()->_tc_double;
111 else if(kind=="string")
112 t=getRuntime()->_tc_string;
114 t=getRuntime()->_tc_int;
115 else if(kind=="bool")
116 t=getRuntime()->_tc_bool;
118 throw Exception("Unknown kind");
120 if(typeMap.count(name)!=0)
121 typeMap[name]->decrRef();
128 //! Create an object reference TypeCode
130 * \param id: the TypeCode repository id
131 * \param name: the TypeCode name
132 * \param ltc: a liste of object reference TypeCode to use as base types for this type
133 * \return the created TypeCode
135 TypeCode *Proc::createInterfaceTc(const std::string& id, const std::string& name,
136 std::list<TypeCodeObjref *> ltc)
138 TypeCode* t = TypeCode::interfaceTc(id.c_str(),name.c_str(),ltc);
139 if(typeMap.count(name)!=0)
140 typeMap[name]->decrRef();
146 //! Create a sequence TypeCode
148 * \param id: the TypeCode repository id ("" for normal use)
149 * \param name: the TypeCode name
150 * \param content: the element TypeCode
151 * \return the created TypeCode
153 TypeCode * Proc::createSequenceTc (const std::string& id, const std::string& name,
156 TypeCode* t = TypeCode::sequenceTc(id.c_str(),name.c_str(),content);
157 if(typeMap.count(name)!=0)
158 typeMap[name]->decrRef();
164 TypeCode * Proc::createStructTc (const std::string& id, const std::string& name)
166 TypeCode* t = TypeCode::structTc(id.c_str(),name.c_str());
167 if(typeMap.count(name)!=0)
168 typeMap[name]->decrRef();
174 TypeCode * Proc::getTypeCode (const std::string& name)
177 if(typeMap.count(name)==0)
178 aTC=getRuntime()->getTypeCode(name);
184 std::stringstream msg;
185 msg << "Type " << name << " does not exist" ;
186 msg << " (" <<__FILE__ << ":" << __LINE__ << ")";
187 throw Exception(msg.str());
193 void Proc::setTypeCode (const std::string& name,TypeCode *t)
195 if(typeMap.count(name)!=0)
196 typeMap[name]->decrRef();
202 void Proc::accept(Visitor *visitor)
204 visitor->visitProc(this);
207 void Proc::setName(const std::string& name)
212 YACS::StatesForNode Proc::getNodeState(int numId)
214 if(YACS::ENGINE::Node::idMap.count(numId) == 0)
216 cerr << "Unknown node id " << numId << endl;
217 return YACS::UNDEFINED;
219 YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[numId];
220 YACS::StatesForNode state = node->getEffectiveState();
224 std::string Proc::getNodeProgress(int numId)
226 std::string progress = "0";
227 if(YACS::ENGINE::Node::idMap.count(numId) == 0)
229 cerr << "Unknown node id " << numId << endl;
231 else if (YACS::ENGINE::ComposedNode* node = dynamic_cast<YACS::ENGINE::ComposedNode*>(YACS::ENGINE::Node::idMap[numId]))
232 progress = node->getProgress();
236 std::string Proc::getXMLState(int numId)
238 if(YACS::ENGINE::Node::idMap.count(numId) == 0)
240 cerr << "Unknown node id " << numId << endl;
241 return "<state>unknown</state>";
243 YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[numId];
245 msg << "<state>" << node->getEffectiveState() << "</state>";
246 msg << "<name>" << node->getQualifiedName() << "</name>";
247 msg << "<id>" << numId << "</id>";
251 std::string Proc::getInPortValue(int nodeNumId, std::string portName)
253 DEBTRACE("Proc::getInPortValue " << nodeNumId << " " << portName);
255 if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
257 msg << "<value><error>unknown node id: " << nodeNumId << "</error></value>";
262 YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
263 InputPort * inputPort = node->getInputPort(portName);
264 return inputPort->getAsString();
266 catch(YACS::Exception& ex)
268 DEBTRACE("Proc::getInPortValue " << ex.what());
269 msg << "<value><error>" << ex.what() << "</error></value>";
274 std::string Proc::setInPortValue(std::string nodeName, std::string portName, std::string value)
276 DEBTRACE("Proc::setInPortValue " << nodeName << " " << portName << " " << value);
280 YACS::ENGINE::Node* node = YACS::ENGINE::Proc::nodeMap[nodeName];
281 YACS::ENGINE::InputPort* inputPort = node->getInputPort(portName);
283 switch (inputPort->edGetType()->kind())
287 double val = atof(value.c_str());
288 inputPort->edInit(val);
292 int val = atoi(value.c_str());
293 inputPort->edInit(val);
296 inputPort->edInit(value.c_str());
299 bool val = (! value.compare("False") ) && (! value.compare("0") );
300 inputPort->edInit(val);
303 DEBTRACE("Proc::setInPortValue: filtered type: " << inputPort->edGetType()->kind());
307 catch(YACS::Exception& ex)
309 DEBTRACE("Proc::setInPortValue " << ex.what());
311 msg << "<value><error>" << ex.what() << "</error></value>";
316 std::string Proc::getOutPortValue(int nodeNumId, std::string portName)
318 DEBTRACE("Proc::getOutPortValue " << nodeNumId << " " << portName);
320 if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
322 msg << "<value><error>unknown node id: " << nodeNumId << "</error></value>";
327 YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
328 OutputPort * outputPort = node->getOutputPort(portName);
329 return outputPort->getAsString();
331 catch(YACS::Exception& ex)
333 DEBTRACE("Proc::getOutPortValue " << ex.what());
334 msg << "<value><error>" << ex.what() << "</error></value>";
339 std::string Proc::getNodeErrorDetails(int nodeNumId)
341 DEBTRACE("Proc::getNodeErrorDetails " << nodeNumId);
343 if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
345 msg << "Unknown node id " << nodeNumId;
348 YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
349 return node->getErrorDetails();
352 std::string Proc::getNodeErrorReport(int nodeNumId)
354 DEBTRACE("Proc::getNodeErrorReport " << nodeNumId);
356 if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
358 msg << "Unknown node id " << nodeNumId;
361 YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
362 return node->getErrorReport();
365 std::string Proc::getNodeContainerLog(int nodeNumId)
367 DEBTRACE("Proc::getNodeContainerLog " << nodeNumId);
369 if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
371 msg << "Unknown node id " << nodeNumId;
374 YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
375 return node->getContainerLog();
378 std::list<int> Proc::getNumIds()
380 list<YACS::ENGINE::Node *> nodes = getAllRecursiveConstituents();
381 int len = nodes.size();
383 for( list<YACS::ENGINE::Node *>::const_iterator iter = nodes.begin();
384 iter != nodes.end(); iter++)
386 numids.push_back((*iter)->getNumId());
388 numids.push_back(this->getNumId());
392 std::list<std::string> Proc::getIds()
394 list<YACS::ENGINE::Node *> nodes = getAllRecursiveConstituents();
395 int len = nodes.size();
397 for( list<YACS::ENGINE::Node *>::const_iterator iter = nodes.begin();
398 iter != nodes.end(); iter++)
400 ids.push_back(getChildName(*iter));
402 ids.push_back("_root_");
406 Logger *Proc::getLogger(const std::string& name)
409 LoggerMap::const_iterator it = _loggers.find(name);
411 if (it != _loggers.end())
417 logger = new Logger(name);
418 _loggers[name]=logger;
423 void Proc::setEdition(bool edition)
425 DEBTRACE("Proc::setEdition: " << edition);
430 //! Sets Proc in modified state and update state if in edition mode
434 void Proc::modified()
436 DEBTRACE("Proc::modified() " << _edition);
442 //! Save Proc in XML schema file
444 * \param xmlSchemaFile: the file name
446 void Proc::saveSchema(std::string xmlSchemaFile)
448 VisitorSaveSchema vss(this);
449 vss.openFileSchema(xmlSchemaFile);
451 vss.closeFileSchema();
454 //! Save Proc state in XML state file
456 * \param xmlStateFile: the file name
458 void Proc::saveState(std::string xmlStateFile)
460 VisitorSaveState vst(this);
461 vst.openFileDump(xmlStateFile);
466 //! Create a new Container and store it in containerMap
468 * \param name: the container name and key in containerMap
469 * \param kind: the container kind (depends on runtime)
470 * \return the created Container
472 Container* Proc::createContainer(const std::string& name,const std::string& kind)
474 Container* co= getRuntime()->createContainer(kind);
476 if(containerMap.count(name)!=0)
477 containerMap[name]->decrRef();
478 containerMap[name]=co;
484 //! Add a ComponentInstance into componentInstanceMap
486 * If the name == "", the component instance is automatically named with a unique (in the Proc) name
488 * \param inst: the component instance
489 * \param name: the component instance name
490 * \param restCtr: try to reuse instance number previously released, false by default
492 void Proc::addComponentInstance(ComponentInstance* inst, const std::string& name, bool resetCtr)
497 inst->setAnonymous(false);
498 if(componentInstanceMap.count(name)!=0)
499 componentInstanceMap[name]->decrRef();
500 componentInstanceMap[name]=inst;
505 //automatic naming : componame_<_compoinstctr>
506 std::string instname;
507 std::string componame=inst->getCompoName();
512 std::ostringstream buffer;
513 buffer << ++_compoinstctr;
514 instname=componame+"_"+buffer.str();
515 if(componentInstanceMap.count(instname)==0)
517 inst->setName(instname);
518 componentInstanceMap[instname]=inst;
526 //! Remove a componentInstance from the componentInstanceMap
528 * To be used for a componentInstance with no service nodes referenced.
530 * \param inst: the component instance
532 void Proc::removeComponentInstance(ComponentInstance* inst)
534 if (componentInstanceMap.count(inst->getInstanceName()))
536 componentInstanceMap.erase(inst->getInstanceName());
541 //! Remove a container from the containerMap
543 * To be used for a container with no componentInstance referenced.
545 * \param cont: the container
547 void Proc::removeContainer(Container* cont)
549 if (containerMap.count(cont->getName()))
551 containerMap.erase(cont->getName());
556 //! Create a new ComponentInstance and add it into componentInstanceMap
558 * If the name == "", the component instance is automatically named with a unique (in the Proc) name
560 * \param componame: the component name
561 * \param name: the component instance name
562 * \param kind: the component instance kind (depends on runtime)
563 * \return the created ComponentInstance
565 ComponentInstance* Proc::createComponentInstance(const std::string& componame, const std::string& name,const std::string& kind)
567 ComponentInstance* inst= getRuntime()->createComponentInstance(componame,kind);
568 addComponentInstance(inst,name);
572 //! Return the proc (this)
573 Proc* Proc::getProc()
578 //! Return the proc (this)
579 const Proc * Proc::getProc() const