aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
O3prmReader_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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 >
37  // This could be way more faster with regex but there are not implemented
38  // with gcc-4.8 !
39  text = replace(text, "Syntax error", "Error");
40 
41  text = replace(text, "LABEL_OR_STAR_LIST", "declaration");
42 
43  text = replace(text, "ARRAY_REFERENCE_SLOT", "declaration");
44  text = replace(text, "FLOAT_AS_LABEL", "declaration");
45  text = replace(text, "FLOAT_OR_INT", "declaration");
46  text = replace(text, "INTEGER_AS_FLOAT", "declaration");
47  text = replace(text, "INTEGER_AS_LABEL", "declaration");
48  text = replace(text, "INT_TYPE_DECLARATION", "declaration");
49  text = replace(text, "LABEL_OR_INT", "declaration");
50  text = replace(text, "LABEL_OR_STAR", "declaration");
51  text = replace(text, "NAMED_CLASS_ELEMENT", "declaration");
52  text = replace(text, "REAL_TYPE_DECLARATION", "declaration");
53 
54  text = replace(text, "AGGREGATE_PARENTS", "declaration");
55  text = replace(text, "CLASS_BODY", "declaration");
56  text = replace(text, "CLASS_DECLARATION", "declaration");
57  text = replace(text, "CLASS_ELEMENT", "declaration");
58  text = replace(text, "CLASS_PARAMETER", "declaration");
59  text = replace(text, "CLASS_UNIT", "declaration");
60  text = replace(text, "FLOAT_LIST", "declaration");
61  text = replace(text, "FORMULA_LIST", "declaration");
62  text = replace(text, "IDENTIFIER_LIST", "declaration");
63  text = replace(text, "IMPORT_BODY", "declaration");
64  text = replace(text, "IMPORT_DECLARATION", "declaration");
65  text = replace(text, "IMPORT_UNIT", "declaration");
66  text = replace(text, "INTERFACE_BODY", "declaration");
67  text = replace(text, "INTERFACE_DECLARATION", "declaration");
68  text = replace(text, "INTERFACE_UNIT", "declaration");
69  text = replace(text, "LABEL_LIST", "declaration");
70  text = replace(text, "PARAMETER_LIST", "declaration");
71  text = replace(text, "PREFIXED_LABEL", "declaration");
72  text = replace(text, "RAW_CPT", "declaration");
73  text = replace(text, "REFERENCE_SLOT", "declaration");
74  text = replace(text, "RULE_CPT", "declaration");
75  text = replace(text, "SYSTEM_BODY", "declaration");
76  text = replace(text, "SYSTEM_DECLARATION", "declaration");
77  text = replace(text, "SYSTEM_UNIT", "declaration");
78  text = replace(text, "TYPE_DECLARATION", "declaration");
79  text = replace(text, "TYPE_LABEL", "declaration");
80  text = replace(text, "TYPE_UNIT", "declaration");
81  text = replace(text, "TYPE_VALUE_LIST", "declaration");
82 
83  text = replace(text, "AGGREGATE", "declaration");
84  text = replace(text, "ARRAY", "declaration");
85  text = replace(text, "ATTRIBUTE", "declaration");
86  text = replace(text, "CAST", "declaration");
87  text = replace(text, "CHAIN", "declaration");
88  text = replace(text, "CLASS", "declaration");
89  text = replace(text, "FLOAT", "declaration");
90  text = replace(text, "FORMULA", "declaration");
91  text = replace(text, "IDENTIFIER", "declaration");
92  text = replace(text, "INT", "declaration");
93  text = replace(text, "INTEGER", "declaration");
94  text = replace(text, "INTERFACE", "declaration");
95  text = replace(text, "LABEL", "declaration");
96  text = replace(text, "LINK", "declaration");
97  text = replace(text, "MAP", "declaration");
98  text = replace(text, "PARAMETER", "declaration");
99  text = replace(text, "REAL", "declaration");
100  text = replace(text, "RULE", "declaration");
101  text = replace(text, "TYPE", "declaration");
102  text = replace(text, "UNIT", "declaration");
103 
104  return text;
105  }
106 
107  template < typename GUM_SCALAR >
109  std::stringstream s;
110  s << err.filename << "|" << err.line << " col " << err.column << "| " << _clean_(err.msg);
111  return s.str();
112  }
113 
114  template < typename GUM_SCALAR >
116  if (inputstr) {
118  int length = int(inputstr.tellg());
120 
121  auto str = std::string();
122  str.resize(length, ' ');
123  auto begin = &*str.begin();
124 
126 
127  return str;
128  }
129  GUM_ERROR(OperationNotAllowed, "Could not open file")
130  }
131 
134 
135  template < typename GUM_SCALAR >
136  INLINE O3prmReader< GUM_SCALAR >::O3prmReader() :
137  _prm_(new PRM< GUM_SCALAR >()), _o3_prm_(std::unique_ptr< O3PRM >(new O3PRM())) {
139  }
140 
141  template < typename GUM_SCALAR >
143  _prm_(&prm), _o3_prm_(std::unique_ptr< O3PRM >(new O3PRM())) {
145  }
146 
147  template < typename GUM_SCALAR >
152  }
153 
154  template < typename GUM_SCALAR >
160  }
161 
162  template < typename GUM_SCALAR >
165  }
166 
167  template < typename GUM_SCALAR >
170  if (this == &src) { return *this; }
171  _prm_ = src._prm_;
172  _o3_prm_ = std::unique_ptr< O3PRM >(new O3PRM(*(src._o3_prm_)));
176  return *this;
177  }
178 
179  template < typename GUM_SCALAR >
181  if (this == &src) { return *this; }
182  _prm_ = std::move(src._prm_);
187  return *this;
188  }
189 
190  template < typename GUM_SCALAR >
192  _class_path_ = std::vector< std::string >();
193  size_t i = 0;
194  size_t j = class_path.find(';');
195 
196  while (j != std::string::npos) {
198  i = j + 1;
199 
200  if (i < class_path.size()) {
201  j = class_path.find(';', i);
202  } else {
203  j = std::string::npos;
204  }
205  }
206 
208  }
209 
210  template < typename GUM_SCALAR >
212  auto path = class_path;
213  if (path[path.size() - 1] != '/') { path.append("/"); }
214  auto dir = Directory(path);
215 
216  if (!dir.isValid()) {
217  _errors_.addException("could not resolve class path", path);
218  } else {
220  }
221  }
222 
223  template < typename GUM_SCALAR >
225  return _errors_.error(i).line;
226  }
227 
228  template < typename GUM_SCALAR >
230  return _errors_.error(i).column;
231  }
232 
233  template < typename GUM_SCALAR >
235  return widen(_errors_.error(i).filename);
236  }
237 
238  template < typename GUM_SCALAR >
240  return _errors_.error(i).is_error;
241  }
242 
243  template < typename GUM_SCALAR >
245  return _errors_.error(i).msg;
246  }
247 
248  template < typename GUM_SCALAR >
250  for (Idx i = 0; i < _errors_.count(); ++i) {
251  auto err = _errors_.error(i);
252  if (err.is_error) { o << _print_(err) << std::endl; }
253  }
254  }
255 
256  template < typename GUM_SCALAR >
258  for (Idx i = 0; i < _errors_.count(); ++i) {
259  auto err = _errors_.error(i);
260  o << _print_(err) << std::endl;
261  }
262  }
263 
264  template < typename GUM_SCALAR >
267  }
268 
269  template < typename GUM_SCALAR >
271  return _errors_.error_count;
272  }
273 
274  template < typename GUM_SCALAR >
276  return _errors_.warning_count;
277  }
278 
279  template < typename GUM_SCALAR >
281  return _errors_;
282  }
283 
284  template < typename GUM_SCALAR >
287  _readStream_(sBuff, "");
288  return _errors_.count();
289  }
290 
291  template < typename GUM_SCALAR >
293  const std::string& module) {
294  try {
295  auto lastSlashIndex = file.find_last_of('/');
296 
297  auto dir = Directory(file.substr(0, lastSlashIndex + 1));
298 
299  if (!dir.isValid()) {
300  _errors_.addException("could not find file", file);
301  return _errors_.count();
302  }
303 
304  auto basename = file.substr(lastSlashIndex + 1);
306 
308  if (input.is_open()) {
310  } else {
311  _errors_.addException("could not open file", file);
312  }
313 
314  return _errors_.count();
315 
316  } catch (gum::Exception& e) {
317  GUM_SHOWERROR(e);
319  return _errors_.count();
320  } catch (...) {
321  _errors_.addException("unknown error", file);
322  return _errors_.count();
323  }
324  }
325 
326  template < typename GUM_SCALAR >
328  std::ostream& output,
329  std::string module) {
330  _readStream_(input, "", module);
331 
333  }
334 
335  template < typename GUM_SCALAR >
337  const std::string& filename,
338  const std::string& module) {
339  auto sBuff = _readStream_(input);
340  auto buffer = std::unique_ptr< unsigned char[] >(new unsigned char[sBuff.length() + 1]);
341  strcpy((char*)buffer.get(), sBuff.c_str());
342  auto s = o3prm_scanner(buffer.get(), int(sBuff.length() + 1), filename);
343  auto p = o3prm_parser(&s);
344  p.set_prm(_o3_prm_.get());
346  p.Parse();
347  _errors_ += p.errors();
348  }
349 
350  template < typename GUM_SCALAR >
352  const std::string& module) {
353  if (!_imported_.exists(i.import().label())) {
355 
356  auto module_path = module;
357  std::replace(module_path.begin(), module_path.end(), '.', '/');
358 
359  auto path = i.import().label();
360  std::replace(path.begin(), path.end(), '.', '/');
361 
362  auto imported = false;
363  for (const auto& cp: _class_path_) {
364  auto file_path = cp + path + ".o3prm";
366 
367  if (file.is_open()) {
369  imported = true;
370  break;
371  }
372 
373  file_path = cp + module + path + ".o3prm";
375 
376  if (file2.is_open()) {
378  imported = true;
379  break;
380  }
381  }
382 
383  if (!imported) {
384  const auto& pos = i.import().position();
386  msg << "Import error: could not resolve import " << i.import().label();
388  }
389  }
390  }
391 
392  template < typename GUM_SCALAR >
394  auto copy = std::vector< const O3Import* >();
395  for (const auto& i: _o3_prm_->imports()) {
396  if (!_imported_.exists(i->import().label())) { copy.push_back(i.get()); }
397  }
398  return copy;
399  }
400 
401  template < typename GUM_SCALAR >
403  const std::string& file,
404  std::string module) {
405  if (module.size() > 0 && module.back() != '.') { module.append("."); }
406 
408 
409  auto imports = _copyImports_();
410  do {
411  for (auto i: imports) {
413  }
415  } while (imports.size() > 0);
416 
417 
418  if (_errors_.error_count == 0) {
421 
422  auto interface_factory
425 
427 
428  try {
441  } catch (Exception&) {
442  if (_errors_.count() == 0) { _errors_.addException("an unknown error occured", file); }
443  } catch (...) { _errors_.addException("an unknown exception occured", file); }
444  }
445  }
446  } // namespace o3prm
447  } // namespace prm
448 } // namespace gum
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)