aGrUM  0.16.0
O3prmBNWriter_tpl.h
Go to the documentation of this file.
1 
23 #ifndef DOXYGEN_SHOULD_SKIP_THIS
24 
26 
27 # define O3PRM_INDENT " "
28 
29 namespace gum {
30  /*
31  * Default constructor.
32  */
33  template < typename GUM_SCALAR >
35  GUM_CONSTRUCTOR(O3prmBNWriter);
36  }
37 
38  /*
39  * Destructor.
40  */
41  template < typename GUM_SCALAR >
43  GUM_DESTRUCTOR(O3prmBNWriter);
44  }
45 
46  /*
47  * Writes a bayes net in the given ouput stream.
48  *
49  * @param output The output stream.
50  * @param bn The bayes net writen in the stream.
51  * @throws IOError Raised if an I/O error occurs.
52  */
53  template < typename GUM_SCALAR >
54  INLINE void
55  O3prmBNWriter< GUM_SCALAR >::write(std::ostream& output,
56  const IBayesNet< GUM_SCALAR >& bn) {
57  if (!output.good()) {
58  GUM_ERROR(IOError, "Stream states flags are not all unset.");
59  }
60 
61  output << "class BayesNet {" << std::endl;
62 
63  for (auto node : bn.nodes()) {
64  output << __extractAttribute(bn, node) << std::endl;
65  }
66 
67  output << "}" << std::endl;
68 
69  output << std::endl;
70 
71  output.flush();
72 
73  if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed."); }
74  }
75 
76  template < typename GUM_SCALAR >
78  const IBayesNet< GUM_SCALAR >& bn, NodeId node) {
79  std::stringstream str;
80  str << O3PRM_INDENT;
81  str << __extractType(bn, node) << " ";
82  str << __extractName(bn, node) << " ";
83  if (bn.parents(node).size() > 0) {
84  str << "dependson " << __extractParents(bn, node) << " ";
85  }
86  str << " {" << __extractCPT(bn, node) << "};" << std::endl;
87  return str.str();
88  }
89 
90  template < typename GUM_SCALAR >
92  const IBayesNet< GUM_SCALAR >& bn, NodeId node) {
93  std::stringstream str;
94  auto var = &(bn.variable(node));
95  for (auto parent : bn.cpt(node).variablesSequence()) {
96  if (var != parent) { str << parent->name() << ", "; }
97  }
98  return str.str().substr(0, str.str().size() - 2);
99  }
100 
101  template < typename GUM_SCALAR >
102  INLINE std::string
103  O3prmBNWriter< GUM_SCALAR >::__extractCPT(const IBayesNet< GUM_SCALAR >& bn,
104  NodeId node) {
105  std::stringstream str;
106  bool first = true;
107  Instantiation inst(bn.cpt(node));
108 
109  str << "[";
110  if (inst.nbrDim() == 1) {
111  // 1D potential
112  for (inst.setFirst(); !inst.end(); inst.inc()) {
113  if (!first) {
114  str << ", ";
115  } else {
116  first = false;
117  }
118  str << bn.cpt(node)[inst];
119  }
120  } else {
121  // (>1)D potential (with parents)
122  Instantiation jnst;
123  for (auto var = inst.variablesSequence().rbegin();
124  var != inst.variablesSequence().rend();
125  --var) {
126  jnst.add(**var);
127  }
128  inst.setFirst();
129  auto currentval = inst.val(0) + 1;
130  for (jnst.setFirst(); !jnst.end(); jnst.inc()) {
131  inst.setVals(jnst);
132  if (!first) {
133  str << ", ";
134  } else {
135  first = false;
136  }
137  if (currentval != inst.val(0)) { // begining line
138  str << std::endl << O3PRM_INDENT << O3PRM_INDENT;
139  currentval = inst.val(0);
140  }
141  str << bn.cpt(node)[inst];
142  }
143  str << std::endl << O3PRM_INDENT;
144  }
145 
146  str << "]";
147  return str.str();
148  }
149 
150  template < typename GUM_SCALAR >
151  INLINE std::string
152  O3prmBNWriter< GUM_SCALAR >::__extractType(const IBayesNet< GUM_SCALAR >& bn,
153  NodeId node) {
154  switch (bn.variable(node).varType()) {
156  auto double_var = dynamic_cast< const DiscretizedVariable< double >* >(
157  &(bn.variable(node)));
158  if (double_var != nullptr) {
159  return __extractDiscretizedType< DiscretizedVariable< double > >(
160  double_var);
161  } else {
162  auto float_var = dynamic_cast< const DiscretizedVariable< float >* >(
163  &(bn.variable(node)));
164  if (float_var != nullptr) {
165  return __extractDiscretizedType< DiscretizedVariable< float > >(
166  float_var);
167  }
168  }
169  GUM_ERROR(InvalidArgument,
170  "DiscretizedVariable ticks are neither doubles or floats");
171  }
172  case gum::VarType::Range: {
173  return __extractRangeType(bn, node);
174  }
175  default: {
176  return __extractLabelizedType(bn, node);
177  }
178  }
179  }
180 
181  template < typename GUM_SCALAR >
183  const IBayesNet< GUM_SCALAR >& bn, NodeId node) {
184  const auto& var = static_cast< const RangeVariable& >(bn.variable(node));
185  std::stringstream str;
186  str << "int (" << var.minVal() << ", " << var.maxVal() << ")";
187  return str.str();
188  }
189 
190  template < typename GUM_SCALAR >
192  const IBayesNet< GUM_SCALAR >& bn, NodeId node) {
193  std::stringstream str;
194  str << "labels(";
195  for (auto l : bn.variable(node).labels()) {
196  str << l << ", ";
197  }
198  return str.str().substr(0, str.str().size() - 2) + ")";
199  }
200 
201  template < typename GUM_SCALAR >
202  template < typename VARTYPE >
203  INLINE std::string
205  std::stringstream str;
206  if (var->ticks().size() >= 3) {
207  str << "real(" << var->ticks()[0];
208  for (size_t i = 1; i < var->ticks().size(); ++i) {
209  str << ", " << var->ticks()[i];
210  }
211  str << ")";
212  return str.str();
213  }
214  GUM_ERROR(InvalidArgument, "discretized variable does not have enough ticks");
215  }
216 
217  template < typename GUM_SCALAR >
218  INLINE std::string
219  O3prmBNWriter< GUM_SCALAR >::__extractName(const IBayesNet< GUM_SCALAR >& bn,
220  NodeId node) {
221  if (!bn.variable(node).name().empty()) {
222  return bn.variable(node).name();
223  } else {
224  std::stringstream str;
225  str << node;
226  return str.str();
227  }
228  }
229 
230  /*
231  * Writes a bayes net in the file referenced by filePath.
232  * If the file doesn't exists, it is created.
233  * If the file exists, it's content will be erased.
234  *
235  * @param filePath The path to the file used to write the bayes net.
236  * @param bn The bayes net writen in the file.
237  * @throw IOError Raised if an I/O error occurs.
238  */
239  template < typename GUM_SCALAR >
240  INLINE void
241  O3prmBNWriter< GUM_SCALAR >::write(const std::string& filePath,
242  const IBayesNet< GUM_SCALAR >& bn) {
243  std::ofstream output(filePath.c_str(), std::ios_base::trunc);
244 
245  write(output, bn);
246 
247  output.close();
248 
249  if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed."); }
250  }
251 
252 } /* namespace gum */
253 
254 #endif // DOXYGEN_SHOULD_SKIP_THIS
std::string __extractType(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
std::string __extractCPT(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
O3prmBNWriter()
Default constructor.
std::string __extractRangeType(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
std::string __extractLabelizedType(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
std::string __extractName(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
std::string __extractAttribute(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
std::string __extractDiscretizedType(const VARTYPE *var)
virtual ~O3prmBNWriter()
Destructor.
Size NodeId
Type for node ids.
Definition: graphElements.h:98
std::string __extractParents(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
virtual void write(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes an bayes net in the given ouput stream.