knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
groundable.h
Go to the documentation of this file.
1 /*
2  * This file is part of KnowRob, please consult
3  * https://github.com/knowrob/knowrob for license details.
4  */
5 
6 #ifndef KNOWROB_GROUNDABLE_H
7 #define KNOWROB_GROUNDABLE_H
8 
9 #include <memory>
10 #include <sstream>
11 #include "Variable.h"
12 #include "knowrob/Logger.h"
13 
14 namespace knowrob {
20  template<class TermType> class groundable {
21  public:
22  groundable() = default;
23 
27  explicit groundable(const std::shared_ptr<TermType> &grounded) : grounded_(grounded) {}
28 
32  explicit groundable(const std::shared_ptr<Variable> &variable) : variable_(variable) {}
33 
37  TermPtr operator*() const { return get(); }
38 
42  TermPtr operator()() const { return get(); }
43 
47  Term* operator->() const { return get().get(); }
48 
53  groundable& operator=(const std::shared_ptr<TermType> &grounded) { set_grounded(grounded); return *this; }
54 
59  groundable& operator=(const std::shared_ptr<Variable> &variable) { set_variable(variable); return *this; }
60 
64  explicit operator bool() const { return has_grounding() || has_variable(); }
65 
69  TermPtr get() const {
70  if (has_grounding()) {
71  return grounded();
72  } else {
73  return variable();
74  }
75  }
76 
81  void set_grounded(const std::shared_ptr<TermType> &grounded) { grounded_ = grounded; variable_ = nullptr; }
82 
87  void set_variable(const std::shared_ptr<Variable> &variable) { grounded_ = nullptr; variable_ = variable; }
88 
92  bool has_grounding() const { return grounded_ != nullptr; }
93 
97  bool has_variable() const { return !has_grounding() && variable_ != nullptr; }
98 
102  auto grounded() const { return grounded_; }
103 
107  auto variable() const { return variable_; }
108 
115  if (term->isVariable()) {
116  return groundable<TermType>(std::static_pointer_cast<Variable>(term));
117  }
118  if (auto grounded = std::dynamic_pointer_cast<TermType>(term)) {
120  } else {
121  KB_WARN("Cannot cast term `{}` to type `{}`.", *term, typeid(TermType).name());
122  return groundable<TermType>();
123  }
124  }
125 
126  protected:
127  std::shared_ptr<TermType> grounded_;
128  std::shared_ptr<Variable> variable_;
129  };
130 
131 } // knowrob
132 
133 #endif //KNOWROB_GROUNDABLE_H
#define KB_WARN
Definition: Logger.h:27
groundable & operator=(const std::shared_ptr< TermType > &grounded)
Definition: groundable.h:53
void set_variable(const std::shared_ptr< Variable > &variable)
Definition: groundable.h:87
std::shared_ptr< TermType > grounded_
Definition: groundable.h:127
auto grounded() const
Definition: groundable.h:102
bool has_grounding() const
Definition: groundable.h:92
TermPtr get() const
Definition: groundable.h:69
auto variable() const
Definition: groundable.h:107
Term * operator->() const
Definition: groundable.h:47
groundable & operator=(const std::shared_ptr< Variable > &variable)
Definition: groundable.h:59
TermPtr operator()() const
Definition: groundable.h:42
static groundable< TermType > cast(const TermPtr &term)
Definition: groundable.h:114
TermPtr operator*() const
Definition: groundable.h:37
std::shared_ptr< Variable > variable_
Definition: groundable.h:128
groundable(const std::shared_ptr< Variable > &variable)
Definition: groundable.h:32
groundable(const std::shared_ptr< TermType > &grounded)
Definition: groundable.h:27
bool has_variable() const
Definition: groundable.h:97
void set_grounded(const std::shared_ptr< TermType > &grounded)
Definition: groundable.h:81
TermRule & term()
Definition: terms.cpp:136
TermType
Definition: Term.h:18
std::shared_ptr< Term > TermPtr
Definition: Term.h:117