]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/Node.hxx
Salome HOME
PR: first version from Antony GEAY, with directory restructuration
[modules/yacs.git] / src / engine / Node.hxx
1 #ifndef __NODE_HXX__
2 #define __NODE_HXX__
3
4 #include "define.hxx"
5 #include "InGate.hxx"
6 #include "OutGate.hxx"
7 #include "Exception.hxx"
8
9 #include <list>
10 #include <string>
11 #include <vector>
12
13 namespace YACS
14 {
15   namespace ENGINE
16   {
17     class Task;
18     class InputPort;
19     class OutputPort;
20     class ComposedNode;
21     class ElementaryNode;
22     class InputDataStreamPort;
23     class OutputDataStreamPort;
24     
25     class Node
26     {
27       friend class Bloc;
28       friend class ComposedNode;
29       friend class ElementaryNode;
30     protected:
31       InGate _inGate;
32       OutGate _outGate;
33       std::string _name;
34       ComposedNode *_father;
35       YACS::StatesForNode _state;
36       std::list<InputPort *> _listOfInputPort;
37       std::list<OutputPort *> _listOfOutputPort;
38       std::list<InputDataStreamPort *> _listOfInputDataStreamPort;
39       std::list<OutputDataStreamPort *> _listOfOutputDataStreamPort;
40       static const char SEP_CHAR_IN_PORT='?';
41     private:
42       //for graphs algorithms
43       mutable YACS::Colour _colour;
44     protected:
45       Node(const std::string& name);
46     public:
47       virtual ~Node();
48       virtual void init();
49       InGate *getInGate() { return &_inGate; }
50       OutGate *getOutGate() { return &_outGate; }
51       const std::string& getName() const { return _name; }
52       std::list<Node *> getOutNodes() const;
53       virtual void exUpdateState();
54       virtual void getReadyTasks(std::vector<Task *>& tasks) = 0;
55       virtual std::list<ElementaryNode *> getRecursiveConstituents() = 0;
56       virtual int getNumberOfInputPorts() const;
57       virtual int getNumberOfOutputPorts() const;
58       virtual std::list<InputPort *> getListOfInputPort() const { return _listOfInputPort; }
59       virtual std::list<OutputPort *> getListOfOutputPort() const { return _listOfOutputPort; }
60       virtual InputPort *getInputPort(const std::string& name) const throw(Exception);
61       virtual OutputPort *getOutputPort(const std::string& name) const throw(Exception);
62       virtual std::list<InputDataStreamPort *> getListOfInputDataStreamPort() const { return _listOfInputDataStreamPort; }
63       virtual std::list<OutputDataStreamPort *> getListOfOutputDataStreamPort() const { return _listOfOutputDataStreamPort; }
64       virtual InputDataStreamPort *getInputDataStreamPort(const std::string& name) const throw(Exception);
65       virtual OutputDataStreamPort *getOutputDataStreamPort(const std::string& name) const throw(Exception);
66       std::list<ComposedNode *> getAllAscendanceOf(ComposedNode *levelToStop = 0);
67     protected:
68       bool areAllInputPortsValid() const;
69       virtual ComposedNode *getRootNode() throw(Exception);
70       virtual void disconnectAllLinksConnectedTo(Node *node) = 0;
71       static void checkValidityOfPortName(const std::string& name) throw(Exception);
72       static ComposedNode *checkHavingCommonFather(Node *node1, Node *node2) throw(Exception);
73       template<class PORT>
74       PORT *getPort(const std::string& name, const std::list<PORT *>& listOfPorts) const throw(Exception);
75       template<class PORT, class ENUMTYPE>
76       PORT *edAddPort(const std::string& portName, std::list<PORT *>& listOfPorts, ENUMTYPE type) throw(Exception);
77       template<class PORT>
78       static void edRemovePortTypedFromList(PORT *port, std::list<PORT *>& listOfPorts) throw(Exception);
79       template<class PORT>
80       static bool isPortNameAlreadyExist(const std::string& portName, const std::list<PORT *>& listOfPorts);
81     private:
82       void initForDFS() const;
83     };
84
85     template<class PORT>
86     PORT *Node::getPort(const std::string& name, const std::list<PORT *>& listOfPorts) const throw(Exception)
87     {
88       for(typename std::list<PORT *>::const_iterator iter=listOfPorts.begin();iter!=listOfPorts.end();iter++)
89         {
90           if((*iter)->getName()==name)
91             return *iter;
92         }
93       std::string what="Node::getPort : unexisting "; what+=PORT::NAME;
94       what+=" with name ";
95       what+=name;
96       throw Exception(what);
97     }
98
99     template<class PORT, class ENUMTYPE>
100     PORT *Node::edAddPort(const std::string& portName, std::list<PORT *>& listOfPorts, ENUMTYPE type) throw(Exception)
101     {
102       checkValidityOfPortName(portName);
103       if(isPortNameAlreadyExist<PORT>(portName, listOfPorts))
104         {
105           std::string what="Port of type "; what+=PORT::NAME; what += " with name : "; what+=portName; what+=" already exists";
106           throw Exception(what);
107         }
108       PORT *ret=new PORT(portName,this,type);
109       listOfPorts.push_back(ret);
110       return ret;
111     }
112
113     template<class PORT>
114     void Node::edRemovePortTypedFromList(PORT *port, std::list<PORT *>& listOfPorts) throw(Exception)
115     {
116       if(!isPortNameAlreadyExist<PORT>(port->getName(), listOfPorts))
117         throw Exception("Port is not part of the list : unable to remove it");
118       listOfPorts.remove(port);
119     }
120
121     template<class PORT>
122     bool Node::isPortNameAlreadyExist(const std::string& portName, const std::list<PORT *>& listOfPorts)
123     {
124       for(typename std::list<PORT *>::const_iterator iter=listOfPorts.begin();iter!=listOfPorts.end();iter++)
125         {
126           if((*iter)->getName()==portName)
127             return true;
128         }
129       return false;
130     }
131   }
132 }
133
134 #endif