aGrUM  0.16.0
BIFWriter_tpl.h
Go to the documentation of this file.
1 
23 #ifndef DOXYGEN_SHOULD_SKIP_THIS
24 
25 # include <agrum/agrum.h>
26 
27 // to ease parsing in IDE
29 
30 namespace gum {
31 
32  /* =========================================================================*/
33  /* === GUM_BIF_WRITER === */
34  /* =========================================================================*/
35  // Default constructor.
36  template < typename GUM_SCALAR >
38  GUM_CONSTRUCTOR(BIFWriter);
39  }
40 
41  // Default destructor.
42  template < typename GUM_SCALAR >
44  GUM_DESTRUCTOR(BIFWriter);
45  }
46 
47  //
48  // Writes a Bayesian Network in the output stream using the BIF format.
49  //
50  // @param ouput The output stream.
51  // @param bn The Bayesian Network writen in output.
52  // @throws Raised if an I/O error occurs.
53  template < typename GUM_SCALAR >
54  INLINE void BIFWriter< GUM_SCALAR >::write(std::ostream& output,
55  const IBayesNet< GUM_SCALAR >& bn) {
56  if (!output.good()) {
57  GUM_ERROR(IOError, "Stream states flags are not all unset.");
58  }
59 
60  output << __header(bn) << std::endl;
61 
62  for (const auto node : bn.nodes()) {
63  output << __variableBloc(bn.variable(node)) << std::endl;
64  }
65 
66  for (const auto node : bn.nodes()) {
67  const Potential< GUM_SCALAR >& proba = bn.cpt(node);
68  output << __variableCPT(proba);
69  }
70 
71  output << std::endl;
72 
73  output.flush();
74 
75  if (output.fail()) { GUM_ERROR(IOError, "Writting in the ostream failed."); }
76  }
77 
78  // Writes a Bayesian Network in the referenced file using the BIF format.
79  // If the file doesn't exists, it is created.
80  // If the file exists, it's content will be erased.
81  //
82  // @param filePath The path to the file used to write the Bayesian Network.
83  // @param bn The Bayesian Network writed in the file.
84  // @throws Raised if an I/O error occurs.
85  template < typename GUM_SCALAR >
86  INLINE void BIFWriter< GUM_SCALAR >::write(const std::string& filePath,
87  const IBayesNet< GUM_SCALAR >& bn) {
88  std::ofstream output(filePath.c_str(), std::ios_base::trunc);
89 
90  if (!output.good()) {
91  GUM_ERROR(IOError, "Stream states flags are not all unset.");
92  }
93 
94  output << __header(bn) << std::endl;
95 
96  for (const auto node : bn.nodes()) {
97  output << __variableBloc(bn.variable(node)) << std::endl;
98  }
99 
100  for (const auto node : bn.nodes()) {
101  const Potential< GUM_SCALAR >& proba = bn.cpt(node);
102  output << __variableCPT(proba);
103  }
104 
105  output << std::endl;
106 
107  output.flush();
108  output.close();
109 
110  if (output.fail()) { GUM_ERROR(IOError, "Writting in the ostream failed."); }
111  }
112 
113  // Returns a bloc defining a variable's CPT in the BIF format.
114  template < typename GUM_SCALAR >
115  INLINE std::string
116  BIFWriter< GUM_SCALAR >::__variableCPT(const Potential< GUM_SCALAR >& cpt) {
117  std::stringstream str;
118  std::string tab = " "; // poor tabulation
119 
120  if (cpt.nbrDim() == 1) {
121  Instantiation inst(cpt);
122  str << "probability (" << cpt.variable(0).name() << ") {" << std::endl;
123  str << tab << "default";
124 
125  for (inst.setFirst(); !inst.end(); ++inst) {
126  str << " " << cpt[inst];
127  }
128 
129  str << ";" << std::endl << "}" << std::endl;
130  } else if (cpt.domainSize() > 1) {
131  Instantiation inst(cpt);
132  Instantiation condVars; // Instantiation on the conditioning variables
133  const Sequence< const DiscreteVariable* >& varsSeq = cpt.variablesSequence();
134  str << "probability (" << (varsSeq[(Idx)0])->name() << " | ";
135 
136  for (Idx i = 1; i < varsSeq.size() - 1; i++) {
137  str << varsSeq[i]->name() << ", ";
138  condVars << *(varsSeq[i]);
139  }
140 
141  str << varsSeq[varsSeq.size() - 1]->name() << ") {" << std::endl;
142 
143  condVars << *(varsSeq[varsSeq.size() - 1]);
144 
145  for (inst.setFirstIn(condVars); !inst.end(); inst.incIn(condVars)) {
146  str << tab << "(" << __variablesLabels(varsSeq, inst) << ")";
147  // Writing the probabilities of the variable
148 
149  for (inst.setFirstOut(condVars); !inst.end(); inst.incOut(condVars)) {
150  str << " " << cpt[inst];
151  }
152 
153  str << ";" << std::endl;
154 
155  inst.unsetOverflow();
156  }
157 
158  str << "}" << std::endl;
159  }
160 
161  return str.str();
162  }
163 
164  // Returns the header of the BIF file.
165  template < typename GUM_SCALAR >
166  INLINE std::string
167  BIFWriter< GUM_SCALAR >::__header(const IBayesNet< GUM_SCALAR >& bn) {
168  std::stringstream str;
169  std::string tab = " "; // poor tabulation
170  str << "network \"" << bn.propertyWithDefault("name", "unnamedBN") << "\" {"
171  << std::endl;
172  str << "// written by aGrUM " << GUM_VERSION << std::endl;
173  str << "}" << std::endl;
174  return str.str();
175  }
176 
177  // Returns a bloc defining a variable in the BIF format.
178  template < typename GUM_SCALAR >
179  INLINE std::string
180  BIFWriter< GUM_SCALAR >::__variableBloc(const DiscreteVariable& var) {
181  std::stringstream str;
182  std::string tab = " "; // poor tabulation
183  str << "variable " << var.name() << " {" << std::endl;
184  str << tab << "type discrete[" << var.domainSize() << "] {";
185 
186  for (Idx i = 0; i < var.domainSize() - 1; i++) {
187  str << var.label(i) << ", ";
188  }
189 
190  str << var.label(var.domainSize() - 1) << "};" << std::endl;
191 
192  str << "}" << std::endl;
193  return str.str();
194  }
195 
196  // Returns the modalities labels of the variables in varsSeq
197  template < typename GUM_SCALAR >
199  const Sequence< const DiscreteVariable* >& varsSeq,
200  const Instantiation& inst) {
201  std::stringstream str;
202  const DiscreteVariable* varPtr = nullptr;
203 
204  for (Idx i = 1; i < varsSeq.size() - 1; i++) {
205  varPtr = varsSeq[i];
206  str << varPtr->label(inst.val(*varPtr)) << ", ";
207  }
208 
209  varPtr = varsSeq[varsSeq.size() - 1];
210 
211  str << varPtr->label(inst.val(*varPtr));
212  return str.str();
213  }
214 
215 } /* namespace gum */
216 
217 #endif // DOXYGEN_SHOULD_SKIP_THIS
~BIFWriter() final
Destructor.
std::string __variableBloc(const DiscreteVariable &var)
void write(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes a Bayesian Network in the output stream using the BIF format.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
std::string __variableCPT(const Potential< GUM_SCALAR > &cpt)
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
std::string __variablesLabels(const Sequence< const DiscreteVariable * > &varsSeq, const Instantiation &inst)
std::string __header(const IBayesNet< GUM_SCALAR > &bn)
Size Idx
Type for indexes.
Definition: types.h:53
BIFWriter()
Default constructor.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55