1 // Copyright (C) 2006-2021 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, or (at your option) any later version.
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
21 #include "InputPort.hxx"
22 #include "OutputPort.hxx"
23 #include "InputDataStreamPort.hxx"
24 #include "OutputDataStreamPort.hxx"
25 #include "LinkInfo.hxx"
26 #include "Runtime.hxx"
27 #include "Visitor.hxx"
32 #include "YacsTrace.hxx"
34 using namespace YACS::ENGINE;
37 InputPort4DF2DS::InputPort4DF2DS(DFToDSForLoop *node, TypeCode* type):
38 InputPort("", node, type),
39 DataPort("", node, type),
44 void InputPort4DF2DS::getAllRepresentants(std::set<InPort *>& repr) const
46 set<InPort *> s=_node->getOutputDataStreamPort("")->edSetInPort();
47 repr.insert(s.begin(),s.end());
50 void *InputPort4DF2DS::get() const
54 std::string what="InputPort4DF2DS::get : no value currently in input whith name \""; what+=_name; what+="\"";
55 throw Exception(what);
60 void InputPort4DF2DS::exRestoreInit()
70 void InputPort4DF2DS::exSaveInit()
73 _initValue->decrRef();
75 _initValue->incrRef();
78 void InputPort4DF2DS::put(const void *data)
83 InputPort *InputPort4DF2DS::clone(Node *newHelder) const
85 throw Exception("InputPort4DF2DS::clone : internal error");
88 void InputPort4DF2DS::releaseData()
95 void InputPort4DF2DS::put(Any *data)
97 InputPort4DF2DS::releaseData();
102 InputPort4DF2DS::~InputPort4DF2DS()
108 DFToDSForLoop::DFToDSForLoop(Loop *loop, const std::string& name, TypeCode* type):ElementaryNode(""),_nbOfTimeUsed(1)
110 _name="DF2DS For "; _name+=loop->getName(); _name+=" representing port "; _name+=name;
112 _setOfInputPort.push_back(new InputPort4DF2DS(this,type));
113 _setOfOutputDataStreamPort.push_back(new OutputDataStreamPort("",this,Loop::MappingDF2DS(type)));
116 DFToDSForLoop::~DFToDSForLoop()
118 edDisconnectAllLinksWithMe();
121 void DFToDSForLoop::getReadyTasks(std::vector<Task *>& tasks)
125 InputPort *DFToDSForLoop::getInputPort(const std::string& name) const
128 return Node::getInputPort(name);
130 catch(Exception& e) {}
132 list<InputPort *>::const_iterator it =_setOfInputPort.begin();
136 OutputDataStreamPort *DFToDSForLoop::getOutputDataStreamPort(const std::string& name) const
138 list<OutputDataStreamPort *>::const_iterator it =_setOfOutputDataStreamPort.begin();
142 void DFToDSForLoop::load()
146 void DFToDSForLoop::execute()
151 Node *DFToDSForLoop::simpleClone(ComposedNode *father, bool editionOnly) const
153 throw Exception("DFToDSForLoop::simpleClone : Internal error");
156 OutputPort4DS2DF::OutputPort4DS2DF(DSToDFForLoop *node, TypeCode *type):
157 OutputPort("", node, type),
158 DataPort("", node, type),
163 void OutputPort4DS2DF::getAllRepresented(std::set<OutPort *>& represented) const
165 set<OutPort *> setO=_node->getInputDataStreamPort("")->edSetOutPort();
166 for(set<OutPort *>::iterator iter=setO.begin();iter!=setO.end();iter++)
167 (*iter)->getAllRepresented(represented);
170 void OutputPort4DS2DF::put(const void *data)
173 OutputPort::put(data);
176 OutputPort *OutputPort4DS2DF::clone(Node *newHelder) const
178 throw Exception("OutputPort4DS2DF::clone : Internal error");
181 void OutputPort4DS2DF::put(Any *data)
189 OutputPort4DS2DF::~OutputPort4DS2DF()
195 InputDataStreamPort4DS2DF::InputDataStreamPort4DS2DF(DSToDFForLoop *node, TypeCode* type):
196 InputDataStreamPort("", node, type),
197 DataPort("", node, type),
202 void InputDataStreamPort4DS2DF::getAllRepresentants(std::set<InPort *>& repr) const
204 set<InPort *> s=_node->getOutputPort("")->edSetInPort();
205 repr.insert(s.begin(),s.end());
208 DSToDFForLoop::DSToDFForLoop(Loop *loop, const std::string& name, TypeCode* type):ElementaryNode(""),_nbOfTimeUsed(1)
210 _name="DS2DF For "; _name+=loop->getName(); _name+=" representing port "; _name+=name;
212 _setOfOutputPort.push_back(new OutputPort4DS2DF(this,type));
213 _setOfInputDataStreamPort.push_back(new InputDataStreamPort4DS2DF(this,type));
216 Node *DSToDFForLoop::simpleClone(ComposedNode *father, bool editionOnly) const
218 throw Exception("DSToDFForLoop::simpleClone : Internal error");
221 DSToDFForLoop::~DSToDFForLoop()
223 edDisconnectAllLinksWithMe();
226 void DSToDFForLoop::getReadyTasks(std::vector<Task *>& tasks)
230 OutputPort *DSToDFForLoop::getOutputPort(const std::string& name) const
232 list<OutputPort *>::const_iterator it = _setOfOutputPort.begin();
236 InputDataStreamPort *DSToDFForLoop::getInputDataStreamPort(const std::string& name) const
238 list<InputDataStreamPort *>::const_iterator it = _setOfInputDataStreamPort.begin();
242 void DSToDFForLoop::load()
246 void DSToDFForLoop::execute()
251 FakeNodeForLoop::FakeNodeForLoop(Loop *loop, bool normalFinish, bool internalError):ElementaryNode("thisIsAFakeNode"),
253 _normalFinish(normalFinish),
254 _internalError(internalError)
256 setState(YACS::TOACTIVATE);
257 _father=_loop->getFather();
260 FakeNodeForLoop::FakeNodeForLoop(const FakeNodeForLoop& other):ElementaryNode(other),_loop(0),
261 _normalFinish(false),_internalError(true)
265 Node *FakeNodeForLoop::simpleClone(ComposedNode *father, bool editionOnly) const
267 return new FakeNodeForLoop(*this);
270 void FakeNodeForLoop::exForwardFailed()
272 _loop->exForwardFailed();
275 void FakeNodeForLoop::exForwardFinished()
277 _loop->exForwardFinished();
280 void FakeNodeForLoop::execute()
283 throw Exception("");//only to trigger ABORT on Executor
286 void FakeNodeForLoop::aborted()
289 _loop->setState(YACS::INTERNALERR);
291 _loop->setState(YACS::ERROR);
294 void FakeNodeForLoop::finished()
296 _loop->setState(YACS::DONE);
299 Loop::Loop(const Loop& other, ComposedNode *father, bool editionOnly):StaticDefinedComposedNode(other,father),_nbOfTurns(0),_nodeForNullTurnOfLoops(0),_node(0)
302 _node=other._node->simpleClone(this,editionOnly);
305 Loop::Loop(const std::string& name):StaticDefinedComposedNode(name),_node(0),_nbOfTurns(0),_nodeForNullTurnOfLoops(0)
312 if(_nodeForNullTurnOfLoops)delete _nodeForNullTurnOfLoops;
313 for(set<DSToDFForLoop *>::iterator iter1=_inputsTraducer.begin();iter1!=_inputsTraducer.end();iter1++)
315 for(set<DFToDSForLoop *>::iterator iter2=_outputsTraducer.begin();iter2!=_outputsTraducer.end();iter2++)
319 void Loop::init(bool start)
321 StaticDefinedComposedNode::init(start);
324 _node->init(start); // if start is true, refresh the internal node
326 throw Exception("Loop::initLoop : no nodes specifies to be repeated ");
327 delete _nodeForNullTurnOfLoops;
328 _nodeForNullTurnOfLoops=0;
331 Node *Loop::edSetNode(Node *node)
339 string what = "Loop::edSetNode: node "; what += node->getName(); what += " is not orphan ! ";
340 throw Exception(what);
343 checkNoCrossHierachyWith(node);
344 StaticDefinedComposedNode::edRemoveChild(_node);
348 //set _modified flag so that edUpdateState() can refresh state
353 bool Loop::edAddChild(Node *node)
355 return edSetNode(node);
358 Node *Loop::edRemoveNode()
360 StaticDefinedComposedNode::edRemoveChild(_node);
367 //! Collect all the child nodes that are ready
369 * \param tasks : vector of tasks to collect ready nodes
371 void Loop::getReadyTasks(std::vector<Task *>& tasks)
376 * To change the way ComposedNode state is handled, uncomment the following line
377 * see Bloc::getReadyTasks
379 if(_state==YACS::TOACTIVATE) setState(YACS::ACTIVATED);
380 if(_state==YACS::TOACTIVATE || _state==YACS::ACTIVATED)
381 if(_nodeForNullTurnOfLoops)
382 _nodeForNullTurnOfLoops->getReadyTasks(tasks);
385 _node->getReadyTasks(tasks);
386 for(set<DSToDFForLoop *>::iterator iter1=_inputsTraducer.begin();iter1!=_inputsTraducer.end();iter1++)
387 (*iter1)->getReadyTasks(tasks);
388 for(set<DFToDSForLoop *>::iterator iter2=_outputsTraducer.begin();iter2!=_outputsTraducer.end();iter2++)
389 (*iter2)->getReadyTasks(tasks);
393 void Loop::edRemoveChild(Node *node)
395 StaticDefinedComposedNode::edRemoveChild(node);
401 std::list<Node *> Loop::edGetDirectDescendants() const
405 ret.push_back(_node);
409 std::list<InputPort *> Loop::getSetOfInputPort() const
411 list<InputPort *> ret=StaticDefinedComposedNode::getSetOfInputPort();
412 ret.push_back(getDecisionPort());
416 int Loop::getNumberOfInputPorts() const
418 return StaticDefinedComposedNode::getNumberOfInputPorts()+1;
421 int Loop::getMaxLevelOfParallelism() const
425 return _node->getMaxLevelOfParallelism();
428 void Loop::getWeightRegardingDPL(ComplexWeight *weight)
431 _node->getWeightRegardingDPL(weight);
434 void Loop::partitionRegardingDPL(const PartDefinition *pd, std::map<ComposedNode *, YACS::BASES::AutoRefCnt<PartDefinition> >& zeMap)
437 _node->partitionRegardingDPL(pd,zeMap);
440 Node *Loop::getChildByShortName(const std::string& name) const
443 if(name==_node->getName())
445 string what("node "); what+= name ; what+=" is not a child of loop node "; what += getName();
446 throw Exception(what);
449 TypeCode* Loop::MappingDF2DS(TypeCode* type)
454 TypeCode* Loop::MappingDS2DF(TypeCode* type)
459 void Loop::buildDelegateOf(InPort * & port, OutPort *initialStart, const std::list<ComposedNode *>& pointsOfView)
461 string typeOfPortInstance=port->getNameOfTypeOfCurrentInstance();
462 if(typeOfPortInstance!=InputPort::NAME ||
463 (typeOfPortInstance == InputPort::NAME &&
464 initialStart->getNameOfTypeOfCurrentInstance()== OutputPort::NAME &&
465 !isNecessaryToBuildSpecificDelegateDF2DS(pointsOfView)) )
467 InputPort *portCasted=(InputPort *)port;
468 set<DSToDFForLoop*>::iterator iter;
469 //Determinig if a DSToDFForLoop node has already been created for delegation of 'port'
470 for(iter=_inputsTraducer.begin();iter!=_inputsTraducer.end();iter++)
471 if((*iter)->getOutputPort("")->isAlreadyInSet(portCasted))
473 if(iter==_inputsTraducer.end())
474 {//first time that 'port' is delegated on higher level
475 pair<set<DSToDFForLoop*>::iterator, bool> iter2=_inputsTraducer.insert(new DSToDFForLoop(this,portCasted->getName(),Loop::MappingDF2DS(portCasted->edGetType())));
477 (*iter)->getOutputPort("")->addInPort(portCasted);
480 (*iter)->loopHasOneMoreRef();
481 port=(*iter)->getInputDataStreamPort("");
484 void Loop::buildDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView)
486 string typeOfPortInstance=(port.first)->getNameOfTypeOfCurrentInstance();
487 if(typeOfPortInstance!=OutputPort::NAME ||
488 ( typeOfPortInstance == OutputPort::NAME &&
489 finalTarget->getNameOfTypeOfCurrentInstance()== InputPort::NAME &&
490 !isNecessaryToBuildSpecificDelegateDF2DS(pointsOfView)) )
492 OutPort *portCasted=port.first;
493 set<DFToDSForLoop*>::iterator iter;
494 //Determinig if a DFToDSForLoop node has already been created for delegation of 'port'
495 for(iter=_outputsTraducer.begin();iter!=_outputsTraducer.end();iter++)
496 if(portCasted->isAlreadyLinkedWith((*iter)->getInputPort("")))
499 if(iter==_outputsTraducer.end())
500 {//first time that 'port' is delegated on higher level
501 //_outputsTraducer.insert(new DFToDSForLoop(this,portCasted->getName(),portCasted->edGetType()));
502 kl=new DFToDSForLoop(this,portCasted->getName(),portCasted->edGetType());
503 pair<set<DFToDSForLoop*>::iterator, bool> iter2=_outputsTraducer.insert(kl);
505 portCasted->addInPort((*iter)->getInputPort(""));
510 kl->loopHasOneMoreRef();
512 edAddLink(isInMyDescendance((port.first)->getNode())->getOutGate(),kl->getInGate());
513 port.first=(*iter)->getOutputDataStreamPort("");
516 void Loop::getDelegateOf(InPort * & port, OutPort *initialStart, const std::list<ComposedNode *>& pointsOfView)
518 string typeOfPortInstance=port->getNameOfTypeOfCurrentInstance();
519 if(typeOfPortInstance!=InputPort::NAME ||
520 (typeOfPortInstance == InputPort::NAME &&
521 initialStart->getNameOfTypeOfCurrentInstance()== OutputPort::NAME &&
522 !isNecessaryToBuildSpecificDelegateDF2DS(pointsOfView)) )
524 InputPort *portCasted=(InputPort *)port;
525 set<DSToDFForLoop*>::iterator iter;
526 for(iter=_inputsTraducer.begin();iter!=_inputsTraducer.end();iter++)
527 if((*iter)->getOutputPort("")->isAlreadyInSet(portCasted))
529 if(iter==_inputsTraducer.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=(*iter)->getInputDataStreamPort("");
538 void Loop::getDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget,
539 const std::list<ComposedNode *>& pointsOfView)
541 string typeOfPortInstance=(port.first)->getNameOfTypeOfCurrentInstance();
542 if(typeOfPortInstance!=OutputPort::NAME ||
543 ( typeOfPortInstance == OutputPort::NAME &&
544 finalTarget->getNameOfTypeOfCurrentInstance()== InputPort::NAME &&
545 !isNecessaryToBuildSpecificDelegateDF2DS(pointsOfView)) )
547 OutPort *portCasted=port.first;
548 set<DFToDSForLoop*>::iterator iter;
549 for(iter=_outputsTraducer.begin();iter!=_outputsTraducer.end();iter++)
550 if(portCasted->isAlreadyLinkedWith((*iter)->getInputPort("")))
552 if(iter==_outputsTraducer.end())
554 string what("Loop::getDelegateOf : Port with name "); what+=portCasted->getName(); what+=" not exported by loop "; what+=_name;
555 throw Exception(what);
558 port.first=(*iter)->getOutputDataStreamPort("");
561 void Loop::releaseDelegateOf(InPort * & port, OutPort *initialStart, const std::list<ComposedNode *>& pointsOfView)
563 string typeOfPortInstance=port->getNameOfTypeOfCurrentInstance();
564 if(typeOfPortInstance!=InputPort::NAME ||
565 ( typeOfPortInstance == InputPort::NAME &&
566 initialStart->getNameOfTypeOfCurrentInstance()== OutputPort::NAME &&
567 !isNecessaryToBuildSpecificDelegateDF2DS(pointsOfView)) )
569 InputPort *portCasted=(InputPort *)port;
570 set<DSToDFForLoop*>::iterator iter;
571 for(iter=_inputsTraducer.begin();iter!=_inputsTraducer.end();iter++)
572 if((*iter)->getOutputPort("")->isAlreadyInSet(portCasted))
574 if(iter==_inputsTraducer.end())
576 string what("Loop::releaseDelegateOf Port with name "); what+=portCasted->getName(); what+=" not exported by loop "; what+=_name;
577 throw Exception(what);
581 port=(*iter)->getInputDataStreamPort("");
582 if((*iter)->loopHasOneLessRef())
584 (*iter)->getOutputPort("")->removeInPort(portCasted,false);
586 _inputsTraducer.erase(iter);
591 void Loop::releaseDelegateOf(OutPort *portDwn, OutPort *portUp, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView)
595 set<DFToDSForLoop*>::iterator iter;
596 for(iter=_outputsTraducer.begin();iter!=_outputsTraducer.end();iter++)
597 if((*iter)->getOutputDataStreamPort("")==portUp)
599 if((*iter)->loopHasOneLessRef())
601 portDwn->removeInPort((*iter)->getInputPort(""),false);
603 _outputsTraducer.erase(iter);
607 void Loop::checkNoCyclePassingThrough(Node *node)
609 //throw Exception("Loop::checkNoCyclePassingThrough : Internal error occured");
612 void Loop::checkCFLinks(const std::list<OutPort *>& starts, InputPort *end, unsigned char& alreadyFed, bool direction, LinkInfo& info) const
614 Node *nodeEnd=end->getNode();
616 {//In this case 'end' port is a special port of this (for exemple ForLoop::_nbOfTimesPort)
617 //ASSERT(!direction) see Loop::checkControlDependancy (bw only)
618 solveObviousOrDelegateCFLinks(starts,end,alreadyFed,direction,info);
621 StaticDefinedComposedNode::checkCFLinks(starts,end,alreadyFed,direction,info);
625 * \note : States if a DF port must be considered on an upper level in hierarchy as a DS port or not from 'pointsOfView' observers.
627 * - True : a traduction DF->DS has to be done
628 * - False : no traduction needed
630 bool Loop::isNecessaryToBuildSpecificDelegateDF2DS(const std::list<ComposedNode *>& pointsOfView)
633 for(list<ComposedNode *>::const_iterator iter=pointsOfView.begin();iter!=pointsOfView.end() && !ret;iter++)
634 ret=(*iter)->isRepeatedUnpredictablySeveralTimes();
638 //! Connect an OutPort to an InPort and add control link if necessary
640 * Connect the ports with a data link (edAddLink)
641 * In a Loop don't add control flow link : use this only to add data back links
643 * \param start : the OutPort to connect
644 * \param end : the InPort to connect
645 * \return true if a new link has been created, false otherwise.
647 bool Loop::edAddDFLink(OutPort *start, InPort *end)
649 return edAddLink(start,end);
652 //! Dump the node state to a stream
654 * \param os : the output stream
656 void Loop::writeDot(std::ostream &os) const
658 os << " subgraph cluster_" << getId() << " {\n" ;
659 //only one node in a loop
663 os << getId() << " -> " << _node->getId() << ";\n";
666 os << getId() << "[fillcolor=\"" ;
667 YACS::StatesForNode state=getEffectiveState();
668 os << getColorState(state);
669 os << "\" label=\"" << "Loop:" ;
670 os << getQualifiedName() <<"\"];\n";
674 void Loop::accept(Visitor *visitor)
676 visitor->visitLoop(this);
679 void Loop::checkControlDependancy(OutPort *start, InPort *end, bool cross,
680 std::map < ComposedNode *, std::list < OutPort * >, SortHierarc >& fw,
681 std::vector<OutPort *>& fwCross,
682 std::map< ComposedNode *, std::list < OutPort *>, SortHierarc >& bw,
683 LinkInfo& info) const
685 //First testing if end==getDecisionPort. This is the only case possible in theory.
686 if(end!=getDecisionPort())
687 return StaticDefinedComposedNode::checkControlDependancy(start,end,cross,fw,fwCross,bw,info);
689 throw Exception("Internal error occured - cross type link detected on decision port of a loop. Forbidden !");
690 fw[(ComposedNode *)this].push_back(start);
693 void Loop::checkBasicConsistency() const
695 DEBTRACE("Loop::checkBasicConsistency");
696 ComposedNode::checkBasicConsistency();
698 throw Exception("For a loop, internal node is mandatory");
702 * For use only when loading a previously saved execution
705 void YACS::ENGINE::NbDoneLoader(Loop* node, int val)
707 node->_nbOfTurns = val;