aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
UAIBNWriter_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(UAIBNWriter);
55 }
56
57 /*
58 * Destructor.
59 */
60 template < typename GUM_SCALAR >
62 GUM_DESTRUCTOR(UAIBNWriter);
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 UAIBNWriter< 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 << _preambule_(bn) << std::endl;
78
79 for (auto node: bn.nodes())
80 output << _cptBloc_(bn, node) << std::endl;
81
82 output << std::endl;
83
84 output.flush();
85
86 if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed.") }
87 }
88
89 /*
90 * Writes a bayes net in the file referenced by filePath.
91 * If the file doesn't exists, it is created.
92 * If the file exists, it's content will be erased.
93 *
94 * @param filePath The path to the file used to write the bayes net.
95 * @param bn The bayes net writen in the file.
96 * @throw IOError Raised if an I/O error occurs.
97 */
98 template < typename GUM_SCALAR >
99 INLINE void UAIBNWriter< GUM_SCALAR >::_doWrite(const std::string& filePath,
100 const IBayesNet< GUM_SCALAR >& bn) {
101 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
102
103 _doWrite(output, bn);
104
105 output.close();
106 if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed.") }
107 }
108
109 template < typename GUM_SCALAR >
111 std::stringstream str;
112
113 str << "BAYES" << std::endl;
114
115 str << bn.size() << std::endl;
116
117 for (auto node: bn.nodes())
118 str << bn.variable(node).domainSize() << " ";
119 str << std::endl;
120
121 str << bn.size() << std::endl; // number of tensors
122
123 for (auto node: bn.nodes()) {
124 const auto& p = bn.cpt(node);
125 str << p.nbrDim() << " ";
126 // P(X|Y,Z) has to be written "Y Z X". So we need to keep the first var (X)
127 // in order to print it at last
128 NodeId first = 0;
129 bool isFirst = true;
130 for (auto k: p.variablesSequence()) {
131 if (isFirst) {
132 isFirst = false;
133 first = bn.idFromName(k->name());
134 } else {
135 str << bn.idFromName(k->name()) << " ";
136 }
137 }
138 str << first << " # " << bn.variable(node).name() << std::endl;
139 }
140 str << std::endl;
141
142 return str.str();
143 }
144
145 template < typename GUM_SCALAR >
147 NodeId node) {
148 std::stringstream str;
149
150 const auto& p = bn.cpt(node);
151 str << p.domainSize();
152 Instantiation I(p);
153 for (I.setFirst(); !I.end(); ++I) {
154 if (I.val(0) == 0) str << std::endl << " ";
155 str << p[I] << " ";
156 }
157 str << std::endl;
158
159 return str.str();
160 }
161
162} /* namespace gum */
163
164#endif // DOXYGEN_SHOULD_SKIP_THIS
Definition file for UAI exportation class.
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.
Writes an bayes net in a text file with UAI format.
Definition UAIBNWriter.h:74
UAIBNWriter()
Default constructor.
std::string _cptBloc_(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
void _doWrite(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes an bayes net in the given ouput stream.
~UAIBNWriter() override
Destructor.
std::string _preambule_(const IBayesNet< GUM_SCALAR > &bn)
Returns the header of the BIF file.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
Size NodeId
Type for node ids.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46