aGrUM  0.13.2
netWriter_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 
23 // to ease parsing in IDE
25 
26 namespace gum {
27  /* =========================================================================*/
28  /* === GUM_BN_WRITER === */
29  /* =========================================================================*/
30  // Default constructor.
31  template < typename GUM_SCALAR >
33  GUM_CONSTRUCTOR(NetWriter);
34  }
35 
36  // Default destructor.
37  template < typename GUM_SCALAR >
39  GUM_DESTRUCTOR(NetWriter);
40  }
41 
42  //
43  // Writes a Bayesian Network in the output stream using the BN format.
44  //
45  // @param ouput The output stream.
46  // @param bn The Bayesian Network writen in output.
47  // @throws Raised if an I/O error occurs.
48  template < typename GUM_SCALAR >
49  INLINE void NetWriter< GUM_SCALAR >::write(std::ostream& output,
50  const IBayesNet< GUM_SCALAR >& bn) {
51  if (!output.good())
52  GUM_ERROR(IOError, "Stream states flags are not all unset.");
53 
54  output << __header(bn) << std::endl;
55 
56  for (auto node : bn.nodes())
57  output << __variableBloc(bn.variable(node)) << std::endl;
58 
59  for (auto node : bn.nodes())
60  output << __variableCPT(bn.cpt(node));
61 
62  output << std::endl;
63 
64  output.flush();
65 
66  if (output.fail()) { GUM_ERROR(IOError, "Writting in the ostream failed."); }
67  }
68 
69  // Writes a Bayesian Network in the referenced file using the BN format.
70  // If the file doesn't exists, it is created.
71  // If the file exists, it's content will be erased.
72  //
73  // @param filePath The path to the file used to write the Bayesian Network.
74  // @param bn The Bayesian Network writed in the file.
75  // @throws Raised if an I/O error occurs.
76  template < typename GUM_SCALAR >
77  INLINE void NetWriter< GUM_SCALAR >::write(const std::string& filePath,
78  const IBayesNet< GUM_SCALAR >& bn) {
79  std::ofstream output(filePath.c_str(), std::ios_base::trunc);
80 
81  if (!output.good()) {
82  GUM_ERROR(IOError, "Stream states flags are not all unset.");
83  }
84 
85  output << __header(bn) << std::endl;
86 
87  for (auto node : bn.nodes())
88  output << __variableBloc(bn.variable(node)) << std::endl;
89 
90  for (auto node : bn.nodes())
91  output << __variableCPT(bn.cpt(node));
92 
93  output << std::endl;
94 
95  output.flush();
96  output.close();
97 
98  if (output.fail()) { GUM_ERROR(IOError, "Writting in the ostream failed."); }
99  }
100 
101  // Returns a bloc defining a variable's CPT in the BN format.
102  template < typename GUM_SCALAR >
103  INLINE std::string
104  NetWriter< GUM_SCALAR >::__variableCPT(const Potential< GUM_SCALAR >& cpt) {
105  std::stringstream str;
106  std::string tab = " "; // poor tabulation
107 
108  Instantiation inst(cpt);
109  if (cpt.nbrDim() == 1) {
110  str << "potential (" << cpt.variable(0).name() << ") {" << std::endl
111  << tab << "data = ( ";
112 
113  for (inst.setFirst(); !inst.end(); ++inst) {
114  str << " " << cpt[inst];
115  }
116 
117  str << ");";
118  } else { // cpt.domainSize() > 1
119  const Sequence< const DiscreteVariable* >& varsSeq = cpt.variablesSequence();
120 
121  Instantiation conds;
122  for (Idx i = 1; i < varsSeq.size(); i++)
123  conds.add(*varsSeq[i]);
124 
125  str << "potential ( " << (varsSeq[(Idx)0])->name() << " | ";
126  for (Idx i = 1; i < varsSeq.size(); i++)
127  str << varsSeq[i]->name() << " ";
128  str << ") {" << std::endl << tab << "data = \n";
129 
130  std::string comment;
131  conds.setFirst();
132  while (true) {
133  str << tab << "(";
134  for (Idx i = 0; i < conds.nbrDim(); i++) {
135  if (conds.val(i) != 0) break;
136  str << "(";
137  }
138 
139  inst.setVals(conds);
140  for (inst.setFirstVar(*varsSeq[0]); !inst.end(); inst.incVar(*varsSeq[0]))
141  str << tab << cpt[inst];
142 
143  comment = tab + "% ";
144  for (Idx i = 0; i < conds.nbrDim(); i++) {
145  comment += conds.variable(i).name() + "="
146  + conds.variable(i).label(conds.val(i)) + tab;
147  }
148 
149  ++conds;
150  if (conds.end()) {
151  for (Idx i = 0; i < inst.nbrDim(); i++) {
152  str << ")";
153  }
154  str << ";" << comment;
155  break;
156  } else {
157  for (Idx i = 0; i < conds.nbrDim(); i++) {
158  str << ")";
159  if (conds.val(i) != 0) break;
160  }
161  str << comment << "\n";
162  }
163  }
164  }
165  str << "\n}\n" << std::endl;
166  return str.str();
167  }
168 
169  // Returns the header of the BN file.
170  template < typename GUM_SCALAR >
171  INLINE std::string
172  NetWriter< GUM_SCALAR >::__header(const IBayesNet< GUM_SCALAR >& bn) {
173  std::stringstream str;
174  std::string tab = " "; // poor tabulation
175  str << std::endl << "net {" << std::endl;
176  str << " name = " << bn.propertyWithDefault("name", "unnamedBN") << ";"
177  << std::endl;
178  str << " software = \"aGrUM " << GUM_VERSION << "\";" << std::endl;
179  str << " node_size = (50 50);" << std::endl;
180  str << "}" << std::endl;
181  return str.str();
182  }
183 
184  // Returns a bloc defining a variable in the BN format.
185  template < typename GUM_SCALAR >
186  INLINE std::string
187  NetWriter< GUM_SCALAR >::__variableBloc(const DiscreteVariable& var) {
188  std::stringstream str;
189  std::string tab = " "; // poor tabulation
190  str << "node " << var.name() << " {" << std::endl;
191  str << tab << "states = (";
192 
193  for (Idx i = 0; i < var.domainSize(); i++) {
194  str << var.label(i) << " ";
195  }
196 
197  str << ");" << std::endl;
198  str << tab << "label = \"" << var.name() << "\";" << std::endl;
199  str << tab << "ID = \"" << var.name() << "\";" << std::endl;
200 
201  str << "}" << std::endl;
202 
203  return str.str();
204  }
205 } /* namespace gum */
206 
207 #endif // DOXYGEN_SHOULD_SKIP_THIS
NetWriter()
Default constructor.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
std::string __header(const IBayesNet< GUM_SCALAR > &bn)
void write(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes a Bayesian Network in the output stream using the BN format.
~NetWriter() final
Destructor.
std::string __variableCPT(const Potential< GUM_SCALAR > &cpt)
Definition of classe for BN file output manipulation.
std::string __variableBloc(const DiscreteVariable &var)
unsigned long Idx
Type for indexes.
Definition: types.h:43
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66