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