knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::Bindings Class Reference

#include <Bindings.h>

Inheritance diagram for knowrob::Bindings:
Collaboration diagram for knowrob::Bindings:

Public Types

using Map = std::map< std::string_view, std::pair< std::shared_ptr< Variable >, TermPtr > >
 
using VarMap = std::map< std::shared_ptr< Variable >, TermPtr >
 
using Map = std::map< std::string_view, std::pair< std::shared_ptr< Variable >, TermPtr > >
 
using VarMap = std::map< std::shared_ptr< Variable >, TermPtr >
 

Public Member Functions

 Bindings ()=default
 
 Bindings (const std::map< std::shared_ptr< Variable >, TermPtr > &mapping)
 
 ~Bindings () override=default
 
bool operator== (const Bindings &other) const
 
void operator+= (const Bindings &other)
 
bool empty () const
 
auto size () const
 
auto begin () const
 
auto end () const
 
auto find (std::string_view varName) const
 
void set (const std::shared_ptr< Variable > &var, const TermPtr &term)
 
const TermPtrget (std::string_view varName) const
 
std::shared_ptr< AtomicgetAtomic (std::string_view varName) const
 
bool contains (std::string_view varName) const
 
bool unifyWith (const Bindings &other)
 
bool isConsistentWith (const Bindings &other) const
 
size_t hash () const
 
void write (std::ostream &os) const override
 
 Bindings ()=default
 
 Bindings (const std::map< std::shared_ptr< Variable >, TermPtr > &mapping)
 
 ~Bindings () override=default
 
bool operator== (const Bindings &other) const
 
void operator+= (const Bindings &other)
 
bool empty () const
 
auto size () const
 
auto begin () const
 
auto end () const
 
auto find (std::string_view varName) const
 
void set (const std::shared_ptr< Variable > &var, const TermPtr &term)
 
const TermPtrget (std::string_view varName) const
 
std::shared_ptr< AtomicgetAtomic (std::string_view varName) const
 
bool contains (std::string_view varName) const
 
bool unifyWith (const Bindings &other)
 
bool isConsistentWith (const Bindings &other) const
 
size_t hash () const
 
void write (std::ostream &os) const override
 
- Public Member Functions inherited from knowrob::Printable
virtual ~Printable ()=default
 
virtual std::string format () const
 
virtual ~Printable ()=default
 
virtual std::string format () const
 

Static Public Member Functions

static std::shared_ptr< const BindingsemptyBindings ()
 
static std::shared_ptr< const BindingsemptyBindings ()
 

Protected Attributes

Map mapping_
 

Detailed Description

A substitution is a mapping from variable names to terms. For example, {x1 -> t1, ..., xn -> tn} represents a substitution of each variable xi with the corresponding term ti. Applying a substitution to a term t means to replace occurrences of each xi with ti. The resulting term is referred to as an instance of t.

Definition at line 28 of file Bindings.h.

Member Typedef Documentation

◆ Map [1/2]

using knowrob::Bindings::Map = std::map<std::string_view, std::pair<std::shared_ptr<Variable>, TermPtr> >

A mapping from variable names to terms.

Definition at line 33 of file Bindings.h.

◆ Map [2/2]

using knowrob::Bindings::Map = std::map<std::string_view, std::pair<std::shared_ptr<Variable>, TermPtr> >

A mapping from variable names to terms.

Definition at line 33 of file Bindings.h.

◆ VarMap [1/2]

using knowrob::Bindings::VarMap = std::map<std::shared_ptr<Variable>, TermPtr>

Definition at line 34 of file Bindings.h.

◆ VarMap [2/2]

using knowrob::Bindings::VarMap = std::map<std::shared_ptr<Variable>, TermPtr>

Definition at line 34 of file Bindings.h.

Constructor & Destructor Documentation

◆ Bindings() [1/4]

knowrob::Bindings::Bindings ( )
default

◆ Bindings() [2/4]

Bindings::Bindings ( const std::map< std::shared_ptr< Variable >, TermPtr > &  mapping)
explicit
Parameters
mappinga mapping from variables to terms.

