aGrUM  0.18.1
a C++ library for (probabilistic) graphical models
O3prmReader_tpl.h
Go to the documentation of this file.
1 
31 
32 namespace gum {
33  namespace prm {
34  namespace o3prm {
35 
36  template < typename GUM_SCALAR >
37  INLINE std::string
38  O3prmReader< GUM_SCALAR >::clean__(std::string text) const {
39  // This could be way more faster with regex but there are not implemented
40  // with gcc-4.8 !
41  text = replace(text, "Syntax error", "Error");
42 
43  text = replace(text, "LABEL_OR_STAR_LIST", "declaration");
44 
45  text = replace(text, "ARRAY_REFERENCE_SLOT", "declaration");
46  text = replace(text, "FLOAT_AS_LABEL", "declaration");
47  text = replace(text, "FLOAT_OR_INT", "declaration");
48  text = replace(text, "INTEGER_AS_FLOAT", "declaration");
49  text = replace(text, "INTEGER_AS_LABEL", "declaration");
50  text = replace(text, "INT_TYPE_DECLARATION", "declaration");
51  text = replace(text, "LABEL_OR_INT", "declaration");
52  text = replace(text, "LABEL_OR_STAR", "declaration");
53  text = replace(text, "NAMED_CLASS_ELEMENT", "declaration");
54  text = replace(text, "REAL_TYPE_DECLARATION", "declaration");
55 
56  text = replace(text, "AGGREGATE_PARENTS", "declaration");
57  text = replace(text, "CLASS_BODY", "declaration");
58  text = replace(text, "CLASS_DECLARATION", "declaration");
59  text = replace(text, "CLASS_ELEMENT", "declaration");
60  text = replace(text, "CLASS_PARAMETER", "declaration");
61  text = replace(text, "CLASS_UNIT", "declaration");
62  text = replace(text, "FLOAT_LIST", "declaration");
63  text = replace(text, "FORMULA_LIST", "declaration");
64  text = replace(text, "IDENTIFIER_LIST", "declaration");
65  text = replace(text, "IMPORT_BODY", "declaration");
66  text = replace(text, "IMPORT_DECLARATION", "declaration");
67  text = replace(text, "IMPORT_UNIT", "declaration");
68  text = replace(text, "INTERFACE_BODY", "declaration");
69  text = replace(text, "INTERFACE_DECLARATION", "declaration");
70  text = replace(text, "INTERFACE_UNIT", "declaration");
71  text = replace(text, "LABEL_LIST", "declaration");
72  text = replace(text, "PARAMETER_LIST", "declaration");
73  text = replace(text, "PREFIXED_LABEL", "declaration");
74  text = replace(text, "RAW_CPT", "declaration");
75  text = replace(text, "REFERENCE_SLOT", "declaration");
76  text = replace(text, "RULE_CPT", "declaration");
77  text = replace(text, "SYSTEM_BODY", "declaration");
78  text = replace(text, "SYSTEM_DECLARATION", "declaration");
79  text = replace(text, "SYSTEM_UNIT", "declaration");
80  text = replace(text, "TYPE_DECLARATION", "declaration");
81  text = replace(text, "TYPE_LABEL", "declaration");
82  text = replace(text, "TYPE_UNIT", "declaration");
83  text = replace(text, "TYPE_VALUE_LIST", "declaration");
84 
85  text = replace(text, "AGGREGATE", "declaration");
86  text = replace(text, "ARRAY", "declaration");
87  text = replace(text, "ATTRIBUTE", "declaration");
88  text = replace(text, "CAST", "declaration");
89  text = replace(text, "CHAIN", "declaration");
90  text = replace(text, "CLASS", "declaration");
91  text = replace(text, "FLOAT", "declaration");
92  text = replace(text, "FORMULA", "declaration");
93  text = replace(text, "IDENTIFIER", "declaration");
94  text = replace(text, "INT", "declaration");
95  text = replace(text, "INTEGER", "declaration");
96  text = replace(text, "INTERFACE", "declaration");
97  text = replace(text, "LABEL", "declaration");
98  text = replace(text, "LINK", "declaration");
99  text = replace(text, "MAP", "declaration");
100  text = replace(text, "PARAMETER", "declaration");
101  text = replace(text, "REAL", "declaration");
102  text = replace(text, "RULE", "declaration");
103  text = replace(text, "TYPE", "declaration");
104  text = replace(text, "UNIT", "declaration");
105 
106  return text;
107  }
108 
109  template < typename GUM_SCALAR >
110  INLINE std::string
112  std::stringstream s;
113  s << err.filename << "|" << err.line << " col " << err.column << "| "
114  << clean__(err.msg);
115  return s.str();
116  }
117 
118  template < typename GUM_SCALAR >
119  INLINE std::string
120  O3prmReader< GUM_SCALAR >::readStream__(std::istream& inputstr) {
121  if (inputstr) {
122  inputstr.seekg(0, inputstr.end);
123  int length = int(inputstr.tellg());
124  inputstr.seekg(0, inputstr.beg);
125 
126  auto str = std::string();
127  str.resize(length, ' ');
128  auto begin = &*str.begin();
129 
130  inputstr.read(begin, length);
131 
132  return str;
133  }
134  GUM_ERROR(OperationNotAllowed, "Could not open file");
135  }
136 
137  using o3prm_scanner = gum::prm::o3prm::Scanner;
138  using o3prm_parser = gum::prm::o3prm::Parser;
139 
140  template < typename GUM_SCALAR >
142  prm__(new PRM< GUM_SCALAR >()),
143  o3_prm__(std::unique_ptr< O3PRM >(new O3PRM())) {
144  GUM_CONSTRUCTOR(O3prmReader);
145  }
146 
147  template < typename GUM_SCALAR >
149  prm__(&prm), o3_prm__(std::unique_ptr< O3PRM >(new O3PRM())) {
150  GUM_CONSTRUCTOR(O3prmReader);
151  }
152 
153  template < typename GUM_SCALAR >
155  prm__(src.prm__),
156  o3_prm__(std::unique_ptr< O3PRM >(new O3PRM(*(src.o3_prm__)))),
157  class_path__(src.class_path__), imported__(src.imported__),
158  errors__(src.errors__) {
159  GUM_CONS_CPY(O3prmReader);
160  }
161 
162  template < typename GUM_SCALAR >
164  prm__(std::move(src.prm__)), o3_prm__(std::move(src.o3_prm__)),
165  class_path__(std::move(src.class_path__)),
166  imported__(std::move(src.imported__)),
167  errors__(std::move(src.errors__)) {
168  GUM_CONS_CPY(O3prmReader);
169  }
170 
171  template < typename GUM_SCALAR >
173  GUM_DESTRUCTOR(O3prmReader);
174  }
175 
176  template < typename GUM_SCALAR >
179  if (this == &src) { return *this; }
180  prm__ = src.prm__;
181  o3_prm__ = std::unique_ptr< O3PRM >(new O3PRM(*(src.o3_prm__)));
182  class_path__ = src.class_path__;
183  imported__ = src.imported__;
184  errors__ = src.errors__;
185  return *this;
186  }
187 
188  template < typename GUM_SCALAR >
191  if (this == &src) { return *this; }
192  prm__ = std::move(src.prm__);
193  o3_prm__ = std::move(src.o3_prm__);
194  class_path__ = std::move(src.class_path__);
195  imported__ = std::move(src.imported__);
196  errors__ = std::move(src.errors__);
197  return *this;
198  }
199 
200  template < typename GUM_SCALAR >
201  void O3prmReader< GUM_SCALAR >::setClassPath(const std::string& class_path) {
202  class_path__ = std::vector< std::string >();
203  size_t i = 0;
204  size_t j = class_path.find(';');
205 
206  while (j != std::string::npos) {
207  addClassPath(class_path.substr(i, j - i));
208  i = j + 1;
209 
210  if (i < class_path.size()) {
211  j = class_path.find(';', i);
212  } else {
213  j = std::string::npos;
214  }
215  }
216 
217  if (i < class_path.size()) {
218  addClassPath(class_path.substr(i, std::string::npos));
219  }
220  }
221 
222  template < typename GUM_SCALAR >
223  void O3prmReader< GUM_SCALAR >::addClassPath(const std::string& class_path) {
224  auto path = class_path;
225  if (path[path.size() - 1] != '/') { path.append("/"); }
226  auto dir = Directory(path);
227 
228  if (!dir.isValid()) {
229  errors__.addException("could not resolve class path", path);
230  } else {
231  class_path__.push_back(std::move(path));
232  }
233  }
234 
235  template < typename GUM_SCALAR >
237  return errors__.error(i).line;
238  }
239 
240  template < typename GUM_SCALAR >
242  return errors__.error(i).column;
243  }
244 
245  template < typename GUM_SCALAR >
246  INLINE std::wstring O3prmReader< GUM_SCALAR >::errFilename(Idx i) const {
247  return widen(errors__.error(i).filename);
248  }
249 
250  template < typename GUM_SCALAR >
252  return errors__.error(i).is_error;
253  }
254 
255  template < typename GUM_SCALAR >
256  INLINE std::string O3prmReader< GUM_SCALAR >::errMsg(Idx i) const {
257  return errors__.error(i).msg;
258  }
259 
260  template < typename GUM_SCALAR >
261  INLINE void
263  for (Idx i = 0; i < errors__.count(); ++i) {
264  auto err = errors__.error(i);
265  if (err.is_error) { o << print__(err) << std::endl; }
266  }
267  }
268 
269  template < typename GUM_SCALAR >
271  std::ostream& o) const {
272  for (Idx i = 0; i < errors__.count(); ++i) {
273  auto err = errors__.error(i);
274  o << print__(err) << std::endl;
275  }
276  }
277 
278  template < typename GUM_SCALAR >
279  INLINE void
281  errors__.syntheticResults(o);
282  }
283 
284  template < typename GUM_SCALAR >
286  return errors__.error_count;
287  }
288 
289  template < typename GUM_SCALAR >
291  return errors__.warning_count;
292  }
293 
294  template < typename GUM_SCALAR >
295  INLINE const ErrorsContainer&
297  return errors__;
298  }
299 
300  template < typename GUM_SCALAR >
301  INLINE Size O3prmReader< GUM_SCALAR >::readString(const std::string& str) {
302  std::stringstream sBuff(str);
303  readStream__(sBuff, "");
304  return errors__.count();
305  }
306 
307  template < typename GUM_SCALAR >
308  INLINE Size O3prmReader< GUM_SCALAR >::readFile(const std::string& file,
309  const std::string& module) {
310  try {
311  auto lastSlashIndex = file.find_last_of('/');
312 
313  auto dir = Directory(file.substr(0, lastSlashIndex + 1));
314 
315  if (!dir.isValid()) {
316  errors__.addException("could not find file", file);
317  return errors__.count();
318  }
319 
320  auto basename = file.substr(lastSlashIndex + 1);
321  auto absFilename = dir.absolutePath() + basename;
322 
323  std::ifstream input(absFilename);
324  if (input.is_open()) {
325  readStream__(input, file, module);
326  } else {
327  errors__.addException("could not open file", file);
328  }
329 
330  return errors__.count();
331 
332  } catch (gum::Exception& e) {
333  GUM_SHOWERROR(e);
334  errors__.addException(e.errorContent(), file);
335  return errors__.count();
336  } catch (...) {
337  errors__.addException("unknown error", file);
338  return errors__.count();
339  }
340  }
341 
342  template < typename GUM_SCALAR >
343  INLINE void O3prmReader< GUM_SCALAR >::parseStream(std::istream& input,
344  std::ostream& output,
345  std::string module) {
346  readStream__(input, "", module);
347 
348  showElegantErrorsAndWarnings(output);
349  }
350 
351  template < typename GUM_SCALAR >
352  INLINE void
354  const std::string& filename,
355  const std::string& module) {
356  auto sBuff = readStream__(input);
357  auto buffer = std::unique_ptr< unsigned char[] >(
358  new unsigned char[sBuff.length() + 1]);
359  strcpy((char*)buffer.get(), sBuff.c_str());
360  auto s = o3prm_scanner(buffer.get(), int(sBuff.length() + 1), filename);
361  auto p = o3prm_parser(&s);
362  p.set_prm(o3_prm__.get());
363  p.set_prefix(module);
364  p.Parse();
365  errors__ += p.errors();
366  }
367 
368  template < typename GUM_SCALAR >
369  INLINE void
371  const std::string& module) {
372  if (!imported__.exists(i.import().label())) {
373  imported__.insert(i.import().label());
374 
375  auto module_path = module;
376  std::replace(module_path.begin(), module_path.end(), '.', '/');
377 
378  auto path = i.import().label();
379  std::replace(path.begin(), path.end(), '.', '/');
380 
381  auto imported = false;
382  for (const auto& cp: class_path__) {
383  auto file_path = cp + path + ".o3prm";
384  std::ifstream file(file_path);
385 
386  if (file.is_open()) {
387  parseStream__(file, file_path, i.import().label());
388  imported = true;
389  break;
390  }
391 
392  file_path = cp + module + path + ".o3prm";
393  std::ifstream file2(file_path);
394 
395  if (file2.is_open()) {
396  parseStream__(file2, file_path, module + "." + i.import().label());
397  imported = true;
398  break;
399  }
400  }
401 
402  if (!imported) {
403  const auto& pos = i.import().position();
404  std::stringstream msg;
405  msg << "Import error: could not resolve import " << i.import().label();
406  errors__.addError(msg.str(), pos.file(), pos.line(), pos.column());
407  }
408  }
409  }
410 
411  template < typename GUM_SCALAR >
412  INLINE std::vector< const O3Import* >
414  auto copy = std::vector< const O3Import* >();
415  for (const auto& i: o3_prm__->imports()) {
416  if (!imported__.exists(i->import().label())) { copy.push_back(i.get()); }
417  }
418  return copy;
419  }
420 
421  template < typename GUM_SCALAR >
422  INLINE void O3prmReader< GUM_SCALAR >::readStream__(std::istream& input,
423  const std::string& file,
424  std::string module) {
425  if (module.size() > 0 && module.back() != '.') { module.append("."); }
426 
427  parseStream__(input, file, module);
428 
429  auto imports = copyImports__();
430  do {
431  for (auto i: imports) {
432  parseImport__(*i, module);
433  }
434  imports = copyImports__();
435  } while (imports.size() > 0);
436 
437 
438  if (errors__.error_count == 0) {
439  auto solver = O3NameSolver< GUM_SCALAR >(*prm__, *o3_prm__, errors__);
440  auto type_factory =
441  O3TypeFactory< GUM_SCALAR >(*prm__, *o3_prm__, solver, errors__);
442 
443  auto interface_factory =
444  O3InterfaceFactory< GUM_SCALAR >(*prm__, *o3_prm__, solver, errors__);
445  auto class_factory =
446  O3ClassFactory< GUM_SCALAR >(*prm__, *o3_prm__, solver, errors__);
447 
448  auto system_factory =
449  O3SystemFactory< GUM_SCALAR >(*prm__, *o3_prm__, solver, errors__);
450 
451  try {
452  type_factory.build();
453  interface_factory.buildInterfaces();
454  class_factory.buildClasses();
455  interface_factory.buildElements();
456  class_factory.buildImplementations();
457  class_factory.buildParameters();
458  class_factory.buildReferenceSlots();
459  class_factory.declareAttributes();
460  class_factory.declareAggregates();
461  class_factory.completeAggregates();
462  class_factory.completeAttributes();
463  system_factory.build();
464  } catch (Exception&) {
465  if (errors__.count() == 0) {
466  errors__.addException("an unknown error occured", file);
467  }
468  // GUM_TRACE_NEWLINE;
469  // GUM_SHOWERROR( e );
470  } catch (...) {
471  errors__.addException("an unknown exception occured", file);
472  }
473  }
474  }
475  } // namespace o3prm
476  } // namespace prm
477 } // namespace gum
PRM< GUM_SCALAR > * prm__
Definition: O3prmReader.h:146
Builds gum::prm::PRMType from gum::prm::o3prm::O3Type, gum::prm::o3prm::O3IntType and gum::prm::o3prm...
Definition: O3TypeFactory.h:61
Cross-platform directory utility.
Definition: utils_dir.h:62
std::string msg
The gum::ParseError message.
O3Position & position()
Definition: O3prm.cpp:239
gum::prm::o3prm::Parser o3prm_parser
std::unique_ptr< O3PRM > o3_prm__
Definition: O3prmReader.h:147
#define GUM_SHOWERROR(e)
Definition: exceptions.h:61
std::string clean__(std::string text) const
std::vector< std::string > class_path__
Definition: O3prmReader.h:148
std::string filename
The file of this gum::ParseError, default is "".
STL namespace.
This class is used to represent parsing errors for the different parser implemented in aGrUM...
gum::prm::o3prm::Scanner o3prm_scanner
This class is used contain and manipulate gum::ParseError.
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
Definition: agrum.h:25
The O3Import is part of the AST of the O3PRM language.
Definition: O3prm.h:868
Builds gum::prm::PRMSystem from gum::prm::o3prm::O3System.
This class read O3PRM files and creates the corresponding gum::prm::PRM.
Definition: O3prmReader.h:69
std::string & label()
Definition: O3prm.cpp:242
Set< std::string > imported__
Definition: O3prmReader.h:149
Bulds gum::prm:PRMInterface from gum::prm::o3prm::O3Interface.
Resolves names for the different O3PRM factories.
Definition: O3NameSolver.h:57
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
DatabaseTable readFile(const std::string &filename)
Idx line
The line of this gum::ParseError.
std::wstring widen(const std::string &str)
Cast a std::string into a std::wstring.
Base class for all aGrUM&#39;s exceptions.
Definition: exceptions.h:106
Builds gum::prm::Class from gum::prm::o3prm::O3Class.
std::string print__(const ParseError &err) const
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition: PRM.h:66
std::string absolutePath() const
Returns directory absolute path.
Definition: utils_dir.cpp:91
std::string replace(const std::string &s, const std::string &val, const std::string &new_val)
not usable for gcc 4.8 std::vector<std::string> split( const std::string& orig, const std::string& de...
Size Idx
Type for indexes.
Definition: types.h:53
void readStream__(std::istream &input, const std::string &file, std::string module="")
The O3PRM is part of the AST of the O3PRM language.
Definition: O3prm.h:892
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
Idx column
The column of this gum::ParseError, default is 0.
const std::string errorContent() const
Returns the message content.
Definition: exceptions.h:135
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55