]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/Proc.cxx
Salome HOME
990b80f8c87c8d9d15649b28c6ef05e329b73cc3
[modules/yacs.git] / src / engine / Proc.cxx
1 // Copyright (C) 2006-2013  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "Proc.hxx"
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"
28 #include "Logger.hxx"
29 #include "Visitor.hxx"
30 #include "VisitorSaveSchema.hxx"
31 #include "VisitorSaveState.hxx"
32 #include <sstream>
33 #include <set>
34
35 //#define _DEVDEBUG_
36 #include "YacsTrace.hxx"
37
38 using namespace std;
39 using namespace YACS::ENGINE;
40
41 /*! \class YACS::ENGINE::Proc
42  *  \brief Base class for all schema objects.
43  *
44  * This is an abstract class that must be specialized in runtime.
45  * \ingroup Nodes
46  */
47
48 Proc::Proc(const std::string& name):Bloc(name),_edition(false),_compoinstctr(0)
49 {
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;
66 }
67
68 Proc::~Proc()
69 {
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();
77
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();
82
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++)
86     delete (*lt).second;
87 }
88
89 void Proc::writeDot(std::ostream &os) const
90 {
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";
95
96   Bloc::writeDot(os);
97   os << "}\n" ;
98 }
99
100 std::ostream& operator<< (std::ostream& os, const Proc& p)
101 {
102   os << "Proc" ;
103   return os;
104 }
105
106 TypeCode *Proc::createType(const std::string& name, const std::string& kind)
107 {
108   TypeCode* t;
109   if(kind=="double")
110     t=getRuntime()->_tc_double;
111   else if(kind=="string")
112     t=getRuntime()->_tc_string;
113   else if(kind=="int")
114     t=getRuntime()->_tc_int;
115   else if(kind=="bool")
116     t=getRuntime()->_tc_bool;
117   else
118     throw Exception("Unknown kind");
119
120   if(typeMap.count(name)!=0)
121     typeMap[name]->decrRef();
122   t->incrRef();
123   typeMap[name]=t;
124   t->incrRef();
125   return t;
126 }
127
128 //! Create an object reference TypeCode 
129 /*!
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
134  */
135 TypeCode *Proc::createInterfaceTc(const std::string& id, const std::string& name,
136                                   std::list<TypeCodeObjref *> ltc)
137 {
138   TypeCode* t = TypeCode::interfaceTc(id.c_str(),name.c_str(),ltc);
139   if(typeMap.count(name)!=0)
140     typeMap[name]->decrRef();
141   typeMap[name]=t;
142   t->incrRef();
143   return t;
144 }
145
146 //! Create a sequence TypeCode 
147 /*!
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
152  */
153 TypeCode * Proc::createSequenceTc (const std::string& id, const std::string& name,
154                                    TypeCode *content)
155 {
156   TypeCode* t = TypeCode::sequenceTc(id.c_str(),name.c_str(),content);
157   if(typeMap.count(name)!=0)
158     typeMap[name]->decrRef();
159   typeMap[name]=t;
160   t->incrRef();
161   return t;
162 }
163
164 TypeCode * Proc::createStructTc (const std::string& id, const std::string& name)
165 {
166   TypeCode* t = TypeCode::structTc(id.c_str(),name.c_str());
167   if(typeMap.count(name)!=0)
168     typeMap[name]->decrRef();
169   typeMap[name]=t;
170   t->incrRef();
171   return t;
172 }
173
174 TypeCode * Proc::getTypeCode (const std::string& name)
175 {
176   TypeCode* aTC=0;
177   if(typeMap.count(name)==0)
178     aTC=getRuntime()->getTypeCode(name);
179   else
180     aTC=typeMap[name];
181
182   if(!aTC)
183     {
184       std::stringstream msg;
185       msg << "Type " << name << " does not exist" ;
186       msg << " (" <<__FILE__ << ":" << __LINE__ << ")";
187       throw Exception(msg.str());
188     }
189
190   return aTC;
191 }
192
193 void Proc::setTypeCode (const std::string& name,TypeCode *t)
194 {
195   if(typeMap.count(name)!=0)
196     typeMap[name]->decrRef();
197   typeMap[name]=t;
198   t->incrRef();
199 }
200
201
202 void Proc::accept(Visitor *visitor)
203 {
204   visitor->visitProc(this);
205 }
206
207 void Proc::setName(const std::string& name)
208 {
209   _name = name;
210 }
211
212 YACS::StatesForNode Proc::getNodeState(int numId)
213 {
214   if(YACS::ENGINE::Node::idMap.count(numId) == 0)
215     {
216       cerr << "Unknown node id " << numId << endl;
217       return YACS::UNDEFINED;
218     }
219   YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[numId];
220   YACS::StatesForNode state = node->getEffectiveState();
221   return state;
222 }
223
224 std::string Proc::getXMLState(int numId)
225 {
226   if(YACS::ENGINE::Node::idMap.count(numId) == 0)
227     {
228       cerr << "Unknown node id " << numId << endl;
229       return "<state>unknown</state>";
230     }
231   YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[numId];
232   stringstream msg;
233   msg << "<state>" << node->getEffectiveState() << "</state>";
234   msg << "<name>" << node->getQualifiedName() << "</name>";
235   msg << "<id>" << numId << "</id>";
236   return msg.str();
237 }
238
239 std::string Proc::getInPortValue(int nodeNumId, std::string portName)
240 {
241   DEBTRACE("Proc::getInPortValue " << nodeNumId << " " << portName);
242   stringstream msg;
243   if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
244     {
245       msg << "<value><error>unknown node id: " << nodeNumId << "</error></value>";
246       return msg.str();
247     }
248   try
249     {
250       YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
251       InputPort * inputPort = node->getInputPort(portName);
252       return inputPort->getAsString();
253     }
254   catch(YACS::Exception& ex)
255     {
256       DEBTRACE("Proc::getInPortValue " << ex.what());
257       msg << "<value><error>" << ex.what() << "</error></value>";
258       return msg.str();
259     }
260 }
261
262 std::string Proc::setInPortValue(std::string nodeName, std::string portName, std::string value)
263 {
264   DEBTRACE("Proc::setInPortValue " << nodeName << " " << portName << " " << value);
265
266   try
267     {
268       YACS::ENGINE::Node* node = YACS::ENGINE::Proc::nodeMap[nodeName];
269       YACS::ENGINE::InputPort* inputPort = node->getInputPort(portName);
270
271       switch (inputPort->edGetType()->kind())
272         {
273           case Double:
274             {
275               double val = atof(value.c_str());
276               inputPort->edInit(val);
277             }
278           case Int:
279             {
280               int val = atoi(value.c_str());
281               inputPort->edInit(val);
282             }
283           case String:
284             inputPort->edInit(value.c_str());
285           case Bool:
286             {
287               bool val = (! value.compare("False") ) && (! value.compare("0") );
288               inputPort->edInit(val);
289             }
290           default:
291             DEBTRACE("Proc::setInPortValue: filtered type: " << inputPort->edGetType()->kind());
292         }
293       return value;
294     }
295   catch(YACS::Exception& ex)
296     {
297       DEBTRACE("Proc::setInPortValue " << ex.what());
298       stringstream msg;
299       msg << "<value><error>" << ex.what() << "</error></value>";
300       return msg.str();
301     }
302 }
303
304 std::string Proc::getOutPortValue(int nodeNumId, std::string portName)
305 {
306   DEBTRACE("Proc::getOutPortValue " << nodeNumId << " " << portName);
307   stringstream msg;
308   if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
309     {
310       msg << "<value><error>unknown node id: " << nodeNumId << "</error></value>";
311       return msg.str();
312     }
313   try
314     {
315       YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
316       OutputPort * outputPort = node->getOutputPort(portName);
317       return outputPort->getAsString();
318     }
319   catch(YACS::Exception& ex)
320     {
321       DEBTRACE("Proc::getOutPortValue " << ex.what());
322       msg << "<value><error>" << ex.what() << "</error></value>";
323       return msg.str();
324     }
325 }
326
327 std::string Proc::getNodeErrorDetails(int nodeNumId)
328 {
329   DEBTRACE("Proc::getNodeErrorDetails " << nodeNumId);
330   stringstream msg;
331   if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
332     {
333       msg << "Unknown node id " << nodeNumId;
334       return msg.str();
335     }
336   YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
337   return node->getErrorDetails();
338 }
339
340 std::string Proc::getNodeErrorReport(int nodeNumId)
341 {
342   DEBTRACE("Proc::getNodeErrorReport " << nodeNumId);
343   stringstream msg;
344   if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
345     {
346       msg << "Unknown node id " << nodeNumId;
347       return msg.str();
348     }
349   YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
350   return node->getErrorReport();
351 }
352
353 std::string Proc::getNodeContainerLog(int nodeNumId)
354 {
355   DEBTRACE("Proc::getNodeContainerLog " << nodeNumId);
356   stringstream msg;
357   if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
358     {
359       msg << "Unknown node id " << nodeNumId;
360       return msg.str();
361     }
362   YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
363   return node->getContainerLog();
364 }
365
366 std::list<int> Proc::getNumIds()
367 {
368   list<YACS::ENGINE::Node *> nodes = getAllRecursiveConstituents();
369   int len = nodes.size();
370   list<int> numids;
371   for( list<YACS::ENGINE::Node *>::const_iterator iter = nodes.begin();
372        iter != nodes.end(); iter++)
373     {
374       numids.push_back((*iter)->getNumId());
375     }
376   numids.push_back(this->getNumId());
377   return numids;
378 }
379
380 std::list<std::string> Proc::getIds()
381 {
382   list<YACS::ENGINE::Node *> nodes = getAllRecursiveConstituents();
383   int len = nodes.size();
384   list<string> ids;
385   for( list<YACS::ENGINE::Node *>::const_iterator iter = nodes.begin();
386        iter != nodes.end(); iter++)
387     {
388       ids.push_back(getChildName(*iter));
389     }
390   ids.push_back("_root_");
391   return ids;
392 }
393
394 Logger *Proc::getLogger(const std::string& name)
395 {
396   Logger* logger;
397   LoggerMap::const_iterator it = _loggers.find(name);
398
399   if (it != _loggers.end())
400   {
401     logger = it->second;
402   }
403   else
404   {
405     logger = new Logger(name);
406     _loggers[name]=logger;
407   }
408   return logger;
409 }
410
411 void Proc::setEdition(bool edition)
412 {
413   DEBTRACE("Proc::setEdition: " << edition);
414   _edition=edition;
415   if(_edition)
416     edUpdateState();
417 }
418 //! Sets Proc in modified state and update state if in edition mode
419 /*!
420  *
421  */
422 void Proc::modified()
423 {
424   DEBTRACE("Proc::modified() " << _edition);
425   _modified=1;
426   if(_edition)
427     edUpdateState();
428 }
429
430 //! Save Proc in XML schema file
431 /*!
432  * \param xmlSchemaFile: the file name
433  */
434 void Proc::saveSchema(std::string xmlSchemaFile)
435 {
436   VisitorSaveSchema vss(this);
437   vss.openFileSchema(xmlSchemaFile);
438   accept(&vss);
439   vss.closeFileSchema();
440 }
441
442 //! Save Proc state in XML state file
443 /*!
444  * \param xmlStateFile: the file name
445  */
446 void Proc::saveState(std::string xmlStateFile)
447 {
448   VisitorSaveState vst(this);
449   vst.openFileDump(xmlStateFile);
450   accept(&vst);
451   vst.closeFileDump();
452 }
453
454 //! Create a new Container and store it in containerMap
455 /*!
456  * \param name: the container name and key in containerMap
457  * \param kind: the container kind (depends on runtime)
458  * \return the created Container
459  */
460 Container* Proc::createContainer(const std::string& name,const std::string& kind)
461 {
462   Container* co=  getRuntime()->createContainer(kind);
463   co->setName(name);
464   if(containerMap.count(name)!=0)
465     containerMap[name]->decrRef();
466   containerMap[name]=co;
467   co->incrRef();
468   co->setProc(this);
469   return co;
470 }
471
472 //! Add a ComponentInstance into componentInstanceMap
473 /*!
474  * If the name == "", the component instance is automatically named with a unique (in the Proc) name
475  *
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
479  */
480 void Proc::addComponentInstance(ComponentInstance* inst, const std::string& name, bool resetCtr)
481 {
482   if(name != "")
483     {
484       inst->setName(name);
485       inst->setAnonymous(false);
486       if(componentInstanceMap.count(name)!=0)
487         componentInstanceMap[name]->decrRef();
488       componentInstanceMap[name]=inst;
489       inst->incrRef();
490     }
491   else
492     {
493       //automatic naming : componame_<_compoinstctr>
494       std::string instname;
495       std::string componame=inst->getCompoName();
496       if (resetCtr)
497         _compoinstctr = 0;        
498       while(1)
499         {
500           std::ostringstream buffer;
501           buffer << ++_compoinstctr;
502           instname=componame+"_"+buffer.str();
503           if(componentInstanceMap.count(instname)==0)
504             {
505               inst->setName(instname);
506               componentInstanceMap[instname]=inst;
507               inst->incrRef();
508               break;
509             }
510         }
511     }
512 }
513
514 //! Remove a componentInstance from the componentInstanceMap
515 /*!
516  * To be used for a componentInstance with no service nodes referenced.
517  *
518  * \param inst: the component instance
519  */
520 void Proc::removeComponentInstance(ComponentInstance* inst)
521 {
522   if (componentInstanceMap.count(inst->getInstanceName()))
523     {
524       componentInstanceMap.erase(inst->getInstanceName());
525       inst->decrRef();
526     }
527 }
528
529 //! Remove a container from the containerMap
530 /*!
531  * To be used for a container with no componentInstance referenced.
532  *
533  * \param cont: the container
534  */
535 void Proc::removeContainer(Container* cont)
536 {
537   if (containerMap.count(cont->getName()))
538     {
539       containerMap.erase(cont->getName());
540       cont->decrRef();
541     }
542 }
543
544 //! Create a new ComponentInstance and add it into componentInstanceMap
545 /*!
546  * If the name == "", the component instance is automatically named with a unique (in the Proc) name
547  *
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
552  */
553 ComponentInstance* Proc::createComponentInstance(const std::string& componame, const std::string& name,const std::string& kind)
554 {
555   ComponentInstance* inst=  getRuntime()->createComponentInstance(componame,kind);
556   addComponentInstance(inst,name);
557   return inst;
558 }
559
560 //! Return the proc (this)
561 Proc* Proc::getProc()
562 {
563   return this;
564 }
565
566 //! Return the proc (this)
567 const Proc * Proc::getProc() const
568 {
569   return this;
570 }