aGrUM 3.1.1
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-2026 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-2026 *
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
41#pragma once
42
43
44#include <agrum/BN/io/XDSL/XDSLBNWriter.h> // to ease IDE parser
45#ifndef DOXYGEN_SHOULD_SKIP_THIS
46
48
49namespace gum {
50
51 /*
52 * Default constructor.
53 */
54 template < GUM_Numeric GUM_SCALAR >
56 GUM_CONSTRUCTOR(XDSLBNWriter);
57 }
58
59 /*
60 * Destructor.
61 */
62 template < GUM_Numeric GUM_SCALAR >
64 GUM_DESTRUCTOR(XDSLBNWriter);
65 }
66
67 /*
68 * Writes a bayes net in the given ouput stream.
69 *
70 * @param output The output stream.
71 * @param bn The bayes net writen in the stream.
72 * @throws IOError Raised if an I/O error occurs.
73 */
74 template < GUM_Numeric GUM_SCALAR >
75 void XDSLBNWriter< GUM_SCALAR >::_doWrite(std::ostream& output,
76 const IBayesNet< GUM_SCALAR >& bn) {
77 if (!output.good()) { GUM_ERROR(IOError, "Input/Output error : stream not writable.") }
78
79 _heading_(output, bn);
80 output << std::endl;
81
82 // heading variables
83 output << " <nodes>" << std::endl;
84 for (auto node: bn.topologicalOrder())
85 _variableBloc_(output, node, bn);
86 output << " </nodes>" << std::endl;
87 output << std::endl;
88
89 _headingExtension_(output, bn);
90 for (auto node: bn.topologicalOrder())
91 _variableExtension_(output, node, bn);
92 _endExtension_(output);
93
94 output << std::endl;
95
96 _documentEnd_(output);
97
98 output.flush();
99
100 if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed.") }
101 }
102
103 /*
104 * Writes a bayes net in the file referenced by filePath.
105 * If the file doesn't exist, it is created.
106 * If the file exists, it's content will be erased.
107 *
108 * @param filePath The path to the file used to write the bayes net.
109 * @param bn The bayes net writen in the file.
110 * @throw IOError Raised if an I/O error occurs.
111 */
112 template < GUM_Numeric GUM_SCALAR >
113 void XDSLBNWriter< GUM_SCALAR >::_doWrite(std::string_view filePath,
114 const IBayesNet< GUM_SCALAR >& bn) {
115 std::ofstream output(std::filesystem::path{filePath}, std::ios_base::trunc);
116
117 _doWrite(output, bn);
118
119 output.close();
120 if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed.") }
121 }
122
123 /*
124 * Send to output the header of the XDSL file.
125 */
126 template < GUM_Numeric GUM_SCALAR >
127 void XDSLBNWriter< GUM_SCALAR >::_heading_(std::ostream& output,
128 const IBayesNet< GUM_SCALAR >& bn) {
129 // Header for every xml
130 output << "<?xml version=\"1.0\" ?>" << std::endl;
131 output << "<!-- This network was created by pyAgrum " << GUM_VERSION
132 << " for any purpose you want -->" << std::endl;
133 // Network declaration
134 output << "<smile version=\"1.0\" id=\"" << bn.propertyWithDefault("name", "unnamedBN") << "\">"
135 << std::endl;
136 }
137
138 /*
139 * Send to output the header of the extension section
140 */
141 template < GUM_Numeric GUM_SCALAR >
142 void XDSLBNWriter< GUM_SCALAR >::_headingExtension_(std::ostream& output,
143 const IBayesNet< GUM_SCALAR >& bn) {
144 // Header for every xml
145 output << " <extensions>" << std::endl;
146 output << " <genie version=\"1.0\" app=\"aGrUM " << GUM_VERSION << "\" name=\""
147 << bn.propertyWithDefault("name", "unnamedBN") << "\">" << std::endl;
148 }
149
150 /*
151 * Send to output a bloc defining a variable in the XDSL format.
152 */
153 template < GUM_Numeric GUM_SCALAR >
154 void XDSLBNWriter< GUM_SCALAR >::_variableBloc_(std::ostream& output,
155 const NodeId& varNodeId,
156 const IBayesNet< GUM_SCALAR >& bn) {
157 //<cpt id="Org_arrangements">
158 // <state id="LOW" />
159 // <state id="HIGH" />
160 // <parents>Business_planning</parents>
161 // <probabilities>0.6909999999999999 0.3090000000000001 0.426
162 // 0.5740000000000001</probabilities>
163 //</cpt>
164 const auto& var = bn.variable(varNodeId);
165
166 output << " <cpt id=\"" << this->_buildNameWithOnlyValidChars(var.name()) << "\">"
167 << std::endl;
168
169 // labels
170 for (const auto& lab: var.labels()) {
171 output << " <state id=\"" << lab << "\" />" << std::endl;
172 }
173
174 bool first;
175
176 // parents
177 const auto& cpt = bn.cpt(varNodeId);
178 const auto nd = cpt.nbrDim();
179 if (nd > 1) {
180 output << " <parents>";
181 first = true;
182 for (Idx i = 1; i < nd; i++) {
183 if (first) first = false;
184 else output << " ";
185
186 output << this->_buildNameWithOnlyValidChars(cpt.variable(i).name());
187 }
188 output << "</parents>" << std::endl;
189 }
190
191 output << " <probabilities>";
192 gum::Instantiation I(cpt);
193 I.setFirst();
194 first = true;
195 while (!I.end()) {
196 if (first) first = false;
197 else output << " ";
198 output << cpt[I];
199 I.inc();
200 }
201 output << "</probabilities>" << std::endl;
202
203 // //Closing tag
204 output << " </cpt>" << std::endl;
205 }
206
207 /*
208 * Send to output a bloc defining a variable's CPT in the XDSL format.
209 */
210 template < GUM_Numeric GUM_SCALAR >
211 void XDSLBNWriter< GUM_SCALAR >::_variableExtension_(std::ostream& output,
212 const NodeId& varNodeId,
213 const IBayesNet< GUM_SCALAR >& bn) {
214 //<node id="Vendor_support">
215 // <name>Vendor support</name>
216 //</node>
217 const std::string& name = bn.variable(varNodeId).name();
218 output << " <node id=\"" << this->_buildNameWithOnlyValidChars(name) << "\">" << std::endl;
219 output << " <name>" << name << "</name>" << std::endl;
220 output << " </node>" << std::endl;
221 }
222
223 /*
224 * Send to output the end of the XDSL file.
225 */
226 template < GUM_Numeric GUM_SCALAR >
227 void XDSLBNWriter< GUM_SCALAR >::_endExtension_(std::ostream& output) {
228 output << " </genie>" << std::endl;
229 output << " </extensions>" << std::endl;
230 }
231
232 /*
233 * Send to output the end of the XDSL file.
234 */
235 template < GUM_Numeric GUM_SCALAR >
236 void XDSLBNWriter< GUM_SCALAR >::_documentEnd_(std::ostream& output) {
237 output << "</smile>" << std::endl;
238 }
239
240} /* namespace gum */
241
242#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 a Bayes net in the given output stream.
void _headingExtension_(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn)
~XDSLBNWriter() override
Destructor.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
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