aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
BIFXMLIDWriter.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 influence diagram 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_ID_WRITER_H
33 #define GUM_BIF_XML_ID_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/ID/io/IDWriter.h>
43 #include <agrum/agrum.h>
44 
45 namespace gum {
46 
47  /**
48  * @class BIFXMLIDWriter
49  * @brief Writes an influence diagram in a XML files with BIF format
50  * @ingroup id_group
51  *
52  * This class export an influence diagram into an XML files, using BIF format
53  * See
54  *http://www-2.cs.cmu.edu/afs/cs/user/fgcozman/www/Research/InterchangeFormat/
55  * for information about this format.
56  *
57  */
58  template < typename GUM_SCALAR >
59 
60  class BIFXMLIDWriter: public IDWriter< GUM_SCALAR > {
61  public:
62  // ==========================================================================
63  /// @name Constructor & destructor
64  // ==========================================================================
65  /// @{
66 
67  /**
68  * Default constructor.
69  */
70  BIFXMLIDWriter();
71 
72  /**
73  * Destructor.
74  */
75  virtual ~BIFXMLIDWriter();
76 
77  /// @}
78 
79  /**
80  * Writes an influence diagram in the given ouput stream.
81  *
82  * @param output The output stream.
83  * @param infdiag The influence diagram writen in the stream.
84  * @throws IOError Raised if an I/O error occurs.
85  */
86  virtual void write(std::ostream& output, const InfluenceDiagram< GUM_SCALAR >& infdiag);
87 
88  /**
89  * Writes an Influence Diagram 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 Influence Diagram.
94  * @param infdiag The Influence Diagram writen in the file.
95  * @throw IOError Raised if an I/O error occurs.
96  */
97  virtual void write(std::string filePath, const InfluenceDiagram< GUM_SCALAR >& infdiag);
98 
99  private:
100  /**
101  * Returns the header of the BIF file.
102  */
103  std::string _heading_();
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, int nodeType);
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,
120  const InfluenceDiagram< GUM_SCALAR >& infdiag);
121  };
122 } /* namespace gum */
123 
124 #include <agrum/ID/io/BIFXML/BIFXMLIDWriter_tpl.h>
125 #endif // GUM_BIF_XML_ID_WRITER_H