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