aGrUM  0.16.0
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 >
178  operator=(const O3prmReader& src) {
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
std::string __clean(std::string text) const
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
void __readStream(std::istream &input, const std::string &file, std::string module="")
gum::prm::o3prm::Parser o3prm_parser
#define GUM_SHOWERROR(e)
Definition: exceptions.h:61
std::string filename
The file of this gum::ParseError, default is "".
std::vector< std::string > __class_path
Definition: O3prmReader.h:148
STL namespace.
This class is used to represent parsing errors for the different parser implemented in aGrUM...
std::unique_ptr< O3PRM > __o3_prm
Definition: O3prmReader.h:147
gum::prm::o3prm::Scanner o3prm_scanner
This class is used contain and manipulate gum::ParseError.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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
Bulds gum::prm:PRMInterface from gum::prm::o3prm::O3Interface.
Resolves names for the different O3PRM factories.
Definition: O3NameSolver.h:57
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.
Set< std::string > __imported
Definition: O3prmReader.h:149
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition: PRM.h:66
std::string __print(const ParseError &err) const
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
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
PRM< GUM_SCALAR > * __prm
Definition: O3prmReader.h:146
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55