]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/Proc.cxx
Salome HOME
9ccc9415d27747392a3ecdcf763a74e2dd43820a
[modules/yacs.git] / src / engine / Proc.cxx
1 #include "Proc.hxx"
2 #include "ElementaryNode.hxx"
3 #include "Runtime.hxx"
4 #include "Container.hxx"
5 #include "InputPort.hxx"
6 #include "OutputPort.hxx"
7 #include "TypeCode.hxx"
8 #include "Logger.hxx"
9 #include "Visitor.hxx"
10 #include <sstream>
11 #include <set>
12
13 //#define _DEVDEBUG_
14 #include "YacsTrace.hxx"
15
16 using namespace std;
17 using namespace YACS::ENGINE;
18
19 Proc::Proc(const std::string& name):Bloc(name),_edition(false)
20 {
21   Runtime *theRuntime=getRuntime();
22   DEBTRACE("theRuntime->_tc_double->ref: " << theRuntime->_tc_double->getRefCnt());
23   DEBTRACE("theRuntime->_tc_int->ref: " << theRuntime->_tc_int->getRefCnt());
24   DEBTRACE("theRuntime->_tc_string->ref: " << theRuntime->_tc_string->getRefCnt());
25   DEBTRACE("theRuntime->_tc_bool->ref: " << theRuntime->_tc_bool->getRefCnt());
26   DEBTRACE("theRuntime->_tc_file->ref: " << theRuntime->_tc_file->getRefCnt());
27   theRuntime->_tc_double->incrRef();
28   theRuntime->_tc_string->incrRef();
29   theRuntime->_tc_int->incrRef();
30   theRuntime->_tc_bool->incrRef();
31   theRuntime->_tc_file->incrRef();
32   typeMap["double"]=theRuntime->_tc_double;
33   typeMap["string"]=theRuntime->_tc_string;
34   typeMap["int"]=theRuntime->_tc_int;
35   typeMap["bool"]=theRuntime->_tc_bool;
36   typeMap["file"]=theRuntime->_tc_file;
37 }
38
39 Proc::~Proc()
40 {
41   DEBTRACE("Proc::~Proc");
42   //for the moment all nodes are owned, so no need to manage their destruction
43   //nodeMap, inlineMap, serviceMap will be cleared automatically
44   //but we need to destroy TypeCodes
45   std::map<std::string, TypeCode *>::iterator pt;
46   for(pt=typeMap.begin();pt!=typeMap.end();pt++)
47     ((*pt).second)->decrRef();
48
49   //get rid of containers in container map
50   std::map<std::string, Container*>::const_iterator it;
51   for(it=containerMap.begin();it!=containerMap.end();it++)
52     ((*it).second)->decrRef();
53
54   //get rid of loggers in logger map
55   std::map<std::string, Logger*>::const_iterator lt;
56   for(lt=_loggers.begin();lt!=_loggers.end();lt++)
57     delete (*lt).second;
58 }
59
60 void Proc::writeDot(std::ostream &os) const
61 {
62   os << "digraph " << getQualifiedName() << " {\n" ;
63   os << "node [ style=\"filled\" ];\n" ;
64   os << "compound=true;";
65   Bloc::writeDot(os);
66   os << "}\n" ;
67 }
68
69 std::ostream& operator<< (std::ostream& os, const Proc& p)
70 {
71   os << "Proc" ;
72   return os;
73 }
74
75 TypeCode *Proc::createType(const std::string& name, const std::string& kind)
76 {
77   TypeCode* t;
78   if(kind=="double")
79     t=getRuntime()->_tc_double;
80   else if(kind=="string")
81     t=getRuntime()->_tc_string;
82   else if(kind=="int")
83     t=getRuntime()->_tc_int;
84   else if(kind=="bool")
85     t=getRuntime()->_tc_bool;
86   else
87     throw Exception("Unknown kind");
88
89   t->incrRef();
90   return t;
91 }
92
93 TypeCode *Proc::createInterfaceTc(const std::string& id, const std::string& name,
94                                   std::list<TypeCodeObjref *> ltc)
95 {
96   return TypeCode::interfaceTc(id.c_str(),name.c_str(),ltc);
97 }
98
99 TypeCode * Proc::createSequenceTc (const std::string& id, const std::string& name,
100                                    TypeCode *content)
101 {
102   return TypeCode::sequenceTc(id.c_str(),name.c_str(),content);
103 }
104
105 TypeCode * Proc::createStructTc (const std::string& id, const std::string& name)
106 {
107   return TypeCode::structTc(id.c_str(),name.c_str());
108 }
109
110 TypeCode * Proc::getTypeCode (const std::string& name)
111 {
112   if(typeMap.count(name)==0)
113     {
114       std::stringstream msg;
115       msg << "Type " << name << " does not exist" ;
116       msg << " (" <<__FILE__ << ":" << __LINE__ << ")";
117       throw Exception(msg.str());
118     }
119   return typeMap[name];
120 }
121
122 void Proc::setTypeCode (const std::string& name,TypeCode *t)
123 {
124   if(typeMap.count(name)!=0)
125     typeMap[name]->decrRef();
126   typeMap[name]=t;
127 }
128
129
130 void Proc::accept(Visitor *visitor)
131 {
132   visitor->visitProc(this);
133 }
134
135 void Proc::setName(const std::string& name)
136 {
137   _name = name;
138 }
139
140 YACS::StatesForNode Proc::getNodeState(int numId)
141 {
142   if(YACS::ENGINE::Node::idMap.count(numId) == 0)
143     {
144       cerr << "Unknown node id " << numId << endl;
145       return YACS::UNDEFINED;
146     }
147   YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[numId];
148   YACS::StatesForNode state = node->getEffectiveState();
149   return state;
150 }
151
152 std::string Proc::getXMLState(int numId)
153 {
154   if(YACS::ENGINE::Node::idMap.count(numId) == 0)
155     {
156       cerr << "Unknown node id " << numId << endl;
157       return "<state>unknown</state>";
158     }
159   YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[numId];
160   stringstream msg;
161   msg << "<state>" << node->getEffectiveState() << "</state>";
162   msg << "<name>" << node->getQualifiedName() << "</name>";
163   msg << "<id>" << numId << "</id>";
164   return msg.str();
165 }
166
167 std::string Proc::getInPortValue(int nodeNumId, std::string portName)
168 {
169   DEBTRACE("Proc::getInPortValue " << nodeNumId << " " << portName);
170   stringstream msg;
171   if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
172     {
173       msg << "<value><error>unknown node id: " << nodeNumId << "</error></value>";
174       return msg.str();
175     }
176   try
177     {
178       YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
179       InputPort * inputPort = node->getInputPort(portName);
180       return inputPort->dump();
181     }
182   catch(YACS::Exception& ex)
183     {
184       DEBTRACE("Proc::getInPortValue " << ex.what());
185       msg << "<value><error>" << ex.what() << "</error></value>";
186       return msg.str();
187     }
188 }
189
190 std::string Proc::getOutPortValue(int nodeNumId, std::string portName)
191 {
192   DEBTRACE("Proc::getOutPortValue " << nodeNumId << " " << portName);
193   stringstream msg;
194   if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
195     {
196       msg << "<value><error>unknown node id: " << nodeNumId << "</error></value>";
197       return msg.str();
198     }
199   try
200     {
201       YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
202       OutputPort * outputPort = node->getOutputPort(portName);
203       return outputPort->dump();
204     }
205   catch(YACS::Exception& ex)
206     {
207       DEBTRACE("Proc::getOutPortValue " << ex.what());
208       msg << "<value><error>" << ex.what() << "</error></value>";
209       return msg.str();
210     }
211 }
212
213 std::string Proc::getNodeErrorDetails(int nodeNumId)
214 {
215   DEBTRACE("Proc::getNodeErrorDetails " << nodeNumId);
216   stringstream msg;
217   if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
218     {
219       msg << "Unknown node id " << nodeNumId;
220       return msg.str();
221     }
222   YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
223   return node->getErrorDetails();
224 }
225
226 std::string Proc::getNodeErrorReport(int nodeNumId)
227 {
228   DEBTRACE("Proc::getNodeErrorReport " << nodeNumId);
229   stringstream msg;
230   if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
231     {
232       msg << "Unknown node id " << nodeNumId;
233       return msg.str();
234     }
235   YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
236   return node->getErrorReport();
237 }
238
239 std::string Proc::getNodeContainerLog(int nodeNumId)
240 {
241   DEBTRACE("Proc::getNodeContainerLog " << nodeNumId);
242   stringstream msg;
243   if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
244     {
245       msg << "Unknown node id " << nodeNumId;
246       return msg.str();
247     }
248   YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
249   return node->getContainerLog();
250 }
251
252 std::list<int> Proc::getNumIds()
253 {
254   list<YACS::ENGINE::Node *> nodes = getAllRecursiveConstituents();
255   int len = nodes.size();
256   list<int> numids;
257   for( list<YACS::ENGINE::Node *>::const_iterator iter = nodes.begin();
258        iter != nodes.end(); iter++)
259     {
260       numids.push_back((*iter)->getNumId());
261     }
262   numids.push_back(this->getNumId());
263   return numids;
264 }
265
266 std::list<std::string> Proc::getIds()
267 {
268   list<YACS::ENGINE::Node *> nodes = getAllRecursiveConstituents();
269   int len = nodes.size();
270   list<string> ids;
271   for( list<YACS::ENGINE::Node *>::const_iterator iter = nodes.begin();
272        iter != nodes.end(); iter++)
273     {
274       ids.push_back(getChildName(*iter));
275     }
276   ids.push_back("_root_");
277   return ids;
278 }
279
280 Logger *Proc::getLogger(const std::string& name)
281 {
282   Logger* logger;
283   LoggerMap::const_iterator it = _loggers.find(name);
284
285   if (it != _loggers.end())
286   {
287     logger = it->second;
288   }
289   else
290   {
291     logger = new Logger(name);
292     _loggers[name]=logger;
293   }
294   return logger;
295 }
296
297 void Proc::setEdition(bool edition)
298 {
299   DEBTRACE("Proc::setEdition: " << edition);
300   _edition=edition;
301   if(_edition)
302     edUpdateState();
303 }
304 //! Sets Proc in modified state and update state if in edition mode
305 /*!
306  *
307  */
308 void Proc::modified()
309 {
310   DEBTRACE("Proc::modified() " << _edition);
311   _modified=1;
312   if(_edition)
313     edUpdateState();
314 }
315