aGrUM 3.0.0
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-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/UAI/UAIBNWriter.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(UAIBNWriter);
57 }
58
59 /*
60 * Destructor.
61 */
62 template < GUM_Numeric GUM_SCALAR >
64 GUM_DESTRUCTOR(UAIBNWriter);
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 UAIBNWriter< 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 output << _preambule_(bn) << std::endl;
80
81 for (auto node: bn.nodes())
82 output << _cptBloc_(bn, node) << std::endl;
83
84 output << std::endl;
85
86 output.flush();
87
88 if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed.") }
89 }
90
91 /*
92 * Writes a bayes net in the file referenced by filePath.
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 bayes net.
97 * @param bn The bayes net writen in the file.
98 * @throw IOError Raised if an I/O error occurs.
99 */
100 template < GUM_Numeric GUM_SCALAR >
101 void UAIBNWriter< GUM_SCALAR >::_doWrite(std::string_view filePath,
102 const IBayesNet< GUM_SCALAR >& bn) {
103 std::ofstream output(std::filesystem::path{filePath}, std::ios_base::trunc);
104
105 _doWrite(output, bn);
106
107 output.close();
108 if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed.") }
109 }
110
111 template < GUM_Numeric GUM_SCALAR >
113 std::stringstream str;
114
115 str << "BAYES" << std::endl;
116
117 str << bn.size() << std::endl;
118
119 for (auto node: bn.nodes())
120 str << bn.variable(node).domainSize() << " ";
121 str << std::endl;
122
123 str << bn.size() << std::endl; // number of tensors
124
125 for (auto node: bn.nodes()) {
126 const auto& p = bn.cpt(node);
127 str << p.nbrDim() << " ";
128 // P(X|Y,Z) has to be written "Y Z X". So we need to keep the first var (X)
129 // in order to print it at last
130 NodeId first = 0;
131 bool isFirst = true;
132 for (auto k: p.variablesSequence()) {
133 if (isFirst) {
134 isFirst = false;
135 first = bn.idFromName(k->name());
136 } else {
137 str << bn.idFromName(k->name()) << " ";
138 }
139 }
140 str << std::format("{} # {}\n", first, bn.variable(node).name());
141 }
142 str << std::endl;
143
144 return str.str();
145 }
146
147 template < GUM_Numeric GUM_SCALAR >
149 std::stringstream str;
150
151 const auto& p = bn.cpt(node);
152 str << p.domainSize();
153 Instantiation I(p);
154 for (I.setFirst(); !I.end(); ++I) {
155 if (I.val(0) == 0) str << std::endl << " ";
156 str << p[I] << " ";
157 }
158 str << std::endl;
159
160 return str.str();
161 }
162
163} /* namespace gum */
164
165#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 a 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 a Bayes net in the given output 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:76
Size NodeId
Type for node ids.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46