aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
DSLWriter_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
/** @file
23
* @brief Templates implementation of bns/io/gumBNWriter.h classes.
24
*
25
* @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
26
*/
27
#
ifndef
DOXYGEN_SHOULD_SKIP_THIS
28
29
// to ease automatic parser
30
#
include
<
agrum
/
BN
/
IBayesNet
.
h
>
31
#
include
<
agrum
/
BN
/
io
/
DSL
/
DSLWriter
.
h
>
32
33
namespace
gum
{
34
/* =========================================================================*/
35
/* === GUM_DSL_WRITER === */
36
/* =========================================================================*/
37
// Default constructor.
38
template
<
typename
GUM_SCALAR >
39
INLINE DSLWriter<
GUM_SCALAR
>::
DSLWriter
() {
40
GUM_CONSTRUCTOR
(
DSLWriter
);
41
}
42
43
// Default destructor.
44
template
<
typename
GUM_SCALAR
>
45
INLINE
DSLWriter
<
GUM_SCALAR
>::~
DSLWriter
() {
46
GUM_DESTRUCTOR
(
DSLWriter
);
47
}
48
49
/** Writes a Bayesian network in the output stream using the DSL format.
50
* @param ouput The output stream.
51
* @param bn The Bayesian network writen in output.
52
* @throws Raised if an I/O error occurs.
53
*/
54
template
<
typename
GUM_SCALAR
>
55
void
DSLWriter
<
GUM_SCALAR
>::
write
(
std
::
ostream
&
output
,
const
IBayesNet
<
GUM_SCALAR
>&
bn
) {
56
if
(!
output
.
good
()) {
GUM_ERROR
(
IOError
,
"Stream states flags are not all unset."
) }
57
58
output
<<
"net "
<<
bn
.
propertyWithDefault
(
"name"
,
"unnamedBN"
) <<
std
::
endl
59
<<
"{"
<<
std
::
endl
;
60
61
output
<<
"// property softwar aGrUM "
<<
GUM_VERSION
<<
std
::
endl
<<
std
::
endl
;
62
63
for
(
auto
node
:
bn
.
topologicalOrder
()) {
64
output
<<
_variableBloc_
(
bn
,
bn
.
variable
(
node
));
65
}
66
67
output
<<
"};"
;
68
69
output
.
flush
();
70
71
if
(
output
.
fail
()) {
GUM_ERROR
(
IOError
,
"Writting in the ostream failed."
) }
72
}
73
74
/** Writes a Bayesian network in the referenced file using the DSL format.
75
* If the file doesn't exists, it is created.
76
* If the file exists, it's content will be erased.
77
* @param filePath The path to the file used to write the Bayesian network.
78
* @param bn The Bayesian network writed in the file.
79
* @throws Raised if an I/O error occurs.
80
*/
81
template
<
typename
GUM_SCALAR
>
82
void
DSLWriter
<
GUM_SCALAR
>::
write
(
const
std
::
string
&
filePath
,
83
const
IBayesNet
<
GUM_SCALAR
>&
bn
) {
84
std
::
filebuf
fb
;
85
fb
.
open
(
filePath
.
c_str
(),
std
::
ios
::
out
);
86
std
::
ostream
output
(&
fb
);
87
88
write
(
output
,
bn
);
89
90
fb
.
close
();
91
}
92
93
/**
94
* Returns a bloc defining a variable in the DSL format.
95
*/
96
template
<
typename
GUM_SCALAR
>
97
std
::
string
DSLWriter
<
GUM_SCALAR
>::
_variableBloc_
(
const
IBayesNet
<
GUM_SCALAR
>&
bn
,
98
const
DiscreteVariable
&
var
) {
99
NodeId
id
;
100
std
::
ostringstream
oss
;
101
102
id
=
bn
.
idFromName
(
var
.
name
());
103
104
oss
<<
"\tnode "
<<
var
.
name
() <<
"\n\t{\n"
;
105
106
oss
<<
"\t\tTYPE = CPT;\n"
;
107
108
oss
<<
"\t\tHEADER =\n\t\t{\n"
;
109
oss
<<
"\t\t\tID = "
<<
var
.
name
() <<
";\n"
;
110
oss
<<
"\t\t\tNAME = \""
<<
var
.
name
() <<
"\";\n"
;
111
oss
<<
"\t\t};\n"
;
112
113
oss
<<
"\t\tPARENTS = ("
;
114
const
Sequence
<
const
DiscreteVariable
* >&
tmp_vars
=
bn
.
cpt
(
id
).
variablesSequence
();
115
116
for
(
Idx
i
=
tmp_vars
.
size
() - 1;
i
> 0;
i
--) {
117
if
(
i
<
tmp_vars
.
size
() - 1)
oss
<<
", "
;
118
119
oss
<<
tmp_vars
[
i
]->
name
();
120
}
121
122
oss
<<
");\n"
;
123
124
oss
<<
"\t\tDEFINITION =\n\t\t{\n"
;
125
126
////////////////////////////
127
oss
<<
"\t\t\tNAMESTATES = ("
;
128
129
for
(
Idx
i
= 0;
i
<
var
.
domainSize
();
i
++) {
130
if
(
i
!= 0)
oss
<<
", "
;
131
132
oss
<<
var
.
label
(
i
);
133
}
134
135
oss
<<
");\n"
;
136
137
////////////////////////////
138
139
////////////////////////////
140
oss
<<
"\t\t\tPROBABILITIES = ("
;
141
Idx
i
= 0;
142
143
Instantiation
iter
(*
bn
.
cpt
(
id
).
content
());
144
for
(
iter
.
setFirst
();
i
<
bn
.
cpt
(
id
).
domainSize
(); ++
iter
, ++
i
) {
145
if
(
i
!= 0)
oss
<<
", "
;
146
oss
<<
bn
.
cpt
(
id
)[
iter
];
147
}
148
149
oss
<<
");\n"
;
150
151
///////////////////////////
152
153
oss
<<
"\t\t};\n"
;
154
155
oss
<<
"\t};\n\n"
;
156
157
return
oss
.
str
();
158
}
159
160
}
/* namespace gum */
161
162
#
endif
// DOXYGEN_SHOULD_SKIP_THIS
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643