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