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