aGrUM  0.14.2
O3prmBNWriter_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
21 #ifndef DOXYGEN_SHOULD_SKIP_THIS
22 
24 
25 # define O3PRM_INDENT " "
26 
27 namespace gum {
28  /*
29  * Default constructor.
30  */
31  template < typename GUM_SCALAR >
33  GUM_CONSTRUCTOR(O3prmBNWriter);
34  }
35 
36  /*
37  * Destructor.
38  */
39  template < typename GUM_SCALAR >
41  GUM_DESTRUCTOR(O3prmBNWriter);
42  }
43 
44  /*
45  * Writes a bayes net in the given ouput stream.
46  *
47  * @param output The output stream.
48  * @param bn The bayes net writen in the stream.
49  * @throws IOError Raised if an I/O error occurs.
50  */
51  template < typename GUM_SCALAR >
52  INLINE void
53  O3prmBNWriter< GUM_SCALAR >::write(std::ostream& output,
54  const IBayesNet< GUM_SCALAR >& bn) {
55  if (!output.good()) {
56  GUM_ERROR(IOError, "Stream states flags are not all unset.");
57  }
58 
59  output << "class BayesNet {" << std::endl;
60 
61  for (auto node : bn.nodes()) {
62  output << __extractAttribute(bn, node) << std::endl;
63  }
64 
65  output << "}" << std::endl;
66 
67  output << std::endl;
68 
69  output.flush();
70 
71  if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed."); }
72  }
73 
74  template < typename GUM_SCALAR >
76  const IBayesNet< GUM_SCALAR >& bn, NodeId node) {
77  std::stringstream str;
78  str << O3PRM_INDENT;
79  str << __extractType(bn, node) << " ";
80  str << __extractName(bn, node) << " ";
81  if (bn.parents(node).size() > 0) {
82  str << "dependson " << __extractParents(bn, node) << " ";
83  }
84  str << " {" << __extractCPT(bn, node) << "};" << std::endl;
85  return str.str();
86  }
87 
88  template < typename GUM_SCALAR >
90  const IBayesNet< GUM_SCALAR >& bn, NodeId node) {
91  std::stringstream str;
92  auto var = &(bn.variable(node));
93  for (auto parent : bn.cpt(node).variablesSequence()) {
94  if (var != parent) { str << parent->name() << ", "; }
95  }
96  return str.str().substr(0, str.str().size() - 2);
97  }
98 
99  template < typename GUM_SCALAR >
100  INLINE std::string
101  O3prmBNWriter< GUM_SCALAR >::__extractCPT(const IBayesNet< GUM_SCALAR >& bn,
102  NodeId node) {
103  std::stringstream str;
104  bool first = true;
105  Instantiation inst(bn.cpt(node));
106 
107  str << "[";
108  if (inst.nbrDim() == 1) {
109  // 1D potential
110  for (inst.setFirst(); !inst.end(); inst.inc()) {
111  if (!first) {
112  str << ", ";
113  } else {
114  first = false;
115  }
116  str << bn.cpt(node)[inst];
117  }
118  } else {
119  // (>1)D potential (with parents)
120  Instantiation jnst;
121  for (auto var = inst.variablesSequence().rbegin();
122  var != inst.variablesSequence().rend();
123  --var) {
124  jnst.add(**var);
125  }
126  inst.setFirst();
127  auto currentval = inst.val(0) + 1;
128  for (jnst.setFirst(); !jnst.end(); jnst.inc()) {
129  inst.setVals(jnst);
130  if (!first) {
131  str << ", ";
132  } else {
133  first = false;
134  }
135  if (currentval != inst.val(0)) { // begining line
136  str << std::endl << O3PRM_INDENT << O3PRM_INDENT;
137  currentval = inst.val(0);
138  }
139  str << bn.cpt(node)[inst];
140  }
141  str << std::endl << O3PRM_INDENT;
142  }
143 
144  str << "]";
145  return str.str();
146  }
147 
148  template < typename GUM_SCALAR >
149  INLINE std::string
150  O3prmBNWriter< GUM_SCALAR >::__extractType(const IBayesNet< GUM_SCALAR >& bn,
151  NodeId node) {
152  switch (bn.variable(node).varType()) {
154  auto double_var = dynamic_cast< const DiscretizedVariable< double >* >(
155  &(bn.variable(node)));
156  if (double_var != nullptr) {
157  return __extractDiscretizedType< DiscretizedVariable< double > >(
158  double_var);
159  } else {
160  auto float_var = dynamic_cast< const DiscretizedVariable< float >* >(
161  &(bn.variable(node)));
162  if (float_var != nullptr) {
163  return __extractDiscretizedType< DiscretizedVariable< float > >(
164  float_var);
165  }
166  }
167  GUM_ERROR(InvalidArgument,
168  "DiscretizedVariable ticks are neither doubles or floats");
169  }
170  case gum::VarType::Range: {
171  return __extractRangeType(bn, node);
172  }
173  default: { return __extractLabelizedType(bn, node); }
174  }
175  }
176 
177  template < typename GUM_SCALAR >
179  const IBayesNet< GUM_SCALAR >& bn, NodeId node) {
180  const auto& var = static_cast< const RangeVariable& >(bn.variable(node));
181  std::stringstream str;
182  str << "int (" << var.minVal() << ", " << var.maxVal() << ")";
183  return str.str();
184  }
185 
186  template < typename GUM_SCALAR >
188  const IBayesNet< GUM_SCALAR >& bn, NodeId node) {
189  std::stringstream str;
190  str << "labels(";
191  for (auto l : bn.variable(node).labels()) {
192  str << l << ", ";
193  }
194  return str.str().substr(0, str.str().size() - 2) + ")";
195  }
196 
197  template < typename GUM_SCALAR >
198  template < typename VARTYPE >
199  INLINE std::string
201  std::stringstream str;
202  if (var->ticks().size() >= 3) {
203  str << "real(" << var->ticks()[0];
204  for (size_t i = 1; i < var->ticks().size(); ++i) {
205  str << ", " << var->ticks()[i];
206  }
207  str << ")";
208  return str.str();
209  }
210  GUM_ERROR(InvalidArgument, "discretized variable does not have enough ticks");
211  }
212 
213  template < typename GUM_SCALAR >
214  INLINE std::string
215  O3prmBNWriter< GUM_SCALAR >::__extractName(const IBayesNet< GUM_SCALAR >& bn,
216  NodeId node) {
217  if (!bn.variable(node).name().empty()) {
218  return bn.variable(node).name();
219  } else {
220  std::stringstream str;
221  str << node;
222  return str.str();
223  }
224  }
225 
226  /*
227  * Writes a bayes net in the file referenced by filePath.
228  * If the file doesn't exists, it is created.
229  * If the file exists, it's content will be erased.
230  *
231  * @param filePath The path to the file used to write the bayes net.
232  * @param bn The bayes net writen in the file.
233  * @throw IOError Raised if an I/O error occurs.
234  */
235  template < typename GUM_SCALAR >
236  INLINE void
237  O3prmBNWriter< GUM_SCALAR >::write(const std::string& filePath,
238  const IBayesNet< GUM_SCALAR >& bn) {
239  std::ofstream output(filePath.c_str(), std::ios_base::trunc);
240 
241  write(output, bn);
242 
243  output.close();
244 
245  if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed."); }
246  }
247 
248 } /* namespace gum */
249 
250 #endif // DOXYGEN_SHOULD_SKIP_THIS
std::string __extractType(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Definition file for BIF XML exportation class.
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:97
std::string __extractParents(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
virtual void write(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes an bayes net in the given ouput stream.