aGrUM  0.16.0
DSLWriter_tpl.h
Go to the documentation of this file.
1 
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 
30 // to ease automatic parser
31 # include <agrum/BN/IBayesNet.h>
33 
34 namespace gum {
35  /* =========================================================================*/
36  /* === GUM_DSL_WRITTER === */
37  /* =========================================================================*/
38  // Default constructor.
39  template < typename GUM_SCALAR >
41  GUM_CONSTRUCTOR(DSLWriter);
42  }
43 
44  // Default destructor.
45  template < typename GUM_SCALAR >
47  GUM_DESTRUCTOR(DSLWriter);
48  }
49 
55  template < typename GUM_SCALAR >
56  void DSLWriter< GUM_SCALAR >::write(std::ostream& output,
57  const IBayesNet< GUM_SCALAR >& bn) {
58  if (!output.good()) {
59  GUM_ERROR(IOError, "Stream states flags are not all unset.");
60  }
61 
62  output << "net " << bn.propertyWithDefault("name", "unnamedBN") << std::endl
63  << "{" << std::endl;
64 
65  output << "// property softwar aGrUM " << GUM_VERSION << std::endl
66  << std::endl;
67 
68  for (auto node : bn.topologicalOrder()) {
69  output << __variableBloc(bn, bn.variable(node));
70  }
71 
72  output << "};";
73 
74  output.flush();
75 
76  if (output.fail()) { GUM_ERROR(IOError, "Writting in the ostream failed."); }
77  }
78 
86  template < typename GUM_SCALAR >
87  void DSLWriter< GUM_SCALAR >::write(const std::string& filePath,
88  const IBayesNet< GUM_SCALAR >& bn) {
89  std::filebuf fb;
90  fb.open(filePath.c_str(), std::ios::out);
91  std::ostream output(&fb);
92 
93  write(output, bn);
94 
95  fb.close();
96  }
97 
101  template < typename GUM_SCALAR >
102  std::string
103  DSLWriter< GUM_SCALAR >::__variableBloc(const IBayesNet< GUM_SCALAR >& bn,
104  const DiscreteVariable& var) {
105  NodeId id;
106  std::ostringstream oss;
107 
108  id = bn.idFromName(var.name());
109 
110  oss << "\tnode " << var.name() << "\n\t{\n";
111 
112  oss << "\t\tTYPE = CPT;\n";
113 
114  oss << "\t\tHEADER =\n\t\t{\n";
115  oss << "\t\t\tID = " << var.name() << ";\n";
116  oss << "\t\t\tNAME = \"" << var.name() << "\";\n";
117  oss << "\t\t};\n";
118 
119  oss << "\t\tPARENTS = (";
120  const Sequence< const DiscreteVariable* >& tmp_vars =
121  bn.cpt(id).variablesSequence();
122 
123  for (Idx i = tmp_vars.size() - 1; i > 0; i--) {
124  if (i < tmp_vars.size() - 1) oss << ", ";
125 
126  oss << tmp_vars[i]->name();
127  }
128 
129  oss << ");\n";
130 
131  oss << "\t\tDEFINITION =\n\t\t{\n";
132 
134  oss << "\t\t\tNAMESTATES = (";
135 
136  for (Idx i = 0; i < var.domainSize(); i++) {
137  if (i != 0) oss << ", ";
138 
139  oss << var.label(i);
140  }
141 
142  oss << ");\n";
143 
145 
147  oss << "\t\t\tPROBABILITIES = (";
148  Idx i = 0;
149 
150  Instantiation iter(*bn.cpt(id).content());
151  for (iter.setFirst(); i < bn.cpt(id).domainSize(); ++iter, ++i) {
152  if (i != 0) oss << ", ";
153  oss << bn.cpt(id)[iter];
154  }
155 
156  oss << ");\n";
157 
159 
160  oss << "\t\t};\n";
161 
162  oss << "\t};\n\n";
163 
164  return oss.str();
165  }
166 
167 } /* namespace gum */
168 
169 #endif // DOXYGEN_SHOULD_SKIP_THIS
~DSLWriter() final
Destructor.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
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.
Definition: graphElements.h:98
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55