1 /**
2  *
3  *   Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(_at_LIP6) et Christophe GONZALES(_at_AMU)
4  * (_at_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(_at_LIP6) & Christophe GONZALES(_at_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();
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(std::vector< std::tuple< float, int, int, int > > quartets);
95 
96     /// @{
97     /// publishing Errors API
98 
99     /// # of errors
100     Size errors();
101     /// # of errors
102     Size warnings();
103 
104     /// line of ith error or warning
105     Idx errLine(Idx i);
106     /// col of ith error or warning
107     Idx errCol(Idx i);
108     /// type of ith error or warning
109     bool errIsError(Idx i);
110     /// message of ith error or warning
111     std::string errMsg(Idx i);
112 
113     /// send on std::cerr the list of errorswith contents
114     void showElegantErrors(std::ostream& o = std::cerr);
115 
116     /// send on std::cerr the list of errors or warnings with contents
117     void showElegantErrorsAndWarnings(std::ostream& o = std::cerr);
118 
119     /// send on std::cerr the list of errors or warnings
120     void showErrorsAndWarnings(std::ostream& o = std::cerr);
121 
122     /// send on std::cerr the number of errors and the number of warnings
123     void showErrorCounts(std::ostream& o = std::cerr);
124     /// @}
125 
126     protected:
127     MarkovNet< GUM_SCALAR >* _mn_;
128     UAIMN::Scanner*          _scanner_;
129     UAIMN::Parser*           _parser_;
130 
131     std::string _streamName_;
132     bool        _traceScanning_;
133     bool        _parseDone_;
134 
135     // a boolean to throw the ioerror not in the constructor but in the
136     // proceed()
137     bool _ioerror_;
138 
139     void _addFatalError_(Idx lig, Idx col,
140                          const std::string& s);   // throw an exception
141     void _addError_(Idx lig, Idx col, const std::string& s);
142     void _addWarning_(Idx lig, Idx col, const std::string& s);
143   };
144 
145 
146 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
147   extern template class UAIMNReader< double >;
148 #endif
149 
150 } /* namespace gum */
151 
152 #include "UAIMNReader_tpl.h"
153 
154 #endif   // UAI_MN_READER_H
155