aGrUM  0.14.2
O3NameSolver_tpl.h
Go to the documentation of this file.
1 /**************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
30 
31 namespace gum {
32  namespace prm {
33  namespace o3prm {
34 
35  template < typename GUM_SCALAR >
37  O3PRM& o3_prm,
38  ErrorsContainer& errors) :
39  __prm(&prm),
40  __o3_prm(&o3_prm), __errors(&errors) {
41  GUM_CONSTRUCTOR(O3NameSolver);
42  }
43 
44  template < typename GUM_SCALAR >
46  const O3NameSolver< GUM_SCALAR >& src) :
47  __prm(src.__prm),
52  GUM_CONS_CPY(O3NameSolver);
53  }
54 
55  template < typename GUM_SCALAR >
58  __prm(std::move(src.__prm)),
59  __o3_prm(std::move(src.__o3_prm)), __errors(std::move(src.__errors)),
60  __typeName(std::move(src.__typeName)),
61  __eltName(std::move(src.__eltName)), __refName(std::move(src.__refName)),
63  __className(std::move(src.__className)) {
64  GUM_CONS_MOV(O3NameSolver);
65  }
66 
67  template < typename GUM_SCALAR >
69  GUM_DESTRUCTOR(O3NameSolver);
70  }
71 
72  template < typename GUM_SCALAR >
75  if (this == &src) { return *this; }
76  __prm = src.__prm;
77  __o3_prm = src.__o3_prm;
78  __errors = src.__errors;
79  __typeName = src.__typeName;
80  __eltName = src.__eltName;
81  __refName = src.__refName;
84  return *this;
85  }
86 
87  template < typename GUM_SCALAR >
90  if (this == &src) { return *this; }
91  __prm = std::move(src.__prm);
92  __o3_prm = std::move(src.__o3_prm);
93  __errors = std::move(src.__errors);
94  __typeName = std::move(src.__typeName);
95  __eltName = std::move(src.__eltName);
96  __refName = std::move(src.__refName);
97  __interfaceName = std::move(src.__interfaceName);
98  __className = std::move(src.__className);
99  return *this;
100  }
101 
102  template < typename GUM_SCALAR >
104  // If empty string, we return an empty string
105  if (name.label() == "") { return true; }
106  // If we've already found the element real name
107  if (__eltName.exists(name.label())) {
108  name.label() = __eltName[name.label()];
109  return true;
110  }
111  // If name exists as is
112  if (__prm->isType(name.label())) {
113  __eltName.insert(name.label(), name.label());
114  return true;
115  }
116  // If name exists as is
117  if (__prm->isInterface(name.label())) {
118  __eltName.insert(name.label(), name.label());
119  return true;
120  }
121  // If name exists as is
122  if (__prm->isClass(name.label())) {
123  __eltName.insert(name.label(), name.label());
124  return true;
125  }
126  // If name exists as is in O3PRM types
127  for (auto& t : __o3_prm->types()) {
128  if (t->name().label() == name.label()) {
129  __eltName.insert(name.label(), name.label());
130  return true;
131  }
132  }
133  // If name exists as is in O3PRM interfaces
134  for (auto& i : __o3_prm->interfaces()) {
135  if (i->name().label() == name.label()) {
136  __eltName.insert(name.label(), name.label());
137  return true;
138  }
139  }
140  // If name exists as is in O3PRM classes
141  for (auto& c : __o3_prm->classes()) {
142  if (c->name().label() == name.label()) {
143  __eltName.insert(name.label(), name.label());
144  return true;
145  }
146  }
147 
148  auto lookup = "." + name.label();
149  auto found = Set< std::string >();
150  auto matches = std::vector< std::string >();
151 
152  // Trying with types
153  for (auto t : __prm->types()) {
154  if (endsWith(t->name(), lookup)) {
155  if (!found.exists(t->name())) {
156  found.insert(t->name());
157  matches.push_back(t->name());
158  }
159  }
160  }
161  // Trying with O3Types
162  for (auto& t : __o3_prm->types()) {
163  if (endsWith(t->name().label(), lookup)) {
164  if (!found.exists(t->name().label())) {
165  found.insert(t->name().label());
166  matches.push_back(t->name().label());
167  }
168  }
169  }
170 
171  // Trying with interfaces
172  for (auto i : __prm->interfaces()) {
173  if (endsWith(i->name(), lookup)) {
174  if (!found.exists(i->name())) {
175  found.insert(i->name());
176  matches.push_back(i->name());
177  }
178  }
179  }
180  // Trying with O3Interface
181  for (auto& i : __o3_prm->interfaces()) {
182  if (endsWith(i->name().label(), lookup)) {
183  if (!found.exists(i->name().label())) {
184  found.insert(i->name().label());
185  matches.push_back(i->name().label());
186  }
187  }
188  }
189 
190  // Trying with class
191  for (auto c : __prm->classes()) {
192  if (endsWith(c->name(), lookup)) {
193  if (!found.exists(c->name())) {
194  found.insert(c->name());
195  matches.push_back(c->name());
196  }
197  }
198  }
199  // Trying with O3Class
200  for (auto& c : __o3_prm->classes()) {
201  if (endsWith(c->name().label(), lookup)) {
202  if (!found.exists(c->name().label())) {
203  found.insert(c->name().label());
204  matches.push_back(c->name().label());
205  }
206  }
207  }
208 
209  if (matches.size() == 1) { // One match is good
210  __eltName.insert(name.label(), matches.back());
211  name.label() = matches.back();
212  return true;
213 
214  } else if (matches.size() == 0) { // 0 match is not found
215 
216  // Unknown name type
217  O3PRM_TYPE_NOT_FOUND(name, *__errors);
218  return false;
219 
220  } else { // More than one match is ambiguous
221 
222  // Ambiguous name
223  O3PRM_TYPE_AMBIGUOUS(name, matches, *__errors);
224  return false;
225  }
226  }
227 
228  template < typename GUM_SCALAR >
230  // If empty string, we return an empty string
231  if (name.label() == "") { return true; }
232 
233  // If we've already found the type real name
234  if (__typeName.exists(name.label())) {
235  name.label() = __typeName[name.label()];
236  return true;
237  }
238 
239  // If name exists as is in PRM
240  if (__prm->isType(name.label())) {
241  __typeName.insert(name.label(), name.label());
242  return true;
243  }
244 
245  // If name exists as is in O3PRM
246  for (auto& t : __o3_prm->types()) {
247  if (t->name().label() == name.label()) {
248  __typeName.insert(name.label(), name.label());
249  return true;
250  }
251  }
252 
253  // If we didn't find it as is, then we must find a namespace
254  // in which it was declared
255  auto lookup = "." + name.label();
256  auto found = Set< std::string >();
257  auto matches = std::vector< std::string >();
258 
259  // Trying with types
260  for (auto t : __prm->types()) {
261  if (endsWith(t->name(), lookup)) {
262  if (!found.exists(t->name())) {
263  found.insert(t->name());
264  matches.push_back(t->name());
265  }
266  }
267  }
268 
269  // Trying with O3Types
270  for (auto& t : __o3_prm->types()) {
271  if (endsWith(t->name().label(), lookup)) {
272  if (!found.exists(t->name().label())) {
273  found.insert(t->name().label());
274  matches.push_back(t->name().label());
275  }
276  }
277  }
278 
279  if (matches.size() == 1) { // One match is good
280  __typeName.insert(name.label(), matches.back());
281  name.label() = matches.back();
282  return true;
283 
284  } else if (matches.size() == 0) { // 0 match is not found
285 
286  // Unknown name type
287  O3PRM_TYPE_NOT_FOUND(name, *__errors);
288  return false;
289 
290  } else { // More than one match is ambiguous
291 
292  // Ambiguous name
293  O3PRM_TYPE_AMBIGUOUS(name, matches, *__errors);
294  return false;
295  }
296  }
297 
298  template < typename GUM_SCALAR >
300  // If empty string, we return an empty string
301  if (name.label() == "") { return true; }
302 
303  // If we've already found the interface real name
304  if (__interfaceName.exists(name.label())) {
305  name.label() = __interfaceName[name.label()];
306  return true;
307  }
308 
309  // If name exists as is
310  if (__prm->isInterface(name.label())) {
311  __interfaceName.insert(name.label(), name.label());
312  return true;
313  }
314 
315  for (auto& i : __o3_prm->interfaces()) {
316  if (i->name().label() == name.label()) {
317  __interfaceName.insert(name.label(), name.label());
318  return true;
319  }
320  }
321 
322  // If we didn't find it as is, then we must find a namespace
323  // in which it was declared
324  auto lookup = "." + name.label();
325  auto found = Set< std::string >();
326  auto matches = std::vector< std::string >();
327 
328  // Trying with interfaces
329  for (auto i : __prm->interfaces()) {
330  if (endsWith(i->name(), lookup)) {
331  if (!found.exists(i->name())) {
332  found.insert(i->name());
333  matches.push_back(i->name());
334  }
335  }
336  }
337 
338  // Trying with O3Interface
339  for (auto& i : __o3_prm->interfaces()) {
340  if (endsWith(i->name().label(), lookup)) {
341  if (!found.exists(i->name().label())) {
342  found.insert(i->name().label());
343  matches.push_back(i->name().label());
344  }
345  }
346  }
347 
348  if (matches.size() == 1) { // One match is good
349 
350  __interfaceName.insert(name.label(), matches.back());
351  name.label() = matches.back();
352  return true;
353 
354  } else if (matches.size() == 0) { // 0 match is not found
355 
356  // Unknown name type
357  O3PRM_INTERFACE_NOT_FOUND(name, *__errors);
358  return false;
359 
360  } else { // More than one match is ambiguous
361 
362  // Ambiguous name
363  O3PRM_INTERFACE_AMBIGUOUS(name, matches, *__errors);
364  return false;
365  }
366  }
367 
368  template < typename GUM_SCALAR >
370  // If empty string, we return an empty string
371  if (name.label() == "") { return true; }
372 
373  // If we've already found super real name
374  if (__className.exists(name.label())) {
375  name.label() = __className[name.label()];
376  return true;
377  }
378 
379  // If class name exists as is
380  if (__prm->isClass(name.label())) {
381  __className.insert(name.label(), name.label());
382  return true;
383  }
384 
385  for (auto& c : __o3_prm->classes()) {
386  if (c->name().label() == name.label()) {
387  __className.insert(name.label(), name.label());
388  return true;
389  }
390  }
391 
392  // If we didn't find it as is, then we must find a namespace
393  // in which it was declared
394  auto lookup = "." + name.label();
395  auto matches = std::vector< std::string >();
396  auto found = Set< std::string >();
397 
398  // Try to complete with Class
399  for (auto c : __prm->classes()) {
400  if (endsWith(c->name(), lookup)) {
401  if (!found.exists(c->name())) {
402  found.insert(c->name());
403  matches.push_back(c->name());
404  }
405  }
406  }
407 
408  // Try to complete with O3Class
409  for (auto& c : __o3_prm->classes()) {
410  if (endsWith(c->name().label(), lookup)) {
411  if (!found.exists(c->name().label())) {
412  found.insert(c->name().label());
413  matches.push_back(c->name().label());
414  }
415  }
416  }
417 
418  if (matches.size() == 1) { // One match is good
419 
420  __className.insert(name.label(), matches.back());
421  name.label() = matches.back();
422  return true;
423 
424  } else if (matches.size() == 0) { // 0 match is not found
425 
426  // Unknown super class
427  O3PRM_CLASS_NOT_FOUND(name, *__errors);
428  return false;
429 
430  } else { // More than one match is ambiguous
431 
432  // Ambiguous name
433  O3PRM_CLASS_AMBIGUOUS(name, matches, *__errors);
434  return false;
435  }
436  }
437 
438  template < typename GUM_SCALAR >
440  // If empty string, we return an empty string
441  if (name.label() == "") { return true; }
442  // If we've already found the reference real name
443  if (__refName.exists(name.label())) {
444  name.label() = __refName[name.label()];
445  return true;
446  }
447  // If name exists as is
448  if (__prm->isInterface(name.label()) || __prm->isClass(name.label())) {
449  __refName.insert(name.label(), name.label());
450  return true;
451  }
452 
453  // We check if it matches an O3Interface
454  for (auto& i : __o3_prm->interfaces()) {
455  if (i->name().label() == name.label()) {
456  __interfaceName.insert(name.label(), name.label());
457  return true;
458  }
459  }
460 
461  // We check if it matches an O3Class
462  for (auto& c : __o3_prm->classes()) {
463  if (c->name().label() == name.label()) {
464  __className.insert(name.label(), name.label());
465  return true;
466  }
467  }
468 
469  // If we didn't find it as is, then we must find a namespace
470  // in which it was declared
471  auto lookup = "." + name.label();
472  auto found = Set< std::string >();
473  auto matches = std::vector< std::string >();
474 
475  // Trying with interfaces
476  for (auto i : __prm->interfaces()) {
477  if (endsWith(i->name(), lookup)) {
478  if (!found.exists(i->name())) {
479  found.insert(i->name());
480  matches.push_back(i->name());
481  }
482  }
483  }
484 
485  // Trying with O3Interface
486  for (auto& i : __o3_prm->interfaces()) {
487  if (endsWith(i->name().label(), lookup)) {
488  if (!found.exists(i->name().label())) {
489  found.insert(i->name().label());
490  matches.push_back(i->name().label());
491  }
492  }
493  }
494 
495  // Try to complete with Class
496  for (auto c : __prm->classes()) {
497  if (endsWith(c->name(), lookup)) {
498  if (!found.exists(c->name())) {
499  found.insert(c->name());
500  matches.push_back(c->name());
501  }
502  }
503  }
504 
505  // Try to complete with O3Class
506  for (auto& c : __o3_prm->classes()) {
507  if (endsWith(c->name().label(), lookup)) {
508  if (!found.exists(c->name().label())) {
509  found.insert(c->name().label());
510  matches.push_back(c->name().label());
511  }
512  }
513  }
514 
515  if (matches.size() == 1) { // One match is good
516 
517  __refName.insert(name.label(), matches.back());
518  name.label() = matches.back();
519  return true;
520 
521  } else if (matches.size() == 0) { // 0 match is not found
522 
523  // Unknown name type
524  O3PRM_REFERENCE_NOT_FOUND(name, *__errors);
525  return false;
526 
527  } else { // More than one match is ambiguous
528 
529  // Ambiguous name
530  O3PRM_REFERENCE_AMBIGUOUS(name, matches, *__errors);
531  return false;
532  }
533  }
534 
535 
536  } // namespace o3prm
537  } // namespace prm
538 } // namespace gum
O3ClassList & classes()
Definition: O3prm.cpp:501
STL namespace.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
Headers for the O3NameSolver class.
This class is used contain and manipulate gum::ParseError.
The O3Label is part of the AST of the O3PRM language.
Definition: O3prm.h:171
bool resolveType(O3Label &name)
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
bool endsWith(std::string const &value, std::string const &ending)
Returns true if value ends with ending.
std::string & label()
Definition: O3prm.cpp:240
O3NameSolver< GUM_SCALAR > & operator=(const O3NameSolver< GUM_SCALAR > &src)
Resolves names for the different O3PRM factories.
Definition: O3NameSolver.h:55
bool resolveClassElement(O3Label &name)
bool resolveSlotType(O3Label &name)
O3InterfaceList & interfaces()
Definition: O3prm.cpp:496
O3NameSolver(PRM< GUM_SCALAR > &prm, O3PRM &o3_prm, ErrorsContainer &errors)
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition: PRM.h:63
ErrorsContainer * __errors
Definition: O3NameSolver.h:85
O3TypeList & types()
Definition: O3prm.cpp:485
bool resolveInterface(O3Label &name)
The O3PRM is part of the AST of the O3PRM language.
Definition: O3prm.h:890
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:698
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:610
bool resolveClass(O3Label &name)
PRM< GUM_SCALAR > * __prm
Definition: O3NameSolver.h:83