knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
Function.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 #include "knowrob/terms/Function.h"
8 #include "knowrob/knowrob.h"
9 #include "knowrob/integration/python/utils.h"
10 
11 using namespace knowrob;
12 
13 Function::Function(AtomPtr functor, const std::vector<TermPtr> &arguments)
15  functor_(std::move(functor)),
16  arguments_(arguments),
17  variables_(getVariables1()) {
18 }
19 
20 Function::Function(std::string_view functor, const std::vector<TermPtr> &arguments)
21  : Function(Atom::Tabled(functor), arguments) {
22 }
23 
24 std::set<std::string_view> Function::getVariables1() const {
25  std::set<std::string_view> out;
26  for (auto &arg: arguments_) {
27  out.insert(arg->variables().begin(), arg->variables().end());
28  }
29  return out;
30 }
31 
32 bool Function::isSameFunction(const Function &other) const {
33  if (arguments_.size() != other.arguments_.size() || *functor_ != *other.functor_) {
34  return false;
35  }
36  for (uint32_t i = 0; i < arguments_.size(); i++) {
37  if (*arguments_[i] != *other.arguments_[i]) {
38  return false;
39  }
40  }
41  return true;
42 }
43 
44 size_t Function::hashOfFunction() const {
45  auto seed = static_cast<size_t>(0);
46  hashCombine(seed, functor_->hash());
47  for (auto &arg: arguments_) {
48  hashCombine(seed, arg->hash());
49  }
50  return seed;
51 }
52 
53 void Function::write(std::ostream &os) const {
54  os << *functor_;
55  if (!arguments_.empty()) {
56  os << '(';
57  for (uint32_t i = 0; i < arguments_.size(); i++) {
58  os << *arguments_[i];
59  if (i + 1 < arguments_.size()) {
60  os << ',' << ' ';
61  }
62  }
63  os << ')';
64  }
65 }
66 
67 namespace knowrob::py {
68  template<>
70  using namespace boost::python;
71  class_<Function, std::shared_ptr<Function>, bases<Term>>
72  ("Function", init<std::string_view, const std::vector<TermPtr> &>())
73  .def(init<const AtomPtr &, const std::vector<TermPtr> &>())
74  .def("functor", &Function::functor, return_value_policy<copy_const_reference>())
75  .def("arguments", &Function::arguments, return_value_policy<copy_const_reference>());
76  }
77 }
Function(AtomPtr functor, const std::vector< TermPtr > &arguments)
Definition: Function.cpp:13
bool isSameFunction(const Function &other) const
Definition: Function.cpp:32
const std::shared_ptr< Atom > functor_
Definition: Function.h:66
const std::vector< TermPtr > arguments_
Definition: Function.h:67
void write(std::ostream &os) const override
Definition: Function.cpp:53
auto & arguments() const
Definition: Function.h:47
auto & functor() const
Definition: Function.h:42
std::set< std::string_view > getVariables1() const
Definition: Function.cpp:24
size_t hashOfFunction() const
Definition: Function.cpp:44
void createType< Function >()
Definition: Function.cpp:69
TermType
Definition: Term.h:18
std::shared_ptr< Atom > AtomPtr
Definition: Atom.h:69
void hashCombine(std::size_t &seed, const std::size_t &v)
Definition: knowrob.cpp:39