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