Definition at line 22 of file Bindings.cpp.

22  {
23  for (const auto &pair: mapping) {
24  set(pair.first, pair.second);
25  }
26 }
void set(const std::shared_ptr< Variable > &var, const TermPtr &term)
Definition: Bindings.cpp:52

◆ ~Bindings() [1/2]

knowrob::Bindings::~Bindings ( )
overridedefault

◆ Bindings() [3/4]

knowrob::Bindings::Bindings ( )
default

◆ Bindings() [4/4]

knowrob::Bindings::Bindings ( const std::map< std::shared_ptr< Variable >, TermPtr > &  mapping)
explicit
Parameters
mappinga mapping from variables to terms.

◆ ~Bindings() [2/2]

knowrob::Bindings::~Bindings ( )
overridedefault

Member Function Documentation

◆ begin() [1/2]

auto knowrob::Bindings::begin ( ) const
inline
Returns
begin iterator of substitution.

Definition at line 70 of file Bindings.h.

70 { return mapping_.begin(); }

◆ begin() [2/2]

auto knowrob::Bindings::begin ( ) const
inline
Returns
begin iterator of substitution.

Definition at line 70 of file Bindings.h.

70 { return mapping_.begin(); }

◆ contains() [1/2]

bool Bindings::contains ( std::string_view  varName) const

Returns true if the given var is mapped to a term by this substitution.

Parameters
varNamea variable.
Returns
true if this substitution isMoreGeneralThan the variable.

Definition at line 56 of file Bindings.cpp.

56  {
57  return mapping_.find(varName) != mapping_.end();
58 }

◆ contains() [2/2]

bool knowrob::Bindings::contains ( std::string_view  varName) const

Returns true if the given var is mapped to a term by this substitution.

Parameters
varNamea variable.
Returns
true if this substitution isMoreGeneralThan the variable.

◆ empty() [1/2]

bool knowrob::Bindings::empty ( ) const
inline
Returns
true if this substitution does not map a single variable to a term.

Definition at line 60 of file Bindings.h.

60 { return mapping_.empty(); }

◆ empty() [2/2]

bool knowrob::Bindings::empty ( ) const
inline
Returns
true if this substitution does not map a single variable to a term.

Definition at line 60 of file Bindings.h.

60 { return mapping_.empty(); }

◆ emptyBindings() [1/2]

std::shared_ptr< const Bindings > Bindings::emptyBindings ( )
static
Returns
a const empty substitution.

Definition at line 99 of file Bindings.cpp.

99  {
100  static const auto empty = std::make_shared<Bindings>();
101  return empty;
102 }
bool empty() const
Definition: Bindings.h:60

◆ emptyBindings() [2/2]

static std::shared_ptr<const Bindings> knowrob::Bindings::emptyBindings ( )
static
Returns
a const empty substitution.

◆ end() [1/2]

auto knowrob::Bindings::end ( ) const
inline
Returns
end iterator of substitution.

Definition at line 75 of file Bindings.h.

75 { return mapping_.end(); }

◆ end() [2/2]

auto knowrob::Bindings::end ( ) const
inline
Returns
end iterator of substitution.

Definition at line 75 of file Bindings.h.

75 { return mapping_.end(); }

◆ find() [1/2]

auto knowrob::Bindings::find ( std::string_view  varName) const
inline
Returns
a const iterator to the variable-term mapping.

Definition at line 80 of file Bindings.h.

80 { return mapping_.find(varName); }

◆ find() [2/2]

auto knowrob::Bindings::find ( std::string_view  varName) const
inline
Returns
a const iterator to the variable-term mapping.

Definition at line 80 of file Bindings.h.

80 { return mapping_.find(varName); }

◆ get() [1/2]

const TermPtr & Bindings::get ( std::string_view  varName) const

Map the name of a variable to a term. A null pointer reference is returned if the given variable is not included in the mapping.

Parameters
varNamea variable.
Returns
a term reference.

Definition at line 60 of file Bindings.cpp.

