aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
BIFReader_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Definition of templatized reader of BIF files for Bayesian networks.
25  *
26  * @author Pierre-Henri WUILLEMIN
27  */
28 
29 
30 #include <agrum/BN/io/BIF/BIFReader.h>
31 #include <agrum/BN/io/BNReader.h>
32 #ifndef DOXYGEN_SHOULD_SKIP_THIS
33 
34 namespace gum {
35 
36  template < typename GUM_SCALAR >
37  BIFReader< GUM_SCALAR >::BIFReader(BayesNet< GUM_SCALAR >* bn, const std::string& filename) :
40  _bn_ = bn;
42  _parseDone_ = false;
43 
45 
46  _ioerror_ = false;
47 
48  try {
50  _parser_ = new BIF::Parser(_scanner_);
52  } catch (IOError&) { _ioerror_ = true; }
53  }
54 
55  template < typename GUM_SCALAR >
58 
59  if (!_ioerror_) {
60  // this could lead to memory leak !!
61  if (_parser_) delete (_parser_);
62 
63  if (_scanner_) delete (_scanner_);
64  }
65 
66  if (_factory_) delete (_factory_);
67  }
68 
69  template < typename GUM_SCALAR >
71  if (_ioerror_) { GUM_ERROR(gum::IOError, "No such file " + streamName()) }
72 
73  return *_scanner_;
74  }
75 
76  template < typename GUM_SCALAR >
77  INLINE const std::string& BIFReader< GUM_SCALAR >::streamName() const {
78  return _streamName_;
79  }
80 
81  template < typename GUM_SCALAR >
82  INLINE bool BIFReader< GUM_SCALAR >::trace() const {
83  return _traceScanning_;
84  }
85 
86  template < typename GUM_SCALAR >
87  INLINE void BIFReader< GUM_SCALAR >::trace(bool b) {
89  scanner().setTrace(b);
90  }
91 
92  template < typename GUM_SCALAR >
94  if (_ioerror_) { GUM_ERROR(gum::IOError, "No such file " + streamName()) }
95 
96  if (!_parseDone_) {
97  try {
98  _parser_->Parse();
99  _parseDone_ = true;
100  } catch (gum::Exception& e) {
101  GUM_SHOWERROR(e);
102  return 1 + _parser_->errors().error_count;
103  }
104  }
105 
106  return (_parser_->errors().error_count);
107  }
108 
109  /// @{
110  /// publishing Errors API
111  template < typename GUM_SCALAR >
113  if (_parseDone_)
114  return _parser_->errors().error(i).line;
115  else {
116  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet")
117  }
118  }
119 
120  template < typename GUM_SCALAR >
122  if (_parseDone_)
123  return _parser_->errors().error(i).column;
124  else {
125  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet")
126  }
127  }
128 
129  template < typename GUM_SCALAR >
131  if (_parseDone_)
132  return _parser_->errors().error(i).is_error;
133  else {
134  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet")
135  }
136  }
137 
138  template < typename GUM_SCALAR >
140  if (_parseDone_)
141  return _parser_->errors().error(i).msg;
142  else {
143  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet")
144  }
145  }
146 
147  template < typename GUM_SCALAR >
149  if (_parseDone_)
151  else {
152  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet")
153  }
154  }
155 
156  template < typename GUM_SCALAR >
158  if (_parseDone_)
160  else {
161  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet")
162  }
163  }
164 
165  template < typename GUM_SCALAR >
167  if (_parseDone_)
169  else {
170  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet")
171  }
172  }
173 
174  template < typename GUM_SCALAR >
176  return (!_parseDone_) ? (Size)0 : _parser_->errors().error_count;
177  }
178 
179  template < typename GUM_SCALAR >
181  return (!_parseDone_) ? (Size)0 : _parser_->errors().warning_count;
182  }
183 
184  /// @}
185 } // namespace gum
186 
187 #endif // DOXYGEN_SHOULD_SKIP_THIS
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643