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