]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/Bloc.hxx
Salome HOME
Win32 compilation.
[modules/yacs.git] / src / engine / Bloc.hxx
1 // Copyright (C) 2006-2013  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 __BLOC_HXX__
21 #define __BLOC_HXX__
22
23 #include "YACSlibEngineExport.hxx"
24 #include "StaticDefinedComposedNode.hxx"
25
26 namespace YACS
27 {
28   namespace ENGINE
29   {
30     class YACSLIBENGINE_EXPORT Bloc : public StaticDefinedComposedNode
31     {
32     protected:
33       std::list<Node *> _setOfNode;//OWNERSHIP OF ALL NODES
34       //! For internal calculations
35       mutable std::map<Node *,std::set<Node *> > *_fwLinks;
36       //! For internal calculations
37       mutable std::map<Node *,std::set<Node *> > *_bwLinks;
38     protected:
39       Node *simpleClone(ComposedNode *father, bool editionOnly=true) const;
40     public:
41       Bloc(const Bloc& other, ComposedNode *father, bool editionOnly);
42       Bloc(const std::string& name);
43       virtual ~Bloc();
44       bool isFinished();
45       int getNumberOfCFLinks() const;
46       void init(bool start=true);
47       void getReadyTasks(std::vector<Task *>& tasks);
48       void exUpdateState();
49       //Node* DISOWNnode is a SWIG notation to indicate that the ownership of the node is transfered to C++
50       bool edAddChild(Node *DISOWNnode) throw(Exception);
51       void edRemoveChild(Node *node) throw(Exception);
52       std::list<Node *> getChildren() const { return _setOfNode; }
53       std::list<Node *> edGetDirectDescendants() const { return _setOfNode; }
54       Node *getChildByShortName(const std::string& name) const throw(Exception);
55       void selectRunnableTasks(std::vector<Task *>& tasks);
56       virtual void writeDot(std::ostream &os) const;
57       void accept(Visitor *visitor);
58       template<bool direction>
59       void findAllPathsStartingFrom(Node *start, std::list< std::vector<Node *> >& vec, std::map<Node *, std::set<Node *> >& accelStr) const;
60       template<bool direction>
61       void findAllNodesStartingFrom(Node *start, std::set<Node *>& result, std::map<Node *, std::set<Node *> >& accelStr, LinkInfo& info) const;
62       virtual std::string typeName() {return "YACS__ENGINE__Bloc";}
63     protected:
64       bool areAllSubNodesFinished() const;
65       bool areAllSubNodesDone() const;
66       bool isNameAlreadyUsed(const std::string& name) const;
67       void checkNoCyclePassingThrough(Node *node) throw(Exception);
68       std::vector< std::pair<OutGate *, InGate *> > getSetOfInternalCFLinks() const;
69       YACS::Event updateStateOnFinishedEventFrom(Node *node);
70       YACS::Event updateStateOnFailedEventFrom(Node *node);
71       void initComputation() const;
72       void performCFComputations(LinkInfo& info) const;
73       void destructCFComputations(LinkInfo& info) const;
74       void checkControlDependancy(OutPort *start, InPort *end, bool cross,
75                                   std::map < ComposedNode *,  std::list < OutPort * >, SortHierarc >& fw,
76                                   std::vector<OutPort *>& fwCross,
77                                   std::map< ComposedNode *, std::list < OutPort *>, SortHierarc >& bw,
78                                   LinkInfo& info) const;
79       bool areLinked(Node *start, Node *end, bool fw) const;
80       bool arePossiblyRunnableAtSameTime(Node *start, Node *end) const;
81       void checkCFLinks(const std::list<OutPort *>& starts, InputPort *end, unsigned char& alreadyFed, bool direction, LinkInfo& info) const;
82       static void verdictForOkAndUseless1(const std::map<Node *,std::list <OutPort *> >& pool, InputPort *end, const std::vector<Node *>& candidates,
83                                           unsigned char& alreadyFed, bool direction, LinkInfo& info);
84       static void verdictForCollapses(const std::map<Node *,std::list <OutPort *> >& pool, InputPort *end, const std::set<Node *>& candidates,
85                                       unsigned char& alreadyFed, bool direction, LinkInfo& info);
86       void seekOkAndUseless1(std::vector<Node *>& okAndUseless1, std::set<Node *>& allNodes) const;
87       void seekUseless2(std::vector<Node *>& useless2, std::set<Node *>& allNodes) const;
88     private:
89       static void findUselessLinksIn(const std::list< std::vector<Node *> >& res , LinkInfo& info);
90       template<bool direction>
91       static unsigned appendIfAlreadyFound(std::list< std::vector<Node *> >& res, const std::vector<Node *>& startRes, Node *node, std::map<Node *, std::set<Node *> >& fastFinder);
92       static void updateWithNewFind(const std::vector<Node *>& path, std::map<Node *, std::set<Node *> >& fastFinder);
93     };
94
95     template<bool direction>
96     struct CFDirectionVisTraits
97     {   
98     };
99     
100     template<>
101     struct CFDirectionVisTraits<true>
102     {
103       typedef std::map<InGate *,bool>::iterator Iterator;
104       typedef std::map<InGate *,bool>& Nexts;
105       static Nexts getNexts(Node *node) { return node->getOutGate()->edMapInGate(); }
106     };
107
108     
109     template<>
110     struct CFDirectionVisTraits<false>
111     {
112       typedef std::map<OutGate *,bool>::iterator Iterator;
113       typedef std::map<OutGate *,bool>& Nexts;
114       static Nexts getNexts(Node *node) { return node->getInGate()->edMapOutGate(); }
115     };
116
117     /*!
118      * Internal method for CF computation. Given 'fastFinder' it searched 'node' to see if an already found path in 'res' go through 'node'.
119      * If true all paths are deduced and append to res and 'fastFinder' is updated for next turn.
120      */
121     template<bool direction>
122     unsigned Bloc::appendIfAlreadyFound(std::list< std::vector<Node *> >& res, const std::vector<Node *>& startRes, Node *node, std::map<Node *, std::set<Node *> >& fastFinder)
123     {
124       std::map<Node *, std::set<Node *> >::iterator iter=fastFinder.find(node);
125       if(iter==fastFinder.end())
126         return 0;
127       unsigned ret=0;
128       std::vector<std::pair<std::set<Node *>::iterator, std::set<Node *>::iterator > > li;
129       std::pair<std::set<Node *>::iterator, std::set<Node *>::iterator> ipr(((*iter).second).begin(),((*iter).second).end());
130       li.push_back(ipr);
131       std::vector<Node *> work(startRes);
132       std::list< std::vector<Node *> >::iterator where=res.end(); where--;
133       std::list< std::vector<Node *> >::iterator updates=where;
134       while(!li.empty())
135         {
136           if(li.back().first!=li.back().second)
137             {
138               work.push_back(*(li.back().first));
139               if(CFDirectionVisTraits<direction>::getNexts(work.back()).empty())
140                 {
141                   where=res.insert(where,work);
142                   ret++;
143                   li.back().first++;
144                   work.pop_back();
145                 }
146               else
147                 {
148                   std::set<Node *>& s=fastFinder[*(li.back().first)];
149                   std::pair<std::set<Node *>::iterator, std::set<Node *>::iterator> pr(s.begin(),s.end());
150                   li.push_back(pr);
151                 }
152             }
153           else
154             {
155               work.pop_back();
156               li.pop_back();
157               if(!li.empty())
158                 li.back().first++;
159             }
160         }
161       updates--;
162       for(unsigned i=0;i<ret;i++,updates--)
163         updateWithNewFind(*updates,fastFinder);
164       return ret;
165     }
166
167     template<bool direction>
168     void Bloc::findAllNodesStartingFrom(Node *start, std::set<Node *>& result, 
169                                         std::map<Node *, std::set<Node *> >& accelStr, LinkInfo& info) const
170     {
171       std::list< std::vector<Node *> > li;
172       findAllPathsStartingFrom<direction>(start,li,accelStr);
173       for(std::list< std::vector<Node *> >::const_iterator iter=li.begin();iter!=li.end();iter++)
174         for(std::vector<Node *>::const_iterator iter2=(*iter).begin()+1;iter2!=(*iter).end();iter2++)
175           result.insert(*iter2);
176       if(direction)
177         findUselessLinksIn(li,info);
178     }
179
180     /*!
181      * Method for CF computation.DFS visitor is used.
182      * if direction is true forward visiting is performed, if false backward is performed.
183      * \param start \b must be a direct descendant of 'this'.
184      * \param vec :
185      * \param accelStr
186      */
187     template<bool direction>
188     void Bloc::findAllPathsStartingFrom(Node *start, std::list< std::vector<Node *> >& vec, 
189                                         std::map<Node *, std::set<Node *> >& accelStr) const
190     {
191       initComputation();
192       Node *current=start;
193       int caseId=0;
194       int idInCase=0;
195       vec.push_back(std::vector<Node *>());
196       typename CFDirectionVisTraits<direction>::Iterator iter;
197       std::list< std::vector<Node *> >::iterator curLine=vec.begin();
198       while(start->_colour!=YACS::Black)
199         {
200           (*curLine).push_back(current);
201           idInCase++;
202           //
203           if(CFDirectionVisTraits<direction>::getNexts(current).empty())
204             {
205               
206               vec.push_back(std::vector<Node *>((*curLine)));
207               updateWithNewFind(*curLine,accelStr);
208               current->_colour=YACS::Black;
209               curLine++;
210               if(idInCase>1)
211                 {
212                   idInCase-=2;
213                   current=(*curLine)[idInCase];
214                   (*curLine).pop_back();
215                   (*curLine).pop_back();
216                 }
217               continue;
218             }
219           if(current->_colour==YACS::Black)
220             {
221               appendIfAlreadyFound<direction>(vec,(*curLine),current,accelStr);
222               curLine=vec.end(); curLine--;
223               current->_colour=YACS::Black;
224               if(idInCase>1)
225                 {
226                   idInCase-=2;
227                   current=(*curLine)[idInCase];
228                   (*curLine).pop_back();
229                   (*curLine).pop_back();
230                 }
231               continue;
232             }
233           for(iter=CFDirectionVisTraits<direction>::getNexts(current).begin();iter!=CFDirectionVisTraits<direction>::getNexts(current).end();iter++)
234             if(!(*iter).second)
235               break;
236           if(iter==CFDirectionVisTraits<direction>::getNexts(current).end())
237             {//Fail this branch should be forgotten go rev
238               current->_colour=YACS::Black;
239               (*curLine).pop_back();
240               if(idInCase>1)
241                 {
242                   idInCase-=2;
243                   current=(*curLine)[idInCase];
244                   (*curLine).pop_back();
245                 }
246             }
247           else
248             {
249               //Nothing to signal continuing in this direction hoping to find
250               current=(*iter).first->getNode();
251               (*iter).second=true;
252             }
253         }
254       vec.pop_back();
255     }
256   }
257 }
258
259
260 #endif