26 #ifndef DOXYGEN_SHOULD_SKIP_THIS 37 template <
typename GUM_SCALAR >
39 GUM_CONSTRUCTOR(DSLWriter);
43 template <
typename GUM_SCALAR >
45 GUM_DESTRUCTOR(DSLWriter);
53 template <
typename GUM_SCALAR >
55 const IBayesNet< GUM_SCALAR >& bn) {
57 GUM_ERROR(IOError,
"Stream states flags are not all unset.");
60 output <<
"net " << bn.propertyWithDefault(
"name",
"unnamedBN") << std::endl
63 output <<
"// property softwar aGrUM " << GUM_VERSION << std::endl
66 for (
auto node : bn.topologicalOrder()) {
67 output << __variableBloc(bn, bn.variable(node));
74 if (output.fail()) {
GUM_ERROR(IOError,
"Writting in the ostream failed."); }
84 template <
typename GUM_SCALAR >
86 const IBayesNet< GUM_SCALAR >& bn) {
88 fb.open(filePath.c_str(), std::ios::out);
89 std::ostream output(&fb);
99 template <
typename GUM_SCALAR >
102 const DiscreteVariable& var) {
104 std::ostringstream oss;
106 id = bn.idFromName(var.name());
108 oss <<
"\tnode " << var.name() <<
"\n\t{\n";
110 oss <<
"\t\tTYPE = CPT;\n";
112 oss <<
"\t\tHEADER =\n\t\t{\n";
113 oss <<
"\t\t\tID = " << var.name() <<
";\n";
114 oss <<
"\t\t\tNAME = \"" << var.name() <<
"\";\n";
117 oss <<
"\t\tPARENTS = (";
118 const Sequence< const DiscreteVariable* >& tmp_vars =
119 bn.cpt(
id).variablesSequence();
121 for (
Idx i = tmp_vars.size() - 1; i > 0; i--) {
122 if (i < tmp_vars.size() - 1) oss <<
", ";
124 oss << tmp_vars[i]->name();
129 oss <<
"\t\tDEFINITION =\n\t\t{\n";
132 oss <<
"\t\t\tNAMESTATES = (";
134 for (
Idx i = 0; i < var.domainSize(); i++) {
135 if (i != 0) oss <<
", ";
145 oss <<
"\t\t\tPROBABILITIES = (";
148 Instantiation iter(*bn.cpt(
id).content());
149 for (iter.setFirst(); i < bn.cpt(
id).domainSize(); ++iter, ++i) {
150 if (i != 0) oss <<
", ";
151 oss << bn.cpt(
id)[iter];
167 #endif // DOXYGEN_SHOULD_SKIP_THIS ~DSLWriter() final
Destructor.
Class representing Bayesian networks.
gum is the global namespace for all aGrUM entities
void write(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)
DSLWriter()
Default constructor.
Size NodeId
Type for node ids.
#define GUM_ERROR(type, msg)