]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/ForEachLoop.hxx
Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/yacs.git] / src / engine / ForEachLoop.hxx
1 // Copyright (C) 2006-2012  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.
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 __FOREACHLOOP_HXX__
21 #define __FOREACHLOOP_HXX__
22
23 #include "YACSlibEngineExport.hxx"
24 #include "ElementaryNode.hxx"
25 #include "DynParaLoop.hxx"
26 #include "OutputPort.hxx"
27 #include "InputPort.hxx"
28 #include "AnyInputPort.hxx"
29
30 namespace YACS
31 {
32   namespace ENGINE
33   {
34     class ForEachLoop;
35     class SplitterNode;
36     class AnySplitOutputPort;
37     class TypeCode;
38     class TypeCodeSeq;
39
40     class InterceptorInputPort : public AnyInputPort
41     {
42       friend class ForEachLoop;
43       friend class SplitterNode;
44     private:
45       AnySplitOutputPort *_repr;
46     private:
47       InterceptorInputPort(const std::string& name, Node *node, TypeCode* type);
48       InterceptorInputPort(const InterceptorInputPort& other, Node *newHelder);
49       void getAllRepresentants(std::set<InPort *>& repr) const;
50       InputPort *clone(Node *newHelder) const;
51       void setRepr(AnySplitOutputPort *repr);
52     };
53
54     class AnySplitOutputPort : public OutputPort
55     {
56       friend class ForEachLoop;
57       friend class SplitterNode;
58     private:
59       OutPort *_repr;
60       InterceptorInputPort *_intercptr;
61       mutable unsigned int _cnt;
62     private:
63       bool decrRef();
64       void incrRef() const;
65       AnySplitOutputPort(const std::string& name, Node *node, TypeCode *type);
66       AnySplitOutputPort(const AnySplitOutputPort& other, Node *newHelder);
67       bool addInPort(InPort *inPort) throw(Exception);
68       void getAllRepresented(std::set<OutPort *>& represented) const;
69       int removeInPort(InPort *inPort, bool forward) throw(Exception);
70       void addRepr(OutPort *repr, InterceptorInputPort *intercptr);
71       OutPort *getRepr() const { return _repr; }
72       OutputPort *clone(Node *newHelder) const;
73     };
74
75     class YACSLIBENGINE_EXPORT SeqAnyInputPort : public AnyInputPort
76     {
77       friend class ForEachLoop;
78       friend class SplitterNode;
79     public:
80       unsigned getNumberOfElements() const;
81       virtual std::string dump();
82     private:
83       SeqAnyInputPort(const std::string& name, Node *node, TypeCodeSeq* type);
84       SeqAnyInputPort(const SeqAnyInputPort& other, Node *newHelder);
85       InputPort *clone(Node *newHelder) const;
86       Any *getValueAtRank(int i) const;
87     };
88
89     class SplitterNode : public ElementaryNode
90     {
91       friend class ForEachLoop;
92     private:
93       static const char NAME_OF_SEQUENCE_INPUT[];
94     private:
95       SplitterNode(const std::string& name, TypeCode *typeOfData, ForEachLoop *father);
96       SplitterNode(const SplitterNode& other, ForEachLoop *father);
97       InputPort *getInputPort(const std::string& name) const throw(Exception);
98       Node *simpleClone(ComposedNode *father, bool editionOnly) const;
99       unsigned getNumberOfElements() const;
100       void execute();
101       void init(bool start=true);
102       void putSplittedValueOnRankTo(int rankInSeq, int branch, bool first);
103     private:
104       SeqAnyInputPort _dataPortToDispatch;
105     };
106
107     class FakeNodeForForEachLoop : public ElementaryNode
108     {
109       friend class ForEachLoop;
110     private:
111       ForEachLoop *_loop;
112       bool _normalFinish;
113     private:
114       FakeNodeForForEachLoop(ForEachLoop *loop, bool normalFinish);
115       FakeNodeForForEachLoop(const FakeNodeForForEachLoop& other);
116       Node *simpleClone(ComposedNode *father, bool editionOnly) const;
117       void exForwardFailed();
118       void exForwardFinished();
119       void execute();
120       void aborted();
121       void finished();
122     private:
123       static const char NAME[];
124     };
125
126     class YACSLIBENGINE_EXPORT ForEachLoop : public DynParaLoop
127     {
128       friend class SplitterNode;
129       friend class FakeNodeForForEachLoop;
130
131     public:
132       static const char NAME_OF_SPLITTERNODE[];
133     protected:
134       static const int NOT_RUNNING_BRANCH_ID;
135     protected:
136       SplitterNode _splitterNode;
137       FakeNodeForForEachLoop *_nodeForSpecialCases;
138       std::vector<AnySplitOutputPort *> _outGoingPorts;//! ports linked to node outside the current scope
139       std::vector<InterceptorInputPort *> _intecptrsForOutGoingPorts;//!ports created for TypeCodes correctness
140       //part of attributes defining graph dynamically built on control notification
141       unsigned _execCurrentId;
142       std::vector<SequenceAny *> _execVals;
143       std::vector< std::vector<AnyInputPort *> > _execOutGoingPorts;
144     public:
145       ForEachLoop(const std::string& name, TypeCode *typeOfDataSplitted);
146       ForEachLoop(const ForEachLoop& other, ComposedNode *father, bool editionOnly);
147       ~ForEachLoop();
148       void init(bool start=true);
149       void exUpdateState();
150       void getReadyTasks(std::vector<Task *>& tasks);
151       int getNumberOfInputPorts() const;
152       //
153       void checkNoCyclePassingThrough(Node *node) throw(Exception);
154       void selectRunnableTasks(std::vector<Task *>& tasks);
155       //
156       unsigned getExecCurrentId() const { return _execCurrentId; } // for update progress bar on GUI part
157       std::list<InputPort *> getSetOfInputPort() const;
158       std::list<InputPort *> getLocalInputPorts() const;
159       InputPort *edGetSeqOfSamplesPort() { return &_splitterNode._dataPortToDispatch; }
160       InputPort *getInputPort(const std::string& name) const throw(Exception);
161       OutPort *getOutPort(const std::string& name) const throw(Exception);
162       OutputPort *getOutputPort(const std::string& name) const throw(Exception);
163       Node *getChildByShortName(const std::string& name) const throw(Exception);
164       std::list<OutputPort *> getLocalOutputPorts() const;
165       void accept(Visitor *visitor);
166       void writeDot(std::ostream &os) const;
167       virtual std::string typeName() {return "YACS__ENGINE__ForEachLoop";}
168       virtual void resetState(int level);
169     protected:
170       Node *simpleClone(ComposedNode *father, bool editionOnly=true) const;
171       void checkLinkPossibility(OutPort *start, const std::list<ComposedNode *>& pointsOfViewStart,
172                                 InPort *end, const std::list<ComposedNode *>& pointsOfViewEnd) throw(Exception);
173       YACS::Event updateStateOnFinishedEventFrom(Node *node);
174       void buildDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView);
175       void getDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView) throw(Exception);
176       void releaseDelegateOf(OutPort *portDwn, OutPort *portUp, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView) throw(Exception);
177     protected:
178       void cleanDynGraph();
179       void pushAllSequenceValues();
180       void createOutputOutOfScopeInterceptors(int branchNb);
181       void prepareSequenceValues(int sizeOfSamples);
182       OutPort *getDynOutPortByAbsName(int branchNb, const std::string& name);
183       void storeOutValsInSeqForOutOfScopeUse(int rank, int branchNb);
184     };
185   }
186
187
188 #endif