aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
fmdpDatReader_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40#pragma once
41
42
51
52#ifndef DOXYGEN_SHOULD_SKIP_THIS
53
55
56namespace gum {
57
58 template < typename GUM_SCALAR >
60 const std::string& filename) :
61 FMDPReader< GUM_SCALAR >(fmdp, filename) {
62 GUM_CONSTRUCTOR(FMDPDatReader);
63
64 _fmdp_ = fmdp;
65 _streamName_ = filename;
66 _parseDone_ = false;
67 // ddf->putOnNoVariableCheckMode();
68 _factory_ = new FMDPFactory< GUM_SCALAR >(_fmdp_);
69 //~ _factory_->setVerbose();
70 _ioerror_ = false;
71
72 try {
73 _scanner_ = new MDPDAT::Scanner(_streamName_.c_str());
74 _parser_ = new MDPDAT::Parser(_scanner_);
75 _parser_->setFactory((AbstractFMDPFactory*)_factory_);
76 } catch (const IOError&) { _ioerror_ = true; }
77 }
78
79 template < typename GUM_SCALAR >
80 FMDPDatReader< GUM_SCALAR >::~FMDPDatReader() {
81 GUM_DESTRUCTOR(FMDPDatReader);
82
83 if (!_ioerror_) {
84 // this could lead to memory leak !!
85 if (_parser_) delete (_parser_);
86
87 if (_scanner_) delete (_scanner_);
88 }
89
90 if (_factory_) delete (_factory_);
91 }
92
93 template < typename GUM_SCALAR >
94 INLINE MDPDAT::Scanner& FMDPDatReader< GUM_SCALAR >::scanner() {
95 if (_ioerror_) { GUM_ERROR(gum::IOError, "No such file " + streamName()) }
96
97 return *_scanner_;
98 }
99
100 template < typename GUM_SCALAR >
101 INLINE const std::string& FMDPDatReader< GUM_SCALAR >::streamName() const {
102 return _streamName_;
103 }
104
105 template < typename GUM_SCALAR >
106 INLINE bool FMDPDatReader< GUM_SCALAR >::trace() const {
107 return _traceScanning_;
108 }
109
110 template < typename GUM_SCALAR >
111 INLINE void FMDPDatReader< GUM_SCALAR >::trace(bool b) {
112 _traceScanning_ = b;
113 scanner().setTrace(b);
114 }
115
116 template < typename GUM_SCALAR >
117 Size FMDPDatReader< GUM_SCALAR >::proceed() {
118 if (_ioerror_) { GUM_ERROR(gum::IOError, "No such file " + streamName()) }
119
120 if (!_parseDone_) {
121 try {
122 _parser_->Parse();
123 _parseDone_ = true;
124 } catch (gum::Exception& e) {
125 GUM_SHOWERROR(e);
126 return 1 + _parser_->errors().error_count;
127 }
128 }
129
130 return (_parser_->errors().error_count);
131 }
132
135 template < typename GUM_SCALAR >
136 INLINE Idx FMDPDatReader< GUM_SCALAR >::errLine(Idx i) {
137 if (_parseDone_) return _parser_->errors().error(i).line;
138 else GUM_ERROR(OperationNotAllowed, "FMDPDat file not parsed yet")
139 }
140
141 template < typename GUM_SCALAR >
142 INLINE Idx FMDPDatReader< GUM_SCALAR >::errCol(Idx i) {
143 if (_parseDone_) return _parser_->errors().error(i).column;
144 else GUM_ERROR(OperationNotAllowed, "FMDPDat file not parsed yet")
145 }
146
147 template < typename GUM_SCALAR >
148 INLINE bool FMDPDatReader< GUM_SCALAR >::errIsError(Idx i) {
149 if (_parseDone_) return _parser_->errors().error(i).is_error;
150 else GUM_ERROR(OperationNotAllowed, "FMDPDat file not parsed yet")
151 }
152
153 template < typename GUM_SCALAR >
154 INLINE std::string FMDPDatReader< GUM_SCALAR >::errMsg(Idx i) {
155 if (_parseDone_) return _parser_->errors().error(i).msg;
156 else GUM_ERROR(OperationNotAllowed, "FMDPDat file not parsed yet")
157 }
158
159 template < typename GUM_SCALAR >
160 INLINE void FMDPDatReader< GUM_SCALAR >::showElegantErrors(std::ostream& o) {
161 if (_parseDone_) _parser_->errors().elegantErrors(o);
162 else GUM_ERROR(OperationNotAllowed, "FMDPDat file not parsed yet")
163 }
164
165 template < typename GUM_SCALAR >
166 INLINE void FMDPDatReader< GUM_SCALAR >::showElegantErrorsAndWarnings(std::ostream& o) {
167 if (_parseDone_) _parser_->errors().elegantErrorsAndWarnings(o);
168 else GUM_ERROR(OperationNotAllowed, "FMDPDat file not parsed yet")
169 }
170
171 template < typename GUM_SCALAR >
172 INLINE void FMDPDatReader< GUM_SCALAR >::showErrorCounts(std::ostream& o) {
173 if (_parseDone_) _parser_->errors().syntheticResults(o);
174 else GUM_ERROR(OperationNotAllowed, "FMDPDat file not parsed yet")
175 }
176
177 template < typename GUM_SCALAR >
178 INLINE Size FMDPDatReader< GUM_SCALAR >::errors() {
179 return (!_parseDone_) ? (Size)0 : _parser_->errors().error_count;
180 }
181
182 template < typename GUM_SCALAR >
183 INLINE Size FMDPDatReader< GUM_SCALAR >::warnings() {
184 return (!_parseDone_) ? (Size)0 : _parser_->errors().warning_count;
185 }
186
188} // namespace gum
189
190#endif // DOXYGEN_SHOULD_SKIP_THIS
Base class for all aGrUM's exceptions.
Definition exceptions.h:118
FMDPDatReader(FMDP< GUM_SCALAR > *fmdp, const std::string &filename)
Pure virtual class for reading a FMDP from a file.
Definition fmdpReader.h:81
This class is used to implement factored decision process.
Definition fmdp.h:73
Exception : input/output problem.
Exception : operation not allowed.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
#define GUM_SHOWERROR(e)
Definition exceptions.h:85
Definition of templatized reader of dat files for Factored Markov Decision Process.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46