knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::GraphBuiltin Class Reference

#include <GraphBuiltin.h>

Inheritance diagram for knowrob::GraphBuiltin:
Collaboration diagram for knowrob::GraphBuiltin:

Public Types

using BuiltinPtr = std::shared_ptr< GraphBuiltin >
 
using BuiltinPtr = std::shared_ptr< GraphBuiltin >
 

Public Member Functions

 GraphBuiltin (GraphBuiltinType builtinType, const AtomPtr &functor, const std::vector< TermPtr > &arguments, VariablePtr bindVar=nullptr)
 
auto builtinType () const
 
auto bindVar () const
 
bool apply (const std::shared_ptr< Bindings > &bindings) const
 
bool isOptional () const
 
void setOptional (bool isOptional)
 
void write (std::ostream &os) const override
 
 GraphBuiltin (GraphBuiltinType builtinType, const AtomPtr &functor, const std::vector< TermPtr > &arguments, VariablePtr bindVar=nullptr)
 
auto builtinType () const
 
auto bindVar () const
 
bool apply (const std::shared_ptr< Bindings > &bindings) const
 
bool isOptional () const
 
void setOptional (bool isOptional)
 
void write (std::ostream &os) const override
 
- Public Member Functions inherited from knowrob::GraphTerm
virtual ~GraphTerm ()=default
 
bool isPattern () const
 
bool isBuiltin () const
 
auto termType () const
 
virtual ~GraphTerm ()=default
 
bool isPattern () const
 
bool isBuiltin () const
 
auto termType () const
 
- Public Member Functions inherited from knowrob::Printable
virtual ~Printable ()=default
 
virtual std::string format () const
 
virtual ~Printable ()=default
 
virtual std::string format () const
 
- Public Member Functions inherited from knowrob::Function
 Function (AtomPtr functor, const std::vector< TermPtr > &arguments)
 
 Function (std::string_view functor, const std::vector< TermPtr > &arguments)
 
bool isSameFunction (const Function &other) const
 
auto & functor () const
 
auto & arguments () const
 
auto arity () const
 
size_t hashOfFunction () const
 
const std::set< std::string_view > & variables () const override
 
 Function (AtomPtr functor, const std::vector< TermPtr > &arguments)
 
 Function (std::string_view functor, const std::vector< TermPtr > &arguments)
 
bool isSameFunction (const Function &other) const
 
auto & functor () const
 
auto & arguments () const
 
auto arity () const
 
size_t hashOfFunction () const
 
const std::set< std::string_view > & variables () const override
 
void write (std::ostream &os) const override
 
- Public Member Functions inherited from knowrob::Term
 Term (TermType termType)
 
 ~Term () override=default
 
bool operator== (const Term &other) const
 
bool operator!= (const Term &other) const
 
TermType termType () const
 
bool isGround () const
 
bool isAtomic () const
 
bool isAtom () const
 
bool isVariable () const
 
bool isFunction () const
 
bool isNumeric () const
 
bool isString () const
 
bool isIRI () const
 
bool isBlank () const
 
size_t hash () const
 
 Term (TermType termType)
 
 ~Term () override=default
 
bool operator== (const Term &other) const
 
bool operator!= (const Term &other) const
 
TermType termType () const
 
bool isGround () const
 
bool isAtomic () const
 
bool isAtom () const
 
bool isVariable () const
 
bool isFunction () const
 
bool isNumeric () const
 
bool isString () const
 
bool isIRI () const
 
bool isBlank () const
 
size_t hash () const
 

Static Public Member Functions

static BuiltinPtr bind (const VariablePtr &var, const TermPtr &val)
 
static BuiltinPtr min (const VariablePtr &var, const TermPtr &a, const TermPtr &b)
 
static BuiltinPtr max (const VariablePtr &var, const TermPtr &a, const TermPtr &b)
 
static BuiltinPtr less (const TermPtr &a, const TermPtr &b)
 
static BuiltinPtr lessOrEqual (const TermPtr &a, const TermPtr &b)
 
static BuiltinPtr greater (const TermPtr &a, const TermPtr &b)
 
static BuiltinPtr greaterOrEqual (const TermPtr &a, const TermPtr &b)
 
static BuiltinPtr equal (const TermPtr &a, const TermPtr &b)
 
static BuiltinPtr notEqual (const TermPtr &a, const TermPtr &b)
 
static BuiltinPtr bind (const VariablePtr &var, const TermPtr &val)
 
static BuiltinPtr min (const VariablePtr &var, const TermPtr &a, const TermPtr &b)
 
