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