Salome HOME
36511f266f15d5903808c65f316c0ffed11605bd
[modules/yacs.git] / src / engine / ForLoop.cxx
1 // Copyright (C) 2006-2013  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.
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
33 /*! \class YACS::ENGINE::ForLoop
34  *  \brief Class for for loop node
35  *
36  * \ingroup Nodes
37  *
38  *   This kind of loop makes a fixed number of steps and stops
39  *
40  */
41
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 throw(YACS::Exception)
71 {
72     if(name == NAME_OF_NSTEPS_NUMBER)return (InputPort*)&_nbOfTimesPort;
73     return Loop::getInputPort(name);
74
75 }
76
77 //! Initialize the node
78 /*!
79  * \param start: a boolean flag to indicate the initialization mode
80  *
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
83  *
84  */
85 void ForLoop::init(bool start)
86 {
87   DEBTRACE("ForLoop::init " << start);
88   Loop::init(start);
89   _nbOfTimesPort.exInit(start);
90   _indexPort.exInit();
91   Any* tmp=AtomAny::New(_nbOfTurns);
92   _indexPort.put(tmp);
93   tmp->decrRef();
94 }
95
96 //! Update the state of the for loop
97 /*!
98  * If the inGate port is ready goes to YACS::ACTIVATED state
99  * If the steps number is 0, create an special internal node
100  *
101  */
102 void ForLoop::exUpdateState()
103 {
104   DEBTRACE("ForLoop::exUpdateState " << getName() << " " << _state);
105   if(_state == YACS::DISABLED)
106     return;
107   if(_inGate.exIsReady())
108     {
109       setState(YACS::ACTIVATED);
110       _node->exUpdateState();
111       if(_nbOfTimesPort.isEmpty())
112         {
113           delete _nodeForNullTurnOfLoops;
114           _nodeForNullTurnOfLoops=new FakeNodeForLoop(this,false,true);
115         }
116       else
117         {
118           if(_nbOfTimesPort.getIntValue()==0)
119             {
120               bool normalFinish=getAllOutPortsLeavingCurrentScope().empty();
121               delete _nodeForNullTurnOfLoops;
122               _nodeForNullTurnOfLoops=new FakeNodeForLoop(this,normalFinish);
123             }
124           else if(_nbOfTimesPort.getIntValue()<0)
125             {
126               delete _nodeForNullTurnOfLoops;
127               _nodeForNullTurnOfLoops=new FakeNodeForLoop(this,false);
128             }
129           else
130             {
131               delete _nodeForNullTurnOfLoops;
132               _nodeForNullTurnOfLoops=0;
133             }
134         }
135     }
136 }
137
138 //! Method used to notify the node that a child node has ended
139 /*!
140  * Update the loop state and return the loop change state 
141  *
142  *  \param node : the child node that has ended
143  *  \return the loop state change
144  */
145 YACS::Event ForLoop::updateStateOnFinishedEventFrom(Node *node)
146 {
147   DEBTRACE("ForLoop::updateStateOnFinishedEventFrom " << node->getName());
148   if((++_nbOfTurns)>=_nbOfTimesPort.getIntValue())
149     {
150       setState(YACS::DONE);
151       return YACS::FINISH;
152     }
153   else
154     {
155       Any* tmp=AtomAny::New(_nbOfTurns);
156       _indexPort.put(tmp);
157       tmp->decrRef();
158       setState(YACS::ACTIVATED);
159       _node->init(false);
160       _node->exUpdateState();
161     }
162   return YACS::NOEVENT;
163 }
164
165 void ForLoop::accept(Visitor *visitor)
166 {
167   visitor->visitForLoop(this);
168 }
169
170 std::list<InputPort *> ForLoop::getLocalInputPorts() const
171 {
172   list<InputPort *> ret;
173   ret.push_back((InputPort *)&_nbOfTimesPort);
174   return ret;
175 }
176
177 OutPort *ForLoop::getOutPort(const std::string& name) const throw(YACS::Exception)
178 {
179   if(name==NAME_OF_INDEX)
180     return (OutPort *)&_indexPort;
181   return Loop::getOutPort(name);
182 }
183
184 OutputPort *ForLoop::getOutputPort(const std::string& name) const throw(YACS::Exception)
185 {
186   if(name==NAME_OF_INDEX)
187     return (OutputPort *)&_indexPort;
188   return Loop::getOutputPort(name);
189 }
190
191 std::list<OutputPort *> ForLoop::getLocalOutputPorts() const
192 {
193   list<OutputPort *> ret;
194   ret.push_back(getOutputPort(NAME_OF_INDEX)); 
195   return ret;
196 }
197
198 void ForLoop::checkControlDependancy(OutPort *start, InPort *end, bool cross,
199                                      std::map < ComposedNode *,  std::list < OutPort * >, SortHierarc >& fw,
200                                      std::vector<OutPort *>& fwCross,
201                                      std::map< ComposedNode *, std::list < OutPort *>, SortHierarc >& bw,
202                                      LinkInfo& info) const
203 {
204   //First testing if start==indexPort. This is the only case possible in theory.
205   if(start != &_indexPort)
206     return StaticDefinedComposedNode::checkControlDependancy(start,end,cross,fw,fwCross,bw,info);
207   if(cross)
208     throw Exception("Internal error occured - cross type link detected on decision port of a loop. Forbidden !");
209   fw[(ComposedNode *)this].push_back(start);
210 }
211
212 void ForLoop::checkCFLinks(const std::list<OutPort *>& starts, InputPort *end, unsigned char& alreadyFed, 
213                            bool direction, LinkInfo& info) const
214 {
215   const char what[]="ForLoop::checkCFLinks : internal error.";
216   Node *nodeEnd=end->getNode();
217   if(nodeEnd==this)
218     {//In this case 'end' port is a special port of this (for exemple ForLoop::_nbOfTimesPort)
219       solveObviousOrDelegateCFLinks(starts,end,alreadyFed,direction,info);
220     }
221   else if(isInMyDescendance(nodeEnd)==0)
222     {
223       solveObviousOrDelegateCFLinks(starts,end,alreadyFed,direction,info);
224     }
225   else
226     {//no forwarding here.
227       if(starts.size()!=1)
228         throw Exception(what);
229
230       Node *nodeStart=(*(starts.begin()))->getNode();
231       if(nodeStart==this)
232         {
233           // Link between the loop and the internal node
234           if(*(starts.begin())!=&_indexPort)
235             throw Exception(what);
236         }
237       else
238         {
239           // Link from internal node to internal node
240           if(nodeEnd!=nodeStart)
241             throw Exception(what);
242         }
243
244       if(alreadyFed==FREE_ST)
245         alreadyFed=FED_ST;
246       else if(alreadyFed==FED_ST)
247         {
248           info.pushInfoLink(*(starts.begin()),end,I_USELESS);
249         }
250     }
251 }
252
253 std::list<OutputPort *> ForLoop::getSetOfOutputPort() const
254 {
255   list<OutputPort *> ret=ComposedNode::getSetOfOutputPort();
256   ret.push_back((OutputPort *)&_indexPort);
257   return ret;
258 }
259