aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
DBInitializerFromCSV_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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), _comment_marker_(commentmarker),
46  _quote_marker_(quoteMarker), _first_row_has_names_(fileContainsNames),
47  _input_stream_(_filename_, std::ifstream::in),
48  _parser_(_input_stream_, delimiter, commentmarker, quoteMarker), _var_names_(alloc) {
49  // check that the input file was opened correctly
50  if ((_input_stream_.rdstate() & std::ifstream::failbit) != 0) {
51  GUM_ERROR(IOError, "File " << filename << " not found")
52  }
53 
54  // if the first line contains names, store them into the intializer
55  if (fileContainsNames) {
56  _parser_.next();
57  _var_names_ = _parser_.current();
58  }
59 
60  GUM_CONSTRUCTOR(DBInitializerFromCSV);
61  }
62 
63 
64  /// copy constructor with a given allocator
65  template < template < typename > class ALLOC >
68  const typename DBInitializerFromCSV< ALLOC >::allocator_type& alloc) :
74  alloc) {}
75 
76  /// copy constructor
77  template < template < typename > class ALLOC >
80 
81  /// move constructor with a given allocator
82  template < template < typename > class ALLOC >
85  const typename DBInitializerFromCSV< ALLOC >::allocator_type& alloc) :
91  alloc) {}
92 
93 
94  /// move constructor
95  template < template < typename > class ALLOC >
98 
99 
100  /// virtual copy constructor with a given allocator
101  template < template < typename > class ALLOC >
103  const typename DBInitializerFromCSV< ALLOC >::allocator_type& alloc) const {
106  try {
108  } catch (...) {
110  throw;
111  }
112 
113  return new_initializer;
114  }
115 
116 
117  /// virtual copy constructor
118  template < template < typename > class ALLOC >
120  return clone(this->getAllocator());
121  }
122 
123 
124  /// destructor
125  template < template < typename > class ALLOC >
128  }
129 
130 
131  /// copy operator
132  template < template < typename > class ALLOC >
135  if (this != &from) {
142 
143  // open the CSV file
146 
147  // check that the input file was opened correctly
148  if ((_input_stream_.rdstate() & std::ifstream::failbit) != 0) {
149  GUM_ERROR(IOError, "File " << _filename_ << " not found")
150  }
151 
152  // make the parser use the new input stream
154 
155  // if the first line contains names, store them into the intializer
156  if (_first_row_has_names_) {
157  _parser_.next();
159  }
160  }
161 
162  return *this;
163  }
164 
165 
166  /// move operator
167  template < template < typename > class ALLOC >
170  return operator=(from);
171  }
172 
173 
174  // ask the child class for the names of the variables
175  template < template < typename > class ALLOC >
178  return _var_names_;
179  }
180 
181 
182  // asks the child class for the content of the current row using strings
183  template < template < typename > class ALLOC >
184  INLINE const std::vector< std::string, ALLOC< std::string > >&
186  return _parser_.current();
187  }
188 
189 
190  // indicates whether there is a next row to read (and point on it)
191  template < template < typename > class ALLOC >
193  return _parser_.next();
194  }
195 
196  } /* namespace learning */
197 
198 } /* namespace gum */
199 
200 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)