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