aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
UAIBNReader.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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(
98  std::vector< std::tuple< float, int, int, int > > quartets);
99 
100  /// @{
101  /// publishing Errors API
102 
103  /// # of errors
104  Size errors();
105  /// # of errors
106  Size warnings();
107 
108  /// line of ith error or warning
109  Idx errLine(Idx i);
110  /// col of ith error or warning
111  Idx errCol(Idx i);
112  /// type of ith error or warning
113  bool errIsError(Idx i);
114  /// message of ith error or warning
115  std::string errMsg(Idx i);
116 
117  /// send on std::cerr the list of errorswith contents
118  void showElegantErrors(std::ostream& o = std::cerr);
119 
120  /// send on std::cerr the list of errors or warnings with contents
121  void showElegantErrorsAndWarnings(std::ostream& o = std::cerr);
122 
123  /// send on std::cerr the list of errors or warnings
124  void showErrorsAndWarnings(std::ostream& o = std::cerr);
125 
126  /// send on std::cerr the number of errors and the number of warnings
127  void showErrorCounts(std::ostream& o = std::cerr);
128  /// @}
129 
130  protected:
131  BayesNet< GUM_SCALAR >* bn__;
132  BayesNetFactory< GUM_SCALAR >* factory__;
133  UAIBN::Scanner* scanner__;
134  UAIBN::Parser* parser__;
135 
136  std::string streamName__;
137  bool traceScanning__;
138  bool parseDone__;
139 
140  // a boolean to throw the ioerror not in the constructor but in the
141  // proceed()
142  bool ioerror__;
143 
144  void addFatalError__(Idx lig,
145  Idx col,
146  const std::string& s); // throw an exception
147  void addError__(Idx lig, Idx col, const std::string& s);
148  void addWarning__(Idx lig, Idx col, const std::string& s);
149  };
150 
151 
152 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
153  extern template class UAIBNReader< double >;
154 #endif
155 
156 } /* namespace gum */
157 
158 #include "UAIBNReader_tpl.h"
159 
160 #endif // UAIBNREADER_H