]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/Node.cxx
Salome HOME
PR: first version from Antony GEAY, with directory restructuration
[modules/yacs.git] / src / engine / Node.cxx
1 #include "Node.hxx"
2 #include "InputPort.hxx"
3 #include "OutputPort.hxx"
4 #include "ComposedNode.hxx"
5 #include "InputDataStreamPort.hxx"
6 #include "OutputDataStreamPort.hxx"
7
8 using namespace YACS::ENGINE;
9
10 Node::Node(const std::string& name):_name(name),_inGate(this),_outGate(this),_father(0),_colour(YACS::White),_state(YACS::INITED)
11 {
12 }
13
14 Node::~Node()
15 {
16 }
17
18 void Node::init()
19 {
20   _inGate.exReset();
21   for(std::list<OutputPort *>::iterator iter=_listOfOutputPort.begin();iter!=_listOfOutputPort.end();iter++)
22     (*iter)->exInit();
23   for(std::list<InputPort *>::iterator iter2=_listOfInputPort.begin();iter2!=_listOfInputPort.end();iter2++)
24     (*iter2)->exInit();
25   if(_inGate.exIsReady())
26     _state=YACS::TOACTIVATE;
27   else
28     _state=YACS::INITED;
29 }
30
31 std::list<Node *> Node::getOutNodes() const
32 {
33   std::list<Node *> ret;
34   std::list<InGate *> inGates=_outGate.edListInGate();
35   for(std::list<InGate *>::iterator iter=inGates.begin();iter!=inGates.end();iter++)
36     ret.push_back((*iter)->getNode());
37   return ret;
38 }
39
40 /**
41  * @ note : Update the '_state' attribute.
42  *          Typically called by 'this->_inGate' when 'this->_inGate' is ready.
43  */
44 void Node::exUpdateState()
45 {
46   if(_inGate.exIsReady())
47     if(areAllInputPortsValid())
48       _state=YACS::TOACTIVATE;
49     else
50       {
51         std::string what("Node::exUpdateState : Invalid graph given : Node with name \"");
52         what+=_name; what+="\" ready to run whereas some inputports are not set correctly\nCheck coherence DF/CF";
53         throw Exception(what);
54       }
55 }
56
57 int Node::getNumberOfInputPorts() const
58 {
59   return _listOfInputPort.size();
60 }
61
62 int Node::getNumberOfOutputPorts() const
63 {
64   return _listOfOutputPort.size();
65 }
66
67 InputPort *Node::getInputPort(const std::string& name) const throw(Exception)
68 {
69   return getPort<InputPort>(name,_listOfInputPort);
70 }
71
72 OutputPort *Node::getOutputPort(const std::string& name) const throw(Exception)
73 {
74   return getPort<OutputPort>(name,_listOfOutputPort);
75 }
76
77 InputDataStreamPort *Node::getInputDataStreamPort(const std::string& name) const throw(Exception)
78 {
79   return getPort<InputDataStreamPort>(name,_listOfInputDataStreamPort);
80 }
81
82 OutputDataStreamPort *Node::getOutputDataStreamPort(const std::string& name) const throw(Exception)
83 {
84   return getPort<OutputDataStreamPort>(name,_listOfOutputDataStreamPort);
85 }
86
87 std::list<ComposedNode *> Node::getAllAscendanceOf(ComposedNode *levelToStop)
88 {
89   std::list<ComposedNode *> ret;
90   for(ComposedNode *iter=_father;iter!=levelToStop && iter!=0; iter=iter->_father)
91       ret.push_back(iter);
92   return ret;
93 }
94
95 bool Node::areAllInputPortsValid() const
96 {
97   bool ret=true;
98   for(std::list<InputPort *>::const_iterator iter=_listOfInputPort.begin();iter!=_listOfInputPort.end();iter++)
99     ret=!(*iter)->exGet().empty();
100   return ret;
101 }
102
103 ComposedNode *Node::getRootNode() throw(Exception)
104 {
105   if(!_father)
106     throw Exception("No root node");
107   ComposedNode *iter=_father;
108   while(iter->_father)
109     iter=iter->_father;
110   return iter;
111 }
112
113 void Node::checkValidityOfPortName(const std::string& name) throw(Exception)
114 {
115   if(name.find(SEP_CHAR_IN_PORT, 0 )!=std::string::npos)
116     {
117       std::string what("Port name "); what+=name; what+="not valid because it contains character "; what+=SEP_CHAR_IN_PORT;
118       throw Exception(what);
119     }
120 }
121
122 /**
123  * @ note : Check that 'node1' and 'node2' have exactly the same father
124  * @ exception : If 'node1' and 'node2' have NOT exactly the same father
125  */
126 ComposedNode *Node::checkHavingCommonFather(Node *node1, Node *node2) throw(Exception)
127 {
128   if(node1!=0 && node2!=0)
129     {
130       if(node1->_father==node2->_father)
131         return node1->_father;
132     }
133   throw Exception("check failed : nodes have not the same father");
134 }
135
136 void Node::initForDFS() const
137 {
138   _colour=YACS::White;
139   std::list<InGate *> inGates=_outGate.edListInGate();
140   for(std::list<InGate *>::iterator iter=inGates.begin();iter!=inGates.end();iter++)
141     (*iter)->initForDFS();
142 }