aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
DBTranslator_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 all the tabular databases' cell translators
24
*
25
* @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26
*/
27
#
include
<
agrum
/
tools
/
database
/
DBTranslator
.
h
>
28
29
#
ifndef
DOXYGEN_SHOULD_SKIP_THIS
30
31
namespace
gum
{
32
33
namespace
learning
{
34
35
36
/// returns the allocator used by the translator
37
template
<
template
<
typename
>
class
ALLOC
>
38
INLINE
typename
DBTranslator
<
ALLOC
>::
allocator_type
39
DBTranslator
<
ALLOC
>::
getAllocator
()
const
{
40
return
*
this
;
41
}
42
43
44
/// returns the type of values handled by the translator
45
template
<
template
<
typename
>
class
ALLOC
>
46
INLINE
DBTranslatedValueType
DBTranslator
<
ALLOC
>::
getValType
()
const
{
47
return
val_type_
;
48
}
49
50
51
/// default constructor
52
template
<
template
<
typename
>
class
ALLOC
>
53
template
<
template
<
typename
>
class
XALLOC
>
54
INLINE
DBTranslator
<
ALLOC
>::
DBTranslator
(
55
DBTranslatedValueType
val_type
,
56
const
std
::
vector
<
std
::
string
,
XALLOC
<
std
::
string
> >&
missing_symbols
,
57
const
bool
dynamic_dictionary
,
58
std
::
size_t
max_dico_entries
,
59
const
typename
DBTranslator
<
ALLOC
>::
allocator_type
&
alloc
) :
60
DBTranslator
<
ALLOC
>::
allocator_type
(
alloc
),
61
is_dictionary_dynamic_
(
dynamic_dictionary
),
62
max_dico_entries_
(
max_dico_entries
),
val_type_
(
val_type
) {
63
const
std
::
size_t
size
=
missing_symbols
.
size
();
64
65
if
(
size
) {
66
// save the set of symbols representing the missing values
67
missing_symbols_
.
resize
((
Size
)
missing_symbols
.
size
());
68
for
(
const
auto
&
symbol
:
missing_symbols
) {
69
missing_symbols_
.
insert
(
symbol
);
70
}
71
}
72
73
GUM_CONSTRUCTOR
(
DBTranslator
);
74
}
75
76
77
/// default constructor
78
template
<
template
<
typename
>
class
ALLOC
>
79
INLINE
DBTranslator
<
ALLOC
>::
DBTranslator
(
80
DBTranslatedValueType
val_type
,
81
const
bool
dynamic_dictionary
,
82
std
::
size_t
max_dico_entries
,
83
const
typename
DBTranslator
<
ALLOC
>::
allocator_type
&
alloc
) :
84
DBTranslator
<
ALLOC
>::
allocator_type
(
alloc
),
85
is_dictionary_dynamic_
(
dynamic_dictionary
),
86
max_dico_entries_
(
max_dico_entries
),
val_type_
(
val_type
) {
87
GUM_CONSTRUCTOR
(
DBTranslator
);
88
}
89
90
91
/// copy constructor with a given allocator
92
template
<
template
<
typename
>
class
ALLOC
>
93
INLINE
DBTranslator
<
ALLOC
>::
DBTranslator
(
94
const
DBTranslator
<
ALLOC
>&
from
,
95
const
typename
DBTranslator
<
ALLOC
>::
allocator_type
&
alloc
) :
96
DBTranslator
<
ALLOC
>::
allocator_type
(
alloc
),
97
is_dictionary_dynamic_
(
from
.
is_dictionary_dynamic_
),
98
max_dico_entries_
(
from
.
max_dico_entries_
),
99
missing_symbols_
(
from
.
missing_symbols_
),
back_dico_
(
from
.
back_dico_
),
100
val_type_
(
from
.
val_type_
) {
101
GUM_CONS_CPY
(
DBTranslator
);
102
}
103
104
105
/// copy constructor
106
template
<
template
<
typename
>
class
ALLOC
>
107
INLINE
DBTranslator
<
ALLOC
>::
DBTranslator
(
const
DBTranslator
<
ALLOC
>&
from
) :
108
DBTranslator
<
ALLOC
>(
from
,
from
.
getAllocator
()) {}
109
110
111
/// move constructor with a given allocator
112
template
<
template
<
typename
>
class
ALLOC
>
113
INLINE
DBTranslator
<
ALLOC
>::
DBTranslator
(
114
DBTranslator
<
ALLOC
>&&
from
,
115
const
typename
DBTranslator
<
ALLOC
>::
allocator_type
&
alloc
) :
116
DBTranslator
<
ALLOC
>::
allocator_type
(
alloc
),
117
is_dictionary_dynamic_
(
from
.
is_dictionary_dynamic_
),
118
max_dico_entries_
(
from
.
max_dico_entries_
),
119
missing_symbols_
(
std
::
move
(
from
.
missing_symbols_
)),
120
back_dico_
(
std
::
move
(
from
.
back_dico_
)),
val_type_
(
from
.
val_type_
) {
121
GUM_CONS_MOV
(
DBTranslator
);
122
}
123
124
125
/// move constructor
126
template
<
template
<
typename
>
class
ALLOC
>
127
INLINE
DBTranslator
<
ALLOC
>::
DBTranslator
(
DBTranslator
<
ALLOC
>&&
from
) :
128
DBTranslator
<
ALLOC
>(
from
,
from
.
getAllocator
()) {}
129
130
131
/// destructor
132
template
<
template
<
typename
>
class
ALLOC
>
133
INLINE
DBTranslator
<
ALLOC
>::~
DBTranslator
() {
134
GUM_DESTRUCTOR
(
DBTranslator
);
135
}
136
137
138
/// copy operator
139
template
<
template
<
typename
>
class
ALLOC
>
140
INLINE
DBTranslator
<
ALLOC
>&
141
DBTranslator
<
ALLOC
>::
operator
=(
const
DBTranslator
<
ALLOC
>&
from
) {
142
if
(
this
!= &
from
) {
143
is_dictionary_dynamic_
=
from
.
is_dictionary_dynamic_
;
144
max_dico_entries_
=
from
.
max_dico_entries_
;
145
missing_symbols_
=
from
.
missing_symbols_
;
146
back_dico_
=
from
.
back_dico_
;
147
val_type_
=
from
.
val_type_
;
148
}
149
return
*
this
;
150
}
151
152
153
/// move operator
154
template
<
template
<
typename
>
class
ALLOC
>
155
INLINE
DBTranslator
<
ALLOC
>&
156
DBTranslator
<
ALLOC
>::
operator
=(
DBTranslator
<
ALLOC
>&&
from
) {
157
is_dictionary_dynamic_
=
from
.
is_dictionary_dynamic_
;
158
max_dico_entries_
=
from
.
max_dico_entries_
;
159
missing_symbols_
=
std
::
move
(
from
.
missing_symbols_
);
160
back_dico_
=
std
::
move
(
from
.
back_dico_
);
161
val_type_
=
from
.
val_type_
;
162
163
return
*
this
;
164
}
165
166
167
/// alias for method translate
168
template
<
template
<
typename
>
class
ALLOC
>
169
INLINE
DBTranslatedValue
170
DBTranslator
<
ALLOC
>::
operator
<<(
const
std
::
string
&
str
) {
171
return
translate
(
str
);
172
}
173
174
175
/// alias for method translateBack
176
template
<
template
<
typename
>
class
ALLOC
>
177
INLINE
std
::
string
178
DBTranslator
<
ALLOC
>::
operator
>>(
const
DBTranslatedValue
translated_val
) {
179
return
translateBack
(
translated_val
);
180
}
181
182
183
/// indicates whether the translator has an editable dictionary or not
184
template
<
template
<
typename
>
class
ALLOC
>
185
INLINE
bool
DBTranslator
<
ALLOC
>::
hasEditableDictionary
()
const
{
186
return
is_dictionary_dynamic_
;
187
}
188
189
190
/// sets/unset the editable dictionary mode
191
template
<
template
<
typename
>
class
ALLOC
>
192
INLINE
void
DBTranslator
<
ALLOC
>::
setEditableDictionaryMode
(
bool
new_mode
) {
193
is_dictionary_dynamic_
=
new_mode
;
194
}
195
196
197
/// returns the set of missing symbols taken into account by the translator
198
template
<
template
<
typename
>
class
ALLOC
>
199
INLINE
const
Set
<
std
::
string
,
ALLOC
<
std
::
string
> >&
200
DBTranslator
<
ALLOC
>::
missingSymbols
()
const
{
201
return
missing_symbols_
;
202
}
203
204
205
/// indicates whether a string corresponds to a missing symbol
206
template
<
template
<
typename
>
class
ALLOC
>
207
INLINE
bool
208
DBTranslator
<
ALLOC
>::
isMissingSymbol
(
const
std
::
string
&
str
)
const
{
209
return
missing_symbols_
.
exists
(
str
);
210
}
211
212
213
/// sets the name of the variable stored into the translator
214
template
<
template
<
typename
>
class
ALLOC
>
215
INLINE
void
216
DBTranslator
<
ALLOC
>::
setVariableName
(
const
std
::
string
&
str
)
const
{
217
const_cast
<
Variable
* >(
this
->
variable
())->
setName
(
str
);
218
}
219
220
221
/// sets the name of the variable stored into the translator
222
template
<
template
<
typename
>
class
ALLOC
>
223
INLINE
void
DBTranslator
<
ALLOC
>::
setVariableDescription
(
224
const
std
::
string
&
str
)
const
{
225
const_cast
<
Variable
* >(
this
->
variable
())->
setDescription
(
str
);
226
}
227
228
229
/// indicates whether a translated value corresponds to a missing value
230
template
<
template
<
typename
>
class
ALLOC
>
231
INLINE
bool
DBTranslator
<
ALLOC
>::
isMissingValue
(
232
const
DBTranslatedValue
&
value
)
const
{
233
switch
(
val_type_
) {
234
case
DBTranslatedValueType
::
DISCRETE
:
235
return
value
.
discr_val
==
std
::
numeric_limits
<
std
::
size_t
>::
max
();
236
237
case
DBTranslatedValueType
::
CONTINUOUS
:
238
return
value
.
cont_val
==
std
::
numeric_limits
<
float
>::
max
();
239
240
default
:
241
GUM_ERROR
(
NotImplementedYet
,
242
"No missing value interpretation for this "
243
"translated value type"
);
244
}
245
}
246
247
248
}
/* namespace learning */
249
250
}
/* namespace gum */
251
252
#
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