]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/WhileLoop.cxx
Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / engine / WhileLoop.cxx
1 //  Copyright (C) 2006-2008  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 #include "WhileLoop.hxx"
20 #include "Runtime.hxx"
21 #include "OutputPort.hxx"
22 #include "Visitor.hxx"
23 #include <iostream>
24
25 //#define _DEVDEBUG_
26 #include "YacsTrace.hxx"
27
28 using namespace YACS::ENGINE;
29 using namespace std;
30
31 const char WhileLoop::NAME_OF_INPUT_CONDITION[]="condition";
32
33 WhileLoop::WhileLoop(const std::string& name):Loop(name),_conditionPort(NAME_OF_INPUT_CONDITION,this)
34 {
35 }
36
37 WhileLoop::WhileLoop(const WhileLoop& other, ComposedNode *father, bool editionOnly):Loop(other,father,editionOnly),
38                                                                                      _conditionPort(other._conditionPort,this)
39 {
40 }
41
42 void WhileLoop::init(bool start)
43 {
44   Loop::init(start);
45   _conditionPort.exInit(start);
46 }
47
48 void WhileLoop::exUpdateState()
49 {
50   if(_state == YACS::DISABLED)
51     return;
52   if(_inGate.exIsReady())
53     {
54       setState(YACS::TOACTIVATE);
55       _node->exUpdateState();
56       if(_conditionPort.isLinkedOutOfScope())
57         if(_conditionPort.isEmpty())
58           {
59             delete _nodeForNullTurnOfLoops;
60             _nodeForNullTurnOfLoops=new FakeNodeForLoop(this,false,true);
61           }
62         else
63           if(!_conditionPort.getValue())
64             {
65               bool normalFinish=getAllOutPortsLeavingCurrentScope().empty();
66               delete _nodeForNullTurnOfLoops;
67               _nodeForNullTurnOfLoops=new FakeNodeForLoop(this,normalFinish);
68             }
69           else
70             {
71               delete _nodeForNullTurnOfLoops;
72               _nodeForNullTurnOfLoops=0;
73             }
74     }
75 }
76
77 Node *WhileLoop::simpleClone(ComposedNode *father, bool editionOnly) const
78 {
79   return new WhileLoop(*this,father,editionOnly);
80 }
81
82 InputPort *WhileLoop::getInputPort(const std::string& name) const throw(Exception)
83 {
84   if(name==NAME_OF_INPUT_CONDITION)
85     return (InputPort*)&_conditionPort;
86   return Loop::getInputPort(name);
87 }
88
89 //! Method used to notify the node that a child node has ended
90 /*!
91  * Update the loop state and return the loop change state
92  *
93  *  \param node : the child node that has ended
94  *  \return the loop state change
95  */
96 YACS::Event WhileLoop::updateStateOnFinishedEventFrom(Node *node)
97 {
98   _nbOfTurns++;
99   if(!_conditionPort.getValue())
100     {
101       setState(YACS::DONE);
102       return YACS::FINISH;
103     }
104   else    
105     {
106       node->init(false);
107       node->exUpdateState();
108     }
109   return YACS::NOEVENT;
110 }
111
112 void WhileLoop::checkLinkPossibility(OutPort *start, 
113                                      const std::list<ComposedNode *>& pointsOfViewStart,
114                                      InPort *end, 
115                                      const std::list<ComposedNode *>& pointsOfViewEnd) throw(Exception)
116 {
117   DEBTRACE("WhileLoop::checkLinkPossibility");
118 }
119
120 void WhileLoop::accept(Visitor *visitor)
121 {
122   visitor->visitWhileLoop(this);
123 }
124
125 std::list<InputPort *> WhileLoop::getLocalInputPorts() const
126 {
127   list<InputPort *> ret;
128   ret.push_back((InputPort *)&_conditionPort);
129   return ret;
130 }