aGrUM  0.14.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&) { __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  __parseDone = true;
99  } catch (gum::Exception& e) {
100  GUM_SHOWERROR(e);
101  return 1 + __parser->errors().error_count;
102  }
103  }
104 
105  return (__parser->errors().error_count);
106  }
107 
110  template < typename GUM_SCALAR >
112  if (__parseDone)
113  return __parser->errors().error(i).line;
114  else {
115  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet");
116  }
117  }
118 
119  template < typename GUM_SCALAR >
121  if (__parseDone)
122  return __parser->errors().error(i).column;
123  else {
124  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet");
125  }
126  }
127 
128  template < typename GUM_SCALAR >
130  if (__parseDone)
131  return __parser->errors().error(i).is_error;
132  else {
133  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet");
134  }
135  }
136 
137  template < typename GUM_SCALAR >
138  INLINE std::string BIFReader< GUM_SCALAR >::errMsg(Idx i) {
139  if (__parseDone)
140  return __parser->errors().error(i).msg;
141  else {
142  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet");
143  }
144  }
145 
146  template < typename GUM_SCALAR >
147  INLINE void BIFReader< GUM_SCALAR >::showElegantErrors(std::ostream& o) {
148  if (__parseDone)
149  __parser->errors().elegantErrors(o);
150  else {
151  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet");
152  }
153  }
154 
155  template < typename GUM_SCALAR >
156  INLINE void
158  if (__parseDone)
159  __parser->errors().elegantErrorsAndWarnings(o);
160  else {
161  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet");
162  }
163  }
164 
165  template < typename GUM_SCALAR >
166  INLINE void BIFReader< GUM_SCALAR >::showErrorCounts(std::ostream& o) {
167  if (__parseDone)
168  __parser->errors().syntheticResults(o);
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 
185 } // namespace gum
186 
187 #endif // DOXYGEN_SHOULD_SKIP_THIS
BayesNet< GUM_SCALAR > * __bn
Definition: BIFReader.h:173
const std::string & streamName() const
name of readen file
bool errIsError(Idx i)
type of ith error or warning
#define GUM_SHOWERROR(e)
Definition: exceptions.h:58
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
Definition of templatized reader of BIF files for Bayesian Networks.
Size proceed() final
parse.
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
bool trace() const
accessor to trace function (just write the number of parser line)
Size warnings()
of errors
Size errors()
publishing Errors API
BIF::Scanner & scanner()
Direct access to BIF scanner (mandatory for listener connection)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
bool __traceScanning
Definition: BIFReader.h:179
Idx errLine(Idx i)
line of ith error or warning
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
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