aGrUM  0.14.2
fmdpDatReader_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  ***************************************************************************/
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 
31 
32 namespace gum {
33 
34  template < typename GUM_SCALAR >
35  FMDPDatReader< GUM_SCALAR >::FMDPDatReader(FMDP< GUM_SCALAR >* fmdp,
36  const std::string& filename) :
37  FMDPReader< GUM_SCALAR >(fmdp, filename) {
38  GUM_CONSTRUCTOR(FMDPDatReader);
39 
40  __fmdp = fmdp;
41  __streamName = filename;
42  __parseDone = false;
43  // ddf->putOnNoVariableCheckMode();
44  __factory = new FMDPFactory< GUM_SCALAR >(__fmdp);
45  //~ __factory->setVerbose();
46  __ioerror = false;
47 
48  try {
49  __scanner = new MDPDAT::Scanner(__streamName.c_str());
50  __parser = new MDPDAT::Parser(__scanner);
51  __parser->setFactory((AbstractFMDPFactory*)__factory);
52  } catch (IOError e) { __ioerror = true; }
53  }
54 
55  template < typename GUM_SCALAR >
57  GUM_DESTRUCTOR(FMDPDatReader);
58 
59  if (!__ioerror) {
60  // this could lead to memory leak !!
61  if (__parser) delete (__parser);
62 
63  if (__scanner) delete (__scanner);
64  }
65 
66  if (__factory) delete (__factory);
67  }
68 
69  template < typename GUM_SCALAR >
70  INLINE MDPDAT::Scanner& FMDPDatReader< GUM_SCALAR >::scanner() {
71  if (__ioerror) { GUM_ERROR(gum::IOError, "No such file " + streamName()); }
72 
73  return *__scanner;
74  }
75 
76  template < typename GUM_SCALAR >
77  INLINE const std::string& FMDPDatReader< GUM_SCALAR >::streamName() const {
78  return __streamName;
79  }
80 
81  template < typename GUM_SCALAR >
82  INLINE bool FMDPDatReader< GUM_SCALAR >::trace() const {
83  return __traceScanning;
84  }
85 
86  template < typename GUM_SCALAR >
87  INLINE void FMDPDatReader< GUM_SCALAR >::trace(bool b) {
88  __traceScanning = b;
89  scanner().setTrace(b);
90  }
91 
92  template < typename GUM_SCALAR >
94  if (__ioerror) { GUM_ERROR(gum::IOError, "No such file " + streamName()); }
95 
96  if (!__parseDone) {
97  try {
98  __parser->Parse();
99  __parseDone = true;
100  } catch (gum::Exception& e) {
101  GUM_SHOWERROR(e);
102  return 1 + __parser->errors().error_count;
103  }
104  }
105 
106  return (__parser->errors().error_count);
107  }
108 
111  template < typename GUM_SCALAR >
113  if (__parseDone)
114  return __parser->errors().error(i).line;
115  else
116  GUM_ERROR(OperationNotAllowed, "FMDPDat file not parsed yet");
117  }
118 
119  template < typename GUM_SCALAR >
121  if (__parseDone)
122  return __parser->errors().error(i).column;
123  else
124  GUM_ERROR(OperationNotAllowed, "FMDPDat file not parsed yet");
125  }
126 
127  template < typename GUM_SCALAR >
129  if (__parseDone)
130  return __parser->errors().error(i).is_error;
131  else
132  GUM_ERROR(OperationNotAllowed, "FMDPDat file not parsed yet");
133  }
134 
135  template < typename GUM_SCALAR >
136  INLINE std::string FMDPDatReader< GUM_SCALAR >::errMsg(Idx i) {
137  if (__parseDone)
138  return __parser->errors().error(i).msg;
139  else
140  GUM_ERROR(OperationNotAllowed, "FMDPDat file not parsed yet");
141  }
142 
143  template < typename GUM_SCALAR >
144  INLINE void FMDPDatReader< GUM_SCALAR >::showElegantErrors(std::ostream& o) {
145  if (__parseDone)
146  __parser->errors().elegantErrors(o);
147  else
148  GUM_ERROR(OperationNotAllowed, "FMDPDat file not parsed yet");
149  }
150 
151  template < typename GUM_SCALAR >
152  INLINE void
154  if (__parseDone)
155  __parser->errors().elegantErrorsAndWarnings(o);
156  else
157  GUM_ERROR(OperationNotAllowed, "FMDPDat file not parsed yet");
158  }
159 
160  template < typename GUM_SCALAR >
161  INLINE void FMDPDatReader< GUM_SCALAR >::showErrorCounts(std::ostream& o) {
162  if (__parseDone)
163  __parser->errors().syntheticResults(o);
164  else
165  GUM_ERROR(OperationNotAllowed, "FMDPDat file not parsed yet");
166  }
167 
168  template < typename GUM_SCALAR >
170  return (!__parseDone) ? (Size)0 : __parser->errors().error_count;
171  }
172 
173  template < typename GUM_SCALAR >
175  return (!__parseDone) ? (Size)0 : __parser->errors().warning_count;
176  }
177 
179 } // namespace gum
180 
181 #endif // DOXYGEN_SHOULD_SKIP_THIS
bool errIsError(Idx i)
type of ith error or warning
FMDPDatReader(FMDP< GUM_SCALAR > *fmdp, const std::string &filename)
const std::string & streamName() const
name of readen file
Size errors()
publishing Errors API
#define GUM_SHOWERROR(e)
Definition: exceptions.h:58
FMDPFactory< GUM_SCALAR > * __factory
FMDP< GUM_SCALAR > * __fmdp
MDPDAT::Parser * __parser
Idx errCol(Idx i)
col of ith error or warning
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
void showElegantErrorsAndWarnings(std::ostream &o=std::cerr)
send on std::cerr the list of errors or warnings
MDPDAT::Scanner & scanner()
Direct access to FMDPDat scanner (mandatory for listener connection)
Size warnings()
of errors
Base class for all aGrUM&#39;s exceptions.
Definition: exceptions.h:103
void showElegantErrors(std::ostream &o=std::cerr)
send on std::cerr the list of errors
MDPDAT::Scanner * __scanner
void showErrorCounts(std::ostream &o=std::cerr)
send on std::cerr the number of errors and the number of warnings
std::string errMsg(Idx i)
message of ith error or warning
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
bool trace() const
accessor to trace function (just write the number of parser line)
Size proceed()
parse.
Definition of templatized reader of dat files for Factored Markov Decision Process.
std::string __streamName
Idx errLine(Idx i)
line of ith error or warning
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52