knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
Predicate.cpp
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 #include <utility>
7 
8 #include "knowrob/formulas/Predicate.h"
9 #include "knowrob/terms/Function.h"
10 #include "knowrob/integration/python/utils.h"
11 
12 using namespace knowrob;
13 
14 Predicate::Predicate(std::string_view functor, const std::vector<TermPtr> &arguments)
15  : Predicate(Atom::Tabled(functor), arguments) {
16 }
17 
18 Predicate::Predicate(AtomPtr functor, const std::vector<TermPtr> &arguments)
20  functor_(std::move(functor)),
21  arguments_(arguments),
22  variables_(getVariables1()) {
23  isGround_ = variables_.empty();
24 }
25 
26 bool Predicate::isEqual(const Formula &other) const {
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 }
37 
38 std::set<std::string_view> Predicate::getVariables1() const {
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 }
45 
46 size_t Predicate::hash() const {
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 }
61 
62 void Predicate::write(std::ostream &os) const {
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 }
84 
85 FunctionPtr Predicate::toFunction(const std::shared_ptr<Predicate> &predicate) {
86  return std::make_shared<Function>(predicate->functor(), predicate->arguments());
87 }
88 
89 std::shared_ptr<Predicate> Predicate::fromFunction(const FunctionPtr &fn) {
90  return std::make_shared<Predicate>(fn->functor(), fn->arguments());
91 }
92 
93 namespace knowrob::py {
94  template<>
96  using namespace boost::python;
97  class_<Predicate, std::shared_ptr<Predicate>, bases<Formula>>
98  ("Predicate", init<std::string_view, const std::vector<TermPtr> &>())
99  .def(init<const AtomPtr &, const std::vector<TermPtr> &>())
100  .def("arguments", &Predicate::arguments, return_value_policy<copy_const_reference>())
101  .def("functor", &Predicate::functor, return_value_policy<copy_const_reference>());
102  }
103 }
static std::shared_ptr< knowrob::Atom > Tabled(std::string_view stringForm)
Definition: Atom.cpp:40
bool isGround_
Definition: Formula.h:78
const AtomPtr functor_
Definition: Predicate.h:68
auto arity() const
Definition: Predicate.h:46
size_t hash() const
Definition: Predicate.cpp:46
const std::set< std::string_view > variables_
Definition: Predicate.h:70
const std::vector< TermPtr > & arguments() const
Definition: Predicate.h:52
auto & functor() const
Definition: Predicate.h:40
std::set< std::string_view > getVariables1() const
Definition: Predicate.cpp:38
bool isEqual(const Formula &other) const override
Definition: Predicate.cpp:26
static FunctionPtr toFunction(const std::shared_ptr< Predicate > &predicate)
Definition: Predicate.cpp:85
static std::shared_ptr< Predicate > fromFunction(const FunctionPtr &fn)
Definition: Predicate.cpp:89
const std::vector< TermPtr > arguments_
Definition: Predicate.h:69
Predicate(std::string_view functor, const std::vector< TermPtr > &arguments={})
Definition: Predicate.cpp:14
void write(std::ostream &os) const override
Definition: Predicate.cpp:62
PREDICATE(mng_collections, 2)
Definition: mongo_kb.cpp:48
PredicateRule & predicate()
Definition: formula.cpp:221
void createType< Predicate >()
Definition: Predicate.cpp:95
std::shared_ptr< Function > FunctionPtr
Definition: Function.h:73
std::shared_ptr< Atom > AtomPtr
Definition: Atom.h:69
FormulaType
Definition: Formula.h:17