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