50#ifndef DOXYGEN_SHOULD_SKIP_THIS
58 template < GUM_Numeric GUM_SCALAR >
63 template < GUM_Numeric GUM_SCALAR >
68 template < GUM_Numeric GUM_SCALAR >
70 return _allowModification_;
73 template < GUM_Numeric GUM_SCALAR >
75 _allowModification_ = am;
78 template < GUM_Numeric GUM_SCALAR >
80 _syntacticalCheck(bn);
84 template < GUM_Numeric GUM_SCALAR >
86 _syntacticalCheck(bn);
88 _doWrite(filePath, bn);
91 template < GUM_Numeric GUM_SCALAR >
96 template < GUM_Numeric GUM_SCALAR >
98 if (_allowModification_)
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)
106 "The variable name '" << v.name() <<
"' contains invalid characters ('" << valid_n
108 for (const auto& lab: v.labels()) {
109 std::string valid_l = _buildNameWithOnlyValidChars(lab);
112 "The variable '" << v <<
"' contains label '" << lab
113 <<
"' with invalid characters ('" << valid_l <<
"').")
118 template < GUM_Numeric GUM_SCALAR >
120 if (!_allowModification_)
121 return std::string{name};
122 return _buildNameWithOnlyValidChars(name);
125 template < GUM_Numeric GUM_SCALAR >
127 std::string pat =
"[^_a-z0-9]+";
128 std::regex reg(pat, std::regex::icase);
131 std::string out{name};
132 while (std::regex_search(out, sm, reg)) {
133 out = std::regex_replace(out, reg,
"_");
136 if (std::isdigit(out[0]))
138 if (out.find_first_not_of(
"0123456789") != std::string::npos) out =
"_" + out;
143 template < GUM_Numeric GUM_SCALAR >
145 std::stringstream ss;
Definition of abstract classes for file output manipulation of Bayesian networks.
virtual class for writing a BN to a file.
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.
#define GUM_ERROR(type, msg)
gum is the global namespace for all aGrUM entities