aGrUM  0.13.2
UAIWriter_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 namespace gum {
26 
27  /*
28  * Default constructor.
29  */
30  template < typename GUM_SCALAR >
32  GUM_CONSTRUCTOR(UAIWriter);
33  }
34 
35  /*
36  * Destructor.
37  */
38  template < typename GUM_SCALAR >
40  GUM_DESTRUCTOR(UAIWriter);
41  }
42 
43  /*
44  * Writes a bayes net in the given ouput stream.
45  *
46  * @param output The output stream.
47  * @param bn The bayes net writen in the stream.
48  * @throws IOError Raised if an I/O error occurs.
49  */
50  template < typename GUM_SCALAR >
51  INLINE void UAIWriter< GUM_SCALAR >::write(std::ostream& output,
52  const IBayesNet< GUM_SCALAR >& bn) {
53  if (!output.good()) {
54  GUM_ERROR(IOError, "Stream states flags are not all unset.");
55  }
56 
57  output << __preambule(bn) << std::endl;
58 
59  for (auto node : bn.nodes())
60  output << __cptBloc(bn, node) << std::endl;
61 
62  output << std::endl;
63 
64  output.flush();
65 
66  if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed."); }
67  }
68 
69  /*
70  * Writes a bayes net in the file referenced by filePath.
71  * If the file doesn't exists, it is created.
72  * If the file exists, it's content will be erased.
73  *
74  * @param filePath The path to the file used to write the bayes net.
75  * @param bn The bayes net writen in the file.
76  * @throw IOError Raised if an I/O error occurs.
77  */
78  template < typename GUM_SCALAR >
79  INLINE void UAIWriter< GUM_SCALAR >::write(const std::string& filePath,
80  const IBayesNet< GUM_SCALAR >& bn) {
81  std::ofstream output(filePath.c_str(), std::ios_base::trunc);
82 
83  write(output, bn);
84 
85  output.close();
86 
87  if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed."); }
88  }
89 
90  template < typename GUM_SCALAR >
91  INLINE std::string
92  UAIWriter< GUM_SCALAR >::__preambule(const IBayesNet< GUM_SCALAR >& bn) {
93  std::stringstream str;
94 
95  str << "BAYES" << std::endl;
96 
97  str << bn.size() << std::endl;
98 
99  for (auto node : bn.nodes())
100  str << bn.variable(node).domainSize() << " ";
101  str << std::endl;
102 
103  str << bn.size() << std::endl; // number of potentials
104 
105  for (auto node : bn.nodes()) {
106  const auto& p = bn.cpt(node);
107  str << p.nbrDim() << " ";
108  for (auto k : p.variablesSequence()) {
109  str << bn.idFromName(k->name()) << " ";
110  }
111  str << " # " << bn.variable(node).name() << std::endl;
112  }
113  str << std::endl;
114 
115  return str.str();
116  }
117  template < typename GUM_SCALAR >
118  INLINE std::string
119  UAIWriter< GUM_SCALAR >::__cptBloc(const IBayesNet< GUM_SCALAR >& bn,
120  NodeId node) {
121  std::stringstream str;
122 
123  const auto& p = bn.cpt(node);
124  str << p.domainSize();
125  Instantiation I(p);
126  for (I.setFirst(); !I.end(); ++I) {
127  if (I.val(0) == 0) str << std::endl << " ";
128  str << p[I] << " ";
129  }
130  str << std::endl;
131 
132  return str.str();
133  }
134 
135 } /* namespace gum */
136 
137 #endif // DOXYGEN_SHOULD_SKIP_THIS
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
~UAIWriter() final
Destructor.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
std::string __cptBloc(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
Definition file for BIF XML exportation class.
void write(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes an bayes net in the given ouput stream.
UAIWriter()
Default constructor.
std::string __preambule(const IBayesNet< GUM_SCALAR > &bn)
Returns the header of the BIF file.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66