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

#include <Predicate.h>

Inheritance diagram for knowrob::Predicate:
Collaboration diagram for knowrob::Predicate:

Public Member Functions

 Predicate (std::string_view functor, const std::vector< TermPtr > &arguments={})
 
 Predicate (AtomPtr functor, const std::vector< TermPtr > &arguments={})
 
auto & functor () const
 
auto arity () const
 
const std::vector< TermPtr > & arguments () const
 
const std::set< std::string_view > & variables ()
 
void write (std::ostream &os) const override
 
size_t hash () const
 
 Predicate (std::string_view functor, const std::vector< TermPtr > &arguments={})
 
 Predicate (AtomPtr functor, const std::vector< TermPtr > &arguments={})
 
auto & functor () const
 
auto arity () const
 
const std::vector< TermPtr > & arguments () const
 
const std::set< std::string_view > & variables ()
 
void write (std::ostream &os) const override
 
size_t hash () const
 
- Public Member Functions inherited from knowrob::Formula
 Formula (const FormulaType &type)
 
virtual ~Formula ()=default
 
bool operator== (const Formula &other) const
 
FormulaType type () const
 
bool isAtomic () const
 
bool isGround () const
 
bool isTop () const
 
bool isBottom () const
 
 Formula (const FormulaType &type)
 
virtual ~Formula ()=default
 
bool operator== (const Formula &other) const
 
FormulaType type () const
 
bool isAtomic () const
 
bool isGround () const
 
bool isTop () const
 
bool isBottom () const
 
- 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 FunctionPtr toFunction (const std::shared_ptr< Predicate > &predicate)
 
static std::shared_ptr< PredicatefromFunction (const FunctionPtr &fn)
 
static FunctionPtr toFunction (const std::shared_ptr< Predicate > &predicate)
 
static std::shared_ptr< PredicatefromFunction (const FunctionPtr &fn)
 

Protected Member Functions

std::set< std::string_view > getVariables1 () const
 
bool isEqual (const Formula &other) const override
 
std::set< std::string_view > getVariables1 () const
 
bool isEqual (const Formula &other) const override
 

Protected Attributes

const AtomPtr functor_
 
const std::vector< TermPtrarguments_
 
const std::set< std::string_view > variables_
 
- Protected Attributes inherited from knowrob::Formula
const FormulaType type_
 
bool isGround_ = true
 

Detailed Description

A predicate with a functor and a number of term arguments.

Definition at line 22 of file Predicate.h.

Constructor & Destructor Documentation

◆ Predicate() [1/4]

Predicate::Predicate ( std::string_view  functor,
const std::vector< TermPtr > &  arguments = {} 
)
explicit
Parameters
functorthe functor name.
argumentsvector of predicate arguments.

Definition at line 14 of file Predicate.cpp.

16 }
static std::shared_ptr< knowrob::Atom > Tabled(std::string_view stringForm)
Definition: Atom.cpp:40
const std::vector< TermPtr > & arguments() const
Definition: Predicate.h:52
auto & functor() const
Definition: Predicate.h:40
Predicate(std::string_view functor, const std::vector< TermPtr > &arguments={})
Definition: Predicate.cpp:14

◆ Predicate() [2/4]

Predicate::Predicate ( AtomPtr  functor,
const std::vector< TermPtr > &  arguments = {} 
)
explicit
Parameters
functorthe functor name.
argumentsvector of predicate arguments.

Definition at line 18 of file Predicate.cpp.

20  functor_(std::move(functor)),
23  isGround_ = variables_.empty();
24 }
Formula(const FormulaType &type)
Definition: Formula.cpp:22
bool isGround_
Definition: Formula.h:78
const AtomPtr functor_
Definition: Predicate.h:68
const std::set< std::string_view > variables_
Definition: Predicate.h:70
std::set< std::string_view > getVariables1() const
Definition: Predicate.cpp:38
const std::vector< TermPtr > arguments_
Definition: Predicate.h:69

◆ Predicate() [3/4]

knowrob::Predicate::Predicate ( std::string_view  functor,
const std::vector< TermPtr > &  arguments = {} 
)
explicit
Parameters
functorthe functor name.
argumentsvector of predicate arguments.

◆ Predicate() [4/4]

knowrob::Predicate::Predicate ( AtomPtr  functor,
const std::vector< TermPtr > &  arguments = {} 
)
explicit
Parameters
functorthe functor name.
argumentsvector of predicate arguments.

Member Function Documentation

◆ arguments() [1/2]

const std::vector<TermPtr>& knowrob::Predicate::arguments ( ) const
inline

Get the arguments of this predicate.

Returns
a vector of predicate arguments.

Definition at line 52 of file Predicate.h.

52 { return arguments_; }

◆ arguments() [2/2]

const std::vector<TermPtr>& knowrob::Predicate::arguments ( ) const
inline

Get the arguments of this predicate.

Returns
a vector of predicate arguments.

Definition at line 52 of file Predicate.h.

52 { return arguments_; }

◆ arity() [1/2]

auto knowrob::Predicate::arity ( ) const
inline

Get the arity of this predicate.

Returns
the arity.

Definition at line 46 of file Predicate.h.

46 { return arguments().size(); }

◆ arity() [2/2]

auto knowrob::Predicate::arity ( ) const
inline

Get the arity of this predicate.

Returns
the arity.

Definition at line 46 of file Predicate.h.

46 { return arguments().size(); }

◆ fromFunction() [1/2]

std::shared_ptr< Predicate > Predicate::fromFunction ( const FunctionPtr fn)
static

