1 // Copyright (C) 2006-2012 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
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::getXMLState(int numId)
226 if(YACS::ENGINE::Node::idMap.count(numId) == 0)
228 cerr << "Unknown node id " << numId << endl;
229 return "<state>unknown</state>";
231 YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[numId];
233 msg << "<state>" << node->getEffectiveState() << "</state>";
234 msg << "<name>" << node->getQualifiedName() << "</name>";
235 msg << "<id>" << numId << "</id>";
239 std::string Proc::getInPortValue(int nodeNumId, std::string portName)
241 DEBTRACE("Proc::getInPortValue " << nodeNumId << " " << portName);
243 if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
245 msg << "<value><error>unknown node id: " << nodeNumId << "</error></value>";
250 YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
251 InputPort * inputPort = node->getInputPort(portName);
252 return inputPort->getAsString();
254 catch(YACS::Exception& ex)
256 DEBTRACE("Proc::getInPortValue " << ex.what());
257 msg << "<value><error>" << ex.what() << "</error></value>";
262 std::string Proc::setInPortValue(std::string nodeName, std::string portName, std::string value)
264 DEBTRACE("Proc::setInPortValue " << nodeName << " " << portName << " " << value);
268 YACS::ENGINE::Node* node = YACS::ENGINE::Proc::nodeMap[nodeName];
269 YACS::ENGINE::InputPort* inputPort = node->getInputPort(portName);
271 switch (inputPort->edGetType()->kind())
275 double val = atof(value.c_str());
276 inputPort->edInit(val);
280 int val = atoi(value.c_str());
281 inputPort->edInit(val);
284 inputPort->edInit(value.c_str());
287 bool val = (! value.compare("False") ) && (! value.compare("0") );
288 inputPort->edInit(val);
291 DEBTRACE("Proc::setInPortValue: filtered type: " << inputPort->edGetType()->kind());
295 catch(YACS::Exception& ex)
297 DEBTRACE("Proc::setInPortValue " << ex.what());
299 msg << "<value><error>" << ex.what() << "</error></value>";
304 std::string Proc::getOutPortValue(int nodeNumId, std::string portName)
306 DEBTRACE("Proc::getOutPortValue " << nodeNumId << " " << portName);
308 if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
310 msg << "<value><error>unknown node id: " << nodeNumId << "</error></value>";
315 YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
316 OutputPort * outputPort = node->getOutputPort(portName);
317 return outputPort->getAsString();
319 catch(YACS::Exception& ex)
321 DEBTRACE("Proc::getOutPortValue " << ex.what());
322 msg << "<value><error>" << ex.what() << "</error></value>";
327 std::string Proc::getNodeErrorDetails(int nodeNumId)
329 DEBTRACE("Proc::getNodeErrorDetails " << nodeNumId);
331 if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
333 msg << "Unknown node id " << nodeNumId;
336 YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
337 return node->getErrorDetails();
340 std::string Proc::getNodeErrorReport(int nodeNumId)
342 DEBTRACE("Proc::getNodeErrorReport " << nodeNumId);
344 if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
346 msg << "Unknown node id " << nodeNumId;
349 YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
350 return node->getErrorReport();
353 std::string Proc::getNodeContainerLog(int nodeNumId)
355 DEBTRACE("Proc::getNodeContainerLog " << nodeNumId);
357 if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
359 msg << "Unknown node id " << nodeNumId;
362 YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
363 return node->getContainerLog();
366 std::list<int> Proc::getNumIds()
368 list<YACS::ENGINE::Node *> nodes = getAllRecursiveConstituents();
369 int len = nodes.size();
371 for( list<YACS::ENGINE::Node *>::const_iterator iter = nodes.begin();
372 iter != nodes.end(); iter++)
374 numids.push_back((*iter)->getNumId());
376 numids.push_back(this->getNumId());
380 std::list<std::string> Proc::getIds()
382 list<YACS::ENGINE::Node *> nodes = getAllRecursiveConstituents();
383 int len = nodes.size();
385 for( list<YACS::ENGINE::Node *>::const_iterator iter = nodes.begin();
386 iter != nodes.end(); iter++)
388 ids.push_back(getChildName(*iter));
390 ids.push_back("_root_");
394 Logger *Proc::getLogger(const std::string& name)
397 LoggerMap::const_iterator it = _loggers.find(name);
399 if (it != _loggers.end())
405 logger = new Logger(name);
406 _loggers[name]=logger;
411 void Proc::setEdition(bool edition)
413 DEBTRACE("Proc::setEdition: " << edition);
418 //! Sets Proc in modified state and update state if in edition mode
422 void Proc::modified()
424 DEBTRACE("Proc::modified() " << _edition);
430 //! Save Proc in XML schema file
432 * \param xmlSchemaFile: the file name
434 void Proc::saveSchema(std::string xmlSchemaFile)
436 VisitorSaveSchema vss(this);
437 vss.openFileSchema(xmlSchemaFile);
439 vss.closeFileSchema();
442 //! Save Proc state in XML state file
444 * \param xmlStateFile: the file name
446 void Proc::saveState(std::string xmlStateFile)
448 VisitorSaveState vst(this);
449 vst.openFileDump(xmlStateFile);
454 //! Create a new Container and store it in containerMap
456 * \param name: the container name and key in containerMap
457 * \param kind: the container kind (depends on runtime)
458 * \return the created Container
460 Container* Proc::createContainer(const std::string& name,const std::string& kind)
462 Container* co= getRuntime()->createContainer(kind);
464 if(containerMap.count(name)!=0)
465 containerMap[name]->decrRef();
466 containerMap[name]=co;
472 //! Add a ComponentInstance into componentInstanceMap
474 * If the name == "", the component instance is automatically named with a unique (in the Proc) name
476 * \param inst: the component instance
477 * \param name: the component instance name
478 * \param restCtr: try to reuse instance number previously released, false by default
480 void Proc::addComponentInstance(ComponentInstance* inst, const std::string& name, bool resetCtr)
485 inst->setAnonymous(false);
486 if(componentInstanceMap.count(name)!=0)
487 componentInstanceMap[name]->decrRef();
488 componentInstanceMap[name]=inst;
493 //automatic naming : componame_<_compoinstctr>
494 std::string instname;
495 std::string componame=inst->getCompoName();
500 std::ostringstream buffer;
501 buffer << ++_compoinstctr;
502 instname=componame+"_"+buffer.str();
503 if(componentInstanceMap.count(instname)==0)
505 inst->setName(instname);
506 componentInstanceMap[instname]=inst;
514 //! Remove a componentInstance from the componentInstanceMap
516 * To be used for a componentInstance with no service nodes referenced.
518 * \param inst: the component instance
520 void Proc::removeComponentInstance(ComponentInstance* inst)
522 if (componentInstanceMap.count(inst->getInstanceName()))
524 componentInstanceMap.erase(inst->getInstanceName());
529 //! Remove a container from the containerMap
531 * To be used for a container with no componentInstance referenced.
533 * \param cont: the container
535 void Proc::removeContainer(Container* cont)
537 if (containerMap.count(cont->getName()))
539 containerMap.erase(cont->getName());
544 //! Create a new ComponentInstance and add it into componentInstanceMap
546 * If the name == "", the component instance is automatically named with a unique (in the Proc) name
548 * \param componame: the component name
549 * \param name: the component instance name
550 * \param kind: the component instance kind (depends on runtime)
551 * \return the created ComponentInstance
553 ComponentInstance* Proc::createComponentInstance(const std::string& componame, const std::string& name,const std::string& kind)
555 ComponentInstance* inst= getRuntime()->createComponentInstance(componame,kind);
556 addComponentInstance(inst,name);
560 //! Return the proc (this)
561 Proc* Proc::getProc()
566 //! Return the proc (this)
567 const Proc * Proc::getProc() const