60  {
61  static const TermPtr null_term;
62 
63  auto it = mapping_.find(varName);
64  if (it != mapping_.end()) {
65  return it->second.second;
66  } else {
67  return null_term;
68  }
69 }
std::shared_ptr< Term > TermPtr
Definition: Term.h:117

◆ get() [2/2]

const TermPtr& knowrob::Bindings::get ( std::string_view  varName) const

Map the name of a variable to a term. A null pointer reference is returned if the given variable is not included in the mapping.

Parameters
varNamea variable.
Returns
a term reference.

◆ getAtomic() [1/2]

std::shared_ptr< Atomic > Bindings::getAtomic ( std::string_view  varName) const

Map the name of a variable to a term. A null pointer reference is returned if the given variable is not included in the mapping.

Parameters
varNamea variable.
Returns
a term reference.

Definition at line 71 of file Bindings.cpp.

71  {
72  static const std::shared_ptr<Atomic> null_term;
73 
74  auto term = get(varName);
75  if (term && term->termType() == TermType::ATOMIC) {
76  return std::static_pointer_cast<Atomic>(term);
77  } else {
78  return null_term;
79  }
80 }
const TermPtr & get(std::string_view varName) const
Definition: Bindings.cpp:60
TermRule & term()
Definition: terms.cpp:136

◆ getAtomic() [2/2]

std::shared_ptr<Atomic> knowrob::Bindings::getAtomic ( std::string_view  varName) const

Map the name of a variable to a term. A null pointer reference is returned if the given variable is not included in the mapping.

Parameters
varNamea variable.
Returns
a term reference.

◆ hash() [1/2]

size_t Bindings::hash ( ) const
Returns
the hash of this.

Definition at line 82 of file Bindings.cpp.

82  {
83  auto seed = static_cast<size_t>(0);
84 
85  for (const auto &item: mapping_) {
86  // Compute the hash of the key using the in-built hash function for string.
87  hashCombine(seed, std::hash<std::string_view>{}(item.first));
88 
89  auto value_hash = static_cast<size_t>(0);
90  if (item.second.second) {
91  value_hash = item.second.second->hash();
92  }
93  hashCombine(seed, value_hash);
94  }
95 
96  return seed;
97 }
void hashCombine(std::size_t &seed, const std::size_t &v)
Definition: knowrob.cpp:39

◆ hash() [2/2]

size_t knowrob::Bindings::hash ( ) const
Returns
the hash of this.

◆ isConsistentWith() [1/2]

bool Bindings::isConsistentWith ( const Bindings other) const

Check if this substitution is consistent with another substitution, i.e. if both can be unified.

Parameters
otheranother substitution.
Returns
true if this substitution is consistent with the other substitution.

Definition at line 104 of file Bindings.cpp.

104  {
105  const Map *map_small;
106  const Map *map_large;
107  if (mapping_.size() < other.mapping_.size()) {
108  map_small = &mapping_;
109  map_large = &other.mapping_;
110  } else {
111  map_small = &other.mapping_;
112  map_large = &mapping_;
113  }
114 
115  for (const auto &pair: *map_small) {
116  auto it = map_large->find(pair.first);
117  if (it != map_large->end()) {
118  auto t0 = pair.second.second;
119  auto t1 = it->second.second;
120  Unifier sigma(t0, t1);
121  if (!sigma.exists()) {
122  return false;
123  }
124  }
125  }
126 
127  return true;
128 }
std::map< std::string_view, std::pair< std::shared_ptr< Variable >, TermPtr > > Map
Definition: Bindings.h:33

◆ isConsistentWith() [2/2]

bool knowrob::Bindings::isConsistentWith ( const Bindings other) const

Check if this substitution is consistent with another substitution, i.e. if both can be unified.

Parameters
otheranother substitution.
Returns
true if this substitution is consistent with the other substitution.

◆ operator+=() [1/2]

void Bindings::operator+= ( const Bindings other)

Combine with another substitution.

Parameters
otheranother substitution.

Definition at line 46 of file Bindings.cpp.

46  {
47  for (auto &x: other.mapping_) {
48  set(x.second.first, x.second.second);
49  }
50 }

◆ operator+=() [2/2]

