aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
BIFWriter_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 #ifndef DOXYGEN_SHOULD_SKIP_THIS
23 
24 # include <agrum/agrum.h>
25 
26 // to ease parsing in IDE
27 # include <agrum/BN/io/BIF/BIFWriter.h>
28 
29 namespace gum {
30 
31  /* =========================================================================*/
32  /* === GUM_BIF_WRITER === */
33  /* =========================================================================*/
34  // Default constructor.
35  template < typename GUM_SCALAR >
36  INLINE BIFWriter< GUM_SCALAR >::BIFWriter() {
38  }
39 
40  // Default destructor.
41  template < typename GUM_SCALAR >
44  }
45 
46  //
47  // Writes a Bayesian network in the output stream using the BIF format.
48  //
49  // @param ouput The output stream.
50  // @param bn The Bayesian network writen in output.
51  // @throws Raised if an I/O error occurs.
52  template < typename GUM_SCALAR >
54  const IBayesNet< GUM_SCALAR >& bn) {
55  if (!output.good()) { GUM_ERROR(IOError, "Stream states flags are not all unset.") }
56 
57  output << _header_(bn) << std::endl;
58 
59  for (const auto node: bn.nodes()) {
61  }
62 
63  for (const auto node: bn.nodes()) {
64  const Potential< GUM_SCALAR >& proba = bn.cpt(node);
66  }
67 
68  output << std::endl;
69 
70  output.flush();
71 
72  if (output.fail()) { GUM_ERROR(IOError, "Writting in the ostream failed.") }
73  }
74 
75  // Writes a Bayesian network in the referenced file using the BIF format.
76  // If the file doesn't exists, it is created.
77  // If the file exists, it's content will be erased.
78  //
79  // @param filePath The path to the file used to write the Bayesian network.
80  // @param bn The Bayesian network writed in the file.
81  // @throws Raised if an I/O error occurs.
82  template < typename GUM_SCALAR >
84  const IBayesNet< GUM_SCALAR >& bn) {
86 
87  if (!output.good()) { GUM_ERROR(IOError, "Stream states flags are not all unset.") }
88 
89  output << _header_(bn) << std::endl;
90 
91  for (const auto node: bn.nodes()) {
93  }
94 
95  for (const auto node: bn.nodes()) {
96  const Potential< GUM_SCALAR >& proba = bn.cpt(node);
98  }
99 
100  output << std::endl;
101 
102  output.flush();
103  output.close();
104 
105  if (output.fail()) { GUM_ERROR(IOError, "Writting in the ostream failed.") }
106  }
107 
108  // Returns a bloc defining a variable's CPT in the BIF format.
109  template < typename GUM_SCALAR >
112  std::string tab = " "; // poor tabulation
113 
114  if (cpt.nbrDim() == 1) {
116  str << "probability (" << cpt.variable(0).name() << ") {" << std::endl;
117  str << tab << "default";
118 
119  for (inst.setFirst(); !inst.end(); ++inst) {
120  str << " " << cpt[inst];
121  }
122 
123  str << ";" << std::endl << "}" << std::endl;
124  } else if (cpt.domainSize() > 1) {
126  Instantiation condVars; // Instantiation on the conditioning variables
128  str << "probability (" << (varsSeq[(Idx)0])->name() << " | ";
129 
130  for (Idx i = 1; i < varsSeq.size() - 1; i++) {
131  str << varsSeq[i]->name() << ", ";
132  condVars << *(varsSeq[i]);
133  }
134 
135  str << varsSeq[varsSeq.size() - 1]->name() << ") {" << std::endl;
136 
137  condVars << *(varsSeq[varsSeq.size() - 1]);
138 
140  str << tab << "(" << _variablesLabels_(varsSeq, inst) << ")";
141  // Writing the probabilities of the variable
142 
144  str << " " << cpt[inst];
145  }
146 
147  str << ";" << std::endl;
148 
150  }
151 
152  str << "}" << std::endl;
153  }
154 
155  return str.str();
156  }
157 
158  // Returns the header of the BIF file.
159  template < typename GUM_SCALAR >
162  std::string tab = " "; // poor tabulation
163  str << "network \"" << bn.propertyWithDefault("name", "unnamedBN") << "\" {" << std::endl;
164  str << "// written by aGrUM " << GUM_VERSION << std::endl;
165  str << "}" << std::endl;
166  return str.str();
167  }
168 
169  // Returns a bloc defining a variable in the BIF format.
170  template < typename GUM_SCALAR >
173  std::string tab = " "; // poor tabulation
174  str << "variable " << var.name() << " {" << std::endl;
175  str << tab << "type discrete[" << var.domainSize() << "] {";
176 
177  for (Idx i = 0; i < var.domainSize() - 1; i++) {
178  str << var.label(i) << ", ";
179  }
180 
181  str << var.label(var.domainSize() - 1) << "};" << std::endl;
182 
183  str << "}" << std::endl;
184  return str.str();
185  }
186 
187  // Returns the modalities labels of the variables in varsSeq
188  template < typename GUM_SCALAR >
189  INLINE std::string
191  const Instantiation& inst) {
193  const DiscreteVariable* varPtr = nullptr;
194 
195  for (Idx i = 1; i < varsSeq.size() - 1; i++) {
196  varPtr = varsSeq[i];
197  str << varPtr->label(inst.val(*varPtr)) << ", ";
198  }
199 
200  varPtr = varsSeq[varsSeq.size() - 1];
201 
202  str << varPtr->label(inst.val(*varPtr));
203  return str.str();
204  }
205 
206 } /* namespace gum */
207 
208 #endif // DOXYGEN_SHOULD_SKIP_THIS
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643