Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / engine / ComposedNode.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 __COMPOSEDNODE_HXX__
20 #define __COMPOSEDNODE_HXX__
21
22 #include "Node.hxx"
23 #include "Scheduler.hxx"
24
25 #include <set>
26 #include <string>
27 #include <vector>
28
29 namespace YACS
30 {
31   namespace ENGINE
32   {
33     class Bloc;
34     class Loop;
35     class InPort;
36     class OutPort;
37     class LinkInfo;
38     class ElementaryNode;
39     
40     class ComposedNode : public Node, public Scheduler
41     {
42       friend class Bloc;
43       friend class Loop;
44       friend class OutPort;
45       friend class ElementaryNode;
46     protected:
47       static const char SEP_CHAR_BTW_LEVEL[];
48     protected:
49       ComposedNode(const std::string& name);
50       ComposedNode(const ComposedNode& other, ComposedNode *father);
51       void performDuplicationOfPlacement(const Node& other);
52     public:
53       virtual ~ComposedNode();
54       bool isFinished();
55       void init(bool start=true);
56       std::string getName() const;
57       std::string getTaskName(Task *task) const;
58       DeploymentTree getDeploymentTree() const;
59       DeploymentTree checkDeploymentTree(bool deep) const throw(Exception);
60       std::vector<Task *> getNextTasks(bool& isMore);
61       virtual bool isPlacementPredictableB4Run() const = 0;
62       void notifyFrom(const Task *sender, YACS::Event event);
63       bool edAddLink(OutPort *start, InPort *end) throw(Exception);
64       virtual bool edAddDFLink(OutPort *start, InPort *end) throw(Exception);
65       //Node* DISOWNnode is a SWIG notation to indicate that the ownership of the node is transfered to C++
66       virtual bool edAddChild(Node *DISOWNnode) throw(Exception);
67       virtual void edRemoveChild(Node *node) throw(Exception);
68       bool edAddLink(OutGate *start, InGate *end) throw(Exception);
69       bool edAddCFLink(Node *nodeS, Node *nodeE) throw(Exception);
70       void edRemoveCFLink(Node *nodeS, Node *nodeE) throw(Exception);
71       void edRemoveLink(OutPort *start, InPort *end) throw(Exception);
72       void edRemoveLink(OutGate *start, InGate *end) throw(Exception);
73       virtual bool isRepeatedUnpredictablySeveralTimes() const { return false; }
74       virtual std::list<Node *> edGetDirectDescendants() const =  0;
75       std::list<ElementaryNode *> getRecursiveConstituents() const;
76       std::list<Node *> getAllRecursiveNodes();
77       virtual std::list<Node *> getAllRecursiveConstituents(); // first implementation
78       std::string getInPortName(const InPort *) const throw (Exception);
79       std::string getOutPortName(const OutPort *) const throw (Exception);
80       //
81       int getNumberOfInputPorts() const;
82       int getNumberOfOutputPorts() const;
83       std::list<InputPort *> getSetOfInputPort() const;
84       std::list<OutputPort *> getSetOfOutputPort() const;
85       std::list<InputPort *> getLocalInputPorts() const;
86       std::list<OutputPort *> getLocalOutputPorts() const;
87       std::set<OutPort *> getAllOutPortsLeavingCurrentScope() const;
88       std::set<InPort *> getAllInPortsComingFromOutsideOfCurrentScope() const;
89       std::list<InputDataStreamPort *> getSetOfInputDataStreamPort() const;
90       std::list<OutputDataStreamPort *> getSetOfOutputDataStreamPort() const;
91       OutPort *getOutPort(const std::string& name) const throw(Exception);
92       InputPort *getInputPort(const std::string& name) const throw(Exception);
93       OutputPort *getOutputPort(const std::string& name) const throw(Exception);
94       InputDataStreamPort *getInputDataStreamPort(const std::string& name) const throw(Exception);
95       OutputDataStreamPort *getOutputDataStreamPort(const std::string& name) const throw(Exception);
96       std::vector< std::pair<OutPort *, InPort *> > getSetOfInternalLinks() const;
97       virtual std::vector< std::pair<OutPort *, InPort *> > getSetOfLinksLeavingCurrentScope() const;
98       void checkConsistency(LinkInfo& info) const throw(Exception);
99       virtual std::vector< std::pair<InPort *, OutPort *> > getSetOfLinksComingInCurrentScope() const;
100       virtual std::string typeName() {return "YACS__ENGINE__ComposedNode";}
101       virtual void edUpdateState();
102       virtual void checkBasicConsistency() const throw(Exception);
103       virtual std::string getErrorReport();
104       //
105       ComposedNode *getRootNode() const throw(Exception);
106       bool isNodeAlreadyAggregated(const Node *node) const;
107       virtual bool isNameAlreadyUsed(const std::string& name) const;
108       Node *isInMyDescendance(Node *nodeToTest) const;
109       std::string getChildName(const Node* node) const throw(Exception);
110       virtual std::string getMyQualifiedName(const Node *directSon) const;
111       Node *getChildByName(const std::string& name) const throw(Exception);
112       static ComposedNode *getLowestCommonAncestor(Node *node1, Node *node2) throw(Exception);
113       void loaded();
114       void connected();
115       void accept(Visitor *visitor);
116     protected:
117       struct SortHierarc
118       {
119         bool operator()(ComposedNode *n1, ComposedNode *n2) const
120         {
121           return *n1<*n2;
122         }
123       };
124     protected:
125       void edDisconnectAllLinksWithMe();
126       static bool splitNamesBySep(const std::string& globalName, const char separator[],
127                                   std::string& firstPart, std::string& lastPart, bool priority) throw(Exception);
128       virtual Node *getChildByShortName(const std::string& name) const throw(Exception) = 0;
129       YACS::Event updateStateFrom(Node *node, YACS::Event event);//update the state of this. Precondition : node->_father == this
130       virtual YACS::Event updateStateOnStartEventFrom(Node *node);//transition 3 doc P.R
131       virtual YACS::Event updateStateOnFinishedEventFrom(Node *node) = 0;//transition 9 doc P.R.
132       virtual YACS::Event updateStateOnFailedEventFrom(Node *node);//transition 9 doc P.R.
133       virtual void checkLinkPossibility(OutPort *start, const std::list<ComposedNode *>& pointsOfViewStart,
134                                         InPort *end, const std::list<ComposedNode *>& pointsOfViewEnd) throw(Exception);
135       virtual void buildDelegateOf(InPort * & port, OutPort *initialStart, const std::list<ComposedNode *>& pointsOfView);
136       virtual void buildDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView);
137       virtual void getDelegateOf(InPort * & port, OutPort *initialStart, const std::list<ComposedNode *>& pointsOfView) throw(Exception);
138       virtual void getDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView) throw(Exception);
139       virtual void releaseDelegateOf(InPort * & port, OutPort *initialStart, const std::list<ComposedNode *>& pointsOfView) throw(Exception);
140       virtual void releaseDelegateOf(OutPort *portDwn, OutPort *portUp, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView) throw(Exception);
141       virtual void checkNoCyclePassingThrough(Node *node) throw(Exception) = 0;
142       void checkInMyDescendance(Node *nodeToTest) const throw(Exception);
143       template <class PORT>
144       std::string getPortName(const PORT * port) const throw (Exception);
145       //For CF Computations
146       void checkNoCrossHierachyWith(Node *node) const throw (Exception);
147       virtual void performCFComputations(LinkInfo& info) const;
148       virtual void destructCFComputations(LinkInfo& info) const;
149       Node *getLowestNodeDealingAll(const std::list<OutPort *>& ports) const;
150       void checkLinksCoherenceRegardingControl(const std::vector<OutPort *>& starts,
151                                                InputPort *end, LinkInfo& info) const throw(Exception);
152       virtual void checkControlDependancy(OutPort *start, InPort *end, bool cross,
153                                           std::map < ComposedNode *,  std::list < OutPort * >, SortHierarc >& fw,
154                                           std::vector<OutPort *>& fwCross,
155                                           std::map< ComposedNode *, std::list < OutPort *>, SortHierarc >& bw,
156                                           LinkInfo& info) const = 0;
157       void solveObviousOrDelegateCFLinks(const std::list<OutPort *>& starts, InputPort *end, unsigned char& alreadyFed, bool direction, LinkInfo& info) const;
158       virtual void checkCFLinks(const std::list<OutPort *>& starts, InputPort *end, unsigned char& alreadyFed, bool direction, LinkInfo& info) const;
159     protected:
160       //For internal calculations.
161       static const unsigned char FED_ST = 2;
162       static const unsigned char FREE_ST = 0;
163       static const unsigned char FED_DS_ST = 1;
164     };
165
166     template <class PORT>
167     std::string ComposedNode::getPortName(const PORT * port) const throw (Exception)
168     {
169       Node *node = port->getNode();
170       std::string portName = port->getName();  
171       checkInMyDescendance(node);
172       Node *father = node;
173       while (father != this)
174         {
175           portName = father->getQualifiedName() + Node::SEP_CHAR_IN_PORT + portName;
176           father = father->_father;
177         }
178       return portName;
179     }
180   }
181 }
182
183 #endif