aGrUM  0.14.2
netReader_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  ***************************************************************************/
20 
21 #ifndef DOXYGEN_SHOULD_SKIP_THIS
22 
24 
25 namespace gum {
26 
27  template < typename GUM_SCALAR >
28  NetReader< GUM_SCALAR >::NetReader(BayesNet< GUM_SCALAR >* bn,
29  const std::string& filename) :
30  BNReader< GUM_SCALAR >(bn, filename) {
31  GUM_CONSTRUCTOR(NetReader);
32  __bn = bn;
33  __streamName = filename;
34  __parseDone = false;
35 
36  __factory = new BayesNetFactory< GUM_SCALAR >(__bn);
37 
38  __ioerror = false;
39 
40  try {
41  __scanner = new net::Scanner(__streamName.c_str());
42  __parser = new net::Parser(__scanner);
43  __parser->setFactory((IBayesNetFactory*)__factory);
44  } catch (IOError&) { __ioerror = true; }
45  }
46 
47  template < typename GUM_SCALAR >
49  GUM_DESTRUCTOR(NetReader);
50 
51  if (!__ioerror) {
52  // this could lead to memory leak !!
53  if (__parser) delete (__parser);
54 
55  if (__scanner) delete (__scanner);
56  }
57 
58  if (__factory) delete (__factory);
59  }
60 
61  template < typename GUM_SCALAR >
62  INLINE net::Scanner& NetReader< GUM_SCALAR >::scanner() {
63  if (__ioerror) { GUM_ERROR(gum::IOError, "No such file " + streamName()); }
64 
65  return *__scanner;
66  }
67 
68  template < typename GUM_SCALAR >
69  INLINE const std::string& NetReader< GUM_SCALAR >::streamName() const {
70  return __streamName;
71  }
72 
73  template < typename GUM_SCALAR >
74  INLINE bool NetReader< GUM_SCALAR >::trace() const {
75  return __traceScanning;
76  }
77 
78  template < typename GUM_SCALAR >
79  INLINE void NetReader< GUM_SCALAR >::trace(bool b) {
80  __traceScanning = b;
81  scanner().setTrace(b);
82  }
83 
84  template < typename GUM_SCALAR >
86  if (__ioerror) { GUM_ERROR(gum::IOError, "No such file " + streamName()); }
87 
88  if (!__parseDone) {
89  try {
90  __parser->Parse();
91  } catch (gum::Exception& e) {
92  GUM_SHOWERROR(e);
93  return 1 + __parser->errors().error_count;
94  }
95 
96  __parseDone = true;
97  }
98 
99  return (__parser->errors().error_count);
100  }
101 
102  // @{
103  // publishing Errors API
104  template < typename GUM_SCALAR >
106  if (__parseDone)
107  return __parser->errors().error(i).line;
108  else {
109  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
110  }
111  }
112 
113  template < typename GUM_SCALAR >
115  if (__parseDone)
116  return __parser->errors().error(i).column;
117  else {
118  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
119  }
120  }
121 
122  template < typename GUM_SCALAR >
124  if (__parseDone)
125  return __parser->errors().error(i).is_error;
126  else {
127  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
128  }
129  }
130 
131  template < typename GUM_SCALAR >
132  INLINE std::string NetReader< GUM_SCALAR >::errMsg(Idx i) {
133  if (__parseDone)
134  return __parser->errors().error(i).msg;
135  else {
136  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
137  }
138  }
139 
140  template < typename GUM_SCALAR >
141  INLINE void NetReader< GUM_SCALAR >::showElegantErrors(std::ostream& o) {
142  if (__parseDone)
143  __parser->errors().elegantErrors(o);
144  else {
145  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
146  }
147  }
148 
149  template < typename GUM_SCALAR >
150  INLINE void
152  if (__parseDone)
153  __parser->errors().elegantErrorsAndWarnings(o);
154  else {
155  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
156  }
157  }
158 
159  template < typename GUM_SCALAR >
160  INLINE void NetReader< GUM_SCALAR >::showErrorsAndWarnings(std::ostream& o) {
161  if (__parseDone)
162  __parser->errors().simpleErrorsAndWarnings(o);
163  else {
164  GUM_ERROR(OperationNotAllowed, "Net file not parsed yet");
165  }
166  }
167 
168  template < typename GUM_SCALAR >
169  INLINE void NetReader< GUM_SCALAR >::showErrorCounts(std::ostream& o) {
170  if (__parseDone)
171  __parser->errors().syntheticResults(o);
172  else {
173  GUM_ERROR(OperationNotAllowed, "Net 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 
187  // @}
188 } // namespace gum
189 
190 #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:115
#define GUM_SHOWERROR(e)
Definition: exceptions.h:58
net::Scanner * __scanner
Definition: netReader.h:117
net::Parser * __parser
Definition: netReader.h:118
gum is the global namespace for all aGrUM entities
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:116
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:103
net::Scanner & scanner()
Direct access to DSL scanner (mandatory for listener connection)
std::string __streamName
Definition: netReader.h:120
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:122
bool __traceScanning
Definition: netReader.h:121
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:45
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:52
Idx errCol(Idx i)
col of ith error or warning
Idx errLine(Idx i)
line of ith error or warning