aGrUM  0.14.2
BIFXMLBNWriter_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(BIFXMLBNWriter);
33  }
34 
35  /*
36  * Destructor.
37  */
38  template < typename GUM_SCALAR >
40  GUM_DESTRUCTOR(BIFXMLBNWriter);
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
52  BIFXMLBNWriter< GUM_SCALAR >::write(std::ostream& output,
53  const IBayesNet< GUM_SCALAR >& bn) {
54  if (!output.good()) {
55  GUM_ERROR(IOError, "Stream states flags are not all unset.");
56  }
57 
58  output << __heading(bn) << std::endl;
59 
60  output << "<!-- Variables -->" << std::endl;
61 
62  for (auto node : bn.nodes())
63  output << __variableBloc(bn.variable(node)) << std::endl;
64 
65  output << "<!-- Probability distributions -->" << std::endl;
66 
67  for (auto node : bn.nodes())
68  output << __variableDefinition(node, bn);
69 
70  output << std::endl;
71 
72  output << __documentend();
73 
74  output.flush();
75 
76  if (output.fail()) { GUM_ERROR(IOError, "Writting in the ostream failed."); }
77  }
78 
79  /*
80  * Writes a bayes net in the file referenced by filePath.
81  * If the file doesn't exists, it is created.
82  * If the file exists, it's content will be erased.
83  *
84  * @param filePath The path to the file used to write the bayes net.
85  * @param bn The bayes net writen in the file.
86  * @throw IOError Raised if an I/O error occurs.
87  */
88  template < typename GUM_SCALAR >
89  INLINE void
90  BIFXMLBNWriter< GUM_SCALAR >::write(const std::string& filePath,
91  const IBayesNet< GUM_SCALAR >& bn) {
92  std::ofstream output(filePath.c_str(), std::ios_base::trunc);
93 
94  write(output, bn);
95 
96  output.close();
97 
98  if (output.fail()) { GUM_ERROR(IOError, "Writting in the ostream failed."); }
99  }
100 
101  /*
102  * Returns the header of the BIF file.
103  */
104  template < typename GUM_SCALAR >
105  INLINE std::string
106  BIFXMLBNWriter< GUM_SCALAR >::__heading(const IBayesNet< GUM_SCALAR >& bn) {
107  std::stringstream str;
108 
109  // Header for every xml
110  str << "<?xml version=\"1.0\" ?>" << std::endl;
111 
112  // Document type definition of BIF 0.3
113  /*str << "<!-- DTD for the XMLBIF 0.3 format -->" << std::endl;
114  str << "<!DOCTYPE BIF [" << std::endl;
115  str << "\t<!ELEMENT BIF ( NETWORK )*>" << std::endl;
116  str << "\t\t<!ATTLIST BIF VERSION CDATA #REQUIRED>" << std::endl;
117  str << "\t<!ELEMENT NETWORK ( NAME, ( PROPERTY | VARIABLE | DEFINITION )*
118  )>" <<
119  std::endl;
120  str << "\t<!ELEMENT NAME (#PCDATA)>" << std::endl;
121  str << "\t<!ELEMENT VARIABLE ( NAME, ( OUTCOME | PROPERTY )* ) >" <<
122  std::endl;
123  str << "\t\t<!ATTLIST VARIABLE TYPE (nature|decision|utility) \"nature\">"
124  <<
125  std::endl;
126  str << "\t<!ELEMENT OUTCOME (#PCDATA)>" << std::endl;
127  str << "\t<!ELEMENT DEFINITION ( FOR | GIVEN | TABLE | PROPERTY )* >" <<
128  std::endl;
129  str << "\t<!ELEMENT FOR (#PCDATA)>" << std::endl;
130  str << "\t<!ELEMENT GIVEN (#PCDATA)>" << std::endl;
131  str << "\t<!ELEMENT TABLE (#PCDATA)>" << std::endl;
132  str << "\t<!ELEMENT PROPERTY (#PCDATA)>" << std::endl;
133  str << "]>" << std::endl;*/
134 
135  // BIF version Tag
136  str << std::endl << "<BIF VERSION=\"0.3\">" << std::endl;
137 
138  // Network declaration
139  str << "<NETWORK>" << std::endl;
140  str << "<NAME>" << bn.propertyWithDefault("name", "unnamedBN") << "</NAME>"
141  << std::endl;
142  str << "<PROPERTY>software aGrUM</PROPERTY>" << std::endl;
143 
144  return str.str();
145  }
146 
147  /*
148  * Returns a bloc defining a variable in the BIF format.
149  */
150  template < typename GUM_SCALAR >
151  INLINE std::string
152  BIFXMLBNWriter< GUM_SCALAR >::__variableBloc(const DiscreteVariable& var) {
153  //<VARIABLE TYPE="nature|decision|utility">
154  //<NAME>name</NAME>
155  //<OUTCOME>outcome1</OUTCOME>
156  //<OUTCOME>outcome2</OUTCOME>
157  //<PROPERTY>property</PROPERTY>
158  //</VARIABLE>
159 
160  std::stringstream str;
161 
162  // Declaration of variable and his type
163  str << "<VARIABLE TYPE=\"nature\">" << std::endl;
164 
165  // Name and description
166  str << "\t<NAME>" << var.name() << "</NAME>" << std::endl;
167  str << "\t<PROPERTY>" << var.description() << "</PROPERTY>" << std::endl;
168 
169  // Outcomes
170  for (Idx i = 0; i < var.domainSize(); i++)
171  str << "\t<OUTCOME>" << var.label(i) << "</OUTCOME>" << std::endl;
172 
173  // //Closing tag
174  str << "</VARIABLE>" << std::endl;
175 
176  return str.str();
177  }
178 
179  /*
180  * Returns a bloc defining a variable's CPT in the BIF format.
181  */
182  template < typename GUM_SCALAR >
184  const NodeId& varNodeId, const IBayesNet< GUM_SCALAR >& bn) {
185  //<DEFINITION>
186  //<FOR>var</FOR>
187  //<GIVEN>conditional var</GIVEN>
188  //<TABLE>conditional probabilities</TABLE>
189  //</DEFINITION>
190  std::stringstream str;
191 
192  // Declaration
193  str << "<DEFINITION>" << std::endl;
194 
195  // Variable
196  str << "\t<FOR>" << bn.variable(varNodeId).name() << "</FOR>" << std::endl;
197 
198  // Table
199  // For historical reason, the code is not the same betwen bIXML for BN and
200  // for ID
201  // ...
202  const Potential< GUM_SCALAR >& cpt = bn.cpt(varNodeId);
203 
204  // Conditional Parents
205  for (Idx i = 1; i < cpt.nbrDim(); i++)
206  str << "\t<GIVEN>" << cpt.variable(i).name() << "</GIVEN>" << std::endl;
207 
208  Instantiation inst;
209  inst << cpt.variable(0);
210 
211  for (Idx i = cpt.nbrDim() - 1; i > 0; i--)
212  inst << cpt.variable(i);
213 
214  str << "\t<TABLE>";
215 
216  for (inst.setFirst(); !inst.end(); inst.inc()) {
217  if (inst.val(0) == 0)
218  str << std::endl << "\t\t";
219  else
220  str << " ";
221 
222  str << cpt[inst]; //"<!-- "<<inst<<" -->"<<std::endl;
223  }
224 
225  str << std::endl << "\t</TABLE>" << std::endl;
226 
227  // Closing tag
228  str << "</DEFINITION>" << std::endl;
229 
230  return str.str();
231  }
232 
233  /*
234  * Returns the end of the BIF file.
235  */
236  template < typename GUM_SCALAR >
237  INLINE std::string BIFXMLBNWriter< GUM_SCALAR >::__documentend() {
238  std::stringstream str;
239 
240  str << "</NETWORK>" << std::endl;
241  str << "</BIF>" << std::endl;
242 
243  return str.str();
244  }
245 
246 } /* namespace gum */
247 
248 #endif // DOXYGEN_SHOULD_SKIP_THIS
BIFXMLBNWriter()
Default constructor.
std::string __heading(const IBayesNet< GUM_SCALAR > &bn)
Returns the header of the BIF file.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
std::string __variableBloc(const DiscreteVariable &var)
Returns a bloc defining a variable in the BIF format.
std::string __documentend()
Returns the end of the BIF file.
std::string __variableDefinition(const NodeId &varNodeId, const IBayesNet< GUM_SCALAR > &bn)
Returns a bloc defining a variable&#39;s table (if she has) in the BIF format.
void write(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes an bayes net in the given ouput stream.
~BIFXMLBNWriter() final
Destructor.
Definition file for BIF XML exportation class.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52