aGrUM 3.3.2
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
50#include <agrum/BN/io/BNWriter.h> // to ease IDE parser
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
53# include <regex>
54
56
57namespace gum {
58 /* =========================================================================*/
59 /* === GUM_BN_WRITER === */
60 /* =========================================================================*/
61 template < GUM_Numeric GUM_SCALAR >
63 _allowModification_(
64 Configuration::instance().typed< bool >("core", "allowModificationWhenSaving")) {
65 GUM_CONSTRUCTOR(BNWriter);
66 }
67
68 template < GUM_Numeric GUM_SCALAR >
69 BNWriter< GUM_SCALAR >::~BNWriter() {
70 GUM_DESTRUCTOR(BNWriter);
71 }
72
73 template < GUM_Numeric GUM_SCALAR >
74 bool BNWriter< GUM_SCALAR >::isModificationAllowed() const {
75 return _allowModification_;
76 }
77
78 template < GUM_Numeric GUM_SCALAR >
79 void BNWriter< GUM_SCALAR >::setAllowModification(bool am) {
80 _allowModification_ = am;
81 }
82
83 template < GUM_Numeric GUM_SCALAR >
84 void BNWriter< GUM_SCALAR >::write(std::ostream& output, IBayesNet< GUM_SCALAR >& bn) {
85 _syntacticalCheck(bn);
86 _doWrite(output, bn);
87 }
88
89 template < GUM_Numeric GUM_SCALAR >
90 void BNWriter< GUM_SCALAR >::write(std::string_view filePath, IBayesNet< GUM_SCALAR >& bn) {
91 _syntacticalCheck(bn);
92 bn.updateMetaData();
93 _doWrite(filePath, bn);
94 }
95
96 template < GUM_Numeric GUM_SCALAR >
97 void BNWriter< GUM_SCALAR >::_syntacticalCheck(const IBayesNet< GUM_SCALAR >& bn) {
98 // no check by default
99 }
100
101 template < GUM_Numeric GUM_SCALAR >
102 void BNWriter< GUM_SCALAR >::_validCharInNamesCheck(const IBayesNet< GUM_SCALAR >& bn) {
103 if (_allowModification_)
104 return; // we do anything if the names will be modified when saved ...
105
106 for (const auto& nod: bn.nodes()) {
107 auto& v = bn.variable(nod);
108 std::string valid_n = _buildNameWithOnlyValidChars(v.name());
109 if (v.name() != valid_n)
111 "The variable name '" << v.name() << "' contains invalid characters ('" << valid_n
112 << "').")
113 for (const auto& lab: v.labels()) {
114 std::string valid_l = _buildNameWithOnlyValidChars(lab);
115 if (lab != valid_l)
117 "The variable '" << v << "' contains label '" << lab
118 << "' with invalid characters ('" << valid_l << "').")
119 }
120 }
121 }
122
123 template < GUM_Numeric GUM_SCALAR >
124 std::string BNWriter< GUM_SCALAR >::_onlyValidCharsInName(std::string_view name) {
125 if (!_allowModification_)
126 return std::string{name}; // we do anything if the names will be modified when saved ...
127 return _buildNameWithOnlyValidChars(name);
128 }
129
130 template < GUM_Numeric GUM_SCALAR >
131 std::string BNWriter< GUM_SCALAR >::_buildNameWithOnlyValidChars(std::string_view name) {
132 std::string pat = "[^_a-z0-9]+";
133 std::regex reg(pat, std::regex::icase);
134 std::smatch sm;
135
136 std::string out{name};
137 while (std::regex_search(out, sm, reg)) {
138 out = std::regex_replace(out, reg, "_");
139 }
140 // first char can not be a digit
141 if (std::isdigit(out[0]))
142 // we allow name containing only an int
143 if (out.find_first_not_of("0123456789") != std::string::npos) out = "_" + out;
144
145 return out;
146 }
147
148 template < GUM_Numeric GUM_SCALAR >
149 std::string BNWriter< GUM_SCALAR >::toString(const IBayesNet< GUM_SCALAR >& bn) {
150 std::stringstream ss;
151 _doWrite(ss, bn);
152 return ss.str();
153 }
154} /* namespace gum */
155
156#endif // DOXYGEN_SHOULD_SKIP_THIS
Definition of abstract classes for file output manipulation of Bayesian networks.
BNWriter()
Default constructor.
Sectioned key/value configuration with typed and documented keys.
Exception : fatal (unknown ?) error.
Typed, documented, sectioned configuration singleton read from an .ini content.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
gum is the global namespace for all aGrUM entities
Definition agrum.h:46