aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
BIFWriter.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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 of classe for BIF file output manipulation
25  *
26  * This class servers to write the content of a Bayesian network in
27  * the BIF format.
28  *
29  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
30  */
31 
32 #ifndef GUM_BIF_WRITER_H
33 #define GUM_BIF_WRITER_H
34 
35 #include <fstream>
36 #include <iostream>
37 #include <sstream>
38 #include <string>
39 
40 #include <agrum/BN/io/BNWriter.h>
41 
42 #include <agrum/agrum.h>
43 
44 namespace gum {
45 
46  /**
47  * @class BIFWriter
48  * @headerfile BIFWriter.h <agrum/BN/io/BIF/BIFWriter.h>
49  * @ingroup bn_io
50  * @brief Writes a IBayesNet in the BIF format.
51  *
52  * This class servers to write the content of a Bayesian network in
53  * the BIF format. See
54  * http://www.cs.cmu.edu/~fgcozman/Research/InterchangeFormat/Old/xmlbif02.html
55  * for information on this format.
56  *
57  */
58  template < typename GUM_SCALAR >
59  class BIFWriter: public BNWriter< GUM_SCALAR > {
60  public:
61  // ==========================================================================
62  /// @name Constructor & destructor
63  // ==========================================================================
64  /// @{
65 
66  /**
67  * Default constructor.
68  */
69  BIFWriter();
70 
71  /**
72  * Destructor.
73  */
74  ~BIFWriter() final;
75 
76  /// @}
77 
78  /**
79  * Writes a Bayesian network in the output stream using the BIF format.
80  *
81  * @param output The output stream.
82  * @param bn The Bayesian network writen in output.
83  * @throws IOError Raised if and I/O error occurs.
84  */
85  void write(std::ostream& output, const IBayesNet< GUM_SCALAR >& bn) final;
86 
87  /**
88  * Writes a Bayesian network in the referenced file using the BIF format.
89  * If the files doesn't exists, it is created.
90  *
91  * @param filePath The path to the file used to write the Bayesian network.
92  * @param bn The Bayesian network writed in the file.
93  * @throws IOError Raised if and I/O error occurs.
94  */
95  void write(const std::string& filePath,
96  const IBayesNet< GUM_SCALAR >& bn) final;
97 
98  private:
99  // Returns the header of the BIF file.
100  std::string header__(const IBayesNet< GUM_SCALAR >& bn);
101 
102  // Returns a bloc defining a variable in the BIF format.
103  std::string variableBloc__(const DiscreteVariable& var);
104 
105  // Returns a bloc defining a variable's CPT in the BIF format.
106  std::string variableCPT__(const Potential< GUM_SCALAR >& cpt);
107 
108  // Returns the modalities labels of the variables in varsSeq
109  std::string
110  variablesLabels__(const Sequence< const DiscreteVariable* >& varsSeq,
111  const Instantiation& inst);
112  };
113 
114 
115 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
116  extern template class BIFWriter< double >;
117 #endif
118 
119 } /* namespace gum */
120 
121 #include <agrum/BN/io/BIF/BIFWriter_tpl.h>
122 #endif // GUM_BIF_WRITER_H