aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
netWriter_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#pragma once
43#ifndef DOXYGEN_SHOULD_SKIP_THIS
44
45# include <fstream>
46
47// to ease parsing in IDE
49
50namespace gum {
51 /* =========================================================================*/
52 /* === GUM_BN_WRITER === */
53 /* =========================================================================*/
54 // Default constructor.
55 template < typename GUM_SCALAR >
57 GUM_CONSTRUCTOR(NetWriter);
58 }
59
60 // Default destructor.
61 template < typename GUM_SCALAR >
63 GUM_DESTRUCTOR(NetWriter);
64 }
65
66 //
67 // Writes a Bayesian network in the output stream using the BN format.
68 //
69 // @param ouput The output stream.
70 // @param bn The Bayesian network writen in output.
71 // @throws Raised if an I/O error occurs.
72 template < typename GUM_SCALAR >
73 INLINE void NetWriter< 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 output << _header_(bn) << std::endl;
78
79 for (auto node: bn.nodes())
80 output << _variableBloc_(bn.variable(node)) << std::endl;
81
82 for (auto node: bn.nodes())
83 output << _variableCPT_(bn.cpt(node));
84
85 output << std::endl;
86
87 output.flush();
88
89 if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed.") }
90 }
91
92 // Writes a Bayesian network in the referenced file using the BN format.
93 // If the file doesn't exists, it is created.
94 // If the file exists, it's content will be erased.
95 //
96 // @param filePath The path to the file used to write the Bayesian network.
97 // @param bn The Bayesian network writed in the file.
98 // @throws Raised if an I/O error occurs.
99 template < typename GUM_SCALAR >
100 INLINE void NetWriter< GUM_SCALAR >::_doWrite(const std::string& filePath,
101 const IBayesNet< GUM_SCALAR >& bn) {
102 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
103
104 _doWrite(output, bn);
105
106 output.close();
107 if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed.") }
108 }
109
110 // Returns a bloc defining a variable's CPT in the BN format.
111 template < typename GUM_SCALAR >
112 INLINE std::string NetWriter< GUM_SCALAR >::_variableCPT_(const Tensor< GUM_SCALAR >& cpt) {
113 std::stringstream str;
114 std::string tab = " "; // poor tabulation
115
116 Instantiation inst(cpt);
117 if (cpt.nbrDim() == 1) {
118 str << "potential (" << cpt.variable(0).name() << ") {" << std::endl << tab << "data = ( ";
119
120 for (inst.setFirst(); !inst.end(); ++inst) {
121 str << std::format(" {}", cpt[inst]);
122 ;
123 }
124
125 str << ");";
126 } else { // cpt.domainSize() > 1
127 const Sequence< const DiscreteVariable* >& varsSeq = cpt.variablesSequence();
128
129 Instantiation conds;
130 for (Idx i = 1; i < varsSeq.size(); i++)
131 conds.add(*varsSeq[varsSeq.size() - i]);
132
133 str << "potential ( " << (varsSeq[static_cast< Idx >(0)])->name() << " | ";
134 for (Idx i = 1; i < varsSeq.size(); i++)
135 str << varsSeq[i]->name() << " ";
136 str << ") {" << std::endl << tab << "data = \n";
137
138 std::string comment;
139 conds.setFirst();
140 while (true) {
141 str << tab << "(";
142 for (Idx i = 0; i < conds.nbrDim(); i++) {
143 if (conds.val(i) != 0) break;
144 str << "(";
145 }
146
147 inst.setVals(conds);
148 for (inst.setFirstVar(*varsSeq[0]); !inst.end(); inst.incVar(*varsSeq[0]))
149 str << std::format(" {}{}", tab, cpt[inst]);
150
151 comment = tab + "% ";
152 for (Idx i = 0; i < conds.nbrDim(); i++) {
153 comment += conds.variable(i).name() + "=" + conds.variable(i).label(conds.val(i)) + tab;
154 }
155
156 ++conds;
157 if (conds.end()) {
158 for (Idx i = 0; i < inst.nbrDim(); i++) {
159 str << ")";
160 }
161 str << ";" << comment;
162 break;
163 } else {
164 for (Idx i = 0; i < conds.nbrDim(); i++) {
165 str << ")";
166 if (conds.val(i) != 0) break;
167 }
168 str << comment << "\n";
169 }
170 }
171 }
172 str << "\n}\n" << std::endl;
173 return str.str();
174 }
175
176 // Returns the header of the BN file.
177 template < typename GUM_SCALAR >
178 INLINE std::string NetWriter< GUM_SCALAR >::_header_(const IBayesNet< GUM_SCALAR >& bn) {
179 std::stringstream str;
180 std::string tab = " "; // poor tabulation
181 str << std::endl << "net {" << std::endl;
182 str << " name = " << bn.propertyWithDefault("name", "unnamedBN") << ";" << std::endl;
183 str << " software = \"aGrUM " << GUM_VERSION << "\";" << std::endl;
184 str << " node_size = (50 50);" << std::endl;
185 str << "}" << std::endl;
186 return str.str();
187 }
188
189 // Returns a bloc defining a variable in the BN format.
190 template < typename GUM_SCALAR >
191 INLINE std::string NetWriter< GUM_SCALAR >::_variableBloc_(const DiscreteVariable& var) {
192 std::stringstream str;
193 std::string tab = " "; // poor tabulation
194 str << "node " << var.name() << " {" << std::endl;
195 str << tab << "states = (";
196
197 for (Idx i = 0; i < var.domainSize(); i++) {
198 str << var.label(i) << " ";
199 }
200
201 str << ");" << std::endl;
202 str << tab << "label = \"" << var.name() << "\";" << std::endl;
203 str << tab << "ID = \"" << var.name() << "\";" << std::endl;
204
205 str << "}" << std::endl;
206
207 return str.str();
208 }
209} /* namespace gum */
210
211#endif // DOXYGEN_SHOULD_SKIP_THIS
Base class for discrete random variable.
Class representing the minimal interface for Bayesian network with no numerical data.
Definition IBayesNet.h:75
Class for assigning/browsing values to tuples of discrete variables.
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
Writes a IBayesNet in the BN format.
Definition netWriter.h:76
static std::string _header_(const IBayesNet< GUM_SCALAR > &bn)
NetWriter()
Default constructor.
std::string _variableCPT_(const Tensor< GUM_SCALAR > &cpt)
void _doWrite(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes a Bayesian network in the output stream using the BN format.
~NetWriter() override
Destructor.
std::string _variableBloc_(const DiscreteVariable &var)
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:972
aGrUM's Tensor is a multi-dimensional array with tensor operators.
Definition tensor.h:85
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
Size Idx
Type for indexes.
Definition types.h:79
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Definition of class for BN file output manipulation.