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