Salome HOME
07f1e6be66017bca8783d22fbed9783cc32cfd3a
[modules/yacs.git] / src / engine / Bloc.cxx
1 // Copyright (C) 2006-2015  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "Bloc.hxx"
21 #include "LinkInfo.hxx"
22 #include "InputPort.hxx"
23 #include "InputDataStreamPort.hxx"
24 #include "OutputPort.hxx"
25 #include "OutputDataStreamPort.hxx"
26 #include "ElementaryNode.hxx"
27 #include "Visitor.hxx"
28 #include "SetOfPoints.hxx"
29
30 #include <queue>
31 #include <iostream>
32 #include <numeric>
33
34 //#define _DEVDEBUG_
35 #include "YacsTrace.hxx"
36
37 using namespace YACS::ENGINE;
38 using namespace std;
39
40 /*! \class YACS::ENGINE::Bloc
41  *  \brief Composed node to group elementary and composed nodes
42  *
43  * \ingroup Nodes
44  */
45
46 Bloc::Bloc(const Bloc& other, ComposedNode *father, bool editionOnly):StaticDefinedComposedNode(other,father),_fwLinks(0),_bwLinks(0)
47 {
48   for(list<Node *>::const_iterator iter=other._setOfNode.begin();iter!=other._setOfNode.end();iter++)
49     _setOfNode.push_back((*iter)->simpleClone(this,editionOnly));
50
51   //CF Linking
52   vector< pair<OutGate *, InGate *> > cfLinksToReproduce=other.getSetOfInternalCFLinks();
53   vector< pair<OutGate *, InGate *> >::iterator iter1=cfLinksToReproduce.begin();
54   for(;iter1!=cfLinksToReproduce.end();iter1++)
55     edAddCFLink(getChildByName(other.getChildName((*iter1).first->getNode())),getChildByName(other.getChildName((*iter1).second->getNode())));
56
57   //Data + DataStream linking
58   vector< pair<OutPort *, InPort *> > linksToReproduce=other.getSetOfInternalLinks();
59   vector< pair<OutPort *, InPort *> >::iterator iter2=linksToReproduce.begin();
60   for(;iter2!=linksToReproduce.end();iter2++)
61     {
62       OutPort* pout = iter2->first;
63       InPort* pin = iter2->second;
64       if(&other == getLowestCommonAncestor(pout->getNode(), pin->getNode()))
65       {
66         edAddLink(getOutPort(other.getPortName(pout)),getInPort(other.getPortName(pin)));
67       }
68     }
69 }
70
71 //! Create a Bloc node with a given name
72 /*!
73  *   \param name : the given name
74  */
75 Bloc::Bloc(const std::string& name):StaticDefinedComposedNode(name),_fwLinks(0),_bwLinks(0)
76 {
77 }
78
79 Bloc::~Bloc()
80 {
81   for(list<Node *>::iterator iter=_setOfNode.begin();iter!=_setOfNode.end();iter++)
82     delete *iter;
83   delete _fwLinks;
84   delete _bwLinks;
85 }
86
87 //! Initialize the bloc
88 /*!
89  * \param start : a boolean flag indicating the kind of initialization
90  * If start is true, it's a complete initialization with reinitialization of port values
91  * If start is false, there is no initialization of port values
92  */
93 void Bloc::init(bool start)
94 {
95   Node::init(start);
96   for(list<Node *>::iterator iter=_setOfNode.begin();iter!=_setOfNode.end();iter++)
97     (*iter)->init(start);
98 }
99
100 //! Indicate if the bloc execution is finished
101 /*!
102  * The execution bloc is finished if all its child nodes
103  * are finished with or without error or if it is disabled (not to execute)
104  */
105 bool Bloc::isFinished()
106 {
107     if(_state==YACS::DONE)return true;
108     if(_state==YACS::ERROR)return true;
109     if(_state==YACS::FAILED)return true;
110     if(_state==YACS::DISABLED)return true;
111     return false;
112 }
113
114 int Bloc::getNumberOfCFLinks() const
115 {
116   int ret=0;
117   for(list<Node *>::const_iterator iter=_setOfNode.begin();iter!=_setOfNode.end();iter++)
118     ret+=(*iter)->getOutGate()->getNbOfInGatesConnected();
119   return ret;
120 }
121
122 Node *Bloc::simpleClone(ComposedNode *father, bool editionOnly) const
123 {
124   return new Bloc(*this,father,editionOnly);
125 }
126
127 //! Collect all nodes that are ready to execute
128 /*!
129  * \param tasks : vector of tasks to collect ready nodes
130  */
131 void Bloc::getReadyTasks(std::vector<Task *>& tasks)
132 {
133   /*
134    * ComposedNode state goes to ACTIVATED when one of its child has been ACTIVATED
135    * To change this uncomment the following line
136    * Then the father node will go to ACTIVATED state before its child node
137    */
138   if(_state==YACS::TOACTIVATE ) setState(YACS::ACTIVATED);
139   if(_state==YACS::TOACTIVATE || _state==YACS::ACTIVATED)
140     for(list<Node *>::iterator iter=_setOfNode.begin();iter!=_setOfNode.end();iter++)
141       (*iter)->getReadyTasks(tasks);
142 }
143
144 //! Update the bloc state
145 /*!
146  * Update the '_state' attribute.
147  * Typically called by 'this->_inGate' when 'this->_inGate' is ready. 
148  * Contrary to Node::exUpdateState no check done on inputs
149  * because internal linked DF inputports are not valid yet.
150  */
151 void Bloc::exUpdateState()
152 {
153   if(_state == YACS::DISABLED)return;
154   if(_state == YACS::DONE)return;
155   if(_inGate.exIsReady())
156     {
157       setState(YACS::ACTIVATED);
158       for(list<Node *>::iterator iter=_setOfNode.begin();iter!=_setOfNode.end();iter++)
159         if((*iter)->exIsControlReady())
160           (*iter)->exUpdateState();
161     }
162 }
163
164 //! Add a child node to the bloc
165 /*!
166  * \param node: the node to add to the bloc
167  * \return a boolean flag indicating if the node has been added
168  *
169  * If node is already a direct child of current bloc, do nothing.
170  * If node is a child of another bloc, throw exception.
171  * If node name already used in bloc, throw exception.
172  * Publish inputPorts in current bloc and ancestors.
173  */
174 bool Bloc::edAddChild(Node *node) throw(YACS::Exception)
175 {
176   if(isNodeAlreadyAggregated(node))
177     {
178       if(node->_father==this)
179         return false;
180       else
181         {
182           string what = "Bloc::edAddChild : node "; what += node->getName();
183           what += " is already grand children of node";
184           throw Exception(what);
185         }
186     }
187
188   if(node->_father)
189     {
190       string what = "Bloc::edAddChild: node is not orphan: "; what += node->getName();
191       throw Exception(what);
192     }
193   
194   checkNoCrossHierachyWith(node);
195
196   if(isNameAlreadyUsed(node->getName()))
197     {
198       string what("Bloc::edAddChild : name "); what+=node->getName(); 
199       what+=" already exists in the scope of "; what+=_name;
200       throw Exception(what);
201     }
202   
203   node->_father=this;
204   _setOfNode.push_back(node);
205   //should we also set _modified flag for node ??
206   ComposedNode *iter=node->_father;
207   //set the _modified flag so that latter on edUpdateState (eventually called by isValid) refresh state
208   //better call it at end
209   modified();
210   return true;
211 }
212
213 /**
214  * Remove 'node' from the set of direct children.
215  * @exception If 'node' is NOT the son of 'this'.
216  */
217
218 void Bloc::edRemoveChild(Node *node) throw(YACS::Exception)
219 {
220   StaticDefinedComposedNode::edRemoveChild(node);
221   list<Node *>::iterator iter=find(_setOfNode.begin(),_setOfNode.end(),node);
222   if(iter!=_setOfNode.end())
223     {
224       _setOfNode.erase(iter);
225       modified();
226     }
227 }
228
229 std::vector< std::list<Node *> > Bloc::splitIntoIndependantGraph() const
230 {
231   std::size_t sz(_setOfNode.size());
232   list<Node *>::const_iterator it=_setOfNode.begin();
233   for(;it!=_setOfNode.end();it++)
234     (*it)->_colour=White;
235   it=_setOfNode.begin();
236   std::vector< list<Node *> > ret;
237   while(it!=_setOfNode.end())
238     {
239       Node *start(*it); start->_colour=Grey;
240       ret.push_back(list<Node *>());
241       list<Node *>& ll(ret.back());
242       std::queue<Node *> fifo; fifo.push(start);
243       while(!fifo.empty())
244         {
245           Node *cur(fifo.front()); fifo.pop();
246           ll.push_back(cur);
247           //
248           OutGate *og(cur->getOutGate());
249           list<InGate *> og2(og->edSetInGate());
250           for(list<InGate *>::const_iterator it2=og2.begin();it2!=og2.end();it2++)
251             {
252               Node *cur2((*it2)->getNode());
253               if(cur2->_colour==White)
254                 {
255                   cur2->_colour=Grey;
256                   fifo.push(cur2);
257                 }
258             }
259           //
260           InGate *ig(cur->getInGate());
261           list<OutGate *> bl(ig->getBackLinks());
262           for(list<OutGate *>::const_iterator it3=bl.begin();it3!=bl.end();it3++)
263             {
264               Node *cur3((*it3)->getNode());
265               if(cur3->_colour==White)
266                 {
267                   cur3->_colour=Grey;
268                   fifo.push(cur3);
269                 }
270             }
271         }
272       for(it=_setOfNode.begin();it!=_setOfNode.end() && (*it)->_colour!=White;it++);
273     }
274   return ret;
275 }
276
277 Node *Bloc::getChildByShortName(const std::string& name) const throw(YACS::Exception)
278 {
279   for (list<Node *>::const_iterator iter = _setOfNode.begin(); iter != _setOfNode.end(); iter++)
280     if ((*iter)->getName() == name)
281       return (*iter);
282   string what("node "); what+= name ; what+=" is not a child of Bloc "; what += getName();
283   throw Exception(what);
284 }
285
286 bool Bloc::areAllSubNodesDone() const
287 {
288   for(list<Node *>::const_iterator iter=_setOfNode.begin();iter!=_setOfNode.end();iter++)
289     {
290       if((*iter)->_state == YACS::DONE)continue;
291       if((*iter)->_state == YACS::DISABLED)continue;
292       return false;
293     }
294   return true;
295 }
296
297 bool Bloc::areAllSubNodesFinished() const
298 {
299   for(list<Node *>::const_iterator iter=_setOfNode.begin();iter!=_setOfNode.end();iter++)
300     {
301       if((*iter)->_state == YACS::DONE)continue;
302       if((*iter)->_state == YACS::FAILED)continue;
303       if((*iter)->_state == YACS::DISABLED)continue;
304       if((*iter)->_state == YACS::ERROR)continue;
305       if((*iter)->_state == YACS::INTERNALERR)continue;
306       return false;
307     }
308   return true;
309 }
310
311 bool Bloc::isNameAlreadyUsed(const std::string& name) const
312 {
313   for(list<Node *>::const_iterator iter=_setOfNode.begin();iter!=_setOfNode.end();iter++)
314     if((*iter)->getName()==name)
315       return true;
316   return false;
317 }
318
319 bool insertNodeChildrenInSet(Node *node, std::set<Node *>& nodeSet)
320 {
321   bool verdict=true;
322   set<Node *> outNodes=node->getOutNodes();
323   for (set<Node *>::iterator iter=outNodes.begin();iter!=outNodes.end(); iter++)
324     {
325       verdict=(nodeSet.insert(*iter)).second;
326       if (verdict) verdict = insertNodeChildrenInSet((*iter),nodeSet);
327     }
328   return verdict;
329 }
330
331 /*!
332  * \note  Checks that in the forest from 'node' there are NO back-edges.
333  *        \b WARNING : When using this method 'node' has to be checked in order to be part of direct children of 'this'. 
334  *
335  */
336 void Bloc::checkNoCyclePassingThrough(Node *node) throw(YACS::Exception)
337 {
338   set<Node *> currentNodesToTest;
339   //don't insert node to test in set. 
340   //If it is present after insertion of connected nodes we have a loop
341   //collect all connected nodes
342   insertNodeChildrenInSet(node,currentNodesToTest);
343   //try to insert node
344   if(!(currentNodesToTest.insert(node)).second)
345     throw Exception("Cycle has been detected",1);
346 }
347
348 std::vector< std::pair<OutGate *, InGate *> > Bloc::getSetOfInternalCFLinks() const
349 {
350   vector< pair<OutGate *, InGate *> > ret;
351   for(list<Node *>::const_iterator iter=_setOfNode.begin();iter!=_setOfNode.end();iter++)
352     {
353       list<InGate *> outCFLinksOfCurNode=(*iter)->_outGate.edSetInGate();
354       for(list<InGate *>::iterator iter2=outCFLinksOfCurNode.begin();iter2!=outCFLinksOfCurNode.end();iter2++)
355         ret.push_back(pair<OutGate *, InGate *>(&(*iter)->_outGate,*iter2));
356     }
357   return ret;
358 }
359
360 /*!
361  *
362  * @note : Runtime called method. Indirectly called by StaticDefinedComposedNode::updateStateFrom which has dispatch to this method
363  *          'when event == FINISH'.
364  *          WARNING Precondition : '_state == Running' and 'node->_father==this'(garanteed by StaticDefinedComposedNode::notifyFrom)
365  *
366  * Calls the node's outgate OutGate::exNotifyDone if all nodes are not finished
367  */
368 YACS::Event Bloc::updateStateOnFinishedEventFrom(Node *node)
369 {
370   DEBTRACE("Bloc::updateStateOnFinishedEventFrom: " << node->getName());
371   //ASSERT(node->_father==this)
372   if(areAllSubNodesFinished())
373     {
374       setState(YACS::DONE);
375       if(!areAllSubNodesDone())
376         {
377           setState(YACS::FAILED);
378           return YACS::ABORT;
379         }
380       return YACS::FINISH;//notify to father node that 'this' has becomed finished.
381     }
382   //more job to do in 'this' bloc
383   //Conversion exceptions can be thrown so catch them to control errors
384   try
385     {
386       //notify the finished node to propagate to its following nodes
387       node->exForwardFinished();
388     }
389   catch(YACS::Exception& ex)
390     {
391       //The node has failed to propagate. It must be put in error
392       DEBTRACE("Bloc::updateStateOnFinishedEventFrom: " << ex.what());
393       // notify the node it has failed
394       node->exForwardFailed();
395       setState(YACS::FAILED);
396       return YACS::ABORT;
397     }
398   return YACS::NOEVENT;//no notification to father needed because from father point of view nothing happened.
399 }
400
401 //! Notify this bloc that a node has failed
402 /*!
403  * \param node : node that has emitted the event
404  * \return the event to notify to bloc's father
405  */
406 YACS::Event Bloc::updateStateOnFailedEventFrom(Node *node, const Executor *execInst)
407 {
408   node->exForwardFailed();
409   if(areAllSubNodesFinished())
410     {
411       setState(YACS::DONE);
412       if(!areAllSubNodesDone()){
413           setState(YACS::FAILED);
414           return YACS::ABORT;
415       }
416       return YACS::FINISH;//notify to father node that 'this' has becomed finished.
417     }
418   return YACS::NOEVENT;
419 }
420
421 void Bloc::writeDot(std::ostream &os) const
422 {
423     os << "  subgraph cluster_" << getId() << "  {\n" ;
424     list<Node *>nodes=getChildren();
425     for(list<Node *>::const_iterator iter=nodes.begin();iter!=nodes.end();iter++)
426     {
427         (*iter)->writeDot(os);
428         string p=(*iter)->getId();
429         //not connected node
430         if((*iter)->_inGate._backLinks.size() == 0) os << getId() << " -> " << p << ";\n";
431         set<Node *>outnodes = (*iter)->getOutNodes();
432         for(set<Node *>::const_iterator itout=outnodes.begin();itout!=outnodes.end();itout++)
433         {
434             os << p << " -> " << (*itout)->getId() << ";\n";
435         }
436     }
437     os << "}\n" ;
438     os << getId() << "[fillcolor=\"" ;
439     YACS::StatesForNode state=getEffectiveState();
440     os << getColorState(state);
441     os << "\" label=\"" << "Bloc:" ;
442     os << getQualifiedName() <<"\"];\n";
443 }
444
445 void Bloc::accept(Visitor* visitor)
446 {
447   visitor->visitBloc(this);
448 }
449
450 /*!
451  * Returns the max level of parallelism is this. The max of parallelism is equal to the sum of the max parallelism level
452  * for all concurrent branches in \a this.
453  */
454 int Bloc::getMaxLevelOfParallelism() const
455 {
456   std::vector< std::list<Node *> > r(splitIntoIndependantGraph());
457   int ret(0);
458   for(std::vector< std::list<Node *> >::const_iterator it=r.begin();it!=r.end();it++)
459     {
460       SetOfPoints sop(*it);
461       sop.simplify();
462       ret+=sop.getMaxLevelOfParallelism();
463     }
464   return ret;
465 }
466
467 /*!
468  * Updates mutable structures _fwLinks and _bwLinks with the result of computation (CPU consuming method).
469  * _fwLinks is a map with a Node* as key and a set<Node*> as value. The set gives
470  * all nodes that are forwardly connected to the key node 
471  * _bwLinks is a map for backward dependencies
472  * The method is : for all CF link (n1->n2) 
473  * add n2 and _fwLinks[n2] in forward dependencies of n1 and _bwLinks[n1]
474  * add n1 and _bwLinks[n1] in backward dependencies of n2 and _fwLinks[n2]
475  * For useless links
476  * If a node is already in a forward dependency when adding and the direct link
477  * already exists so it's a useless link (see the code !)
478  */
479 void Bloc::performCFComputations(LinkInfo& info) const
480 {
481   StaticDefinedComposedNode::performCFComputations(info);
482   delete _fwLinks;//Normally useless
483   delete _bwLinks;//Normally useless
484   _fwLinks=new map<Node *,set<Node *> >;
485   _bwLinks=new map<Node *,set<Node *> >;
486
487   //a set to store all CF links : used to find fastly if two nodes are connected
488   std::set< std::pair< Node*, Node* > > links;
489
490   for(list<Node *>::const_iterator iter=_setOfNode.begin();iter!=_setOfNode.end();iter++)
491     {
492       Node* n1=*iter;
493       std::list<InGate *> ingates=n1->getOutGate()->edSetInGate();
494       for(std::list<InGate *>::const_iterator it2=ingates.begin();it2!=ingates.end();it2++)
495         {
496           //CF link : n1 -> (*it2)->getNode()
497           Node* n2=(*it2)->getNode();
498           links.insert(std::pair< Node*, Node* >(n1,n2));
499           std::set<Node *> bwn1=(*_bwLinks)[n1];
500           std::set<Node *> fwn1=(*_fwLinks)[n1];
501           std::set<Node *> fwn2=(*_fwLinks)[n2];
502           std::set<Node *> bwn2=(*_bwLinks)[n2];
503           std::pair<std::set<Node*>::iterator,bool> ret;
504           for(std::set<Node *>::const_iterator iter2=bwn1.begin();iter2!=bwn1.end();iter2++)
505             {
506               for(std::set<Node *>::const_iterator it3=fwn2.begin();it3!=fwn2.end();it3++)
507                 {
508                   ret=(*_fwLinks)[*iter2].insert(*it3);
509                   if(ret.second==false)
510                     {
511                       //dependency already exists (*iter2) -> (*it3) : if a direct link exists it's a useless one
512                       if(links.find(std::pair< Node*, Node* >(*iter2,*it3)) != links.end())
513                         info.pushUselessCFLink(*iter2,*it3);
514                     }
515                 }
516               ret=(*_fwLinks)[*iter2].insert(n2);
517               if(ret.second==false)
518                 {
519                   //dependency already exists (*iter2) -> n2 : if a direct link exists it's a useless one
520                   if(links.find(std::pair< Node*, Node* >(*iter2,n2)) != links.end())
521                     info.pushUselessCFLink(*iter2,n2);
522                 }
523             }
524           for(std::set<Node *>::const_iterator it3=fwn2.begin();it3!=fwn2.end();it3++)
525             {
526               ret=(*_fwLinks)[n1].insert(*it3);
527               if(ret.second==false)
528                 {
529                   //dependency already exists n1 -> *it3 : if a direct link exists it's a useless one
530                   if(links.find(std::pair< Node*, Node* >(n1,*it3)) != links.end())
531                     info.pushUselessCFLink(n1,*it3);
532                 }
533             }
534           ret=(*_fwLinks)[n1].insert(n2);
535           if(ret.second==false)
536             {
537               //dependency already exists n1 -> n2 : it's a useless link
538               info.pushUselessCFLink(n1,n2);
539             }
540
541           for(std::set<Node *>::const_iterator iter2=fwn2.begin();iter2!=fwn2.end();iter2++)
542             {
543               (*_bwLinks)[*iter2].insert(bwn1.begin(),bwn1.end());
544               (*_bwLinks)[*iter2].insert(n1);
545             }
546           (*_bwLinks)[n2].insert(bwn1.begin(),bwn1.end());
547           (*_bwLinks)[n2].insert(n1);
548         }
549     }
550 }
551
552 void Bloc::destructCFComputations(LinkInfo& info) const
553 {
554   StaticDefinedComposedNode::destructCFComputations(info);
555   delete _fwLinks; _fwLinks=0;
556   delete _bwLinks; _bwLinks=0;
557 }
558
559 /*!
560  * \b WARNING \b Needs call of performCFComputations before beeing called.
561  *  Perform updates of containers regarding attributes of link 'start' -> 'end' and check the correct linking.
562  *  The output is in info struct.
563  *
564  * \param start : start port
565  * \param end : end port
566  * \param cross : 
567  * \param fw out parameter being append if start -> end link is a forward link \b without cross type DF/DS.
568  * \param fwCross out parameter being append if start -> end link is a forward link \b with cross type DF/DS.
569  * \param bw out parameter being append if start -> end link is a backward link.
570  * \param info out parameter being informed about eventual errors.
571  */
572 void Bloc::checkControlDependancy(OutPort *start, InPort *end, bool cross,
573                                   std::map < ComposedNode *,  std::list < OutPort * >, SortHierarc >& fw,
574                                   std::vector<OutPort *>& fwCross,
575                                   std::map< ComposedNode *, std::list < OutPort *>, SortHierarc >& bw,
576                                   LinkInfo& info) const
577 {
578   if(!cross)
579     {
580       Node *startN=isInMyDescendance(start->getNode());
581       Node *endN=isInMyDescendance(end->getNode());
582       if(startN==endN)
583         bw[(ComposedNode *)this].push_back(start);
584       else if(areLinked(startN,endN,true))
585         fw[(ComposedNode *)this].push_back(start);
586       else
587         if(areLinked(startN,endN,false))
588           bw[(ComposedNode *)this].push_back(start);
589         else
590           info.pushErrLink(start,end,E_UNPREDICTABLE_FED);
591     }
592   else//DFDS detected
593     if(arePossiblyRunnableAtSameTime(isInMyDescendance(start->getNode()),isInMyDescendance(end->getNode())))
594       fwCross.push_back(start);
595     else
596       info.pushErrLink(start,end,E_DS_LINK_UNESTABLISHABLE);
597 }
598
599 //! Check if two nodes are linked
600 /*!
601  * 'start' and 'end' \b must be direct son of 'this'.
602  * Typically used for data link.
603  * \param start : start node
604  * \param end : end node
605  * \param fw indicates if it is a forward link searched (true : default value) or a backward link serach.
606  * \return if true or false
607  */
608 bool Bloc::areLinked(Node *start, Node *end, bool fw) const
609 {
610   set<Node *>& nexts=fw ? (*_fwLinks)[start] : (*_bwLinks)[start];
611   return nexts.find(end)!=nexts.end();
612 }
613
614 //! Check if two nodes can run in parallel
615 /*!
616  * Typically used for stream link.
617  * 'start' and 'end' \b must be direct son of 'this'.
618  * \param start : start node
619  * \param end : end node
620  * \return true or false
621  */
622 bool Bloc::arePossiblyRunnableAtSameTime(Node *start, Node *end) const
623 {
624   set<Node *>& nexts=(*_fwLinks)[start];
625   set<Node *>& preds=(*_bwLinks)[start];
626   return nexts.find(end)==nexts.end() && preds.find(end)==preds.end();
627 }
628
629 //! Check control flow links
630 /*!
631  * \param starts If different of 0, must aggregate at leat \b 1 element.
632  * \param end : end port
633  * \param alreadyFed in/out parameter. Indicates if 'end' ports is already and surely set or fed by an another port.
634  * \param direction If true : forward direction else backward direction.
635  * \param info : collected information
636  */
637 void Bloc::checkCFLinks(const std::list<OutPort *>& starts, InputPort *end, unsigned char& alreadyFed, bool direction, LinkInfo& info) const
638 {
639   if(alreadyFed==FREE_ST || alreadyFed==FED_ST)
640     {
641       map<Node *,list <OutPort *> > classPerNodes;
642       for(list< OutPort *>::const_iterator iter1=starts.begin();iter1!=starts.end();iter1++)
643         classPerNodes[isInMyDescendance((*iter1)->getNode())].push_back(*iter1);
644       set<Node *> allNodes;
645       for(map<Node *,list <OutPort *> >::iterator iter2=classPerNodes.begin();iter2!=classPerNodes.end();iter2++)
646         allNodes.insert((*iter2).first);
647       vector<Node *> okAndUseless1,useless2;
648       seekOkAndUseless1(okAndUseless1,allNodes);
649       seekUseless2(useless2,allNodes);//after this point allNodes contains collapses
650       verdictForOkAndUseless1(classPerNodes,end,okAndUseless1,alreadyFed,direction,info);
651       verdictForCollapses(classPerNodes,end,allNodes,alreadyFed,direction,info);
652       verdictForOkAndUseless1(classPerNodes,end,useless2,alreadyFed,direction,info);
653     }
654   else if(alreadyFed==FED_DS_ST)
655     for(list< OutPort *>::const_iterator iter1=starts.begin();iter1!=starts.end();iter1++)
656       info.pushErrLink(*iter1,end,E_COLLAPSE_DFDS);
657 }
658
659 void Bloc::initComputation() const
660 {
661   for(list<Node *>::const_iterator iter=_setOfNode.begin();iter!=_setOfNode.end();iter++)
662     {
663       (*iter)->_colour=White;
664       (*iter)->getInGate()->exReset();
665       (*iter)->getOutGate()->exReset();
666     }
667 }
668
669 /*!
670  * Part of final step for CF graph anylizing. This is the part of non collapse nodes. 
671  * \param pool :
672  * \param end :
673  * \param candidates :
674  * \param alreadyFed in/out parameter. Indicates if 'end' ports is already and surely set or fed by an another port.
675  * \param direction
676  * \param info : collected information
677  */
678 void Bloc::verdictForOkAndUseless1(const std::map<Node *,std::list <OutPort *> >& pool, InputPort *end, 
679                                    const std::vector<Node *>& candidates, unsigned char& alreadyFed, 
680                                    bool direction, LinkInfo& info)
681 {
682   for(vector<Node *>::const_iterator iter=candidates.begin();iter!=candidates.end();iter++)
683     {
684       const list<OutPort *>& mySet=(*pool.find(*iter)).second;
685       if(mySet.size()==1)
686         {
687           if(alreadyFed==FREE_ST)
688             {
689               alreadyFed=FED_ST;//This the final choice. General case !
690               if(!direction)
691                 info.pushInfoLink(*(mySet.begin()),end,I_BACK);
692             }
693           else if(alreadyFed==FED_ST)
694               info.pushInfoLink(*(mySet.begin()),end,direction ? I_USELESS : I_BACK_USELESS);//Second or more turn in case of alreadyFed==FREE_ST before call of this method
695         }
696       else
697         {
698           if(dynamic_cast<ElementaryNode *>(*iter))
699             {
700               WarnReason reason;
701               if(alreadyFed==FREE_ST)
702                 reason=direction ? W_COLLAPSE_EL : W_BACK_COLLAPSE_EL;
703               else if(alreadyFed==FED_ST)
704                 reason=direction ? W_COLLAPSE_EL_AND_USELESS : W_BACK_COLLAPSE_EL_AND_USELESS;
705               for(list<OutPort *>::const_iterator iter2=mySet.begin();iter2!=mySet.end();iter2++)    
706                 info.pushWarnLink(*iter2,end,reason);
707             }
708           else
709             ((ComposedNode *)(*iter))->checkCFLinks(mySet,end,alreadyFed,direction,info);//Thanks to recursive model!
710         }
711     }
712 }
713
714 /*!
715  * Part of final step for CF graph anylizing. This is the part of collapses nodes. 
716  * \param pool :
717  * \param end :
718  * \param candidates :
719  * \param alreadyFed in/out parameter. Indicates if 'end' ports is already and surely set or fed by an another port.
720  * \param direction
721  * \param info : collected information
722  */
723 void Bloc::verdictForCollapses(const std::map<Node *,std::list <OutPort *> >& pool, InputPort *end, 
724                                const std::set<Node *>& candidates, unsigned char& alreadyFed, 
725                                bool direction, LinkInfo& info)
726 {
727   info.startCollapseTransac();
728   for(set<Node *>::const_iterator iter=candidates.begin();iter!=candidates.end();iter++)
729     {
730       const list<OutPort *>& mySet=(*pool.find(*iter)).second;
731       if(mySet.size()==1)
732         {
733           if(alreadyFed==FREE_ST)
734             info.pushWarnLink(*(mySet.begin()),end,direction ? W_COLLAPSE : W_BACK_COLLAPSE);
735           else if(alreadyFed==FED_ST)
736             info.pushWarnLink(*(mySet.begin()),end,direction ? W_COLLAPSE_AND_USELESS : W_BACK_COLLAPSE_EL_AND_USELESS);
737         }
738       else
739         {
740           if(dynamic_cast<ElementaryNode *>(*iter))
741             {
742               WarnReason reason;
743               if(alreadyFed==FREE_ST)
744                 reason=direction ? W_COLLAPSE_EL : W_BACK_COLLAPSE_EL;
745               else if(alreadyFed==FED_ST)
746                 reason=direction ? W_COLLAPSE_EL_AND_USELESS : W_BACK_COLLAPSE_EL_AND_USELESS;
747               for(list<OutPort *>::const_iterator iter2=mySet.begin();iter2!=mySet.end();iter2++)    
748                 info.pushWarnLink(*iter2,end,reason);
749             }
750           else
751             {
752               ((ComposedNode *)(*iter))->checkCFLinks(mySet,end,alreadyFed,direction,info);//Thanks to recursive model!
753               WarnReason reason;
754               if(alreadyFed==FREE_ST)
755                 reason=direction ? W_COLLAPSE : W_BACK_COLLAPSE;
756               else if(alreadyFed==FED_ST)
757                 reason=direction ? W_COLLAPSE_AND_USELESS : W_BACK_COLLAPSE_AND_USELESS;
758               for(list<OutPort *>::const_iterator iter2=mySet.begin();iter2!=mySet.end();iter2++)    
759                 info.pushWarnLink(*iter2,end,reason);
760             }
761         }
762     }
763   if(!candidates.empty())
764     if(alreadyFed==FREE_ST)
765       alreadyFed=FED_ST;
766   info.endCollapseTransac();
767 }
768
769 /*!
770  * \b WARNING use this method only after having called Bloc::performCFComputations method.
771  * \param okAndUseless1 out param contains at the end, the nodes without any collapse.
772  * \param allNodes in/out param. At the end, all the nodes in 'okAndUseless1' are deleted from 'allNodes'.
773  */
774 void Bloc::seekOkAndUseless1(std::vector<Node *>& okAndUseless1, std::set<Node *>& allNodes) const
775 {
776   set<Node *>::iterator iter=allNodes.begin();
777   while(iter!=allNodes.end())
778     {
779       set<Node *>& whereToFind=(*_bwLinks)[*iter];
780       std::set<Node *>::iterator iter2;
781       for(iter2=allNodes.begin();iter2!=allNodes.end();iter2++)
782         if((*iter)!=(*iter2))
783           if(whereToFind.find(*iter2)==whereToFind.end())
784             break;
785       if(iter2!=allNodes.end())
786         iter++;
787       else
788         {
789           okAndUseless1.push_back((*iter));
790           allNodes.erase(iter);
791           iter=allNodes.begin();
792         }
793     }
794 }
795
796 /*!
797  * \b WARNING use this method only after having called Bloc::performCFComputations method.
798  * For params see Bloc::seekOkAndUseless1.
799  */
800 void Bloc::seekUseless2(std::vector<Node *>& useless2, std::set<Node *>& allNodes) const
801 {
802   set<Node *>::iterator iter=allNodes.begin();
803   while(iter!=allNodes.end())
804     {
805       set<Node *>& whereToFind=(*_fwLinks)[*iter];
806       std::set<Node *>::iterator iter2;
807       for(iter2=allNodes.begin();iter2!=allNodes.end();iter2++)
808         if((*iter)!=(*iter2))
809           if(whereToFind.find(*iter2)==whereToFind.end())
810             break;
811       if(iter2!=allNodes.end())
812         {
813           iter++;
814         }
815       else
816         {
817           useless2.push_back((*iter));
818           allNodes.erase(iter);
819           iter=allNodes.begin();
820         }
821     }
822 }
823
824 /*! 
825  * Internal method : Given a succeful path : updates 'fastFinder'
826  */
827 void Bloc::updateWithNewFind(const std::vector<Node *>& path, std::map<Node *, std::set<Node *> >& fastFinder)
828 {
829   if(path.size()>=3)
830     {
831       vector<Node *>::const_iterator iter=path.begin(); iter++;
832       vector<Node *>::const_iterator iter2=path.end(); iter2-=1;
833       for(;iter!=iter2;iter++)
834         fastFinder[*iter].insert(*(iter+1));
835     }
836 }
837
838 /*! 
839  * Internal method : After all paths have been found, useless CF links are searched
840  */
841 void Bloc::findUselessLinksIn(const std::list< std::vector<Node *> >& res , LinkInfo& info)
842 {
843   unsigned maxSize=0;
844   list< vector<Node *> >::const_iterator whereToPeerAt;
845   for(list< vector<Node *> >::const_iterator iter=res.begin();iter!=res.end();iter++)
846     if((*iter).size()>maxSize)
847       {
848         maxSize=(*iter).size();
849         whereToPeerAt=iter;
850       }
851   //
852   if(maxSize>1)
853     {
854       vector<Node *>::const_iterator iter2=(*whereToPeerAt).begin();
855       map<Node *,bool>::iterator iter4;
856       set<Node *> searcher(iter2+1,(*whereToPeerAt).end());//to boost research
857       for(;iter2!=((*whereToPeerAt).end()-2);iter2++)
858         {
859           list< pair<InGate *,bool> >& nexts=(*iter2)->getOutGate()->edMapInGate();
860           for(list< pair<InGate *,bool> >::iterator iter4=nexts.begin();iter4!=nexts.end();iter4++)
861             if((*iter4).first->getNode()!=*(iter2+1))
862               if(searcher.find((*iter4).first->getNode())!=searcher.end())
863                 info.pushUselessCFLink(*iter2,(*iter4).first->getNode());
864           searcher.erase(*iter2);
865         }
866     }
867 }