1 // Copyright (C) 2006-2014 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "ForLoop.hxx"
21 #include "Runtime.hxx"
22 #include "LinkInfo.hxx"
23 #include "OutputPort.hxx"
24 #include "Visitor.hxx"
28 #include "YacsTrace.hxx"
30 using namespace YACS::ENGINE;
33 /*! \class YACS::ENGINE::ForLoop
34 * \brief Class for for loop node
38 * This kind of loop makes a fixed number of steps and stops
42 const char ForLoop::NAME_OF_NSTEPS_NUMBER[]="nsteps";
43 const char ForLoop::NAME_OF_INDEX[]="index";
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)
50 ForLoop::ForLoop(const ForLoop& other, ComposedNode *father, bool editionOnly):Loop(other,father,editionOnly),
51 _nbOfTimesPort(other._nbOfTimesPort,this),
52 _indexPort(other._indexPort,this)
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)
59 OutPort* pout = iter->first;
60 InPort* pin = iter->second;
61 edAddLink(getOutPort(other.getPortName(pout)),getInPort(other.getPortName(pin)));
65 Node *ForLoop::simpleClone(ComposedNode *father, bool editionOnly) const
67 return new ForLoop(*this,father,editionOnly);
70 InputPort* ForLoop::getInputPort(const std::string& name) const throw(YACS::Exception)
72 if(name == NAME_OF_NSTEPS_NUMBER)return (InputPort*)&_nbOfTimesPort;
73 return Loop::getInputPort(name);
77 //! Initialize the node
79 * \param start: a boolean flag to indicate the initialization mode
81 * If start is true, it's a complete initialization (with port values initialization)
82 * If start is false, there is no port values initialization
85 void ForLoop::init(bool start)
87 DEBTRACE("ForLoop::init " << start);
89 _nbOfTimesPort.exInit(start);
91 Any* tmp=AtomAny::New(_nbOfTurns);
97 //! Update the state of the for loop
99 * If the inGate port is ready goes to YACS::ACTIVATED state
100 * If the steps number is 0, create an special internal node
103 void ForLoop::exUpdateState()
105 DEBTRACE("ForLoop::exUpdateState " << getName() << " " << _state);
106 if(_state == YACS::DISABLED)
108 if(_inGate.exIsReady())
110 setState(YACS::ACTIVATED);
111 _node->exUpdateState();
112 if(_nbOfTimesPort.isEmpty())
114 delete _nodeForNullTurnOfLoops;
115 _nodeForNullTurnOfLoops=new FakeNodeForLoop(this,false,true);
119 if(_nbOfTimesPort.getIntValue()==0)
121 bool normalFinish=getAllOutPortsLeavingCurrentScope().empty();
122 delete _nodeForNullTurnOfLoops;
123 _nodeForNullTurnOfLoops=new FakeNodeForLoop(this,normalFinish);
125 else if(_nbOfTimesPort.getIntValue()<0)
127 delete _nodeForNullTurnOfLoops;
128 _nodeForNullTurnOfLoops=new FakeNodeForLoop(this,false);
132 delete _nodeForNullTurnOfLoops;
133 _nodeForNullTurnOfLoops=0;
139 //! Method used to notify the node that a child node has ended
141 * Update the loop state and return the loop change state
143 * \param node : the child node that has ended
144 * \return the loop state change
146 YACS::Event ForLoop::updateStateOnFinishedEventFrom(Node *node)
148 DEBTRACE("ForLoop::updateStateOnFinishedEventFrom " << node->getName());
150 if((++_nbOfTurns)>=_nbOfTimesPort.getIntValue())
152 setState(YACS::DONE);
157 Any* tmp=AtomAny::New(_nbOfTurns);
160 setState(YACS::ACTIVATED);
162 _node->exUpdateState();
164 return YACS::NOEVENT;
167 void ForLoop::exUpdateProgress()
169 // emit notification to all observers registered with the dispatcher on any change of the node's state
170 sendEvent("progress");
173 void ForLoop::accept(Visitor *visitor)
175 visitor->visitForLoop(this);
178 std::list<InputPort *> ForLoop::getLocalInputPorts() const
180 list<InputPort *> ret;
181 ret.push_back((InputPort *)&_nbOfTimesPort);
185 OutPort *ForLoop::getOutPort(const std::string& name) const throw(YACS::Exception)
187 if(name==NAME_OF_INDEX)
188 return (OutPort *)&_indexPort;
189 return Loop::getOutPort(name);
192 OutputPort *ForLoop::getOutputPort(const std::string& name) const throw(YACS::Exception)
194 if(name==NAME_OF_INDEX)
195 return (OutputPort *)&_indexPort;
196 return Loop::getOutputPort(name);
199 std::list<OutputPort *> ForLoop::getLocalOutputPorts() const
201 list<OutputPort *> ret;
202 ret.push_back(getOutputPort(NAME_OF_INDEX));
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
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);
216 throw Exception("Internal error occured - cross type link detected on decision port of a loop. Forbidden !");
217 fw[(ComposedNode *)this].push_back(start);
220 void ForLoop::checkCFLinks(const std::list<OutPort *>& starts, InputPort *end, unsigned char& alreadyFed,
221 bool direction, LinkInfo& info) const
223 const char what[]="ForLoop::checkCFLinks : internal error.";
224 Node *nodeEnd=end->getNode();
226 {//In this case 'end' port is a special port of this (for exemple ForLoop::_nbOfTimesPort)
227 solveObviousOrDelegateCFLinks(starts,end,alreadyFed,direction,info);
229 else if(isInMyDescendance(nodeEnd)==0)
231 solveObviousOrDelegateCFLinks(starts,end,alreadyFed,direction,info);
234 {//no forwarding here.
236 throw Exception(what);
238 Node *nodeStart=(*(starts.begin()))->getNode();
241 // Link between the loop and the internal node
242 if(*(starts.begin())!=&_indexPort)
243 throw Exception(what);
247 // Link from internal node to internal node
248 if(nodeEnd!=nodeStart)
249 throw Exception(what);
252 if(alreadyFed==FREE_ST)
254 else if(alreadyFed==FED_ST)
256 info.pushInfoLink(*(starts.begin()),end,I_USELESS);
261 std::list<OutputPort *> ForLoop::getSetOfOutputPort() const
263 list<OutputPort *> ret=ComposedNode::getSetOfOutputPort();
264 ret.push_back((OutputPort *)&_indexPort);
268 std::string ForLoop::getProgress() const
270 std::stringstream aProgress;
272 AnyInputPort* aNbStepsPort = (AnyInputPort*)&_nbOfTimesPort;
273 if (aNbStepsPort && !aNbStepsPort->isEmpty()) {
274 int nbSteps = aNbStepsPort->getIntValue();
275 if (nbSteps > 0 && _nbOfTurns >= 0) {
277 aProgress << _nbOfTurns << "/" << nbSteps;
280 return aProgress.str();