aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
UAIMNReader_tpl.h
Go to the documentation of this file.
1
/**
2
*
3
* Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) et Christophe GONZALES(@AMU)
4
* (@AMU) 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
#
ifndef
DOXYGEN_SHOULD_SKIP_THIS
23
24
namespace
gum
{
25
26
template
<
typename
GUM_SCALAR >
27
UAIMNReader< GUM_SCALAR >::UAIMNReader(
MarkovNet
<
GUM_SCALAR
>*
MN
,
const
std
::
string
&
filename
) :
28
MNReader
<
GUM_SCALAR
>(
MN
,
filename
) {
29
GUM_CONSTRUCTOR
(
UAIMNReader
);
30
_mn_
=
MN
;
31
_mn_
->
clear
();
32
33
_streamName_
=
filename
;
34
_parseDone_
=
false
;
35
36
_ioerror_
=
false
;
37
38
try
{
39
_scanner_
=
new
UAIMN
::
Scanner
(
_streamName_
.
c_str
());
40
_parser_
=
new
UAIMN
::
Parser
(
_scanner_
);
41
}
catch
(
IOError
&) {
_ioerror_
=
true
; }
42
}
43
44
template
<
typename
GUM_SCALAR
>
45
UAIMNReader
<
GUM_SCALAR
>::~
UAIMNReader
() {
46
GUM_DESTRUCTOR
(
UAIMNReader
);
47
48
if
(!
_ioerror_
) {
49
// this could lead to memory leak !!
50
if
(
_parser_
)
delete
(
_parser_
);
51
52
if
(
_scanner_
)
delete
(
_scanner_
);
53
}
54
}
55
56
template
<
typename
GUM_SCALAR
>
57
INLINE
UAIMN
::
Scanner
&
UAIMNReader
<
GUM_SCALAR
>::
scanner
() {
58
if
(
_ioerror_
) {
GUM_ERROR
(
gum
::
IOError
,
"No such file "
+
streamName
()) }
59
60
return
*
_scanner_
;
61
}
62
63
template
<
typename
GUM_SCALAR
>
64
INLINE
const
std
::
string
&
UAIMNReader
<
GUM_SCALAR
>::
streamName
()
const
{
65
return
_streamName_
;
66
}
67
68
template
<
typename
GUM_SCALAR
>
69
INLINE
bool
UAIMNReader
<
GUM_SCALAR
>::
trace
()
const
{
70
return
_traceScanning_
;
71
}
72
73
template
<
typename
GUM_SCALAR
>
74
INLINE
void
UAIMNReader
<
GUM_SCALAR
>::
trace
(
bool
b
) {
75
_traceScanning_
=
b
;
76
scanner
().
setTrace
(
b
);
77
}
78
79
template
<
typename
GUM_SCALAR
>
80
Size
UAIMNReader
<
GUM_SCALAR
>::
proceed
() {
81
if
(
_ioerror_
) {
GUM_ERROR
(
gum
::
IOError
,
"No such file "
+
streamName
()) }
82
83
if
(!
_parseDone_
) {
84
try
{
85
_parser_
->
Parse
();
86
_parseDone_
=
true
;
87
buildFromQuartets
(
_parser_
->
getQuartets
());
88
}
catch
(
gum
::
Exception
&
e
) {
89
GUM_SHOWERROR
(
e
);
90
return
1 +
_parser_
->
errors
().
error_count
;
91
}
92
}
93
94
return
(
_parser_
->
errors
().
error_count
);
95
}
96
97
template
<
typename
GUM_SCALAR
>
98
void
UAIMNReader
<
GUM_SCALAR
>::
buildFromQuartets
(
99
std
::
vector
<
std
::
tuple
<
float
,
int
,
int
,
int
> >
quartets
) {
100
Idx
current
;
101
Size
max
=
quartets
.
size
();
102
if
(
max
== 0) {
103
_addWarning_
(1, 1,
"Empty MarkovNet"
);
104
return
;
105
}
106
107
auto
isInt
= [&]() ->
bool
{
108
return
(
std
::
get
< 0 >(
quartets
[
current
]) == -1);
109
};
110
auto
lig
= [&]() ->
int
{
111
return
std
::
get
< 2 >(
quartets
[
current
]);
112
};
113
auto
col
= [&]() ->
int
{
114
return
std
::
get
< 3 >(
quartets
[
current
]);
115
};
116
117
auto
getInt
= [&]() ->
int
{
118
if
(!
isInt
())
this
->
_addFatalError_
(
lig
(),
col
(),
"int expected"
);
119
return
std
::
get
< 1 >(
quartets
[
current
]);
120
};
121
auto
getVal
= [&]() ->
GUM_SCALAR
{
122
return
(
isInt
()) ? (
std
::
get
< 1 >(
quartets
[
current
])) : (
std
::
get
< 0 >(
quartets
[
current
]));
123
};
124
auto
incCurrent
= [&]() {
125
current
+= 1;
126
if
(
current
>=
max
)
this
->
_addFatalError_
(
lig
(),
col
(),
"Not enough data in UAI file"
);
127
};
128
129
current
= 0;
130
Size
nbrNode
= (
Size
)
getInt
();
131
132
for
(
NodeId
i
= 0;
i
<
nbrNode
;
i
++) {
133
incCurrent
();
134
int
mod
=
getInt
();
135
if
(
mod
< 2)
_addError_
(
lig
(),
col
(),
"Number of modalities should be greater than 2."
);
136
_mn_
->
add
(
gum
::
LabelizedVariable
(
std
::
to_string
(
i
),
""
,
mod
));
137
}
138
139
incCurrent
();
140
Size
nbrFactors
= (
Size
)
getInt
();
141
142
std
::
vector
<
NodeSet
>
clicks
;
143
for
(
NodeId
i
= 0;
i
<
nbrFactors
;
i
++) {
144
incCurrent
();
145
Size
nbrVar
= (
Size
)
getInt
();
146
if
(
nbrVar
== 0)
_addError_
(
lig
(),
col
(),
"0 is not possible here"
);
147
148
NodeSet
vars
;
149
for
(
NodeId
j
= 0;
j
<
nbrVar
;
j
++) {
150
incCurrent
();
151
NodeId
nod
= (
NodeId
)
getInt
();
152
if
(
nod
>=
nbrNode
)
_addError_
(
lig
(),
col
(),
"Not enough variables in the MarkovNet"
);
153
vars
.
insert
(
nod
);
154
}
155
_mn_
->
addFactor
(
vars
);
156
clicks
.
push_back
(
vars
);
157
}
158
159
for
(
NodeId
i
= 0;
i
<
nbrFactors
;
i
++) {
160
incCurrent
();
161
Size
nbrParam
= (
Size
)
getInt
();
162
if
(
nbrParam
!=
_mn_
->
factor
(
clicks
[
i
]).
domainSize
()) {
163
_addFatalError_
(
lig
(),
col
(),
"Size does not fit between clique and parameters"
);
164
}
165
std
::
vector
<
GUM_SCALAR
>
v
;
166
for
(
Idx
j
= 0;
j
<
nbrParam
;
j
++) {
167
incCurrent
();
168
v
.
push_back
(
getVal
());
169
}
170
_mn_
->
factor
(
clicks
[
i
]).
fillWith
(
v
);
171
v
.
clear
();
172
}
173
174
if
(
current
!=
max
- 1)
_addError_
(
lig
(),
col
(),
"Too many data in this file"
);
175
}
176
177
// @{
178
// publishing Errors API
179
template
<
typename
GUM_SCALAR
>
180
INLINE
Idx
UAIMNReader
<
GUM_SCALAR
>::
errLine
(
Idx
i
) {
181
if
(
_parseDone_
)
182
return
_parser_
->
errors
().
error
(
i
).
line
;
183
else
{
184
GUM_ERROR
(
OperationNotAllowed
,
"UAI file not parsed yet"
)
185
}
186
}
187
188
template
<
typename
GUM_SCALAR
>
189
INLINE
Idx
UAIMNReader
<
GUM_SCALAR
>::
errCol
(
Idx
i
) {
190
if
(
_parseDone_
)
191
return
_parser_
->
errors
().
error
(
i
).
column
;
192
else
{
193
GUM_ERROR
(
OperationNotAllowed
,
"UAI file not parsed yet"
)
194
}
195
}
196
197
template
<
typename
GUM_SCALAR
>
198
INLINE
bool
UAIMNReader
<
GUM_SCALAR
>::
errIsError
(
Idx
i
) {
199
if
(
_parseDone_
)
200
return
_parser_
->
errors
().
error
(
i
).
is_error
;
201
else
{
202
GUM_ERROR
(
OperationNotAllowed
,
"UAI file not parsed yet"
)
203
}
204
}
205
206
template
<
typename
GUM_SCALAR
>
207
INLINE
std
::
string
UAIMNReader
<
GUM_SCALAR
>::
errMsg
(
Idx
i
) {
208
if
(
_parseDone_
)
209
return
_parser_
->
errors
().
error
(
i
).
msg
;
210
else
{
211
GUM_ERROR
(
OperationNotAllowed
,
"UAI file not parsed yet"
)
212
}
213
}
214
215
template
<
typename
GUM_SCALAR
>
216
INLINE
void
UAIMNReader
<
GUM_SCALAR
>::
showElegantErrors
(
std
::
ostream
&
o
) {
217
if
(
_parseDone_
)
218
_parser_
->
errors
().
elegantErrors
(
o
);
219
else
{
220
GUM_ERROR
(
OperationNotAllowed
,
"UAI file not parsed yet"
)
221
}
222
}
223
224
template
<
typename
GUM_SCALAR
>
225
INLINE
void
UAIMNReader
<
GUM_SCALAR
>::
showElegantErrorsAndWarnings
(
std
::
ostream
&
o
) {
226
if
(
_parseDone_
)
227
_parser_
->
errors
().
elegantErrorsAndWarnings
(
o
);
228
else
{
229
GUM_ERROR
(
OperationNotAllowed
,
"UAI file not parsed yet"
)
230
}
231
}
232
233
template
<
typename
GUM_SCALAR
>
234
INLINE
void
UAIMNReader
<
GUM_SCALAR
>::
showErrorsAndWarnings
(
std
::
ostream
&
o
) {
235
if
(
_parseDone_
)
236
_parser_
->
errors
().
simpleErrorsAndWarnings
(
o
);
237
else
{
238
GUM_ERROR
(
OperationNotAllowed
,
"UAI file not parsed yet"
)
239
}
240
}
241
242
template
<
typename
GUM_SCALAR
>
243
INLINE
void
UAIMNReader
<
GUM_SCALAR
>::
showErrorCounts
(
std
::
ostream
&
o
) {
244
if
(
_parseDone_
)
245
_parser_
->
errors
().
syntheticResults
(
o
);
246
else
{
247
GUM_ERROR
(
OperationNotAllowed
,
"UAI file not parsed yet"
)
248
}
249
}
250
251
template
<
typename
GUM_SCALAR
>
252
INLINE
Size
UAIMNReader
<
GUM_SCALAR
>::
errors
() {
253
return
(!
_parseDone_
) ? (
Size
)0 :
_parser_
->
errors
().
error_count
;
254
}
255
256
template
<
typename
GUM_SCALAR
>
257
INLINE
Size
UAIMNReader
<
GUM_SCALAR
>::
warnings
() {
258
return
(!
_parseDone_
) ? (
Size
)0 :
_parser_
->
errors
().
warning_count
;
259
}
260
261
template
<
typename
GUM_SCALAR
>
262
INLINE
void
UAIMNReader
<
GUM_SCALAR
>::
_addFatalError_
(
Idx
lig
,
Idx
col
,
const
std
::
string
&
s
) {
263
_parser_
->
errors
().
addError
(
s
,
_streamName_
,
lig
,
col
);
264
GUM_ERROR
(
gum
::
OperationNotAllowed
,
s
)
265
}
266
267
template
<
typename
GUM_SCALAR
>
268
INLINE
void
UAIMNReader
<
GUM_SCALAR
>::
_addError_
(
Idx
lig
,
Idx
col
,
const
std
::
string
&
s
) {
269
_parser_
->
errors
().
addError
(
s
,
_streamName_
,
lig
,
col
);
270
}
271
272
template
<
typename
GUM_SCALAR
>
273
INLINE
void
UAIMNReader
<
GUM_SCALAR
>::
_addWarning_
(
Idx
lig
,
Idx
col
,
const
std
::
string
&
s
) {
274
_parser_
->
errors
().
addWarning
(
s
,
_streamName_
,
lig
,
col
);
275
}
276
277
// @}
278
}
// namespace gum
279
280
#
endif
// DOXYGEN_SHOULD_SKIP_THIS
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643