Salome HOME
copy tag mergefrom_BR_V0_1_CC_Salome_04oct07
[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       bool isDifferentTypeOf(const DataPort *other) 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     private://Specific part
31       bool removePotentialProducerForMaster();
32       void checkCompletenessOfCases() const throw(Exception);
33       CollectorSwOutPort(Switch *master, InPort *port);
34       CollectorSwOutPort(const CollectorSwOutPort& other, Switch *master);
35       void addPotentialProducerForMaster(OutPort *port);
36       bool checkManagementOfPort(OutPort *port) throw(Exception);
37     private:
38       InPort *_consumer;
39       std::string _className;
40       OutPort *_currentProducer;
41       std::map<int, OutPort *> _potentialProducers;
42     };
43
44     class FakeNodeForSwitch : public ElementaryNode
45     {
46       friend class Switch;
47     private:
48       Switch *_sw;
49       bool _normalFinish;
50       bool _internalError;
51     private:
52       FakeNodeForSwitch(Switch *sw, bool normalFinish, bool internalError=false);
53       FakeNodeForSwitch(const FakeNodeForSwitch& other);
54       Node *simpleClone(ComposedNode *father, bool editionOnly) const;
55       void exForwardFailed();
56       void exForwardFinished();
57       void execute();
58       void aborted();
59       void finished();
60     };
61
62     class Switch : public StaticDefinedComposedNode
63     {
64       friend class FakeNodeForSwitch;
65       friend class CollectorSwOutPort;
66     public:
67       static const char DEFAULT_NODE_NAME[];
68       static const char SELECTOR_INPUTPORT_NAME[];
69       static const int ID_FOR_DEFAULT_NODE;
70     protected:
71       AnyInputPort _condition;
72       std::map< int , Node * > _mapOfNode;//Nodes ownered
73       FakeNodeForSwitch *_undispatchableNotificationNode;
74       std::map<InPort *, CollectorSwOutPort * > _outPortsCollector;
75       mutable std::vector<CollectorSwOutPort *> _alreadyExistingCollectors;
76     public:
77       Switch(const Switch& other, ComposedNode *father, bool editionOnly);
78       Switch(const std::string& name);
79       ~Switch();
80       void exUpdateState();
81       void init(bool start=true);
82       Node *edSetDefaultNode(Node *node);
83       Node *edReleaseDefaultNode() throw(Exception);
84       Node *edReleaseCase(int caseId) throw(Exception);
85       Node *edSetNode(int caseId, Node *node) throw(Exception);
86       void getReadyTasks(std::vector<Task *>& tasks);
87       void selectRunnableTasks(std::vector<Task *>& tasks);
88       std::set<Node *> edGetDirectDescendants() const;
89       InputPort *edGetConditionPort() { return &_condition; }
90       void writeDot(std::ostream &os);
91       int getNumberOfInputPorts() const;
92       void edRemoveChild(Node *node) throw(Exception);
93       std::list<InputPort *> getSetOfInputPort() const;
94       YACS::StatesForNode getEffectiveState(Node* node);
95       OutPort *getOutPort(const std::string& name) const throw(Exception);
96       InputPort* getInputPort(const std::string& name) const throw(Exception);
97       void checkConsistency(ComposedNode *pointOfView=0) const throw(Exception);
98       Node *getChildByShortName(const std::string& name) const throw(Exception);
99       std::string getMyQualifiedName(const Node *directSon) const;
100       std::string getCaseId(const Node *node) const throw(Exception);
101       virtual void accept(Visitor *visitor);
102       int getRankOfNode(Node *node) const;
103     protected:
104       YACS::Event updateStateOnFinishedEventFrom(Node *node);
105       Node *simpleClone(ComposedNode *father, bool editionOnly=true) const;
106       std::set<InPort *> getAllInPortsComingFromOutsideOfCurrentScope() const;
107       std::vector< std::pair<InPort *, OutPort *> > getSetOfLinksComingInCurrentScope() const;
108       void checkLinkPossibility(OutPort *start, const std::set<ComposedNode *>& pointsOfViewStart,
109                                 InPort *end, const std::set<ComposedNode *>& pointsOfViewEnd) throw(Exception);
110       void buildDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::set<ComposedNode *>& pointsOfView);
111       void getDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::set<ComposedNode *>& pointsOfView) throw(Exception);
112       void releaseDelegateOf(OutPort *portDwn, OutPort *portUp, InPort *finalTarget, const std::set<ComposedNode *>& pointsOfView) throw(Exception);
113       void checkNoCyclePassingThrough(Node *node) throw(Exception);
114     private:
115       int getNbOfCases() const;
116       static std::string getRepresentationOfCase(int i);
117     };
118   }
119 }
120
121 #endif