knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::groundable< TermType > Class Template Reference

#include <groundable.h>

Public Member Functions

 groundable ()=default
 
 groundable (const std::shared_ptr< TermType > &grounded)
 
 groundable (const std::shared_ptr< Variable > &variable)
 
TermPtr operator* () const
 
TermPtr operator() () const
 
Termoperator-> () const
 
groundableoperator= (const std::shared_ptr< TermType > &grounded)
 
groundableoperator= (const std::shared_ptr< Variable > &variable)
 
 operator bool () const
 
TermPtr get () const
 
void set_grounded (const std::shared_ptr< TermType > &grounded)
 
void set_variable (const std::shared_ptr< Variable > &variable)
 
bool has_grounding () const
 
bool has_variable () const
 
auto grounded () const
 
auto variable () const
 
 groundable ()=default
 
 groundable (const std::shared_ptr< TermType > &grounded)
 
 groundable (const std::shared_ptr< Variable > &variable)
 
TermPtr operator* () const
 
TermPtr operator() () const
 
Termoperator-> () const
 
groundableoperator= (const std::shared_ptr< TermType > &grounded)
 
groundableoperator= (const std::shared_ptr< Variable > &variable)
 
 operator bool () const
 
TermPtr get () const
 
void set_grounded (const std::shared_ptr< TermType > &grounded)
 
void set_variable (const std::shared_ptr< Variable > &variable)
 
bool has_grounding () const
 
bool has_variable () const
 
auto grounded () const
 
auto variable () const
 

Static Public Member Functions

static groundable< TermTypecast (const TermPtr &term)
 
static groundable< TermTypecast (const TermPtr &term)
 

Protected Attributes

std::shared_ptr< TermTypegrounded_
 
std::shared_ptr< Variablevariable_
 

Detailed Description

template<class TermType>
class knowrob::groundable< TermType >

An interface for groundable terms that have either a particular term type or are a variable.

Template Parameters
TermTypethe term type.

Definition at line 20 of file groundable.h.

Constructor & Destructor Documentation

◆ groundable() [1/6]

template<class TermType >
knowrob::groundable< TermType >::groundable ( )
default

◆ groundable() [2/6]

template<class TermType >
knowrob::groundable< TermType >::groundable ( const std::shared_ptr< TermType > &  grounded)
inlineexplicit
Parameters
groundeda grounding.

Definition at line 27 of file groundable.h.

27 : grounded_(grounded) {}
std::shared_ptr< TermType > grounded_
Definition: groundable.h:127
auto grounded() const
Definition: groundable.h:102

◆ groundable() [3/6]

template<class TermType >
knowrob::groundable< TermType >::groundable ( const std::shared_ptr< Variable > &  variable)
inlineexplicit
Parameters
variablea variable.

Definition at line 32 of file groundable.h.

32 : variable_(variable) {}
auto variable() const
Definition: groundable.h:107
std::shared_ptr< Variable > variable_
Definition: groundable.h:128

◆ groundable() [4/6]

template<class TermType >
knowrob::groundable< TermType >::groundable ( )
default

◆ groundable() [5/6]

template<class TermType >
knowrob::groundable< TermType >::groundable ( const std::shared_ptr< TermType > &  grounded)
inlineexplicit
Parameters
groundeda grounding.

Definition at line 27 of file groundable.h.

27 : grounded_(grounded) {}

◆ groundable() [6/6]

template<class TermType >
knowrob::groundable< TermType >::groundable ( const std::shared_ptr< Variable > &  variable)
inlineexplicit
Parameters
variablea variable.

Definition at line 32 of file groundable.h.

32 : variable_(variable) {}

Member Function Documentation

◆ cast() [1/2]

template<class TermType >
static groundable<TermType> knowrob::groundable< TermType >::cast ( const TermPtr term)
inlinestatic

Cast a term to a groundable term.

Parameters
terma term.
Returns
a groundable term.

Definition at line 114 of file groundable.h.

114  {
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)) {
119  return groundable<TermType>(grounded);
120  } else {
121  KB_WARN("Cannot cast term `{}` to type `{}`.", *term, typeid(TermType).name());
122  return groundable<TermType>();
123  }
124  }
#define KB_WARN
Definition: Logger.h:27
TermRule & term()
Definition: terms.cpp:136
TermType
Definition: Term.h:18

◆ cast() [2/2]

