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