aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
IDBInitializer_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 base class for initializing DatabaseTables and RawDatabaseTables
24
* from files or sql databases
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
/// returns the allocator used
36
template
<
template
<
typename
>
class
ALLOC
>
37
INLINE
typename
IDBInitializer
<
ALLOC
>::
allocator_type
38
IDBInitializer
<
ALLOC
>::
getAllocator
()
const
{
39
return
_var_names_
.
get_allocator
();
40
}
41
42
43
/// default constructor
44
template
<
template
<
typename
>
class
ALLOC
>
45
INLINE
IDBInitializer
<
ALLOC
>::
IDBInitializer
(
46
typename
IDBInitializer
<
ALLOC
>::
InputType
type
,
47
const
typename
IDBInitializer
<
ALLOC
>::
allocator_type
&
alloc
) :
48
_var_names_
(
alloc
),
49
_input_type_
(
type
) {
50
GUM_CONSTRUCTOR
(
IDBInitializer
);
51
}
52
53
54
/// copy constructor with a given allocator
55
template
<
template
<
typename
>
class
ALLOC
>
56
INLINE
IDBInitializer
<
ALLOC
>::
IDBInitializer
(
57
const
IDBInitializer
<
ALLOC
>&
from
,
58
const
typename
IDBInitializer
<
ALLOC
>::
allocator_type
&
alloc
) :
59
_var_names_
(
from
.
_var_names_
,
alloc
),
60
_input_type_
(
from
.
_input_type_
) {
61
GUM_CONS_CPY
(
IDBInitializer
);
62
}
63
64
65
/// copy constructor
66
template
<
template
<
typename
>
class
ALLOC
>
67
INLINE
IDBInitializer
<
ALLOC
>::
IDBInitializer
(
const
IDBInitializer
<
ALLOC
>&
from
) :
68
IDBInitializer
(
from
,
from
.
getAllocator
()) {}
69
70
71
/// move constructor with a given allocator
72
template
<
template
<
typename
>
class
ALLOC
>
73
INLINE
IDBInitializer
<
ALLOC
>::
IDBInitializer
(
74
IDBInitializer
<
ALLOC
>&&
from
,
75
const
typename
IDBInitializer
<
ALLOC
>::
allocator_type
&
alloc
) :
76
_var_names_
(
std
::
move
(
from
.
_var_names_
),
alloc
),
77
_input_type_
(
from
.
_input_type_
) {
78
GUM_CONS_MOV
(
IDBInitializer
);
79
}
80
81
82
/// move constructor
83
template
<
template
<
typename
>
class
ALLOC
>
84
INLINE
IDBInitializer
<
ALLOC
>::
IDBInitializer
(
IDBInitializer
<
ALLOC
>&&
from
) :
85
IDBInitializer
(
std
::
move
(
from
),
from
.
getAllocator
()) {}
86
87
88
/// destructor
89
template
<
template
<
typename
>
class
ALLOC
>
90
IDBInitializer
<
ALLOC
>::~
IDBInitializer
() {
91
GUM_DESTRUCTOR
(
IDBInitializer
);
92
}
93
94
95
/// returns the names of the variables in the input database
96
template
<
template
<
typename
>
class
ALLOC
>
97
const
std
::
vector
<
std
::
string
,
ALLOC
<
std
::
string
> >&
98
IDBInitializer
<
ALLOC
>::
variableNames
() {
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
>&
107
IDBInitializer
<
ALLOC
>::
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
>::
operator
=(
IDBInitializer
<
ALLOC
>&&
from
) {
120
if
(
this
!= &
from
) {
121
_var_names_
=
std
::
move
(
from
.
_var_names_
);
122
_input_type_
=
from
.
_input_type_
;
123
_last_insertion_failed_
=
false
;
124
}
125
return
*
this
;
126
}
127
128
129
/// fills the rows of the database
130
template
<
template
<
typename
>
class
ALLOC
>
131
template
<
template
<
template
<
typename
>
class
>
class
DATABASE
>
132
INLINE
void
IDBInitializer
<
ALLOC
>::
fillDatabase
(
DATABASE
<
ALLOC
>&
database
,
133
const
bool
retry_insertion
) {
134
switch
(
_input_type_
) {
135
case
InputType
::
STRING
:
136
_fillDatabaseFromStrings_
(
database
,
retry_insertion
);
137
return
;
138
139
case
InputType
::
DBCELL
:
140
_fillDatabaseFromDBCells_
(
database
,
retry_insertion
);
141
return
;
142
143
default
:
144
GUM_ERROR
(
NotImplementedYet
,
145
"fillDatabase has not been implemented yet for this "
146
"type of IDBInitializerInputType"
);
147
}
148
}
149
150
151
/// fills the rows of the database using string inputs
152
template
<
template
<
typename
>
class
ALLOC
>
153
template
<
template
<
template
<
typename
>
class
>
class
DATABASE
>
154
void
IDBInitializer
<
ALLOC
>::
_fillDatabaseFromStrings_
(
DATABASE
<
ALLOC
>&
database
,
155
const
bool
retry_insertion
) {
156
// if need be, try to reinsert the row that could not be inserted
157
if
(
retry_insertion
&&
_last_insertion_failed_
) {
158
database
.
insertRow
(
currentStringRow_
());
159
_last_insertion_failed_
=
false
;
160
}
161
162
// try to insert the next rows
163
while
(
this
->
nextRow_
()) {
164
try
{
165
// read a new line in the input file and insert it into the database
166
database
.
insertRow
(
currentStringRow_
());
167
}
catch
(...) {
168
_last_insertion_failed_
=
true
;
169
throw
;
170
}
171
}
172
}
173
174
175
/// fills the rows of the database using DBCell inputs
176
template
<
template
<
typename
>
class
ALLOC
>
177
template
<
template
<
template
<
typename
>
class
>
class
DATABASE
>
178
void
IDBInitializer
<
ALLOC
>::
_fillDatabaseFromDBCells_
(
DATABASE
<
ALLOC
>&
database
,
179
const
bool
retry_insertion
) {
180
// if need be, try to reinsert the row that could not be inserted
181
if
(
retry_insertion
&&
_last_insertion_failed_
) {
182
database
.
insertRow
(
currentDBCellRow_
());
183
_last_insertion_failed_
=
false
;
184
}
185
186
// try to insert the next rows
187
while
(
this
->
nextRow_
()) {
188
try
{
189
// read a new line in the input file and insert it into the database
190
database
.
insertRow
(
currentDBCellRow_
());
191
}
catch
(...) {
192
_last_insertion_failed_
=
true
;
193
throw
;
194
}
195
}
196
}
197
198
199
/// asks the child class for the content of the current row using strings
200
template
<
template
<
typename
>
class
ALLOC
>
201
const
std
::
vector
<
std
::
string
,
ALLOC
<
std
::
string
> >&
202
IDBInitializer
<
ALLOC
>::
currentStringRow_
() {
203
GUM_ERROR
(
FatalError
,
204
"Method currentStringRow_ should not be used or it should be "
205
"overloaded in children classes."
);
206
}
207
208
209
/// asks the child class for the content of the current row using dbcells
210
template
<
template
<
typename
>
class
ALLOC
>
211
const
DBRow
<
DBCell
,
ALLOC
>&
IDBInitializer
<
ALLOC
>::
currentDBCellRow_
() {
212
GUM_ERROR
(
FatalError
,
213
"Method currentDBCellRow_ should not be used or it should be "
214
"overloaded in children classes."
);
215
}
216
217
218
}
/* namespace learning */
219
220
}
/* namespace gum */
221
222
#
endif
/* DOXYGEN_SHOULD_SKIP_THIS */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643
gum::learning::genericBNLearner::Database::Database
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)
Definition:
genericBNLearner_tpl.h:31