Salome HOME
Copyright update: 2016
[modules/yacs.git] / src / engine / ForEachLoop.cxx
1 // Copyright (C) 2006-2016  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 "ForEachLoop.hxx"
21 #include "TypeCode.hxx"
22 #include "Visitor.hxx"
23 #include "ComposedNode.hxx"
24 #include "Executor.hxx"
25 #include "AutoLocker.hxx"
26
27 #include <iostream>
28 #include <iomanip>
29 #include <sstream>
30 #include <algorithm>    // std::replace_if
31
32 //#define _DEVDEBUG_
33 #include "YacsTrace.hxx"
34
35 using namespace YACS::ENGINE;
36 using namespace std;
37
38 /*! \class YACS::ENGINE::ForEachLoop
39  *  \brief Loop node for parametric calculation
40  *
41  *  \ingroup Nodes
42  */
43
44 const char FakeNodeForForEachLoop::NAME[]="thisIsAFakeNode";
45
46 const char SplitterNode::NAME_OF_SEQUENCE_INPUT[]="SmplsCollection";
47
48 const char ForEachLoop::NAME_OF_SPLITTERNODE[]="splitter";
49
50 const int ForEachLoop::NOT_RUNNING_BRANCH_ID=-1;
51
52 const char ForEachLoop::INTERCEPTOR_STR[]="_interceptor";
53
54 InterceptorInputPort::InterceptorInputPort(const std::string& name, Node *node, TypeCode* type):AnyInputPort(name,node,type),
55                                                                                                 DataPort(name,node,type),Port(node),
56                                                                                                 _repr(0)
57 {
58 }
59
60 InterceptorInputPort::InterceptorInputPort(const  InterceptorInputPort& other, Node *newHelder):AnyInputPort(other,newHelder),DataPort(other,newHelder),
61                                                                                                 Port(other,newHelder),
62                                                                                                 _repr(0)
63 {
64 }
65
66 void InterceptorInputPort::getAllRepresentants(std::set<InPort *>& repr) const
67 {
68   set<InPort *> ports=_repr->edSetInPort();
69   for(set<InPort *>::iterator iter=ports.begin();iter!=ports.end();iter++)
70     (*iter)->getAllRepresentants(repr);
71 }
72
73 InputPort *InterceptorInputPort::clone(Node *newHelder) const
74 {
75   return new InterceptorInputPort(*this,newHelder);
76 }
77
78 void InterceptorInputPort::setRepr(AnySplitOutputPort *repr)
79 {
80   _repr=repr;
81 }
82
83 bool AnySplitOutputPort::decrRef()
84 {
85   return (--_cnt==0);
86 }
87
88 void AnySplitOutputPort::incrRef() const
89 {
90   _cnt++;
91 }
92
93 AnySplitOutputPort::AnySplitOutputPort(const std::string& name, Node *node, TypeCode *type):OutputPort(name,node,type),
94                                                                                             DataPort(name,node,type),Port(node),
95                                                                                             _repr(0),_intercptr(0),_cnt(1)
96 {
97 }
98
99 AnySplitOutputPort::AnySplitOutputPort(const AnySplitOutputPort& other, Node *newHelder):OutputPort(other,newHelder),
100                                                                                          DataPort(other,newHelder),
101                                                                                          Port(other,newHelder),
102                                                                                          _repr(0),_intercptr(0),_cnt(1)
103 {
104 }
105
106 bool AnySplitOutputPort::addInPort(InPort *inPort) throw(YACS::Exception)
107 {
108   bool ret=OutputPort::addInPort(inPort);
109   if(_repr)
110     _repr->addInPort(_intercptr);
111   return ret;
112 }
113
114 void AnySplitOutputPort::getAllRepresented(std::set<OutPort *>& represented) const
115 {
116   if(!_repr)
117     OutPort::getAllRepresented(represented);
118   else
119     _repr->getAllRepresented(represented);
120 }
121
122 int AnySplitOutputPort::removeInPort(InPort *inPort, bool forward) throw(YACS::Exception)
123 {
124   bool ret=OutputPort::removeInPort(inPort,forward);
125   if(_repr)
126     if(_setOfInputPort.empty())
127       _repr->removeInPort(_intercptr,forward);
128   return ret;
129 }
130
131 void AnySplitOutputPort::addRepr(OutPort *repr, InterceptorInputPort *intercptr)
132 {
133   _repr=repr;
134   _intercptr=intercptr;
135 }
136
137 OutputPort *AnySplitOutputPort::clone(Node *newHelder) const
138 {
139   return new AnySplitOutputPort(*this,newHelder);
140 }
141
142 SeqAnyInputPort::SeqAnyInputPort(const std::string& name, Node *node, TypeCodeSeq* type):AnyInputPort(name,node,type),DataPort(name,node,type),Port(node)
143 {
144   _type->decrRef();
145 }
146
147 SeqAnyInputPort::SeqAnyInputPort(const  SeqAnyInputPort& other, Node *newHelder):AnyInputPort(other,newHelder),DataPort(other,newHelder),Port(other,newHelder)
148 {
149 }
150
151 InputPort *SeqAnyInputPort::clone(Node *newHelder) const
152 {
153   return new SeqAnyInputPort(*this,newHelder);
154 }
155
156 unsigned SeqAnyInputPort::getNumberOfElements() const
157 {
158   const SequenceAny * valCsted=(const SequenceAny *) _value;
159   if (valCsted) return valCsted->size();
160   return 0;
161 }
162
163 Any *SeqAnyInputPort::getValueAtRank(int i) const
164 {
165   const SequenceAny * valCsted=(const SequenceAny *) _value;
166   AnyPtr ret=(*valCsted)[i];
167   ret->incrRef();
168   return ret;
169 }
170
171 std::string SeqAnyInputPort::dump()
172 {
173   stringstream xmldump;
174   int nbElem = getNumberOfElements();
175   xmldump << "<value><array><data>" << endl;
176   for (int i = 0; i < nbElem; i++)
177     {
178       Any *val = getValueAtRank(i);
179       switch (val->getType()->kind())
180         {
181         case Double:
182           xmldump << "<value><double>" << setprecision(16) << val->getDoubleValue() << "</double></value>" << endl;
183           break;
184         case Int:
185           xmldump << "<value><int>" << val->getIntValue() << "</int></value>" << endl;
186           break;
187         case Bool:
188           xmldump << "<value><boolean>" << val->getBoolValue() << "</boolean></value>" << endl;
189           break;
190         case String:
191           xmldump << "<value><string>" << val->getStringValue() << "</string></value>" << endl;
192           break;
193         case Objref:
194           xmldump << "<value><objref>" << val->getStringValue() << "</objref></value>" << endl;
195           break;
196         default:
197           xmldump << "<value><error> NO_SERIALISATION_AVAILABLE </error></value>" << endl;
198           break;
199         }
200     }
201   xmldump << "</data></array></value>" << endl;
202   return xmldump.str();
203 }
204
205 SplitterNode::SplitterNode(const std::string& name, TypeCode *typeOfData, 
206                            ForEachLoop *father):ElementaryNode(name),
207                                                 _dataPortToDispatch(NAME_OF_SEQUENCE_INPUT,
208                                                                     this,(TypeCodeSeq *)TypeCode::sequenceTc("","",typeOfData))
209 {
210   _father=father;
211 }
212
213 SplitterNode::SplitterNode(const SplitterNode& other, ForEachLoop *father):ElementaryNode(other,father),
214                                                                            _dataPortToDispatch(other._dataPortToDispatch,this)
215 {
216 }
217
218 InputPort *SplitterNode::getInputPort(const std::string& name) const throw(YACS::Exception)
219 {
220   if(name==NAME_OF_SEQUENCE_INPUT)
221     return (InputPort *)&_dataPortToDispatch;
222   else
223     return ElementaryNode::getInputPort(name);
224 }
225
226 Node *SplitterNode::simpleClone(ComposedNode *father, bool editionOnly) const
227 {
228   return new SplitterNode(*this,(ForEachLoop *)father);
229 }
230
231 unsigned SplitterNode::getNumberOfElements() const
232 {
233   return _dataPortToDispatch.getNumberOfElements();
234 }
235
236 void SplitterNode::execute()
237 {
238   //Nothing : should never been called elsewhere big problem...
239 }
240
241 void SplitterNode::init(bool start)
242 {
243   ElementaryNode::init(start);
244   _dataPortToDispatch.exInit(start);
245 }
246
247 void SplitterNode::putSplittedValueOnRankTo(int rankInSeq, int branch, bool first)
248 {
249   Any *valueToDispatch=_dataPortToDispatch.getValueAtRank(rankInSeq);
250   ForEachLoop *fatherTyped=(ForEachLoop *)_father;
251   fatherTyped->putValueOnBranch(valueToDispatch,branch,first);
252   valueToDispatch->decrRef();
253 }
254
255 FakeNodeForForEachLoop::FakeNodeForForEachLoop(ForEachLoop *loop, bool normalFinish):ElementaryNode(NAME),
256                                                                                      _loop(loop),
257                                                                                      _normalFinish(normalFinish)
258 {
259   _state=YACS::TOACTIVATE;
260   _father=_loop->getFather();
261 }
262
263 FakeNodeForForEachLoop::FakeNodeForForEachLoop(const FakeNodeForForEachLoop& other):ElementaryNode(other),_loop(0),
264                                                                                     _normalFinish(false)
265 {
266 }
267
268 Node *FakeNodeForForEachLoop::simpleClone(ComposedNode *father, bool editionOnly) const
269 {
270   return new FakeNodeForForEachLoop(*this);
271 }
272
273 void FakeNodeForForEachLoop::exForwardFailed()
274 {
275   _loop->exForwardFailed();
276 }
277
278 void FakeNodeForForEachLoop::exForwardFinished()
279
280   _loop->exForwardFinished();
281 }
282
283 void FakeNodeForForEachLoop::execute()
284 {
285   if(!_normalFinish)
286     throw Exception("");//only to trigger ABORT on Executor
287   else
288     _loop->pushAllSequenceValues();
289 }
290
291 void FakeNodeForForEachLoop::aborted()
292 {
293   _loop->setState(YACS::ERROR);
294 }
295
296 void FakeNodeForForEachLoop::finished()
297 {
298   _loop->setState(YACS::DONE);
299 }
300
301 ForEachLoopPassedData::ForEachLoopPassedData(const std::vector<unsigned int>& passedIds, const std::vector<SequenceAny *>& passedOutputs, const std::vector<std::string>& nameOfOutputs):_passedIds(passedIds),_passedOutputs(passedOutputs),_nameOfOutputs(nameOfOutputs)
302 {
303   std::size_t sz(_passedIds.size()),sz1(passedOutputs.size()),sz2(nameOfOutputs.size());
304   if(sz1!=sz2)
305     throw YACS::Exception("ForEachLoopPassedData::ForEachLoopPassedData : nameOfOutputs and passedOutputs must have the same size !");
306   for(std::vector<SequenceAny *>::iterator it=_passedOutputs.begin();it!=_passedOutputs.end();it++)
307     {
308       const SequenceAny *elt(*it);
309       if(elt)
310         if(sz!=(std::size_t)elt->size())
311           throw YACS::Exception("ForEachLoopPassedData::ForEachLoopPassedData : incoherent input of passed data !");
312     }
313   for(std::vector<SequenceAny *>::iterator it=_passedOutputs.begin();it!=_passedOutputs.end();it++)
314     {
315       SequenceAny *elt(*it);
316       if(elt)
317         elt->incrRef();
318     }
319 }
320
321 ForEachLoopPassedData::~ForEachLoopPassedData()
322 {
323   for(std::vector<SequenceAny *>::iterator it=_passedOutputs.begin();it!=_passedOutputs.end();it++)
324     {
325       SequenceAny *elt(*it);
326       if(elt)
327         elt->decrRef();
328     }
329 }
330
331 void ForEachLoopPassedData::init()
332 {
333   _flagsIds.clear();
334 }
335
336 void ForEachLoopPassedData::checkCompatibilyWithNb(int nbOfElts) const
337 {
338   if(nbOfElts<0)
339     throw YACS::Exception("ForEachLoopPassedData::checkCompatibilyWithNb : nb of elts is expected to be > 0 !");
340   std::size_t sizeExp(_passedIds.size()),nbOfElts2(nbOfElts);
341   if(nbOfElts2<sizeExp)
342     throw YACS::Exception("ForEachLoopPassedData::checkCompatibilyWithNb : Invalid nb of elemts in input seq regarding passed data set !");
343   for(std::vector<unsigned int>::const_iterator it=_passedIds.begin();it!=_passedIds.end();it++)
344     {
345       if((*it)>=nbOfElts2)
346         throw YACS::Exception("ForEachLoopPassedData::checkCompatibilyWithNb : Invalid nb of elemts in input seq regarding passed data set 2 !");
347     }
348   _flagsIds.resize(nbOfElts);
349   std::fill(_flagsIds.begin(),_flagsIds.end(),false);
350   for(std::vector<unsigned int>::const_iterator it=_passedIds.begin();it!=_passedIds.end();it++)
351     {
352       if(*it<nbOfElts)
353         {
354           if(!_flagsIds[*it])
355             _flagsIds[*it]=true;
356           else
357             {
358               std::ostringstream oss; oss << "ForEachLoopPassedData::checkCompatibilyWithNb : id " << *it << " in list of ids appears more than once !";
359               throw YACS::Exception(oss.str());
360             }
361         }
362       else
363         {
364           std::ostringstream oss; oss << "ForEachLoopPassedData::checkCompatibilyWithNb : Presence of id " << *it << " in list of ids ! Must be in [0," <<  nbOfElts << ") !";
365           throw YACS::Exception(oss.str());
366         }
367     }
368 }
369
370 void ForEachLoopPassedData::checkLevel2(const std::vector<AnyInputPort *>& ports) const
371 {
372   std::size_t sz(_nameOfOutputs.size());
373   if(sz!=ports.size())
374     throw YACS::Exception("ForEachLoopPassedData::checkLevel2 : mismatch of size of vectors !");
375   for(std::size_t i=0;i<sz;i++)
376     {
377       AnyInputPort *elt(ports[i]);
378       if(!elt)
379         throw YACS::Exception("ForEachLoopPassedData::checkLevel2 : presence of null instance !");
380       if(_nameOfOutputs[i]!=elt->getName())
381         {
382           std::ostringstream oss; oss << "ForEachLoopPassedData::checkLevel2 : At pos #" << i << " the name is not OK !";
383           throw YACS::Exception(oss.str());
384         }
385     }
386 }
387
388 /*!
389  * performs local to abs id. Input \a localId refers to an id in all jobs to perform. Returned id refers to pos in whole output sequences.
390  */
391 int ForEachLoopPassedData::toAbsId(int localId) const
392 {
393   if(localId<0)
394     throw YACS::Exception("ForEachLoopPassedData::toAbsId : local pos must be >= 0 !");
395   int ret(0),curLocId(0);
396   for(std::vector<bool>::const_iterator it=_flagsIds.begin();it!=_flagsIds.end();it++,ret++)
397     {
398       if(!*it)
399         {
400           if(localId==curLocId)
401             return ret;
402           curLocId++;
403         }
404     }
405   throw YACS::Exception("ForEachLoopPassedData::toAbsId : not referenced Id !");
406 }
407
408 /*!
409  * Equivalent to toAbsId except that only ON are considered here.
410  */
411 int ForEachLoopPassedData::toAbsIdNot(int localId) const
412 {
413   if(localId<0)
414     throw YACS::Exception("ForEachLoopPassedData::toAbsIdNot : local pos must be >= 0 !");
415   int ret(0),curLocId(0);
416   for(std::vector<bool>::const_iterator it=_flagsIds.begin();it!=_flagsIds.end();it++,ret++)
417     {
418       if(*it)//<- diff is here !
419         {
420           if(localId==curLocId)
421             return ret;
422           curLocId++;
423         }
424     }
425   throw YACS::Exception("ForEachLoopPassedData::toAbsIdNot : not referenced Id !");
426 }
427
428 int ForEachLoopPassedData::getNumberOfElementsToDo() const
429 {
430   std::size_t nbAllElts(_flagsIds.size());
431   std::size_t ret(nbAllElts-_passedIds.size());
432   return ret;
433 }
434
435 void ForEachLoopPassedData::assignAlreadyDone(const std::vector<SequenceAny *>& execVals) const
436 {
437   std::size_t sz(execVals.size());
438   if(_passedOutputs.size()!=sz)
439     throw YACS::Exception("ForEachLoopPassedData::assignedAlreadyDone : mismatch of size of vectors !");
440   for(std::size_t i=0;i<sz;i++)
441     {
442       SequenceAny *elt(_passedOutputs[i]);
443       SequenceAny *eltDestination(execVals[i]);
444       if(!elt)
445         throw YACS::Exception("ForEachLoopPassedData::assignedAlreadyDone : presence of null elt !");
446       unsigned int szOfElt(elt->size());
447       for(unsigned int j=0;j<szOfElt;j++)
448         {
449           AnyPtr elt1((*elt)[j]);
450           int jAbs(toAbsIdNot(j));
451           eltDestination->setEltAtRank(jAbs,elt1);
452         }
453     }
454 }
455
456 ForEachLoop::ForEachLoop(const std::string& name, TypeCode *typeOfDataSplitted):DynParaLoop(name,typeOfDataSplitted),
457                                                                                 _splitterNode(NAME_OF_SPLITTERNODE,typeOfDataSplitted,this),
458                                                                                 _execCurrentId(0),_nodeForSpecialCases(0),_currentIndex(0),_passedData(0)
459 {
460 }
461
462 ForEachLoop::ForEachLoop(const ForEachLoop& other, ComposedNode *father, bool editionOnly):DynParaLoop(other,father,editionOnly),
463                                                                                            _splitterNode(other._splitterNode,this),
464                                                                                            _execCurrentId(0),_nodeForSpecialCases(0),_currentIndex(0),_passedData(0)
465 {
466   int i=0;
467   if(!editionOnly)
468     for(vector<AnySplitOutputPort *>::const_iterator iter2=other._outGoingPorts.begin();iter2!=other._outGoingPorts.end();iter2++,i++)
469       {
470         AnySplitOutputPort *temp=new AnySplitOutputPort(*(*iter2),this);
471         InterceptorInputPort *interc=new InterceptorInputPort(*other._intecptrsForOutGoingPorts[i],this);
472         temp->addRepr(getOutPort(other.getOutPortName((*iter2)->getRepr())),interc);
473         interc->setRepr(temp);
474         _outGoingPorts.push_back(temp);
475         _intecptrsForOutGoingPorts.push_back(interc);
476       }
477 }
478
479 Node *ForEachLoop::simpleClone(ComposedNode *father, bool editionOnly) const
480 {
481   return new ForEachLoop(*this,father,editionOnly);
482 }
483
484 ForEachLoop::~ForEachLoop()
485 {
486   cleanDynGraph();
487   for(vector<AnySplitOutputPort *>::iterator iter=_outGoingPorts.begin();iter!=_outGoingPorts.end();iter++)
488     delete *iter;
489   for(vector<InterceptorInputPort *>::iterator iter2=_intecptrsForOutGoingPorts.begin();iter2!=_intecptrsForOutGoingPorts.end();iter2++)
490     delete *iter2;
491   delete _passedData;
492 }
493
494 void ForEachLoop::init(bool start)
495 {
496   DynParaLoop::init(start);
497   _splitterNode.init(start);
498   _execCurrentId=0;
499   cleanDynGraph();
500   _currentIndex = 0;
501   exUpdateProgress();
502   if(_passedData)
503     _passedData->init();
504 }
505
506 void ForEachLoop::exUpdateState()
507 {
508   DEBTRACE("ForEachLoop::exUpdateState");
509   if(_state == YACS::DISABLED)
510     return;
511   if(_state == YACS::DONE)
512     return;
513   if(_inGate.exIsReady())
514     {
515       //internal graph update
516       int i;
517       int nbOfBr(_nbOfBranches.getIntValue()),nbOfElts(_splitterNode.getNumberOfElements()),nbOfEltsDone(0);
518       if(_passedData)
519         {
520           _passedData->checkCompatibilyWithNb(nbOfElts);
521           nbOfEltsDone=_passedData->getNumberOfEltsAlreadyDone();
522         }
523       int nbOfEltsToDo(nbOfElts-nbOfEltsDone);
524
525       DEBTRACE("nbOfElts=" << nbOfElts);
526       DEBTRACE("nbOfBr=" << nbOfBr);
527
528       if(nbOfEltsToDo==0)
529         {
530           prepareSequenceValues(0);
531           delete _nodeForSpecialCases;
532           _nodeForSpecialCases=new FakeNodeForForEachLoop(this,true);
533           setState(YACS::ACTIVATED);
534           return ;
535         }
536       if(nbOfBr<=0)
537         {
538           delete _nodeForSpecialCases;
539           _nodeForSpecialCases=new FakeNodeForForEachLoop(this,getAllOutPortsLeavingCurrentScope().empty());
540           setState(YACS::ACTIVATED);
541           return ;
542         }
543       if(nbOfBr>nbOfEltsToDo)
544         nbOfBr=nbOfEltsToDo;
545       _execNodes.resize(nbOfBr);
546       _execIds.resize(nbOfBr);
547       _execOutGoingPorts.resize(nbOfBr);
548       prepareSequenceValues(nbOfElts);
549       if(_initNode)
550         _execInitNodes.resize(nbOfBr);
551       _initializingCounter = 0;
552       if (_finalizeNode)
553         _execFinalizeNodes.resize(nbOfBr);
554
555       vector<Node *> origNodes;
556       origNodes.push_back(_initNode);
557       origNodes.push_back(_node);
558       origNodes.push_back(_finalizeNode);
559
560       //Conversion exceptions can be thrown by createOutputOutOfScopeInterceptors 
561       //so catch them to control errors
562       try
563         {
564           for(i=0;i<nbOfBr;i++)
565             {
566               DEBTRACE( "-------------- 2" );
567               vector<Node *> clonedNodes = cloneAndPlaceNodesCoherently(origNodes);
568               if(_initNode)
569                 _execInitNodes[i] = clonedNodes[0];
570               _execNodes[i] = clonedNodes[1];
571               if(_finalizeNode)
572                 _execFinalizeNodes[i] = clonedNodes[2];
573               DEBTRACE( "-------------- 4" );
574               prepareInputsFromOutOfScope(i);
575               DEBTRACE( "-------------- 5" );
576               createOutputOutOfScopeInterceptors(i);
577               DEBTRACE( "-------------- 6" );
578             }
579           for(i=0;i<nbOfBr;i++)
580             {
581               DEBTRACE( "-------------- 1 " << i << " " << _execCurrentId);
582               _execIds[i]=_execCurrentId;
583               int posInAbs(_execCurrentId);
584               if(_passedData)
585                 posInAbs=_passedData->toAbsId(_execCurrentId);
586               _splitterNode.putSplittedValueOnRankTo(posInAbs,i,true);
587               _execCurrentId++;
588               DEBTRACE( "-------------- 7" );
589             }
590           if(_passedData)
591             {
592               _passedData->checkLevel2(_execOutGoingPorts[0]);
593               _passedData->assignAlreadyDone(_execVals);
594             }
595         }
596       catch(YACS::Exception& ex)
597         {
598           //ForEachLoop must be put in error and the exception rethrown to notify the caller
599           DEBTRACE( "ForEachLoop::exUpdateState: " << ex.what() );
600           setState(YACS::ERROR);
601           exForwardFailed();
602           throw;
603         }
604
605       setState(YACS::ACTIVATED); // move the calling of setState method there for adding observers for clone nodes in GUI part
606
607       //let's go
608       for(i=0;i<nbOfBr;i++)
609         if(_initNode)
610           {
611             _execInitNodes[i]->exUpdateState();
612             _initializingCounter++;
613           }
614         else
615           {
616             _nbOfEltConsumed++;
617             _execNodes[i]->exUpdateState();
618           }
619
620       forwardExecStateToOriginalBody(_execNodes[nbOfBr-1]);
621     }
622 }
623
624 void ForEachLoop::exUpdateProgress()
625 {
626   // emit notification to all observers registered with the dispatcher on any change of the node's state
627   sendEvent("progress");
628 }
629
630 void ForEachLoop::getReadyTasks(std::vector<Task *>& tasks)
631 {
632   if(!_node)
633     return;
634   if(_state==YACS::TOACTIVATE) setState(YACS::ACTIVATED);
635   if(_state==YACS::TOACTIVATE || _state==YACS::ACTIVATED)
636     {
637       if(_nodeForSpecialCases)
638         {
639           _nodeForSpecialCases->getReadyTasks(tasks);
640           return ;
641         }
642       vector<Node *>::iterator iter;
643       for (iter=_execNodes.begin() ; iter!=_execNodes.end() ; iter++)
644         (*iter)->getReadyTasks(tasks);
645       for (iter=_execInitNodes.begin() ; iter!=_execInitNodes.end() ; iter++)
646         (*iter)->getReadyTasks(tasks);
647       for (iter=_execFinalizeNodes.begin() ; iter!=_execFinalizeNodes.end() ; iter++)
648         (*iter)->getReadyTasks(tasks);
649     }
650 }
651
652 int ForEachLoop::getNumberOfInputPorts() const
653 {
654   return DynParaLoop::getNumberOfInputPorts()+1;
655 }
656
657 void ForEachLoop::checkNoCyclePassingThrough(Node *node) throw(YACS::Exception)
658 {
659   //TO DO
660 }
661
662 void ForEachLoop::selectRunnableTasks(std::vector<Task *>& tasks)
663 {
664 }
665
666 std::list<InputPort *> ForEachLoop::getSetOfInputPort() const
667 {
668   list<InputPort *> ret=DynParaLoop::getSetOfInputPort();
669   ret.push_back((InputPort *)&_splitterNode._dataPortToDispatch);
670   return ret;
671 }
672
673 std::list<InputPort *> ForEachLoop::getLocalInputPorts() const
674 {
675   list<InputPort *> ret=DynParaLoop::getLocalInputPorts();
676   ret.push_back((InputPort *)&_splitterNode._dataPortToDispatch);
677   return ret;
678 }
679
680 InputPort *ForEachLoop::getInputPort(const std::string& name) const throw(YACS::Exception)
681 {
682   if(name==SplitterNode::NAME_OF_SEQUENCE_INPUT)
683     return (InputPort *)&_splitterNode._dataPortToDispatch;
684   else
685     return DynParaLoop::getInputPort(name);
686 }
687
688 OutputPort *ForEachLoop::getOutputPort(const std::string& name) const throw(YACS::Exception)
689 {
690   for(vector<AnySplitOutputPort *>::const_iterator iter=_outGoingPorts.begin();iter!=_outGoingPorts.end();iter++)
691     {
692       if(name==(*iter)->getName())
693         return (OutputPort *)(*iter);
694     }
695   return DynParaLoop::getOutputPort(name);
696 }
697
698 OutPort *ForEachLoop::getOutPort(const std::string& name) const throw(YACS::Exception)
699 {
700   for(vector<AnySplitOutputPort *>::const_iterator iter=_outGoingPorts.begin();iter!=_outGoingPorts.end();iter++)
701     {
702       if(name==(*iter)->getName())
703         return (OutPort *)(*iter);
704     }
705   return DynParaLoop::getOutPort(name);
706 }
707
708 Node *ForEachLoop::getChildByShortName(const std::string& name) const throw(YACS::Exception)
709 {
710   if(name==NAME_OF_SPLITTERNODE)
711     return (Node *)&_splitterNode;
712   else
713     return DynParaLoop::getChildByShortName(name);
714 }
715
716 //! Method used to notify the node that a child node has finished
717 /*!
718  * Update the current state and return the change state
719  *
720  *  \param node : the child node that has finished
721  *  \return the state change
722  */
723 YACS::Event ForEachLoop::updateStateOnFinishedEventFrom(Node *node)
724 {
725   DEBTRACE("updateStateOnFinishedEventFrom " << node->getName() << " " << node->getState());
726   unsigned int id;
727   switch(getIdentityOfNotifyerNode(node,id))
728     {
729     case INIT_NODE:
730       return updateStateForInitNodeOnFinishedEventFrom(node,id);
731     case WORK_NODE:
732       return updateStateForWorkNodeOnFinishedEventFrom(node,id,true);
733     case FINALIZE_NODE:
734       return updateStateForFinalizeNodeOnFinishedEventFrom(node,id);
735     default:
736       YASSERT(false);
737     }
738   return YACS::NOEVENT;
739 }
740
741 YACS::Event ForEachLoop::updateStateForInitNodeOnFinishedEventFrom(Node *node, unsigned int id)
742 {
743   _execNodes[id]->exUpdateState();
744   _nbOfEltConsumed++;
745   _initializingCounter--;
746   _currentIndex++;
747   if (_initializingCounter == 0)
748     _initNode->setState(DONE);
749   return YACS::NOEVENT;
750 }
751
752 /*!
753  * \param [in] isNormalFinish - if true
754  */
755 YACS::Event ForEachLoop::updateStateForWorkNodeOnFinishedEventFrom(Node *node, unsigned int id, bool isNormalFinish)
756 {
757   _currentIndex++;
758   exUpdateProgress();
759   if(isNormalFinish)
760     {
761       int globalId(_execIds[id]);
762       if(_passedData)
763         globalId=_passedData->toAbsId(globalId);
764       storeOutValsInSeqForOutOfScopeUse(globalId,id);
765     }
766   //
767   if(_execCurrentId==getFinishedId())
768     {//No more elements of _dataPortToDispatch to treat
769       _execIds[id]=NOT_RUNNING_BRANCH_ID;
770       //analyzing if some samples are still on treatment on other branches.
771       bool isFinished(true);
772       for(int i=0;i<_execIds.size() && isFinished;i++)
773         isFinished=(_execIds[i]==NOT_RUNNING_BRANCH_ID);
774       if(isFinished)
775         {
776           try
777           {
778               if(_failedCounter!=0)
779                 {// case of keepgoing mode + a failed
780                   std::ostringstream oss; oss << "Keep Going mode activated and some errors (" << _failedCounter << ")reported !";
781                   DEBTRACE("ForEachLoop::updateStateOnFinishedEventFrom : "<< oss.str());
782                   setState(YACS::FAILED);
783                   return YACS::ABORT;
784                 }
785               pushAllSequenceValues();
786
787               if (_node)
788                 {
789                   _node->setState(YACS::DONE);
790
791                   ComposedNode* compNode = dynamic_cast<ComposedNode*>(_node);
792                   if (compNode)
793                     {
794                       std::list<Node *> aChldn = compNode->getAllRecursiveConstituents();
795                       std::list<Node *>::iterator iter=aChldn.begin();
796                       for(;iter!=aChldn.end();iter++)
797                         (*iter)->setState(YACS::DONE);
798                     }
799                 }
800
801               if (_finalizeNode == NULL)
802                 {
803                   // No finalize node, we just finish the loop at the end of exec nodes execution
804                   setState(YACS::DONE);
805                   return YACS::FINISH;
806                 }
807               else
808                 {
809                   // Run the finalize nodes, the loop will be done only when they all finish
810                   _unfinishedCounter = 0;  // This counter indicates how many branches are not finished
811                   for (int i=0 ; i<_execIds.size() ; i++)
812                     {
813                       YASSERT(_execIds[i] == NOT_RUNNING_BRANCH_ID);
814                       DEBTRACE("Launching finalize node for branch " << i);
815                       _execFinalizeNodes[i]->exUpdateState();
816                       _unfinishedCounter++;
817                     }
818                   return YACS::NOEVENT;
819                 }
820           }
821           catch(YACS::Exception& ex)
822           {
823               DEBTRACE("ForEachLoop::updateStateOnFinishedEventFrom: "<<ex.what());
824               //no way to push results : put following nodes in FAILED state
825               //TODO could be more fine grain : put only concerned nodes in FAILED state
826               exForwardFailed();
827               setState(YACS::ERROR);
828               return YACS::ABORT;
829           }
830         }
831     }
832   else if(_state == YACS::ACTIVATED)
833     {//more elements to do and loop still activated
834       _execIds[id]=_execCurrentId;
835       node->init(false);
836       int posInAbs(_execCurrentId);
837       if(_passedData)
838         posInAbs=_passedData->toAbsId(_execCurrentId);
839       _splitterNode.putSplittedValueOnRankTo(posInAbs,id,false);
840       _execCurrentId++;
841       node->exUpdateState();
842       forwardExecStateToOriginalBody(node);
843       _nbOfEltConsumed++;
844     }
845   else
846     {//elements to process and loop no more activated
847       DEBTRACE("foreach loop state " << _state);
848     }
849   return YACS::NOEVENT;
850 }
851
852 YACS::Event ForEachLoop::updateStateForFinalizeNodeOnFinishedEventFrom(Node *node, unsigned int id)
853 {
854   DEBTRACE("Finalize node finished on branch " << id);
855   _unfinishedCounter--;
856   _currentIndex++;
857   exUpdateProgress();
858   DEBTRACE(_unfinishedCounter << " finalize nodes still running");
859   if (_unfinishedCounter == 0)
860     {
861       _finalizeNode->setState(YACS::DONE);
862       setState(YACS::DONE);
863       return YACS::FINISH;
864     }
865   else
866     return YACS::NOEVENT;
867 }
868
869 YACS::Event ForEachLoop::updateStateOnFailedEventFrom(Node *node, const Executor *execInst)
870 {
871   unsigned int id;
872   DynParaLoop::TypeOfNode ton(getIdentityOfNotifyerNode(node,id));
873   if(ton!=WORK_NODE || !execInst->getKeepGoingProperty())
874     return DynParaLoop::updateStateOnFailedEventFrom(node,execInst);
875   else
876     {
877       _failedCounter++;
878       return updateStateForWorkNodeOnFinishedEventFrom(node,id,false);
879     }
880 }
881
882 void ForEachLoop::InterceptorizeNameOfPort(std::string& portName)
883 {
884   std::replace_if(portName.begin(), portName.end(), std::bind1st(std::equal_to<char>(), '.'), '_');
885   portName += INTERCEPTOR_STR;
886 }
887
888 void ForEachLoop::buildDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView)
889 {
890   DynParaLoop::buildDelegateOf(port,finalTarget,pointsOfView);
891   string typeOfPortInstance=(port.first)->getNameOfTypeOfCurrentInstance();
892   if(typeOfPortInstance==OutputPort::NAME)
893     {
894       vector<AnySplitOutputPort *>::iterator iter=_outGoingPorts.begin();
895       int i=0;
896       for(;iter!=_outGoingPorts.end();iter++,i++)
897         if((*iter)->getRepr()==port.first || *iter==port.first)
898           break;
899       if(iter!=_outGoingPorts.end())
900         {
901           if(*iter!=port.first)
902             {
903               (*iter)->incrRef();
904               (*iter)->addRepr(port.first,_intecptrsForOutGoingPorts[i]);
905             }
906           port.first=*iter;
907         }
908       else
909         {
910           TypeCodeSeq *newTc=(TypeCodeSeq *)TypeCode::sequenceTc("","",port.first->edGetType());
911           // The out going ports belong to the ForEachLoop, whereas
912           // the delegated port belongs to a node child of the ForEachLoop.
913           // The name of the delegated port contains dots (bloc.node.outport),
914           // whereas the name of the out going port shouldn't do.
915           std::string outputPortName(getPortName(port.first));
916           InterceptorizeNameOfPort(outputPortName);
917           AnySplitOutputPort *newPort(new AnySplitOutputPort(outputPortName,this,newTc));
918           InterceptorInputPort *intercptor(new InterceptorInputPort(outputPortName + "_in",this,port.first->edGetType()));
919           intercptor->setRepr(newPort);
920           newTc->decrRef();
921           newPort->addRepr(port.first,intercptor);
922           _outGoingPorts.push_back(newPort);
923           _intecptrsForOutGoingPorts.push_back(intercptor);
924           port.first=newPort;
925         }
926     }
927   else
928     throw Exception("ForEachLoop::buildDelegateOf : not implemented for DS because not specified");
929 }
930
931 void ForEachLoop::getDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView) throw(YACS::Exception)
932 {
933   string typeOfPortInstance=(port.first)->getNameOfTypeOfCurrentInstance();
934   if(typeOfPortInstance==OutputPort::NAME)
935     {
936       vector<AnySplitOutputPort *>::iterator iter=_outGoingPorts.begin();
937       for(;iter!=_outGoingPorts.end();iter++)
938         if((*iter)->getRepr()==port.first)
939           break;
940       if(iter==_outGoingPorts.end())
941         {
942           string what("ForEachLoop::getDelegateOf : Port with name "); what+=port.first->getName(); what+=" not exported by ForEachLoop "; what+=_name; 
943           throw Exception(what);
944         }
945       else
946         port.first=(*iter);
947     }
948   else
949     throw Exception("ForEachLoop::getDelegateOf : not implemented because not specified");
950 }
951
952 void ForEachLoop::releaseDelegateOf(OutPort *portDwn, OutPort *portUp, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView) throw(YACS::Exception)
953 {
954   string typeOfPortInstance=portDwn->getNameOfTypeOfCurrentInstance();
955   if(typeOfPortInstance==OutputPort::NAME)
956     {
957       vector<AnySplitOutputPort *>::iterator iter=_outGoingPorts.begin();
958       vector<InterceptorInputPort *>::iterator iter2=_intecptrsForOutGoingPorts.begin();
959       for(;iter!=_outGoingPorts.end();iter++,iter2++)
960         if((*iter)->getRepr()==portDwn)
961           break;
962       //ASSERT(portUp==*iter.second)
963       if((*iter)->decrRef())
964         {
965           AnySplitOutputPort *p=*iter;
966           _outGoingPorts.erase(iter);
967           delete p;
968           InterceptorInputPort *ip=*iter2;
969           _intecptrsForOutGoingPorts.erase(iter2);
970           delete ip;
971         }
972     }
973 }
974
975 OutPort *ForEachLoop::getDynOutPortByAbsName(int branchNb, const std::string& name)
976 {
977   string portName, nodeName;
978   splitNamesBySep(name,Node::SEP_CHAR_IN_PORT,nodeName,portName,false);
979   Node *staticChild = getChildByName(nodeName);
980   return _execNodes[branchNb]->getOutPort(portName);//It's impossible(garanteed by YACS::ENGINE::ForEachLoop::buildDelegateOf)
981   //that a link starting from _initNode goes out of scope of 'this'.
982 }
983
984 void ForEachLoop::cleanDynGraph()
985 {
986   DynParaLoop::cleanDynGraph();
987   for(vector< SequenceAny *>::iterator iter3=_execVals.begin();iter3!=_execVals.end();iter3++)
988     (*iter3)->decrRef();
989   _execVals.clear();
990   for(vector< vector<AnyInputPort *> >::iterator iter4=_execOutGoingPorts.begin();iter4!=_execOutGoingPorts.end();iter4++)
991     for(vector<AnyInputPort *>::iterator iter5=(*iter4).begin();iter5!=(*iter4).end();iter5++)
992       delete *iter5;
993   _execOutGoingPorts.clear();
994 }
995
996 void ForEachLoop::storeOutValsInSeqForOutOfScopeUse(int rank, int branchNb)
997 {
998   vector<AnyInputPort *>::iterator iter;
999   int i=0;
1000   for(iter=_execOutGoingPorts[branchNb].begin();iter!=_execOutGoingPorts[branchNb].end();iter++,i++)
1001     {
1002       Any *val=(Any *)(*iter)->getValue();
1003       _execVals[i]->setEltAtRank(rank,val);
1004     }
1005 }
1006
1007 int ForEachLoop::getFinishedId()
1008 {
1009   if(!_passedData)
1010     return _splitterNode.getNumberOfElements();
1011   else
1012     return _passedData->getNumberOfElementsToDo();
1013 }
1014
1015 void ForEachLoop::prepareSequenceValues(int sizeOfSamples)
1016 {
1017   _execVals.resize(_outGoingPorts.size());
1018   vector<AnySplitOutputPort *>::iterator iter=_outGoingPorts.begin();
1019   for(int i=0;iter!=_outGoingPorts.end();iter++,i++)
1020     _execVals[i]=SequenceAny::New((*iter)->edGetType()->contentType(),sizeOfSamples);
1021 }
1022
1023 void ForEachLoop::pushAllSequenceValues()
1024 {
1025   vector<AnySplitOutputPort *>::iterator iter=_outGoingPorts.begin();
1026   int i=0;
1027   for(;iter!=_outGoingPorts.end();iter++,i++)
1028     (*iter)->put((const void *)_execVals[i]);
1029 }
1030
1031 void ForEachLoop::createOutputOutOfScopeInterceptors(int branchNb)
1032 {
1033   vector<AnySplitOutputPort *>::iterator iter=_outGoingPorts.begin();
1034   int i=0;
1035   for(;iter!=_outGoingPorts.end();iter++,i++)
1036     {
1037       DEBTRACE( (*iter)->getName() << " " << (*iter)->edGetType()->kind() );
1038       //AnyInputPort *interceptor=new AnyInputPort((*iter)->getName(),this,(*iter)->edGetType());
1039       OutPort *portOut=getDynOutPortByAbsName(branchNb,getOutPortName(((*iter)->getRepr())));
1040       DEBTRACE( portOut->getName() );
1041       AnyInputPort *interceptor=new AnyInputPort((*iter)->getName(),this,portOut->edGetType());
1042       portOut->addInPort(interceptor);
1043       _execOutGoingPorts[branchNb].push_back(interceptor);
1044     }
1045 }
1046
1047 void ForEachLoop::checkLinkPossibility(OutPort *start, const std::list<ComposedNode *>& pointsOfViewStart,
1048                                        InPort *end, const std::list<ComposedNode *>& pointsOfViewEnd) throw(YACS::Exception)
1049 {
1050   DynParaLoop::checkLinkPossibility(start, pointsOfViewStart, end, pointsOfViewEnd);
1051   if(end->getNode() == &_splitterNode)
1052     throw Exception("Illegal link within a foreach loop: \
1053 the 'SmplsCollection' port cannot be linked within the scope of the loop.");
1054   if(end == &_nbOfBranches)
1055     throw Exception("Illegal link within a foreach loop: \
1056 the 'nbBranches' port cannot be linked within the scope of the loop.");
1057 }
1058
1059 std::list<OutputPort *> ForEachLoop::getLocalOutputPorts() const
1060 {
1061   list<OutputPort *> ret;
1062   ret.push_back(getOutputPort(NAME_OF_SPLITTED_SEQ_OUT)); 
1063   return ret;
1064 }
1065
1066 void ForEachLoop::accept(Visitor *visitor)
1067 {
1068   visitor->visitForEachLoop(this);
1069 }
1070
1071 //! Dump the node state to a stream
1072 /*!
1073  * \param os : the output stream
1074  */
1075 void ForEachLoop::writeDot(std::ostream &os) const
1076 {
1077   os << "  subgraph cluster_" << getId() << "  {\n" ;
1078   //only one node in a loop
1079   if(_node)
1080     {
1081       _node->writeDot(os);
1082       os << getId() << " -> " << _node->getId() << ";\n";
1083     }
1084   os << "}\n" ;
1085   os << getId() << "[fillcolor=\"" ;
1086   YACS::StatesForNode state=getEffectiveState();
1087   os << getColorState(state);
1088   os << "\" label=\"" << "Loop:" ;
1089   os << getName() <<"\"];\n";
1090 }
1091
1092 //! Reset the state of the node and its children depending on the parameter level
1093 void ForEachLoop::resetState(int level)
1094 {
1095   if(level==0)return;
1096   DynParaLoop::resetState(level);
1097   _execCurrentId=0;
1098   //Note: cleanDynGraph is not a virtual method (must be called from ForEachLoop object) 
1099   cleanDynGraph();
1100 }
1101
1102 std::string ForEachLoop::getProgress() const
1103 {
1104   int nbElems(getNbOfElementsToBeProcessed());
1105   std::stringstream aProgress;
1106   if (nbElems > 0)
1107     aProgress << _currentIndex << "/" << nbElems;
1108   else
1109     aProgress << "0";
1110   return aProgress.str();
1111 }
1112
1113 //! Get the progress weight for all elementary nodes
1114 /*!
1115  * Only elementary nodes have weight. For each node in the loop, the weight done is multiplied
1116  * by the number of elements done and the weight total by the number total of elements
1117  */
1118 list<ProgressWeight> ForEachLoop::getProgressWeight() const
1119 {
1120   list<ProgressWeight> ret;
1121   list<Node *> setOfNode=edGetDirectDescendants();
1122   int elemDone=getCurrentIndex();
1123   int elemTotal=getNbOfElementsToBeProcessed();
1124   for(list<Node *>::const_iterator iter=setOfNode.begin();iter!=setOfNode.end();iter++)
1125     {
1126       list<ProgressWeight> myCurrentSet=(*iter)->getProgressWeight();
1127       for(list<ProgressWeight>::iterator iter=myCurrentSet.begin();iter!=myCurrentSet.end();iter++)
1128         {
1129           (*iter).weightDone=((*iter).weightTotal) * elemDone;
1130           (*iter).weightTotal*=elemTotal;
1131         }
1132       ret.insert(ret.end(),myCurrentSet.begin(),myCurrentSet.end());
1133     }
1134   return ret;
1135 }
1136
1137 int ForEachLoop::getNbOfElementsToBeProcessed() const
1138 {
1139   int nbBranches = _nbOfBranches.getIntValue();
1140   return _splitterNode.getNumberOfElements()
1141          + (_initNode ? nbBranches:0)
1142          + (_finalizeNode ? nbBranches:0) ;
1143 }
1144
1145 /*!
1146  * This method allows to retrieve the state of \a this during execution or after. This method works even if this is \b NOT complete, or during execution or after a failure in \a this.
1147  * The typical usage of this method is to retrieve the results of items that passed successfully to avoid to lose all of them if only one fails.
1148  * This method has one input \a execut and 3 outputs.
1149  *
1150  * \param [in] execut - The single input is for threadsafety reasons because this method can be called safely during the execution of \a this.
1151  * \param [out] outputs - For each output ports in \a this linked with nodes sharing the same father than \a this the passed results are stored.
1152  *                        All of the items in \a outputs have the same size.
1153  * \param [out] nameOfOutputs - The array with same size than \a outputs, that tells for each item in outputs the output port it refers to.
1154  * \return the list of ids among \c this->edGetSeqOfSamplesPort() that run successfully. The length of this returned array will be the length of all
1155  *         SequenceAny objects contained in \a outputs.
1156  *
1157  * \sa edGetSeqOfSamplesPort
1158  */
1159 std::vector<unsigned int> ForEachLoop::getPassedResults(Executor *execut, std::vector<SequenceAny *>& outputs, std::vector<std::string>& nameOfOutputs) const
1160 {
1161   YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&(execut->getTheMutexForSchedulerUpdate()));
1162   if(_execVals.empty())
1163     return std::vector<unsigned int>();
1164   if(_execOutGoingPorts.empty())
1165     return std::vector<unsigned int>();
1166   std::size_t sz(_execVals.size()); outputs.resize(sz); nameOfOutputs.resize(sz);
1167   const std::vector<AnyInputPort *>& ports(_execOutGoingPorts[0]);
1168   for(std::size_t i=0;i<sz;i++)
1169     {
1170       outputs[i]=_execVals[i]->removeUnsetItemsFromThis();
1171       nameOfOutputs[i]=ports[i]->getName();
1172     }
1173   return _execVals[0]->getSetItems();
1174 }
1175
1176 /*!
1177  * This method is typically useful for post-mortem relaunch to avoid to recompute already passed cases. This method takes in input exactly the parameters retrieved by
1178  * getPassedResults method.
1179  */
1180 void ForEachLoop::assignPassedResults(const std::vector<unsigned int>& passedIds, const std::vector<SequenceAny *>& passedOutputs, const std::vector<std::string>& nameOfOutputs)
1181 {
1182   delete _passedData;
1183   _failedCounter=0;
1184   _passedData=new ForEachLoopPassedData(passedIds,passedOutputs,nameOfOutputs);
1185 }
1186