2 #include "ElementaryNode.hxx"
4 #include "Container.hxx"
5 #include "InputPort.hxx"
6 #include "OutputPort.hxx"
7 #include "TypeCode.hxx"
14 #include "YacsTrace.hxx"
17 using namespace YACS::ENGINE;
19 Proc::Proc(const std::string& name):Bloc(name),_edition(false)
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;
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();
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();
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++)
60 void Proc::writeDot(std::ostream &os) const
62 os << "digraph " << getQualifiedName() << " {\n" ;
63 os << "node [ style=\"filled\" ];\n" ;
64 os << "compound=true;";
69 std::ostream& operator<< (std::ostream& os, const Proc& p)
75 TypeCode *Proc::createType(const std::string& name, const std::string& kind)
79 t=getRuntime()->_tc_double;
80 else if(kind=="string")
81 t=getRuntime()->_tc_string;
83 t=getRuntime()->_tc_int;
85 t=getRuntime()->_tc_bool;
87 throw Exception("Unknown kind");
93 TypeCode *Proc::createInterfaceTc(const std::string& id, const std::string& name,
94 std::list<TypeCodeObjref *> ltc)
96 return TypeCode::interfaceTc(id.c_str(),name.c_str(),ltc);
99 TypeCode * Proc::createSequenceTc (const std::string& id, const std::string& name,
102 return TypeCode::sequenceTc(id.c_str(),name.c_str(),content);
105 TypeCode * Proc::createStructTc (const std::string& id, const std::string& name)
107 return TypeCode::structTc(id.c_str(),name.c_str());
110 TypeCode * Proc::getTypeCode (const std::string& name)
112 if(typeMap.count(name)==0)
114 std::stringstream msg;
115 msg << "Type " << name << " does not exist" ;
116 msg << " (" <<__FILE__ << ":" << __LINE__ << ")";
117 throw Exception(msg.str());
119 return typeMap[name];
122 void Proc::setTypeCode (const std::string& name,TypeCode *t)
124 if(typeMap.count(name)!=0)
125 typeMap[name]->decrRef();
130 void Proc::accept(Visitor *visitor)
132 visitor->visitProc(this);
135 void Proc::setName(const std::string& name)
140 YACS::StatesForNode Proc::getNodeState(int numId)
142 if(YACS::ENGINE::Node::idMap.count(numId) == 0)
144 cerr << "Unknown node id " << numId << endl;
145 return YACS::UNDEFINED;
147 YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[numId];
148 YACS::StatesForNode state = node->getEffectiveState();
152 std::string Proc::getXMLState(int numId)
154 if(YACS::ENGINE::Node::idMap.count(numId) == 0)
156 cerr << "Unknown node id " << numId << endl;
157 return "<state>unknown</state>";
159 YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[numId];
161 msg << "<state>" << node->getEffectiveState() << "</state>";
162 msg << "<name>" << node->getQualifiedName() << "</name>";
163 msg << "<id>" << numId << "</id>";
167 std::string Proc::getInPortValue(int nodeNumId, std::string portName)
169 DEBTRACE("Proc::getInPortValue " << nodeNumId << " " << portName);
171 if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
173 msg << "<value><error>unknown node id: " << nodeNumId << "</error></value>";
178 YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
179 InputPort * inputPort = node->getInputPort(portName);
180 return inputPort->dump();
182 catch(YACS::Exception& ex)
184 DEBTRACE("Proc::getInPortValue " << ex.what());
185 msg << "<value><error>" << ex.what() << "</error></value>";
190 std::string Proc::getOutPortValue(int nodeNumId, std::string portName)
192 DEBTRACE("Proc::getOutPortValue " << nodeNumId << " " << portName);
194 if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
196 msg << "<value><error>unknown node id: " << nodeNumId << "</error></value>";
201 YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
202 OutputPort * outputPort = node->getOutputPort(portName);
203 return outputPort->dump();
205 catch(YACS::Exception& ex)
207 DEBTRACE("Proc::getOutPortValue " << ex.what());
208 msg << "<value><error>" << ex.what() << "</error></value>";
213 std::string Proc::getNodeErrorDetails(int nodeNumId)
215 DEBTRACE("Proc::getNodeErrorDetails " << nodeNumId);
217 if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
219 msg << "Unknown node id " << nodeNumId;
222 YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
223 return node->getErrorDetails();
226 std::string Proc::getNodeErrorReport(int nodeNumId)
228 DEBTRACE("Proc::getNodeErrorReport " << nodeNumId);
230 if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
232 msg << "Unknown node id " << nodeNumId;
235 YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
236 return node->getErrorReport();
239 std::string Proc::getNodeContainerLog(int nodeNumId)
241 DEBTRACE("Proc::getNodeContainerLog " << nodeNumId);
243 if(YACS::ENGINE::Node::idMap.count(nodeNumId) == 0)
245 msg << "Unknown node id " << nodeNumId;
248 YACS::ENGINE::Node* node = YACS::ENGINE::Node::idMap[nodeNumId];
249 return node->getContainerLog();
252 std::list<int> Proc::getNumIds()
254 list<YACS::ENGINE::Node *> nodes = getAllRecursiveConstituents();
255 int len = nodes.size();
257 for( list<YACS::ENGINE::Node *>::const_iterator iter = nodes.begin();
258 iter != nodes.end(); iter++)
260 numids.push_back((*iter)->getNumId());
262 numids.push_back(this->getNumId());
266 std::list<std::string> Proc::getIds()
268 list<YACS::ENGINE::Node *> nodes = getAllRecursiveConstituents();
269 int len = nodes.size();
271 for( list<YACS::ENGINE::Node *>::const_iterator iter = nodes.begin();
272 iter != nodes.end(); iter++)
274 ids.push_back(getChildName(*iter));
276 ids.push_back("_root_");
280 Logger *Proc::getLogger(const std::string& name)
283 LoggerMap::const_iterator it = _loggers.find(name);
285 if (it != _loggers.end())
291 logger = new Logger(name);
292 _loggers[name]=logger;
297 void Proc::setEdition(bool edition)
299 DEBTRACE("Proc::setEdition: " << edition);
304 //! Sets Proc in modified state and update state if in edition mode
308 void Proc::modified()
310 DEBTRACE("Proc::modified() " << _edition);