aGrUM  0.14.2
IDBInitializer_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
26 #ifndef DOXYGEN_SHOULD_SKIP_THIS
27 
28 namespace gum {
29 
30  namespace learning {
31 
32 
34  template < template < typename > class ALLOC >
37  return __var_names.get_allocator();
38  }
39 
40 
42  template < template < typename > class ALLOC >
45  const typename IDBInitializer< ALLOC >::allocator_type& alloc) :
46  __var_names(alloc),
47  __input_type(type) {
48  GUM_CONSTRUCTOR(IDBInitializer);
49  }
50 
51 
53  template < template < typename > class ALLOC >
55  const IDBInitializer< ALLOC >& from,
56  const typename IDBInitializer< ALLOC >::allocator_type& alloc) :
57  __var_names(from.__var_names, alloc),
58  __input_type(from.__input_type) {
59  GUM_CONS_CPY(IDBInitializer);
60  }
61 
62 
64  template < template < typename > class ALLOC >
66  const IDBInitializer< ALLOC >& from) :
67  IDBInitializer(from, from.getAllocator()) {}
68 
69 
71  template < template < typename > class ALLOC >
73  IDBInitializer< ALLOC >&& from,
74  const typename IDBInitializer< ALLOC >::allocator_type& alloc) :
75  __var_names(std::move(from.__var_names), alloc),
76  __input_type(from.__input_type) {
77  GUM_CONS_MOV(IDBInitializer);
78  }
79 
80 
82  template < template < typename > class ALLOC >
83  INLINE
84  IDBInitializer< ALLOC >::IDBInitializer(IDBInitializer< ALLOC >&& from) :
85  IDBInitializer(std::move(from), from.getAllocator()) {}
86 
87 
89  template < template < typename > class ALLOC >
91  GUM_DESTRUCTOR(IDBInitializer);
92  }
93 
94 
96  template < template < typename > class ALLOC >
97  const std::vector< std::string, ALLOC< std::string > >&
99  if (__var_names.empty()) __var_names = this->_variableNames();
100  return __var_names;
101  }
102 
103 
104  // copy operator
105  template < template < typename > class ALLOC >
106  IDBInitializer< ALLOC >& IDBInitializer< ALLOC >::
107  operator=(const IDBInitializer< ALLOC >& from) {
108  if (this != &from) {
109  __var_names = from.__var_names;
110  __input_type = from.__input_type;
111  __last_insertion_failed = false;
112  }
113  return *this;
114  }
115 
116 
117  // move constructor
118  template < template < typename > class ALLOC >
119  IDBInitializer< ALLOC >& IDBInitializer< ALLOC >::
120  operator=(IDBInitializer< ALLOC >&& from) {
121  if (this != &from) {
122  __var_names = std::move(from.__var_names);
123  __input_type = from.__input_type;
124  __last_insertion_failed = false;
125  }
126  return *this;
127  }
128 
129 
131  template < template < typename > class ALLOC >
132  template < template < template < typename > class > class DATABASE >
133  INLINE void IDBInitializer< ALLOC >::fillDatabase(DATABASE< ALLOC >& database,
134  const bool retry_insertion) {
135  switch (__input_type) {
136  case InputType::STRING:
137  __fillDatabaseFromStrings(database, retry_insertion);
138  return;
139 
140  case InputType::DBCELL:
141  __fillDatabaseFromDBCells(database, retry_insertion);
142  return;
143 
144  default:
145  GUM_ERROR(NotImplementedYet,
146  "fillDatabase has not been implemented yet for this "
147  "type of IDBInitializerInputType");
148  }
149  }
150 
151 
153  template < template < typename > class ALLOC >
154  template < template < template < typename > class > class DATABASE >
155  void IDBInitializer< ALLOC >::__fillDatabaseFromStrings(
156  DATABASE< ALLOC >& database, const bool retry_insertion) {
157  // if need be, try to reinsert the row that could not be inserted
158  if (retry_insertion && __last_insertion_failed) {
159  database.insertRow(_currentStringRow());
160  __last_insertion_failed = false;
161  }
162 
163  // try to insert the next rows
164  while (this->_nextRow()) {
165  try {
166  // read a new line in the input file and insert it into the database
167  database.insertRow(_currentStringRow());
168  } catch (...) {
169  __last_insertion_failed = true;
170  throw;
171  }
172  }
173  }
174 
175 
177  template < template < typename > class ALLOC >
178  template < template < template < typename > class > class DATABASE >
179  void IDBInitializer< ALLOC >::__fillDatabaseFromDBCells(
180  DATABASE< ALLOC >& database, const bool retry_insertion) {
181  // if need be, try to reinsert the row that could not be inserted
182  if (retry_insertion && __last_insertion_failed) {
183  database.insertRow(_currentDBCellRow());
184  __last_insertion_failed = false;
185  }
186 
187  // try to insert the next rows
188  while (this->_nextRow()) {
189  try {
190  // read a new line in the input file and insert it into the database
191  database.insertRow(_currentDBCellRow());
192  } catch (...) {
193  __last_insertion_failed = true;
194  throw;
195  }
196  }
197  }
198 
199 
201  template < template < typename > class ALLOC >
202  const std::vector< std::string, ALLOC< std::string > >&
204  GUM_ERROR(FatalError,
205  "Method _currentStringRow should not be used or it should be "
206  "overloaded in children classes.");
207  }
208 
209 
211  template < template < typename > class ALLOC >
212  const DBRow< DBCell, ALLOC >& IDBInitializer< ALLOC >::_currentDBCellRow() {
213  GUM_ERROR(FatalError,
214  "Method _currentDBCellRow should not be used or it should be "
215  "overloaded in children classes.");
216  }
217 
218 
219  } /* namespace learning */
220 
221 } /* namespace gum */
222 
223 #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
gum is the global namespace for all aGrUM entities
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:52