static BuiltinPtr max (const VariablePtr &var, const TermPtr &a, const TermPtr &b)
 
static BuiltinPtr less (const TermPtr &a, const TermPtr &b)
 
static BuiltinPtr lessOrEqual (const TermPtr &a, const TermPtr &b)
 
static BuiltinPtr greater (const TermPtr &a, const TermPtr &b)
 
static BuiltinPtr greaterOrEqual (const TermPtr &a, const TermPtr &b)
 
static BuiltinPtr equal (const TermPtr &a, const TermPtr &b)
 
static BuiltinPtr notEqual (const TermPtr &a, const TermPtr &b)
 

Protected Attributes

GraphBuiltinType builtinType_
 
VariablePtr bindVar_
 
bool isOptional_
 
- Protected Attributes inherited from knowrob::GraphTerm
GraphTermType termType_
 
- Protected Attributes inherited from knowrob::Function
const std::shared_ptr< Atomfunctor_
 
const std::vector< TermPtrarguments_
 
const std::set< std::string_view > variables_
 
- Protected Attributes inherited from knowrob::Term
const TermType termType_
 
bool isBlank_ = false
 
bool isIRI_ = false
 

Additional Inherited Members

- Protected Member Functions inherited from knowrob::GraphTerm
 GraphTerm (GraphTermType termType)
 
 GraphTerm (GraphTermType termType)
 
- Protected Member Functions inherited from knowrob::Function
std::set< std::string_view > getVariables1 () const
 
std::set< std::string_view > getVariables1 () const
 
- Static Protected Attributes inherited from knowrob::Term
static const std::set< std::string_view > noVariables_ = {}
 

Detailed Description

A builtin term that is part of a graph query.

Definition at line 84 of file GraphBuiltin.h.

Member Typedef Documentation

◆ BuiltinPtr [1/2]

Definition at line 86 of file GraphBuiltin.h.

◆ BuiltinPtr [2/2]

Definition at line 86 of file GraphBuiltin.h.

Constructor & Destructor Documentation

◆ GraphBuiltin() [1/2]

knowrob::GraphBuiltin::GraphBuiltin ( GraphBuiltinType  builtinType,
const AtomPtr functor,
const std::vector< TermPtr > &  arguments,
VariablePtr  bindVar = nullptr 
)
inline

Constructs a builtin term from a builtin type, a functor, a list of arguments, and an optional variable to bind.

Parameters
builtinTypethe builtin type.
functorthe functor.
argumentsthe arguments.
bindVarthe variable to bind or null if no binding is used by the builtin.

Definition at line 95 of file GraphBuiltin.h.

102  bindVar_(std::move(bindVar)),
103  isOptional_(true) {}
Function(AtomPtr functor, const std::vector< TermPtr > &arguments)
Definition: Function.cpp:13
auto & arguments() const
Definition: Function.h:47
auto & functor() const
Definition: Function.h:42
auto bindVar() const
Definition: GraphBuiltin.h:113
auto builtinType() const
Definition: GraphBuiltin.h:108
GraphBuiltinType builtinType_
Definition: GraphBuiltin.h:258
GraphTerm(GraphTermType termType)
Definition: GraphTerm.h:50

◆ GraphBuiltin() [2/2]

knowrob::GraphBuiltin::GraphBuiltin ( GraphBuiltinType  builtinType,
const AtomPtr functor,
const std::vector< TermPtr > &  arguments,
VariablePtr  bindVar = nullptr 
)
inline

Constructs a builtin term from a builtin type, a functor, a list of arguments, and an optional variable to bind.

Parameters
builtinTypethe builtin type.
functorthe functor.
argumentsthe arguments.
bindVarthe variable to bind or null if no binding is used by the builtin.

Definition at line 95 of file GraphBuiltin.h.

102  bindVar_(std::move(bindVar)),
103  isOptional_(true) {}

Member Function Documentation

◆ apply() [1/2]

bool GraphBuiltin::apply ( const std::shared_ptr< Bindings > &  bindings) const

Apply the builtin to the given bindings.

Parameters
bindingsthe bindings to apply the builtin to.

Definition at line 30 of file GraphBuiltin.cpp.

30  {
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 }
const std::vector< TermPtr > arguments_
Definition: Function.h:67
FirstOrderLiteralPtr applyBindings(const FirstOrderLiteralPtr &lit, const Bindings &bindings)

◆ apply() [2/2]

bool knowrob::GraphBuiltin::apply ( const std::shared_ptr< Bindings > &  bindings) const

Apply the builtin to the given bindings.

Parameters
bindingsthe bindings to apply the builtin to.

◆ bind() [1/2]

