Version: 9.15.0
ForLoop.cxx
Go to the documentation of this file.
1 // Copyright (C) 2006-2025 CEA, EDF
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 "ForLoop.hxx"
21 #include "Runtime.hxx"
22 #include "LinkInfo.hxx"
23 #include "OutputPort.hxx"
24 #include "Visitor.hxx"
25 #include <iostream>
26 
27 //#define _DEVDEBUG_
28 #include "YacsTrace.hxx"
29 
30 using namespace YACS::ENGINE;
31 using namespace std;
32 
42 const char ForLoop::NAME_OF_NSTEPS_NUMBER[]="nsteps";
43 const char ForLoop::NAME_OF_INDEX[]="index";
44 
45 ForLoop::ForLoop(const std::string& name):Loop(name),_nbOfTimesPort(NAME_OF_NSTEPS_NUMBER,this,Runtime::_tc_int),
46  _indexPort(NAME_OF_INDEX,this,Runtime::_tc_int)
47 {
48 }
49 
50 ForLoop::ForLoop(const ForLoop& other, ComposedNode *father, bool editionOnly):Loop(other,father,editionOnly),
51  _nbOfTimesPort(other._nbOfTimesPort,this),
52  _indexPort(other._indexPort,this)
53 {
54  //Copy Data linking
55  std::vector< std::pair<OutPort *, InPort *> > linksToReproduce=other.getSetOfInternalLinks();
56  std::vector< std::pair<OutPort *, InPort *> >::iterator iter=linksToReproduce.begin();
57  for(;iter!=linksToReproduce.end();++iter)
58  {
59  OutPort* pout = iter->first;
60  InPort* pin = iter->second;
61  edAddLink(getOutPort(other.getPortName(pout)),getInPort(other.getPortName(pin)));
62  }
63 }
64 
65 Node *ForLoop::simpleClone(ComposedNode *father, bool editionOnly) const
66 {
67  return new ForLoop(*this,father,editionOnly);
68 }
69 
70 InputPort* ForLoop::getInputPort(const std::string& name) const
71 {
72  if(name == NAME_OF_NSTEPS_NUMBER)return (InputPort*)&_nbOfTimesPort;
73  return Loop::getInputPort(name);
74 
75 }
76 
78 
85 void ForLoop::init(bool start)
86 {
87  DEBTRACE("ForLoop::init " << start);
88  Loop::init(start);
89  _nbOfTimesPort.exInit(start);
92  _indexPort.put(tmp);
93  tmp->decrRef();
95 }
96 
98 
104 {
105  DEBTRACE("ForLoop::exUpdateState " << getName() << " " << _state);
106  if(_state == YACS::DISABLED)
107  return;
108  if(_inGate.exIsReady())
109  {
111  _node->exUpdateState();
112  if(_nbOfTimesPort.isEmpty())
113  {
115  _nodeForNullTurnOfLoops=new FakeNodeForLoop(this,false,true);
116  }
117  else
118  {
119  if(_nbOfTimesPort.getIntValue()==0)
120  {
121  bool normalFinish=getAllOutPortsLeavingCurrentScope().empty();
123  _nodeForNullTurnOfLoops=new FakeNodeForLoop(this,normalFinish);
124  }
125  else if(_nbOfTimesPort.getIntValue()<0)
126  {
129  }
130  else
131  {
134  }
135  }
136  }
137 }
138 
140 
147 {
148  DEBTRACE("ForLoop::updateStateOnFinishedEventFrom " << node->getName());
151  {
153  return YACS::FINISH;
154  }
155  else
156  {
158  _indexPort.put(tmp);
159  tmp->decrRef();
161  _node->init(false);
162  _node->exUpdateState();
163  }
164  return YACS::NOEVENT;
165 }
166 
168 {
169  // emit notification to all observers registered with the dispatcher on any change of the node's state
170  sendEvent("progress");
171 }
172 
173 void ForLoop::accept(Visitor *visitor)
174 {
175  visitor->visitForLoop(this);
176 }
177 
178 std::list<InputPort *> ForLoop::getLocalInputPorts() const
179 {
180  list<InputPort *> ret;
181  ret.push_back((InputPort *)&_nbOfTimesPort);
182  return ret;
183 }
184 
185 OutPort *ForLoop::getOutPort(const std::string& name) const
186 {
187  if(name==NAME_OF_INDEX)
188  return (OutPort *)&_indexPort;
189  return Loop::getOutPort(name);
190 }
191 
192 OutputPort *ForLoop::getOutputPort(const std::string& name) const
193 {
194  if(name==NAME_OF_INDEX)
195  return (OutputPort *)&_indexPort;
196  return Loop::getOutputPort(name);
197 }
198 
199 std::list<OutputPort *> ForLoop::getLocalOutputPorts() const
200 {
201  list<OutputPort *> ret;
202  ret.push_back(getOutputPort(NAME_OF_INDEX));
203  return ret;
204 }
205 
206 void ForLoop::checkControlDependancy(OutPort *start, InPort *end, bool cross,
207  std::map < ComposedNode *, std::list < OutPort * >, SortHierarc >& fw,
208  std::vector<OutPort *>& fwCross,
209  std::map< ComposedNode *, std::list < OutPort *>, SortHierarc >& bw,
210  LinkInfo& info) const
211 {
212  //First testing if start==indexPort. This is the only case possible in theory.
213  if(start != &_indexPort)
214  return StaticDefinedComposedNode::checkControlDependancy(start,end,cross,fw,fwCross,bw,info);
215  if(cross)
216  throw Exception("Internal error occured - cross type link detected on decision port of a loop. Forbidden !");
217  fw[(ComposedNode *)this].push_back(start);
218 }
219 
220 void ForLoop::checkCFLinks(const std::list<OutPort *>& starts, InputPort *end, unsigned char& alreadyFed,
221  bool direction, LinkInfo& info) const
222 {
223  const char what[]="ForLoop::checkCFLinks : internal error.";
224  Node *nodeEnd=end->getNode();
225  if(nodeEnd==this)
226  {//In this case 'end' port is a special port of this (for exemple ForLoop::_nbOfTimesPort)
227  solveObviousOrDelegateCFLinks(starts,end,alreadyFed,direction,info);
228  }
229  else if(isInMyDescendance(nodeEnd)==0)
230  {
231  solveObviousOrDelegateCFLinks(starts,end,alreadyFed,direction,info);
232  }
233  else
234  {//no forwarding here.
235  if(starts.size()!=1)
236  throw Exception(what);
237 
238  Node *nodeStart=(*(starts.begin()))->getNode();
239  if(nodeStart==this)
240  {
241  // Link between the loop and the internal node
242  if(*(starts.begin())!=&_indexPort)
243  throw Exception(what);
244  }
245  else
246  {
247  // Link from internal node to internal node
248  if(nodeEnd!=nodeStart)
249  throw Exception(what);
250  }
251 
252  if(alreadyFed==FREE_ST)
253  alreadyFed=FED_ST;
254  else if(alreadyFed==FED_ST)
255  {
256  info.pushInfoLink(*(starts.begin()),end,I_USELESS);
257  }
258  }
259 }
260 
261 std::list<OutputPort *> ForLoop::getSetOfOutputPort() const
262 {
263  list<OutputPort *> ret=ComposedNode::getSetOfOutputPort();
264  ret.push_back((OutputPort *)&_indexPort);
265  return ret;
266 }
267 
268 
270 {
271  AnyInputPort* aNbStepsPort = (AnyInputPort*)&_nbOfTimesPort;
272  int nbSteps = 0;
273  if (aNbStepsPort && !aNbStepsPort->isEmpty())
274  nbSteps = aNbStepsPort->getIntValue();
275  return nbSteps;
276 }
277 
278 std::string ForLoop::getProgress() const
279 {
280  std::stringstream aProgress;
281  aProgress << "0";
282  int nbSteps = getNbSteps();
283  if (nbSteps > 0 && _nbOfTurns >= 0)
284  {
285  aProgress.str("");
286  aProgress << _nbOfTurns << "/" << nbSteps;
287  }
288  return aProgress.str();
289 }
290 
292 
297 list<ProgressWeight> ForLoop::getProgressWeight() const
298 {
299  list<ProgressWeight> ret;
300  list<Node *> setOfNode=edGetDirectDescendants();
301  int nbStepsDone=getNbOfTurns();
302  int nbStepsTotal=getNbSteps();
303  for(list<Node *>::const_iterator iter=setOfNode.begin();iter!=setOfNode.end();iter++)
304  {
305  list<ProgressWeight> myCurrentSet=(*iter)->getProgressWeight();
306  for(list<ProgressWeight>::iterator iter=myCurrentSet.begin();iter!=myCurrentSet.end();iter++)
307  {
308  (*iter).weightDone=((*iter).weightTotal) * nbStepsDone;
309  (*iter).weightTotal*=nbStepsTotal;
310  }
311  ret.insert(ret.end(),myCurrentSet.begin(),myCurrentSet.end());
312  }
313  return ret;
314 }
315 
#define DEBTRACE(msg)
Definition: YacsTrace.hxx:31
virtual void put(const void *data)
: Interface for management of storage of data formated dynamically in its TypeCode....
Definition: Any.hxx:79
static AtomAny * New(T val)
Definition: Any.hxx:126
Base class for all composed nodes.
static const unsigned char FREE_ST
OutPort * getOutPort(const std::string &name) const
std::vector< std::pair< OutPort *, InPort * > > getSetOfInternalLinks() const
std::string getName() const
void solveObviousOrDelegateCFLinks(const std::list< OutPort * > &starts, InputPort *end, unsigned char &alreadyFed, bool direction, LinkInfo &info) const
OutputPort * getOutputPort(const std::string &name) const
Get an output port given its name.
InputPort * getInputPort(const std::string &name) const
Get an input port given its name.
std::list< OutputPort * > getSetOfOutputPort() const
std::set< OutPort * > getAllOutPortsLeavingCurrentScope() const
List all output ports of children nodes that are linked to out of scope input ports.
std::string getPortName(const PORT *port) const
static const unsigned char FED_ST
bool edAddLink(OutPort *start, InPort *end)
Add a dataflow link between two data ports.
Node * isInMyDescendance(Node *nodeToTest) const
Returns the parent of a node that is the direct child of this node.
Class for for loop node.
Definition: ForLoop.hxx:33
OutputPort * getOutputPort(const std::string &name) const
Get an output port given its name.
Definition: ForLoop.cxx:192
std::list< InputPort * > getLocalInputPorts() const
redefined on derived class of ComposedNode. by default a ComposedNode has no port by itself
Definition: ForLoop.cxx:178
void exUpdateState()
Update the state of the for loop.
Definition: ForLoop.cxx:103
virtual void accept(Visitor *visitor)
Definition: ForLoop.cxx:173
void checkControlDependancy(OutPort *start, InPort *end, bool cross, std::map< ComposedNode *, std::list< OutPort * >, SortHierarc > &fw, std::vector< OutPort * > &fwCross, std::map< ComposedNode *, std::list< OutPort * >, SortHierarc > &bw, LinkInfo &info) const
Definition: ForLoop.cxx:206
std::string getProgress() const
Definition: ForLoop.cxx:278
std::list< OutputPort * > getSetOfOutputPort() const
Definition: ForLoop.cxx:261
void checkCFLinks(const std::list< OutPort * > &starts, InputPort *end, unsigned char &alreadyFed, bool direction, LinkInfo &info) const
check control flow links
Definition: ForLoop.cxx:220
InputPort * getInputPort(const std::string &name) const
Get an input port given its name.
Definition: ForLoop.cxx:70
ForLoop(const ForLoop &other, ComposedNode *father, bool editionOnly)
Definition: ForLoop.cxx:50
static const char NAME_OF_NSTEPS_NUMBER[]
Definition: ForLoop.hxx:36
OutPort * getOutPort(const std::string &name) const
Definition: ForLoop.cxx:185
void init(bool start=true)
Initialize the node.
Definition: ForLoop.cxx:85
static const char NAME_OF_INDEX[]
Definition: ForLoop.hxx:35
YACS::Event updateStateOnFinishedEventFrom(Node *node)
Method used to notify the node that a child node has ended.
Definition: ForLoop.cxx:146
AnyInputPort _nbOfTimesPort
Definition: ForLoop.hxx:37
int getNbSteps() const
Definition: ForLoop.cxx:269
std::list< ProgressWeight > getProgressWeight() const
Get the progress weight for all elementary nodes.
Definition: ForLoop.cxx:297
std::list< OutputPort * > getLocalOutputPorts() const
redefined on derived class of ComposedNode. by default a ComposedNode has no port by itself
Definition: ForLoop.cxx:199
Node * simpleClone(ComposedNode *father, bool editionOnly=true) const
Definition: ForLoop.cxx:65
AnyOutputPort _indexPort
Definition: ForLoop.hxx:38
bool exIsReady() const
Definition: InGate.cxx:126
Base class for Input Ports.
Definition: InputPort.hxx:44
virtual void exInit(bool start)
Definition: InputPort.cxx:63
Class that deal with list of semantics links for high level analysis.
Definition: LinkInfo.hxx:83
void pushInfoLink(OutPort *semStart, InPort *end, InfoReason reason)
Definition: LinkInfo.cxx:71
Base class for loop node.
Definition: Loop.hxx:147
void init(bool start=true)
Definition: Loop.cxx:319
std::list< Node * > edGetDirectDescendants() const
Definition: Loop.cxx:401
int getNbOfTurns() const
Definition: Loop.hxx:162
friend class FakeNodeForLoop
Definition: Loop.hxx:149
FakeNodeForLoop * _nodeForNullTurnOfLoops
Definition: Loop.hxx:154
Base class for all nodes.
Definition: Node.hxx:70
InGate _inGate
Definition: Node.hxx:86
virtual void exUpdateState()
Update the node state.
Definition: Node.cxx:206
virtual void init(bool start=true)
Definition: Node.cxx:102
void setState(YACS::StatesForNode theState)
Sets the given state for node.
Definition: Node.cxx:652
virtual void sendEvent(const std::string &event)
emit notification to all observers registered with the dispatcher
Definition: Node.cxx:691
InPort * getInPort(const std::string &name) const
Definition: Node.cxx:239
const std::string & getName() const
Definition: Node.hxx:125
YACS::StatesForNode _state
Definition: Node.hxx:91
virtual void exInit()
Definition: OutputPort.cxx:62
Node * getNode() const
Definition: Port.hxx:46
void checkControlDependancy(OutPort *start, InPort *end, bool cross, std::map< ComposedNode *, std::list< OutPort * >, SortHierarc > &fw, std::vector< OutPort * > &fwCross, std::map< ComposedNode *, std::list< OutPort * >, SortHierarc > &bw, LinkInfo &info) const
virtual void visitForLoop(ForLoop *node)=0
Event
Definition: define.hxx:56
@ NOEVENT
Definition: define.hxx:57
@ FINISH
Definition: define.hxx:59
@ ACTIVATED
Definition: define.hxx:41
@ DONE
Definition: define.hxx:43
@ DISABLED
Definition: define.hxx:50