aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
netReader_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 #ifndef DOXYGEN_SHOULD_SKIP_THIS
23 
24 # include <agrum/BN/io/net/netReader.h>
25 
26 namespace gum {
27 
28  template < typename GUM_SCALAR >
29  NetReader< GUM_SCALAR >::NetReader(BayesNet< GUM_SCALAR >* bn, const std::string& filename) :
32  _bn_ = bn;
34  _parseDone_ = false;
35 
37 
38  _ioerror_ = false;
39 
40  try {
42  _parser_ = new net::Parser(_scanner_);
44  } catch (IOError&) { _ioerror_ = true; }
45  }
46 
47  template < typename GUM_SCALAR >
50 
51  if (!_ioerror_) {
52  // this could lead to memory leak !!
53  if (_parser_) delete (_parser_);
54 
55  if (_scanner_) delete (_scanner_);
56  }
57 
58  if (_factory_) delete (_factory_);
59  }
60 
61  template < typename GUM_SCALAR >
63  if (_ioerror_) { GUM_ERROR(gum::IOError, "No such file " + streamName()) }
64 
65  return *_scanner_;
66  }
67 
68  template < typename GUM_SCALAR >
69  INLINE const std::string& NetReader< GUM_SCALAR >::streamName() const {
70  return _streamName_;
71  }
72 
73  template < typename GUM_SCALAR >
74  INLINE bool NetReader< GUM_SCALAR >::trace() const {
75  return _traceScanning_;
76  }
77 
78  template < typename GUM_SCALAR >
79  INLINE void NetReader< GUM_SCALAR >::trace(bool b) {
81  scanner().setTrace(b);
82  }
83 
84  template < typename GUM_SCALAR >
86  if (_ioerror_) { GUM_ERROR(gum::IOError, "No such file " + streamName()) }
87 
88  if (!_parseDone_) {
89  try {
90  _parser_->Parse();
91  } catch (gum::Exception& e) {
93  return 1 + _parser_->errors().error_count;
94  }
95 
96  _parseDone_ = true;
97  }
98 
99  return (_parser_->errors().error_count);
100  }
101 
102  // @{
103  // publishing Errors API
104  template < typename GUM_SCALAR >
106  if (_parseDone_)
107  return _parser_->errors().error(i).line;
108  else {
109  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet")
110  }
111  }
112 
113  template < typename GUM_SCALAR >
115  if (_parseDone_)
116  return _parser_->errors().error(i).column;
117  else {
118  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet")
119  }
120  }
121 
122  template < typename GUM_SCALAR >
124  if (_parseDone_)
125  return _parser_->errors().error(i).is_error;
126  else {
127  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet")
128  }
129  }
130 
131  template < typename GUM_SCALAR >
133  if (_parseDone_)
134  return _parser_->errors().error(i).msg;
135  else {
136  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet")
137  }
138  }
139 
140  template < typename GUM_SCALAR >
142  if (_parseDone_)
144  else {
145  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet")
146  }
147  }
148 
149  template < typename GUM_SCALAR >
151  if (_parseDone_)
153  else {
154  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet")
155  }
156  }
157 
158  template < typename GUM_SCALAR >
160  if (_parseDone_)
162  else {
163  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet")
164  }
165  }
166 
167  template < typename GUM_SCALAR >
169  if (_parseDone_)
171  else {
172  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet")
173  }
174  }
175 
176  template < typename GUM_SCALAR >
178  return (!_parseDone_) ? (Size)0 : _parser_->errors().error_count;
179  }
180 
181  template < typename GUM_SCALAR >
183  return (!_parseDone_) ? (Size)0 : _parser_->errors().warning_count;
184  }
185 
186  // @}
187 } // namespace gum
188 
189 #endif // DOXYGEN_SHOULD_SKIP_THIS
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643