]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/ForEachLoop.cxx
Salome HOME
83af9c7e7c9d5b738338da5a7eef930b71dc19ac
[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       sendEvent2("progress_ok",&globalId);
765       storeOutValsInSeqForOutOfScopeUse(globalId,id);
766     }
767   else
768     {
769       int globalId(_execIds[id]);
770       if(_passedData)
771         globalId=_passedData->toAbsId(globalId);
772       sendEvent2("progress_ko",&globalId);
773     }
774   //
775   if(_execCurrentId==getFinishedId())
776     {//No more elements of _dataPortToDispatch to treat
777       _execIds[id]=NOT_RUNNING_BRANCH_ID;
778       //analyzing if some samples are still on treatment on other branches.
779       bool isFinished(true);
780       for(int i=0;i<_execIds.size() && isFinished;i++)
781         isFinished=(_execIds[i]==NOT_RUNNING_BRANCH_ID);
782       if(isFinished)
783         {
784           try
785           {
786               if(_failedCounter!=0)
787                 {// case of keepgoing mode + a failed
788                   std::ostringstream oss; oss << "Keep Going mode activated and some errors (" << _failedCounter << ")reported !";
789                   DEBTRACE("ForEachLoop::updateStateOnFinishedEventFrom : "<< oss.str());
790                   setState(YACS::FAILED);
791                   return YACS::ABORT;
792                 }
793               pushAllSequenceValues();
794
795               if (_node)
796                 {
797                   _node->setState(YACS::DONE);
798
799                   ComposedNode* compNode = dynamic_cast<ComposedNode*>(_node);
800                   if (compNode)
801                     {
802                       std::list<Node *> aChldn = compNode->getAllRecursiveConstituents();
803                       std::list<Node *>::iterator iter=aChldn.begin();
804                       for(;iter!=aChldn.end();iter++)
805                         (*iter)->setState(YACS::DONE);
806                     }
807                 }
808
809               if (_finalizeNode == NULL)
810                 {
811                   // No finalize node, we just finish the loop at the end of exec nodes execution
812                   setState(YACS::DONE);
813                   return YACS::FINISH;
814                 }
815               else
816                 {
817                   // Run the finalize nodes, the loop will be done only when they all finish
818                   _unfinishedCounter = 0;  // This counter indicates how many branches are not finished
819                   for (int i=0 ; i<_execIds.size() ; i++)
820                     {
821                       YASSERT(_execIds[i] == NOT_RUNNING_BRANCH_ID);
822                       DEBTRACE("Launching finalize node for branch " << i);
823                       _execFinalizeNodes[i]->exUpdateState();
824                       _unfinishedCounter++;
825                     }
826                   return YACS::NOEVENT;
827                 }
828           }
829           catch(YACS::Exception& ex)
830           {
831               DEBTRACE("ForEachLoop::updateStateOnFinishedEventFrom: "<<ex.what());
832               //no way to push results : put following nodes in FAILED state
833               //TODO could be more fine grain : put only concerned nodes in FAILED state
834               exForwardFailed();
835               setState(YACS::ERROR);
836               return YACS::ABORT;
837           }
838         }
839     }
840   else if(_state == YACS::ACTIVATED)
841     {//more elements to do and loop still activated
842       _execIds[id]=_execCurrentId;
843       node->init(false);
844       int posInAbs(_execCurrentId);
845       if(_passedData)
846         posInAbs=_passedData->toAbsId(_execCurrentId);
847       _splitterNode.putSplittedValueOnRankTo(posInAbs,id,false);
848       _execCurrentId++;
849       node->exUpdateState();
850       forwardExecStateToOriginalBody(node);
851       _nbOfEltConsumed++;
852     }
853   else
854     {//elements to process and loop no more activated
855       DEBTRACE("foreach loop state " << _state);
856     }
857   return YACS::NOEVENT;
858 }
859
860 YACS::Event ForEachLoop::updateStateForFinalizeNodeOnFinishedEventFrom(Node *node, unsigned int id)
861 {
862   DEBTRACE("Finalize node finished on branch " << id);
863   _unfinishedCounter--;
864   _currentIndex++;
865   exUpdateProgress();
866   DEBTRACE(_unfinishedCounter << " finalize nodes still running");
867   if (_unfinishedCounter == 0)
868     {
869       _finalizeNode->setState(YACS::DONE);
870       setState(YACS::DONE);
871       return YACS::FINISH;
872     }
873   else
874     return YACS::NOEVENT;
875 }
876
877 YACS::Event ForEachLoop::updateStateOnFailedEventFrom(Node *node, const Executor *execInst)
878 {
879   unsigned int id;
880   DynParaLoop::TypeOfNode ton(getIdentityOfNotifyerNode(node,id));
881   if(ton!=WORK_NODE || !execInst->getKeepGoingProperty())
882     return DynParaLoop::updateStateOnFailedEventFrom(node,execInst);
883   else
884     {
885       _failedCounter++;
886       return updateStateForWorkNodeOnFinishedEventFrom(node,id,false);
887     }
888 }
889
890 void ForEachLoop::InterceptorizeNameOfPort(std::string& portName)
891 {
892   std::replace_if(portName.begin(), portName.end(), std::bind1st(std::equal_to<char>(), '.'), '_');
893   portName += INTERCEPTOR_STR;
894 }
895
896 void ForEachLoop::buildDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView)
897 {
898   DynParaLoop::buildDelegateOf(port,finalTarget,pointsOfView);
899   string typeOfPortInstance=(port.first)->getNameOfTypeOfCurrentInstance();
900   if(typeOfPortInstance==OutputPort::NAME)
901     {
902       vector<AnySplitOutputPort *>::iterator iter=_outGoingPorts.begin();
903       int i=0;
904       for(;iter!=_outGoingPorts.end();iter++,i++)
905         if((*iter)->getRepr()==port.first || *iter==port.first)
906           break;
907       if(iter!=_outGoingPorts.end())
908         {
909           if(*iter!=port.first)
910             {
911               (*iter)->incrRef();
912               (*iter)->addRepr(port.first,_intecptrsForOutGoingPorts[i]);
913             }
914           port.first=*iter;
915         }
916       else
917         {
918           TypeCodeSeq *newTc=(TypeCodeSeq *)TypeCode::sequenceTc("","",port.first->edGetType());
919           // The out going ports belong to the ForEachLoop, whereas
920           // the delegated port belongs to a node child of the ForEachLoop.
921           // The name of the delegated port contains dots (bloc.node.outport),
922           // whereas the name of the out going port shouldn't do.
923           std::string outputPortName(getPortName(port.first));
924           InterceptorizeNameOfPort(outputPortName);
925           AnySplitOutputPort *newPort(new AnySplitOutputPort(outputPortName,this,newTc));
926           InterceptorInputPort *intercptor(new InterceptorInputPort(outputPortName + "_in",this,port.first->edGetType()));
927           intercptor->setRepr(newPort);
928           newTc->decrRef();
929           newPort->addRepr(port.first,intercptor);
930           _outGoingPorts.push_back(newPort);
931           _intecptrsForOutGoingPorts.push_back(intercptor);
932           port.first=newPort;
933         }
934     }
935   else
936     throw Exception("ForEachLoop::buildDelegateOf : not implemented for DS because not specified");
937 }
938
939 void ForEachLoop::getDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView) throw(YACS::Exception)
940 {
941   string typeOfPortInstance=(port.first)->getNameOfTypeOfCurrentInstance();
942   if(typeOfPortInstance==OutputPort::NAME)
943     {
944       vector<AnySplitOutputPort *>::iterator iter=_outGoingPorts.begin();
945       for(;iter!=_outGoingPorts.end();iter++)
946         if((*iter)->getRepr()==port.first)
947           break;
948       if(iter==_outGoingPorts.end())
949         {
950           string what("ForEachLoop::getDelegateOf : Port with name "); what+=port.first->getName(); what+=" not exported by ForEachLoop "; what+=_name; 
951           throw Exception(what);
952         }
953       else
954         port.first=(*iter);
955     }
956   else
957     throw Exception("ForEachLoop::getDelegateOf : not implemented because not specified");
958 }
959
960 void ForEachLoop::releaseDelegateOf(OutPort *portDwn, OutPort *portUp, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView) throw(YACS::Exception)
961 {
962   string typeOfPortInstance=portDwn->getNameOfTypeOfCurrentInstance();
963   if(typeOfPortInstance==OutputPort::NAME)
964     {
965       vector<AnySplitOutputPort *>::iterator iter=_outGoingPorts.begin();
966       vector<InterceptorInputPort *>::iterator iter2=_intecptrsForOutGoingPorts.begin();
967       for(;iter!=_outGoingPorts.end();iter++,iter2++)
968         if((*iter)->getRepr()==portDwn)
969           break;
970       //ASSERT(portUp==*iter.second)
971       if((*iter)->decrRef())
972         {
973           AnySplitOutputPort *p=*iter;
974           _outGoingPorts.erase(iter);
975           delete p;
976           InterceptorInputPort *ip=*iter2;
977           _intecptrsForOutGoingPorts.erase(iter2);
978           delete ip;
979         }
980     }
981 }
982
983 OutPort *ForEachLoop::getDynOutPortByAbsName(int branchNb, const std::string& name)
984 {
985   string portName, nodeName;
986   splitNamesBySep(name,Node::SEP_CHAR_IN_PORT,nodeName,portName,false);
987   Node *staticChild = getChildByName(nodeName);
988   return _execNodes[branchNb]->getOutPort(portName);//It's impossible(garanteed by YACS::ENGINE::ForEachLoop::buildDelegateOf)
989   //that a link starting from _initNode goes out of scope of 'this'.
990 }
991
992 void ForEachLoop::cleanDynGraph()
993 {
994   DynParaLoop::cleanDynGraph();
995   for(vector< SequenceAny *>::iterator iter3=_execVals.begin();iter3!=_execVals.end();iter3++)
996     (*iter3)->decrRef();
997   _execVals.clear();
998   for(vector< vector<AnyInputPort *> >::iterator iter4=_execOutGoingPorts.begin();iter4!=_execOutGoingPorts.end();iter4++)
999     for(vector<AnyInputPort *>::iterator iter5=(*iter4).begin();iter5!=(*iter4).end();iter5++)
1000       delete *iter5;
1001   _execOutGoingPorts.clear();
1002 }
1003
1004 void ForEachLoop::storeOutValsInSeqForOutOfScopeUse(int rank, int branchNb)
1005 {
1006   vector<AnyInputPort *>::iterator iter;
1007   int i=0;
1008   for(iter=_execOutGoingPorts[branchNb].begin();iter!=_execOutGoingPorts[branchNb].end();iter++,i++)
1009     {
1010       Any *val=(Any *)(*iter)->getValue();
1011       _execVals[i]->setEltAtRank(rank,val);
1012     }
1013 }
1014
1015 int ForEachLoop::getFinishedId()
1016 {
1017   if(!_passedData)
1018     return _splitterNode.getNumberOfElements();
1019   else
1020     return _passedData->getNumberOfElementsToDo();
1021 }
1022
1023 void ForEachLoop::prepareSequenceValues(int sizeOfSamples)
1024 {
1025   _execVals.resize(_outGoingPorts.size());
1026   vector<AnySplitOutputPort *>::iterator iter=_outGoingPorts.begin();
1027   for(int i=0;iter!=_outGoingPorts.end();iter++,i++)
1028     _execVals[i]=SequenceAny::New((*iter)->edGetType()->contentType(),sizeOfSamples);
1029 }
1030
1031 void ForEachLoop::pushAllSequenceValues()
1032 {
1033   vector<AnySplitOutputPort *>::iterator iter=_outGoingPorts.begin();
1034   int i=0;
1035   for(;iter!=_outGoingPorts.end();iter++,i++)
1036     (*iter)->put((const void *)_execVals[i]);
1037 }
1038
1039 void ForEachLoop::createOutputOutOfScopeInterceptors(int branchNb)
1040 {
1041   vector<AnySplitOutputPort *>::iterator iter=_outGoingPorts.begin();
1042   int i=0;
1043   for(;iter!=_outGoingPorts.end();iter++,i++)
1044     {
1045       DEBTRACE( (*iter)->getName() << " " << (*iter)->edGetType()->kind() );
1046       //AnyInputPort *interceptor=new AnyInputPort((*iter)->getName(),this,(*iter)->edGetType());
1047       OutPort *portOut=getDynOutPortByAbsName(branchNb,getOutPortName(((*iter)->getRepr())));
1048       DEBTRACE( portOut->getName() );
1049       AnyInputPort *interceptor=new AnyInputPort((*iter)->getName(),this,portOut->edGetType());
1050       portOut->addInPort(interceptor);
1051       _execOutGoingPorts[branchNb].push_back(interceptor);
1052     }
1053 }
1054
1055 void ForEachLoop::checkLinkPossibility(OutPort *start, const std::list<ComposedNode *>& pointsOfViewStart,
1056                                        InPort *end, const std::list<ComposedNode *>& pointsOfViewEnd) throw(YACS::Exception)
1057 {
1058   DynParaLoop::checkLinkPossibility(start, pointsOfViewStart, end, pointsOfViewEnd);
1059   if(end->getNode() == &_splitterNode)
1060     throw Exception("Illegal link within a foreach loop: \
1061 the 'SmplsCollection' port cannot be linked within the scope of the loop.");
1062   if(end == &_nbOfBranches)
1063     throw Exception("Illegal link within a foreach loop: \
1064 the 'nbBranches' port cannot be linked within the scope of the loop.");
1065 }
1066
1067 std::list<OutputPort *> ForEachLoop::getLocalOutputPorts() const
1068 {
1069   list<OutputPort *> ret;
1070   ret.push_back(getOutputPort(NAME_OF_SPLITTED_SEQ_OUT)); 
1071   return ret;
1072 }
1073
1074 void ForEachLoop::accept(Visitor *visitor)
1075 {
1076   visitor->visitForEachLoop(this);
1077 }
1078
1079 //! Dump the node state to a stream
1080 /*!
1081  * \param os : the output stream
1082  */
1083 void ForEachLoop::writeDot(std::ostream &os) const
1084 {
1085   os << "  subgraph cluster_" << getId() << "  {\n" ;
1086   //only one node in a loop
1087   if(_node)
1088     {
1089       _node->writeDot(os);
1090       os << getId() << " -> " << _node->getId() << ";\n";
1091     }
1092   os << "}\n" ;
1093   os << getId() << "[fillcolor=\"" ;
1094   YACS::StatesForNode state=getEffectiveState();
1095   os << getColorState(state);
1096   os << "\" label=\"" << "Loop:" ;
1097   os << getName() <<"\"];\n";
1098 }
1099
1100 //! Reset the state of the node and its children depending on the parameter level
1101 void ForEachLoop::resetState(int level)
1102 {
1103   if(level==0)return;
1104   DynParaLoop::resetState(level);
1105   _execCurrentId=0;
1106   //Note: cleanDynGraph is not a virtual method (must be called from ForEachLoop object) 
1107   cleanDynGraph();
1108 }
1109
1110 std::string ForEachLoop::getProgress() const
1111 {
1112   int nbElems(getNbOfElementsToBeProcessed());
1113   std::stringstream aProgress;
1114   if (nbElems > 0)
1115     aProgress << _currentIndex << "/" << nbElems;
1116   else
1117     aProgress << "0";
1118   return aProgress.str();
1119 }
1120
1121 //! Get the progress weight for all elementary nodes
1122 /*!
1123  * Only elementary nodes have weight. For each node in the loop, the weight done is multiplied
1124  * by the number of elements done and the weight total by the number total of elements
1125  */
1126 list<ProgressWeight> ForEachLoop::getProgressWeight() const
1127 {
1128   list<ProgressWeight> ret;
1129   list<Node *> setOfNode=edGetDirectDescendants();
1130   int elemDone=getCurrentIndex();
1131   int elemTotal=getNbOfElementsToBeProcessed();
1132   for(list<Node *>::const_iterator iter=setOfNode.begin();iter!=setOfNode.end();iter++)
1133     {
1134       list<ProgressWeight> myCurrentSet=(*iter)->getProgressWeight();
1135       for(list<ProgressWeight>::iterator iter=myCurrentSet.begin();iter!=myCurrentSet.end();iter++)
1136         {
1137           (*iter).weightDone=((*iter).weightTotal) * elemDone;
1138           (*iter).weightTotal*=elemTotal;
1139         }
1140       ret.insert(ret.end(),myCurrentSet.begin(),myCurrentSet.end());
1141     }
1142   return ret;
1143 }
1144
1145 int ForEachLoop::getNbOfElementsToBeProcessed() const
1146 {
1147   int nbBranches = _nbOfBranches.getIntValue();
1148   return _splitterNode.getNumberOfElements()
1149          + (_initNode ? nbBranches:0)
1150          + (_finalizeNode ? nbBranches:0) ;
1151 }
1152
1153 /*!
1154  * 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.
1155  * 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.
1156  * This method has one input \a execut and 3 outputs.
1157  *
1158  * \param [in] execut - The single input is for threadsafety reasons because this method can be called safely during the execution of \a this.
1159  * \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.
1160  *                        All of the items in \a outputs have the same size.
1161  * \param [out] nameOfOutputs - The array with same size than \a outputs, that tells for each item in outputs the output port it refers to.
1162  * \return the list of ids among \c this->edGetSeqOfSamplesPort() that run successfully. The length of this returned array will be the length of all
1163  *         SequenceAny objects contained in \a outputs.
1164  *
1165  * \sa edGetSeqOfSamplesPort
1166  */
1167 std::vector<unsigned int> ForEachLoop::getPassedResults(Executor *execut, std::vector<SequenceAny *>& outputs, std::vector<std::string>& nameOfOutputs) const
1168 {
1169   YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&(execut->getTheMutexForSchedulerUpdate()));
1170   if(_execVals.empty())
1171     return std::vector<unsigned int>();
1172   if(_execOutGoingPorts.empty())
1173     return std::vector<unsigned int>();
1174   std::size_t sz(_execVals.size()); outputs.resize(sz); nameOfOutputs.resize(sz);
1175   const std::vector<AnyInputPort *>& ports(_execOutGoingPorts[0]);
1176   for(std::size_t i=0;i<sz;i++)
1177     {
1178       outputs[i]=_execVals[i]->removeUnsetItemsFromThis();
1179       nameOfOutputs[i]=ports[i]->getName();
1180     }
1181   return _execVals[0]->getSetItems();
1182 }
1183
1184 /*!
1185  * 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
1186  * getPassedResults method.
1187  */
1188 void ForEachLoop::assignPassedResults(const std::vector<unsigned int>& passedIds, const std::vector<SequenceAny *>& passedOutputs, const std::vector<std::string>& nameOfOutputs)
1189 {
1190   delete _passedData;
1191   _failedCounter=0;
1192   _passedData=new ForEachLoopPassedData(passedIds,passedOutputs,nameOfOutputs);
1193 }
1194