aGrUM 2.3.2
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-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
48#ifndef DOXYGEN_SHOULD_SKIP_THIS
49
50// to ease automatic parser
51# include <agrum/BN/IBayesNet.h>
53
54namespace gum {
55 /* =========================================================================*/
56 /* === GUM_DSL_WRITER === */
57 /* =========================================================================*/
58 // Default constructor.
59 template < typename GUM_SCALAR >
61 GUM_CONSTRUCTOR(DSLWriter);
62 }
63
64 // Default destructor.
65 template < typename GUM_SCALAR >
67 GUM_DESTRUCTOR(DSLWriter);
68 }
69
75 template < typename GUM_SCALAR >
76 void DSLWriter< GUM_SCALAR >::_doWrite(std::ostream& output, const IBayesNet< GUM_SCALAR >& bn) {
77 if (!output.good()) { GUM_ERROR(IOError, "Input/Output error : stream not writable.") }
78
79 output << "net " << bn.propertyWithDefault("name", "unnamedBN") << std::endl
80 << "{" << std::endl;
81
82 output << "// property softwar aGrUM " << GUM_VERSION << std::endl << std::endl;
83
84 for (auto node: bn.topologicalOrder()) {
85 output << _variableBloc_(bn, bn.variable(node));
86 }
87
88 output << "};";
89
90 output.flush();
91
92 if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed.") }
93 }
94
102 template < typename GUM_SCALAR >
103 void DSLWriter< GUM_SCALAR >::_doWrite(const std::string& filePath,
104 const IBayesNet< GUM_SCALAR >& bn) {
105 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
106
107 _doWrite(output, bn);
108
109 output.close();
110 if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed.") }
111 }
112
116 template < typename GUM_SCALAR >
118 const DiscreteVariable& var) {
119 NodeId id;
120 std::ostringstream oss;
121
122 id = bn.idFromName(var.name());
123
124 oss << "\tnode " << this->_onlyValidCharsInName(var.name()) << "\n\t{\n";
125
126 oss << "\t\tTYPE = CPT;\n";
127
128 oss << "\t\tHEADER =\n\t\t{\n";
129 oss << "\t\t\tID = " << this->_onlyValidCharsInName(var.name()) << ";\n";
130 oss << "\t\t\tNAME = \"" << this->_onlyValidCharsInName(var.name()) << "\";\n";
131 oss << "\t\t};\n";
132
133 oss << "\t\tPARENTS = (";
134 const Sequence< const DiscreteVariable* >& tmp_vars = bn.cpt(id).variablesSequence();
135
136 for (Idx i = tmp_vars.size() - 1; i > 0; i--) {
137 if (i < tmp_vars.size() - 1) oss << ", ";
138
139 oss << this->_onlyValidCharsInName(tmp_vars[i]->name());
140 }
141
142 oss << ");\n";
143
144 oss << "\t\tDEFINITION =\n\t\t{\n";
145
147 oss << "\t\t\tNAMESTATES = (";
148
149 for (Idx i = 0; i < var.domainSize(); i++) {
150 if (i != 0) oss << ", ";
151
152 oss << this->_onlyValidCharsInName(var.label(i));
153 }
154
155 oss << ");\n";
156
158
160 oss << "\t\t\tPROBABILITIES = (";
161 Idx i = 0;
162
163 Instantiation iter(*bn.cpt(id).content());
164 for (iter.setFirst(); i < bn.cpt(id).domainSize(); ++iter, ++i) {
165 if (i != 0) oss << ", ";
166 oss << bn.cpt(id)[iter];
167 }
168
169 oss << ");\n";
170
172
173 oss << "\t\t};\n";
174
175 oss << "\t};\n\n";
176
177 return oss.str();
178 }
179
180 template < typename GUM_SCALAR >
182 this->_validCharInNamesCheck(bn);
183 }
184} /* namespace gum */
185
186#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:972
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
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