aGrUM  0.13.2
UAIReader_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 
23 namespace gum {
24 
25  template < typename GUM_SCALAR >
26  UAIReader< GUM_SCALAR >::UAIReader(BayesNet< GUM_SCALAR >* bn,
27  const std::string& filename) :
28  BNReader< GUM_SCALAR >(bn, filename) {
29  GUM_CONSTRUCTOR(UAIReader);
30  __bn = bn;
31  __streamName = filename;
32  __parseDone = false;
33 
34  __ioerror = false;
35 
36  try {
37  __scanner = new UAI::Scanner(__streamName.c_str());
38  __parser = new UAI::Parser(__scanner);
39  } catch (IOError& e) { __ioerror = true; }
40  }
41 
42  template < typename GUM_SCALAR >
44  GUM_DESTRUCTOR(UAIReader);
45 
46  if (!__ioerror) {
47  // this could lead to memory leak !!
48  if (__parser) delete (__parser);
49 
50  if (__scanner) delete (__scanner);
51  }
52  }
53 
54  template < typename GUM_SCALAR >
55  INLINE UAI::Scanner& UAIReader< GUM_SCALAR >::scanner() {
56  if (__ioerror) { GUM_ERROR(gum::IOError, "No such file " + streamName()); }
57 
58  return *__scanner;
59  }
60 
61  template < typename GUM_SCALAR >
62  INLINE const std::string& UAIReader< GUM_SCALAR >::streamName() const {
63  return __streamName;
64  }
65 
66  template < typename GUM_SCALAR >
67  INLINE bool UAIReader< GUM_SCALAR >::trace() const {
68  return __traceScanning;
69  }
70 
71  template < typename GUM_SCALAR >
72  INLINE void UAIReader< GUM_SCALAR >::trace(bool b) {
73  __traceScanning = b;
74  scanner().setTrace(b);
75  }
76 
77  template < typename GUM_SCALAR >
79  if (__ioerror) { GUM_ERROR(gum::IOError, "No such file " + streamName()); }
80 
81  if (!__parseDone) {
82  try {
83  __parser->Parse();
84  buildFromQuartets(__parser->getQuartets());
85  } catch (gum::Exception& e) {
86  GUM_SHOWERROR(e);
87  return 1 + __parser->errors().error_count;
88  }
89 
90  __parseDone = true;
91  }
92 
93  return (__parser->errors().error_count);
94  }
95 
96  template < typename GUM_SCALAR >
98  std::vector< std::tuple< float, int, int, int > > quartets) {
99  Idx current;
100  Size max = quartets.size();
101  if (max == 0) {
102  __addWarning(1, 1, "Empty BayesNet");
103  return;
104  }
105 
106  auto isInt = [&]() -> bool {
107  return (std::get< 0 >(quartets[current]) == -1);
108  };
109  auto lig = [&]() -> int { return std::get< 2 >(quartets[current]); };
110  auto col = [&]() -> int { return std::get< 3 >(quartets[current]); };
111 
112  auto getInt = [&]() -> int {
113  if (!isInt()) this->__addFatalError(lig(), col(), "int expected");
114  return std::get< 1 >(quartets[current]);
115  };
116  auto getVal = [&]() -> GUM_SCALAR {
117  return (isInt()) ? (std::get< 1 >(quartets[current]))
118  : (std::get< 0 >(quartets[current]));
119  };
120  auto incCurrent = [&]() {
121  current += 1;
122  if (current >= max)
123  this->__addFatalError(lig(), col(), "Not enough data in UAI file");
124  };
125 
126  current = 0;
127  Size nbrNode = (Size)getInt();
128 
129  for (NodeId i = 0; i < nbrNode; i++) {
130  incCurrent();
131  int mod = getInt();
132  if (mod < 2)
133  __addError(lig(), col(), "Number of modalities should be greater than 2.");
134  __bn->add(gum::LabelizedVariable(std::to_string(i), "", mod));
135  }
136 
137  incCurrent();
138  Size nbrPot = (Size)getInt();
139  if (nbrPot != nbrNode)
140  __addWarning(
141  lig(), col(), "Number of CPTs should be the same as number of nodes");
142 
143  Set< NodeId > s;
144  for (NodeId i = 0; i < nbrPot; i++) {
145  incCurrent();
146  Size nbrPar = (Size)getInt();
147  if (nbrPar == 0) __addError(lig(), col(), "0 is not possible here");
148 
149  incCurrent();
150  NodeId nodePot = (Size)getInt();
151  if (nodePot >= nbrNode)
152  __addError(lig(), col(), "Not enough variables in the BayesNet");
153  if (s.contains(nodePot)) __addError(lig(), col(), "Parents already defined");
154  s.insert(nodePot);
155 
156  for (NodeId j = 1; j < nbrPar; j++) {
157  incCurrent();
158  NodeId papa = (NodeId)getInt();
159  if (papa >= nbrNode)
160  __addError(lig(), col(), "Not enough variables in the BayesNet");
161 
162  __bn->addArc(papa, nodePot);
163  }
164  }
165 
166  std::vector< GUM_SCALAR > v;
167  for (NodeId i = 0; i < nbrPot; i++) {
168  incCurrent();
169  Size nbrParam = (Size)getInt();
170  if (nbrParam != __bn->cpt(i).domainSize())
172  lig(), col(), "Size does not fit between parents and parameters");
173  for (Idx j = 0; j < nbrParam; j++) {
174  incCurrent();
175  v.push_back(getVal());
176  }
177  __bn->cpt(i).fillWith(v);
178  v.clear();
179  }
180 
181  if (current != max - 1) __addError(lig(), col(), "Too many data in this file");
182  }
183 
184  // @{
185  // publishing Errors API
186  template < typename GUM_SCALAR >
188  if (__parseDone)
189  return __parser->errors().error(i).line;
190  else {
191  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet");
192  }
193  }
194 
195  template < typename GUM_SCALAR >
197  if (__parseDone)
198  return __parser->errors().error(i).column;
199  else {
200  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet");
201  }
202  }
203 
204  template < typename GUM_SCALAR >
206  if (__parseDone)
207  return __parser->errors().error(i).is_error;
208  else {
209  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet");
210  }
211  }
212 
213  template < typename GUM_SCALAR >
214  INLINE std::string UAIReader< GUM_SCALAR >::errMsg(Idx i) {
215  if (__parseDone)
216  return __parser->errors().error(i).msg;
217  else {
218  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet");
219  }
220  }
221 
222  template < typename GUM_SCALAR >
223  INLINE void UAIReader< GUM_SCALAR >::showElegantErrors(std::ostream& o) {
224  if (__parseDone)
225  __parser->errors().elegantErrors(o);
226  else {
227  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet");
228  }
229  }
230 
231  template < typename GUM_SCALAR >
232  INLINE void
234  if (__parseDone)
235  __parser->errors().elegantErrorsAndWarnings(o);
236  else {
237  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet");
238  }
239  }
240 
241  template < typename GUM_SCALAR >
242  INLINE void UAIReader< GUM_SCALAR >::showErrorsAndWarnings(std::ostream& o) {
243  if (__parseDone)
244  __parser->errors().simpleErrorsAndWarnings(o);
245  else {
246  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet");
247  }
248  }
249 
250  template < typename GUM_SCALAR >
251  INLINE void UAIReader< GUM_SCALAR >::showErrorCounts(std::ostream& o) {
252  if (__parseDone)
253  __parser->errors().syntheticResults(o);
254  else {
255  GUM_ERROR(OperationNotAllowed, "UAI file not parsed yet");
256  }
257  }
258 
259  template < typename GUM_SCALAR >
261  return (!__parseDone) ? (Size)0 : __parser->errors().error_count;
262  }
263 
264  template < typename GUM_SCALAR >
266  return (!__parseDone) ? (Size)0 : __parser->errors().warning_count;
267  }
268 
269  template < typename GUM_SCALAR >
271  Idx col,
272  const std::string& s) {
273  __parser->errors().addError(s, __streamName, lig, col);
275  }
276  template < typename GUM_SCALAR >
277  INLINE void
278  UAIReader< GUM_SCALAR >::__addError(Idx lig, Idx col, const std::string& s) {
279  __parser->errors().addError(s, __streamName, lig, col);
280  }
281  template < typename GUM_SCALAR >
282  INLINE void
283  UAIReader< GUM_SCALAR >::__addWarning(Idx lig, Idx col, const std::string& s) {
284  __parser->errors().addWarning(s, __streamName, lig, col);
285  }
286 
287  // @}
288 } // namespace gum
289 
290 #endif // DOXYGEN_SHOULD_SKIP_THIS
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
class LabelizedVariable
Idx errCol(Idx i)
col of ith error or warning
std::string __streamName
Definition: UAIReader.h:124
#define GUM_SHOWERROR(e)
Definition: exceptions.h:73
UAI::Parser * __parser
Definition: UAIReader.h:122
void __addError(Idx lig, Idx col, const std::string &s)
void __addWarning(Idx lig, Idx col, const std::string &s)
bool trace() const
accessor to trace function (just write the number of parser line)
Size proceed() final
parse.
void showElegantErrorsAndWarnings(std::ostream &o=std::cerr)
send on std::cerr the list of errors or warnings with contents
Idx errLine(Idx i)
line of ith error or warning
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:581
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
void __addFatalError(Idx lig, Idx col, const std::string &s)
UAIReader(BayesNet< GUM_SCALAR > *bn, const std::string &filename)
Constructor A reader is defined for reading a defined file.
Size errors()
publishing Errors API
bool errIsError(Idx i)
type of ith error or warning
std::string to_string(const Formula &f)
Definition: formula_inl.h:479
Base class for all aGrUM&#39;s exceptions.
Definition: exceptions.h:103
bool __parseDone
Definition: UAIReader.h:126
void showErrorCounts(std::ostream &o=std::cerr)
send on std::cerr the number of errors and the number of warnings
const std::string & streamName() const
name of readen file
void showElegantErrors(std::ostream &o=std::cerr)
send on std::cerr the list of errorswith contents
~UAIReader() final
Default destructor.
BayesNet< GUM_SCALAR > * __bn
Definition: UAIReader.h:119
std::string errMsg(Idx i)
message of ith error or warning
UAI::Scanner * __scanner
Definition: UAIReader.h:121
void showErrorsAndWarnings(std::ostream &o=std::cerr)
send on std::cerr the list of errors or warnings
UAI::Scanner & scanner()
Direct access to DSL scanner (mandatory for listener connection)
unsigned long Idx
Type for indexes.
Definition: types.h:43
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
bool __traceScanning
Definition: UAIReader.h:125
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66
void buildFromQuartets(std::vector< std::tuple< float, int, int, int > > quartets)
Size warnings()
of errors