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