aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
DSLReader.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 #ifndef DSLREADER_H
23 #define DSLREADER_H
24 
25 #include <agrum/BN/IBayesNet.h>
26 #include <agrum/BN/io/BNReader.h>
27 #include <agrum/agrum.h>
28 #include <iostream>
29 #include <string>
30 
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
32 // including coco-generated PARSER and SCANNER
33 
34 # include <agrum/BN/io/DSL/cocoR/Parser.h>
35 #endif // DOXYGEN_SHOULD_SKIP_THIS
36 
37 namespace gum {
38  /* =========================================================================*/
39  /* === READERS === */
40  /* =========================================================================*/
41  /**
42  * @class DSLReader
43  * @headerfile DSLReader.h <agrum/BN/io/DSL/DSLReader.h>
44  * @ingroup bn_io
45  * @brief Pure virtual class for reading a BN from a file.
46  *
47  * Every class used to read the content of a Bayesian network from a stream,
48  * or a file must be a subclass of DSLReader.
49  */
50  template < typename GUM_SCALAR >
51  class DSLReader: public BNReader< GUM_SCALAR > {
52  public:
53  /**
54  * Constructor
55  * A reader is defined for reading a defined file. Hence the 2 args of the
56  * constructor.
57  * Note that the BN has to be built outside the reader. There is no
58  * delegation to
59  * create/destroy
60  * the BN from inside the reader.
61  */
62  DSLReader(BayesNet< GUM_SCALAR >* bn, const std::string& filename);
63 
64  /**
65  * Default destructor.
66  */
67  ~DSLReader() final;
68 
69  /// Direct access to DSL scanner (mandatory for listener connection)
70  /// @throws IOError if file not exists
71  DSL::Scanner& scanner();
72 
73  /// name of readen file
74  const std::string& streamName() const;
75 
76  /// accessor to trace function (just write the number of parser line)
77  bool trace() const;
78  void trace(bool b);
79 
80  /// parse.
81  /// @return the number of detected errors
82  /// @throws IOError if file not exists
83  Size proceed() final;
84 
85  /// @{
86  /// publishing Errors API
87 
88  /// # of errors
89  Size errors();
90  /// # of errors
91  Size warnings();
92 
93  /// line of ith error or warning
94  Idx errLine(Idx i);
95  /// col of ith error or warning
96  Idx errCol(Idx i);
97  /// type of ith error or warning
98  bool errIsError(Idx i);
99  /// message of ith error or warning
100  std::string errMsg(Idx i);
101 
102  /// send on std::cerr the list of errorswith contents
103  void showElegantErrors(std::ostream& o = std::cerr);
104 
105  /// send on std::cerr the list of errors or warnings with contents
106  void showElegantErrorsAndWarnings(std::ostream& o = std::cerr);
107 
108  /// send on std::cerr the list of errors or warnings
109  void showErrorsAndWarnings(std::ostream& o = std::cerr);
110 
111  /// send on std::cerr the number of errors and the number of warnings
112  void showErrorCounts(std::ostream& o = std::cerr);
113  /// @}
114 
115  protected:
116  BayesNet< GUM_SCALAR >* _bn_;
117  BayesNetFactory< GUM_SCALAR >* _factory_;
118  DSL::Scanner* _scanner_;
119  DSL::Parser* _parser_;
120 
121  std::string _streamName_;
122  bool _traceScanning_;
123  bool _parseDone_;
124 
125  // a boolean to throw the ioerror not in the constructor but in the
126  // proceed()
127  bool _ioerror_;
128  };
129 
130 
131 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
132  extern template class DSLReader< double >;
133 #endif
134 
135 } /* namespace gum */
136 
137 #include "DSLReader_tpl.h"
138 
139 #endif // DSLREADER_H