aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
DSLReader_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
44#include <agrum/BN/io/DSL/DSLReader.h> // to ease IDE parser
45#ifndef DOXYGEN_SHOULD_SKIP_THIS
46
48
49namespace gum {
50
51 template < GUM_Numeric GUM_SCALAR >
52 DSLReader< GUM_SCALAR >::DSLReader(BayesNet< GUM_SCALAR >* bn, std::string_view filename) :
53 BNReader< GUM_SCALAR >(bn, filename) {
54 GUM_CONSTRUCTOR(DSLReader);
55 _bn_ = bn;
56 _streamName_ = filename;
57 _parseDone_ = false;
58
59 _factory_ = new BayesNetFactory< GUM_SCALAR >(_bn_);
60
61 _ioerror_ = false;
62
63 try {
64 _scanner_ = new DSL::Scanner(_streamName_.c_str());
65 _parser_ = new DSL::Parser(_scanner_);
66 _parser_->setFactory((IBayesNetFactory*)_factory_);
67 } catch (IOError const&) { _ioerror_ = true; }
68 }
69
70 template < GUM_Numeric GUM_SCALAR >
71 DSLReader< GUM_SCALAR >::~DSLReader() {
72 GUM_DESTRUCTOR(DSLReader);
73
74 if (!_ioerror_) {
75 // this could lead to memory leak !!
76 delete (_parser_);
77 delete (_scanner_);
78 }
79 delete (_factory_);
80 }
81
82 template < GUM_Numeric GUM_SCALAR >
83 DSL::Scanner& DSLReader< GUM_SCALAR >::scanner() {
84 if (_ioerror_) { GUM_ERROR(gum::IOError, "No such file " + streamName()) }
85
86 return *_scanner_;
87 }
88
89 template < GUM_Numeric GUM_SCALAR >
90 const std::string& DSLReader< GUM_SCALAR >::streamName() const {
91 return _streamName_;
92 }
93
94 template < GUM_Numeric GUM_SCALAR >
95 bool DSLReader< GUM_SCALAR >::trace() const {
96 return _traceScanning_;
97 }
98
99 template < GUM_Numeric GUM_SCALAR >
100 void DSLReader< GUM_SCALAR >::trace(bool b) {
101 _traceScanning_ = b;
102 scanner().setTrace(b);
103 }
104
105 template < GUM_Numeric GUM_SCALAR >
106 Size DSLReader< GUM_SCALAR >::proceed() {
107 if (_ioerror_) { GUM_ERROR(gum::IOError, "No such file " + streamName()) }
108
109 if (!_parseDone_) {
110 try {
111 _parseDone_ = true;
112 _parser_->Parse();
113 } catch (gum::Exception& e) {
114 GUM_SHOWERROR(e);
115 return 1 + _parser_->errors().error_count;
116 }
117 }
118
119 return (_parser_->errors().error_count);
120 }
121
124 template < GUM_Numeric GUM_SCALAR >
125 Idx DSLReader< GUM_SCALAR >::errLine(Idx i) {
126 if (_parseDone_) return _parser_->errors().error(i).line;
127 else { GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet") }
128 }
129
130 template < GUM_Numeric GUM_SCALAR >
131 Idx DSLReader< GUM_SCALAR >::errCol(Idx i) {
132 if (_parseDone_) return _parser_->errors().error(i).column;
133 else { GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet") }
134 }
135
136 template < GUM_Numeric GUM_SCALAR >
137 bool DSLReader< GUM_SCALAR >::errIsError(Idx i) {
138 if (_parseDone_) return _parser_->errors().error(i).is_error;
139 else { GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet") }
140 }
141
142 template < GUM_Numeric GUM_SCALAR >
143 std::string DSLReader< GUM_SCALAR >::errMsg(Idx i) {
144 if (_parseDone_) return _parser_->errors().error(i).msg;
145 else { GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet") }
146 }
147
148 template < GUM_Numeric GUM_SCALAR >
149 void DSLReader< GUM_SCALAR >::showElegantErrors(std::ostream& o) {
150 if (_parseDone_) _parser_->errors().elegantErrors(o);
151 else { GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet") }
152 }
153
154 template < GUM_Numeric GUM_SCALAR >
155 void DSLReader< GUM_SCALAR >::showElegantErrorsAndWarnings(std::ostream& o) {
156 if (_parseDone_) _parser_->errors().elegantErrorsAndWarnings(o);
157 else { GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet") }
158 }
159
160 template < GUM_Numeric GUM_SCALAR >
161 void DSLReader< GUM_SCALAR >::showErrorsAndWarnings(std::ostream& o) {
162 if (_parseDone_) _parser_->errors().simpleErrorsAndWarnings(o);
163 else { GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet") }
164 }
165
166 template < GUM_Numeric GUM_SCALAR >
167 void DSLReader< GUM_SCALAR >::showErrorCounts(std::ostream& o) {
168 if (_parseDone_) _parser_->errors().syntheticResults(o);
169 else { GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet") }
170 }
171
172 template < GUM_Numeric GUM_SCALAR >
173 Size DSLReader< GUM_SCALAR >::errors() {
174 return (!_parseDone_) ? (Size)0 : _parser_->errors().error_count;
175 }
176
177 template < GUM_Numeric GUM_SCALAR >
178 Size DSLReader< GUM_SCALAR >::warnings() {
179 return (!_parseDone_) ? (Size)0 : _parser_->errors().warning_count;
180 }
181
183} // namespace gum
184
185#endif // DOXYGEN_SHOULD_SKIP_THIS
Pure virtual class for reading a BN from a file.
Definition BNReader.h:78
Class representing a Bayesian network.
Definition BayesNet.h:99
DSLReader(BayesNet< GUM_SCALAR > *bn, std::string_view filename)
Constructor A reader is defined for reading a defined file.
Base class for all aGrUM's exceptions.
Definition exceptions.h:122
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
gum is the global namespace for all aGrUM entities
Definition agrum.h:46