48#ifndef DOXYGEN_SHOULD_SKIP_THIS
57 template <
typename GUM_SCALAR >
62 template <
typename GUM_SCALAR >
67 template <
typename GUM_SCALAR >
69 return _allowModification_;
72 template <
typename GUM_SCALAR >
74 _allowModification_ = am;
77 template <
typename GUM_SCALAR >
79 _syntacticalCheck(bn);
83 template <
typename GUM_SCALAR >
86 _syntacticalCheck(bn);
87 _doWrite(filePath, bn);
90 template <
typename GUM_SCALAR >
95 template <
typename GUM_SCALAR >
97 if (_allowModification_)
100 for (
const auto& nod: bn.nodes()) {
101 auto& v = bn.variable(nod);
102 std::string valid_n = _buildNameWithOnlyValidChars(v.name());
103 if (v.name() != valid_n)
105 "The variable name '" << v.name() <<
"' contains invalid characters ('" << valid_n
107 for (const auto& lab: v.labels()) {
108 std::string valid_l = _buildNameWithOnlyValidChars(lab);
111 "The variable '" << v <<
"' contains label '" << lab
112 <<
"' with invalid characters ('" << valid_l <<
"').")
117 template <
typename GUM_SCALAR >
119 if (!_allowModification_)
121 return _buildNameWithOnlyValidChars(name);
124 template <
typename GUM_SCALAR >
126 std::string pat =
"[^_a-z0-9]+";
127 std::regex reg(pat, std::regex::icase);
130 std::string out = name;
131 while (std::regex_search(out, sm, reg)) {
132 out = std::regex_replace(out, reg,
"_");
135 if (std::isdigit(out[0]))
137 if (out.find_first_not_of(
"0123456789") != std::string::npos) out =
"_" + out;
virtual class for writing a BN to a file.
void _validCharInNamesCheck(const IBayesNet< GUM_SCALAR > &bn)
}
std::string _onlyValidCharsInName(const std::string &name)
}
void write(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn)
Writes a Bayesian network in the output stream.
BNWriter()
Default constructor.
GUM_NODISCARD bool isModificationAllowed() const
virtual ~BNWriter()
Default destructor.
void setAllowModification(bool am)
virtual void _syntacticalCheck(const IBayesNet< GUM_SCALAR > &bn)
Check whether the BN is syntactically correct for BIF format.
std::string _buildNameWithOnlyValidChars(const std::string &name)
}
Class representing the minimal interface for Bayesian network with no numerical data.
#define GUM_ERROR(type, msg)
gum is the global namespace for all aGrUM entities