Definition at line 89 of file Predicate.cpp.

89  {
90  return std::make_shared<Predicate>(fn->functor(), fn->arguments());
91 }

◆ fromFunction() [2/2]

static std::shared_ptr<Predicate> knowrob::Predicate::fromFunction ( const FunctionPtr fn)
static

◆ functor() [1/2]

auto& knowrob::Predicate::functor ( ) const
inline

Get the functor of this predicate.

Returns
the functor.

Definition at line 40 of file Predicate.h.

40 { return functor_; }

◆ functor() [2/2]

auto& knowrob::Predicate::functor ( ) const
inline

Get the functor of this predicate.

Returns
the functor.

Definition at line 40 of file Predicate.h.

40 { return functor_; }

◆ getVariables1() [1/2]

std::set< std::string_view > Predicate::getVariables1 ( ) const
protected

Definition at line 38 of file Predicate.cpp.

38  {
39  std::set<std::string_view> out;
40  for (auto &arg: arguments_) {
41  out.insert(arg->variables().begin(), arg->variables().end());
42  }
43  return out;
44 }

◆ getVariables1() [2/2]

std::set<std::string_view> knowrob::Predicate::getVariables1 ( ) const
protected

◆ hash() [1/2]

size_t Predicate::hash ( ) const

Definition at line 46 of file Predicate.cpp.

46  {
47  static const auto GOLDEN_RATIO_HASH = static_cast<size_t>(0x9e3779b9);
48  auto seed = static_cast<size_t>(0);
49 
50  // Combine the hashes.
51  // The function (a ^ (b + GOLDEN_RATIO_HASH + (a << 6) + (a >> 2))) is known to
52  // give a good distribution of hash values across the range of size_t.
53  //
54  seed ^= std::hash<std::string_view>{}(functor_->stringForm()) + GOLDEN_RATIO_HASH + (seed << 6) + (seed >> 2);
55  for (auto &arg: arguments_) {
56  seed ^= arg->hash() + GOLDEN_RATIO_HASH + (seed << 6) + (seed >> 2);
57  }
58 
59  return seed;
60 }

◆ hash() [2/2]

size_t knowrob::Predicate::hash ( ) const

◆ isEqual() [1/2]

bool Predicate::isEqual ( const Formula other) const
overrideprotectedvirtual

Implements knowrob::Formula.

Reimplemented in knowrob::Top, knowrob::Bottom, and knowrob::Top.

Definition at line 26 of file Predicate.cpp.

26  {
27  const auto &x = static_cast<const Predicate &>(other); // NOLINT
28  if (*functor() == *x.functor() && arguments_.size() == x.arguments_.size()) {
29  for (std::size_t i = 0; i < arguments_.size(); ++i) {
30  if (!(*(arguments_[i]) == *(x.arguments_[i]))) return false;
31  }
32  return true;
33  } else {
34  return false;
35  }
36 }

◆ isEqual() [2/2]

bool knowrob::Predicate::isEqual ( const Formula other) const
overrideprotectedvirtual

Implements knowrob::Formula.

Reimplemented in knowrob::Top.

◆ toFunction() [1/2]

FunctionPtr Predicate::toFunction ( const std::shared_ptr< Predicate > &  predicate)
static

Definition at line 85 of file Predicate.cpp.

85  {
86  return std::make_shared<Function>(predicate->functor(), predicate->arguments());
87 }
PredicateRule & predicate()
Definition: formula.cpp:221

◆ toFunction() [2/2]

static FunctionPtr knowrob::Predicate::toFunction ( const std::shared_ptr< Predicate > &  predicate)
static

◆ variables() [1/2]

const std::set<std::string_view>& knowrob::Predicate::variables ( )
inline

Definition at line 55 of file Predicate.h.

55 { return variables_; }

◆ variables() [2/2]

const std::set<std::string_view>& knowrob::Predicate::variables ( )
inline

Definition at line 55 of file Predicate.h.

55 { return variables_; }

◆ write() [1/2]

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

Print this object to a stream.

Parameters
osthe stream to print to.

Implements knowrob::Printable.

Reimplemented in knowrob::Top, knowrob::Bottom, and knowrob::Top.

Definition at line 62 of file Predicate.cpp.

62  {
63  static const auto a_triple = Atom::Tabled("triple");
64 
65  if (arity() == 3 &&
66  arguments_[1]->termType() != TermType::VARIABLE &&
67  *functor_ == *a_triple) {
68  os << *arguments_[1] << '(' << *arguments_[0] << ',' << *arguments_[2] << ')';
69  } else {
70  os << *functor_;
71  if (!arguments_.empty()) {
72  os << '(';
73  for (uint32_t i = 0; i < arguments_.size(); i++) {
74  Term *t = arguments_[i].get();
75  os << (*t);
76  if (i + 1 < arguments_.size()) {
77  os << ',' << ' ';
78  }
79  }
80  os << ')';
81  }
82  }
83 }
auto arity() const
Definition: Predicate.h:46

◆ write() [2/2]

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

Print this object to a stream.

Parameters
osthe stream to print to.

Implements knowrob::Printable.

Reimplemented in knowrob::Top.

Member Data Documentation

◆ arguments_

const std::vector< TermPtr > knowrob::Predicate::arguments_
protected

Definition at line 69 of file Predicate.h.

◆ functor_

const AtomPtr knowrob::Predicate::functor_
protected

Definition at line 68 of file Predicate.h.

◆ variables_

const std::set< std::string_view > knowrob::Predicate::variables_
protected

Definition at line 70 of file Predicate.h.


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