Salome HOME
4de85225c4e8c62dd8f8e9f3fae0e72e17e0490f
[modules/yacs.git] / src / engine / Switch.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 __SWITCH_HXX__
20 #define __SWITCH_HXX__
21
22 #include "StaticDefinedComposedNode.hxx"
23 #include "ElementaryNode.hxx"
24 #include "AnyInputPort.hxx"
25 #include "OutPort.hxx"
26
27 #include <map>
28
29 namespace YACS
30 {
31   namespace ENGINE
32   {
33     class Switch;
34
35     class CollectorSwOutPort : public OutPort
36     {
37       friend class Switch;
38     private:
39       int edGetNumberOfOutLinks() const;
40       std::set<InPort *> edSetInPort() const;
41       bool isAlreadyLinkedWith(InPort *with) const;
42       std::string getNameOfTypeOfCurrentInstance() const;
43       void edRemoveAllLinksLinkedWithMe() throw(Exception);
44       TypeOfChannel getTypeOfChannel() const;
45       void getAllRepresented(std::set<OutPort *>& represented) const;
46       bool addInPort(InPort *inPort) throw(Exception);
47       int removeInPort(InPort *inPort, bool forward) throw(Exception);
48     public:
49       void getHumanReprOfIncompleteCases(std::ostream& stream) const;
50     private://Specific part
51       bool removePotentialProducerForMaster();
52       void checkConsistency(LinkInfo& info) const;
53       CollectorSwOutPort(Switch *master, InPort *port);
54       CollectorSwOutPort(const CollectorSwOutPort& other, Switch *master);
55       void addPotentialProducerForMaster(OutPort *port);
56       bool checkManagementOfPort(OutPort *port) throw(Exception);
57     private:
58       InPort *_consumer;
59       std::string _className;
60       OutPort *_currentProducer;
61       std::map<int, OutPort *> _potentialProducers;
62     };
63
64     class FakeNodeForSwitch : public ElementaryNode
65     {
66       friend class Switch;
67     private:
68       Switch *_sw;
69       bool _normalFinish;
70       bool _internalError;
71     private:
72       FakeNodeForSwitch(Switch *sw, bool normalFinish, bool internalError=false);
73       FakeNodeForSwitch(const FakeNodeForSwitch& other);
74       Node *simpleClone(ComposedNode *father, bool editionOnly) const;
75       void exForwardFailed();
76       void exForwardFinished();
77       void execute();
78       void aborted();
79       void finished();
80     };
81
82     class Switch : public StaticDefinedComposedNode
83     {
84       friend class FakeNodeForSwitch;
85       friend class CollectorSwOutPort;
86     public:
87       static const char DEFAULT_NODE_NAME[];
88       static const char SELECTOR_INPUTPORT_NAME[];
89       static const int ID_FOR_DEFAULT_NODE;
90     protected:
91       AnyInputPort _condition;
92       std::map< int , Node * > _mapOfNode;//Nodes ownered
93       FakeNodeForSwitch *_undispatchableNotificationNode;
94       std::map<InPort *, CollectorSwOutPort * > _outPortsCollector;
95       mutable std::vector<CollectorSwOutPort *> _alreadyExistingCollectors;
96     public:
97       Switch(const Switch& other, ComposedNode *father, bool editionOnly);
98       Switch(const std::string& name);
99       ~Switch();
100       void exUpdateState();
101       void init(bool start=true);
102       //Node* DISOWNnode is a SWIG notation to indicate that the ownership of the node is transfered to C++
103       Node *edSetDefaultNode(Node *DISOWNnode);
104       Node *edReleaseDefaultNode() throw(Exception);
105       Node *edReleaseCase(int caseId) throw(Exception);
106       Node *edGetNode(int caseId);
107       Node *edSetNode(int caseId, Node *DISOWNnode) throw(Exception);
108       virtual bool edAddChild(Node *DISOWNnode) throw(Exception);
109       void getReadyTasks(std::vector<Task *>& tasks);
110       void selectRunnableTasks(std::vector<Task *>& tasks);
111       std::list<Node *> edGetDirectDescendants() const;
112       InputPort *edGetConditionPort() { return &_condition; }
113       void writeDot(std::ostream &os) const;
114       int getNumberOfInputPorts() const;
115       void edRemoveChild(Node *node) throw(Exception);
116       std::list<InputPort *> getSetOfInputPort() const;
117       std::list<InputPort *> getLocalInputPorts() const;
118       YACS::StatesForNode getEffectiveState() const;
119       YACS::StatesForNode getEffectiveState(const Node* node) const;
120       OutPort *getOutPort(const std::string& name) const throw(Exception);
121       InputPort* getInputPort(const std::string& name) const throw(Exception);
122       Node *getChildByShortName(const std::string& name) const throw(Exception);
123       std::string getMyQualifiedName(const Node *directSon) const;
124       std::string getCaseId(const Node *node) const throw(Exception);
125       virtual void accept(Visitor *visitor);
126       int getRankOfNode(Node *node) const;
127       virtual std::string typeName() {return "YACS__ENGINE__Switch";}
128     protected:
129       YACS::Event updateStateOnFinishedEventFrom(Node *node);
130       Node *simpleClone(ComposedNode *father, bool editionOnly=true) const;
131       std::set<InPort *> getAllInPortsComingFromOutsideOfCurrentScope() const;
132       void checkLinkPossibility(OutPort *start, const std::list<ComposedNode *>& pointsOfViewStart,
133                                 InPort *end, const std::list<ComposedNode *>& pointsOfViewEnd) throw(Exception);
134       void buildDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView);
135       void getDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView) throw(Exception);
136       void releaseDelegateOf(OutPort *portDwn, OutPort *portUp, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView) throw(Exception);
137       void checkCFLinks(const std::list<OutPort *>& starts, InputPort *end, unsigned char& alreadyFed, bool direction, LinkInfo& info) const;
138       void checkControlDependancy(OutPort *start, InPort *end, bool cross,
139                                   std::map < ComposedNode *,  std::list < OutPort * > >& fw,
140                                   std::vector<OutPort *>& fwCross,
141                                   std::map< ComposedNode *, std::list < OutPort *> >& bw,
142                                   LinkInfo& info) const;
143       void checkNoCyclePassingThrough(Node *node) throw(Exception);
144     private:
145       int getNbOfCases() const;
146       static std::string getRepresentationOfCase(int i);
147     };
148   }
149 }
150
151 #endif