aGrUM  0.16.0
netReader_tpl.h
Go to the documentation of this file.
1 
23 #ifndef DOXYGEN_SHOULD_SKIP_THIS
24 
26 
27 namespace gum {
28 
29  template < typename GUM_SCALAR >
30  NetReader< GUM_SCALAR >::NetReader(BayesNet< GUM_SCALAR >* bn,
31  const std::string& filename) :
32  BNReader< GUM_SCALAR >(bn, filename) {
33  GUM_CONSTRUCTOR(NetReader);
34  __bn = bn;
35  __streamName = filename;
36  __parseDone = false;
37 
38  __factory = new BayesNetFactory< GUM_SCALAR >(__bn);
39 
40  __ioerror = false;
41 
42  try {
43  __scanner = new net::Scanner(__streamName.c_str());
44  __parser = new net::Parser(__scanner);
45  __parser->setFactory((IBayesNetFactory*)__factory);
46  } catch (IOError&) { __ioerror = true; }
47  }
48 
49  template < typename GUM_SCALAR >
51  GUM_DESTRUCTOR(NetReader);
52 
53  if (!__ioerror) {
54  // this could lead to memory leak !!
55  if (__parser) delete (__parser);
56 
57  if (__scanner) delete (__scanner);
58  }
59 
60  if (__factory) delete (__factory);
61  }
62 
63  template < typename GUM_SCALAR >
64  INLINE net::Scanner& NetReader< GUM_SCALAR >::scanner() {
65  if (__ioerror) { GUM_ERROR(gum::IOError, "No such file " + streamName()); }
66 
67  return *__scanner;
68  }
69 
70  template < typename GUM_SCALAR >
71  INLINE const std::string& NetReader< GUM_SCALAR >::streamName() const {
72  return __streamName;
73  }
74 
75  template < typename GUM_SCALAR >
76  INLINE bool NetReader< GUM_SCALAR >::trace() const {
77  return __traceScanning;
78  }
79 
80  template < typename GUM_SCALAR >
81  INLINE void NetReader< GUM_SCALAR >::trace(bool b) {
82  __traceScanning = b;
83  scanner().setTrace(b);
84  }
85 
86  template < typename GUM_SCALAR >
88  if (__ioerror) { GUM_ERROR(gum::IOError, "No such file " + streamName()); }
89 
90  if (!__parseDone) {
91  try {
92  __parser->Parse();
93  } catch (gum::Exception& e) {
94  GUM_SHOWERROR(e);
95  return 1 + __parser->errors().error_count;
96  }
97 
98  __parseDone = true;
99  }
100 
101  return (__parser->errors().error_count);
102  }
103 
104  // @{
105  // publishing Errors API
106  template < typename GUM_SCALAR >
108  if (__parseDone)
109  return __parser->errors().error(i).line;
110  else {
111  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
112  }
113  }
114 
115  template < typename GUM_SCALAR >
117  if (__parseDone)
118  return __parser->errors().error(i).column;
119  else {
120  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
121  }
122  }
123 
124  template < typename GUM_SCALAR >
126  if (__parseDone)
127  return __parser->errors().error(i).is_error;
128  else {
129  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
130  }
131  }
132 
133  template < typename GUM_SCALAR >
134  INLINE std::string NetReader< GUM_SCALAR >::errMsg(Idx i) {
135  if (__parseDone)
136  return __parser->errors().error(i).msg;
137  else {
138  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
139  }
140  }
141 
142  template < typename GUM_SCALAR >
143  INLINE void NetReader< GUM_SCALAR >::showElegantErrors(std::ostream& o) {
144  if (__parseDone)
145  __parser->errors().elegantErrors(o);
146  else {
147  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
148  }
149  }
150 
151  template < typename GUM_SCALAR >
152  INLINE void
154  if (__parseDone)
155  __parser->errors().elegantErrorsAndWarnings(o);
156  else {
157  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
158  }
159  }
160 
161  template < typename GUM_SCALAR >
162  INLINE void NetReader< GUM_SCALAR >::showErrorsAndWarnings(std::ostream& o) {
163  if (__parseDone)
164  __parser->errors().simpleErrorsAndWarnings(o);
165  else {
166  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
167  }
168  }
169 
170  template < typename GUM_SCALAR >
171  INLINE void NetReader< GUM_SCALAR >::showErrorCounts(std::ostream& o) {
172  if (__parseDone)
173  __parser->errors().syntheticResults(o);
174  else {
175  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
176  }
177  }
178 
179  template < typename GUM_SCALAR >
181  return (!__parseDone) ? (Size)0 : __parser->errors().error_count;
182  }
183 
184  template < typename GUM_SCALAR >
186  return (!__parseDone) ? (Size)0 : __parser->errors().warning_count;
187  }
188 
189  // @}
190 } // namespace gum
191 
192 #endif // DOXYGEN_SHOULD_SKIP_THIS
bool errIsError(Idx i)
type of ith error or warning
void showErrorsAndWarnings(std::ostream &o=std::cerr)
send on std::cerr the list of errors or warnings
BayesNet< GUM_SCALAR > * __bn
Definition: netReader.h:118
#define GUM_SHOWERROR(e)
Definition: exceptions.h:61
net::Scanner * __scanner
Definition: netReader.h:120
net::Parser * __parser
Definition: netReader.h:121
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
bool trace() const
accessor to trace function (just write the number of parser line)
Size warnings()
of errors
std::string errMsg(Idx i)
message of ith error or warning
BayesNetFactory< GUM_SCALAR > * __factory
Definition: netReader.h:119
Size errors()
publishing Errors API
const std::string & streamName() const
name of readen file
Base class for all aGrUM&#39;s exceptions.
Definition: exceptions.h:106
net::Scanner & scanner()
Direct access to DSL scanner (mandatory for listener connection)
std::string __streamName
Definition: netReader.h:123
void showErrorCounts(std::ostream &o=std::cerr)
send on std::cerr the number of errors and the number of warnings
~NetReader() final
Default destructor.
Size proceed() final
parse.
void showElegantErrorsAndWarnings(std::ostream &o=std::cerr)
send on std::cerr the list of errors or warnings with contents
bool __parseDone
Definition: netReader.h:125
bool __traceScanning
Definition: netReader.h:124
void showElegantErrors(std::ostream &o=std::cerr)
send on std::cerr the list of errorswith contents
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
NetReader(BayesNet< GUM_SCALAR > *bn, const std::string &filename)
Constructor A reader is defined for reading a defined file.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
Idx errCol(Idx i)
col of ith error or warning
Idx errLine(Idx i)
line of ith error or warning