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