aGrUM  0.14.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  // P(X|Y,Z) has to be written "Y Z X". So we need to keep the first var (X)
109  // in order to print it at last
110  NodeId first = 0;
111  bool isFirst = true;
112  for (auto k : p.variablesSequence()) {
113  if (isFirst) {
114  isFirst = false;
115  first = bn.idFromName(k->name());
116  } else {
117  str << bn.idFromName(k->name()) << " ";
118  }
119  }
120  str << first << " # " << bn.variable(node).name() << std::endl;
121  }
122  str << std::endl;
123 
124  return str.str();
125  }
126  template < typename GUM_SCALAR >
127  INLINE std::string
128  UAIWriter< GUM_SCALAR >::__cptBloc(const IBayesNet< GUM_SCALAR >& bn,
129  NodeId node) {
130  std::stringstream str;
131 
132  const auto& p = bn.cpt(node);
133  str << p.domainSize();
134  Instantiation I(p);
135  for (I.setFirst(); !I.end(); ++I) {
136  if (I.val(0) == 0) str << std::endl << " ";
137  str << p[I] << " ";
138  }
139  str << std::endl;
140 
141  return str.str();
142  }
143 
144 } /* namespace gum */
145 
146 #endif // DOXYGEN_SHOULD_SKIP_THIS
~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.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52