aGrUM  0.16.0
IDBInitializer_tpl.h
Go to the documentation of this file.
1 
29 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 
31 namespace gum {
32 
33  namespace learning {
34 
35 
37  template < template < typename > class ALLOC >
40  return __var_names.get_allocator();
41  }
42 
43 
45  template < template < typename > class ALLOC >
48  const typename IDBInitializer< ALLOC >::allocator_type& alloc) :
49  __var_names(alloc),
50  __input_type(type) {
51  GUM_CONSTRUCTOR(IDBInitializer);
52  }
53 
54 
56  template < template < typename > class ALLOC >
58  const IDBInitializer< ALLOC >& from,
59  const typename IDBInitializer< ALLOC >::allocator_type& alloc) :
60  __var_names(from.__var_names, alloc),
61  __input_type(from.__input_type) {
62  GUM_CONS_CPY(IDBInitializer);
63  }
64 
65 
67  template < template < typename > class ALLOC >
69  const IDBInitializer< ALLOC >& from) :
70  IDBInitializer(from, from.getAllocator()) {}
71 
72 
74  template < template < typename > class ALLOC >
76  IDBInitializer< ALLOC >&& from,
77  const typename IDBInitializer< ALLOC >::allocator_type& alloc) :
78  __var_names(std::move(from.__var_names), alloc),
79  __input_type(from.__input_type) {
80  GUM_CONS_MOV(IDBInitializer);
81  }
82 
83 
85  template < template < typename > class ALLOC >
86  INLINE
87  IDBInitializer< ALLOC >::IDBInitializer(IDBInitializer< ALLOC >&& from) :
88  IDBInitializer(std::move(from), from.getAllocator()) {}
89 
90 
92  template < template < typename > class ALLOC >
94  GUM_DESTRUCTOR(IDBInitializer);
95  }
96 
97 
99  template < template < typename > class ALLOC >
100  const std::vector< std::string, ALLOC< std::string > >&
102  if (__var_names.empty()) __var_names = this->_variableNames();
103  return __var_names;
104  }
105 
106 
107  // copy operator
108  template < template < typename > class ALLOC >
109  IDBInitializer< ALLOC >& IDBInitializer< ALLOC >::
110  operator=(const IDBInitializer< ALLOC >& from) {
111  if (this != &from) {
112  __var_names = from.__var_names;
113  __input_type = from.__input_type;
114  __last_insertion_failed = false;
115  }
116  return *this;
117  }
118 
119 
120  // move constructor
121  template < template < typename > class ALLOC >
122  IDBInitializer< ALLOC >& IDBInitializer< ALLOC >::
123  operator=(IDBInitializer< ALLOC >&& from) {
124  if (this != &from) {
125  __var_names = std::move(from.__var_names);
126  __input_type = from.__input_type;
127  __last_insertion_failed = false;
128  }
129  return *this;
130  }
131 
132 
134  template < template < typename > class ALLOC >
135  template < template < template < typename > class > class DATABASE >
136  INLINE void IDBInitializer< ALLOC >::fillDatabase(DATABASE< ALLOC >& database,
137  const bool retry_insertion) {
138  switch (__input_type) {
139  case InputType::STRING:
140  __fillDatabaseFromStrings(database, retry_insertion);
141  return;
142 
143  case InputType::DBCELL:
144  __fillDatabaseFromDBCells(database, retry_insertion);
145  return;
146 
147  default:
148  GUM_ERROR(NotImplementedYet,
149  "fillDatabase has not been implemented yet for this "
150  "type of IDBInitializerInputType");
151  }
152  }
153 
154 
156  template < template < typename > class ALLOC >
157  template < template < template < typename > class > class DATABASE >
158  void IDBInitializer< ALLOC >::__fillDatabaseFromStrings(
159  DATABASE< ALLOC >& database, const bool retry_insertion) {
160  // if need be, try to reinsert the row that could not be inserted
161  if (retry_insertion && __last_insertion_failed) {
162  database.insertRow(_currentStringRow());
163  __last_insertion_failed = false;
164  }
165 
166  // try to insert the next rows
167  while (this->_nextRow()) {
168  try {
169  // read a new line in the input file and insert it into the database
170  database.insertRow(_currentStringRow());
171  } catch (...) {
172  __last_insertion_failed = true;
173  throw;
174  }
175  }
176  }
177 
178 
180  template < template < typename > class ALLOC >
181  template < template < template < typename > class > class DATABASE >
182  void IDBInitializer< ALLOC >::__fillDatabaseFromDBCells(
183  DATABASE< ALLOC >& database, const bool retry_insertion) {
184  // if need be, try to reinsert the row that could not be inserted
185  if (retry_insertion && __last_insertion_failed) {
186  database.insertRow(_currentDBCellRow());
187  __last_insertion_failed = false;
188  }
189 
190  // try to insert the next rows
191  while (this->_nextRow()) {
192  try {
193  // read a new line in the input file and insert it into the database
194  database.insertRow(_currentDBCellRow());
195  } catch (...) {
196  __last_insertion_failed = true;
197  throw;
198  }
199  }
200  }
201 
202 
204  template < template < typename > class ALLOC >
205  const std::vector< std::string, ALLOC< std::string > >&
207  GUM_ERROR(FatalError,
208  "Method _currentStringRow should not be used or it should be "
209  "overloaded in children classes.");
210  }
211 
212 
214  template < template < typename > class ALLOC >
215  const DBRow< DBCell, ALLOC >& IDBInitializer< ALLOC >::_currentDBCellRow() {
216  GUM_ERROR(FatalError,
217  "Method _currentDBCellRow should not be used or it should be "
218  "overloaded in children classes.");
219  }
220 
221 
222  } /* namespace learning */
223 
224 } /* namespace gum */
225 
226 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
const std::vector< std::string, ALLOC< std::string > > & variableNames()
returns the names of the variables in the input dataset
virtual std::vector< std::string, ALLOC< std::string > > _variableNames()=0
ask the child class for the names of the variables
virtual const DBRow< DBCell, ALLOC > & _currentDBCellRow()
asks the child class for the content of the current row using dbcells
STL namespace.
allocator_type getAllocator() const
returns the allocator used
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
IDBInitializer< ALLOC > & operator=(const IDBInitializer< ALLOC > &from)
copy operator
void fillDatabase(DATABASE< ALLOC > &database, const bool retry_insertion=false)
fills the rows of the database table
InputType
the enumeration indicating the type of the data the IDBInitializer expects as input data ...
virtual bool _nextRow()=0
indicates whether there is a next row to read (and point on it)
ALLOC< std::string > allocator_type
type for the allocators passed in arguments of methods
virtual const std::vector< std::string, ALLOC< std::string > > & _currentStringRow()
asks the child class for the content of the current row using strings
IDBInitializer(const InputType type, const allocator_type &alloc)
default constructor
virtual ~IDBInitializer()
destructor
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55