]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/ElementaryNode.hxx
Salome HOME
Ready to update Executor to take into account of the new type of containers.
[modules/yacs.git] / src / engine / ElementaryNode.hxx
1 // Copyright (C) 2006-2014  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, or (at your option) any later version.
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
20 #ifndef __ELEMENTARYNODE_HXX__
21 #define __ELEMENTARYNODE_HXX__
22
23 #include "YACSlibEngineExport.hxx"
24 #include "Node.hxx"
25 #include "Task.hxx"
26 #include "define.hxx"
27
28 #include <string>
29 #include <algorithm>
30
31 namespace YACS
32 {
33   namespace ENGINE
34   {
35     class TypeCode;
36     class Port;
37     class InputPort;
38     class OutputPort;
39     class ComposedNode;
40     class InputDataStreamPort;
41     class OutputDataStreamPort;
42
43     class YACSLIBENGINE_EXPORT ElementaryNode : public Node, public Task
44     {
45       friend class ComposedNode;
46     protected:
47       std::list<InputPort *> _setOfInputPort;
48       std::list<OutputPort *> _setOfOutputPort;
49       std::list<InputDataStreamPort *> _setOfInputDataStreamPort;
50       std::list<OutputDataStreamPort *> _setOfOutputDataStreamPort;
51
52       // Management of multi property
53       bool _createDatastreamPorts;
54       bool _multi_port_node;
55     protected:
56       ElementaryNode(const std::string& name);
57       ElementaryNode(const ElementaryNode& other, ComposedNode *father);
58       void performDuplicationOfPlacement(const Node& other);
59     public:
60       virtual ~ElementaryNode();
61       void exUpdateState();
62       void init(bool start=true);
63       bool isDeployable() const;
64       ComponentInstance *getComponent();
65       const ComponentInstance *getComponent() const;
66       Container *getContainer();
67       YACS::StatesForNode getState() const;
68       void getReadyTasks(std::vector<Task *>& tasks);
69       void edRemovePort(Port *port) throw(Exception);
70       std::list<ElementaryNode *> getRecursiveConstituents() const;
71       Node *getChildByName(const std::string& name) const throw(Exception);
72       virtual void checkBasicConsistency() const throw(Exception);
73       ComposedNode *getDynClonerIfExists(const ComposedNode *levelToStop) const;
74       int getNumberOfInputPorts() const;
75       int getNumberOfOutputPorts() const;
76       std::string getInPortName(const InPort *) const throw (Exception);
77       std::string getOutPortName(const OutPort *) const throw (Exception);
78       InputPort *getInputPort(const std::string& name) const throw(Exception);
79       OutputPort *getOutputPort(const std::string& name) const throw(Exception);
80       std::list<InputPort *> getSetOfInputPort() const { return _setOfInputPort; }
81       std::list<OutputPort *> getSetOfOutputPort() const { return _setOfOutputPort; }
82       std::list<InputPort *> getLocalInputPorts() const { return _setOfInputPort; }
83       std::list<OutputPort *> getLocalOutputPorts() const { return _setOfOutputPort; }
84       std::set<OutPort *> getAllOutPortsLeavingCurrentScope() const;
85       std::set<InPort *> getAllInPortsComingFromOutsideOfCurrentScope() const;
86       virtual std::vector< std::pair<OutPort *, InPort *> > getSetOfLinksLeavingCurrentScope() const;
87       virtual std::vector< std::pair<InPort *, OutPort *> > getSetOfLinksComingInCurrentScope() const;
88       std::list<InputDataStreamPort *> getSetOfInputDataStreamPort() const { return _setOfInputDataStreamPort; }
89       std::list<OutputDataStreamPort *> getSetOfOutputDataStreamPort() const { return _setOfOutputDataStreamPort; }
90       InputDataStreamPort *getInputDataStreamPort(const std::string& name) const throw(Exception);
91       OutputDataStreamPort *getOutputDataStreamPort(const std::string& name) const throw(Exception);
92       virtual InputPort *createInputPort(const std::string& inputPortName, TypeCode* type);
93       virtual OutputPort *createOutputPort(const std::string& outputPortName, TypeCode* type);
94       virtual InputDataStreamPort *createInputDataStreamPort(const std::string& inputPortDSName, TypeCode* type);
95       virtual OutputDataStreamPort *createOutputDataStreamPort(const std::string& outputPortDSName, TypeCode* type);
96       virtual InputPort *edAddInputPort(const std::string& inputPortName, TypeCode* type) throw(Exception);
97       virtual OutputPort *edAddOutputPort(const std::string& outputPortName, TypeCode* type) throw(Exception);
98       virtual InputDataStreamPort *edAddInputDataStreamPort(const std::string& inputPortDSName, TypeCode* type) throw(Exception);
99       virtual OutputDataStreamPort *edAddOutputDataStreamPort(const std::string& outputPortDSName, TypeCode* type) throw(Exception);
100       virtual void edOrderInputPorts(const std::list<InputPort*>& ports);
101       virtual void edOrderOutputPorts(const std::list<OutputPort*>& ports);
102
103       virtual std::string typeName() {return "YACS__ENGINE__ElementaryNode";}
104       virtual void edUpdateState();
105       virtual void ensureLoading();
106
107       //run part
108       void begin();
109       bool isReady();
110       void finished();
111       void aborted();
112       void loaded();
113       void connected();
114       virtual std::string getErrorDetails();
115       virtual void initService() { }
116       virtual void connectService() { }
117       virtual void disconnectService() { }
118       virtual void load() { }
119       virtual void getCoupledTasks(std::set<Task*>& coupledSet);
120       virtual void getCoupledNodes(std::set<Task*>& coupledSet);
121       void accept(Visitor *visitor);
122
123       // Used for runtime nodes that need
124       // to configure their services for the multi property
125       virtual void addDatastreamPortToInitMultiService(const std::string & port_name,
126                                                        int number) {}
127     protected:
128       // Management of multi property
129       virtual void createMultiDatastreamPorts();
130
131       void edDisconnectAllLinksWithMe();
132       bool areAllInputPortsValid() const;
133       template<class PORT>
134       PORT *getPort(const std::string& name, const std::list<PORT *>& setOfPorts) const throw(Exception);
135       template<class PORT, class ENUMTYPE>
136       PORT *edAddPort(const std::string& portName, std::list<PORT *>& setOfPorts, ENUMTYPE type) throw(Exception);
137       template<class PORT, class ENUMTYPE>
138       bool edCheckAddPort(const std::string& portName, std::list<PORT *>& setOfPorts, ENUMTYPE type) throw(Exception);
139       template<class PORT>
140       static void edRemovePortTypedFromSet(PORT *port, std::list<PORT *>& setOfPorts) throw(Exception);
141       template<class PORT>
142       static bool isPortNameAlreadyExist(const std::string& portName, const std::list<PORT *>& setOfPorts);
143     };
144
145     /**
146      * protected: get a port in a list given it's name
147      */
148
149     template<class PORT>
150     PORT *ElementaryNode::getPort(const std::string& name, const std::list<PORT *>& setOfPorts) const throw(Exception)
151     {
152       for(typename std::list<PORT *>::const_iterator iter=setOfPorts.begin();iter!=setOfPorts.end();iter++)
153         {
154           if((*iter)->getName()==name)
155             return *iter;
156         }
157       std::string what="ElementaryNode::getPort : unexisting "; what+=PORT::NAME;
158       what+=" with name ";
159       what+=name;
160       throw Exception(what);
161     }
162
163     /**
164      * protected: add a port given it's name and type, in a given list of ports
165      * WHY TEMPLATE PARAMETER ENUMTYPE?
166      */
167
168     template<class PORT, class ENUMTYPE>
169     PORT *ElementaryNode::edAddPort(const std::string& portName, std::list<PORT *>& setOfPorts, ENUMTYPE type) throw(Exception)
170     {
171       checkValidityOfPortName(portName);
172       if(isPortNameAlreadyExist<PORT>(portName, setOfPorts))
173         {
174           std::string what="Port of type "; what+=PORT::NAME; what += " with name : "; what+=portName; what+=" already exists";
175           throw Exception(what);
176         }
177       PORT *ret=new PORT(portName,this,type);
178       setOfPorts.push_back(ret);
179       return ret;
180     }
181
182     template<class PORT, class ENUMTYPE>
183     bool ElementaryNode::edCheckAddPort(const std::string& portName, std::list<PORT *>& setOfPorts, ENUMTYPE type) throw(Exception)
184     {
185       checkValidityOfPortName(portName);
186       if(isPortNameAlreadyExist<PORT>(portName, setOfPorts))
187         {
188           std::string what="Port of type "; what+=PORT::NAME; what += " with name : "; what+=portName; what+=" already exists";
189           throw Exception(what);
190         }
191       return true;
192     }
193
194     /**
195      * protected: remove a port from a given list
196      */
197
198     template<class PORT>
199     void ElementaryNode::edRemovePortTypedFromSet(PORT *port, std::list<PORT *>& setOfPorts) throw(Exception)
200     {
201       if(!isPortNameAlreadyExist<PORT>(port->getName(), setOfPorts))
202         throw Exception("Port is not part of the list : unable to remove it");
203       typename std::list<PORT *>::iterator iter=std::find(setOfPorts.begin(),setOfPorts.end(),port);
204       if(iter!=setOfPorts.end())
205         {
206           (*iter)->edRemoveAllLinksLinkedWithMe();
207           setOfPorts.erase(iter);
208         }
209     }
210
211     /**
212      * protected: checks existence of a port, given it's name, in a list
213      */
214
215     template<class PORT>
216     bool ElementaryNode::isPortNameAlreadyExist(const std::string& portName, const std::list<PORT *>& setOfPorts)
217     {
218       for(typename std::list<PORT *>::const_iterator iter=setOfPorts.begin();iter!=setOfPorts.end();iter++)
219         {
220           if((*iter)->getName()==portName)
221             return true;
222         }
223       return false;
224     }
225   }
226 }
227
228 #endif