SelectStringField.cpp
1 /*---------------------------------------------------------------------------*\
2  *
3  * mimmo
4  *
5  * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6  *
7  * -------------------------------------------------------------------------
8  * License
9  * This file is part of mimmo.
10  *
11  * mimmo is free software: you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License v3 (LGPL)
13  * as published by the Free Software Foundation.
14  *
15  * mimmo is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with mimmo. If not, see <http://www.gnu.org/licenses/>.
22  *
23 \*---------------------------------------------------------------------------*/
24 
25 #include "SelectField.hpp"
26 #include "SkdTreeUtils.hpp"
27 #include "ExtractFields.hpp"
28 #include <unordered_map>
29 
30 namespace mimmo{
31 
37  m_name = "mimmo.SelectStringField";
38  m_loc = loc;
39  if(m_loc == MPVLocation::UNDEFINED) m_loc= MPVLocation::POINT;
40 }
41 
46 SelectStringField::SelectStringField(const bitpit::Config::Section & rootXML){
47 
48  std::string fallback_name = "ClassNONE";
49  std::string fallback_loc = "-1";
50 
51  std::string input_name = rootXML.get("ClassName", fallback_name);
52  input_name = bitpit::utils::string::trim(input_name);
53 
54  std::string input_loc = rootXML.get("DataLocation", fallback_loc);
55  input_loc = bitpit::utils::string::trim(input_loc);
56 
57  int loc = std::stoi(input_loc);
58  if(loc > 0 && loc < 4){
59  m_loc =static_cast<MPVLocation>(loc);
60  }else{
61  m_loc = MPVLocation::POINT;
62  }
63 
64  m_name = "mimmo.SelectStringField";
65 
66  if(input_name == "mimmo.SelectStringField"){
67  absorbSectionXML(rootXML);
68  }else{
70  };
71 }
72 
77 
82  m_loc = other.m_loc;
83  m_fields = other.m_fields;
84  m_result = other.m_result;
85 }
86 
92 {
93  std::swap(m_loc, x.m_loc);
94  std::swap(m_fields, x.m_fields);
95  //std::swap(m_result, x.m_result);
96  m_result.swap(x.m_result);
98 }
99 
103 void
105  bool built = true;
106  built = (built && createPortIn<std::vector<MimmoPiercedVector<std::string>*>, SelectStringField>(this, &mimmo::SelectStringField::setFields, M_VECSTRINGFIELDS, true, 1));
107  built = (built && createPortIn<MimmoPiercedVector<std::string>*, SelectStringField>(this, &mimmo::SelectStringField::addField, M_STRINGFIELD, true, 1));
108  built = (built && createPortOut<MimmoPiercedVector<std::string>*, SelectStringField>(this, &mimmo::SelectStringField::getSelectedField, M_STRINGFIELD));
109 
111  m_arePortsBuilt = built;
112 }
113 
120  return &m_result;
121 }
122 
127 void
129  m_fields.clear();
130  m_fields.reserve(fields.size());
131  for(MimmoPiercedVector<std::string> * ff : fields){
132  if(!ff) continue;
133  if(ff->getDataLocation() == m_loc && ff->getGeometry()!= nullptr){
134  m_fields.push_back(*ff);
135  }
136  }
137  m_fields.shrink_to_fit();
138 }
139 
144 void
146  if(!field) return;
147  if(field->getDataLocation() == m_loc && field->getGeometry() != nullptr){
148  m_fields.push_back(*field);
149  }
150 
151 }
152 
156 void
158  m_fields.clear();
159  m_result.clear();
161 }
162 
166 void
168 
169  // No data to write. String are not supported by bitpit vtk.
170 
171 }
172 
177 bool
178 SelectStringField::mSelect(){
179 
180  m_result.clear();
181 
182  switch(m_mode) {
183 
185  {
186  //Check if geometry is linked
187  if (getGeometry() == nullptr) return false;
188 
189  //Extract by link to geometry
190  for (const auto & field : m_fields){
191  if (field.getGeometry() == getGeometry()){
192  m_result = field;
193  //geometry, location and name are copied from field.
194  return true;
195  }
196  }
197  break;
198  }
199 
200  case SelectType::NAME:
201  {
202  //Extract by link to geometry
203  for (const auto & field : m_fields){
204  if (field.getName() == getFieldName()){
205  m_result = field;
206  //geometry, location and name are copied from field.
207  return true;
208  }
209  }
210  break;
211  }
212 
213  case SelectType::MAPPING:
214  {
215  //Check if geometry is linked
216  if (getGeometry() == nullptr) return false;
217 
218  //Extract by geometric mapping if active and if no positive match is found.
219  if (getGeometry()->getType() != 3){
220 
221  m_result.setGeometry(getGeometry());
222  m_result.setDataLocation(m_loc);
223 
224  ExtractStringField * ef = new ExtractStringField();
225  ef->setGeometry(getGeometry());
226  ef->setMode(ExtractMode::MAPPING);
227  ef->setTolerance(m_tol);
228 
229  //create map for overlapping ids purpose;
230  std::unordered_map<long, int> idRepetition;
231 
232  for (MimmoPiercedVector<std::string> & field : m_fields){
233  ef->setField(&field);
234  bool check = ef->extract();
235  if(!check) continue;
236 
237  MimmoPiercedVector<std::string> * temp = ef->getExtractedField();
238  MimmoPiercedVector<std::string>::iterator itB;
239  auto itE = temp->end();
240  long id;
241  for(itB = temp->begin(); itB != itE; ++itB){
242  id = itB.getId();
243  // For string values insert the first encountered
244  if(!m_result.exists(id)){
245  m_result.insert(id, *itB);
246  }
247  }
248  }
249 
250  //not resolve overlapping because I've chosen the first encountered value
251 
252  delete ef;
253  return true;
254  }
255  break;
256  }
257 
258  } // end switch mode
259 
260  //if you are here something was wrong.
261  return false;
262 }
263 
269 void SelectStringField::absorbSectionXML(const bitpit::Config::Section & slotXML, std::string name){
270 
271  BITPIT_UNUSED(name);
272 
273  if(slotXML.hasOption("DataLocation")){
274  std::string input = slotXML.get("DataLocation");
275  input = bitpit::utils::string::trim(input);
276  int temp = -1;
277  if(!input.empty()){
278  std::stringstream ss(input);
279  ss>>temp;
280  }
281  if(int(m_loc) != temp){
282  (*m_log)<<"Error absorbing DataLocation in "<<m_name<<". Class and read locations mismatch"<<std::endl;
283  throw std::runtime_error (m_name + " : xml absorbing failed");
284  }
285  }
286 
287  SelectField::absorbSectionXML(slotXML, name);
288 
289 
290 };
291 
297 void SelectStringField::flushSectionXML(bitpit::Config::Section & slotXML, std::string name){
298 
299  BITPIT_UNUSED(name);
300  slotXML.set("DataLocation", std::to_string(int(m_loc)));
301  SelectField::flushSectionXML(slotXML, name);
302 };
303 
304 
305 }
void addField(MimmoPiercedVector< std::string > *field)
void swap(SelectField &) noexcept
Definition: SelectField.cpp:56
MPVLocation
Define data location for the MimmoPiercedVector field.
MimmoPiercedVector< std::string > * getSelectedField()
void warningXML(bitpit::Logger *log, std::string name)
SelectStringField(MPVLocation loc=MPVLocation::POINT)
MimmoSharedPointer< MimmoObject > getGeometry() const
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")
std::string getFieldName()
#define M_STRINGFIELD
void setGeometry(MimmoSharedPointer< MimmoObject > geo)
SelectStringField is specialized derived class of SelectField to Select a scalar field of string data...
void setFields(std::vector< MimmoPiercedVector< std::string > * > fields)
void setDataLocation(MPVLocation loc)
virtual void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")
void swap(SelectStringField &) noexcept
SelectField is an abstract executable block class capable of Selecting a field from a list of fields.
Definition: SelectField.hpp:97
MimmoSharedPointer< MimmoObject > getGeometry()
#define M_VECSTRINGFIELDS
virtual void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")