Salome HOME
Copyright update 2022
[modules/yacs.git] / src / engine / Loop.hxx
1 // Copyright (C) 2006-2022  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 __LOOP_HXX__
21 #define __LOOP_HXX__
22
23 #include "YACSlibEngineExport.hxx"
24 #include "StaticDefinedComposedNode.hxx"
25 #include "InputDataStreamPort.hxx"
26 #include "ElementaryNode.hxx"
27 #include "OutputPort.hxx"
28 #include "InputPort.hxx"
29
30 namespace YACS
31 {
32   namespace ENGINE
33   {
34     void YACSLIBENGINE_EXPORT NbDoneLoader(Loop* node, int val);
35     class Loop;
36     class ForLoop;
37     class WhileLoop;
38     class DFToDSForLoop;
39     class DSToDFForLoop;
40
41     class YACSLIBENGINE_EXPORT InputPort4DF2DS : public InputPort
42     {
43     public:
44       InputPort4DF2DS(DFToDSForLoop *node, TypeCode* type);
45       void getAllRepresentants(std::set<InPort *>& repr) const;
46       void releaseData() override;
47       void put(const void *data);
48       InputPort *clone(Node *newHelder) const;
49       void *get() const ;
50       void exRestoreInit();
51       void exSaveInit();
52       void put(Any *data);
53       ~InputPort4DF2DS();
54     protected:
55       Any *_data;
56     };
57
58     class DFToDSForLoop : public ElementaryNode
59     {
60       friend class Loop;
61     private:
62       int _nbOfTimeUsed;
63       Loop *_loopArtificiallyBuiltMe;
64     private:
65       DFToDSForLoop(Loop *loop, const std::string& name, TypeCode* type);
66       void loopHasOneMoreRef() { _nbOfTimeUsed++; }
67       bool loopHasOneLessRef() { return --_nbOfTimeUsed==0; }
68       void getReadyTasks(std::vector<Task *>& tasks);
69       InputPort *getInputPort(const std::string& name) const ;
70       OutputDataStreamPort *getOutputDataStreamPort(const std::string& name) const ;
71       Node *simpleClone(ComposedNode *father, bool editionOnly=true) const;
72       //run part
73       void execute();
74       void load();
75     public:
76       ~DFToDSForLoop();
77     };
78
79     class YACSLIBENGINE_EXPORT OutputPort4DS2DF : public OutputPort
80     {
81     public:
82       OutputPort4DS2DF(DSToDFForLoop *node, TypeCode *type);
83       void getAllRepresented(std::set<OutPort *>& represented) const;
84       void put(const void *data);
85       OutputPort *clone(Node *newHelder) const;
86       void put(Any *data);
87       ~OutputPort4DS2DF();
88     protected:
89       Any *_data;
90     };
91     
92     class YACSLIBENGINE_EXPORT InputDataStreamPort4DS2DF : public InputDataStreamPort
93     {
94     public:
95       InputDataStreamPort4DS2DF(DSToDFForLoop *node, TypeCode* type);
96       void getAllRepresentants(std::set<InPort *>& repr) const;
97     };
98
99     class DSToDFForLoop : public ElementaryNode
100     {
101       friend class Loop;
102     private:
103       int _nbOfTimeUsed;
104       Loop *_loopArtificiallyBuiltMe;
105     private:
106       DSToDFForLoop(Loop *loop, const std::string& name, TypeCode* type);
107       Node *simpleClone(ComposedNode *father, bool editionOnly=true) const;
108       void loopHasOneMoreRef() { _nbOfTimeUsed++; }
109       bool loopHasOneLessRef() { return --_nbOfTimeUsed==0; }
110       void getReadyTasks(std::vector<Task *>& tasks);
111       OutputPort *getOutputPort(const std::string& name) const ;
112       InputDataStreamPort *getInputDataStreamPort(const std::string& name) const ;
113       //run part
114       void execute();
115       void load();
116     public:
117       ~DSToDFForLoop();
118     };
119
120     class FakeNodeForLoop : public ElementaryNode
121     {
122       friend class ForLoop;
123       friend class WhileLoop;
124     private:
125       Loop *_loop;
126       bool _normalFinish;
127       bool _internalError;
128     private:
129       FakeNodeForLoop(Loop *loop, bool normalFinish, bool internalError=false);
130       FakeNodeForLoop(const FakeNodeForLoop& other);
131       Node *simpleClone(ComposedNode *father, bool editionOnly) const;
132       void exForwardFailed();
133       void exForwardFinished();
134       void execute();
135       void aborted();
136       void finished();
137     };
138
139 /*! \brief Base class for loop node
140  *
141  * \ingroup Nodes
142  *
143  * \see ForLoop
144  * \see WhileLoop
145  */
146     class YACSLIBENGINE_EXPORT Loop : public StaticDefinedComposedNode
147     {
148       friend class DSToDFForLoop;
149       friend class FakeNodeForLoop;
150       friend void NbDoneLoader(Loop* node, int val);
151     protected:
152       Node *_node;
153       int _nbOfTurns;
154       FakeNodeForLoop *_nodeForNullTurnOfLoops;
155       std::set<DSToDFForLoop *> _inputsTraducer;
156       std::set<DFToDSForLoop *> _outputsTraducer;
157     public:
158       Loop(const Loop& other, ComposedNode *father, bool editionOnly);
159       Loop(const std::string& name);
160       ~Loop();
161       void init(bool start=true);
162       int getNbOfTurns() const { return _nbOfTurns; }
163       //Node* DISOWNnode is a SWIG notation to indicate that the ownership of the node is transfered to C++
164       Node *edSetNode(Node *DISOWNnode);
165       virtual bool edAddChild(Node *DISOWNnode) ;
166       bool isLoop() const override { return true; }
167       Node *edRemoveNode();
168       virtual void checkBasicConsistency() const ;
169       //! Returns the port which value is used to take decision about the continuation of the loop.
170       virtual InputPort *getDecisionPort() const = 0;
171       void getReadyTasks(std::vector<Task *>& tasks);
172       void edRemoveChild(Node *node) ;
173       bool isRepeatedUnpredictablySeveralTimes() const { return true; }
174       std::list<Node *> edGetDirectDescendants() const;
175       std::list<InputPort *> getSetOfInputPort() const;
176       int getNumberOfInputPorts() const;
177       int getMaxLevelOfParallelism() const;
178       void getWeightRegardingDPL(ComplexWeight *weight);
179       void partitionRegardingDPL(const PartDefinition *pd, std::map<ComposedNode *, YACS::BASES::AutoRefCnt<PartDefinition> >& zeMap);
180       Node *getChildByShortName(const std::string& name) const ;
181       static TypeCode* MappingDF2DS(TypeCode* type) ;
182       static TypeCode* MappingDS2DF(TypeCode* type) ;
183       virtual bool edAddDFLink(OutPort *start, InPort *end) ;
184       void writeDot(std::ostream &os) const;
185       virtual void accept(Visitor *visitor);
186       virtual std::string typeName() {return "YACS__ENGINE__Loop";}
187     protected:
188       void buildDelegateOf(InPort * & port, OutPort *initialStart, const std::list<ComposedNode *>& pointsOfView);
189       void buildDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView);
190       void getDelegateOf(InPort * & port, OutPort *initialStart, const std::list<ComposedNode *>& pointsOfView) ;
191       void getDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView) ;
192       void releaseDelegateOf(InPort * & port, OutPort *initialStart, const std::list<ComposedNode *>& pointsOfView) ;
193       void releaseDelegateOf(OutPort *portDwn, OutPort *portUp, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView) ;
194       void checkNoCyclePassingThrough(Node *node) ;
195       void checkControlDependancy(OutPort *start, InPort *end, bool cross,
196                                   std::map < ComposedNode *,  std::list < OutPort * >, SortHierarc >& fw,
197                                   std::vector<OutPort *>& fwCross,
198                                   std::map< ComposedNode *, std::list < OutPort *>, SortHierarc >& bw,
199                                   LinkInfo& info) const;
200       void checkCFLinks(const std::list<OutPort *>& starts, InputPort *end, unsigned char& alreadyFed, bool direction, LinkInfo& info) const;
201       static bool isNecessaryToBuildSpecificDelegateDF2DS(const std::list<ComposedNode *>& pointsOfView);
202     };
203
204   }
205 }
206
207 #endif