aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
DBInitializerFromCSV_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief The class for initializing DatabaseTables and RawDatabaseTables
24  * from CSV files
25  *
26  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 
30 namespace gum {
31 
32  namespace learning {
33 
34 
35  /// default constructor
36  template < template < typename > class ALLOC >
37  DBInitializerFromCSV< ALLOC >::DBInitializerFromCSV(
38  const std::string filename,
39  bool fileContainsNames,
40  const std::string delimiter,
41  const char commentmarker,
42  const char quoteMarker,
43  const typename DBInitializerFromCSV< ALLOC >::allocator_type& alloc) :
44  IDBInitializer< ALLOC >(IDBInitializer< ALLOC >::InputType::STRING, alloc),
45  filename__(filename), delimiter__(delimiter),
46  comment_marker__(commentmarker), quote_marker__(quoteMarker),
47  first_row_has_names__(fileContainsNames),
48  input_stream__(filename__, std::ifstream::in),
49  parser__(input_stream__, delimiter, commentmarker, quoteMarker),
50  var_names__(alloc) {
51  // check that the input file was opened correctly
52  if ((input_stream__.rdstate() & std::ifstream::failbit) != 0) {
53  GUM_ERROR(IOError, "File " << filename << " not found");
54  }
55 
56  // if the first line contains names, store them into the intializer
57  if (fileContainsNames) {
58  parser__.next();
59  var_names__ = parser__.current();
60  }
61 
62  GUM_CONSTRUCTOR(DBInitializerFromCSV);
63  }
64 
65 
66  /// copy constructor with a given allocator
67  template < template < typename > class ALLOC >
70  const typename DBInitializerFromCSV< ALLOC >::allocator_type& alloc) :
76  alloc) {}
77 
78  /// copy constructor
79  template < template < typename > class ALLOC >
81  const DBInitializerFromCSV< ALLOC >& from) :
83 
84  /// move constructor with a given allocator
85  template < template < typename > class ALLOC >
88  const typename DBInitializerFromCSV< ALLOC >::allocator_type& alloc) :
94  alloc) {}
95 
96 
97  /// move constructor
98  template < template < typename > class ALLOC >
102 
103 
104  /// virtual copy constructor with a given allocator
105  template < template < typename > class ALLOC >
107  const typename DBInitializerFromCSV< ALLOC >::allocator_type& alloc) const {
110  try {
112  } catch (...) {
114  throw;
115  }
116 
117  return new_initializer;
118  }
119 
120 
121  /// virtual copy constructor
122  template < template < typename > class ALLOC >
124  return clone(this->getAllocator());
125  }
126 
127 
128  /// destructor
129  template < template < typename > class ALLOC >
132  }
133 
134 
135  /// copy operator
136  template < template < typename > class ALLOC >
138  const DBInitializerFromCSV< ALLOC >& from) {
139  if (this != &from) {
146 
147  // open the CSV file
150 
151  // check that the input file was opened correctly
152  if ((input_stream__.rdstate() & std::ifstream::failbit) != 0) {
153  GUM_ERROR(IOError, "File " << filename__ << " not found");
154  }
155 
156  // make the parser use the new input stream
158  delimiter__,
161 
162  // if the first line contains names, store them into the intializer
163  if (first_row_has_names__) {
164  parser__.next();
166  }
167  }
168 
169  return *this;
170  }
171 
172 
173  /// move operator
174  template < template < typename > class ALLOC >
177  return operator=(from);
178  }
179 
180 
181  // ask the child class for the names of the variables
182  template < template < typename > class ALLOC >
185  return var_names__;
186  }
187 
188 
189  // asks the child class for the content of the current row using strings
190  template < template < typename > class ALLOC >
191  INLINE const std::vector< std::string, ALLOC< std::string > >&
193  return parser__.current();
194  }
195 
196 
197  // indicates whether there is a next row to read (and point on it)
198  template < template < typename > class ALLOC >
200  return parser__.next();
201  }
202 
203  } /* namespace learning */
204 
205 } /* namespace gum */
206 
207 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)