aGrUM  0.16.0
BIFReader_tpl.h
Go to the documentation of this file.
1 
32 #include <agrum/BN/io/BNReader.h>
33 #ifndef DOXYGEN_SHOULD_SKIP_THIS
34 
35 namespace gum {
36 
37  template < typename GUM_SCALAR >
38  BIFReader< GUM_SCALAR >::BIFReader(BayesNet< GUM_SCALAR >* bn,
39  const std::string& filename) :
40  BNReader< GUM_SCALAR >(bn, filename) {
41  GUM_CONSTRUCTOR(BIFReader);
42  __bn = bn;
43  __streamName = filename;
44  __parseDone = false;
45 
46  __factory = new BayesNetFactory< GUM_SCALAR >(__bn);
47 
48  __ioerror = false;
49 
50  try {
51  __scanner = new BIF::Scanner(__streamName.c_str());
52  __parser = new BIF::Parser(__scanner);
53  __parser->setFactory((IBayesNetFactory*)__factory);
54  } catch (IOError&) { __ioerror = true; }
55  }
56 
57  template < typename GUM_SCALAR >
59  GUM_DESTRUCTOR(BIFReader);
60 
61  if (!__ioerror) {
62  // this could lead to memory leak !!
63  if (__parser) delete (__parser);
64 
65  if (__scanner) delete (__scanner);
66  }
67 
68  if (__factory) delete (__factory);
69  }
70 
71  template < typename GUM_SCALAR >
72  INLINE BIF::Scanner& BIFReader< GUM_SCALAR >::scanner() {
73  if (__ioerror) { GUM_ERROR(gum::IOError, "No such file " + streamName()); }
74 
75  return *__scanner;
76  }
77 
78  template < typename GUM_SCALAR >
79  INLINE const std::string& BIFReader< GUM_SCALAR >::streamName() const {
80  return __streamName;
81  }
82 
83  template < typename GUM_SCALAR >
84  INLINE bool BIFReader< GUM_SCALAR >::trace() const {
85  return __traceScanning;
86  }
87 
88  template < typename GUM_SCALAR >
89  INLINE void BIFReader< GUM_SCALAR >::trace(bool b) {
90  __traceScanning = b;
91  scanner().setTrace(b);
92  }
93 
94  template < typename GUM_SCALAR >
96  if (__ioerror) { GUM_ERROR(gum::IOError, "No such file " + streamName()); }
97 
98  if (!__parseDone) {
99  try {
100  __parser->Parse();
101  __parseDone = true;
102  } catch (gum::Exception& e) {
103  GUM_SHOWERROR(e);
104  return 1 + __parser->errors().error_count;
105  }
106  }
107 
108  return (__parser->errors().error_count);
109  }
110 
113  template < typename GUM_SCALAR >
115  if (__parseDone)
116  return __parser->errors().error(i).line;
117  else {
118  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet");
119  }
120  }
121 
122  template < typename GUM_SCALAR >
124  if (__parseDone)
125  return __parser->errors().error(i).column;
126  else {
127  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet");
128  }
129  }
130 
131  template < typename GUM_SCALAR >
133  if (__parseDone)
134  return __parser->errors().error(i).is_error;
135  else {
136  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet");
137  }
138  }
139 
140  template < typename GUM_SCALAR >
141  INLINE std::string BIFReader< GUM_SCALAR >::errMsg(Idx i) {
142  if (__parseDone)
143  return __parser->errors().error(i).msg;
144  else {
145  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet");
146  }
147  }
148 
149  template < typename GUM_SCALAR >
150  INLINE void BIFReader< GUM_SCALAR >::showElegantErrors(std::ostream& o) {
151  if (__parseDone)
152  __parser->errors().elegantErrors(o);
153  else {
154  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet");
155  }
156  }
157 
158  template < typename GUM_SCALAR >
159  INLINE void
161  if (__parseDone)
162  __parser->errors().elegantErrorsAndWarnings(o);
163  else {
164  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet");
165  }
166  }
167 
168  template < typename GUM_SCALAR >
169  INLINE void BIFReader< GUM_SCALAR >::showErrorCounts(std::ostream& o) {
170  if (__parseDone)
171  __parser->errors().syntheticResults(o);
172  else {
173  GUM_ERROR(OperationNotAllowed, "BIF file not parsed yet");
174  }
175  }
176 
177  template < typename GUM_SCALAR >
179  return (!__parseDone) ? (Size)0 : __parser->errors().error_count;
180  }
181 
182  template < typename GUM_SCALAR >
184  return (!__parseDone) ? (Size)0 : __parser->errors().warning_count;
185  }
186 
188 } // namespace gum
189 
190 #endif // DOXYGEN_SHOULD_SKIP_THIS
BayesNet< GUM_SCALAR > * __bn
Definition: BIFReader.h:176
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:61
std::string errMsg(Idx i)
message of ith error or warning
BayesNetFactory< GUM_SCALAR > * __factory
Definition: BIFReader.h:177
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:179
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
void showElegantErrors(std::ostream &o=std::cerr)
send on std::cerr the list of errors
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Size proceed() final
parse.
BIF::Scanner * __scanner
Definition: BIFReader.h:178
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
~BIFReader() final
Idx errCol(Idx i)
col of ith error or warning
bool __parseDone
Definition: BIFReader.h:183
Base class for all aGrUM&#39;s exceptions.
Definition: exceptions.h:106
std::string __streamName
Definition: BIFReader.h:181
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:48
bool __traceScanning
Definition: BIFReader.h:182
Idx errLine(Idx i)
line of ith error or warning
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
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