Salome HOME
Merge branch 'V8_5_BR'
[modules/yacs.git] / src / engine / BagPoint.cxx
1 // Copyright (C) 2015-2016  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 #include "BagPoint.hxx"
21 #include "Exception.hxx"
22 #include "LinkedBlocPoint.hxx"
23 #include "ForkBlocPoint.hxx"
24
25 #include <sstream>
26 #include <algorithm>
27
28 using namespace YACS::ENGINE;
29
30 BagPoint::BagPoint(const std::list<AbstractPoint *>& nodes, AbstractPoint *father):BlocPoint(nodes,father)
31 {
32 }
33
34 AbstractPoint *BagPoint::getUnique()
35 {
36   if(_nodes.size()!=1)
37     throw YACS::Exception("BagPoint::getUnique : invalid call !");
38   else
39     {
40       AbstractPoint *ret(*_nodes.begin());
41       if(!ret)
42         throw YACS::Exception("BagPoint::getUnique : Ooops !");
43       return ret;
44     }
45 }
46
47 const AbstractPoint *BagPoint::getUnique() const
48 {
49   if(_nodes.size()!=1)
50     throw YACS::Exception("BagPoint::getUnique const : invalid call !");
51   else
52     {
53       AbstractPoint *ret(*_nodes.begin());
54       if(!ret)
55         throw YACS::Exception("BagPoint::getUnique : Ooops !");
56       return ret;
57     }
58 }
59
60 AbstractPoint *BagPoint::getUniqueAndReleaseIt()
61 {
62   AbstractPoint *ret(getUnique());
63   getOutPoint(ret);
64   return ret;
65 }
66
67 void BagPoint::accept(PointVisitor *pv)
68 {
69   if(_nodes.size()!=1)
70     throw YACS::Exception("BagPoint::accept : simplification has failed !");
71   AbstractPoint *ret(*_nodes.begin());
72   if(!ret)
73     throw YACS::Exception("BagPoint::accept : Ooops !");
74   ret->accept(pv);
75 }
76
77 Node *BagPoint::getFirstNode()
78 {
79   return getUnique()->getFirstNode();
80 }
81
82 Node *BagPoint::getLastNode()
83 {
84   return getUnique()->getLastNode();
85 }
86
87 int BagPoint::getMaxLevelOfParallelism() const
88 {
89   return getUnique()->getMaxLevelOfParallelism();
90 }
91
92 void BagPoint::getWeightRegardingDPL(ComplexWeight *weight)
93 {
94   getUnique()->getWeightRegardingDPL(weight);
95 }
96
97 void BagPoint::partitionRegardingDPL(const PartDefinition *pd, std::map<ComposedNode *, YACS::BASES::AutoRefCnt<PartDefinition> >& zeMap) const
98 {
99   getUnique()->partitionRegardingDPL(pd,zeMap);
100 }
101
102 std::string BagPoint::getRepr() const
103 {
104   std::ostringstream oss;
105   for(std::list<AbstractPoint *>::const_iterator it=_nodes.begin();it!=_nodes.end();it++)
106     oss << (*it)->getRepr() << " - ";
107   return oss.str();
108 }
109
110 #include <iostream>
111
112 void BagPoint::replaceInMe(BlocPoint *aSet)
113 {
114   const std::list<AbstractPoint *>& pts(aSet->getListOfPoints());
115   for(std::list<AbstractPoint *>::const_iterator it0=pts.begin();it0!=pts.end();it0++)
116     {
117       std::list<AbstractPoint *>::iterator it1(std::find(_nodes.begin(),_nodes.end(),*it0));
118       if(it1==_nodes.end())
119         throw Exception("SetOfPoints::replaceInMe : internal error !");
120       _nodes.erase(it1);
121     }
122   _nodes.push_back(aSet);
123 }
124
125 void BagPoint::deal1(bool& somethingDone)
126 {
127   somethingDone=false;
128   for(std::list<AbstractPoint *>::iterator it=_nodes.begin();it!=_nodes.end();it++)
129     {
130       if(!(*it)->isSimplyLinkedBeforeAfter(this))
131         if(!(*it)->isSimplyLinkedAfterNullBefore(this) && !(*it)->isSimplyLinkedBeforeNullAfter(this))
132            continue;
133       LinkedBlocPoint *try0((*it)->tryAsLink(this));
134       if(try0)
135         {
136           replaceInMe(try0);
137           somethingDone=true;
138           break;
139         }
140     }
141 }
142
143 void BagPoint::deal2(bool& somethingDone)
144 {
145   somethingDone=false;
146   for(std::list<AbstractPoint *>::iterator it=_nodes.begin();it!=_nodes.end();it++)
147     {
148       if(!(*it)->isSimplyLinkedBeforeAfter(this))
149         continue;
150       ForkBlocPoint *try1((*it)->tryAsFork(this));
151       if(try1)
152         {
153           replaceInMe(try1);
154           somethingDone=true;
155           break;
156         }
157     }
158 }
159
160 void BagPoint::deal2Bis(bool& somethingDone)
161 {
162   somethingDone=false;
163   for(std::list<AbstractPoint *>::iterator it=_nodes.begin();it!=_nodes.end();it++)
164     {
165       if(!(*it)->isSimplyLinkedAfterNullBefore(this))
166         continue;
167       ForkBlocPoint *try1((*it)->tryAsForkBis(this));
168       if(try1)
169         {
170           replaceInMe(try1);
171           somethingDone=true;
172           break;
173         }
174     }
175 }
176
177 void BagPoint::deal2Ter(bool& somethingDone)
178 {
179   somethingDone=false;
180   for(std::list<AbstractPoint *>::iterator it=_nodes.begin();it!=_nodes.end();it++)
181     {
182       if(!(*it)->isSimplyLinkedBeforeNullAfter(this))
183         continue;
184       ForkBlocPoint *try1((*it)->tryAsForkTer(this));
185       if(try1)
186         {
187           replaceInMe(try1);
188           somethingDone=true;
189           break;
190         }
191       }
192 }
193