aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
BIFXMLIDReader.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 classe for import of Influence Diagram from a XML file written with
25 BIF
26 Format
27  *
28  * Read an influence diagram from a XML file with BIF format
29  *
30  * how to use it :
31  * @code
32 // OPTIONAL LISTENER CLASS
33 class aSimpleListener : public gum::Listener {
34 public:
35  void whenProceeding(const void *buffer,int percent, std::string status) {
36  // percent goes from 0 to 100 (whenLoading is called at most once for each
37 integer
38 between 0 and 100
39  // percent=200 recieved when End Of File.
40  }
41 };
42 // END OF OPTIONAL LISTENER
43 
44  gum::InfluenceDiagram<double> id;
45 
46  try {
47  gum::BIFXMLIDReader<double> reader(&id,std::string(args[1]));
48 
49  // OPTIONAL SECTION
50  aSimpleListener asl;
51  GUM_CONNECT( reader, onProceed, asl, aSimpleListener::whenProceing );
52  // END OF OPTIONNAL SECTION
53 
54  // Not implemented yet section
55  //if (reader.proceed()==0) {
56  //std::cerr<<"Well done !"<<std::endl;
57  //} else {
58  //reader.showElegantErrorsAndWarnings();
59  //reader.showErrorCounts();
60  //}
61  // End not implemented section
62 
63  } catch (gum::IOError& e) {GUM_SHOWERROR(e);}
64 
65  return 0;
66 
67  * @endcode
68  *
69  *
70  * @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
71 GONZALES(@AMU)
72  */
73 #ifndef GUM_BIF_XML_ID_READER_H
74 #define GUM_BIF_XML_ID_READER_H
75 
76 #define TIXML_USE_TICPP
77 
78 #include <list>
79 #include <sstream>
80 #include <string>
81 
82 #include <agrum/ID/io/IDReader.h>
83 #include <agrum/agrum.h>
84 #include <agrum/tools/core/signal/signaler.h>
85 #include <agrum/tools/core/signal/signaler2.h>
86 #include <agrum/tools/external/tinyxml/ticpp/ticpp.h>
87 #include <agrum/tools/variables/labelizedVariable.h>
88 
89 namespace gum {
90  /**
91  * @class BIFXMLIDReader
92  * @brief Read an influence diagram from an XML file with BIF format.
93  * @ingroup id_group
94  *
95  * This class import an influence diagram from an XML files using BIF format
96  * See
97  *http://www-2.cs.cmu.edu/afs/cs/user/fgcozman/www/Research/InterchangeFormat/
98  * for information about this format.
99  *
100  */
101  template < typename GUM_SCALAR >
102  class BIFXMLIDReader: IDReader< GUM_SCALAR > {
103  public:
104  /**
105  * Constructor
106  * A reader is created to reading a defined file.
107  * Note that an ID as to be created before and given in parameter.
108  */
109  BIFXMLIDReader(InfluenceDiagram< GUM_SCALAR >* infdiag,
110  const std::string& filePath);
111 
112  /**
113  * Default destructor.
114  */
115  ~BIFXMLIDReader();
116 
117  /**
118  * Reads the influence diagram from the file referenced by filePath given
119  * at the
120  * creation of class
121  * @return Returns the number of error during the parsing (0 if none).
122  */
123  virtual void proceed();
124 
125  /**
126  * Signaler used to indicates how many percent of the Xml files have been
127  * parsed
128  * yet
129  */
130  typename gum::Signaler2< int, std::string > onProceed;
131 
132  private:
133  /**
134  * Parsing xml element containing data on variables
135  */
136  void parsingVariables__(ticpp::Element* parentNetwork);
137 
138  /**
139  * fill the diagram
140  */
141  void fillingDiagram__(ticpp::Element* parentNetwork);
142 
143  /**
144  * An handle to the influence diagram in which will be load the content of
145  * the
146  * xml filePath
147  */
148  InfluenceDiagram< GUM_SCALAR >* infdiag__;
149 
150  /**
151  * the path to the xml filePath
152  */
153  std::string filePath__;
154  };
155 
156 } /* namespace gum */
157 
158 #include <agrum/ID/io/BIFXML/BIFXMLIDReader_tpl.h>
159 
160 #endif // GUM_BIF_XML_ID_READER_H