51#ifndef DOXYGEN_SHOULD_SKIP_THIS
61 template < GUM_Numeric GUM_SCALAR >
64 Configuration::instance().typed< bool >(
"core",
"allowModificationWhenSaving")) {
65 GUM_CONSTRUCTOR(BNWriter);
68 template < GUM_Numeric GUM_SCALAR >
69 BNWriter< GUM_SCALAR >::~BNWriter() {
70 GUM_DESTRUCTOR(BNWriter);
73 template < GUM_Numeric GUM_SCALAR >
74 bool BNWriter< GUM_SCALAR >::isModificationAllowed()
const {
75 return _allowModification_;
78 template < GUM_Numeric GUM_SCALAR >
79 void BNWriter< GUM_SCALAR >::setAllowModification(
bool am) {
80 _allowModification_ = am;
83 template < GUM_Numeric GUM_SCALAR >
84 void BNWriter< GUM_SCALAR >::write(std::ostream& output, IBayesNet< GUM_SCALAR >& bn) {
85 _syntacticalCheck(bn);
89 template < GUM_Numeric GUM_SCALAR >
90 void BNWriter< GUM_SCALAR >::write(std::string_view filePath, IBayesNet< GUM_SCALAR >& bn) {
91 _syntacticalCheck(bn);
93 _doWrite(filePath, bn);
96 template < GUM_Numeric GUM_SCALAR >
97 void BNWriter< GUM_SCALAR >::_syntacticalCheck(
const IBayesNet< GUM_SCALAR >& bn) {
101 template < GUM_Numeric GUM_SCALAR >
102 void BNWriter< GUM_SCALAR >::_validCharInNamesCheck(
const IBayesNet< GUM_SCALAR >& bn) {
103 if (_allowModification_)
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
113 for (const auto& lab: v.labels()) {
114 std::string valid_l = _buildNameWithOnlyValidChars(lab);
117 "The variable '" << v <<
"' contains label '" << lab
118 <<
"' with invalid characters ('" << valid_l <<
"').")
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};
127 return _buildNameWithOnlyValidChars(name);
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);
136 std::string out{name};
137 while (std::regex_search(out, sm, reg)) {
138 out = std::regex_replace(out, reg,
"_");
141 if (std::isdigit(out[0]))
143 if (out.find_first_not_of(
"0123456789") != std::string::npos) out =
"_" + out;
148 template < GUM_Numeric GUM_SCALAR >
149 std::string BNWriter< GUM_SCALAR >::toString(
const IBayesNet< GUM_SCALAR >& bn) {
150 std::stringstream ss;
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)
gum is the global namespace for all aGrUM entities