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