aGrUM
0.20.3
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
/// 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
),
max_dico_entries_
(
max_dico_entries
),
62
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
),
max_dico_entries_
(
max_dico_entries
),
86
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_
),
missing_symbols_
(
from
.
missing_symbols_
),
99
back_dico_
(
from
.
back_dico_
),
val_type_
(
from
.
val_type_
) {
100
GUM_CONS_CPY
(
DBTranslator
);
101
}
102
103
104
/// copy constructor
105
template
<
template
<
typename
>
class
ALLOC
>
106
INLINE
DBTranslator
<
ALLOC
>::
DBTranslator
(
const
DBTranslator
<
ALLOC
>&
from
) :
107
DBTranslator
<
ALLOC
>(
from
,
from
.
getAllocator
()) {}
108
109
110
/// move constructor with a given allocator
111
template
<
template
<
typename
>
class
ALLOC
>
112
INLINE
DBTranslator
<
ALLOC
>::
DBTranslator
(
113
DBTranslator
<
ALLOC
>&&
from
,
114
const
typename
DBTranslator
<
ALLOC
>::
allocator_type
&
alloc
) :
115
DBTranslator
<
ALLOC
>::
allocator_type
(
alloc
),
116
is_dictionary_dynamic_
(
from
.
is_dictionary_dynamic_
),
117
max_dico_entries_
(
from
.
max_dico_entries_
),
118
missing_symbols_
(
std
::
move
(
from
.
missing_symbols_
)),
back_dico_
(
std
::
move
(
from
.
back_dico_
)),
119
val_type_
(
from
.
val_type_
) {
120
GUM_CONS_MOV
(
DBTranslator
);
121
}
122
123
124
/// move constructor
125
template
<
template
<
typename
>
class
ALLOC
>
126
INLINE
DBTranslator
<
ALLOC
>::
DBTranslator
(
DBTranslator
<
ALLOC
>&&
from
) :
127
DBTranslator
<
ALLOC
>(
from
,
from
.
getAllocator
()) {}
128
129
130
/// destructor
131
template
<
template
<
typename
>
class
ALLOC
>
132
INLINE
DBTranslator
<
ALLOC
>::~
DBTranslator
() {
133
GUM_DESTRUCTOR
(
DBTranslator
);
134
}
135
136
137
/// copy operator
138
template
<
template
<
typename
>
class
ALLOC
>
139
INLINE
DBTranslator
<
ALLOC
>&
140
DBTranslator
<
ALLOC
>::
operator
=(
const
DBTranslator
<
ALLOC
>&
from
) {
141
if
(
this
!= &
from
) {
142
is_dictionary_dynamic_
=
from
.
is_dictionary_dynamic_
;
143
max_dico_entries_
=
from
.
max_dico_entries_
;
144
missing_symbols_
=
from
.
missing_symbols_
;
145
back_dico_
=
from
.
back_dico_
;
146
val_type_
=
from
.
val_type_
;
147
}
148
return
*
this
;
149
}
150
151
152
/// move operator
153
template
<
template
<
typename
>
class
ALLOC
>
154
INLINE
DBTranslator
<
ALLOC
>&
DBTranslator
<
ALLOC
>::
operator
=(
DBTranslator
<
ALLOC
>&&
from
) {
155
is_dictionary_dynamic_
=
from
.
is_dictionary_dynamic_
;
156
max_dico_entries_
=
from
.
max_dico_entries_
;
157
missing_symbols_
=
std
::
move
(
from
.
missing_symbols_
);
158
back_dico_
=
std
::
move
(
from
.
back_dico_
);
159
val_type_
=
from
.
val_type_
;
160
161
return
*
this
;
162
}
163
164
165
/// alias for method translate
166
template
<
template
<
typename
>
class
ALLOC
>
167
INLINE
DBTranslatedValue
DBTranslator
<
ALLOC
>::
operator
<<(
const
std
::
string
&
str
) {
168
return
translate
(
str
);
169
}
170
171
172
/// alias for method translateBack
173
template
<
template
<
typename
>
class
ALLOC
>
174
INLINE
std
::
string
DBTranslator
<
ALLOC
>::
operator
>>(
const
DBTranslatedValue
translated_val
) {
175
return
translateBack
(
translated_val
);
176
}
177
178
179
/// indicates whether the translator has an editable dictionary or not
180
template
<
template
<
typename
>
class
ALLOC
>
181
INLINE
bool
DBTranslator
<
ALLOC
>::
hasEditableDictionary
()
const
{
182
return
is_dictionary_dynamic_
;
183
}
184
185
186
/// sets/unset the editable dictionary mode
187
template
<
template
<
typename
>
class
ALLOC
>
188
INLINE
void
DBTranslator
<
ALLOC
>::
setEditableDictionaryMode
(
bool
new_mode
) {
189
is_dictionary_dynamic_
=
new_mode
;
190
}
191
192
193
/// returns the set of missing symbols taken into account by the translator
194
template
<
template
<
typename
>
class
ALLOC
>
195
INLINE
const
Set
<
std
::
string
,
ALLOC
<
std
::
string
> >&
196
DBTranslator
<
ALLOC
>::
missingSymbols
()
const
{
197
return
missing_symbols_
;
198
}
199
200
201
/// indicates whether a string corresponds to a missing symbol
202
template
<
template
<
typename
>
class
ALLOC
>
203
INLINE
bool
DBTranslator
<
ALLOC
>::
isMissingSymbol
(
const
std
::
string
&
str
)
const
{
204
return
missing_symbols_
.
exists
(
str
);
205
}
206
207
208
/// sets the name of the variable stored into the translator
209
template
<
template
<
typename
>
class
ALLOC
>
210
INLINE
void
DBTranslator
<
ALLOC
>::
setVariableName
(
const
std
::
string
&
str
)
const
{
211
const_cast
<
Variable
* >(
this
->
variable
())->
setName
(
str
);
212
}
213
214
215
/// sets the name of the variable stored into the translator
216
template
<
template
<
typename
>
class
ALLOC
>
217
INLINE
void
DBTranslator
<
ALLOC
>::
setVariableDescription
(
const
std
::
string
&
str
)
const
{
218
const_cast
<
Variable
* >(
this
->
variable
())->
setDescription
(
str
);
219
}
220
221
222
/// indicates whether a translated value corresponds to a missing value
223
template
<
template
<
typename
>
class
ALLOC
>
224
INLINE
bool
DBTranslator
<
ALLOC
>::
isMissingValue
(
const
DBTranslatedValue
&
value
)
const
{
225
switch
(
val_type_
) {
226
case
DBTranslatedValueType
::
DISCRETE
:
227
return
value
.
discr_val
==
std
::
numeric_limits
<
std
::
size_t
>::
max
();
228
229
case
DBTranslatedValueType
::
CONTINUOUS
:
230
return
value
.
cont_val
==
std
::
numeric_limits
<
float
>::
max
();
231
232
default
:
233
GUM_ERROR
(
NotImplementedYet
,
234
"No missing value interpretation for this "
235
"translated value type"
);
236
}
237
}
238
239
240
}
/* namespace learning */
241
242
}
/* namespace gum */
243
244
#
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