1 #include "ElementaryNode.hxx"
3 #include "InputPort.hxx"
4 #include "OutputPort.hxx"
5 #include "ComposedNode.hxx"
6 #include "InputDataStreamPort.hxx"
7 #include "OutputDataStreamPort.hxx"
11 using namespace YACS::ENGINE;
14 ElementaryNode::ElementaryNode(const std::string& name):Node(name)
18 ElementaryNode::ElementaryNode(const ElementaryNode& other, ComposedNode *father):Node(other,father)
20 for(list<InputPort *>::const_iterator iter1=other._setOfInputPort.begin();iter1!=other._setOfInputPort.end();iter1++)
21 _setOfInputPort.push_back((InputPort *)(*iter1)->clone(this));
22 for(list<OutputPort *>::const_iterator iter2=other._setOfOutputPort.begin();iter2!=other._setOfOutputPort.end();iter2++)
23 _setOfOutputPort.push_back((OutputPort *)(*iter2)->clone(this));
24 for(list<InputDataStreamPort *>::const_iterator iter3=other._setOfInputDataStreamPort.begin();iter3!=other._setOfInputDataStreamPort.end();iter3++)
25 _setOfInputDataStreamPort.push_back((InputDataStreamPort *)(*iter3)->clone(this));
26 for(list<OutputDataStreamPort *>::const_iterator iter4=other._setOfOutputDataStreamPort.begin();iter4!=other._setOfOutputDataStreamPort.end();iter4++)
27 _setOfOutputDataStreamPort.push_back((OutputDataStreamPort *)(*iter4)->clone(this));
30 void ElementaryNode::performDuplicationOfPlacement(const Node& other)
34 ElementaryNode::~ElementaryNode()
36 for(list<InputPort *>::iterator iter1=_setOfInputPort.begin();iter1!=_setOfInputPort.end();iter1++)
38 for(list<OutputPort *>::iterator iter2=_setOfOutputPort.begin();iter2!=_setOfOutputPort.end();iter2++)
40 for(list<InputDataStreamPort *>::iterator iter3=_setOfInputDataStreamPort.begin();iter3!=_setOfInputDataStreamPort.end();iter3++)
42 for(list<OutputDataStreamPort *>::iterator iter4=_setOfOutputDataStreamPort.begin();iter4!=_setOfOutputDataStreamPort.end();iter4++)
46 void ElementaryNode::init(bool start)
48 for(list<OutputPort *>::iterator iter=_setOfOutputPort.begin();iter!=_setOfOutputPort.end();iter++)
50 for(list<InputPort *>::iterator iter2=_setOfInputPort.begin();iter2!=_setOfInputPort.end();iter2++)
51 (*iter2)->exInit(start);
53 if(_state == YACS::DISABLED)
55 exDisabledState(); // to refresh propagation of DISABLED state
58 if(start) //complete initialization
59 setState(YACS::INITED);
60 else //partial initialization (inside a loop)
64 bool ElementaryNode::isDeployable() const
69 ComponentInstance *ElementaryNode::getComponent()
74 YACS::StatesForNode ElementaryNode::getState() const
76 return Node::getState();
79 void ElementaryNode::exUpdateState()
81 if(_state==YACS::DISABLED)return;
82 if(_inGate.exIsReady())
83 if(areAllInputPortsValid())
84 if(_state == YACS::INITED)
85 setState(YACS::TOLOAD);
87 setState(YACS::TOACTIVATE);
90 string what("ElementaryNode::exUpdateState : Invalid graph given : Node with name \"");
91 what+=_name; what+="\" ready to run whereas some inputports are not set correctly\nCheck coherence DF/CF";
92 setState(YACS::INTERNALERR);
93 throw Exception(what);
97 int ElementaryNode::getNumberOfInputPorts() const
99 return _setOfInputPort.size();
102 int ElementaryNode::getNumberOfOutputPorts() const
104 return _setOfOutputPort.size();
107 InputPort *ElementaryNode::getInputPort(const std::string& name) const throw(Exception)
109 return getPort<InputPort>(name,_setOfInputPort);
112 OutputPort *ElementaryNode::getOutputPort(const std::string& name) const throw(Exception)
114 return getPort<OutputPort>(name,_setOfOutputPort);
117 std::set<OutPort *> ElementaryNode::getAllOutPortsLeavingCurrentScope() const
120 list<OutPort *> temp=getSetOfOutPort();
121 for(list<OutPort *>::iterator iter2=temp.begin();iter2!=temp.end();iter2++)
123 set<InPort *> temp2=(*iter2)->edSetInPort();
130 std::set<InPort *> ElementaryNode::getAllInPortsComingFromOutsideOfCurrentScope() const
133 list<InPort *> temp=getSetOfInPort();
134 for(list<InPort *>::iterator iter2=temp.begin();iter2!=temp.end();iter2++)
136 set<OutPort *> temp2=(*iter2)->edSetOutPort();
143 InputDataStreamPort *ElementaryNode::getInputDataStreamPort(const std::string& name) const throw(Exception)
145 return getPort<InputDataStreamPort>(name,_setOfInputDataStreamPort);
148 OutputDataStreamPort *ElementaryNode::getOutputDataStreamPort(const std::string& name) const throw(Exception)
150 return getPort<OutputDataStreamPort>(name,_setOfOutputDataStreamPort);
153 void ElementaryNode::edDisconnectAllLinksWithMe()
156 Node::edDisconnectAllLinksWithMe();
159 for(list<InputPort *>::iterator iter=_setOfInputPort.begin();iter!=_setOfInputPort.end();iter++)
160 (*iter)->edRemoveAllLinksLinkedWithMe();
162 for(list<InputDataStreamPort *>::iterator iter2=_setOfInputDataStreamPort.begin();iter2!=_setOfInputDataStreamPort.end();iter2++)
163 (*iter2)->edRemoveAllLinksLinkedWithMe();
166 for(list<OutputPort *>::iterator iter=_setOfOutputPort.begin();iter!=_setOfOutputPort.end();iter++)
167 (*iter)->edRemoveAllLinksLinkedWithMe();
169 for(list<OutputDataStreamPort *>::iterator iter2=_setOfOutputDataStreamPort.begin();iter2!=_setOfOutputDataStreamPort.end();iter2++)
170 (*iter2)->edRemoveAllLinksLinkedWithMe();
174 * checks if all input ports contains a valid data. Used at execution to change the state of the node
178 bool ElementaryNode::areAllInputPortsValid() const
181 for(list<InputPort *>::const_iterator iter=_setOfInputPort.begin();iter!=_setOfInputPort.end();iter++)
183 ret=!(*iter)->isEmpty();
190 * add this node task to a given set of ready tasks, if this task is ready to activate
193 void ElementaryNode::getReadyTasks(std::vector<Task *>& tasks)
195 if(_state==YACS::TOACTIVATE or _state==YACS::TOLOAD)
196 tasks.push_back(this);
200 * remove port from node at edition. Ports are typed.
203 void ElementaryNode::edRemovePort(Port *port) throw(Exception)
205 if(port->getNode()!=this)
206 throw Exception("ElementaryNode::edRemovePort : Port is not held by this");
207 string typeOfPortInstance=port->getNameOfTypeOfCurrentInstance();
208 if(typeOfPortInstance==InputPort::NAME)
209 edRemovePortTypedFromSet<InputPort>(dynamic_cast<InputPort *>(port),_setOfInputPort);
210 else if(typeOfPortInstance==OutputPort::NAME)
211 edRemovePortTypedFromSet<OutputPort>(dynamic_cast<OutputPort *>(port),_setOfOutputPort);
212 else if(typeOfPortInstance==InputDataStreamPort::NAME)
213 edRemovePortTypedFromSet<InputDataStreamPort>(dynamic_cast<InputDataStreamPort *>(port),_setOfInputDataStreamPort);
214 else if(typeOfPortInstance==OutputDataStreamPort::NAME)
215 edRemovePortTypedFromSet<OutputDataStreamPort>(dynamic_cast<OutputDataStreamPort *>(port),_setOfOutputDataStreamPort);
217 throw Exception("ElementaryNode::edRemovePort : unknown port type");
222 * @return a set with only this node. (Same method in composed nodes)
225 set<ElementaryNode *> ElementaryNode::getRecursiveConstituents() const
227 set<ElementaryNode *> ret;
228 ret.insert((ElementaryNode *)this);
232 Node *ElementaryNode::getChildByName(const std::string& name) const throw(Exception)
234 string what("ElementaryNode does not agregate any nodes particullary node with name "); what+=name;
235 throw Exception(what);
238 void ElementaryNode::checkBasicConsistency() const throw(Exception)
240 list<InputPort *>::const_iterator iter;
241 for(iter=_setOfInputPort.begin();iter!=_setOfInputPort.end();iter++)
242 (*iter)->checkBasicConsistency();
245 ComposedNode *ElementaryNode::getDynClonerIfExists(const ComposedNode *levelToStop) const
247 for(ComposedNode *iter=_father;iter!=levelToStop && iter!=0; iter=iter->_father)
248 if(!iter->isPlacementPredictableB4Run())
254 * the input port is also published recursively in ancestors because it may be visible from everywhere.
255 * WARNING: CHECK CASE OF BLOC: ONLY INPUT PORTS NOT INTERNALLY CONNECTED MUST BE VISIBLE.
258 InputPort *ElementaryNode::edAddInputPort(const std::string& inputPortName, TypeCode* type) throw(Exception)
261 if (edCheckAddPort<InputPort, TypeCode*>(inputPortName,_setOfInputPort,type))
263 ret = getRuntime()->createInputPort(inputPortName, _implementation, this, type);
264 _setOfInputPort.push_back(ret);
265 ComposedNode *iter=_father;
273 * TO SOLVE : The output port is not published in father. Father must create an output port.
274 * for now, publication is done the same way as input ports
277 OutputPort *ElementaryNode::edAddOutputPort(const std::string& outputPortName, TypeCode* type) throw(Exception)
280 if (edCheckAddPort<OutputPort, TypeCode*>(outputPortName,_setOfOutputPort,type))
282 ret = getRuntime()->createOutputPort(outputPortName, _implementation, this, type);
283 _setOfOutputPort.push_back(ret);
284 ComposedNode *iter=_father;
291 InputDataStreamPort *ElementaryNode::edAddInputDataStreamPort(const std::string& inputPortDSName, TypeCode* type) throw(Exception)
293 InputDataStreamPort *ret = 0;
294 if (edCheckAddPort<InputDataStreamPort, TypeCode*>(inputPortDSName,_setOfInputDataStreamPort,type))
296 ret = getRuntime()->createInputDataStreamPort(inputPortDSName, this, type);
297 _setOfInputDataStreamPort.push_back(ret);
302 OutputDataStreamPort *ElementaryNode::edAddOutputDataStreamPort(const std::string& outputPortDSName, TypeCode* type) throw(Exception)
304 OutputDataStreamPort *ret = 0;
305 if (edCheckAddPort<OutputDataStreamPort, TypeCode*>(outputPortDSName,_setOfOutputDataStreamPort,type))
307 ret = getRuntime()->createOutputDataStreamPort(outputPortDSName, this, type);
308 _setOfOutputDataStreamPort.push_back(ret);
314 * get the input port name used by the current node (see composed nodes)
317 string ElementaryNode::getInPortName(const InPort * inPort) const throw (Exception)
319 Node *node = inPort->getNode();
322 string what("InputPort "); what += inPort->getName(); what += " does not belong to node "; what += node->getName();
323 throw Exception(what);
325 return inPort->getName();
328 string ElementaryNode::getOutPortName(const OutPort *outPort) const throw (Exception)
330 Node *node = outPort->getNode();
333 string what("OutputPort "); what += outPort->getName(); what += " does not belong to node "; what += node->getName();
334 throw Exception(what);
336 return outPort->getName();
339 void ElementaryNode::begin()
344 bool ElementaryNode::isReady()
346 return _state==TOACTIVATE;
349 void ElementaryNode::finished()
353 void ElementaryNode::aborted()
358 //! Notify this node that it is loaded
360 * When an elementary node has been loaded it goes to TOACTIVATE state
361 * It is then ready to be executed
364 void ElementaryNode::loaded()
366 setState(TOACTIVATE);
369 void ElementaryNode::accept(Visitor *visitor)
371 visitor->visitElementaryNode(this);