aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
BIFReader.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 of templatized reader of BIF files for Bayesian networks.
25  *
26  * how to use it :
27  * @code
28 // OPTIONAL LISTENER CLASS
29 class aSimpleListener : public gum::Listener {
30 public:
31  void whenLoading(const void *buffer,int percent) {
32  // percent goes from 0 to 100 (whenLoading is called at most once for each
33 integer
34 between 0 and 100
35  // percent=200 recieved when End Of File.
36  }
37 };
38 // END OF OPTIONAL LISTENER
39 
40  gum::BayesNet<double> bn;
41 
42  try {
43  gum::BIFReader<double> reader(&bn,std::string(args[1]));
44 
45  // OPTIONAL SECTION
46  aSimpleListener l;
47  GUM_CONNECT(reader.scanner(),onLoad,l,aSimpleListener::whenLoading);
48  // END OF OPTIONNAL SECTION
49 
50  if (reader.proceed()==0) {
51  std::cerr<<"Well done !"<<std::endl;
52  } else {
53  reader.showElegantErrorsAndWarnings();
54  reader.showErrorCounts();
55  }
56  } catch (gum::IOError& e) {GUM_SHOWERROR(e);}
57 
58  return 0;
59 
60  * @endcode
61  *
62  * @author Pierre-Henri WUILLEMIN(@LIP6)
63  */
64 #ifndef BIF_READER_H
65 #define BIF_READER_H
66 
67 #include <fstream>
68 #include <iostream>
69 #include <string>
70 
71 #include <agrum/BN/BayesNetFactory.h>
72 #include <agrum/BN/IBayesNet.h>
73 #include <agrum/BN/io/BNReader.h>
74 
75 #ifndef DOXYGEN_SHOULD_SKIP_THIS
76 // including coco-generated PARSER and SCANNER
77 # include <agrum/BN/io/BIF/cocoR/Parser.h>
78 #endif // DOXYGEN_SHOULD_SKIP_THIS
79 
80 namespace gum {
81  /**
82 * @class BIFReader
83 * @headerfile BIFReader.h <agrum/BN/io/BIF/BIFReader.h>
84  * @ingroup bn_io
85  * @brief Definition of templatized reader of BIF files for Bayesian networks.
86  *
87  * how to use it :
88  * @code
89  // OPTIONAL LISTENER CLASS
90  class aSimpleListener : public gum::Listener {
91  public:
92  void whenLoading(const void *buffer,int percent) {
93  // percent goes from 0 to 100 (whenLoading is called at most once for each
94  integer between 0 and 100
95  // percent=200 recieved when End Of File.
96  }
97  };
98  // END OF OPTIONAL LISTENER
99 
100  gum::BayesNet<double> bn;
101 
102  try {
103  gum::BIFReader<double> reader(&bn,std::string(args[1]));
104 
105  // OPTIONAL SECTION
106  aSimpleListener l;
107  GUM_CONNECT(reader.scanner(),onLoad,l,aSimpleListener::whenLoading);
108  // END OF OPTIONNAL SECTION
109 
110  if (reader.proceed()==0) {
111  std::cerr<<"Well done !"<<std::endl;
112  } else {
113  reader.showElegantErrorsAndWarnings();
114  reader.showErrorCounts();
115  }
116  } catch (gum::IOError& e) {GUM_SHOWERROR(e);}
117 
118  return 0;
119 
120  * @endcode
121  *
122  * @author Pierre-Henri WUILLEMIN(@LIP6)
123  */
124  template < typename GUM_SCALAR >
125  class BIFReader: public BNReader< GUM_SCALAR > {
126  public:
127  BIFReader(BayesNet< GUM_SCALAR >* bn, const std::string& filename);
128 
129  ~BIFReader() final;
130 
131  /// Direct access to BIF scanner (mandatory for listener connection)
132  /// @throws IOError if file not exists
133  BIF::Scanner& scanner();
134 
135  /// name of readen file
136  const std::string& streamName() const;
137 
138  /// accessor to trace function (just write the number of parser line)
139  bool trace() const;
140  void trace(bool b);
141 
142  /// parse.
143  /// @return the number of detected errors
144  /// @throws IOError if file not exists
145  Size proceed() final;
146 
147  /// @{
148  /// publishing Errors API
149 
150  /// # of errors
151  Size errors();
152  /// # of errors
153  Size warnings();
154 
155  /// line of ith error or warning
156  Idx errLine(Idx i);
157  /// col of ith error or warning
158  Idx errCol(Idx i);
159  /// type of ith error or warning
160  bool errIsError(Idx i);
161  /// message of ith error or warning
162  std::string errMsg(Idx i);
163 
164  /// send on std::cerr the list of errors
165  void showElegantErrors(std::ostream& o = std::cerr);
166 
167  /// send on std::cerr the list of errors or warnings
168  void showElegantErrorsAndWarnings(std::ostream& o = std::cerr);
169 
170  /// send on std::cerr the number of errors and the number of warnings
171  void showErrorCounts(std::ostream& o = std::cerr);
172  /// @}
173 
174  protected:
175  BayesNet< GUM_SCALAR >* _bn_;
176  BayesNetFactory< GUM_SCALAR >* _factory_;
177  BIF::Scanner* _scanner_;
178  BIF::Parser* _parser_;
179 
180  std::string _streamName_;
181  bool _traceScanning_;
182  bool _parseDone_;
183 
184  // a boolean to throw the ioerror not in the constructor but in the
185  // proceed()
186  bool _ioerror_;
187  };
188 
189 
190 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
191  extern template class BIFReader< double >;
192 #endif
193 
194 } // namespace gum
195 
196 #include <agrum/BN/io/BIF/BIFReader_tpl.h>
197 
198 #endif // BIF_READER_H