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