knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
GraphBuiltin.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 "knowrob/semweb/GraphBuiltin.h"
7 #include "knowrob/integration/python/utils.h"
8 #include "knowrob/terms/Numeric.h"
9 
10 using namespace knowrob;
11 
12 static inline bool isSmaller(const TermPtr &a, const TermPtr &b) {
13  if (a->isNumeric() && b->isNumeric()) {
14  auto a_numeric = std::static_pointer_cast<Numeric>(a);
15  auto b_numeric = std::static_pointer_cast<Numeric>(b);
16  return a_numeric->asDouble() < b_numeric->asDouble();
17  }
18  return false;
19 }
20 
21 static inline bool isGreater(const TermPtr &a, const TermPtr &b) {
22  if (a->isNumeric() && b->isNumeric()) {
23  auto a_numeric = std::static_pointer_cast<Numeric>(a);
24  auto b_numeric = std::static_pointer_cast<Numeric>(b);
25  return a_numeric->asDouble() > b_numeric->asDouble();
26  }
27  return false;
28 }
29 
30 bool GraphBuiltin::apply(const std::shared_ptr<Bindings> &bindings) const {
31  switch (builtinType()) {
33  auto valueTerm = applyBindings(arguments_[0], *bindings);
34  bindings->set(bindVar(), valueTerm);
35  return true;
36  }
37  case GraphBuiltinType::Min: {
38  auto a = applyBindings(arguments_[0], *bindings);
39  auto b = applyBindings(arguments_[1], *bindings);
40  if (a->isGround() && b->isGround()) {
41  if (isSmaller(a, b)) {
42  bindings->set(bindVar(), a);
43  } else {
44  bindings->set(bindVar(), b);
45  }
46  }
47  return true;
48  }
49  case GraphBuiltinType::Max: {
50  auto a = applyBindings(arguments_[0], *bindings);
51  auto b = applyBindings(arguments_[1], *bindings);
52  if (a->isGround() && b->isGround()) {
53  if (isGreater(a, b)) {
54  bindings->set(bindVar(), a);
55  } else {
56  bindings->set(bindVar(), b);
57  }
58  }
59  return true;
60  }
62  auto a = applyBindings(arguments_[0], *bindings);
63  auto b = applyBindings(arguments_[1], *bindings);
64  return isSmaller(a, b);
65  }
67  auto a = applyBindings(arguments_[0], *bindings);
68  auto b = applyBindings(arguments_[1], *bindings);
69  return isSmaller(a, b) || (*a == *b);
70  }
72  auto a = applyBindings(arguments_[0], *bindings);
73  auto b = applyBindings(arguments_[1], *bindings);
74  return isGreater(a, b);
75  }
77  auto a = applyBindings(arguments_[0], *bindings);
78  auto b = applyBindings(arguments_[1], *bindings);
79  return isGreater(a, b) || (*a == *b);
80  }
82  auto a = applyBindings(arguments_[0], *bindings);
83  auto b = applyBindings(arguments_[1], *bindings);
84  return *a == *b;
85  }
87  auto a = applyBindings(arguments_[0], *bindings);
88  auto b = applyBindings(arguments_[1], *bindings);
89  return *a != *b;
90  }
91  }
92  return true;
93 }
94 
95 namespace knowrob::py {
96  template<>
98  using namespace boost::python;
99 
100  class_<GraphBuiltin, bases<GraphTerm, Function>, std::shared_ptr<GraphBuiltin>, boost::noncopyable>
101  ("GraphBuiltin", no_init)
102  .def("setOptional", &GraphBuiltin::setOptional)
103  .def("isOptional", &GraphBuiltin::isOptional)
104  .def("bind", &GraphBuiltin::bind).staticmethod("bind")
105  .def("min", &GraphBuiltin::min).staticmethod("min")
106  .def("max", &GraphBuiltin::max).staticmethod("max")
107  .def("less", &GraphBuiltin::less).staticmethod("less")
108  .def("lessOrEqual", &GraphBuiltin::lessOrEqual).staticmethod("lessOrEqual")
109  .def("greater", &GraphBuiltin::greater).staticmethod("greater")
110  .def("greaterOrEqual", &GraphBuiltin::greaterOrEqual).staticmethod("greaterOrEqual")
111  .def("equal", &GraphBuiltin::equal).staticmethod("equal")
112  .def("notEqual", &GraphBuiltin::notEqual).staticmethod("notEqual");
113  }
114 }
const std::vector< TermPtr > arguments_
Definition: Function.h:67
void setOptional(bool isOptional)
Definition: GraphBuiltin.h:252
static BuiltinPtr greaterOrEqual(const TermPtr &a, const TermPtr &b)
Definition: GraphBuiltin.h:210
auto bindVar() const
Definition: GraphBuiltin.h:113
auto builtinType() const
Definition: GraphBuiltin.h:108
static BuiltinPtr max(const VariablePtr &var, const TermPtr &a, const TermPtr &b)
Definition: GraphBuiltin.h:157
static BuiltinPtr lessOrEqual(const TermPtr &a, const TermPtr &b)
Definition: GraphBuiltin.h:184
static BuiltinPtr equal(const TermPtr &a, const TermPtr &b)
Definition: GraphBuiltin.h:223
static BuiltinPtr notEqual(const TermPtr &a, const TermPtr &b)
Definition: GraphBuiltin.h:236
static BuiltinPtr less(const TermPtr &a, const TermPtr &b)
Definition: GraphBuiltin.h:171
static BuiltinPtr bind(const VariablePtr &var, const TermPtr &val)
Definition: GraphBuiltin.h:127
bool apply(const std::shared_ptr< Bindings > &bindings) const
static BuiltinPtr greater(const TermPtr &a, const TermPtr &b)
Definition: GraphBuiltin.h:197
bool isOptional() const
Definition: GraphBuiltin.h:246
static BuiltinPtr min(const VariablePtr &var, const TermPtr &a, const TermPtr &b)
Definition: GraphBuiltin.h:142
void createType< GraphBuiltin >()
std::shared_ptr< Term > TermPtr
Definition: Term.h:117
FirstOrderLiteralPtr applyBindings(const FirstOrderLiteralPtr &lit, const Bindings &bindings)