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