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

#include <Unifier.h>

Inheritance diagram for knowrob::Unifier:
Collaboration diagram for knowrob::Unifier:

Public Member Functions

 Unifier (const TermPtr &t0, const TermPtr &t1)
 
bool exists () const
 
TermPtr apply ()
 
 Unifier (const TermPtr &t0, const TermPtr &t1)
 
bool exists () const
 
TermPtr apply ()
 
- Public Member Functions inherited from knowrob::Bindings
 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
 

Protected Member Functions

bool unify (const TermPtr &t0, const TermPtr &t1)
 
bool unify (const std::shared_ptr< Variable > &var, const TermPtr &t)
 
bool unify (const TermPtr &t0, const TermPtr &t1)
 
bool unify (const std::shared_ptr< Variable > &var, const TermPtr &t)
 

Protected Attributes

TermPtr t0_
 
TermPtr t1_
 
bool exists_
 
- Protected Attributes inherited from knowrob::Bindings
Map mapping_
 

Additional Inherited Members

- Public Types inherited from knowrob::Bindings
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 >
 
- Static Public Member Functions inherited from knowrob::Bindings
static std::shared_ptr< const BindingsemptyBindings ()
 
static std::shared_ptr< const BindingsemptyBindings ()
 

Detailed Description

A substitution that unifies some terms.

Definition at line 18 of file Unifier.h.

Constructor & Destructor Documentation

◆ Unifier() [1/2]

Unifier::Unifier ( const TermPtr t0,
const TermPtr t1 
)

Compute a unifier of two terms.

Parameters
t0a term.
t1a term.

Definition at line 16 of file Unifier.cpp.

17  : Bindings(),
18  t0_(t0),
19  t1_(t1),
20  exists_(unify(t0, t1)) {
21 }
Bindings()=default
TermPtr t1_
Definition: Unifier.h:40
bool unify(const TermPtr &t0, const TermPtr &t1)
Definition: Unifier.cpp:23
TermPtr t0_
Definition: Unifier.h:39

◆ Unifier() [2/2]

knowrob::Unifier::Unifier ( const TermPtr t0,
const TermPtr t1 
)

Compute a unifier of two terms.

Parameters
t0a term.
t1a term.

Member Function Documentation

◆ apply() [1/2]

TermPtr Unifier::apply ( )

Applies the unifier to one of the unified terms.

Returns
an instance of the unified terms.

Definition at line 74 of file Unifier.cpp.

74  {
75  if (!exists_) {
76  // no unifier exists
77  return Bottom::get()->functor();
78  } else if (t0_->isGround() || t1_->termType() == TermType::VARIABLE) {
79  // empty unifier, or only substitutions in t1
80  return t0_;
81  } else if (t1_->isGround() || t0_->termType() == TermType::VARIABLE) {
82  // only substitutions in t0
83  return t1_;
84  } else if (t0_->termType() == TermType::FUNCTION) {
85  // both t0_ and t1_ contain variables.
86  auto &p = (
87  t0_->variables().size() < t1_->variables().size() ?
88  t0_ : t1_);
89  return applyBindings(p, *this);
90  } else {
91  KB_WARN("something went wrong during unification.");
92  return Bottom::get()->functor();
93  }
94 }
#define KB_WARN
Definition: Logger.h:27
static const std::shared_ptr< Bottom > & get()
Definition: Bottom.cpp:12
FirstOrderLiteralPtr applyBindings(const FirstOrderLiteralPtr &lit, const Bindings &bindings)

◆ apply() [2/2]

TermPtr knowrob::Unifier::apply ( )

Applies the unifier to one of the unified terms.

Returns
an instance of the unified terms.

◆ exists() [1/2]

bool knowrob::Unifier::exists ( ) const
inline
Returns
true is a unifier exists.

Definition at line 30 of file Unifier.h.

30 { return exists_; }

◆ exists() [2/2]

bool knowrob::Unifier::exists ( ) const
inline
Returns
true is a unifier exists.

Definition at line 30 of file Unifier.h.

30 { return exists_; }

◆ unify() [1/4]

bool Unifier::unify ( const std::shared_ptr< Variable > &  var,
const TermPtr t 
)
protected

Definition at line 60 of file Unifier.cpp.

60  {
61 #ifdef USE_OCCURS_CHECK
62  if (t->variables().find(var->name()) != t->variables().end()) {
63  // fail if var *occurs* in t (occurs check)
64  return false;
65  } else {
66 #endif
67  set(var, t);
68  return true;
69 #ifdef USE_OCCURS_CHECK
70  }
71 #endif
72 }
void set(const std::shared_ptr< Variable > &var, const TermPtr &term)
Definition: Bindings.cpp:52
VariableRule & var()
Definition: terms.cpp:91

◆ unify() [2/4]

bool knowrob::Unifier::unify ( const std::shared_ptr< Variable > &  var,
const TermPtr t 
)
protected

◆ unify() [3/4]

bool Unifier::unify ( const TermPtr t0,
const TermPtr t1 
)
protected

Definition at line 23 of file Unifier.cpp.

24 {
25  if (t1->termType() == TermType::VARIABLE) {
26  return unify(std::static_pointer_cast<Variable>(t1), t0);
27  } else {
28  switch (t0->termType()) {
29  case TermType::VARIABLE:
30  return unify(std::static_pointer_cast<Variable>(t0), t1);
31  case TermType::ATOMIC:
32  // if one of the terms is an atomic, the other must be an equal atomic
33  return *t0 == *t1;
34  case TermType::FUNCTION: {
35  // n-ary functions with n>0 only unify with other n-ary functions
36  if (t1->termType() != TermType::FUNCTION) {
37  return false;
38  }
39  auto *p0 = (Function *) t0.get();
40  auto *p1 = (Function *) t1.get();
41  // tests for functor equality, and matching arity
42  if (*p0->functor() != *p1->functor() ||
43  p0->arity() != p1->arity()) {
44  return false;
45  }
46  // unify all arguments
47  for (std::size_t i = 0; i < p0->arity(); ++i) {
48  if (!unify(p0->arguments()[i], p1->arguments()[i])) {
49  return false;
50  }
51  }
52  break;
53  }
54  }
55  }
56 
57  return true;
58 }

◆ unify() [4/4]

bool knowrob::Unifier::unify ( const TermPtr t0,
const TermPtr t1 
)
protected

Member Data Documentation

◆ exists_

bool knowrob::Unifier::exists_
protected

Definition at line 41 of file Unifier.h.

◆ t0_

TermPtr knowrob::Unifier::t0_
protected

Definition at line 39 of file Unifier.h.

◆ t1_

TermPtr knowrob::Unifier::t1_
protected

Definition at line 40 of file Unifier.h.


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