aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
BIFWriter_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
45# include <agrum/agrum.h>
46
47// to ease parsing in IDE
49
50namespace gum {
51
52 /* =========================================================================*/
53 /* === GUM_BIF_WRITER === */
54 /* =========================================================================*/
55 // Default constructor.
56 template < typename GUM_SCALAR >
58 GUM_CONSTRUCTOR(BIFWriter);
59 }
60
61 // Default destructor.
62 template < typename GUM_SCALAR >
64 GUM_DESTRUCTOR(BIFWriter);
65 }
66
67 //
68 // Writes a Bayesian network in the output stream using the BIF format.
69 //
70 // @param ouput The output stream.
71 // @param bn The Bayesian network writen in output.
72 // @throws Raised if an I/O error occurs.
73 template < typename GUM_SCALAR >
74 INLINE void BIFWriter< GUM_SCALAR >::_doWrite(std::ostream& output,
75 const IBayesNet< GUM_SCALAR >& bn) {
76 if (!output.good()) { GUM_ERROR(IOError, "Input/Output error : stream not writable.") }
77
78 output << _header_(bn) << std::endl;
79
80 for (const auto node: bn.nodes()) {
81 output << _variableBloc_(bn.variable(node)) << std::endl;
82 }
83
84 for (const auto node: bn.nodes()) {
85 const Tensor< GUM_SCALAR >& proba = bn.cpt(node);
86 output << _variableCPT_(proba);
87 }
88
89 output << std::endl;
90
91 output.flush();
92
93 if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed.") }
94 }
95
96 // Writes a Bayesian network in the referenced file using the BIF format.
97 // If the file doesn't exists, it is created.
98 // If the file exists, it's content will be erased.
99 //
100 // @param filePath The path to the file used to write the Bayesian network.
101 // @param bn The Bayesian network writed in the file.
102 // @throws Raised if an I/O error occurs.
103 template < typename GUM_SCALAR >
104 INLINE void BIFWriter< GUM_SCALAR >::_doWrite(const std::string& filePath,
105 const IBayesNet< GUM_SCALAR >& bn) {
106 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
107
108 _doWrite(output, bn);
109
110 output.close();
111 if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed.") }
112 }
113
114 // Returns a bloc defining a variable's CPT in the BIF format.
115 template < typename GUM_SCALAR >
116 INLINE std::string BIFWriter< GUM_SCALAR >::_variableCPT_(const Tensor< GUM_SCALAR >& cpt) {
117 std::stringstream str;
118 std::string tab = " "; // poor tabulation
119
120 if (cpt.nbrDim() == 1) {
121 Instantiation inst(cpt);
122 str << "probability (" << this->_onlyValidCharsInName(cpt.variable(0).name()) << ") {"
123 << std::endl;
124 str << tab << "table";
125
126 for (inst.setFirst(); !inst.end(); ++inst) {
127 str << std::format(" {}", cpt[inst]);
128 }
129
130 str << ";" << std::endl << "}" << std::endl;
131 } else if (cpt.domainSize() > 1) {
132 Instantiation inst(cpt);
133 Instantiation condVars; // Instantiation on the conditioning variables
134 const Sequence< const DiscreteVariable* >& varsSeq = cpt.variablesSequence();
135 str << "probability (" << this->_onlyValidCharsInName((varsSeq[(Idx)0])->name()) << " | ";
136
137 for (Idx i = 1; i < varsSeq.size(); i++) {
138 if (i > 1) str << ", ";
139 str << this->_onlyValidCharsInName(varsSeq[i]->name());
140 condVars << *(varsSeq[i]);
141 }
142 str << ") {" << std::endl;
143
144 for (inst.setFirstIn(condVars); !inst.end(); inst.incIn(condVars)) {
145 str << tab << "(" << _variablesLabels_(varsSeq, inst) << ")";
146 // Writing the probabilities of the variable
147
148 for (inst.setFirstOut(condVars); !inst.end(); inst.incOut(condVars)) {
149 str << std::format(" {}", cpt[inst]);
150 }
151
152 str << ";" << std::endl;
153
154 inst.unsetOverflow();
155 }
156
157 str << "}" << std::endl;
158 }
159
160 return str.str();
161 }
162
163 // Returns the header of the BIF file.
164 template < typename GUM_SCALAR >
165 INLINE std::string BIFWriter< GUM_SCALAR >::_header_(const IBayesNet< GUM_SCALAR >& bn) {
166 std::stringstream str;
167 std::string tab = " "; // poor tabulation
168 str << "network \"" << bn.propertyWithDefault("name", "unnamedBN") << "\" {" << std::endl;
169 str << "// written by aGrUM " << GUM_VERSION << std::endl;
170 str << "}" << std::endl;
171 return str.str();
172 }
173
174 // Returns a bloc defining a variable in the BIF format.
175 template < typename GUM_SCALAR >
176 INLINE std::string BIFWriter< GUM_SCALAR >::_variableBloc_(const DiscreteVariable& var) {
177 std::stringstream str;
178 std::string tab = " "; // poor tabulation
179 str << "variable " << this->_onlyValidCharsInName(var.name()) << " {" << std::endl;
180 str << tab << "type discrete[" << var.domainSize() << "] {";
181
182 for (Idx i = 0; i < var.domainSize(); i++) {
183 if (i > 0) str << ", ";
184 str << this->_onlyValidCharsInName(var.label(i));
185 }
186
187 str << "};" << std::endl;
188
189 str << "}" << std::endl;
190 return str.str();
191 }
192
193 // Returns the modalities labels of the variables in varsSeq
194 template < typename GUM_SCALAR >
195 INLINE std::string
197 const Instantiation& inst) {
198 std::stringstream str;
199 const DiscreteVariable* varPtr = nullptr;
200
201 for (Idx i = 1; i < varsSeq.size(); i++) {
202 if (i > 1) str << ", ";
203 varPtr = varsSeq[i];
204 str << this->_onlyValidCharsInName(varPtr->label(inst.val(*varPtr)));
205 }
206 return str.str();
207 }
208
209 template < typename GUM_SCALAR >
211 this->_validCharInNamesCheck(bn);
212 }
213} /* namespace gum */
214
215#endif // DOXYGEN_SHOULD_SKIP_THIS
Definition of class for BIF file output manipulation.
Writes a IBayesNet in the BIF format.
Definition BIFWriter.h:79
std::string _variablesLabels_(const Sequence< const DiscreteVariable * > &varsSeq, const Instantiation &inst)
BIFWriter()
Default constructor.
std::string _variableCPT_(const Tensor< GUM_SCALAR > &cpt)
std::string _variableBloc_(const DiscreteVariable &var)
void _syntacticalCheck(const IBayesNet< GUM_SCALAR > &bn) final
Check whether the BN is syntactically correct for BIF format.
~BIFWriter() override
Destructor.
void _doWrite(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes a Bayesian network in the output stream using the BIF format.
std::string _header_(const IBayesNet< GUM_SCALAR > &bn)
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.
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