aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
O3prmReader_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Implementation for the O3prmReader class.
25  *
26  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27  * @author Lionel TORTI
28  */
29 #include <agrum/PRM/o3prm/O3prmReader.h>
30 
31 namespace gum {
32  namespace prm {
33  namespace o3prm {
34 
35  template < typename GUM_SCALAR >
38  // This could be way more faster with regex but there are not implemented
39  // with gcc-4.8 !
40  text = replace(text, "Syntax error", "Error");
41 
42  text = replace(text, "LABEL_OR_STAR_LIST", "declaration");
43 
44  text = replace(text, "ARRAY_REFERENCE_SLOT", "declaration");
45  text = replace(text, "FLOAT_AS_LABEL", "declaration");
46  text = replace(text, "FLOAT_OR_INT", "declaration");
47  text = replace(text, "INTEGER_AS_FLOAT", "declaration");
48  text = replace(text, "INTEGER_AS_LABEL", "declaration");
49  text = replace(text, "INT_TYPE_DECLARATION", "declaration");
50  text = replace(text, "LABEL_OR_INT", "declaration");
51  text = replace(text, "LABEL_OR_STAR", "declaration");
52  text = replace(text, "NAMED_CLASS_ELEMENT", "declaration");
53  text = replace(text, "REAL_TYPE_DECLARATION", "declaration");
54 
55  text = replace(text, "AGGREGATE_PARENTS", "declaration");
56  text = replace(text, "CLASS_BODY", "declaration");
57  text = replace(text, "CLASS_DECLARATION", "declaration");
58  text = replace(text, "CLASS_ELEMENT", "declaration");
59  text = replace(text, "CLASS_PARAMETER", "declaration");
60  text = replace(text, "CLASS_UNIT", "declaration");
61  text = replace(text, "FLOAT_LIST", "declaration");
62  text = replace(text, "FORMULA_LIST", "declaration");
63  text = replace(text, "IDENTIFIER_LIST", "declaration");
64  text = replace(text, "IMPORT_BODY", "declaration");
65  text = replace(text, "IMPORT_DECLARATION", "declaration");
66  text = replace(text, "IMPORT_UNIT", "declaration");
67  text = replace(text, "INTERFACE_BODY", "declaration");
68  text = replace(text, "INTERFACE_DECLARATION", "declaration");
69  text = replace(text, "INTERFACE_UNIT", "declaration");
70  text = replace(text, "LABEL_LIST", "declaration");
71  text = replace(text, "PARAMETER_LIST", "declaration");
72  text = replace(text, "PREFIXED_LABEL", "declaration");
73  text = replace(text, "RAW_CPT", "declaration");
74  text = replace(text, "REFERENCE_SLOT", "declaration");
75  text = replace(text, "RULE_CPT", "declaration");
76  text = replace(text, "SYSTEM_BODY", "declaration");
77  text = replace(text, "SYSTEM_DECLARATION", "declaration");
78  text = replace(text, "SYSTEM_UNIT", "declaration");
79  text = replace(text, "TYPE_DECLARATION", "declaration");
80  text = replace(text, "TYPE_LABEL", "declaration");
81  text = replace(text, "TYPE_UNIT", "declaration");
82  text = replace(text, "TYPE_VALUE_LIST", "declaration");
83 
84  text = replace(text, "AGGREGATE", "declaration");
85  text = replace(text, "ARRAY", "declaration");
86  text = replace(text, "ATTRIBUTE", "declaration");
87  text = replace(text, "CAST", "declaration");
88  text = replace(text, "CHAIN", "declaration");
89  text = replace(text, "CLASS", "declaration");
90  text = replace(text, "FLOAT", "declaration");
91  text = replace(text, "FORMULA", "declaration");
92  text = replace(text, "IDENTIFIER", "declaration");
93  text = replace(text, "INT", "declaration");
94  text = replace(text, "INTEGER", "declaration");
95  text = replace(text, "INTERFACE", "declaration");
96  text = replace(text, "LABEL", "declaration");
97  text = replace(text, "LINK", "declaration");
98  text = replace(text, "MAP", "declaration");
99  text = replace(text, "PARAMETER", "declaration");
100  text = replace(text, "REAL", "declaration");
101  text = replace(text, "RULE", "declaration");
102  text = replace(text, "TYPE", "declaration");
103  text = replace(text, "UNIT", "declaration");
104 
105  return text;
106  }
107 
108  template < typename GUM_SCALAR >
109  INLINE std::string
111  std::stringstream s;
112  s << err.filename << "|" << err.line << " col " << err.column << "| "
113  << clean__(err.msg);
114  return s.str();
115  }
116 
117  template < typename GUM_SCALAR >
118  INLINE std::string
120  if (inputstr) {
122  int length = int(inputstr.tellg());
124 
125  auto str = std::string();
126  str.resize(length, ' ');
127  auto begin = &*str.begin();
128 
130 
131  return str;
132  }
133  GUM_ERROR(OperationNotAllowed, "Could not open file");
134  }
135 
138 
139  template < typename GUM_SCALAR >
140  INLINE O3prmReader< GUM_SCALAR >::O3prmReader() :
141  prm__(new PRM< GUM_SCALAR >()),
142  o3_prm__(std::unique_ptr< O3PRM >(new O3PRM())) {
144  }
145 
146  template < typename GUM_SCALAR >
148  prm__(&prm), o3_prm__(std::unique_ptr< O3PRM >(new O3PRM())) {
150  }
151 
152  template < typename GUM_SCALAR >
154  prm__(src.prm__),
155  o3_prm__(std::unique_ptr< O3PRM >(new O3PRM(*(src.o3_prm__)))),
159  }
160 
161  template < typename GUM_SCALAR >
168  }
169 
170  template < typename GUM_SCALAR >
173  }
174 
175  template < typename GUM_SCALAR >
178  if (this == &src) { return *this; }
179  prm__ = src.prm__;
180  o3_prm__ = std::unique_ptr< O3PRM >(new O3PRM(*(src.o3_prm__)));
184  return *this;
185  }
186 
187  template < typename GUM_SCALAR >
190  if (this == &src) { return *this; }
191  prm__ = std::move(src.prm__);
196  return *this;
197  }
198 
199  template < typename GUM_SCALAR >
201  class_path__ = std::vector< std::string >();
202  size_t i = 0;
203  size_t j = class_path.find(';');
204 
205  while (j != std::string::npos) {
207  i = j + 1;
208 
209  if (i < class_path.size()) {
210  j = class_path.find(';', i);
211  } else {
212  j = std::string::npos;
213  }
214  }
215 
216  if (i < class_path.size()) {
218  }
219  }
220 
221  template < typename GUM_SCALAR >
223  auto path = class_path;
224  if (path[path.size() - 1] != '/') { path.append("/"); }
225  auto dir = Directory(path);
226 
227  if (!dir.isValid()) {
228  errors__.addException("could not resolve class path", path);
229  } else {
231  }
232  }
233 
234  template < typename GUM_SCALAR >
236  return errors__.error(i).line;
237  }
238 
239  template < typename GUM_SCALAR >
241  return errors__.error(i).column;
242  }
243 
244  template < typename GUM_SCALAR >
246  return widen(errors__.error(i).filename);
247  }
248 
249  template < typename GUM_SCALAR >
251  return errors__.error(i).is_error;
252  }
253 
254  template < typename GUM_SCALAR >
256  return errors__.error(i).msg;
257  }
258 
259  template < typename GUM_SCALAR >
260  INLINE void
262  for (Idx i = 0; i < errors__.count(); ++i) {
263  auto err = errors__.error(i);
264  if (err.is_error) { o << print__(err) << std::endl; }
265  }
266  }
267 
268  template < typename GUM_SCALAR >
270  std::ostream& o) const {
271  for (Idx i = 0; i < errors__.count(); ++i) {
272  auto err = errors__.error(i);
273  o << print__(err) << std::endl;
274  }
275  }
276 
277  template < typename GUM_SCALAR >
278  INLINE void
281  }
282 
283  template < typename GUM_SCALAR >
285  return errors__.error_count;
286  }
287 
288  template < typename GUM_SCALAR >
290  return errors__.warning_count;
291  }
292 
293  template < typename GUM_SCALAR >
294  INLINE const ErrorsContainer&
296  return errors__;
297  }
298 
299  template < typename GUM_SCALAR >
302  readStream__(sBuff, "");
303  return errors__.count();
304  }
305 
306  template < typename GUM_SCALAR >
308  const std::string& module) {
309  try {
310  auto lastSlashIndex = file.find_last_of('/');
311 
312  auto dir = Directory(file.substr(0, lastSlashIndex + 1));
313 
314  if (!dir.isValid()) {
315  errors__.addException("could not find file", file);
316  return errors__.count();
317  }
318 
319  auto basename = file.substr(lastSlashIndex + 1);
321 
323  if (input.is_open()) {
325  } else {
326  errors__.addException("could not open file", file);
327  }
328 
329  return errors__.count();
330 
331  } catch (gum::Exception& e) {
332  GUM_SHOWERROR(e);
334  return errors__.count();
335  } catch (...) {
336  errors__.addException("unknown error", file);
337  return errors__.count();
338  }
339  }
340 
341  template < typename GUM_SCALAR >
343  std::ostream& output,
344  std::string module) {
345  readStream__(input, "", module);
346 
348  }
349 
350  template < typename GUM_SCALAR >
351  INLINE void
353  const std::string& filename,
354  const std::string& module) {
355  auto sBuff = readStream__(input);
356  auto buffer = std::unique_ptr< unsigned char[] >(
357  new unsigned char[sBuff.length() + 1]);
358  strcpy((char*)buffer.get(), sBuff.c_str());
359  auto s = o3prm_scanner(buffer.get(), int(sBuff.length() + 1), filename);
360  auto p = o3prm_parser(&s);
361  p.set_prm(o3_prm__.get());
363  p.Parse();
364  errors__ += p.errors();
365  }
366 
367  template < typename GUM_SCALAR >
368  INLINE void
370  const std::string& module) {
371  if (!imported__.exists(i.import().label())) {
373 
374  auto module_path = module;
375  std::replace(module_path.begin(), module_path.end(), '.', '/');
376 
377  auto path = i.import().label();
378  std::replace(path.begin(), path.end(), '.', '/');
379 
380  auto imported = false;
381  for (const auto& cp: class_path__) {
382  auto file_path = cp + path + ".o3prm";
384 
385  if (file.is_open()) {
387  imported = true;
388  break;
389  }
390 
391  file_path = cp + module + path + ".o3prm";
393 
394  if (file2.is_open()) {
396  imported = true;
397  break;
398  }
399  }
400 
401  if (!imported) {
402  const auto& pos = i.import().position();
404  msg << "Import error: could not resolve import " << i.import().label();
406  }
407  }
408  }
409 
410  template < typename GUM_SCALAR >
411  INLINE std::vector< const O3Import* >
413  auto copy = std::vector< const O3Import* >();
414  for (const auto& i: o3_prm__->imports()) {
415  if (!imported__.exists(i->import().label())) { copy.push_back(i.get()); }
416  }
417  return copy;
418  }
419 
420  template < typename GUM_SCALAR >
422  const std::string& file,
423  std::string module) {
424  if (module.size() > 0 && module.back() != '.') { module.append("."); }
425 
427 
428  auto imports = copyImports__();
429  do {
430  for (auto i: imports) {
432  }
434  } while (imports.size() > 0);
435 
436 
437  if (errors__.error_count == 0) {
439  auto type_factory
441 
443  *o3_prm__,
444  solver,
445  errors__);
446  auto class_factory
448 
449  auto system_factory
451 
452  try {
465  } catch (Exception&) {
466  if (errors__.count() == 0) {
467  errors__.addException("an unknown error occured", file);
468  }
469  // GUM_TRACE_NEWLINE;
470  // GUM_SHOWERROR( e );
471  } catch (...) {
472  errors__.addException("an unknown exception occured", file);
473  }
474  }
475  }
476  } // namespace o3prm
477  } // namespace prm
478 } // namespace gum
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)