template<class TermType >
static groundable<TermType> knowrob::groundable< TermType >::cast ( const TermPtr term)
inlinestatic

Cast a term to a groundable term.

Parameters
terma term.
Returns
a groundable term.

Definition at line 114 of file groundable.h.

114  {
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)) {
119  return groundable<TermType>(grounded);
120  } else {
121  KB_WARN("Cannot cast term `{}` to type `{}`.", *term, typeid(TermType).name());
122  return groundable<TermType>();
123  }
124  }

◆ get() [1/2]

template<class TermType >
TermPtr knowrob::groundable< TermType >::get ( ) const
inline
Returns
either the grounding or the variable.

Definition at line 69 of file groundable.h.

69  {
70  if (has_grounding()) {
71  return grounded();
72  } else {
73  return variable();
74  }
75  }
bool has_grounding() const
Definition: groundable.h:92

◆ get() [2/2]

template<class TermType >
TermPtr knowrob::groundable< TermType >::get ( ) const
inline
Returns
either the grounding or the variable.

Definition at line 69 of file groundable.h.

69  {
70  if (has_grounding()) {
71  return grounded();
72  } else {
73  return variable();
74  }
75  }

◆ grounded() [1/2]

template<class TermType >
auto knowrob::groundable< TermType >::grounded ( ) const
inline
Returns
the grounding or nullptr if this groundable is a variable.

Definition at line 102 of file groundable.h.

102 { return grounded_; }

◆ grounded() [2/2]

template<class TermType >
auto knowrob::groundable< TermType >::grounded ( ) const
inline
Returns
the grounding or nullptr if this groundable is a variable.

Definition at line 102 of file groundable.h.

102 { return grounded_; }

◆ has_grounding() [1/2]

template<class TermType >
bool knowrob::groundable< TermType >::has_grounding ( ) const
inline
Returns
true if this groundable has a grounding.

Definition at line 92 of file groundable.h.

92 { return grounded_ != nullptr; }

◆ has_grounding() [2/2]

template<class TermType >
bool knowrob::groundable< TermType >::has_grounding ( ) const
inline
Returns
true if this groundable has a grounding.

Definition at line 92 of file groundable.h.

92 { return grounded_ != nullptr; }

◆ has_variable() [1/2]

template<class TermType >
bool knowrob::groundable< TermType >::has_variable ( ) const
inline
Returns
true if this groundable has a variable.

Definition at line 97 of file groundable.h.

97 { return !has_grounding() && variable_ != nullptr; }

◆ has_variable() [2/2]

template<class TermType >
bool knowrob::groundable< TermType >::has_variable ( ) const
inline
Returns
true if this groundable has a variable.

Definition at line 97 of file groundable.h.

97 { return !has_grounding() && variable_ != nullptr; }

◆ operator bool() [1/2]

template<class TermType >
knowrob::groundable< TermType >::operator bool ( ) const
inlineexplicit
Returns
true if this groundable has a grounding.

Definition at line 64 of file groundable.h.

64 { return has_grounding() || has_variable(); }
bool has_variable() const
Definition: groundable.h:97

◆ operator bool() [2/2]

template<class TermType >
knowrob::groundable< TermType >::operator bool ( ) const
inlineexplicit
Returns
true if this groundable has a grounding.

Definition at line 64 of file groundable.h.

64 { return has_grounding() || has_variable(); }

◆ operator()() [1/2]

template<class TermType >
TermPtr knowrob::groundable< TermType >::operator() ( ) const
inline
Returns
either the grounding or the variable.

Definition at line 42 of file groundable.h.

42 { return get(); }
TermPtr get() const
Definition: groundable.h:69

◆ operator()() [2/2]

template<class TermType >
TermPtr knowrob::groundable< TermType >::operator() ( ) const
inline
Returns
either the grounding or the variable.

Definition at line 42 of file groundable.h.

42 { return get(); }

◆ operator*() [1/2]

template<class TermType >
TermPtr knowrob::groundable< TermType >::operator* ( ) const
inline
Returns
either the grounding or the variable.

Definition at line 37 of file groundable.h.

37 { return get(); }

◆ operator*() [2/2]

template<class TermType >
TermPtr knowrob::groundable< TermType >::operator* ( ) const
inline
Returns
either the grounding or the variable.

Definition at line 37 of file groundable.h.

37 { return get(); }

◆ operator->() [1/2]

