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