aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
XDSLBNWriter_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40#pragma once
41
42
43#ifndef DOXYGEN_SHOULD_SKIP_THIS
44
46
47namespace gum {
48
49 /*
50 * Default constructor.
51 */
52 template < typename GUM_SCALAR >
54 GUM_CONSTRUCTOR(XDSLBNWriter);
55 }
56
57 /*
58 * Destructor.
59 */
60 template < typename GUM_SCALAR >
62 GUM_DESTRUCTOR(XDSLBNWriter);
63 }
64
65 /*
66 * Writes a bayes net in the given ouput stream.
67 *
68 * @param output The output stream.
69 * @param bn The bayes net writen in the stream.
70 * @throws IOError Raised if an I/O error occurs.
71 */
72 template < typename GUM_SCALAR >
73 INLINE void XDSLBNWriter< GUM_SCALAR >::_doWrite(std::ostream& output,
74 const IBayesNet< GUM_SCALAR >& bn) {
75 if (!output.good()) { GUM_ERROR(IOError, "Input/Output error : stream not writable.") }
76
77 _heading_(output, bn);
78 output << std::endl;
79
80 // heading variables
81 output << " <nodes>" << std::endl;
82 for (auto node: bn.topologicalOrder())
83 _variableBloc_(output, node, bn);
84 output << " </nodes>" << std::endl;
85 output << std::endl;
86
87 _headingExtension_(output, bn);
88 for (auto node: bn.topologicalOrder())
89 _variableExtension_(output, node, bn);
90 _endExtension_(output);
91
92 output << std::endl;
93
94 _documentEnd_(output);
95
96 output.flush();
97
98 if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed.") }
99 }
100
101 /*
102 * Writes a bayes net in the file referenced by filePath.
103 * If the file doesn't exist, it is created.
104 * If the file exists, it's content will be erased.
105 *
106 * @param filePath The path to the file used to write the bayes net.
107 * @param bn The bayes net writen in the file.
108 * @throw IOError Raised if an I/O error occurs.
109 */
110 template < typename GUM_SCALAR >
111 INLINE void XDSLBNWriter< GUM_SCALAR >::_doWrite(const std::string& filePath,
112 const IBayesNet< GUM_SCALAR >& bn) {
113 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
114
115 _doWrite(output, bn);
116
117 output.close();
118 if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed.") }
119 }
120
121 /*
122 * Send to output the header of the XDSL file.
123 */
124 template < typename GUM_SCALAR >
125 INLINE void XDSLBNWriter< GUM_SCALAR >::_heading_(std::ostream& output,
126 const IBayesNet< GUM_SCALAR >& bn) {
127 // Header for every xml
128 output << "<?xml version=\"1.0\" ?>" << std::endl;
129 output << "<!-- This network was created by pyAgrum " << GUM_VERSION
130 << " for any purpose you want -->" << std::endl;
131 // Network declaration
132 output << "<smile version=\"1.0\" id=\"" << bn.propertyWithDefault("name", "unnamedBN") << "\">"
133 << std::endl;
134 }
135
136 /*
137 * Send to output the header of the extension section
138 */
139 template < typename GUM_SCALAR >
140 INLINE void XDSLBNWriter< GUM_SCALAR >::_headingExtension_(std::ostream& output,
141 const IBayesNet< GUM_SCALAR >& bn) {
142 // Header for every xml
143 output << " <extensions>" << std::endl;
144 output << " <genie version=\"1.0\" app=\"aGrUM " << GUM_VERSION << "\" name=\""
145 << bn.propertyWithDefault("name", "unnamedBN") << "\">" << std::endl;
146 }
147
148 /*
149 * Send to output a bloc defining a variable in the XDSL format.
150 */
151 template < typename GUM_SCALAR >
152 INLINE void XDSLBNWriter< GUM_SCALAR >::_variableBloc_(std::ostream& output,
153 const NodeId& varNodeId,
154 const IBayesNet< GUM_SCALAR >& bn) {
155 //<cpt id="Org_arrangements">
156 // <state id="LOW" />
157 // <state id="HIGH" />
158 // <parents>Business_planning</parents>
159 // <probabilities>0.6909999999999999 0.3090000000000001 0.426
160 // 0.5740000000000001</probabilities>
161 //</cpt>
162 const auto& var = bn.variable(varNodeId);
163
164 output << " <cpt id=\"" << this->_buildNameWithOnlyValidChars(var.name()) << "\">"
165 << std::endl;
166
167 // labels
168 for (const auto& lab: var.labels()) {
169 output << " <state id=\"" << lab << "\" />" << std::endl;
170 }
171
172 bool first;
173
174 // parents
175 const auto& cpt = bn.cpt(varNodeId);
176 const auto nd = cpt.nbrDim();
177 if (nd > 1) {
178 output << " <parents>";
179 first = true;
180 for (Idx i = 1; i < nd; i++) {
181 if (first) first = false;
182 else output << " ";
183
184 output << this->_buildNameWithOnlyValidChars(cpt.variable(i).name());
185 }
186 output << "</parents>" << std::endl;
187 }
188
189 output << " <probabilities>";
190 gum::Instantiation I(cpt);
191 I.setFirst();
192 first = true;
193 while (!I.end()) {
194 if (first) first = false;
195 else output << " ";
196 output << cpt[I];
197 I.inc();
198 }
199 output << "</probabilities>" << std::endl;
200
201 // //Closing tag
202 output << " </cpt>" << std::endl;
203 }
204
205 /*
206 * Send to output a bloc defining a variable's CPT in the XDSL format.
207 */
208 template < typename GUM_SCALAR >
209 INLINE void XDSLBNWriter< GUM_SCALAR >::_variableExtension_(std::ostream& output,
210 const NodeId& varNodeId,
211 const IBayesNet< GUM_SCALAR >& bn) {
212 //<node id="Vendor_support">
213 // <name>Vendor support</name>
214 //</node>
215 const std::string& name = bn.variable(varNodeId).name();
216 output << " <node id=\"" << this->_buildNameWithOnlyValidChars(name) << "\">" << std::endl;
217 output << " <name>" << name << "</name>" << std::endl;
218 output << " </node>" << std::endl;
219 }
220
221 /*
222 * Send to output the end of the XDSL file.
223 */
224 template < typename GUM_SCALAR >
225 INLINE void XDSLBNWriter< GUM_SCALAR >::_endExtension_(std::ostream& output) {
226 output << " </genie>" << std::endl;
227 output << " </extensions>" << std::endl;
228 }
229
230 /*
231 * Send to output the end of the XDSL file.
232 */
233 template < typename GUM_SCALAR >
234 INLINE void XDSLBNWriter< GUM_SCALAR >::_documentEnd_(std::ostream& output) {
235 output << "</smile>" << std::endl;
236 }
237
238} /* namespace gum */
239
240#endif // DOXYGEN_SHOULD_SKIP_THIS
Definition file for XDSL XML exportation class.
Class representing the minimal interface for Bayesian network with no numerical data.
Definition IBayesNet.h:75
<agrum/BN/io/XDSLXML/XDSLBNWriter.h>
void _endExtension_(std::ostream &output)
void _documentEnd_(std::ostream &output)
send to output the end of the XDSL file.
XDSLBNWriter()
Default constructor.
void _variableBloc_(std::ostream &output, const NodeId &varNodeId, const IBayesNet< GUM_SCALAR > &bn)
send to output a bloc defining a variable in the XDSL format.
void _variableExtension_(std::ostream &output, const NodeId &varNodeId, const IBayesNet< GUM_SCALAR > &bn)
send to output a bloc defining a variable's extensoion in the XDSL
void _heading_(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn)
send to output the header of the XDSL file.
void _doWrite(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes an bayes net in the given ouput stream.
void _headingExtension_(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn)
~XDSLBNWriter() override
Destructor.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
Size Idx
Type for indexes.
Definition types.h:79
Size NodeId
Type for node ids.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46