aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
BIFReader_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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,
38  const std::string& filename) :
41  bn__ = bn;
43  parseDone__ = false;
44 
46 
47  ioerror__ = false;
48 
49  try {
51  parser__ = new BIF::Parser(scanner__);
53  } catch (IOError&) { ioerror__ = true; }
54  }
55 
56  template < typename GUM_SCALAR >
59 
60  if (!ioerror__) {
61  // this could lead to memory leak !!
62  if (parser__) delete (parser__);
63 
64  if (scanner__) delete (scanner__);
65  }
66 
67  if (factory__) delete (factory__);
68  }
69 
70  template < typename GUM_SCALAR >
72  if (ioerror__) { GUM_ERROR(gum::IOError, "No such file " + streamName()); }
73 
74  return *scanner__;
75  }
76 
77  template < typename GUM_SCALAR >
78  INLINE const std::string& BIFReader< GUM_SCALAR >::streamName() const {
79  return streamName__;
80  }
81 
82  template < typename GUM_SCALAR >
83  INLINE bool BIFReader< GUM_SCALAR >::trace() const {
84  return traceScanning__;
85  }
86 
87  template < typename GUM_SCALAR >
88  INLINE void BIFReader< GUM_SCALAR >::trace(bool b) {
90  scanner().setTrace(b);
91  }
92 
93  template < typename GUM_SCALAR >
95  if (ioerror__) { GUM_ERROR(gum::IOError, "No such file " + streamName()); }
96 
97  if (!parseDone__) {
98  try {
99  parser__->Parse();
100  parseDone__ = true;
101  } catch (gum::Exception& e) {
102  GUM_SHOWERROR(e);
103  return 1 + parser__->errors().error_count;
104  }
105  }
106 
107  return (parser__->errors().error_count);
108  }
109 
110  /// @{
111  /// publishing Errors API
112  template < typename GUM_SCALAR >
114  if (parseDone__)
115  return parser__->errors().error(i).line;
116  else {
117  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet");
118  }
119  }
120 
121  template < typename GUM_SCALAR >
123  if (parseDone__)
124  return parser__->errors().error(i).column;
125  else {
126  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet");
127  }
128  }
129 
130  template < typename GUM_SCALAR >
132  if (parseDone__)
133  return parser__->errors().error(i).is_error;
134  else {
135  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet");
136  }
137  }
138 
139  template < typename GUM_SCALAR >
141  if (parseDone__)
142  return parser__->errors().error(i).msg;
143  else {
144  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet");
145  }
146  }
147 
148  template < typename GUM_SCALAR >
150  if (parseDone__)
152  else {
153  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet");
154  }
155  }
156 
157  template < typename GUM_SCALAR >
158  INLINE void
160  if (parseDone__)
162  else {
163  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet");
164  }
165  }
166 
167  template < typename GUM_SCALAR >
169  if (parseDone__)
171  else {
172  GUM_ERROR(OperationNotAllowed, "BIF 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:669