aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
DSLWriter_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
49#include <agrum/BN/io/DSL/DSLWriter.h> // to ease IDE parser
50#ifndef DOXYGEN_SHOULD_SKIP_THIS
51
52// to ease automatic parser
53# include <agrum/BN/IBayesNet.h>
55
56namespace gum {
57 /* =========================================================================*/
58 /* === GUM_DSL_WRITER === */
59 /* =========================================================================*/
60 // Default constructor.
61 template < GUM_Numeric GUM_SCALAR >
63 GUM_CONSTRUCTOR(DSLWriter);
64 }
65
66 // Default destructor.
67 template < GUM_Numeric GUM_SCALAR >
69 GUM_DESTRUCTOR(DSLWriter);
70 }
71
77 template < GUM_Numeric GUM_SCALAR >
78 void DSLWriter< GUM_SCALAR >::_doWrite(std::ostream& output, const IBayesNet< GUM_SCALAR >& bn) {
79 if (!output.good()) { GUM_ERROR(IOError, "Input/Output error : stream not writable.") }
80
81 output << "net " << bn.propertyWithDefault("name", "unnamedBN") << std::endl
82 << "{" << std::endl;
83
84 output << "// property softwar aGrUM " << GUM_VERSION << std::endl << std::endl;
85
86 for (auto node: bn.topologicalOrder()) {
87 output << _variableBloc_(bn, bn.variable(node));
88 }
89
90 output << "};";
91
92 output.flush();
93
94 if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed.") }
95 }
96
104 template < GUM_Numeric GUM_SCALAR >
105 void DSLWriter< GUM_SCALAR >::_doWrite(std::string_view filePath,
106 const IBayesNet< GUM_SCALAR >& bn) {
107 std::ofstream output(std::filesystem::path{filePath}, std::ios_base::trunc);
108
109 _doWrite(output, bn);
110
111 output.close();
112 if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed.") }
113 }
114
118 template < GUM_Numeric GUM_SCALAR >
120 const DiscreteVariable& var) {
121 NodeId id;
122 std::ostringstream oss;
123
124 id = bn.idFromName(var.name());
125
126 oss << std::format("\tnode {}\n\t{{\n", this->_onlyValidCharsInName(var.name()));
127
128 oss << "\t\tTYPE = CPT;\n";
129
130 oss << "\t\tHEADER =\n\t\t{\n";
131 oss << std::format("\t\t\tID = {};\n", this->_onlyValidCharsInName(var.name()));
132 oss << std::format("\t\t\tNAME = \"{}\";\n", this->_onlyValidCharsInName(var.name()));
133 oss << "\t\t};\n";
134
135 oss << "\t\tPARENTS = (";
136 const Sequence< const DiscreteVariable* >& tmp_vars = bn.cpt(id).variablesSequence();
137
138 for (Idx i = tmp_vars.size() - 1; i > 0; i--) {
139 if (i < tmp_vars.size() - 1) oss << ", ";
140
141 oss << this->_onlyValidCharsInName(tmp_vars[i]->name());
142 }
143
144 oss << ");\n";
145
146 oss << "\t\tDEFINITION =\n\t\t{\n";
147
149 oss << "\t\t\tNAMESTATES = (";
150
151 for (Idx i = 0; i < var.domainSize(); i++) {
152 if (i != 0) oss << ", ";
153
154 oss << this->_onlyValidCharsInName(var.label(i));
155 }
156
157 oss << ");\n";
158
160
162 oss << "\t\t\tPROBABILITIES = (";
163 Idx i = 0;
164
165 Instantiation iter(*bn.cpt(id).content());
166 for (iter.setFirst(); i < bn.cpt(id).domainSize(); ++iter, ++i) {
167 if (i != 0) oss << ", ";
168 oss << bn.cpt(id)[iter];
169 }
170
171 oss << ");\n";
172
174
175 oss << "\t\t};\n";
176
177 oss << "\t};\n\n";
178
179 return oss.str();
180 }
181
182 template < GUM_Numeric GUM_SCALAR >
184 this->_validCharInNamesCheck(bn);
185 }
186} /* namespace gum */
187
188#endif // DOXYGEN_SHOULD_SKIP_THIS
Class representing the minimal interface for Bayesian network with no numerical data.
Writes a IBayesNet in the DSL format.
Definition DSLWriter.h:69
void _doWrite(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes a Bayesian network in the output stream using the DSL format.
std::string _variableBloc_(const IBayesNet< GUM_SCALAR > &bn, const DiscreteVariable &var)
void _syntacticalCheck(const IBayesNet< GUM_SCALAR > &bn) final
Check whether the BN is syntactically correct for BIF format.
DSLWriter()
Default constructor.
~DSLWriter() override
Destructor.
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:994
#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