void knowrob::Bindings::operator+= ( const Bindings other)

Combine with another substitution.

Parameters
otheranother substitution.

◆ operator==() [1/2]

bool Bindings::operator== ( const Bindings other) const
Parameters
otheranother substitution.
Returns
true if this substitution is equal to the other substitution.

Definition at line 28 of file Bindings.cpp.

28  {
29  if (mapping_.size() != other.mapping_.size()) {
30  return false;
31  }
32 
33  for (auto &x: mapping_) {
34  auto &val1 = x.second.second;
35  auto &val2 = other.get(x.first);
36  if (val1 && val2) {
37  if (!(*val1 == *val2)) return false;
38  } else if (val1 || val2) {
39  return false;
40  }
41  }
42 
43  return true;
44 }

◆ operator==() [2/2]

bool knowrob::Bindings::operator== ( const Bindings other) const
Parameters
otheranother substitution.
Returns
true if this substitution is equal to the other substitution.

◆ set() [1/2]

void Bindings::set ( const std::shared_ptr< Variable > &  var,
const TermPtr term 
)

Map a variable to a term.

Parameters
vara variable.
terma term.

Definition at line 52 of file Bindings.cpp.

52  {
53  mapping_.insert({var->name(), {var, term}});
54 }
VariableRule & var()
Definition: terms.cpp:91

◆ set() [2/2]

void knowrob::Bindings::set ( const std::shared_ptr< Variable > &  var,
const TermPtr term 
)

Map a variable to a term.

Parameters
vara variable.
terma term.

◆ size() [1/2]

auto knowrob::Bindings::size ( ) const
inline
Returns
number of variables mapped to terms.

Definition at line 65 of file Bindings.h.

65 { return mapping_.size(); }

◆ size() [2/2]

auto knowrob::Bindings::size ( ) const
inline
Returns
number of variables mapped to terms.

Definition at line 65 of file Bindings.h.

65 { return mapping_.size(); }

◆ unifyWith() [1/2]

bool Bindings::unifyWith ( const Bindings other)

Combine with another substitution. If both substitute the same variable to some term, then the combination maps to the unification of these terms, if one exists.

Parameters
otheranother substitution
Returns
true if the operation succeeded.

Definition at line 130 of file Bindings.cpp.

130  {
131  for (const auto &pair: other.mapping_) {
132  auto it = mapping_.find(pair.first);
133  if (it == mapping_.end()) {
134  // new variable instantiation
135  mapping_.insert(pair);
136  } else {
137  // variable has already an instantiation, need to unify
138  TermPtr t0 = it->second.second;
139  TermPtr t1 = pair.second.second;
140 
141  // t0 and t1 are not syntactically equal -> compute a unifier
142  Unifier sigma(t0, t1);
143  if (sigma.exists()) {
144  // a unifier exists
145  it->second.second = sigma.apply();
146  } else {
147  // no unifier exists
148  return false;
149  }
150  }
151  }
152 
153  return true;
154 }

◆ unifyWith() [2/2]

bool knowrob::Bindings::unifyWith ( const Bindings other)

Combine with another substitution. If both substitute the same variable to some term, then the combination maps to the unification of these terms, if one exists.

Parameters
otheranother substitution
Returns
true if the operation succeeded.

◆ write() [1/2]

void Bindings::write ( std::ostream &  os) const
overridevirtual

Print this object to a stream.

Parameters
osthe stream to print to.

Implements knowrob::Printable.

Definition at line 156 of file Bindings.cpp.

156  {
157  uint32_t i = 0;
158  os << '{';
159  for (const auto &pair: mapping_) {
160  if (i++ > 0) os << ',';
161  os << pair.first << ':' << ' ' << (*pair.second.second);
162  }
163  os << '}';
164 }

◆ write() [2/2]

void knowrob::Bindings::write ( std::ostream &  os) const
overridevirtual

Print this object to a stream.

Parameters
osthe stream to print to.

Implements knowrob::Printable.

Member Data Documentation

◆ mapping_

Map knowrob::Bindings::mapping_
protected

Definition at line 147 of file Bindings.h.


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