aGrUM  0.18.1
a C++ library for (probabilistic) graphical models
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
net::Parser * parser__
Definition: netReader.h:121
#define GUM_SHOWERROR(e)
Definition: exceptions.h:61
net::Scanner * scanner__
Definition: netReader.h:120
BayesNetFactory< GUM_SCALAR > * factory__
Definition: netReader.h:119
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
Definition: agrum.h:25
bool trace() const
accessor to trace function (just write the number of parser line)
Size warnings()
of errors
bool traceScanning__
Definition: netReader.h:124
std::string errMsg(Idx i)
message of ith error or warning
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)
void showErrorCounts(std::ostream &o=std::cerr)
send on std::cerr the number of errors and the number of warnings
~NetReader() final
Default destructor.
BayesNet< GUM_SCALAR > * bn__
Definition: netReader.h:118
Size proceed() final
parse.
std::string streamName__
Definition: netReader.h:123
void showElegantErrorsAndWarnings(std::ostream &o=std::cerr)
send on std::cerr the list of errors or warnings with contents
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.
bool parseDone__
Definition: netReader.h:125
#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