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