50#ifndef DOXYGEN_SHOULD_SKIP_THIS
61 template < GUM_Numeric GUM_SCALAR >
67 template < GUM_Numeric GUM_SCALAR >
77 template < GUM_Numeric GUM_SCALAR >
79 if (!output.good()) {
GUM_ERROR(IOError,
"Input/Output error : stream not writable.") }
81 output <<
"net " << bn.propertyWithDefault(
"name",
"unnamedBN") << std::endl
84 output <<
"// property softwar aGrUM " << GUM_VERSION << std::endl << std::endl;
86 for (
auto node: bn.topologicalOrder()) {
87 output << _variableBloc_(bn, bn.variable(node));
94 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed.") }
104 template < GUM_Numeric GUM_SCALAR >
107 std::ofstream output(std::filesystem::path{filePath}, std::ios_base::trunc);
109 _doWrite(output, bn);
112 if (output.fail()) {
GUM_ERROR(IOError,
"Writing in the ostream failed.") }
118 template < GUM_Numeric GUM_SCALAR >
122 std::ostringstream oss;
124 id = bn.idFromName(var.name());
126 oss << std::format(
"\tnode {}\n\t{{\n", this->_onlyValidCharsInName(var.name()));
128 oss <<
"\t\tTYPE = CPT;\n";
130 oss <<
"\t\tHEADER =\n\t\t{\n";
131 oss << std::format(
"\t\t\tID = {};\n", this->_onlyValidCharsInName(var.name()));
132 oss << std::format(
"\t\t\tNAME = \"{}\";\n", this->_onlyValidCharsInName(var.name()));
135 oss <<
"\t\tPARENTS = (";
138 for (
Idx i = tmp_vars.size() - 1; i > 0; i--) {
139 if (i < tmp_vars.size() - 1) oss <<
", ";
141 oss << this->_onlyValidCharsInName(tmp_vars[i]->name());
146 oss <<
"\t\tDEFINITION =\n\t\t{\n";
149 oss <<
"\t\t\tNAMESTATES = (";
151 for (
Idx i = 0; i < var.domainSize(); i++) {
152 if (i != 0) oss <<
", ";
154 oss << this->_onlyValidCharsInName(var.label(i));
162 oss <<
"\t\t\tPROBABILITIES = (";
166 for (iter.setFirst(); i < bn.cpt(
id).domainSize(); ++iter, ++i) {
167 if (i != 0) oss <<
", ";
168 oss << bn.cpt(
id)[iter];
182 template < GUM_Numeric GUM_SCALAR >
184 this->_validCharInNamesCheck(bn);
Class representing the minimal interface for Bayesian network with no numerical data.
Writes a IBayesNet in the DSL format.
void _doWrite(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes a Bayesian network in the output stream using the DSL format.
std::string _variableBloc_(const IBayesNet< GUM_SCALAR > &bn, const DiscreteVariable &var)
void _syntacticalCheck(const IBayesNet< GUM_SCALAR > &bn) final
Check whether the BN is syntactically correct for BIF format.
DSLWriter()
Default constructor.
~DSLWriter() override
Destructor.
Base class for discrete random variable.
Class representing the minimal interface for Bayesian network with no numerical data.
Class for assigning/browsing values to tuples of discrete variables.
The generic class for storing (ordered) sequences of objects.
#define GUM_ERROR(type, msg)
Size Idx
Type for indexes.
Size NodeId
Type for node ids.
gum is the global namespace for all aGrUM entities