aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
BIFXMLIDWriter_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
/
ID
/
io
/
BIFXML
/
BIFXMLIDWriter
.
h
>
25
26
namespace
gum
{
27
28
/*
29
* Default constructor.
30
*/
31
template
<
typename
GUM_SCALAR >
32
INLINE BIFXMLIDWriter<
GUM_SCALAR
>::
BIFXMLIDWriter
() {
33
GUM_CONSTRUCTOR
(
BIFXMLIDWriter
);
34
}
35
36
/*
37
* Destructor.
38
*/
39
template
<
typename
GUM_SCALAR
>
40
INLINE
BIFXMLIDWriter
<
GUM_SCALAR
>::~
BIFXMLIDWriter
() {
41
GUM_DESTRUCTOR
(
BIFXMLIDWriter
);
42
}
43
44
/*
45
* Writes an influence diagram in the given ouput stream.
46
*
47
* @param output The output stream.
48
* @param infdiag The influence diagram writen in the stream.
49
* @throws IOError Raised if an I/O error occurs.
50
*/
51
template
<
typename
GUM_SCALAR
>
52
INLINE
void
BIFXMLIDWriter
<
GUM_SCALAR
>::
write
(
std
::
ostream
&
output
,
53
const
InfluenceDiagram
<
GUM_SCALAR
>&
infdiag
) {
54
if
(!
output
.
good
()) {
GUM_ERROR
(
IOError
,
"Stream states flags are not all unset."
) }
55
56
output
<<
_heading_
() <<
std
::
endl
;
57
output
<<
"<!-- Variables -->"
<<
std
::
endl
;
58
59
for
(
const
auto
node
:
infdiag
.
nodes
()) {
60
int
nodeType
= 1;
61
62
if
(
infdiag
.
isChanceNode
(
node
))
63
nodeType
= 2;
64
else
if
(
infdiag
.
isUtilityNode
(
node
))
65
nodeType
= 3;
66
67
output
<<
_variableBloc_
(
infdiag
.
variable
(
node
),
nodeType
) <<
std
::
endl
;
68
}
69
70
output
<<
"<!-- Probability distributions -->"
<<
std
::
endl
;
71
72
for
(
const
auto
node
:
infdiag
.
nodes
())
73
output
<<
_variableDefinition_
(
node
,
infdiag
);
74
75
output
<<
std
::
endl
;
76
output
<<
_documentend_
();
77
output
.
flush
();
78
79
if
(
output
.
fail
()) {
GUM_ERROR
(
IOError
,
"Writting in the ostream failed."
) }
80
}
81
82
/*
83
* Writes an Influence Diagram in the file referenced by filePath.
84
* If the file doesn't exists, it is created.
85
* If the file exists, it's content will be erased.
86
*
87
* @param filePath The path to the file used to write the Influence Diagram.
88
* @param infdiag The Influence Diagram writen in the file.
89
* @throw IOError Raised if an I/O error occurs.
90
*/
91
template
<
typename
GUM_SCALAR
>
92
INLINE
void
BIFXMLIDWriter
<
GUM_SCALAR
>::
write
(
std
::
string
filePath
,
93
const
InfluenceDiagram
<
GUM_SCALAR
>&
infdiag
) {
94
std
::
ofstream
output
(
filePath
.
c_str
(),
std
::
ios_base
::
trunc
);
95
96
write
(
output
,
infdiag
);
97
98
output
.
close
();
99
100
if
(
output
.
fail
()) {
GUM_ERROR
(
IOError
,
"Writting in the ostream failed."
) }
101
}
102
103
/*
104
* Returns the header of the BIF file.
105
*/
106
template
<
typename
GUM_SCALAR
>
107
INLINE
std
::
string
BIFXMLIDWriter
<
GUM_SCALAR
>::
_heading_
() {
108
std
::
stringstream
str
;
109
110
// Header for every xml
111
str
<<
"<?xml version=\"1.0\" ?>"
<<
std
::
endl
;
112
113
// Document type definition of BIF 0.3
114
/*str << "<!-- DTD for the XMLBIF 0.3 format -->" << std::endl;
115
str << "<!DOCTYPE BIF [" << std::endl;
116
str << "\t<!ELEMENT BIF ( NETWORK )*>" << std::endl;
117
str << "\t\t<!ATTLIST BIF VERSION CDATA #REQUIRED>" << std::endl;
118
str << "\t<!ELEMENT NETWORK ( NAME, ( PROPERTY | VARIABLE | DEFINITION )*
119
)>" <<
120
std::endl;
121
str << "\t<!ELEMENT NAME (#PCDATA)>" << std::endl;
122
str << "\t<!ELEMENT VARIABLE ( NAME, ( OUTCOME | PROPERTY )* ) >" <<
123
std::endl;
124
str << "\t\t<!ATTLIST VARIABLE TYPE (nature|decision|utility) \"nature\">"
125
<<
126
std::endl;
127
str << "\t<!ELEMENT OUTCOME (#PCDATA)>" << std::endl;
128
str << "\t<!ELEMENT DEFINITION ( FOR | GIVEN | TABLE | PROPERTY )* >" <<
129
std::endl;
130
str << "\t<!ELEMENT FOR (#PCDATA)>" << std::endl;
131
str << "\t<!ELEMENT GIVEN (#PCDATA)>" << std::endl;
132
str << "\t<!ELEMENT TABLE (#PCDATA)>" << std::endl;
133
str << "\t<!ELEMENT PROPERTY (#PCDATA)>" << std::endl;
134
str << "]>" << std::endl;*/
135
136
// BIF version Tag
137
str
<<
std
::
endl
<<
"<BIF VERSION=\"0.3\">"
<<
std
::
endl
;
138
139
// Network declaration
140
str
<<
"<NETWORK>"
<<
std
::
endl
;
141
142
return
str
.
str
();
143
}
144
145
/*
146
* Returns a bloc defining a variable in the BIF format.
147
*/
148
template
<
typename
GUM_SCALAR
>
149
INLINE
std
::
string
BIFXMLIDWriter
<
GUM_SCALAR
>::
_variableBloc_
(
const
DiscreteVariable
&
var
,
150
int
varType
) {
151
//<VARIABLE TYPE="nature|decision|utility">
152
//<NAME>name</NAME>
153
//<OUTCOME>outcome1</OUTCOME>
154
//<OUTCOME>outcome2</OUTCOME>
155
//<PROPERTY>property</PROPERTY>
156
//</VARIABLE>
157
158
std
::
stringstream
str
;
159
160
// Declaration of variable and his type
161
str
<<
"<VARIABLE TYPE=\""
;
162
163
switch
(
varType
) {
164
case
1:
165
str
<<
"decision"
;
166
break
;
167
168
case
2:
169
str
<<
"nature"
;
170
break
;
171
172
case
3:
173
str
<<
"utility"
;
174
break
;
175
176
default
:
177
break
;
178
}
179
180
str
<<
"\">"
<<
std
::
endl
;
181
182
// Name and description
183
str
<<
"\t<NAME>"
<<
var
.
name
() <<
"</NAME>"
<<
std
::
endl
;
184
str
<<
"\t<PROPERTY>"
<<
var
.
description
() <<
"</PROPERTY>"
<<
std
::
endl
;
185
186
// Outcomes
187
188
for
(
Idx
i
= 0;
i
<
var
.
domainSize
();
i
++)
189
str
<<
"\t<OUTCOME>"
<<
var
.
label
(
i
) <<
"</OUTCOME>"
<<
std
::
endl
;
190
191
// //Closing tag
192
str
<<
"</VARIABLE>"
<<
std
::
endl
;
193
194
return
str
.
str
();
195
}
196
197
/*
198
* Returns a bloc defining a variable's CPT in the BIF format.
199
*/
200
template
<
typename
GUM_SCALAR
>
201
INLINE
std
::
string
BIFXMLIDWriter
<
GUM_SCALAR
>::
_variableDefinition_
(
202
const
NodeId
&
varNodeId
,
203
const
InfluenceDiagram
<
GUM_SCALAR
>&
infdiag
) {
204
//<DEFINITION>
205
//<FOR>var</FOR>
206
//<GIVEN>conditional var</GIVEN>
207
//<TABLE>conditional probabilities</TABLE>
208
//</DEFINITION>
209
std
::
stringstream
str
;
210
211
if
(!((
infdiag
.
isDecisionNode
(
varNodeId
)) && (
infdiag
.
parents
(
varNodeId
).
empty
()))) {
212
// Declaration
213
str
<<
"<DEFINITION>"
<<
std
::
endl
;
214
215
// Variable
216
str
<<
"\t<FOR>"
<<
infdiag
.
variable
(
varNodeId
).
name
() <<
"</FOR>"
<<
std
::
endl
;
217
218
// Conditional Parents
219
List
<
std
::
string
>
parentList
;
220
221
for
(
const
auto
par
:
infdiag
.
parents
(
varNodeId
))
222
parentList
.
pushBack
(
infdiag
.
variable
(
par
).
name
());
223
224
for
(
List
<
std
::
string
>::
iterator
parentListIte
=
parentList
.
rbegin
();
225
parentListIte
!=
parentList
.
rend
();
226
--
parentListIte
)
227
str
<<
"\t<GIVEN>"
<< (*
parentListIte
) <<
"</GIVEN>"
<<
std
::
endl
;
228
229
if
(
infdiag
.
isChanceNode
(
varNodeId
)) {
230
Instantiation
inst
(
infdiag
.
cpt
(
varNodeId
));
231
str
<<
"\t<TABLE>"
;
232
233
for
(
inst
.
setFirst
(); !
inst
.
end
();
inst
.
inc
())
234
str
<<
infdiag
.
cpt
(
varNodeId
)[
inst
] <<
" "
;
235
236
str
<<
"</TABLE>"
<<
std
::
endl
;
237
}
else
if
(
infdiag
.
isUtilityNode
(
varNodeId
)) {
238
// Values
239
Instantiation
inst
(
infdiag
.
utility
(
varNodeId
));
240
str
<<
"\t<TABLE>"
;
241
242
for
(
inst
.
setFirst
(); !
inst
.
end
();
inst
.
inc
())
243
str
<<
infdiag
.
utility
(
varNodeId
)[
inst
] <<
" "
;
244
245
str
<<
"</TABLE>"
<<
std
::
endl
;
246
}
247
248
// Closing tag
249
str
<<
"</DEFINITION>"
<<
std
::
endl
;
250
}
251
252
return
str
.
str
();
253
}
254
255
/*
256
* Returns the end of the BIF file.
257
*/
258
template
<
typename
GUM_SCALAR
>
259
INLINE
std
::
string
BIFXMLIDWriter
<
GUM_SCALAR
>::
_documentend_
() {
260
std
::
stringstream
str
;
261
262
str
<<
"</NETWORK>"
<<
std
::
endl
;
263
str
<<
"</BIF>"
<<
std
::
endl
;
264
265
return
str
.
str
();
266
}
267
268
}
/* namespace gum */
269
270
#
endif
// DOXYGEN_SHOULD_SKIP_THIS
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643