aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
BIFXMLIDReader.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 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, const std::string& filePath);
110 
111  /**
112  * Default destructor.
113  */
114  ~BIFXMLIDReader();
115 
116  /**
117  * Reads the influence diagram from the file referenced by filePath given
118  * at the
119  * creation of class
120  * @return Returns the number of error during the parsing (0 if none).
121  */
122  virtual void proceed();
123 
124  /**
125  * Signaler used to indicates how many percent of the Xml files have been
126  * parsed
127  * yet
128  */
129  typename gum::Signaler2< int, std::string > onProceed;
130 
131  private:
132  /**
133  * Parsing xml element containing data on variables
134  */
135  void _parsingVariables_(ticpp::Element* parentNetwork);
136 
137  /**
138  * fill the diagram
139  */
140  void _fillingDiagram_(ticpp::Element* parentNetwork);
141 
142  /**
143  * An handle to the influence diagram in which will be load the content of
144  * the
145  * xml filePath
146  */
147  InfluenceDiagram< GUM_SCALAR >* _infdiag_;
148 
149  /**
150  * the path to the xml filePath
151  */
152  std::string _filePath_;
153  };
154 
155 } /* namespace gum */
156 
157 #include <agrum/ID/io/BIFXML/BIFXMLIDReader_tpl.h>
158 
159 #endif // GUM_BIF_XML_ID_READER_H