2 #include "InputPort.hxx"
3 #include "OutputPort.hxx"
4 #include "InputDataStreamPort.hxx"
5 #include "OutputDataStreamPort.hxx"
11 using namespace YACS::ENGINE;
14 InputPort4DF2DS::InputPort4DF2DS(DFToDSForLoop *node, TypeCode* type):
15 InputPort("", node, type),
16 DataPort("", node, type),
21 void InputPort4DF2DS::getAllRepresentants(std::set<InPort *>& repr) const
23 set<InPort *> s=_node->getOutputDataStreamPort("")->edSetInPort();
24 repr.insert(s.begin(),s.end());
27 void *InputPort4DF2DS::get() const throw(Exception)
31 std::string what="InputPort4DF2DS::get : no value currently in input whith name \""; what+=_name; what+="\"";
32 throw Exception(what);
37 void InputPort4DF2DS::exRestoreInit()
47 void InputPort4DF2DS::exSaveInit()
50 _initValue->decrRef();
52 _initValue->incrRef();
55 void InputPort4DF2DS::put(const void *data) throw(ConversionException)
60 InputPort *InputPort4DF2DS::clone(Node *newHelder) const
62 throw Exception("InputPort4DF2DS::clone : internal error");
65 void InputPort4DF2DS::put(Any *data)
73 InputPort4DF2DS::~InputPort4DF2DS()
79 DFToDSForLoop::DFToDSForLoop(Loop *loop, const std::string& name, TypeCode* type):ElementaryNode(""),_nbOfTimeUsed(1)
81 _name="DF2DS For "; _name+=loop->getName(); _name+=" representing port "; _name+=name;
83 _setOfInputPort.push_back(new InputPort4DF2DS(this,type));
84 _setOfOutputDataStreamPort.push_back(new OutputDataStreamPort("",this,Loop::MappingDF2DS(type)));
87 DFToDSForLoop::~DFToDSForLoop()
91 void DFToDSForLoop::getReadyTasks(std::vector<Task *>& tasks)
95 InputPort *DFToDSForLoop::getInputPort(const std::string& name) const throw(Exception)
97 list<InputPort *>::const_iterator it =_setOfInputPort.begin();
101 OutputDataStreamPort *DFToDSForLoop::getOutputDataStreamPort(const std::string& name) const throw(Exception)
103 list<OutputDataStreamPort *>::const_iterator it =_setOfOutputDataStreamPort.begin();
107 void DFToDSForLoop::load()
111 void DFToDSForLoop::execute()
116 Node *DFToDSForLoop::simpleClone(ComposedNode *father, bool editionOnly) const
118 throw Exception("DFToDSForLoop::simpleClone : Internal error");
121 OutputPort4DS2DF::OutputPort4DS2DF(DSToDFForLoop *node, TypeCode *type):
122 OutputPort("", node, type),
123 DataPort("", node, type),
128 void OutputPort4DS2DF::getAllRepresented(std::set<OutPort *>& represented) const
130 set<OutPort *> setO=_node->getInputDataStreamPort("")->edSetOutPort();
131 for(set<OutPort *>::iterator iter=setO.begin();iter!=setO.end();iter++)
132 (*iter)->getAllRepresented(represented);
135 void OutputPort4DS2DF::put(const void *data) throw(ConversionException)
138 OutputPort::put(data);
141 OutputPort *OutputPort4DS2DF::clone(Node *newHelder) const
143 throw Exception("OutputPort4DS2DF::clone : Internal error");
146 void OutputPort4DS2DF::put(Any *data)
154 OutputPort4DS2DF::~OutputPort4DS2DF()
160 InputDataStreamPort4DS2DF::InputDataStreamPort4DS2DF(DSToDFForLoop *node, TypeCode* type):
161 InputDataStreamPort("", node, type),
162 DataPort("", node, type),
167 void InputDataStreamPort4DS2DF::getAllRepresentants(std::set<InPort *>& repr) const
169 set<InPort *> s=_node->getOutputPort("")->edSetInPort();
170 repr.insert(s.begin(),s.end());
173 DSToDFForLoop::DSToDFForLoop(Loop *loop, const std::string& name, TypeCode* type):ElementaryNode(""),_nbOfTimeUsed(1)
175 _name="DS2DF For "; _name+=loop->getName(); _name+=" representing port "; _name+=name;
177 _setOfOutputPort.push_back(new OutputPort4DS2DF(this,type));
178 _setOfInputDataStreamPort.push_back(new InputDataStreamPort4DS2DF(this,type));
181 Node *DSToDFForLoop::simpleClone(ComposedNode *father, bool editionOnly) const
183 throw Exception("DSToDFForLoop::simpleClone : Internal error");
186 DSToDFForLoop::~DSToDFForLoop()
190 void DSToDFForLoop::getReadyTasks(std::vector<Task *>& tasks)
194 OutputPort *DSToDFForLoop::getOutputPort(const std::string& name) const throw(Exception)
196 list<OutputPort *>::const_iterator it = _setOfOutputPort.begin();
200 InputDataStreamPort *DSToDFForLoop::getInputDataStreamPort(const std::string& name) const throw(Exception)
202 list<InputDataStreamPort *>::const_iterator it = _setOfInputDataStreamPort.begin();
206 void DSToDFForLoop::load()
210 void DSToDFForLoop::execute()
215 FakeNodeForLoop::FakeNodeForLoop(Loop *loop, bool normalFinish, bool internalError):ElementaryNode("thisIsAFakeNode"),
217 _normalFinish(normalFinish),
218 _internalError(internalError)
220 setState(YACS::TOACTIVATE);
221 _father=_loop->getFather();
224 FakeNodeForLoop::FakeNodeForLoop(const FakeNodeForLoop& other):ElementaryNode(other),_loop(0),
225 _normalFinish(false),_internalError(true)
229 Node *FakeNodeForLoop::simpleClone(ComposedNode *father, bool editionOnly) const
231 return new FakeNodeForLoop(*this);
234 void FakeNodeForLoop::exForwardFailed()
236 _loop->exForwardFailed();
237 FakeNodeForLoop *normallyThis=_loop->_nodeForNullTurnOfLoops;
238 _loop->_nodeForNullTurnOfLoops=0;
242 void FakeNodeForLoop::exForwardFinished()
244 _loop->exForwardFinished();
245 FakeNodeForLoop *normallyThis=_loop->_nodeForNullTurnOfLoops;
246 _loop->_nodeForNullTurnOfLoops=0;
250 void FakeNodeForLoop::execute()
253 throw Exception("");//only to trigger ABORT on Executor
256 void FakeNodeForLoop::aborted()
259 _loop->setState(YACS::INTERNALERR);
261 _loop->setState(YACS::ERROR);
264 void FakeNodeForLoop::finished()
266 _loop->setState(YACS::DONE);
269 Loop::Loop(const Loop& other, ComposedNode *father, bool editionOnly):StaticDefinedComposedNode(other,father),_nbOfTurns(0),_nodeForNullTurnOfLoops(0)
272 _node=other._node->simpleClone(this,editionOnly);
275 Loop::Loop(const std::string& name):StaticDefinedComposedNode(name),_node(0),_nbOfTurns(0),_nodeForNullTurnOfLoops(0)
282 delete _nodeForNullTurnOfLoops;
283 for(set<DSToDFForLoop *>::iterator iter1=_inputsTraducer.begin();iter1!=_inputsTraducer.end();iter1++)
285 for(set<DFToDSForLoop *>::iterator iter2=_outputsTraducer.begin();iter2!=_outputsTraducer.end();iter2++)
289 void Loop::init(bool start)
291 StaticDefinedComposedNode::init(start);
294 _node->init(start); // if start is true, refresh the internal node
296 throw Exception("Loop::initLoop : no nodes specifies to be repeated ");
297 delete _nodeForNullTurnOfLoops;
298 _nodeForNullTurnOfLoops=0;
301 Node *Loop::edSetNode(Node *node)
309 string what = "Loop::edSetNode: node "; what += node->getName(); what += " is not orphan ! ";
310 throw Exception(what);
313 StaticDefinedComposedNode::edRemoveChild(_node);
320 Node *Loop::edRemoveNode()
322 StaticDefinedComposedNode::edRemoveChild(_node);
328 //! Collect all the child nodes that are ready
330 * \param tasks : vector of tasks to collect ready nodes
332 void Loop::getReadyTasks(std::vector<Task *>& tasks)
337 * To change the way ComposedNode state is handled, uncomment the following line
338 * see Bloc::getReadyTasks
340 if(_state==YACS::TOACTIVATE) setState(YACS::ACTIVATED);
341 if(_state==YACS::TOACTIVATE || _state==YACS::ACTIVATED)
342 if(_nodeForNullTurnOfLoops)
343 _nodeForNullTurnOfLoops->getReadyTasks(tasks);
346 _node->getReadyTasks(tasks);
347 for(set<DSToDFForLoop *>::iterator iter1=_inputsTraducer.begin();iter1!=_inputsTraducer.end();iter1++)
348 (*iter1)->getReadyTasks(tasks);
349 for(set<DFToDSForLoop *>::iterator iter2=_outputsTraducer.begin();iter2!=_outputsTraducer.end();iter2++)
350 (*iter2)->getReadyTasks(tasks);
354 void Loop::edRemoveChild(Node *node) throw(Exception)
356 StaticDefinedComposedNode::edRemoveChild(node);
361 void Loop::selectRunnableTasks(std::vector<Task *>& tasks)
365 std::set<Node *> Loop::edGetDirectDescendants() const
373 void Loop::checkConsistency(ComposedNode *pointOfView) const throw(Exception)
377 Node *Loop::getChildByShortName(const std::string& name) const throw(Exception)
379 if(name==_node->getName())
381 string what("node "); what+= name ; what+=" is not a child of loop node "; what += getName();
382 throw Exception(what);
385 TypeCode* Loop::MappingDF2DS(TypeCode* type) throw(Exception)
390 TypeCode* Loop::MappingDS2DF(TypeCode* type) throw(Exception)
395 void Loop::buildDelegateOf(InPort * & port, OutPort *initialStart, const std::set<ComposedNode *>& pointsOfView)
397 string typeOfPortInstance=port->getNameOfTypeOfCurrentInstance();
398 if(typeOfPortInstance!=InputPort::NAME or
399 (typeOfPortInstance == InputPort::NAME and
400 initialStart->getNameOfTypeOfCurrentInstance()== OutputPort::NAME and
401 !isNecessaryToBuildSpecificDelegateDF2DS(pointsOfView)) )
403 InputPort *portCasted=(InputPort *)port;
404 set<DSToDFForLoop*>::iterator iter;
405 //Determinig if a DSToDFForLoop node has already been created for delegation of 'port'
406 for(iter=_inputsTraducer.begin();iter!=_inputsTraducer.end();iter++)
407 if((*iter)->getOutputPort("")->isAlreadyInSet(portCasted))
409 if(iter==_inputsTraducer.end())
410 {//first time that 'port' is delegated on higher level
411 pair<set<DSToDFForLoop*>::iterator, bool> iter2=_inputsTraducer.insert(new DSToDFForLoop(this,portCasted->getName(),Loop::MappingDF2DS(portCasted->edGetType())));
413 (*iter)->getOutputPort("")->addInPort(portCasted);
416 (*iter)->loopHasOneMoreRef();
417 port=(*iter)->getInputDataStreamPort("");
420 void Loop::buildDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::set<ComposedNode *>& pointsOfView)
422 string typeOfPortInstance=(port.first)->getNameOfTypeOfCurrentInstance();
423 if(typeOfPortInstance!=OutputPort::NAME or
424 ( typeOfPortInstance == OutputPort::NAME and
425 finalTarget->getNameOfTypeOfCurrentInstance()== InputPort::NAME and
426 !isNecessaryToBuildSpecificDelegateDF2DS(pointsOfView)) )
428 OutPort *portCasted=port.first;
429 set<DFToDSForLoop*>::iterator iter;
430 //Determinig if a DFToDSForLoop node has already been created for delegation of 'port'
431 for(iter=_outputsTraducer.begin();iter!=_outputsTraducer.end();iter++)
432 if(portCasted->isAlreadyLinkedWith((*iter)->getInputPort("")))
435 if(iter==_outputsTraducer.end())
436 {//first time that 'port' is delegated on higher level
437 //_outputsTraducer.insert(new DFToDSForLoop(this,portCasted->getName(),portCasted->edGetType()));
438 kl=new DFToDSForLoop(this,portCasted->getName(),portCasted->edGetType());
439 pair<set<DFToDSForLoop*>::iterator, bool> iter2=_outputsTraducer.insert(kl);
441 portCasted->addInPort((*iter)->getInputPort(""));
446 kl->loopHasOneMoreRef();
448 edAddLink(isInMyDescendance((port.first)->getNode())->getOutGate(),kl->getInGate());
449 port.first=(*iter)->getOutputDataStreamPort("");
452 void Loop::getDelegateOf(InPort * & port, OutPort *initialStart, const std::set<ComposedNode *>& pointsOfView) throw(Exception)
454 string typeOfPortInstance=port->getNameOfTypeOfCurrentInstance();
455 if(typeOfPortInstance!=InputPort::NAME or
456 (typeOfPortInstance == InputPort::NAME and
457 initialStart->getNameOfTypeOfCurrentInstance()== OutputPort::NAME and
458 !isNecessaryToBuildSpecificDelegateDF2DS(pointsOfView)) )
460 InputPort *portCasted=(InputPort *)port;
461 set<DSToDFForLoop*>::iterator iter;
462 for(iter=_inputsTraducer.begin();iter!=_inputsTraducer.end();iter++)
463 if((*iter)->getOutputPort("")->isAlreadyInSet(portCasted))
465 if(iter==_inputsTraducer.end())
467 string what("Loop::getDelegateOf : Port with name "); what+=portCasted->getName(); what+=" not exported by loop "; what+=_name;
468 throw Exception(what);
471 port=(*iter)->getInputDataStreamPort("");
474 void Loop::getDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget,
475 const std::set<ComposedNode *>& pointsOfView) throw(Exception)
477 string typeOfPortInstance=(port.first)->getNameOfTypeOfCurrentInstance();
478 if(typeOfPortInstance!=OutputPort::NAME or
479 ( typeOfPortInstance == OutputPort::NAME and
480 finalTarget->getNameOfTypeOfCurrentInstance()== InputPort::NAME and
481 !isNecessaryToBuildSpecificDelegateDF2DS(pointsOfView)) )
483 OutPort *portCasted=port.first;
484 set<DFToDSForLoop*>::iterator iter;
485 for(iter=_outputsTraducer.begin();iter!=_outputsTraducer.end();iter++)
486 if(portCasted->isAlreadyLinkedWith((*iter)->getInputPort("")))
488 if(iter==_outputsTraducer.end())
490 string what("Loop::getDelegateOf : Port with name "); what+=portCasted->getName(); what+=" not exported by loop "; what+=_name;
491 throw Exception(what);
494 port.first=(*iter)->getOutputDataStreamPort("");
497 void Loop::releaseDelegateOf(InPort * & port, OutPort *initialStart, const std::set<ComposedNode *>& pointsOfView) throw(Exception)
499 string typeOfPortInstance=port->getNameOfTypeOfCurrentInstance();
500 if(typeOfPortInstance!=InputPort::NAME or
501 ( typeOfPortInstance == InputPort::NAME and
502 initialStart->getNameOfTypeOfCurrentInstance()== OutputPort::NAME and
503 !isNecessaryToBuildSpecificDelegateDF2DS(pointsOfView)) )
505 InputPort *portCasted=(InputPort *)port;
506 set<DSToDFForLoop*>::iterator iter;
507 for(iter=_inputsTraducer.begin();iter!=_inputsTraducer.end();iter++)
508 if((*iter)->getOutputPort("")->isAlreadyInSet(portCasted))
510 if(iter==_inputsTraducer.end())
512 string what("Loop::releaseDelegateOf Port with name "); what+=portCasted->getName(); what+=" not exported by loop "; what+=_name;
513 throw Exception(what);
517 port=(*iter)->getInputDataStreamPort("");
518 if((*iter)->loopHasOneLessRef())
520 (*iter)->getOutputPort("")->removeInPort(portCasted,false);
522 _inputsTraducer.erase(iter);
527 void Loop::releaseDelegateOf(OutPort *portDwn, OutPort *portUp, InPort *finalTarget, const std::set<ComposedNode *>& pointsOfView) throw(Exception)
531 set<DFToDSForLoop*>::iterator iter;
532 for(iter=_outputsTraducer.begin();iter!=_outputsTraducer.end();iter++)
533 if((*iter)->getOutputDataStreamPort("")==portUp)
535 if((*iter)->loopHasOneLessRef())
537 portDwn->removeInPort((*iter)->getInputPort(""),false);
539 _outputsTraducer.erase(iter);
543 void Loop::checkNoCyclePassingThrough(Node *node) throw(Exception)
545 //throw Exception("Loop::checkNoCyclePassingThrough : Internal error occured");
549 * \note : States if a DF port must be considered on an upper level in hierarchy as a DS port or not from 'pointsOfView' observers.
551 * - True : a traduction DF->DS has to be done
552 * - False : no traduction needed
554 bool Loop::isNecessaryToBuildSpecificDelegateDF2DS(const std::set<ComposedNode *>& pointsOfView)
557 for(set<ComposedNode *>::const_iterator iter=pointsOfView.begin();iter!=pointsOfView.end() && !ret;iter++)
558 ret=(*iter)->isRepeatedUnpredictablySeveralTimes();
562 //! Connect an OutPort to an InPort and add control link if necessary
564 * Connect the ports with a data link (edAddLink)
565 * In a Loop don't add control flow link : use this only to add data back links
567 * \param start : the OutPort to connect
568 * \param end : the InPort to connect
569 * \return true if a new link has been created, false otherwise.
571 bool Loop::edAddDFLink(OutPort *start, InPort *end) throw(Exception)
573 return edAddLink(start,end);
576 //! Dump the node state to a stream
578 * \param os : the output stream
580 void Loop::writeDot(std::ostream &os)
582 os << " subgraph cluster_" << getId() << " {\n" ;
583 //only one node in a loop
585 os << getId() << " -> " << _node->getId() << ";\n";
587 os << getId() << "[fillcolor=\"" ;
588 YACS::StatesForNode state=getEffectiveState();
589 os << getColorState(state);
590 os << "\" label=\"" << "Loop:" ;
591 os << getQualifiedName() <<"\"];\n";
595 void Loop::accept(Visitor *visitor)
597 visitor->visitLoop(this);
601 * For use only when loading a previously saved execution
604 void YACS::ENGINE::NbDoneLoader(Loop* node, int val)
606 node->_nbOfTurns = val;