static BuiltinPtr knowrob::GraphBuiltin::bind ( const VariablePtr var,
const TermPtr val 
)
inlinestatic

Bind a value to a variable in the query pipeline.

Parameters
varthe variable to bind.
valthe value to bind to the variable.
Returns
a new Builtin that binds the value to the variable.

Definition at line 127 of file GraphBuiltin.h.

127  {
128  return std::make_shared<GraphBuiltin>(
131  std::vector<TermPtr>{val},
132  var);
133  }
const AtomPtr bindFunctor
Definition: GraphBuiltin.h:38
VariableRule & var()
Definition: terms.cpp:91

◆ bind() [2/2]

static BuiltinPtr knowrob::GraphBuiltin::bind ( const VariablePtr var,
const TermPtr val 
)
inlinestatic

Bind a value to a variable in the query pipeline.

Parameters
varthe variable to bind.
valthe value to bind to the variable.
Returns
a new Builtin that binds the value to the variable.

Definition at line 127 of file GraphBuiltin.h.

127  {
128  return std::make_shared<GraphBuiltin>(
131  std::vector<TermPtr>{val},
132  var);
133  }

◆ bindVar() [1/2]

auto knowrob::GraphBuiltin::bindVar ( ) const
inline
Returns
the variable to bind or null if no binding is used by the builtin.

Definition at line 113 of file GraphBuiltin.h.

113 { return bindVar_; }

◆ bindVar() [2/2]

auto knowrob::GraphBuiltin::bindVar ( ) const
inline
Returns
the variable to bind or null if no binding is used by the builtin.

Definition at line 113 of file GraphBuiltin.h.

113 { return bindVar_; }

◆ builtinType() [1/2]

auto knowrob::GraphBuiltin::builtinType ( ) const
inline
Returns
the builtin type.

Definition at line 108 of file GraphBuiltin.h.

108 { return builtinType_; }

◆ builtinType() [2/2]

auto knowrob::GraphBuiltin::builtinType ( ) const
inline
Returns
the builtin type.

Definition at line 108 of file GraphBuiltin.h.

108 { return builtinType_; }

◆ equal() [1/2]

static BuiltinPtr knowrob::GraphBuiltin::equal ( const TermPtr a,
const TermPtr b 
)
inlinestatic

Create a new Builtin that checks if the first term is equal to the second term.

Parameters
athe first term.
bthe second term.
Returns
a new Builtin that checks if the first term is equal to the second term.

Definition at line 223 of file GraphBuiltin.h.

223  {
224  return std::make_shared<GraphBuiltin>(
227  std::vector<TermPtr>{a, b});
228  }
const AtomPtr equalFunctor
Definition: GraphBuiltin.h:73

◆ equal() [2/2]

static BuiltinPtr knowrob::GraphBuiltin::equal ( const TermPtr a,
const TermPtr b 
)
inlinestatic

Create a new Builtin that checks if the first term is equal to the second term.

Parameters
athe first term.
bthe second term.
Returns
a new Builtin that checks if the first term is equal to the second term.

Definition at line 223 of file GraphBuiltin.h.

223  {
224  return std::make_shared<GraphBuiltin>(
227  std::vector<TermPtr>{a, b});
228  }

◆ greater() [1/2]

static BuiltinPtr knowrob::GraphBuiltin::greater ( const TermPtr a,
const TermPtr b 
)
inlinestatic

Create a new Builtin that checks if the first term is greater than the second term.

Parameters
athe first term.
bthe second term.
Returns
a new Builtin that checks if the first term is greater than the second term.

Definition at line 197 of file GraphBuiltin.h.

197  {
198  return std::make_shared<GraphBuiltin>(
201  std::vector<TermPtr>{a, b});
202  }
const AtomPtr greaterFunctor
Definition: GraphBuiltin.h:63

◆ greater() [2/2]

static BuiltinPtr knowrob::GraphBuiltin::greater ( const TermPtr a,
const TermPtr b 
)
inlinestatic

Create a new Builtin that checks if the first term is greater than the second term.

Parameters
athe first term.
bthe second term.
Returns
a new Builtin that checks if the first term is greater than the second term.

Definition at line 197 of file GraphBuiltin.h.

197  {
198  return std::make_shared<GraphBuiltin>(
201  std::vector<TermPtr>{a, b});
202  }

◆ greaterOrEqual() [1/2]

static BuiltinPtr knowrob::GraphBuiltin::greaterOrEqual ( const TermPtr a,
const TermPtr b 
)
inlinestatic

Create a new Builtin that checks if the first term is greater than or equal to the second term.

