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