1 // Copyright (C) 2006-2008 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "InputPort.hxx"
21 #include "OutputPort.hxx"
22 #include "InputDataStreamPort.hxx"
23 #include "OutputDataStreamPort.hxx"
24 #include "LinkInfo.hxx"
25 #include "Runtime.hxx"
26 #include "Visitor.hxx"
31 #include "YacsTrace.hxx"
33 using namespace YACS::ENGINE;
36 InputPort4DF2DS::InputPort4DF2DS(DFToDSForLoop *node, TypeCode* type):
37 InputPort("", node, type),
38 DataPort("", node, type),
43 void InputPort4DF2DS::getAllRepresentants(std::set<InPort *>& repr) const
45 set<InPort *> s=_node->getOutputDataStreamPort("")->edSetInPort();
46 repr.insert(s.begin(),s.end());
49 void *InputPort4DF2DS::get() const throw(Exception)
53 std::string what="InputPort4DF2DS::get : no value currently in input whith name \""; what+=_name; what+="\"";
54 throw Exception(what);
59 void InputPort4DF2DS::exRestoreInit()
69 void InputPort4DF2DS::exSaveInit()
72 _initValue->decrRef();
74 _initValue->incrRef();
77 void InputPort4DF2DS::put(const void *data) throw(ConversionException)
82 InputPort *InputPort4DF2DS::clone(Node *newHelder) const
84 throw Exception("InputPort4DF2DS::clone : internal error");
87 void InputPort4DF2DS::put(Any *data)
95 InputPort4DF2DS::~InputPort4DF2DS()
101 DFToDSForLoop::DFToDSForLoop(Loop *loop, const std::string& name, TypeCode* type):ElementaryNode(""),_nbOfTimeUsed(1)
103 _name="DF2DS For "; _name+=loop->getName(); _name+=" representing port "; _name+=name;
105 _setOfInputPort.push_back(new InputPort4DF2DS(this,type));
106 _setOfOutputDataStreamPort.push_back(new OutputDataStreamPort("",this,Loop::MappingDF2DS(type)));
109 DFToDSForLoop::~DFToDSForLoop()
113 void DFToDSForLoop::getReadyTasks(std::vector<Task *>& tasks)
117 InputPort *DFToDSForLoop::getInputPort(const std::string& name) const throw(Exception)
119 list<InputPort *>::const_iterator it =_setOfInputPort.begin();
123 OutputDataStreamPort *DFToDSForLoop::getOutputDataStreamPort(const std::string& name) const throw(Exception)
125 list<OutputDataStreamPort *>::const_iterator it =_setOfOutputDataStreamPort.begin();
129 void DFToDSForLoop::load()
133 void DFToDSForLoop::execute()
138 Node *DFToDSForLoop::simpleClone(ComposedNode *father, bool editionOnly) const
140 throw Exception("DFToDSForLoop::simpleClone : Internal error");
143 OutputPort4DS2DF::OutputPort4DS2DF(DSToDFForLoop *node, TypeCode *type):
144 OutputPort("", node, type),
145 DataPort("", node, type),
150 void OutputPort4DS2DF::getAllRepresented(std::set<OutPort *>& represented) const
152 set<OutPort *> setO=_node->getInputDataStreamPort("")->edSetOutPort();
153 for(set<OutPort *>::iterator iter=setO.begin();iter!=setO.end();iter++)
154 (*iter)->getAllRepresented(represented);
157 void OutputPort4DS2DF::put(const void *data) throw(ConversionException)
160 OutputPort::put(data);
163 OutputPort *OutputPort4DS2DF::clone(Node *newHelder) const
165 throw Exception("OutputPort4DS2DF::clone : Internal error");
168 void OutputPort4DS2DF::put(Any *data)
176 OutputPort4DS2DF::~OutputPort4DS2DF()
182 InputDataStreamPort4DS2DF::InputDataStreamPort4DS2DF(DSToDFForLoop *node, TypeCode* type):
183 InputDataStreamPort("", node, type),
184 DataPort("", node, type),
189 void InputDataStreamPort4DS2DF::getAllRepresentants(std::set<InPort *>& repr) const
191 set<InPort *> s=_node->getOutputPort("")->edSetInPort();
192 repr.insert(s.begin(),s.end());
195 DSToDFForLoop::DSToDFForLoop(Loop *loop, const std::string& name, TypeCode* type):ElementaryNode(""),_nbOfTimeUsed(1)
197 _name="DS2DF For "; _name+=loop->getName(); _name+=" representing port "; _name+=name;
199 _setOfOutputPort.push_back(new OutputPort4DS2DF(this,type));
200 _setOfInputDataStreamPort.push_back(new InputDataStreamPort4DS2DF(this,type));
203 Node *DSToDFForLoop::simpleClone(ComposedNode *father, bool editionOnly) const
205 throw Exception("DSToDFForLoop::simpleClone : Internal error");
208 DSToDFForLoop::~DSToDFForLoop()
212 void DSToDFForLoop::getReadyTasks(std::vector<Task *>& tasks)
216 OutputPort *DSToDFForLoop::getOutputPort(const std::string& name) const throw(Exception)
218 list<OutputPort *>::const_iterator it = _setOfOutputPort.begin();
222 InputDataStreamPort *DSToDFForLoop::getInputDataStreamPort(const std::string& name) const throw(Exception)
224 list<InputDataStreamPort *>::const_iterator it = _setOfInputDataStreamPort.begin();
228 void DSToDFForLoop::load()
232 void DSToDFForLoop::execute()
237 FakeNodeForLoop::FakeNodeForLoop(Loop *loop, bool normalFinish, bool internalError):ElementaryNode("thisIsAFakeNode"),
239 _normalFinish(normalFinish),
240 _internalError(internalError)
242 setState(YACS::TOACTIVATE);
243 _father=_loop->getFather();
246 FakeNodeForLoop::FakeNodeForLoop(const FakeNodeForLoop& other):ElementaryNode(other),_loop(0),
247 _normalFinish(false),_internalError(true)
251 Node *FakeNodeForLoop::simpleClone(ComposedNode *father, bool editionOnly) const
253 return new FakeNodeForLoop(*this);
256 void FakeNodeForLoop::exForwardFailed()
258 _loop->exForwardFailed();
259 FakeNodeForLoop *normallyThis=_loop->_nodeForNullTurnOfLoops;
260 _loop->_nodeForNullTurnOfLoops=0;
264 void FakeNodeForLoop::exForwardFinished()
266 _loop->exForwardFinished();
267 FakeNodeForLoop *normallyThis=_loop->_nodeForNullTurnOfLoops;
268 _loop->_nodeForNullTurnOfLoops=0;
272 void FakeNodeForLoop::execute()
275 throw Exception("");//only to trigger ABORT on Executor
278 void FakeNodeForLoop::aborted()
281 _loop->setState(YACS::INTERNALERR);
283 _loop->setState(YACS::ERROR);
286 void FakeNodeForLoop::finished()
288 _loop->setState(YACS::DONE);
291 Loop::Loop(const Loop& other, ComposedNode *father, bool editionOnly):StaticDefinedComposedNode(other,father),_nbOfTurns(0),_nodeForNullTurnOfLoops(0),_node(0)
294 _node=other._node->simpleClone(this,editionOnly);
297 Loop::Loop(const std::string& name):StaticDefinedComposedNode(name),_node(0),_nbOfTurns(0),_nodeForNullTurnOfLoops(0)
304 delete _nodeForNullTurnOfLoops;
305 for(set<DSToDFForLoop *>::iterator iter1=_inputsTraducer.begin();iter1!=_inputsTraducer.end();iter1++)
307 for(set<DFToDSForLoop *>::iterator iter2=_outputsTraducer.begin();iter2!=_outputsTraducer.end();iter2++)
311 void Loop::init(bool start)
313 StaticDefinedComposedNode::init(start);
316 _node->init(start); // if start is true, refresh the internal node
318 throw Exception("Loop::initLoop : no nodes specifies to be repeated ");
319 delete _nodeForNullTurnOfLoops;
320 _nodeForNullTurnOfLoops=0;
323 Node *Loop::edSetNode(Node *node)
331 string what = "Loop::edSetNode: node "; what += node->getName(); what += " is not orphan ! ";
332 throw Exception(what);
335 checkNoCrossHierachyWith(node);
336 StaticDefinedComposedNode::edRemoveChild(_node);
340 //set _modified flag so that edUpdateState() can refresh state
345 bool Loop::edAddChild(Node *node) throw(Exception)
347 return edSetNode(node);
350 Node *Loop::edRemoveNode()
352 StaticDefinedComposedNode::edRemoveChild(_node);
359 //! Collect all the child nodes that are ready
361 * \param tasks : vector of tasks to collect ready nodes
363 void Loop::getReadyTasks(std::vector<Task *>& tasks)
368 * To change the way ComposedNode state is handled, uncomment the following line
369 * see Bloc::getReadyTasks
371 if(_state==YACS::TOACTIVATE) setState(YACS::ACTIVATED);
372 if(_state==YACS::TOACTIVATE || _state==YACS::ACTIVATED)
373 if(_nodeForNullTurnOfLoops)
374 _nodeForNullTurnOfLoops->getReadyTasks(tasks);
377 _node->getReadyTasks(tasks);
378 for(set<DSToDFForLoop *>::iterator iter1=_inputsTraducer.begin();iter1!=_inputsTraducer.end();iter1++)
379 (*iter1)->getReadyTasks(tasks);
380 for(set<DFToDSForLoop *>::iterator iter2=_outputsTraducer.begin();iter2!=_outputsTraducer.end();iter2++)
381 (*iter2)->getReadyTasks(tasks);
385 void Loop::edRemoveChild(Node *node) throw(Exception)
387 StaticDefinedComposedNode::edRemoveChild(node);
393 void Loop::selectRunnableTasks(std::vector<Task *>& tasks)
397 std::list<Node *> Loop::edGetDirectDescendants() const
401 ret.push_back(_node);
405 std::list<InputPort *> Loop::getSetOfInputPort() const
407 list<InputPort *> ret=StaticDefinedComposedNode::getSetOfInputPort();
408 ret.push_back(getDecisionPort());
412 int Loop::getNumberOfInputPorts() const
414 return StaticDefinedComposedNode::getNumberOfInputPorts()+1;
417 Node *Loop::getChildByShortName(const std::string& name) const throw(Exception)
420 if(name==_node->getName())
422 string what("node "); what+= name ; what+=" is not a child of loop node "; what += getName();
423 throw Exception(what);
426 TypeCode* Loop::MappingDF2DS(TypeCode* type) throw(Exception)
431 TypeCode* Loop::MappingDS2DF(TypeCode* type) throw(Exception)
436 void Loop::buildDelegateOf(InPort * & port, OutPort *initialStart, const std::list<ComposedNode *>& pointsOfView)
438 string typeOfPortInstance=port->getNameOfTypeOfCurrentInstance();
439 if(typeOfPortInstance!=InputPort::NAME or
440 (typeOfPortInstance == InputPort::NAME and
441 initialStart->getNameOfTypeOfCurrentInstance()== OutputPort::NAME and
442 !isNecessaryToBuildSpecificDelegateDF2DS(pointsOfView)) )
444 InputPort *portCasted=(InputPort *)port;
445 set<DSToDFForLoop*>::iterator iter;
446 //Determinig if a DSToDFForLoop node has already been created for delegation of 'port'
447 for(iter=_inputsTraducer.begin();iter!=_inputsTraducer.end();iter++)
448 if((*iter)->getOutputPort("")->isAlreadyInSet(portCasted))
450 if(iter==_inputsTraducer.end())
451 {//first time that 'port' is delegated on higher level
452 pair<set<DSToDFForLoop*>::iterator, bool> iter2=_inputsTraducer.insert(new DSToDFForLoop(this,portCasted->getName(),Loop::MappingDF2DS(portCasted->edGetType())));
454 (*iter)->getOutputPort("")->addInPort(portCasted);
457 (*iter)->loopHasOneMoreRef();
458 port=(*iter)->getInputDataStreamPort("");
461 void Loop::buildDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView)
463 string typeOfPortInstance=(port.first)->getNameOfTypeOfCurrentInstance();
464 if(typeOfPortInstance!=OutputPort::NAME or
465 ( typeOfPortInstance == OutputPort::NAME and
466 finalTarget->getNameOfTypeOfCurrentInstance()== InputPort::NAME and
467 !isNecessaryToBuildSpecificDelegateDF2DS(pointsOfView)) )
469 OutPort *portCasted=port.first;
470 set<DFToDSForLoop*>::iterator iter;
471 //Determinig if a DFToDSForLoop node has already been created for delegation of 'port'
472 for(iter=_outputsTraducer.begin();iter!=_outputsTraducer.end();iter++)
473 if(portCasted->isAlreadyLinkedWith((*iter)->getInputPort("")))
476 if(iter==_outputsTraducer.end())
477 {//first time that 'port' is delegated on higher level
478 //_outputsTraducer.insert(new DFToDSForLoop(this,portCasted->getName(),portCasted->edGetType()));
479 kl=new DFToDSForLoop(this,portCasted->getName(),portCasted->edGetType());
480 pair<set<DFToDSForLoop*>::iterator, bool> iter2=_outputsTraducer.insert(kl);
482 portCasted->addInPort((*iter)->getInputPort(""));
487 kl->loopHasOneMoreRef();
489 edAddLink(isInMyDescendance((port.first)->getNode())->getOutGate(),kl->getInGate());
490 port.first=(*iter)->getOutputDataStreamPort("");
493 void Loop::getDelegateOf(InPort * & port, OutPort *initialStart, const std::list<ComposedNode *>& pointsOfView) throw(Exception)
495 string typeOfPortInstance=port->getNameOfTypeOfCurrentInstance();
496 if(typeOfPortInstance!=InputPort::NAME or
497 (typeOfPortInstance == InputPort::NAME and
498 initialStart->getNameOfTypeOfCurrentInstance()== OutputPort::NAME and
499 !isNecessaryToBuildSpecificDelegateDF2DS(pointsOfView)) )
501 InputPort *portCasted=(InputPort *)port;
502 set<DSToDFForLoop*>::iterator iter;
503 for(iter=_inputsTraducer.begin();iter!=_inputsTraducer.end();iter++)
504 if((*iter)->getOutputPort("")->isAlreadyInSet(portCasted))
506 if(iter==_inputsTraducer.end())
508 string what("Loop::getDelegateOf : Port with name "); what+=portCasted->getName(); what+=" not exported by loop "; what+=_name;
509 throw Exception(what);
512 port=(*iter)->getInputDataStreamPort("");
515 void Loop::getDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget,
516 const std::list<ComposedNode *>& pointsOfView) throw(Exception)
518 string typeOfPortInstance=(port.first)->getNameOfTypeOfCurrentInstance();
519 if(typeOfPortInstance!=OutputPort::NAME or
520 ( typeOfPortInstance == OutputPort::NAME and
521 finalTarget->getNameOfTypeOfCurrentInstance()== InputPort::NAME and
522 !isNecessaryToBuildSpecificDelegateDF2DS(pointsOfView)) )
524 OutPort *portCasted=port.first;
525 set<DFToDSForLoop*>::iterator iter;
526 for(iter=_outputsTraducer.begin();iter!=_outputsTraducer.end();iter++)
527 if(portCasted->isAlreadyLinkedWith((*iter)->getInputPort("")))
529 if(iter==_outputsTraducer.end())
531 string what("Loop::getDelegateOf : Port with name "); what+=portCasted->getName(); what+=" not exported by loop "; what+=_name;
532 throw Exception(what);
535 port.first=(*iter)->getOutputDataStreamPort("");
538 void Loop::releaseDelegateOf(InPort * & port, OutPort *initialStart, const std::list<ComposedNode *>& pointsOfView) throw(Exception)
540 string typeOfPortInstance=port->getNameOfTypeOfCurrentInstance();
541 if(typeOfPortInstance!=InputPort::NAME or
542 ( typeOfPortInstance == InputPort::NAME and
543 initialStart->getNameOfTypeOfCurrentInstance()== OutputPort::NAME and
544 !isNecessaryToBuildSpecificDelegateDF2DS(pointsOfView)) )
546 InputPort *portCasted=(InputPort *)port;
547 set<DSToDFForLoop*>::iterator iter;
548 for(iter=_inputsTraducer.begin();iter!=_inputsTraducer.end();iter++)
549 if((*iter)->getOutputPort("")->isAlreadyInSet(portCasted))
551 if(iter==_inputsTraducer.end())
553 string what("Loop::releaseDelegateOf Port with name "); what+=portCasted->getName(); what+=" not exported by loop "; what+=_name;
554 throw Exception(what);
558 port=(*iter)->getInputDataStreamPort("");
559 if((*iter)->loopHasOneLessRef())
561 (*iter)->getOutputPort("")->removeInPort(portCasted,false);
563 _inputsTraducer.erase(iter);
568 void Loop::releaseDelegateOf(OutPort *portDwn, OutPort *portUp, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView) throw(Exception)
572 set<DFToDSForLoop*>::iterator iter;
573 for(iter=_outputsTraducer.begin();iter!=_outputsTraducer.end();iter++)
574 if((*iter)->getOutputDataStreamPort("")==portUp)
576 if((*iter)->loopHasOneLessRef())
578 portDwn->removeInPort((*iter)->getInputPort(""),false);
580 _outputsTraducer.erase(iter);
584 void Loop::checkNoCyclePassingThrough(Node *node) throw(Exception)
586 //throw Exception("Loop::checkNoCyclePassingThrough : Internal error occured");
589 void Loop::checkCFLinks(const std::list<OutPort *>& starts, InputPort *end, unsigned char& alreadyFed, bool direction, LinkInfo& info) const
591 Node *nodeEnd=end->getNode();
593 {//In this case 'end' port is a special port of this (for exemple ForLoop::_nbOfTimesPort)
594 //ASSERT(!direction) see Loop::checkControlDependancy (bw only)
595 solveObviousOrDelegateCFLinks(starts,end,alreadyFed,direction,info);
598 StaticDefinedComposedNode::checkCFLinks(starts,end,alreadyFed,direction,info);
602 * \note : States if a DF port must be considered on an upper level in hierarchy as a DS port or not from 'pointsOfView' observers.
604 * - True : a traduction DF->DS has to be done
605 * - False : no traduction needed
607 bool Loop::isNecessaryToBuildSpecificDelegateDF2DS(const std::list<ComposedNode *>& pointsOfView)
610 for(list<ComposedNode *>::const_iterator iter=pointsOfView.begin();iter!=pointsOfView.end() && !ret;iter++)
611 ret=(*iter)->isRepeatedUnpredictablySeveralTimes();
615 //! Connect an OutPort to an InPort and add control link if necessary
617 * Connect the ports with a data link (edAddLink)
618 * In a Loop don't add control flow link : use this only to add data back links
620 * \param start : the OutPort to connect
621 * \param end : the InPort to connect
622 * \return true if a new link has been created, false otherwise.
624 bool Loop::edAddDFLink(OutPort *start, InPort *end) throw(Exception)
626 return edAddLink(start,end);
629 //! Dump the node state to a stream
631 * \param os : the output stream
633 void Loop::writeDot(std::ostream &os) const
635 os << " subgraph cluster_" << getId() << " {\n" ;
636 //only one node in a loop
640 os << getId() << " -> " << _node->getId() << ";\n";
643 os << getId() << "[fillcolor=\"" ;
644 YACS::StatesForNode state=getEffectiveState();
645 os << getColorState(state);
646 os << "\" label=\"" << "Loop:" ;
647 os << getQualifiedName() <<"\"];\n";
651 void Loop::accept(Visitor *visitor)
653 visitor->visitLoop(this);
656 void Loop::checkControlDependancy(OutPort *start, InPort *end, bool cross,
657 std::map < ComposedNode *, std::list < OutPort * >, SortHierarc >& fw,
658 std::vector<OutPort *>& fwCross,
659 std::map< ComposedNode *, std::list < OutPort *>, SortHierarc >& bw,
660 LinkInfo& info) const
662 //First testing if end==getDecisionPort. This is the only case possible in theory.
663 if(end!=getDecisionPort())
664 return StaticDefinedComposedNode::checkControlDependancy(start,end,cross,fw,fwCross,bw,info);
666 throw Exception("Internal error occured - cross type link detected on decision port of a loop. Forbidden !");
667 fw[(ComposedNode *)this].push_back(start);
670 void Loop::checkBasicConsistency() const throw(Exception)
672 DEBTRACE("Loop::checkBasicConsistency");
673 ComposedNode::checkBasicConsistency();
675 throw Exception("For a loop, internal node is mandatory");
679 * For use only when loading a previously saved execution
682 void YACS::ENGINE::NbDoneLoader(Loop* node, int val)
684 node->_nbOfTurns = val;