aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
BIFWriter_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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()) {
56  GUM_ERROR(IOError, "Stream states flags are not all unset.");
57  }
58 
59  output << header__(bn) << std::endl;
60 
61  for (const auto node: bn.nodes()) {
63  }
64 
65  for (const auto node: bn.nodes()) {
66  const Potential< GUM_SCALAR >& proba = bn.cpt(node);
68  }
69 
70  output << std::endl;
71 
72  output.flush();
73 
74  if (output.fail()) { GUM_ERROR(IOError, "Writting in the ostream failed."); }
75  }
76 
77  // Writes a Bayesian network in the referenced file using the BIF format.
78  // If the file doesn't exists, it is created.
79  // If the file exists, it's content will be erased.
80  //
81  // @param filePath The path to the file used to write the Bayesian network.
82  // @param bn The Bayesian network writed in the file.
83  // @throws Raised if an I/O error occurs.
84  template < typename GUM_SCALAR >
86  const IBayesNet< GUM_SCALAR >& bn) {
88 
89  if (!output.good()) {
90  GUM_ERROR(IOError, "Stream states flags are not all unset.");
91  }
92 
93  output << header__(bn) << std::endl;
94 
95  for (const auto node: bn.nodes()) {
97  }
98 
99  for (const auto node: bn.nodes()) {
100  const Potential< GUM_SCALAR >& proba = bn.cpt(node);
102  }
103 
104  output << std::endl;
105 
106  output.flush();
107  output.close();
108 
109  if (output.fail()) { GUM_ERROR(IOError, "Writting in the ostream failed."); }
110  }
111 
112  // Returns a bloc defining a variable's CPT in the BIF format.
113  template < typename GUM_SCALAR >
114  INLINE std::string
117  std::string tab = " "; // poor tabulation
118 
119  if (cpt.nbrDim() == 1) {
121  str << "probability (" << cpt.variable(0).name() << ") {" << std::endl;
122  str << tab << "default";
123 
124  for (inst.setFirst(); !inst.end(); ++inst) {
125  str << " " << cpt[inst];
126  }
127 
128  str << ";" << std::endl << "}" << std::endl;
129  } else if (cpt.domainSize() > 1) {
131  Instantiation condVars; // Instantiation on the conditioning variables
133  str << "probability (" << (varsSeq[(Idx)0])->name() << " | ";
134 
135  for (Idx i = 1; i < varsSeq.size() - 1; i++) {
136  str << varsSeq[i]->name() << ", ";
137  condVars << *(varsSeq[i]);
138  }
139 
140  str << varsSeq[varsSeq.size() - 1]->name() << ") {" << std::endl;
141 
142  condVars << *(varsSeq[varsSeq.size() - 1]);
143 
145  str << tab << "(" << variablesLabels__(varsSeq, inst) << ")";
146  // Writing the probabilities of the variable
147 
149  str << " " << cpt[inst];
150  }
151 
152  str << ";" << std::endl;
153 
155  }
156 
157  str << "}" << std::endl;
158  }
159 
160  return str.str();
161  }
162 
163  // Returns the header of the BIF file.
164  template < typename GUM_SCALAR >
165  INLINE std::string
168  std::string tab = " "; // poor tabulation
169  str << "network \"" << bn.propertyWithDefault("name", "unnamedBN") << "\" {"
170  << std::endl;
171  str << "// written by aGrUM " << GUM_VERSION << std::endl;
172  str << "}" << std::endl;
173  return str.str();
174  }
175 
176  // Returns a bloc defining a variable in the BIF format.
177  template < typename GUM_SCALAR >
178  INLINE std::string
181  std::string tab = " "; // poor tabulation
182  str << "variable " << var.name() << " {" << std::endl;
183  str << tab << "type discrete[" << var.domainSize() << "] {";
184 
185  for (Idx i = 0; i < var.domainSize() - 1; i++) {
186  str << var.label(i) << ", ";
187  }
188 
189  str << var.label(var.domainSize() - 1) << "};" << std::endl;
190 
191  str << "}" << std::endl;
192  return str.str();
193  }
194 
195  // Returns the modalities labels of the variables in varsSeq
196  template < typename GUM_SCALAR >
198  const Sequence< const DiscreteVariable* >& varsSeq,
199  const Instantiation& inst) {
201  const DiscreteVariable* varPtr = nullptr;
202 
203  for (Idx i = 1; i < varsSeq.size() - 1; i++) {
204  varPtr = varsSeq[i];
205  str << varPtr->label(inst.val(*varPtr)) << ", ";
206  }
207 
208  varPtr = varsSeq[varsSeq.size() - 1];
209 
210  str << varPtr->label(inst.val(*varPtr));
211  return str.str();
212  }
213 
214 } /* namespace gum */
215 
216 #endif // DOXYGEN_SHOULD_SKIP_THIS
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669