aGrUM  0.13.2
BIFReader_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
29 #include <agrum/BN/io/BNReader.h>
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 
32 namespace gum {
33 
34  template < typename GUM_SCALAR >
35  BIFReader< GUM_SCALAR >::BIFReader(BayesNet< GUM_SCALAR >* bn,
36  const std::string& filename) :
37  BNReader< GUM_SCALAR >(bn, filename) {
38  GUM_CONSTRUCTOR(BIFReader);
39  __bn = bn;
40  __streamName = filename;
41  __parseDone = false;
42 
43  __factory = new BayesNetFactory< GUM_SCALAR >(__bn);
44 
45  __ioerror = false;
46 
47  try {
48  __scanner = new BIF::Scanner(__streamName.c_str());
49  __parser = new BIF::Parser(__scanner);
50  __parser->setFactory((IBayesNetFactory*)__factory);
51  } catch (IOError& e) { __ioerror = true; }
52  }
53 
54  template < typename GUM_SCALAR >
56  GUM_DESTRUCTOR(BIFReader);
57 
58  if (!__ioerror) {
59  // this could lead to memory leak !!
60  if (__parser) delete (__parser);
61 
62  if (__scanner) delete (__scanner);
63  }
64 
65  if (__factory) delete (__factory);
66  }
67 
68  template < typename GUM_SCALAR >
69  INLINE BIF::Scanner& BIFReader< GUM_SCALAR >::scanner() {
70  if (__ioerror) { GUM_ERROR(gum::IOError, "No such file " + streamName()); }
71 
72  return *__scanner;
73  }
74 
75  template < typename GUM_SCALAR >
76  INLINE const std::string& BIFReader< GUM_SCALAR >::streamName() const {
77  return __streamName;
78  }
79 
80  template < typename GUM_SCALAR >
81  INLINE bool BIFReader< GUM_SCALAR >::trace() const {
82  return __traceScanning;
83  }
84 
85  template < typename GUM_SCALAR >
86  INLINE void BIFReader< GUM_SCALAR >::trace(bool b) {
87  __traceScanning = b;
88  scanner().setTrace(b);
89  }
90 
91  template < typename GUM_SCALAR >
93  if (__ioerror) { GUM_ERROR(gum::IOError, "No such file " + streamName()); }
94 
95  if (!__parseDone) {
96  try {
97  __parser->Parse();
98  } catch (gum::Exception& e) {
99  GUM_SHOWERROR(e);
100  return 1 + __parser->errors().error_count;
101  }
102 
103  __parseDone = true;
104  }
105 
106  return (__parser->errors().error_count);
107  }
108 
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 >
139  INLINE std::string BIFReader< GUM_SCALAR >::errMsg(Idx i) {
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 >
148  INLINE void BIFReader< GUM_SCALAR >::showElegantErrors(std::ostream& o) {
149  if (__parseDone)
150  __parser->errors().elegantErrors(o);
151  else {
152  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet");
153  }
154  }
155 
156  template < typename GUM_SCALAR >
157  INLINE void
159  if (__parseDone)
160  __parser->errors().elegantErrorsAndWarnings(o);
161  else {
162  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet");
163  }
164  }
165 
166  template < typename GUM_SCALAR >
167  INLINE void BIFReader< GUM_SCALAR >::showErrorCounts(std::ostream& o) {
168  if (__parseDone)
169  __parser->errors().syntheticResults(o);
170  else {
171  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet");
172  }
173  }
174 
175  template < typename GUM_SCALAR >
177  return (!__parseDone) ? (Size)0 : __parser->errors().error_count;
178  }
179 
180  template < typename GUM_SCALAR >
182  return (!__parseDone) ? (Size)0 : __parser->errors().warning_count;
183  }
184 
186 } // namespace gum
187 
188 #endif // DOXYGEN_SHOULD_SKIP_THIS
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
BayesNet< GUM_SCALAR > * __bn
Definition: BIFReader.h:173
bool errIsError(Idx i)
type of ith error or warning
#define GUM_SHOWERROR(e)
Definition: exceptions.h:73
std::string errMsg(Idx i)
message of ith error or warning
BayesNetFactory< GUM_SCALAR > * __factory
Definition: BIFReader.h:174
void showErrorCounts(std::ostream &o=std::cerr)
send on std::cerr the number of errors and the number of warnings
BIF::Parser * __parser
Definition: BIFReader.h:176
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
void showElegantErrors(std::ostream &o=std::cerr)
send on std::cerr the list of errors
bool trace() const
accessor to trace function (just write the number of parser line)
Definition of templatized reader of BIF files for Bayesian Networks.
Size proceed() final
parse.
const std::string & streamName() const
name of readen file
BIF::Scanner * __scanner
Definition: BIFReader.h:175
Definition of abstract classes for file input manipulation of Bayesian Networks.
~BIFReader() final
Idx errCol(Idx i)
col of ith error or warning
bool __parseDone
Definition: BIFReader.h:180
Base class for all aGrUM&#39;s exceptions.
Definition: exceptions.h:103
std::string __streamName
Definition: BIFReader.h:178
Size warnings()
of errors
Size errors()
publishing Errors API
BIF::Scanner & scanner()
Direct access to BIF scanner (mandatory for listener connection)
bool __traceScanning
Definition: BIFReader.h:179
unsigned long Idx
Type for indexes.
Definition: types.h:43
Idx errLine(Idx i)
line of ith error or warning
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66
BIFReader(BayesNet< GUM_SCALAR > *bn, const std::string &filename)
void showElegantErrorsAndWarnings(std::ostream &o=std::cerr)
send on std::cerr the list of errors or warnings