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