knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
GraphBuiltin.h
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 #ifndef KNOWROB_GRAPH_BUILTIN_H
7 #define KNOWROB_GRAPH_BUILTIN_H
8 
9 #include <utility>
10 
11 #include "memory"
12 #include "knowrob/semweb/GraphTerm.h"
13 #include "knowrob/terms/Variable.h"
14 #include "knowrob/terms/Atomic.h"
15 #include "knowrob/terms/Function.h"
16 #include "knowrob/terms/Bindings.h"
17 
18 namespace knowrob {
22  enum class GraphBuiltinType {
23  Bind, // Bind a value to a variable in the query pipeline.
24  Min, // Calculate the minimum of two terms.
25  Max, // Calculate the maximum of two terms.
26  Less, // Check if the first term is less than the second term.
27  LessOrEqual, // Check if the first term is less than or equal to the second term.
28  Greater, // Check if the first term is greater than the second term.
29  GreaterOrEqual, // Check if the first term is greater than or equal to the second term.
30  Equal, // Check if the first term is equal to the second term.
31  NotEqual // Check if the first term is not equal to the second term.
32  };
33 
34  namespace graph::builtins {
38  const AtomPtr bindFunctor = Atom::Tabled("bind");
39 
43  const AtomPtr minFunctor = Atom::Tabled("min");
44 
48  const AtomPtr maxFunctor = Atom::Tabled("max");
49 
53  const AtomPtr lessFunctor = Atom::Tabled("less");
54 
58  const AtomPtr lessOrEqualFunctor = Atom::Tabled("lessOrEqual");
59 
63  const AtomPtr greaterFunctor = Atom::Tabled("greater");
64 
68  const AtomPtr greaterOrEqualFunctor = Atom::Tabled("greaterOrEqual");
69 
73  const AtomPtr equalFunctor = Atom::Tabled("equal");
74 
78  const AtomPtr notEqualFunctor = Atom::Tabled("notEqual");
79  }
80 
84  class GraphBuiltin : public GraphTerm, public Function {
85  public:
86  using BuiltinPtr = std::shared_ptr<GraphBuiltin>;
87 
96  const AtomPtr &functor,
97  const std::vector<TermPtr> &arguments,
98  VariablePtr bindVar = nullptr)
102  bindVar_(std::move(bindVar)),
103  isOptional_(true) {}
104 
108  auto builtinType() const { return builtinType_; }
109 
113  auto bindVar() const { return bindVar_; }
114 
119  bool apply(const std::shared_ptr<Bindings> &bindings) const;
120 
127  static BuiltinPtr bind(const VariablePtr &var, const TermPtr &val) {
128  return std::make_shared<GraphBuiltin>(
131  std::vector<TermPtr>{val},
132  var);
133  }
134 
142  static BuiltinPtr min(const VariablePtr &var, const TermPtr &a, const TermPtr &b) {
143  return std::make_shared<GraphBuiltin>(
146  std::vector<TermPtr>{a, b},
147  var);
148  }
149 
157  static BuiltinPtr max(const VariablePtr &var, const TermPtr &a, const TermPtr &b) {
158  return std::make_shared<GraphBuiltin>(
161  std::vector<TermPtr>{a, b},
162  var);
163  }
164 
171  static BuiltinPtr less(const TermPtr &a, const TermPtr &b) {
172  return std::make_shared<GraphBuiltin>(
175  std::vector<TermPtr>{a, b});
176  }
177 
184  static BuiltinPtr lessOrEqual(const TermPtr &a, const TermPtr &b) {
185  return std::make_shared<GraphBuiltin>(
188  std::vector<TermPtr>{a, b});
189  }
190 
197  static BuiltinPtr greater(const TermPtr &a, const TermPtr &b) {
198  return std::make_shared<GraphBuiltin>(
201  std::vector<TermPtr>{a, b});
202  }
203 
210  static BuiltinPtr greaterOrEqual(const TermPtr &a, const TermPtr &b) {
211  return std::make_shared<GraphBuiltin>(
214  std::vector<TermPtr>{a, b});
215  }
216 
223  static BuiltinPtr equal(const TermPtr &a, const TermPtr &b) {
224  return std::make_shared<GraphBuiltin>(
227  std::vector<TermPtr>{a, b});
228  }
229 
236  static BuiltinPtr notEqual(const TermPtr &a, const TermPtr &b) {
237  return std::make_shared<GraphBuiltin>(
240  std::vector<TermPtr>{a, b});
241  }
242 
246  bool isOptional() const { return isOptional_; }
247 
253 
254  // Overwritten from GraphTerm
255  void write(std::ostream &os) const override { Function::write(os); }
256 
257  protected:
260  bool isOptional_;
261  };
262 } // knowrob
263 
264 #endif //KNOWROB_GRAPH_BUILTIN_H
static std::shared_ptr< knowrob::Atom > Tabled(std::string_view stringForm)
Definition: Atom.cpp:40
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
bool apply(const std::shared_ptr< Bindings > &bindings) const
void setOptional(bool isOptional)
Definition: GraphBuiltin.h:252
GraphBuiltin(GraphBuiltinType builtinType, const AtomPtr &functor, const std::vector< TermPtr > &arguments, VariablePtr bindVar=nullptr)
Definition: GraphBuiltin.h:95
std::shared_ptr< GraphBuiltin > BuiltinPtr
Definition: GraphBuiltin.h:86
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
GraphBuiltinType builtinType_
Definition: GraphBuiltin.h:258
static BuiltinPtr bind(const VariablePtr &var, const TermPtr &val)
Definition: GraphBuiltin.h:127
static BuiltinPtr greater(const TermPtr &a, const TermPtr &b)
Definition: GraphBuiltin.h:197
bool isOptional() const
Definition: GraphBuiltin.h:246
void write(std::ostream &os) const override
Definition: GraphBuiltin.h:255
static BuiltinPtr min(const VariablePtr &var, const TermPtr &a, const TermPtr &b)
Definition: GraphBuiltin.h:142
const AtomPtr maxFunctor
Definition: GraphBuiltin.h:48
const AtomPtr bindFunctor
Definition: GraphBuiltin.h:38
const AtomPtr greaterFunctor
Definition: GraphBuiltin.h:63
const AtomPtr minFunctor
Definition: GraphBuiltin.h:43
const AtomPtr notEqualFunctor
Definition: GraphBuiltin.h:78
const AtomPtr lessFunctor
Definition: GraphBuiltin.h:53
const AtomPtr greaterOrEqualFunctor
Definition: GraphBuiltin.h:68
const AtomPtr lessOrEqualFunctor
Definition: GraphBuiltin.h:58
const AtomPtr equalFunctor
Definition: GraphBuiltin.h:73
VariableRule & var()
Definition: terms.cpp:91
std::shared_ptr< Term > TermPtr
Definition: Term.h:117
std::shared_ptr< Atom > AtomPtr
Definition: Atom.h:69
GraphBuiltinType
Definition: GraphBuiltin.h:22
std::shared_ptr< Variable > VariablePtr
Definition: Variable.h:60
GraphTermType
Definition: GraphTerm.h:16