aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
BNWriter_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/BNWriter.h> // to ease IDE parser
50#ifndef DOXYGEN_SHOULD_SKIP_THIS
51
52# include <regex>
53
54namespace gum {
55 /* =========================================================================*/
56 /* === GUM_BN_WRITER === */
57 /* =========================================================================*/
58 template < GUM_Numeric GUM_SCALAR >
60 GUM_CONSTRUCTOR(BNWriter);
61 }
62
63 template < GUM_Numeric GUM_SCALAR >
65 GUM_DESTRUCTOR(BNWriter);
66 }
67
68 template < GUM_Numeric GUM_SCALAR >
70 return _allowModification_;
71 }
72
73 template < GUM_Numeric GUM_SCALAR >
75 _allowModification_ = am;
76 }
77
78 template < GUM_Numeric GUM_SCALAR >
79 void BNWriter< GUM_SCALAR >::write(std::ostream& output, IBayesNet< GUM_SCALAR >& bn) {
80 _syntacticalCheck(bn);
81 _doWrite(output, bn);
82 }
83
84 template < GUM_Numeric GUM_SCALAR >
85 void BNWriter< GUM_SCALAR >::write(std::string_view filePath, IBayesNet< GUM_SCALAR >& bn) {
86 _syntacticalCheck(bn);
87 bn.updateMetaData();
88 _doWrite(filePath, bn);
89 }
90
91 template < GUM_Numeric GUM_SCALAR >
93 // no check by default
94 }
95
96 template < GUM_Numeric GUM_SCALAR >
98 if (_allowModification_)
99 return; // we do anything if the names will be modified when saved ...
100
101 for (const auto& nod: bn.nodes()) {
102 auto& v = bn.variable(nod);
103 std::string valid_n = _buildNameWithOnlyValidChars(v.name());
104 if (v.name() != valid_n)
105 GUM_ERROR(FatalError,
106 "The variable name '" << v.name() << "' contains invalid characters ('" << valid_n
107 << "').")
108 for (const auto& lab: v.labels()) {
109 std::string valid_l = _buildNameWithOnlyValidChars(lab);
110 if (lab != valid_l)
111 GUM_ERROR(FatalError,
112 "The variable '" << v << "' contains label '" << lab
113 << "' with invalid characters ('" << valid_l << "').")
114 }
115 }
116 }
117
118 template < GUM_Numeric GUM_SCALAR >
119 std::string BNWriter< GUM_SCALAR >::_onlyValidCharsInName(std::string_view name) {
120 if (!_allowModification_)
121 return std::string{name}; // we do anything if the names will be modified when saved ...
122 return _buildNameWithOnlyValidChars(name);
123 }
124
125 template < GUM_Numeric GUM_SCALAR >
126 std::string BNWriter< GUM_SCALAR >::_buildNameWithOnlyValidChars(std::string_view name) {
127 std::string pat = "[^_a-z0-9]+";
128 std::regex reg(pat, std::regex::icase);
129 std::smatch sm;
130
131 std::string out{name};
132 while (std::regex_search(out, sm, reg)) {
133 out = std::regex_replace(out, reg, "_");
134 }
135 // first char can not be a digit
136 if (std::isdigit(out[0]))
137 // we allow name containing only an int
138 if (out.find_first_not_of("0123456789") != std::string::npos) out = "_" + out;
139
140 return out;
141 }
142
143 template < GUM_Numeric GUM_SCALAR >
145 std::stringstream ss;
146 _doWrite(ss, bn);
147 return ss.str();
148 }
149} /* namespace gum */
150
151#endif // DOXYGEN_SHOULD_SKIP_THIS
Definition of abstract classes for file output manipulation of Bayesian networks.
virtual class for writing a BN to a file.
Definition BNWriter.h:79
void _validCharInNamesCheck(const IBayesNet< GUM_SCALAR > &bn)
}
void write(std::ostream &output, IBayesNet< GUM_SCALAR > &bn)
Writes a Bayesian network in the output stream.
std::string toString(const IBayesNet< GUM_SCALAR > &bn)
BNWriter()
Default constructor.
GUM_NODISCARD bool isModificationAllowed() const
virtual ~BNWriter()
Default destructor.
std::string _buildNameWithOnlyValidChars(std::string_view name)
}
void setAllowModification(bool am)
virtual void _syntacticalCheck(const IBayesNet< GUM_SCALAR > &bn)
Check whether the BN is syntactically correct for BIF format.
std::string _onlyValidCharsInName(std::string_view name)
}
Class representing the minimal interface for Bayesian network with no numerical data.
Definition IBayesNet.h:75
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
gum is the global namespace for all aGrUM entities
Definition agrum.h:46