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