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