aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
BIFXMLBNReader_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
#
include
<
agrum
/
BN
/
io
/
BIFXML
/
BIFXMLBNReader
.
h
>
24
25
namespace
gum
{
26
/*
27
* Constructor
28
* A reader is created to reading a defined file.
29
* Note that an BN as to be created before and given in parameter.
30
*/
31
template
<
typename
GUM_SCALAR >
32
INLINE BIFXMLBNReader<
GUM_SCALAR
>::
BIFXMLBNReader
(
BayesNet
<
GUM_SCALAR
>*
bn
,
33
const
std
::
string
&
filePath
) :
34
BNReader
<
GUM_SCALAR
>(
bn
,
filePath
) {
35
GUM_CONSTRUCTOR
(
BIFXMLBNReader
);
36
_bn_
=
bn
;
37
_filePath_
=
filePath
;
38
}
39
40
/*
41
* Default destructor.
42
*/
43
template
<
typename
GUM_SCALAR
>
44
INLINE
BIFXMLBNReader
<
GUM_SCALAR
>::~
BIFXMLBNReader
() {
45
GUM_DESTRUCTOR
(
BIFXMLBNReader
);
46
}
47
48
/*
49
* Reads the bayes net from the file referenced by filePath given at the
50
* creation
51
* of class
52
* @return Returns the number of error during the parsing (0 if none).
53
*/
54
template
<
typename
GUM_SCALAR
>
55
Size
BIFXMLBNReader
<
GUM_SCALAR
>::
proceed
() {
56
try
{
57
// Loading file
58
std
::
string
status
=
"Loading File ..."
;
59
GUM_EMIT2
(
onProceed
, 0,
status
);
60
61
ticpp
::
Document
xmlDoc
(
_filePath_
);
62
xmlDoc
.
LoadFile
();
63
64
if
(
xmlDoc
.
NoChildren
()) {
65
GUM_ERROR
(
IOError
,
": Loading fail, please check the file for any syntax error."
)
66
}
67
68
// Finding BIF element
69
status
=
"File loaded. Now looking for BIF element ..."
;
70
GUM_EMIT2
(
onProceed
, 4,
status
);
71
72
ticpp
::
Element
*
bifElement
=
xmlDoc
.
FirstChildElement
(
"BIF"
);
73
74
// Finding network element
75
status
=
"BIF Element reached. Now searching network ..."
;
76
GUM_EMIT2
(
onProceed
, 7,
status
);
77
78
ticpp
::
Element
*
networkElement
=
bifElement
->
FirstChildElement
(
"NETWORK"
);
79
80
// Finding id variables
81
status
=
"Network found. Now proceedind variables instanciation..."
;
82
GUM_EMIT2
(
onProceed
, 10,
status
);
83
84
_parsingVariables_
(
networkElement
);
85
86
// Filling diagram
87
status
=
"All variables have been instancied. Now filling up diagram..."
;
88
GUM_EMIT2
(
onProceed
, 55,
status
);
89
90
_fillingBN_
(
networkElement
);
91
92
status
=
"Instanciation of network completed"
;
93
GUM_EMIT2
(
onProceed
, 100,
status
);
94
95
return
0;
96
}
catch
(
ticpp
::
Exception
&
tinyexception
) {
GUM_ERROR
(
IOError
,
tinyexception
.
what
()) }
97
}
98
99
template
<
typename
GUM_SCALAR
>
100
void
BIFXMLBNReader
<
GUM_SCALAR
>::
_parsingVariables_
(
ticpp
::
Element
*
parentNetwork
) {
101
// Counting the number of variable for the signal
102
int
nbVar
= 0;
103
ticpp
::
Iterator
<
ticpp
::
Element
>
varIte
(
"VARIABLE"
);
104
105
for
(
varIte
=
varIte
.
begin
(
parentNetwork
);
varIte
!=
varIte
.
end
(); ++
varIte
)
106
nbVar
++;
107
108
// Iterating on variable element
109
int
nbIte
= 0;
110
111
for
(
varIte
=
varIte
.
begin
(
parentNetwork
);
varIte
!=
varIte
.
end
(); ++
varIte
) {
112
ticpp
::
Element
*
currentVar
=
varIte
.
Get
();
113
114
// Getting variable name
115
ticpp
::
Element
*
varNameElement
=
currentVar
->
FirstChildElement
(
"NAME"
);
116
std
::
string
varName
=
varNameElement
->
GetTextOrDefault
(
""
);
117
118
// Getting variable description
119
ticpp
::
Element
*
varDescrElement
=
currentVar
->
FirstChildElement
(
"PROPERTY"
);
120
std
::
string
varDescription
=
varDescrElement
->
GetTextOrDefault
(
""
);
121
122
// Instanciation de la variable
123
auto
newVar
=
new
LabelizedVariable
(
varName
,
varDescription
, 0);
124
125
// Getting variable outcomes
126
ticpp
::
Iterator
<
ticpp
::
Element
>
varOutComesIte
(
"OUTCOME"
);
127
128
for
(
varOutComesIte
=
varOutComesIte
.
begin
(
currentVar
);
129
varOutComesIte
!=
varOutComesIte
.
end
();
130
++
varOutComesIte
)
131
newVar
->
addLabel
(
varOutComesIte
->
GetTextOrDefault
(
""
));
132
133
// Add the variable to the bn and then delete newVar (add makes a copy)
134
_bn_
->
add
(*
newVar
);
135
delete
(
newVar
);
136
137
// Emitting progress.
138
std
::
string
status
=
"Network found. Now proceedind variables instanciation..."
;
139
int
progress
= (
int
)((
float
)
nbIte
/ (
float
)
nbVar
* 45) + 10;
140
GUM_EMIT2
(
onProceed
,
progress
,
status
);
141
nbIte
++;
142
}
143
}
144
145
template
<
typename
GUM_SCALAR
>
146
void
BIFXMLBNReader
<
GUM_SCALAR
>::
_fillingBN_
(
ticpp
::
Element
*
parentNetwork
) {
147
// Counting the number of variable for the signal
148
int
nbDef
= 0;
149
ticpp
::
Iterator
<
ticpp
::
Element
>
definitionIte
(
"DEFINITION"
);
150
151
for
(
definitionIte
=
definitionIte
.
begin
(
parentNetwork
);
definitionIte
!=
definitionIte
.
end
();
152
++
definitionIte
)
153
nbDef
++;
154
155
// Iterating on definition nodes
156
int
nbIte
= 0;
157
158
for
(
definitionIte
=
definitionIte
.
begin
(
parentNetwork
);
definitionIte
!=
definitionIte
.
end
();
159
++
definitionIte
) {
160
ticpp
::
Element
*
currentVar
=
definitionIte
.
Get
();
161
162
// Considered Node
163
std
::
string
currentVarName
=
currentVar
->
FirstChildElement
(
"FOR"
)->
GetTextOrDefault
(
""
);
164
NodeId
currentVarId
=
_bn_
->
idFromName
(
currentVarName
);
165
166
// Get Node's parents
167
ticpp
::
Iterator
<
ticpp
::
Element
>
givenIte
(
"GIVEN"
);
168
List
<
NodeId
>
parentList
;
169
170
for
(
givenIte
=
givenIte
.
begin
(
currentVar
);
givenIte
!=
givenIte
.
end
(); ++
givenIte
) {
171
std
::
string
parentNode
=
givenIte
->
GetTextOrDefault
(
""
);
172
NodeId
parentId
=
_bn_
->
idFromName
(
parentNode
);
173
parentList
.
pushBack
(
parentId
);
174
}
175
176
for
(
List
<
NodeId
>::
iterator_safe
parentListIte
=
parentList
.
rbeginSafe
();
177
parentListIte
!=
parentList
.
rendSafe
();
178
--
parentListIte
)
179
_bn_
->
addArc
(*
parentListIte
,
currentVarId
);
180
181
// Recuperating tables values
182
ticpp
::
Element
*
tableElement
=
currentVar
->
FirstChildElement
(
"TABLE"
);
183
std
::
istringstream
issTableString
(
tableElement
->
GetTextOrDefault
(
""
));
184
std
::
list
<
GUM_SCALAR
>
tablelist
;
185
GUM_SCALAR
value
;
186
187
while
(!
issTableString
.
eof
()) {
188
issTableString
>>
value
;
189
tablelist
.
push_back
(
value
);
190
}
191
192
std
::
vector
<
GUM_SCALAR
>
tablevector
(
tablelist
.
begin
(),
tablelist
.
end
());
193
194
// Filling tables
195
_bn_
->
cpt
(
currentVarId
).
fillWith
(
tablevector
);
196
197
// Emitting progress.
198
std
::
string
status
=
"All variables have been instancied. Now filling up diagram..."
;
199
int
progress
= (
int
)((
float
)
nbIte
/ (
float
)
nbDef
* 45) + 55;
200
GUM_EMIT2
(
onProceed
,
progress
,
status
);
201
nbIte
++;
202
}
203
}
204
205
}
/* namespace gum */
206
207
#
endif
// DOXYGEN_SHOULD_SKIP_THIS
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643