template<class TermType >
Term* knowrob::groundable< TermType >::operator-> ( ) const
inline
Returns
either the grounding or the variable.

Definition at line 47 of file groundable.h.

47 { return get().get(); }

◆ operator->() [2/2]

template<class TermType >
Term* knowrob::groundable< TermType >::operator-> ( ) const
inline
Returns
either the grounding or the variable.

Definition at line 47 of file groundable.h.

47 { return get().get(); }

◆ operator=() [1/4]

template<class TermType >
groundable& knowrob::groundable< TermType >::operator= ( const std::shared_ptr< TermType > &  grounded)
inline

Toggle the state of this groundable to "grounded" and assign the grounding.

Parameters
groundeda grounding.

Definition at line 53 of file groundable.h.

53 { set_grounded(grounded); return *this; }
void set_grounded(const std::shared_ptr< TermType > &grounded)
Definition: groundable.h:81

◆ operator=() [2/4]

template<class TermType >
groundable& knowrob::groundable< TermType >::operator= ( const std::shared_ptr< TermType > &  grounded)
inline

Toggle the state of this groundable to "grounded" and assign the grounding.

Parameters
groundeda grounding.

Definition at line 53 of file groundable.h.

53 { set_grounded(grounded); return *this; }

◆ operator=() [3/4]

template<class TermType >
groundable& knowrob::groundable< TermType >::operator= ( const std::shared_ptr< Variable > &  variable)
inline

Toggle the state of this groundable to "variable" and assign the variable.

Parameters
variablea variable.

Definition at line 59 of file groundable.h.

59 { set_variable(variable); return *this; }
void set_variable(const std::shared_ptr< Variable > &variable)
Definition: groundable.h:87

◆ operator=() [4/4]

template<class TermType >
groundable& knowrob::groundable< TermType >::operator= ( const std::shared_ptr< Variable > &  variable)
inline

Toggle the state of this groundable to "variable" and assign the variable.

Parameters
variablea variable.

Definition at line 59 of file groundable.h.

59 { set_variable(variable); return *this; }

◆ set_grounded() [1/2]

template<class TermType >
void knowrob::groundable< TermType >::set_grounded ( const std::shared_ptr< TermType > &  grounded)
inline

Toggle the state of this groundable to "grounded" and assign the grounding.

Parameters
groundeda grounding.

Definition at line 81 of file groundable.h.

81 { grounded_ = grounded; variable_ = nullptr; }

◆ set_grounded() [2/2]

template<class TermType >
void knowrob::groundable< TermType >::set_grounded ( const std::shared_ptr< TermType > &  grounded)
inline

Toggle the state of this groundable to "grounded" and assign the grounding.

Parameters
groundeda grounding.

Definition at line 81 of file groundable.h.

81 { grounded_ = grounded; variable_ = nullptr; }

◆ set_variable() [1/2]

template<class TermType >
void knowrob::groundable< TermType >::set_variable ( const std::shared_ptr< Variable > &  variable)
inline

Toggle the state of this groundable to "variable" and assign the variable.

Parameters
variablea variable.

Definition at line 87 of file groundable.h.

87 { grounded_ = nullptr; variable_ = variable; }

◆ set_variable() [2/2]

template<class TermType >
void knowrob::groundable< TermType >::set_variable ( const std::shared_ptr< Variable > &  variable)
inline

Toggle the state of this groundable to "variable" and assign the variable.

Parameters
variablea variable.

Definition at line 87 of file groundable.h.

87 { grounded_ = nullptr; variable_ = variable; }

◆ variable() [1/2]

template<class TermType >
auto knowrob::groundable< TermType >::variable ( ) const
inline
Returns
the variable or nullptr if this groundable is grounded.

Definition at line 107 of file groundable.h.

107 { return variable_; }

◆ variable() [2/2]

template<class TermType >
auto knowrob::groundable< TermType >::variable ( ) const
inline
Returns
the variable or nullptr if this groundable is grounded.

Definition at line 107 of file groundable.h.

107 { return variable_; }

Member Data Documentation

◆ grounded_

template<class TermType >
std::shared_ptr< TermType > knowrob::groundable< TermType >::grounded_
protected

Definition at line 127 of file groundable.h.

◆ variable_

template<class TermType >
std::shared_ptr< Variable > knowrob::groundable< TermType >::variable_
protected

Definition at line 128 of file groundable.h.


The documentation for this class was generated from the following file: