aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
fmdpDatReader.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 dat files for Factored Markov
25  Decision Process.
26  *
27  * how to use it :
28  * @code
29  // OPTIONAL LISTENER CLASS
30  class aSimpleListener : public gum::Listener {
31  public:
32  void whenLoading(const void *buffer,int percent) {
33  // percent goes from 0 to 100 (whenLoading is called at most once for each
34  integer between 0 and 100
35  // percent=200 recieved when End Of File.
36  }
37  };
38  // END OF OPTIONAL LISTENER
39 
40  gum::FMDP<double> fmdp;
41 
42  try {
43  gum::FMDPDatReader<double> reader( &fmdp, 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 
65 // ======================================================================================================
66 #ifndef FMDP_DAT_READER_H
67 #define FMDP_DAT_READER_H
68 // ======================================================================================================
69 #include <fstream>
70 #include <iostream>
71 #include <string>
72 // ======================================================================================================
73 #include <agrum/FMDP/fmdp.h>
74 #include <agrum/FMDP/fmdpFactory.h>
75 #include <agrum/FMDP/io/fmdpReader.h>
76 // ======================================================================================================
77 #include <agrum/tools/multidim/implementations/multiDimFunctionGraph.h>
78 // ======================================================================================================
79 
80 #ifndef DOXYGEN_SHOULD_SKIP_THIS
81 // including coco-generated PARSER and SCANNER
82 # include <agrum/FMDP/io/dat/cocoR/Parser.h>
83 #endif // DOXYGEN_SHOULD_SKIP_THIS
84 
85 namespace gum {
86  /**
87  * @class FMDPDatReader
88  * @brief Definition of templatized reader of FMDPDat files for Factored
89  * Markov Decision Processes.
90  * @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and
91  * Christophe GONZALES(@AMU)
92  */
93  template < typename GUM_SCALAR >
94  class FMDPDatReader: public FMDPReader< GUM_SCALAR > {
95  public:
96  FMDPDatReader(FMDP< GUM_SCALAR >* fmdp, const std::string& filename);
97  ~FMDPDatReader();
98 
99  /// Direct access to FMDPDat scanner (mandatory for listener connection)
100  /// @throws IOError if file not exists
101  MDPDAT::Scanner& scanner();
102 
103  /// name of readen file
104  const std::string& streamName() const;
105 
106  /// accessor to trace function (just write the number of parser line)
107  bool trace() const;
108  void trace(bool b);
109 
110  /// parse.
111  /// @return the number of detected errors
112  /// @throws IOError if file not exists
113  Size proceed();
114 
115  /// @{
116  /// publishing Errors API
117 
118  /// # of errors
119  Size errors();
120  /// # of errors
121  Size warnings();
122 
123  /// line of ith error or warning
124  Idx errLine(Idx i);
125  /// col of ith error or warning
126  Idx errCol(Idx i);
127  /// type of ith error or warning
128  bool errIsError(Idx i);
129  /// message of ith error or warning
130  std::string errMsg(Idx i);
131 
132  /// send on std::cerr the list of errors
133  void showElegantErrors(std::ostream& o = std::cerr);
134 
135  /// send on std::cerr the list of errors or warnings
136  void showElegantErrorsAndWarnings(std::ostream& o = std::cerr);
137 
138  /// send on std::cerr the number of errors and the number of warnings
139  void showErrorCounts(std::ostream& o = std::cerr);
140  /// @}
141 
142  protected:
143  FMDP< GUM_SCALAR >* _fmdp_;
144  FMDPFactory< GUM_SCALAR >* _factory_;
145  MDPDAT::Scanner* _scanner_;
146  MDPDAT::Parser* _parser_;
147 
148  std::string _streamName_;
149  bool _traceScanning_;
150  bool _parseDone_;
151 
152  // a boolean to throw the ioerror not in the constructor but in the
153  // proceed()
154  bool _ioerror_;
155  };
156 
157 
158 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
159  extern template class FMDPDatReader< double >;
160 #endif
161 
162 } // namespace gum
163 
164 #include <agrum/FMDP/io/dat/fmdpDatReader_tpl.h>
165 
166 #endif // FMDP_DAT_READER_H