aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
UAIMNWriter.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) et Christophe GONZALES(@AMU)
4  * (@AMU) 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  * @file
23  * @brief Definition file for UAI exportation class
24  *
25  * Writes a markov net in UAI format
26  *
27  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
28  */
29 
30 
31 #ifndef UAI_MN_WRITER
32 #define UAI_MN_WRITER
33 
34 
35 #include <fstream>
36 #include <iostream>
37 #include <sstream>
38 #include <string>
39 
40 #include <agrum/MN/io/MNWriter.h>
41 #include <agrum/agrum.h>
42 
43 namespace gum {
44  /**
45  * @class UAIMNWriter UAIMNWriter.h
46  *<agrum/MN/io/UAI/UAIMNWriter.h>
47  * @ingroup mn_io
48  * @brief Writes an Markov net in a text file with UAI format
49  *
50  * This class export a Markov net into an text file, using UAI format
51  *
52  * cf. http://www.cs.huji.ac.il/project/PASCAL/fileFormat.php
53  *
54  */
55  template < typename GUM_SCALAR >
56  class UAIMNWriter: public MNWriter< GUM_SCALAR > {
57  public:
58  // ==========================================================================
59  /// @name Constructor & destructor
60  // ==========================================================================
61  /// @{
62 
63  /**
64  * Default constructor.
65  */
66  UAIMNWriter();
67 
68  /**
69  * Destructor.
70  */
71  ~UAIMNWriter() final;
72 
73  /// @}
74 
75  /**
76  * Writes an Markov net in the given ouput stream.
77  *
78  * @param output The output stream.
79  * @param MN The Markov net writen in the stream.
80  * @throws IOError Raised if an I/O error occurs.
81  */
82  void write(std::ostream& output, const IMarkovNet< GUM_SCALAR >& MN) final;
83 
84  /**
85  * Writes an Markov net in the file referenced by filePath.
86  * If the file doesn't exists, it is created.
87  * If the file exists, it's content will be erased.
88  *
89  * @param filePath The path to the file used to write the Markov net.
90  * @param MN The Markov net writen in the file.
91  * @throw IOError Raised if an I/O error occurs.
92  */
93  void write(const std::string& filePath, const IMarkovNet< GUM_SCALAR >& MN) final;
94 
95  private:
96  /**
97  * Returns the header of the BIF file.
98  */
99  std::string _preambule_(const IMarkovNet< GUM_SCALAR >& MN);
100 
101  std::string _factorBloc_(const IMarkovNet< GUM_SCALAR >& MN,
102  const Potential< GUM_SCALAR >& clikpot);
103  };
104 
105 
106 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
107  extern template class UAIMNWriter< double >;
108 #endif
109 
110 } /* namespace gum */
111 
112 #include <agrum/MN/io/UAI/UAIMNWriter_tpl.h>
113 #endif // UAI_MN_WRITER