aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
O3prmBNWriter.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_O3PRMBNWRITER_H
33 #define GUM_O3PRMBNWRITER_H
34 
35 
36 #include <fstream>
37 #include <iostream>
38 #include <sstream>
39 #include <string>
40 
41 #include <agrum/BN/io/BNWriter.h>
42 #include <agrum/agrum.h>
43 #include <agrum/tools/variables/discreteVariable.h>
44 #include <agrum/tools/variables/discretizedVariable.h>
45 #include <agrum/tools/variables/rangeVariable.h>
46 
47 namespace gum {
48  /**
49  * @class O3prmBNWriter O3prmBNWriter.h
50  *<agrum/PRM/o3prm/O3prmBNWriter.h>
51  * @ingroup bn_io
52  * @brief Writes an bayes net in a text file with O3PRM format
53  *
54  * This class export a bayes net into an text file, using O3PRM format
55  *
56  */
57  template < typename GUM_SCALAR >
58  class O3prmBNWriter: public BNWriter< GUM_SCALAR > {
59  public:
60  // ==========================================================================
61  /// @name Constructor & destructor
62  // ==========================================================================
63  /// @{
64 
65  /**
66  * Default constructor.
67  */
68  O3prmBNWriter();
69 
70  /**
71  * Destructor.
72  */
73  virtual ~O3prmBNWriter();
74 
75  /// @}
76 
77  /**
78  * Writes an bayes net in the given ouput stream.
79  *
80  * @param output The output stream.
81  * @param bn The bayes net writen in the stream.
82  * @throws IOError Raised if an I/O error occurs.
83  */
84  virtual void write(std::ostream& output, const IBayesNet< GUM_SCALAR >& bn) final;
85 
86  /**
87  * Writes an bayes net in the file referenced by filePath.
88  * If the file doesn't exists, it is created.
89  * If the file exists, it's content will be erased.
90  *
91  * @param filePath The path to the file used to write the bayes net.
92  * @param bn The bayes net written in the file.
93  * @throw IOError Raised if an I/O error occurs.
94  */
95  virtual void write(const std::string& filePath, const IBayesNet< GUM_SCALAR >& bn) final;
96 
97  private:
98  std::string _extractAttribute_(const IBayesNet< GUM_SCALAR >& bn, NodeId node);
99 
100  std::string _extractType_(const IBayesNet< GUM_SCALAR >& bn, NodeId node);
101 
102  template < typename VARTYPE >
103  std::string _extractDiscretizedType_(const VARTYPE* var);
104 
105  std::string _extractName_(const IBayesNet< GUM_SCALAR >& bn, NodeId node);
106 
107  std::string _extractParents_(const IBayesNet< GUM_SCALAR >& bn, NodeId node);
108 
109  std::string _extractCPT_(const IBayesNet< GUM_SCALAR >& bn, NodeId node);
110 
111  std::string _extractRangeType_(const IBayesNet< GUM_SCALAR >& bn, NodeId node);
112 
113  std::string _extractLabelizedType_(const IBayesNet< GUM_SCALAR >& bn, NodeId node);
114  };
115 
116 
117 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
118  extern template class O3prmBNWriter< double >;
119 #endif
120 
121 } /* namespace gum */
122 
123 #include <agrum/PRM/o3prm/O3prmBNWriter_tpl.h>
124 
125 #endif // GUM_O3PRMBNWRITER_H