aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
IDBInitializer_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 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
(
68
const
IDBInitializer
<
ALLOC
>&
from
) :
69
IDBInitializer
(
from
,
from
.
getAllocator
()) {}
70
71
72
/// move constructor with a given allocator
73
template
<
template
<
typename
>
class
ALLOC
>
74
INLINE
IDBInitializer
<
ALLOC
>::
IDBInitializer
(
75
IDBInitializer
<
ALLOC
>&&
from
,
76
const
typename
IDBInitializer
<
ALLOC
>::
allocator_type
&
alloc
) :
77
var_names__
(
std
::
move
(
from
.
var_names__
),
alloc
),
78
input_type__
(
from
.
input_type__
) {
79
GUM_CONS_MOV
(
IDBInitializer
);
80
}
81
82
83
/// move constructor
84
template
<
template
<
typename
>
class
ALLOC
>
85
INLINE
86
IDBInitializer
<
ALLOC
>::
IDBInitializer
(
IDBInitializer
<
ALLOC
>&&
from
) :
87
IDBInitializer
(
std
::
move
(
from
),
from
.
getAllocator
()) {}
88
89
90
/// destructor
91
template
<
template
<
typename
>
class
ALLOC
>
92
IDBInitializer
<
ALLOC
>::~
IDBInitializer
() {
93
GUM_DESTRUCTOR
(
IDBInitializer
);
94
}
95
96
97
/// returns the names of the variables in the input database
98
template
<
template
<
typename
>
class
ALLOC
>
99
const
std
::
vector
<
std
::
string
,
ALLOC
<
std
::
string
> >&
100
IDBInitializer
<
ALLOC
>::
variableNames
() {
101
if
(
var_names__
.
empty
())
var_names__
=
this
->
variableNames_
();
102
return
var_names__
;
103
}
104
105
106
// copy operator
107
template
<
template
<
typename
>
class
ALLOC
>
108
IDBInitializer
<
ALLOC
>&
109
IDBInitializer
<
ALLOC
>::
operator
=(
const
IDBInitializer
<
ALLOC
>&
from
) {
110
if
(
this
!= &
from
) {
111
var_names__
=
from
.
var_names__
;
112
input_type__
=
from
.
input_type__
;
113
last_insertion_failed__
=
false
;
114
}
115
return
*
this
;
116
}
117
118
119
// move constructor
120
template
<
template
<
typename
>
class
ALLOC
>
121
IDBInitializer
<
ALLOC
>&
122
IDBInitializer
<
ALLOC
>::
operator
=(
IDBInitializer
<
ALLOC
>&&
from
) {
123
if
(
this
!= &
from
) {
124
var_names__
=
std
::
move
(
from
.
var_names__
);
125
input_type__
=
from
.
input_type__
;
126
last_insertion_failed__
=
false
;
127
}
128
return
*
this
;
129
}
130
131
132
/// fills the rows of the database
133
template
<
template
<
typename
>
class
ALLOC
>
134
template
<
template
<
template
<
typename
>
class
>
class
DATABASE
>
135
INLINE
void
IDBInitializer
<
ALLOC
>::
fillDatabase
(
DATABASE
<
ALLOC
>&
database
,
136
const
bool
retry_insertion
) {
137
switch
(
input_type__
) {
138
case
InputType
::
STRING
:
139
fillDatabaseFromStrings__
(
database
,
retry_insertion
);
140
return
;
141
142
case
InputType
::
DBCELL
:
143
fillDatabaseFromDBCells__
(
database
,
retry_insertion
);
144
return
;
145
146
default
:
147
GUM_ERROR
(
NotImplementedYet
,
148
"fillDatabase has not been implemented yet for this "
149
"type of IDBInitializerInputType"
);
150
}
151
}
152
153
154
/// fills the rows of the database using string inputs
155
template
<
template
<
typename
>
class
ALLOC
>
156
template
<
template
<
template
<
typename
>
class
>
class
DATABASE
>
157
void
IDBInitializer
<
ALLOC
>::
fillDatabaseFromStrings__
(
158
DATABASE
<
ALLOC
>&
database
,
159
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
179
/// fills the rows of the database using DBCell inputs
180
template
<
template
<
typename
>
class
ALLOC
>
181
template
<
template
<
template
<
typename
>
class
>
class
DATABASE
>
182
void
IDBInitializer
<
ALLOC
>::
fillDatabaseFromDBCells__
(
183
DATABASE
<
ALLOC
>&
database
,
184
const
bool
retry_insertion
) {
185
// if need be, try to reinsert the row that could not be inserted
186
if
(
retry_insertion
&&
last_insertion_failed__
) {
187
database
.
insertRow
(
currentDBCellRow_
());
188
last_insertion_failed__
=
false
;
189
}
190
191
// try to insert the next rows
192
while
(
this
->
nextRow_
()) {
193
try
{
194
// read a new line in the input file and insert it into the database
195
database
.
insertRow
(
currentDBCellRow_
());
196
}
catch
(...) {
197
last_insertion_failed__
=
true
;
198
throw
;
199
}
200
}
201
}
202
203
204
/// asks the child class for the content of the current row using strings
205
template
<
template
<
typename
>
class
ALLOC
>
206
const
std
::
vector
<
std
::
string
,
ALLOC
<
std
::
string
> >&
207
IDBInitializer
<
ALLOC
>::
currentStringRow_
() {
208
GUM_ERROR
(
FatalError
,
209
"Method currentStringRow_ should not be used or it should be "
210
"overloaded in children classes."
);
211
}
212
213
214
/// asks the child class for the content of the current row using dbcells
215
template
<
template
<
typename
>
class
ALLOC
>
216
const
DBRow
<
DBCell
,
ALLOC
>&
IDBInitializer
<
ALLOC
>::
currentDBCellRow_
() {
217
GUM_ERROR
(
FatalError
,
218
"Method currentDBCellRow_ should not be used or it should be "
219
"overloaded in children classes."
);
220
}
221
222
223
}
/* namespace learning */
224
225
}
/* namespace gum */
226
227
#
endif
/* DOXYGEN_SHOULD_SKIP_THIS */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669
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