]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/ElementaryNode.hxx
Salome HOME
aecf8efbcfdd0d661e986f2ac845e42792f15167
[modules/yacs.git] / src / engine / ElementaryNode.hxx
1 #ifndef __ELEMENTARYNODE_HXX__
2 #define __ELEMENTARYNODE_HXX__
3
4 #include "Node.hxx"
5 #include "Task.hxx"
6 #include "define.hxx"
7 #include <string>
8
9 namespace YACS
10 {
11   namespace ENGINE
12   {
13     class TypeCode;
14     class Port;
15     class InputPort;
16     class OutputPort;
17     class ComposedNode;
18     class InputDataStreamPort;
19     class OutputDataStreamPort;
20
21     class ElementaryNode : public Node, public Task
22     {
23       friend class ComposedNode;
24     protected:
25       std::list<InputPort *> _setOfInputPort;
26       std::list<OutputPort *> _setOfOutputPort;
27       std::list<InputDataStreamPort *> _setOfInputDataStreamPort;
28       std::list<OutputDataStreamPort *> _setOfOutputDataStreamPort;
29     protected:
30       ElementaryNode(const std::string& name);
31       ElementaryNode(const ElementaryNode& other, ComposedNode *father);
32       void performDuplicationOfPlacement(const Node& other);
33     public:
34       virtual ~ElementaryNode();
35       void exUpdateState();
36       void init(bool start=true);
37       bool isDeployable() const;
38       ComponentInstance *getComponent();
39       YACS::StatesForNode getState() const;
40       void getReadyTasks(std::vector<Task *>& tasks);
41       void edRemovePort(Port *port) throw(Exception);
42       std::list<ElementaryNode *> getRecursiveConstituents() const;
43       Node *getChildByName(const std::string& name) const throw(Exception);
44       virtual void checkBasicConsistency() const throw(Exception);
45       ComposedNode *getDynClonerIfExists(const ComposedNode *levelToStop) const;
46       int getNumberOfInputPorts() const;
47       int getNumberOfOutputPorts() const;
48       std::string getInPortName(const InPort *) const throw (Exception);
49       std::string getOutPortName(const OutPort *) const throw (Exception);
50       InputPort *getInputPort(const std::string& name) const throw(Exception);
51       OutputPort *getOutputPort(const std::string& name) const throw(Exception);
52       std::list<InputPort *> getSetOfInputPort() const { return _setOfInputPort; }
53       std::list<OutputPort *> getSetOfOutputPort() const { return _setOfOutputPort; }
54       std::list<InputPort *> getLocalInputPorts() const { return _setOfInputPort; }
55       std::list<OutputPort *> getLocalOutputPorts() const { return _setOfOutputPort; }
56       std::set<OutPort *> getAllOutPortsLeavingCurrentScope() const;
57       std::set<InPort *> getAllInPortsComingFromOutsideOfCurrentScope() const;
58       virtual std::vector< std::pair<OutPort *, InPort *> > getSetOfLinksLeavingCurrentScope() const;
59       virtual std::vector< std::pair<InPort *, OutPort *> > getSetOfLinksComingInCurrentScope() const;
60       std::list<InputDataStreamPort *> getSetOfInputDataStreamPort() const { return _setOfInputDataStreamPort; }
61       std::list<OutputDataStreamPort *> getSetOfOutputDataStreamPort() const { return _setOfOutputDataStreamPort; }
62       InputDataStreamPort *getInputDataStreamPort(const std::string& name) const throw(Exception);
63       OutputDataStreamPort *getOutputDataStreamPort(const std::string& name) const throw(Exception);
64       virtual InputPort *createInputPort(const std::string& inputPortName, TypeCode* type);
65       virtual OutputPort *createOutputPort(const std::string& outputPortName, TypeCode* type);
66       virtual InputDataStreamPort *createInputDataStreamPort(const std::string& inputPortDSName, TypeCode* type);
67       virtual OutputDataStreamPort *createOutputDataStreamPort(const std::string& outputPortDSName, TypeCode* type);
68       virtual InputPort *edAddInputPort(const std::string& inputPortName, TypeCode* type) throw(Exception);
69       virtual OutputPort *edAddOutputPort(const std::string& outputPortName, TypeCode* type) throw(Exception);
70       virtual InputDataStreamPort *edAddInputDataStreamPort(const std::string& inputPortDSName, TypeCode* type) throw(Exception);
71       virtual OutputDataStreamPort *edAddOutputDataStreamPort(const std::string& outputPortDSName, TypeCode* type) throw(Exception);
72       virtual std::string typeName() {return "YACS__ENGINE__ElementaryNode";}
73       virtual void edUpdateState();
74       virtual void ensureLoading();
75
76       //run part
77       void begin();
78       bool isReady();
79       void finished();
80       void aborted();
81       void loaded();
82       virtual std::string getErrorDetails();
83       virtual void initService() { }
84       virtual void connectService() { }
85       virtual void disconnectService() { }
86       virtual void load() { }
87       void accept(Visitor *visitor);
88     protected:
89       void edDisconnectAllLinksWithMe();
90       bool areAllInputPortsValid() const;
91       template<class PORT>
92       PORT *getPort(const std::string& name, const std::list<PORT *>& setOfPorts) const throw(Exception);
93       template<class PORT, class ENUMTYPE>
94       PORT *edAddPort(const std::string& portName, std::list<PORT *>& setOfPorts, ENUMTYPE type) throw(Exception);
95       template<class PORT, class ENUMTYPE>
96       bool edCheckAddPort(const std::string& portName, std::list<PORT *>& setOfPorts, ENUMTYPE type) throw(Exception);
97       template<class PORT>
98       static void edRemovePortTypedFromSet(PORT *port, std::list<PORT *>& setOfPorts) throw(Exception);
99       template<class PORT>
100       static bool isPortNameAlreadyExist(const std::string& portName, const std::list<PORT *>& setOfPorts);
101     };
102
103     /**
104      * protected: get a port in a list given it's name
105      */
106
107     template<class PORT>
108     PORT *ElementaryNode::getPort(const std::string& name, const std::list<PORT *>& setOfPorts) const throw(Exception)
109     {
110       for(typename std::list<PORT *>::const_iterator iter=setOfPorts.begin();iter!=setOfPorts.end();iter++)
111         {
112           if((*iter)->getName()==name)
113             return *iter;
114         }
115       std::string what="ElementaryNode::getPort : unexisting "; what+=PORT::NAME;
116       what+=" with name ";
117       what+=name;
118       throw Exception(what);
119     }
120
121     /**
122      * protected: add a port given it's name and type, in a given list of ports
123      * WHY TEMPLATE PARAMETER ENUMTYPE?
124      */
125
126     template<class PORT, class ENUMTYPE>
127     PORT *ElementaryNode::edAddPort(const std::string& portName, std::list<PORT *>& setOfPorts, ENUMTYPE type) throw(Exception)
128     {
129       checkValidityOfPortName(portName);
130       if(isPortNameAlreadyExist<PORT>(portName, setOfPorts))
131         {
132           std::string what="Port of type "; what+=PORT::NAME; what += " with name : "; what+=portName; what+=" already exists";
133           throw Exception(what);
134         }
135       PORT *ret=new PORT(portName,this,type);
136       setOfPorts.push_back(ret);
137       return ret;
138     }
139
140     template<class PORT, class ENUMTYPE>
141     bool ElementaryNode::edCheckAddPort(const std::string& portName, std::list<PORT *>& setOfPorts, ENUMTYPE type) throw(Exception)
142     {
143       checkValidityOfPortName(portName);
144       if(isPortNameAlreadyExist<PORT>(portName, setOfPorts))
145         {
146           std::string what="Port of type "; what+=PORT::NAME; what += " with name : "; what+=portName; what+=" already exists";
147           throw Exception(what);
148         }
149       return true;
150     }
151
152     /**
153      * protected: remove a port from a given list
154      */
155
156     template<class PORT>
157     void ElementaryNode::edRemovePortTypedFromSet(PORT *port, std::list<PORT *>& setOfPorts) throw(Exception)
158     {
159       if(!isPortNameAlreadyExist<PORT>(port->getName(), setOfPorts))
160         throw Exception("Port is not part of the list : unable to remove it");
161       typename std::list<PORT *>::iterator iter=std::find(setOfPorts.begin(),setOfPorts.end(),port);
162       if(iter!=setOfPorts.end())
163         {
164           (*iter)->edRemoveAllLinksLinkedWithMe();
165           setOfPorts.erase(iter);
166         }
167     }
168
169     /**
170      * protected: checks existence of a port, given it's name, in a list
171      */
172
173     template<class PORT>
174     bool ElementaryNode::isPortNameAlreadyExist(const std::string& portName, const std::list<PORT *>& setOfPorts)
175     {
176       for(typename std::list<PORT *>::const_iterator iter=setOfPorts.begin();iter!=setOfPorts.end();iter++)
177         {
178           if((*iter)->getName()==portName)
179             return true;
180         }
181       return false;
182     }
183   }
184 }
185
186 #endif