aGrUM 3.1.1
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-2026 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-2026 *
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
41#pragma once
42
43
52
53#include <agrum/FMDP/io/dat/fmdpDatReader.h> // to ease IDE parser
54#ifndef DOXYGEN_SHOULD_SKIP_THIS
55
57
58namespace gum {
59
60 template < typename GUM_ELEMENT >
62 std::string_view filename) :
63 FMDPReader< GUM_ELEMENT >(fmdp, filename) {
64 GUM_CONSTRUCTOR(FMDPDatReader);
65
66 _fmdp_ = fmdp;
67 _streamName_ = filename;
68 _parseDone_ = false;
69 // ddf->putOnNoVariableCheckMode();
70 _factory_ = new FMDPFactory< GUM_ELEMENT >(_fmdp_);
71 //~ _factory_->setVerbose();
72 _ioerror_ = false;
73 _scanner_ = nullptr;
74 _parser_ = nullptr;
75
76 try {
77 _scanner_ = new MDPDAT::Scanner(_streamName_.c_str());
78 _parser_ = new MDPDAT::Parser(_scanner_);
79 _parser_->setFactory((AbstractFMDPFactory*)_factory_);
80 } catch (const IOError&) { _ioerror_ = true; }
81 }
82
83 template < typename GUM_ELEMENT >
84 FMDPDatReader< GUM_ELEMENT >::~FMDPDatReader() {
85 GUM_DESTRUCTOR(FMDPDatReader);
86
87 if (_parser_) delete (_parser_);
88 if (_scanner_) delete (_scanner_);
89
90 if (_factory_) delete (_factory_);
91 }
92
93 template < typename GUM_ELEMENT >
94 MDPDAT::Scanner& FMDPDatReader< GUM_ELEMENT >::scanner() {
95 if (_ioerror_) { GUM_ERROR(gum::IOError, "No such file " + streamName()) }
96
97 return *_scanner_;
98 }
99
100 template < typename GUM_ELEMENT >
101 const std::string& FMDPDatReader< GUM_ELEMENT >::streamName() const {
102 return _streamName_;
103 }
104
105 template < typename GUM_ELEMENT >
106 bool FMDPDatReader< GUM_ELEMENT >::trace() const {
107 return _traceScanning_;
108 }
109
110 template < typename GUM_ELEMENT >
111 void FMDPDatReader< GUM_ELEMENT >::trace(bool b) {
112 _traceScanning_ = b;
113 scanner().setTrace(b);
114 }
115
116 template < typename GUM_ELEMENT >
117 Size FMDPDatReader< GUM_ELEMENT >::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_ELEMENT >
136 Idx FMDPDatReader< GUM_ELEMENT >::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_ELEMENT >
142 Idx FMDPDatReader< GUM_ELEMENT >::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_ELEMENT >
148 bool FMDPDatReader< GUM_ELEMENT >::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_ELEMENT >
154 std::string FMDPDatReader< GUM_ELEMENT >::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_ELEMENT >
160 void FMDPDatReader< GUM_ELEMENT >::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_ELEMENT >
166 void FMDPDatReader< GUM_ELEMENT >::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_ELEMENT >
172 void FMDPDatReader< GUM_ELEMENT >::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_ELEMENT >
178 Size FMDPDatReader< GUM_ELEMENT >::errors() {
179 return (!_parseDone_) ? (Size)0 : _parser_->errors().error_count;
180 }
181
182 template < typename GUM_ELEMENT >
183 Size FMDPDatReader< GUM_ELEMENT >::warnings() {
184 return (!_parseDone_) ? (Size)0 : _parser_->errors().warning_count;
185 }
186
188
189
190} // namespace gum
191
192#endif // DOXYGEN_SHOULD_SKIP_THIS
Base class for all aGrUM's exceptions.
Definition exceptions.h:122
FMDPDatReader(FMDP< GUM_ELEMENT > *fmdp, std::string_view filename)
Pure virtual class for reading a FMDP from a file.
Definition fmdpReader.h:83
This class is used to implement factored decision process.
Definition fmdp.h:75
Exception : input/output problem.
Exception : operation not allowed.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
#define GUM_SHOWERROR(e)
Definition exceptions.h:89
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