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