aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
BIFXMLBNWriter.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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 /**
23  * @file
24  * @brief Definition file for BIF XML exportation class
25  *
26  * Writes an bayes net in XML files with BIF format
27  *
28  * @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
29  * GONZALES(@AMU)
30  */
31 
32 #ifndef GUM_BIF_XML_BN_WRITER_H
33 #define GUM_BIF_XML_BN_WRITER_H
34 
35 #define TIXML_USE_TICPP
36 
37 #include <fstream>
38 #include <iostream>
39 #include <sstream>
40 #include <string>
41 
42 #include <agrum/BN/io/BNWriter.h>
43 #include <agrum/agrum.h>
44 
45 namespace gum {
46 
47  /**
48  * @class BIFXMLBNWriter BIFXMLBNWriter.h
49  *<agrum/BN/io/BIFXML/BIFXMLBNWriter.h>
50  * @ingroup bn_io
51  * @brief Writes an bayes net in a XML file with BIF format
52  *
53  * This class export a bayes net into an XML files, using BIF format
54  * See
55  *http://www-2.cs.cmu.edu/afs/cs/user/fgcozman/www/Research/InterchangeFormat/
56  * for information about this format.
57  *
58  */
59  template < typename GUM_SCALAR >
60  class BIFXMLBNWriter: public BNWriter< GUM_SCALAR > {
61  public:
62  // ==========================================================================
63  /// @name Constructor & destructor
64  // ==========================================================================
65  /// @{
66 
67  /**
68  * Default constructor.
69  */
70  BIFXMLBNWriter();
71 
72  /**
73  * Destructor.
74  */
75  ~BIFXMLBNWriter() final;
76 
77  /// @}
78 
79  /**
80  * Writes an bayes net in the given ouput stream.
81  *
82  * @param output The output stream.
83  * @param infdiag The bayes net writen in the stream.
84  * @throws IOError Raised if an I/O error occurs.
85  */
86  void write(std::ostream& output, const IBayesNet< GUM_SCALAR >& bn) final;
87 
88  /**
89  * Writes an bayes net in the file referenced by filePath.
90  * If the file doesn't exists, it is created.
91  * If the file exists, it's content will be erased.
92  *
93  * @param filePath The path to the file used to write the bayes net.
94  * @param infdiag The bayes net writen in the file.
95  * @throw IOError Raised if an I/O error occurs.
96  */
97  void write(const std::string& filePath, const IBayesNet< GUM_SCALAR >& bn) final;
98 
99  private:
100  /**
101  * Returns the header of the BIF file.
102  */
103  std::string _heading_(const IBayesNet< GUM_SCALAR >& bn);
104 
105  /**
106  * Returns the end of the BIF file.
107  */
108  std::string _documentend_();
109 
110  /**
111  * Returns a bloc defining a variable in the BIF format.
112  */
113  std::string _variableBloc_(const DiscreteVariable& var);
114 
115  /**
116  * Returns a bloc defining a variable's table (if she has) in the BIF
117  * format.
118  */
119  std::string _variableDefinition_(const NodeId& varNodeId, const IBayesNet< GUM_SCALAR >& bn);
120  };
121 
122 
123 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
124  extern template class BIFXMLBNWriter< double >;
125 #endif
126 
127 } /* namespace gum */
128 
129 #include <agrum/BN/io/BIFXML/BIFXMLBNWriter_tpl.h>
130 #endif // GUM_BIF_XML_BN_WRITER_H