Parameters
athe first term.
bthe second term.
Returns
a new Builtin that checks if the first term is greater than or equal to the second term.

Definition at line 210 of file GraphBuiltin.h.

210  {
211  return std::make_shared<GraphBuiltin>(
214  std::vector<TermPtr>{a, b});
215  }
const AtomPtr greaterOrEqualFunctor
Definition: GraphBuiltin.h:68

◆ greaterOrEqual() [2/2]

static BuiltinPtr knowrob::GraphBuiltin::greaterOrEqual ( const TermPtr a,
const TermPtr b 
)
inlinestatic

Create a new Builtin that checks if the first term is greater than or equal to the second term.

Parameters
athe first term.
bthe second term.
Returns
a new Builtin that checks if the first term is greater than or equal to the second term.

Definition at line 210 of file GraphBuiltin.h.

210  {
211  return std::make_shared<GraphBuiltin>(
214  std::vector<TermPtr>{a, b});
215  }

◆ isOptional() [1/2]

bool knowrob::GraphBuiltin::isOptional ( ) const
inline
Returns
true if the builtin is optional, false otherwise.

Definition at line 246 of file GraphBuiltin.h.

246 { return isOptional_; }

◆ isOptional() [2/2]

bool knowrob::GraphBuiltin::isOptional ( ) const
inline
Returns
true if the builtin is optional, false otherwise.

Definition at line 246 of file GraphBuiltin.h.

246 { return isOptional_; }

◆ less() [1/2]

static BuiltinPtr knowrob::GraphBuiltin::less ( const TermPtr a,
const TermPtr b 
)
inlinestatic

Create a new Builtin that checks if the first term is less than the second term.

Parameters
athe first term.
bthe second term.
Returns
a new Builtin that checks if the first term is less than the second term.

Definition at line 171 of file GraphBuiltin.h.

171  {
172  return std::make_shared<GraphBuiltin>(
175  std::vector<TermPtr>{a, b});
176  }
const AtomPtr lessFunctor
Definition: GraphBuiltin.h:53

◆ less() [2/2]

static BuiltinPtr knowrob::GraphBuiltin::less ( const TermPtr a,
const TermPtr b 
)
inlinestatic

Create a new Builtin that checks if the first term is less than the second term.

Parameters
athe first term.
bthe second term.
Returns
a new Builtin that checks if the first term is less than the second term.

Definition at line 171 of file GraphBuiltin.h.

171  {
172  return std::make_shared<GraphBuiltin>(
175  std::vector<TermPtr>{a, b});
176  }

◆ lessOrEqual() [1/2]

static BuiltinPtr knowrob::GraphBuiltin::lessOrEqual ( const TermPtr a,
const TermPtr b 
)
inlinestatic

Create a new Builtin that checks if the first term is less than or equal to the second term.

Parameters
athe first term.
bthe second term.
Returns
a new Builtin that checks if the first term is less than or equal to the second term.

Definition at line 184 of file GraphBuiltin.h.

184  {
185  return std::make_shared<GraphBuiltin>(
188  std::vector<TermPtr>{a, b});
189  }
const AtomPtr lessOrEqualFunctor
Definition: GraphBuiltin.h:58

◆ lessOrEqual() [2/2]

static BuiltinPtr knowrob::GraphBuiltin::lessOrEqual ( const TermPtr a,
const TermPtr b 
)
inlinestatic

Create a new Builtin that checks if the first term is less than or equal to the second term.

Parameters
athe first term.
bthe second term.
Returns
a new Builtin that checks if the first term is less than or equal to the second term.

Definition at line 184 of file GraphBuiltin.h.

184  {
185  return std::make_shared<GraphBuiltin>(
188  std::vector<TermPtr>{a, b});
189  }

◆ max() [1/2]

static BuiltinPtr knowrob::GraphBuiltin::max ( const VariablePtr var,
const TermPtr a,
const TermPtr b 
)
inlinestatic

Create a new Builtin that calculates the maximum of two terms.

Parameters
varthe variable to bind the result to.
athe first term.
bthe second term.
Returns
a new Builtin that calculates the maximum of two terms.

Definition at line 157 of file GraphBuiltin.h.

157  {
158  return std::make_shared<GraphBuiltin>(
161  std::vector<TermPtr>{a, b},
162  var);
163  }
const AtomPtr maxFunctor
Definition: GraphBuiltin.h:48

◆ max() [2/2]

static BuiltinPtr knowrob::GraphBuiltin::max ( const VariablePtr var,
const TermPtr a,
const TermPtr b 
)
inlinestatic

