1 /**
2  *
3  *   Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(_at_LIP6) & Christophe GONZALES(_at_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 bayes net from a XML file written with BIF Format
25  *
26  * Read an bayes net from a XML file with BIF format
27  *
28  * how to use it :
29  * @code
30 // OPTIONAL LISTENER CLASS
31 class aSimpleListener : public gum::Listener {
32 public:
33   void whenProceeding(const void *buffer,int percent, std::string status) {
34   // percent goes from 0 to 100 (whenLoading is called at most once for each
35 integer
36 between 0 and 100
37     // percent=200 recieved when End Of File.
38  }
39 };
40 // END OF OPTIONAL LISTENER
41 
42  gum::BayesNet<double> bn;
43 
44  try {
45   gum::BIFXMLBNReader<double> reader(&bn,std::string(args[1]));
46 
47   // OPTIONAL SECTION
48   aSimpleListener asl;
49   GUM_CONNECT( reader, onProceed, asl, aSimpleListener::whenProceing );
50   // END OF OPTIONAL SECTION
51 
52   // Not implemented yet section
53   //if (reader.proceed()==0) {
54    //std::cerr<<"Well done !"<<std::endl;
55   //} else {
56    //reader.showElegantErrorsAndWarnings();
57    //reader.showErrorCounts();
58   //}
59   // End not implemented section
60 
61  } catch (gum::IOError& e) {GUM_SHOWERROR(e);}
62 
63   return 0;
64 
65  * @endcode
66  *
67  *
68  * @author Pierre-Henri WUILLEMIN(_at_LIP6) and Jean-Christophe MAGNAN and Christophe
69 GONZALES(_at_AMU)
70  */
71 #ifndef GUM_BIF_XML_BN_READER_H
72 #define GUM_BIF_XML_BN_READER_H
73 
74 #define TIXML_USE_TICPP
75 
76 #include <list>
77 #include <sstream>
78 #include <string>
79 
80 #include <agrum/BN/io/BNReader.h>
81 #include <agrum/agrum.h>
82 #include <agrum/tools/core/signal/signaler.h>
83 #include <agrum/tools/core/signal/signaler2.h>
84 #include <agrum/tools/external/tinyxml/ticpp/ticpp.h>
85 #include <agrum/tools/variables/labelizedVariable.h>
86 
87 namespace gum {
88 
89   /**
90    * @class BIFXMLBNReader BIFXMLBNReader.h
91    *<agrum/BN/io/BIFXML/BIFXMLBNReader.h>
92    * @ingroup bn_io
93    * @brief Read an bayes net from an XML file with BIF format.
94    *
95    * This class import an bayes net 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 BIFXMLBNReader: BNReader< GUM_SCALAR > {
103     public:
104     /**
105      * Constructor
106      * A reader is created to reading a defined file.
107      * Note that an BN as to be created before and given in parameter.
108      */
109     BIFXMLBNReader(BayesNet< GUM_SCALAR >* bn, const std::string& filePath);
110 
111     /**
112      * Default destructor.
113      */
114     ~BIFXMLBNReader();
115 
116     /**
117      * Reads the bayes net from the file referenced by filePath  given at the
118      * creation of class
119      * @return Returns 0 if no error, 1 if any
120      * @warning XMLBNReader can not give the number of errors.
121      */
122     Size proceed() final;
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 _fillingBN_(ticpp::Element* parentNetwork);
141 
142     /**
143      * An handle to the bayes net in which will be load the content of the xml
144      * filePath
145      */
146     BayesNet< GUM_SCALAR >* _bn_;
147 
148     /**
149      * the path to the xml filePath
150      */
151     std::string _filePath_;
152   };
153 
154 
155 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
156   extern template class BIFXMLBNReader< double >;
157 #endif
158 
159 } /* namespace gum */
160 
161 #include <agrum/BN/io/BIFXML/BIFXMLBNReader_tpl.h>
162 
163 #endif   // GUM_BIF_XML_BN_READER_H
164