aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
BIFReader_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
/**
23
* @file
24
* @brief Definition of templatized reader of BIF files for Bayesian networks.
25
*
26
* @author Pierre-Henri WUILLEMIN
27
*/
28
29
30
#
include
<
agrum
/
BN
/
io
/
BIF
/
BIFReader
.
h
>
31
#
include
<
agrum
/
BN
/
io
/
BNReader
.
h
>
32
#
ifndef
DOXYGEN_SHOULD_SKIP_THIS
33
34
namespace
gum
{
35
36
template
<
typename
GUM_SCALAR >
37
BIFReader< GUM_SCALAR >::BIFReader(
BayesNet
<
GUM_SCALAR
>*
bn
,
const
std
::
string
&
filename
) :
38
BNReader
<
GUM_SCALAR
>(
bn
,
filename
) {
39
GUM_CONSTRUCTOR
(
BIFReader
);
40
_bn_
=
bn
;
41
_streamName_
=
filename
;
42
_parseDone_
=
false
;
43
44
_factory_
=
new
BayesNetFactory
<
GUM_SCALAR
>(
_bn_
);
45
46
_ioerror_
=
false
;
47
48
try
{
49
_scanner_
=
new
BIF
::
Scanner
(
_streamName_
.
c_str
());
50
_parser_
=
new
BIF
::
Parser
(
_scanner_
);
51
_parser_
->
setFactory
((
IBayesNetFactory
*)
_factory_
);
52
}
catch
(
IOError
&) {
_ioerror_
=
true
; }
53
}
54
55
template
<
typename
GUM_SCALAR
>
56
BIFReader
<
GUM_SCALAR
>::~
BIFReader
() {
57
GUM_DESTRUCTOR
(
BIFReader
);
58
59
if
(!
_ioerror_
) {
60
// this could lead to memory leak !!
61
if
(
_parser_
)
delete
(
_parser_
);
62
63
if
(
_scanner_
)
delete
(
_scanner_
);
64
}
65
66
if
(
_factory_
)
delete
(
_factory_
);
67
}
68
69
template
<
typename
GUM_SCALAR
>
70
INLINE
BIF
::
Scanner
&
BIFReader
<
GUM_SCALAR
>::
scanner
() {
71
if
(
_ioerror_
) {
GUM_ERROR
(
gum
::
IOError
,
"No such file "
+
streamName
()) }
72
73
return
*
_scanner_
;
74
}
75
76
template
<
typename
GUM_SCALAR
>
77
INLINE
const
std
::
string
&
BIFReader
<
GUM_SCALAR
>::
streamName
()
const
{
78
return
_streamName_
;
79
}
80
81
template
<
typename
GUM_SCALAR
>
82
INLINE
bool
BIFReader
<
GUM_SCALAR
>::
trace
()
const
{
83
return
_traceScanning_
;
84
}
85
86
template
<
typename
GUM_SCALAR
>
87
INLINE
void
BIFReader
<
GUM_SCALAR
>::
trace
(
bool
b
) {
88
_traceScanning_
=
b
;
89
scanner
().
setTrace
(
b
);
90
}
91
92
template
<
typename
GUM_SCALAR
>
93
Size
BIFReader
<
GUM_SCALAR
>::
proceed
() {
94
if
(
_ioerror_
) {
GUM_ERROR
(
gum
::
IOError
,
"No such file "
+
streamName
()) }
95
96
if
(!
_parseDone_
) {
97
try
{
98
_parser_
->
Parse
();
99
_parseDone_
=
true
;
100
}
catch
(
gum
::
Exception
&
e
) {
101
GUM_SHOWERROR
(
e
);
102
return
1 +
_parser_
->
errors
().
error_count
;
103
}
104
}
105
106
return
(
_parser_
->
errors
().
error_count
);
107
}
108
109
/// @{
110
/// publishing Errors API
111
template
<
typename
GUM_SCALAR
>
112
INLINE
Idx
BIFReader
<
GUM_SCALAR
>::
errLine
(
Idx
i
) {
113
if
(
_parseDone_
)
114
return
_parser_
->
errors
().
error
(
i
).
line
;
115
else
{
116
GUM_ERROR
(
OperationNotAllowed
,
"BIF file not parsed yet"
)
117
}
118
}
119
120
template
<
typename
GUM_SCALAR
>
121
INLINE
Idx
BIFReader
<
GUM_SCALAR
>::
errCol
(
Idx
i
) {
122
if
(
_parseDone_
)
123
return
_parser_
->
errors
().
error
(
i
).
column
;
124
else
{
125
GUM_ERROR
(
OperationNotAllowed
,
"BIF file not parsed yet"
)
126
}
127
}
128
129
template
<
typename
GUM_SCALAR
>
130
INLINE
bool
BIFReader
<
GUM_SCALAR
>::
errIsError
(
Idx
i
) {
131
if
(
_parseDone_
)
132
return
_parser_
->
errors
().
error
(
i
).
is_error
;
133
else
{
134
GUM_ERROR
(
OperationNotAllowed
,
"BIF file not parsed yet"
)
135
}
136
}
137
138
template
<
typename
GUM_SCALAR
>
139
INLINE
std
::
string
BIFReader
<
GUM_SCALAR
>::
errMsg
(
Idx
i
) {
140
if
(
_parseDone_
)
141
return
_parser_
->
errors
().
error
(
i
).
msg
;
142
else
{
143
GUM_ERROR
(
OperationNotAllowed
,
"BIF file not parsed yet"
)
144
}
145
}
146
147
template
<
typename
GUM_SCALAR
>
148
INLINE
void
BIFReader
<
GUM_SCALAR
>::
showElegantErrors
(
std
::
ostream
&
o
) {
149
if
(
_parseDone_
)
150
_parser_
->
errors
().
elegantErrors
(
o
);
151
else
{
152
GUM_ERROR
(
OperationNotAllowed
,
"BIF file not parsed yet"
)
153
}
154
}
155
156
template
<
typename
GUM_SCALAR
>
157
INLINE
void
BIFReader
<
GUM_SCALAR
>::
showElegantErrorsAndWarnings
(
std
::
ostream
&
o
) {
158
if
(
_parseDone_
)
159
_parser_
->
errors
().
elegantErrorsAndWarnings
(
o
);
160
else
{
161
GUM_ERROR
(
OperationNotAllowed
,
"BIF file not parsed yet"
)
162
}
163
}
164
165
template
<
typename
GUM_SCALAR
>
166
INLINE
void
BIFReader
<
GUM_SCALAR
>::
showErrorCounts
(
std
::
ostream
&
o
) {
167
if
(
_parseDone_
)
168
_parser_
->
errors
().
syntheticResults
(
o
);
169
else
{
170
GUM_ERROR
(
OperationNotAllowed
,
"BIF file not parsed yet"
)
171
}
172
}
173
174
template
<
typename
GUM_SCALAR
>
175
INLINE
Size
BIFReader
<
GUM_SCALAR
>::
errors
() {
176
return
(!
_parseDone_
) ? (
Size
)0 :
_parser_
->
errors
().
error_count
;
177
}
178
179
template
<
typename
GUM_SCALAR
>
180
INLINE
Size
BIFReader
<
GUM_SCALAR
>::
warnings
() {
181
return
(!
_parseDone_
) ? (
Size
)0 :
_parser_
->
errors
().
warning_count
;
182
}
183
184
/// @}
185
}
// namespace gum
186
187
#
endif
// DOXYGEN_SHOULD_SKIP_THIS
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643