Create a new Builtin that calculates the maximum of two terms.

Parameters
varthe variable to bind the result to.
athe first term.
bthe second term.
Returns
a new Builtin that calculates the maximum of two terms.

Definition at line 157 of file GraphBuiltin.h.

157  {
158  return std::make_shared<GraphBuiltin>(
161  std::vector<TermPtr>{a, b},
162  var);
163  }

◆ min() [1/2]

static BuiltinPtr knowrob::GraphBuiltin::min ( const VariablePtr var,
const TermPtr a,
const TermPtr b 
)
inlinestatic

Create a new Builtin that calculates the minimum of two terms.

Parameters
varthe variable to bind the result to.
athe first term.
bthe second term.
Returns
a new Builtin that calculates the minimum of two terms.

Definition at line 142 of file GraphBuiltin.h.

142  {
143  return std::make_shared<GraphBuiltin>(
146  std::vector<TermPtr>{a, b},
147  var);
148  }
const AtomPtr minFunctor
Definition: GraphBuiltin.h:43

◆ min() [2/2]

static BuiltinPtr knowrob::GraphBuiltin::min ( const VariablePtr var,
const TermPtr a,
const TermPtr b 
)
inlinestatic

Create a new Builtin that calculates the minimum of two terms.

Parameters
varthe variable to bind the result to.
athe first term.
bthe second term.
Returns
a new Builtin that calculates the minimum of two terms.

Definition at line 142 of file GraphBuiltin.h.

142  {
143  return std::make_shared<GraphBuiltin>(
146  std::vector<TermPtr>{a, b},
147  var);
148  }

◆ notEqual() [1/2]

static BuiltinPtr knowrob::GraphBuiltin::notEqual ( const TermPtr a,
const TermPtr b 
)
inlinestatic

Create a new Builtin that checks if the first term is not equal to the second term.

Parameters
athe first term.
bthe second term.
Returns
a new Builtin that checks if the first term is not equal to the second term.

Definition at line 236 of file GraphBuiltin.h.

236  {
237  return std::make_shared<GraphBuiltin>(
240  std::vector<TermPtr>{a, b});
241  }
const AtomPtr notEqualFunctor
Definition: GraphBuiltin.h:78

◆ notEqual() [2/2]

static BuiltinPtr knowrob::GraphBuiltin::notEqual ( const TermPtr a,
const TermPtr b 
)
inlinestatic

Create a new Builtin that checks if the first term is not equal to the second term.

Parameters
athe first term.
bthe second term.
Returns
a new Builtin that checks if the first term is not equal to the second term.

Definition at line 236 of file GraphBuiltin.h.

236  {
237  return std::make_shared<GraphBuiltin>(
240  std::vector<TermPtr>{a, b});
241  }

◆ setOptional() [1/2]

void knowrob::GraphBuiltin::setOptional ( bool  isOptional)
inline

Set the optional flag of the builtin.

Parameters
isOptionalthe optional flag.

Definition at line 252 of file GraphBuiltin.h.

252 { isOptional_ = isOptional; }
bool isOptional() const
Definition: GraphBuiltin.h:246

◆ setOptional() [2/2]

void knowrob::GraphBuiltin::setOptional ( bool  isOptional)
inline

Set the optional flag of the builtin.

Parameters
isOptionalthe optional flag.

Definition at line 252 of file GraphBuiltin.h.

252 { isOptional_ = isOptional; }

◆ write() [1/2]

void knowrob::GraphBuiltin::write ( std::ostream &  os) const
inlineoverridevirtual

Print this object to a stream.

Parameters
osthe stream to print to.

Implements knowrob::GraphTerm.

Definition at line 255 of file GraphBuiltin.h.

255 { Function::write(os); }
void write(std::ostream &os) const override
Definition: Function.cpp:53

◆ write() [2/2]

void knowrob::GraphBuiltin::write ( std::ostream &  os) const
inlineoverridevirtual

Print this object to a stream.

Parameters
osthe stream to print to.

Implements knowrob::GraphTerm.

Definition at line 255 of file GraphBuiltin.h.

255 { Function::write(os); }

Member Data Documentation

◆ bindVar_

VariablePtr knowrob::GraphBuiltin::bindVar_
protected

Definition at line 259 of file GraphBuiltin.h.

◆ builtinType_

GraphBuiltinType knowrob::GraphBuiltin::builtinType_
protected

Definition at line 258 of file GraphBuiltin.h.

◆ isOptional_

bool knowrob::GraphBuiltin::isOptional_
protected

Definition at line 260 of file GraphBuiltin.h.


The documentation for this class was generated from the following files: