knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
Term.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 "sstream"
7 #include "knowrob/terms/Term.h"
8 #include "knowrob/terms/Atomic.h"
9 #include "knowrob/terms/Function.h"
10 #include "knowrob/terms/Variable.h"
11 #include "knowrob/integration/python/utils.h"
12 #include "knowrob/knowrob.h"
13 
14 using namespace knowrob;
15 
16 const std::set<std::string_view> Term::noVariables_ = {};
17 
18 bool Term::isAtom() const {
19  return termType() == TermType::ATOMIC && ((Atomic *) this)->atomicType() == AtomicType::ATOM;
20 }
21 
22 bool Term::isVariable() const {
23  return termType() == TermType::VARIABLE;
24 }
25 
26 bool Term::isFunction() const {
27  return termType() == TermType::FUNCTION;
28 }
29 
30 bool Term::isNumeric() const {
31  return termType() == TermType::ATOMIC && ((Atomic *) this)->atomicType() == AtomicType::NUMERIC;
32 }
33 
34 bool Term::isString() const {
35  return termType() == TermType::ATOMIC && ((Atomic *) this)->atomicType() == AtomicType::STRING;
36 }
37 
38 size_t Term::hash() const {
39  size_t val = 0;
40  hashCombine(val, uint8_t(termType()));
41  switch (termType()) {
42  case TermType::ATOMIC:
43  hashCombine(val, ((Atomic *) this)->hashOfAtomic());
44  break;
45  case TermType::FUNCTION:
46  hashCombine(val, ((Function *) this)->hashOfFunction());
47  break;
48  case TermType::VARIABLE:
49  hashCombine(val, std::hash<std::string_view>{}(((Variable *) this)->name()));
50  break;
51  }
52  return val;
53 }
54 
55 bool Term::operator==(const Term &other) const {
56  if (this == &other) return true;
57  if (termType() != other.termType()) return false;
58  switch (termType()) {
59  case TermType::ATOMIC:
60  return ((Atomic *) this)->isSameAtomic(*((Atomic *) &other));
61  case TermType::VARIABLE:
62  return ((Variable *) this)->isSameVariable(*((Variable *) &other));
63  case TermType::FUNCTION:
64  return ((Function *) this)->isSameFunction(*((Function *) &other));
65  }
66  return false;
67 }
68 
69 namespace knowrob::py {
70  // this struct is needed because Term has pure virtual methods
71  struct TermWrap : public Term, boost::python::wrapper<Term> {
72  explicit TermWrap(PyObject *p, TermType termType) : Term(termType), self(p) {}
73 
74  const std::set<std::string_view> &
75  variables() const override { return knowrob::py::call_method<std::set<std::string_view> &>(self, "variables"); }
76 
77  private:
78  PyObject *self;
79  };
80 
81  template<>
83  using namespace boost::python;
84  enum_<TermType>("TermType")
85  .value("FUNCTION", TermType::FUNCTION)
86  .value("ATOMIC", TermType::ATOMIC)
87  .value("VARIABLE", TermType::VARIABLE)
88  .export_values();
89  class_<Term, std::shared_ptr<TermWrap>, boost::noncopyable>
90  ("Term", no_init)
91  .def("__eq__", &Term::operator==)
92  .def("__str__", &Term::format)
93  .def("__repr__", &Term::format)
94  .def("__hash__", &Term::hash)
95  .def("termType", &Term::termType)
96  .def("isAtomic", &Term::isAtomic)
97  .def("isAtom", &Term::isAtom)
98  .def("isVariable", &Term::isVariable)
99  .def("isFunction", &Term::isFunction)
100  .def("isNumeric", &Term::isNumeric)
101  .def("isString", &Term::isString)
102  .def("isIRI", &Term::isIRI)
103  .def("isBlank", &Term::isBlank)
104  .def("isGround", &Term::isGround)
105  .def("variables", pure_virtual(&Term::variables), return_value_policy<copy_const_reference>());
106  // register shared_ptr to Term
107  register_ptr_to_python<std::shared_ptr<Term> >();
108  }
109 }
virtual std::string format() const
Definition: Printable.h:32
bool isAtomic() const
Definition: Term.h:62
bool isVariable() const
Definition: Term.cpp:22
bool isAtom() const
Definition: Term.cpp:18
bool isFunction() const
Definition: Term.cpp:26
bool isGround() const
Definition: Term.h:57
bool isBlank() const
Definition: Term.h:97
bool operator==(const Term &other) const
Definition: Term.cpp:55
bool isNumeric() const
Definition: Term.cpp:30
bool isString() const
Definition: Term.cpp:34
size_t hash() const
Definition: Term.cpp:38
static const std::set< std::string_view > noVariables_
Definition: Term.h:110
TermType termType() const
Definition: Term.h:52
virtual const std::set< std::string_view > & variables() const =0
bool isIRI() const
Definition: Term.h:92
void createType< Term >()
Definition: Term.cpp:82
TermType
Definition: Term.h:18
void hashCombine(std::size_t &seed, const std::size_t &v)
Definition: knowrob.cpp:39