Salome HOME
50118885f07d53a8e267dcaadc266a5779904efa
[modules/yacs.git] / src / engine / BlocPoint.cxx
1 // Copyright (C) 2015-2023  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 "BlocPoint.hxx"
21 #include "Node.hxx"
22 #include "NotSimpleCasePoint.hxx"
23
24 #include <algorithm>
25
26 using namespace YACS::ENGINE;
27
28 BlocPoint::BlocPoint(const std::list<AbstractPoint *>& nodes, AbstractPoint *father):AbstractPoint(father),_nodes(nodes)
29 {
30   for(std::list<AbstractPoint *>::const_iterator it=_nodes.begin();it!=_nodes.end();it++)
31     (*it)->setFather(this);
32 }
33
34 void BlocPoint::deepCopyFrom(const BlocPoint& other)
35 {
36   const std::list<AbstractPoint *>& nodesToIterateOn(other.getListOfPoints());
37   for(auto it : nodesToIterateOn)
38     {
39       _nodes.push_back(it->deepCopy(this));
40     }
41 }
42
43 void BlocPoint::getOutPoint(AbstractPoint *node)
44 {
45   std::list<AbstractPoint *>::iterator it(std::find(_nodes.begin(),_nodes.end(),node));
46   if(it==_nodes.end())
47     throw YACS::Exception("BlocPoint::getOutPoint : node not in this !");
48   _nodes.erase(it);
49   node->setFather(NULL);
50 }
51
52 AbstractPoint *BlocPoint::findPointWithNode(Node *node)
53 {
54   for(std::list<AbstractPoint *>::iterator it=_nodes.begin();it!=_nodes.end();it++)
55     {
56       AbstractPoint *ret((*it)->findPointWithNode(node));
57       if(ret)
58         return AbstractPoint::GetDirectSonOf(this,ret);
59     }
60   return 0;
61 }
62
63 AbstractPoint *BlocPoint::getNodeAfter(Node *node)
64 {
65   OutGate *oug(node->getOutGate());
66   std::list<InGate *> fl(oug->edSetInGate());
67   if(fl.size()>=1)
68     {
69       AbstractPoint *ret(0);
70       IsCommonDirectSonOf(this,fl,ret);
71       return ret;
72     }
73   else
74     return 0;
75 }
76
77 AbstractPoint *BlocPoint::getNodeB4(Node *node)
78 {
79   InGate *ing(node->getInGate());
80   std::list<OutGate *> bl(ing->getBackLinks());
81   if(bl.size()>=1)
82     {
83       AbstractPoint *ret(0);
84       IsCommonDirectSonOf(this,bl,ret);
85       return ret;
86     }
87   else
88     return 0;
89 }
90
91 bool BlocPoint::contains(Node *node) const
92 {
93   for(auto it : _nodes)
94     {
95       if(it->contains(node))
96         return true;
97     }
98   return false;
99 }
100
101 bool BlocPoint::anyOf(const std::set<Node *>& nodes) const
102 {
103   for(auto it : nodes)
104     {
105       if(this->contains(it))
106         return true;
107     }
108   return false;
109 }
110
111 AbstractPoint *BlocPoint::getUnique()
112 {
113   if(_nodes.size()!=1)
114     throw YACS::Exception("BlocPoint::getUnique : invalid call !");
115   else
116     {
117       AbstractPoint *ret(*_nodes.begin());
118       if(!ret)
119         throw YACS::Exception("BlocPoint::getUnique : Ooops !");
120       return ret;
121     }
122 }
123
124 const AbstractPoint *BlocPoint::getUnique() const
125 {
126   if(_nodes.size()!=1)
127     throw YACS::Exception("BlocPoint::getUnique const : invalid call !");
128   else
129     {
130       AbstractPoint *ret(*_nodes.begin());
131       if(!ret)
132         throw YACS::Exception("BlocPoint::getUnique : Ooops !");
133       return ret;
134     }
135 }
136
137 AbstractPoint *BlocPoint::getUniqueAndReleaseIt()
138 {
139   AbstractPoint *ret(getUnique());
140   getOutPoint(ret);
141   return ret;
142 }
143
144 int BlocPoint::getNumberOfNodes() const
145 {
146   int ret(0);
147   for(std::list<AbstractPoint *>::const_iterator it=_nodes.begin();it!=_nodes.end();it++)
148     ret+=(*it)->getNumberOfNodes();
149   return ret;
150 }
151
152 BlocPoint::~BlocPoint()
153 {
154   for(std::list<AbstractPoint *>::iterator it=_nodes.begin();it!=_nodes.end();it++)
155     delete *it;
156 }
157
158 bool BlocPoint::internalContinueForSimplify() const
159 {
160   std::size_t i(0);
161   std::for_each(_nodes.begin(),_nodes.end(),[&i](AbstractPoint *elt) { if(!dynamic_cast<NotSimpleCasePoint *>(elt)) i++; });
162   return i>1;
163 }
164
165 bool BlocPoint::presenceOfNonSimpleCase() const
166 {
167   std::size_t i(0);
168   std::for_each(_nodes.begin(),_nodes.end(),[&i](AbstractPoint *elt) { if(dynamic_cast<NotSimpleCasePoint *>(elt)) i++; });
169   return i>0;
170 }