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

#include <PrologTerm.h>

Inheritance diagram for knowrob::PrologTerm:
Collaboration diagram for knowrob::PrologTerm:

Public Member Functions

 PrologTerm ()
 
 PrologTerm (const GraphQueryPtr &query)
 
 PrologTerm (const std::shared_ptr< GraphTerm > &kbTerm)
 
 PrologTerm (const TermPtr &kbTerm)
 
 PrologTerm (const FormulaPtr &kbFormula)
 
 PrologTerm (const TriplePattern &literal, const char *triple_functor)
 
 PrologTerm (const Triple &triple, std::string_view functor)
 
 PrologTerm (const std::vector< PrologTerm > &args, std::string_view functor)
 
template<typename ... Args>
 PrologTerm (std::string_view functor, Args &&... args)
 
 PrologTerm (const PrologTerm &other)=default
 
term_t operator() () const
 
PrologTerm operator& (const PrologTerm &other) const
 
PrologTerm operator| (const PrologTerm &other) const
 
PrologTerm operator~ () const
 
void setModule (std::string_view module)
 
auto module () const
 
bool putTriple (std::string_view functor, const Triple &triple)
 
bool putTerm (const TermPtr &kbTerm)
 
bool putTerm (const std::shared_ptr< GraphTerm > &kbTerm)
 
bool putFormula (const FormulaPtr &phi)
 
bool isGround () const
 
auto & vars () const
 
qid_t openQuery (int flags) const
 
TermPtr toKnowRobTerm () const
 
FormulaPtr toKnowRobFormula () const
 
void write (std::ostream &os) const override
 
 PrologTerm ()
 
 PrologTerm (const GraphQueryPtr &query)
 
 PrologTerm (const std::shared_ptr< GraphTerm > &kbTerm)
 
 PrologTerm (const TermPtr &kbTerm)
 
 PrologTerm (const FormulaPtr &kbFormula)
 
 PrologTerm (const TriplePattern &literal, const char *triple_functor)
 
 PrologTerm (const Triple &triple, std::string_view functor)
 
 PrologTerm (const std::vector< PrologTerm > &args, std::string_view functor)
 
template<typename ... Args>
 PrologTerm (std::string_view functor, Args &&... args)
 
 PrologTerm (const PrologTerm &other)=default
 
term_t operator() () const
 
PrologTerm operator& (const PrologTerm &other) const
 
PrologTerm operator| (const PrologTerm &other) const
 
PrologTerm operator~ () const
 
void setModule (std::string_view module)
 
auto module () const
 
bool putTriple (std::string_view functor, const Triple &triple)
 
bool putTerm (const TermPtr &kbTerm)
 
bool putTerm (const std::shared_ptr< GraphTerm > &kbTerm)
 
bool putFormula (const FormulaPtr &phi)
 
bool isGround () const
 
auto & vars () const
 
qid_t openQuery (int flags) const
 
TermPtr toKnowRobTerm () const
 
FormulaPtr toKnowRobFormula () const
 
void write (std::ostream &os) const override
 
- Public Member Functions inherited from knowrob::Printable
virtual ~Printable ()=default
 
virtual std::string format () const
 
virtual ~Printable ()=default
 
virtual std::string format () const
 

Static Public Member Functions

static const atom_t & ATOM_fail ()
 
static const atom_t & ATOM_false ()
 
static const atom_t & ATOM_true ()
 
static const atom_t & ATOM_comma ()
 
static const atom_t & ATOM_semicolon ()
 
static const functor_t & FUNCTOR_comma ()
 
static const functor_t & FUNCTOR_semicolon ()
 
static const predicate_t & PREDICATE_comma ()
 
static const predicate_t & PREDICATE_semicolon ()
 
static bool nextSolution (qid_t qid)
 
static TermPtr toKnowRobTerm (const term_t &t)
 
static FormulaPtr toKnowRobFormula (const TermPtr &t)
 
static std::string getVarName (term_t plTerm)
 
static PrologTerm nil ()
 
static bool display (std::ostream &os, term_t t, const std::string &indent="")
 
static const atom_t & ATOM_fail ()
 
static const atom_t & ATOM_false ()
 
static const atom_t & ATOM_true ()
 
static const atom_t & ATOM_comma ()
 
static const atom_t & ATOM_semicolon ()
 
static const functor_t & FUNCTOR_comma ()
 
static const functor_t & FUNCTOR_semicolon ()
 
static const predicate_t & PREDICATE_comma ()
 
static const predicate_t & PREDICATE_semicolon ()
 
static bool nextSolution (qid_t qid)
 
static TermPtr toKnowRobTerm (const term_t &t)
 
static FormulaPtr toKnowRobFormula (const TermPtr &t)
 
static std::string getVarName (term_t plTerm)
 
static PrologTerm nil ()
 
static bool display (std::ostream &os, term_t t, const std::string &indent="")
 

Protected Member Functions

bool putFormula (const FormulaPtr &phi, term_t plTerm)
 
bool putFunction (Function *fn, term_t pl_term)
 
bool putList (ListTerm *list, term_t pl_term)
 
bool putTerm (const TermPtr &kbTerm, term_t plTerm)
 
bool putCompound (CompoundFormula *phi, term_t pl_term, const functor_t &pl_functor)
 
bool putCompoundTerm (term_t plTerm, std::string_view functor, const std::vector< PrologTerm > &args)
 
bool putTerm (const std::shared_ptr< GraphTerm > &term, term_t plTerm)
 
bool putTriplePattern (const TriplePattern &pattern, const char *functor, term_t plTerm)
 
bool putRDFAtomic (const std::shared_ptr< Atomic > &atomic, term_t pl_term) const
 
bool putBuiltin (const std::shared_ptr< GraphBuiltin > &builtin, term_t plTerm)
 
void unifyVars (const PrologTerm &other)
 
template<typename ... Args>
std::vector< PrologTermreadArgs (Args &&... args)
 
bool putFormula (const FormulaPtr &phi, term_t plTerm)
 
bool putFunction (Function *fn, term_t pl_term)
 
bool putList (ListTerm *list, term_t pl_term)
 
bool putTerm (const TermPtr &kbTerm, term_t plTerm)
 
bool putCompound (CompoundFormula *phi, term_t pl_term, const functor_t &pl_functor)
 
bool putCompoundTerm (term_t plTerm, std::string_view functor, const std::vector< PrologTerm > &args)
 
bool putTerm (const std::shared_ptr< GraphTerm > &term, term_t plTerm)
 
bool putTriplePattern (const TriplePattern &pattern, const char *functor, term_t plTerm)
 
bool putRDFAtomic (const std::shared_ptr< Atomic > &atomic, term_t pl_term) const
 
bool putBuiltin (const std::shared_ptr< GraphBuiltin > &builtin, term_t plTerm)
 
void unifyVars (const PrologTerm &other)
 
template<typename ... Args>
std::vector< PrologTermreadArgs (Args &&... args)
 

Static Protected Member Functions

static bool putNativeAtomic (const std::shared_ptr< Atomic > &atomic, term_t pl_term)
 
static bool putNativeAtomic (const std::shared_ptr< Atomic > &atomic, term_t pl_term)
 

Protected Attributes

std::map< Variable, term_t, std::less<> > vars_
 
std::optional< std::string_view > module_
 
term_t plTerm_
 
bool isRDFTerm_ = false
 
bool isRDFFilter_ = false
 

Friends

class PrologList
 

Detailed Description

A Prolog term. It can be constructed from several KnowRob datatypes and can be used to query a Prolog engine in a worker thread.

Definition at line 29 of file PrologTerm.h.

Constructor & Destructor Documentation

◆ PrologTerm() [1/20]

PrologTerm::PrologTerm ( )

Generates an empty Prolog term.

Definition at line 49 of file PrologTerm.cpp.

50  : plTerm_(PL_new_term_ref()) {
51  // the new term reference is initialized to a variable
53 }
std::map< Variable, term_t, std::less<> > vars_
Definition: PrologTerm.h:266
static std::string getVarName(term_t plTerm)
Definition: PrologTerm.cpp:680

◆ PrologTerm() [2/20]

PrologTerm::PrologTerm ( const GraphQueryPtr query)
explicit

Generates a Prolog term holding a graph query.

Parameters
querya graph query

Definition at line 65 of file PrologTerm.cpp.

66  : plTerm_(PL_new_term_ref()) {
67  putTerm(query->term());
68 }
bool putTerm(const TermPtr &kbTerm)
Definition: PrologTerm.cpp:328

◆ PrologTerm() [3/20]

PrologTerm::PrologTerm ( const std::shared_ptr< GraphTerm > &  kbTerm)
explicit

Generates a Prolog term holding a KnowRob term.

Parameters
kbTerma KnowRob term

Definition at line 60 of file PrologTerm.cpp.

61  : plTerm_(PL_new_term_ref()) {
62  putTerm(term);
63 }
TermRule & term()
Definition: terms.cpp:136

◆ PrologTerm() [4/20]

PrologTerm::PrologTerm ( const TermPtr kbTerm)
explicit

Generates a Prolog term holding a KnowRob term.

Parameters
kbTerma KnowRob term

Definition at line 85 of file PrologTerm.cpp.

86  : plTerm_(PL_new_term_ref()) {
87  putTerm(kbTerm);
88 }

◆ PrologTerm() [5/20]

PrologTerm::PrologTerm ( const FormulaPtr kbFormula)
explicit

Generates a Prolog term holding a KnowRob formula.

Parameters
kbFormulaa KnowRob formula

Definition at line 80 of file PrologTerm.cpp.

81  : plTerm_(PL_new_term_ref()) {
82  putFormula(kbFormula);
83 }
bool putFormula(const FormulaPtr &phi)
Definition: PrologTerm.cpp:286

◆ PrologTerm() [6/20]

PrologTerm::PrologTerm ( const TriplePattern literal,
const char *  triple_functor 
)
explicit
Parameters
literalan RDF literal
triple_functorthe functor of the term

Definition at line 70 of file PrologTerm.cpp.

71  : plTerm_(PL_new_term_ref()) {
72  putTriplePattern(pattern, functor, plTerm_);
73 }
bool putTriplePattern(const TriplePattern &pattern, const char *functor, term_t plTerm)
Definition: PrologTerm.cpp:408
GraphTermRule & pattern()
Definition: graph.cpp:23

◆ PrologTerm() [7/20]

PrologTerm::PrologTerm ( const Triple triple,
std::string_view  functor 
)

Generates a Prolog term holding a KnowRob triple.

Parameters
functorthe functor of the term
triplea triple

Definition at line 55 of file PrologTerm.cpp.

56  : plTerm_(PL_new_term_ref()) {
57  putTriple(functor, triple);
58 }
bool putTriple(std::string_view functor, const Triple &triple)
Definition: PrologTerm.cpp:237

◆ PrologTerm() [8/20]

PrologTerm::PrologTerm ( const std::vector< PrologTerm > &  args,
std::string_view  functor 
)

Generates a Prolog term holding a list of terms.

Parameters
argsthe list of terms
functorthe functor of the term

Definition at line 75 of file PrologTerm.cpp.

76  : plTerm_(PL_new_term_ref()) {
77  putCompoundTerm(plTerm_, functor, args);
78 }
bool putCompoundTerm(term_t plTerm, std::string_view functor, const std::vector< PrologTerm > &args)
Definition: PrologTerm.cpp:214

◆ PrologTerm() [9/20]

template<typename ... Args>
knowrob::PrologTerm::PrologTerm ( std::string_view  functor,
Args &&...  args 
)
inlineexplicit

Generates a Prolog term holding a list of terms.

Template Parameters
Argsanything that can be converted to a PrologTerm with a single-argument constructor.
Parameters
functorthe functor of the term
argsthe list of terms

Definition at line 132 of file PrologTerm.h.

133  : PrologTerm(readArgs(std::forward<Args>(args)...), functor) {}
std::vector< PrologTerm > readArgs(Args &&... args)
Definition: PrologTerm.h:297

◆ PrologTerm() [10/20]

knowrob::PrologTerm::PrologTerm ( const PrologTerm other)
default

Copies a term handle.

◆ PrologTerm() [11/20]

knowrob::PrologTerm::PrologTerm ( )

Generates an empty Prolog term.

◆ PrologTerm() [12/20]

knowrob::PrologTerm::PrologTerm ( const GraphQueryPtr query)
explicit

Generates a Prolog term holding a graph query.

Parameters
querya graph query

◆ PrologTerm() [13/20]

knowrob::PrologTerm::PrologTerm ( const std::shared_ptr< GraphTerm > &  kbTerm)
explicit

Generates a Prolog term holding a KnowRob term.

Parameters
kbTerma KnowRob term

◆ PrologTerm() [14/20]

knowrob::PrologTerm::PrologTerm ( const TermPtr kbTerm)
explicit

Generates a Prolog term holding a KnowRob term.

Parameters
kbTerma KnowRob term

◆ PrologTerm() [15/20]

knowrob::PrologTerm::PrologTerm ( const FormulaPtr kbFormula)
explicit

Generates a Prolog term holding a KnowRob formula.

Parameters
kbFormulaa KnowRob formula

◆ PrologTerm() [16/20]

knowrob::PrologTerm::PrologTerm ( const TriplePattern literal,
const char *  triple_functor 
)
explicit
Parameters
literalan RDF literal
triple_functorthe functor of the term

◆ PrologTerm() [17/20]

knowrob::PrologTerm::PrologTerm ( const Triple triple,
std::string_view  functor 
)

Generates a Prolog term holding a KnowRob triple.

Parameters
functorthe functor of the term
triplea triple

◆ PrologTerm() [18/20]

knowrob::PrologTerm::PrologTerm ( const std::vector< PrologTerm > &  args,
std::string_view  functor 
)

Generates a Prolog term holding a list of terms.

Parameters
argsthe list of terms
functorthe functor of the term

◆ PrologTerm() [19/20]

template<typename ... Args>
knowrob::PrologTerm::PrologTerm ( std::string_view  functor,
Args &&...  args 
)
inlineexplicit

Generates a Prolog term holding a list of terms.

Template Parameters
Argsanything that can be converted to a PrologTerm with a single-argument constructor.
Parameters
functorthe functor of the term
argsthe list of terms

Definition at line 132 of file PrologTerm.h.

133  : PrologTerm(readArgs(std::forward<Args>(args)...), functor) {}

◆ PrologTerm() [20/20]

knowrob::PrologTerm::PrologTerm ( const PrologTerm other)
default

Copies a term handle.

Member Function Documentation

◆ ATOM_comma() [1/2]

const atom_t & PrologTerm::ATOM_comma ( )
static
Returns
the ',' atom.

Definition at line 886 of file PrologTerm.cpp.

886  {
887  static atom_t a = PL_new_atom(",");
888  return a;
889 }

◆ ATOM_comma() [2/2]

static const atom_t& knowrob::PrologTerm::ATOM_comma ( )
static
Returns
the ',' atom.

◆ ATOM_fail() [1/2]

const atom_t & PrologTerm::ATOM_fail ( )
static
Returns
the 'fail' atom.

Definition at line 871 of file PrologTerm.cpp.

871  {
872  static atom_t a = PL_new_atom("fail");
873  return a;
874 }

◆ ATOM_fail() [2/2]

static const atom_t& knowrob::PrologTerm::ATOM_fail ( )
static
Returns
the 'fail' atom.

◆ ATOM_false() [1/2]

const atom_t & PrologTerm::ATOM_false ( )
static
Returns
the 'false' atom.

Definition at line 876 of file PrologTerm.cpp.

876  {
877  static atom_t a = PL_new_atom("false");
878  return a;
879 }

◆ ATOM_false() [2/2]

static const atom_t& knowrob::PrologTerm::ATOM_false ( )
static
Returns
the 'false' atom.

◆ ATOM_semicolon() [1/2]

const atom_t & PrologTerm::ATOM_semicolon ( )
static
Returns
the ';' atom.

Definition at line 891 of file PrologTerm.cpp.

891  {
892  static atom_t a = PL_new_atom(";");
893  return a;
894 }

◆ ATOM_semicolon() [2/2]

static const atom_t& knowrob::PrologTerm::ATOM_semicolon ( )
static
Returns
the ';' atom.

◆ ATOM_true() [1/2]

const atom_t & PrologTerm::ATOM_true ( )
static
Returns
the 'true' atom.

Definition at line 881 of file PrologTerm.cpp.

881  {
882  static atom_t a = PL_new_atom("true");
883  return a;
884 }

◆ ATOM_true() [2/2]

static const atom_t& knowrob::PrologTerm::ATOM_true ( )
static
Returns
the 'true' atom.

◆ display() [1/2]

bool PrologTerm::display ( std::ostream &  os,
term_t  t,
const std::string &  indent = "" 
)
static
Parameters
osan output stream
ta Prolog term
indentan indentation string
Returns
true if the term was displayed

Definition at line 916 of file PrologTerm.cpp.

916  { //NOLINT(misc-no-recursion)
917  std::size_t n;
918  size_t len;
919  char *s;
920 
921  switch (PL_term_type(t)) {
922  case PL_ATOM:
923  case PL_INTEGER:
924  case PL_FLOAT:
925  if (PL_get_chars(t, &s, CVT_ALL|BUF_MALLOC)) {
926  os << s;
927  PL_free(s);
928  return true;
929  }
930  break;
931  case PL_VARIABLE:
932  if (PL_get_chars(t, &s, CVT_VARIABLE|BUF_MALLOC)) {
933  os << s;
934  PL_free(s);
935  return true;
936  }
937  break;
938  case PL_STRING:
939  if (PL_get_string_chars(t, &s, &len)) {
940  os << s;
941  return true;
942  }
943  break;
944  case PL_TERM: {
945  term_t a = PL_new_term_ref();
946  atom_t name;
947  size_t arity;
948  if (!PL_get_name_arity(t, &name, &arity)) break;
949  std::string_view functor(PL_atom_chars(name));
950 
951  if (functor == "," || functor == ";") {
952  os << '(' << ' ';
953  for (n = 1; n <= arity; n++) {
954  if (!PL_get_arg(n, t, a)) break;
955  if (n > 1) {
956  os << '\n' << indent << functor << ' ';
957  }
958  if (!display(os, a, indent+'\t')) os << "_";
959  }
960  os << ' ' << ')';
961  } else {
962  os << functor << '(';
963  for (n = 1; n <= arity; n++) {
964  if (!PL_get_arg(n, t, a)) break;
965  if (n > 1) os << ", ";
966  if (!display(os, a)) os << "_";
967  }
968  os << ')';
969  }
970  return true;
971  }
972  }
973  return false;
974 }
static bool display(std::ostream &os, term_t t, const std::string &indent="")
Definition: PrologTerm.cpp:916

◆ display() [2/2]

static bool knowrob::PrologTerm::display ( std::ostream &  os,
term_t  t,
const std::string &  indent = "" 
)
static
Parameters
osan output stream
ta Prolog term
indentan indentation string
Returns
true if the term was displayed

◆ FUNCTOR_comma() [1/2]

const functor_t & PrologTerm::FUNCTOR_comma ( )
static
Returns
the functor ','/2.

Definition at line 896 of file PrologTerm.cpp.

896  {
897  static atom_t a = PL_new_functor(PrologTerm::ATOM_comma(), 2);
898  return a;
899 }
static const atom_t & ATOM_comma()
Definition: PrologTerm.cpp:886

◆ FUNCTOR_comma() [2/2]

static const functor_t& knowrob::PrologTerm::FUNCTOR_comma ( )
static
Returns
the functor ','/2.

◆ FUNCTOR_semicolon() [1/2]

const functor_t & PrologTerm::FUNCTOR_semicolon ( )
static
Returns
the functor ';'/2.

Definition at line 901 of file PrologTerm.cpp.

901  {
902  static atom_t a = PL_new_functor(PrologTerm::ATOM_semicolon(), 2);
903  return a;
904 }
static const atom_t & ATOM_semicolon()
Definition: PrologTerm.cpp:891

◆ FUNCTOR_semicolon() [2/2]

static const functor_t& knowrob::PrologTerm::FUNCTOR_semicolon ( )
static
Returns
the functor ';'/2.

◆ getVarName() [1/2]

std::string PrologTerm::getVarName ( term_t  plTerm)
static
Parameters
plTerma Prolog variable term
Returns
the name of the variable

Definition at line 680 of file PrologTerm.cpp.

680  {
681  char *s;
682  if (PL_get_chars(plTerm, &s, CVT_VARIABLE|BUF_MALLOC)) {
683  std::string str(s);
684  PL_free(s);
685  return str;
686  } else {
687  throw QueryError("Failed to get variable name.");
688  }
689 }
TermRule & string()
Definition: terms.cpp:63

◆ getVarName() [2/2]

static std::string knowrob::PrologTerm::getVarName ( term_t  plTerm)
static
Parameters
plTerma Prolog variable term
Returns
the name of the variable

◆ isGround() [1/2]

bool knowrob::PrologTerm::isGround ( ) const
inline
Returns
true if the term is ground

Definition at line 201 of file PrologTerm.h.

201 { return vars_.empty(); }

◆ isGround() [2/2]

bool knowrob::PrologTerm::isGround ( ) const
inline
Returns
true if the term is ground

Definition at line 201 of file PrologTerm.h.

201 { return vars_.empty(); }

◆ module() [1/2]

auto knowrob::PrologTerm::module ( ) const
inline
Returns
the module to query

Definition at line 171 of file PrologTerm.h.

171 { return module_; }
std::optional< std::string_view > module_
Definition: PrologTerm.h:267

◆ module() [2/2]

auto knowrob::PrologTerm::module ( ) const
inline
Returns
the module to query

Definition at line 171 of file PrologTerm.h.

171 { return module_; }

◆ nextSolution() [1/2]

bool PrologTerm::nextSolution ( qid_t  qid)
static

If this call succeeds, then vars() should hold the instantiations of the variables.

Parameters
qidthe query id
Returns
true if the query has a next solution

Definition at line 163 of file PrologTerm.cpp.

163  {
164  if (PL_next_solution(qid)) {
165  return true;
166  } else {
167  auto pl_exception = PL_exception(qid);
168  if (pl_exception != (term_t) 0) {
169  // there was an exception
170  auto errorTerm = PrologTerm::toKnowRobTerm(pl_exception);
171  PL_clear_exception();
172  PL_close_query(qid);
173  throw QueryError("Prolog error: {}.", *errorTerm);
174  }
175  // no exception
176  return false;
177  }
178 }
TermPtr toKnowRobTerm() const
Definition: PrologTerm.cpp:691

◆ nextSolution() [2/2]

static bool knowrob::PrologTerm::nextSolution ( qid_t  qid)
static

If this call succeeds, then vars() should hold the instantiations of the variables.

Parameters
qidthe query id
Returns
true if the query has a next solution

◆ nil() [1/2]

PrologTerm PrologTerm::nil ( )
static
Returns
a term representing the empty list

Definition at line 90 of file PrologTerm.cpp.

90  {
91  PrologTerm empty_list;
92  PL_put_nil(empty_list());
93  return empty_list;
94 }

◆ nil() [2/2]

static PrologTerm knowrob::PrologTerm::nil ( )
static
Returns
a term representing the empty list

◆ openQuery() [1/2]

qid_t PrologTerm::openQuery ( int  flags) const
Parameters
flagsthe flags to open the query
Returns
the query id

Definition at line 135 of file PrologTerm.cpp.

135  {
136  if (PL_term_type(plTerm_) != PL_TERM && PL_term_type(plTerm_) != PL_ATOM) {
137  throw QueryError("PrologTerm is not a compound nor atom (actual type: {}).", PL_term_type(plTerm_));
138  }
139  // Note that the context module only matters for meta-predicates
140  module_t ctxModule = module_.has_value() ?
141  PL_new_module(PL_new_atom(module_.value().data())) : nullptr;
142  // get name and arity of the Prolog term
143  size_t arity;
144  atom_t name_atom;
145  if (!PL_get_name_arity(plTerm_, &name_atom, &arity)) {
146  throw QueryError("Failed to get name and arity of Prolog term.");
147  }
148  // create argument term_t references
149  term_t args = PL_new_term_refs(arity);
150  for (std::size_t i = 0; i < arity; i++) {
151  if (!PL_get_arg(i + 1, plTerm_, args + i)) {
152  throw QueryError("Failed to get argument {} of Prolog term.", i);
153  }
154  }
155  // create a predicate_t object and open query
156  predicate_t pred = PL_predicate(
157  PL_atom_nchars(name_atom, nullptr),
158  static_cast<int>(arity),
159  nullptr);
160  return PL_open_query(ctxModule, flags, pred, args);
161 }

◆ openQuery() [2/2]

qid_t knowrob::PrologTerm::openQuery ( int  flags) const
Parameters
flagsthe flags to open the query
Returns
the query id

◆ operator&() [1/2]

PrologTerm PrologTerm::operator& ( const PrologTerm other) const
Parameters
othera term handle
Returns
the conjunction of the two terms

Definition at line 96 of file PrologTerm.cpp.

96  {
97  static const auto comma_f = ",";
98  if (PL_term_type(plTerm_) == PL_VARIABLE) {
99  return other;
100  } else if (PL_term_type(other.plTerm_) == PL_VARIABLE) {
101  return *this;
102  } else {
103  return PrologTerm(comma_f, *this, other);
104  }
105 }

◆ operator&() [2/2]

PrologTerm knowrob::PrologTerm::operator& ( const PrologTerm other) const
Parameters
othera term handle
Returns
the conjunction of the two terms

◆ operator()() [1/2]

term_t knowrob::PrologTerm::operator() ( ) const
inline
Returns
the term handle.

Definition at line 143 of file PrologTerm.h.

143 { return plTerm_; }

◆ operator()() [2/2]

term_t knowrob::PrologTerm::operator() ( ) const
inline
Returns
the term handle.

Definition at line 143 of file PrologTerm.h.

143 { return plTerm_; }

◆ operator|() [1/2]

PrologTerm PrologTerm::operator| ( const PrologTerm other) const
Parameters
othera term handle
Returns
the disjunction of the two terms

Definition at line 107 of file PrologTerm.cpp.

107  {
108  static const auto semicolon_f = ";";
109  if (PL_term_type(plTerm_) == PL_VARIABLE) {
110  return other;
111  } else if (PL_term_type(other.plTerm_) == PL_VARIABLE) {
112  return *this;
113  } else {
114  return PrologTerm(semicolon_f, *this, other);
115  }
116 }

◆ operator|() [2/2]

PrologTerm knowrob::PrologTerm::operator| ( const PrologTerm other) const
Parameters
othera term handle
Returns
the disjunction of the two terms

◆ operator~() [1/2]

PrologTerm PrologTerm::operator~ ( ) const
Parameters
othera term handle
Returns
the negation of the term

Definition at line 118 of file PrologTerm.cpp.

118  {
119  static const auto semicolon_f = "\\+";
120  return PrologTerm(semicolon_f, *this);
121 }

◆ operator~() [2/2]

PrologTerm knowrob::PrologTerm::operator~ ( ) const
Parameters
othera term handle
Returns
the negation of the term

◆ PREDICATE_comma() [1/2]

const predicate_t & PrologTerm::PREDICATE_comma ( )
static
Returns
the comma predicate.

Definition at line 906 of file PrologTerm.cpp.

906  {
907  static predicate_t a = PL_pred(PrologTerm::FUNCTOR_comma(), nullptr);
908  return a;
909 }
static const functor_t & FUNCTOR_comma()
Definition: PrologTerm.cpp:896

◆ PREDICATE_comma() [2/2]

static const predicate_t& knowrob::PrologTerm::PREDICATE_comma ( )
static
Returns
the comma predicate.

◆ PREDICATE_semicolon() [1/2]

const predicate_t & PrologTerm::PREDICATE_semicolon ( )
static
Returns
the semicolon predicate.

Definition at line 911 of file PrologTerm.cpp.

911  {
912  static predicate_t a = PL_pred(PrologTerm::FUNCTOR_semicolon(), nullptr);
913  return a;
914 }
static const functor_t & FUNCTOR_semicolon()
Definition: PrologTerm.cpp:901

◆ PREDICATE_semicolon() [2/2]

static const predicate_t& knowrob::PrologTerm::PREDICATE_semicolon ( )
static
Returns
the semicolon predicate.

◆ putBuiltin() [1/2]

bool PrologTerm::putBuiltin ( const std::shared_ptr< GraphBuiltin > &  builtin,
term_t  plTerm 
)
protected

Definition at line 476 of file PrologTerm.cpp.

476  {
477  static const auto less_a = Atom::Tabled("<");
478  static const auto less_or_equal_a = Atom::Tabled("=<");
479  static const auto greater_a = Atom::Tabled(">");
480  static const auto greater_or_equal_a = Atom::Tabled(">=");
481  static const auto equal_a = Atom::Tabled("==");
482  static const auto not_equal_a = Atom::Tabled("\\==");
483  static const auto bind_f = "=";
484  static const auto min_f = "sw_literal_min";
485  static const auto max_f = "sw_literal_max";
486  static const auto compare_f = "sw_literal_compare";
487 
488  switch (builtin->builtinType()) {
490  if (builtin->arguments().size() == 2) {
491  PrologTerm t = PrologTerm(compare_f, less_a, builtin->arguments()[0], builtin->arguments()[1]);
492  unifyVars(t);
493  return PL_put_term(plTerm, t());
494  }
495  break;
497  if (builtin->arguments().size() == 2) {
498  PrologTerm t = PrologTerm(compare_f, less_or_equal_a, builtin->arguments()[0], builtin->arguments()[1]);
499  unifyVars(t);
500  return PL_put_term(plTerm, t());
501  }
502  break;
504  if (builtin->arguments().size() == 2) {
505  PrologTerm t = PrologTerm(compare_f, greater_a, builtin->arguments()[0], builtin->arguments()[1]);
506  unifyVars(t);
507  return PL_put_term(plTerm, t());
508  }
509  break;
511  if (builtin->arguments().size() == 2) {
512  PrologTerm t = PrologTerm(compare_f, greater_or_equal_a, builtin->arguments()[0], builtin->arguments()[1]);
513  unifyVars(t);
514  return PL_put_term(plTerm, t());
515  }
516  break;
518  if (builtin->arguments().size() == 2) {
519  PrologTerm t = PrologTerm(compare_f, equal_a, builtin->arguments()[0], builtin->arguments()[1]);
520  unifyVars(t);
521  return PL_put_term(plTerm, t());
522  }
523  break;
525  if (builtin->arguments().size() == 1 && builtin->bindVar() != nullptr) {
526  PrologTerm t(bind_f, builtin->bindVar(), builtin->arguments()[0]);
527  unifyVars(t);
528  return PL_put_term(plTerm, t());
529  }
530  break;
532  if (builtin->arguments().size() == 2 && builtin->bindVar() != nullptr) {
533  PrologTerm t(min_f, builtin->arguments()[0], builtin->arguments()[1], builtin->bindVar());
534  unifyVars(t);
535  return PL_put_term(plTerm, t());
536  }
537  break;
539  if (builtin->arguments().size() == 2 && builtin->bindVar() != nullptr) {
540  PrologTerm t(max_f, builtin->arguments()[0], builtin->arguments()[1], builtin->bindVar());
541  unifyVars(t);
542  return PL_put_term(plTerm, t());
543  }
544  break;
546  if (builtin->arguments().size() == 2) {
547  PrologTerm t = PrologTerm(compare_f, not_equal_a, builtin->arguments()[0], builtin->arguments()[1]);
548  unifyVars(t);
549  return PL_put_term(plTerm, t());
550  }
551  break;
552  }
553  return false;
554 }
static std::shared_ptr< knowrob::Atom > Tabled(std::string_view stringForm)
Definition: Atom.cpp:40
void unifyVars(const PrologTerm &other)
Definition: PrologTerm.cpp:123

◆ putBuiltin() [2/2]

bool knowrob::PrologTerm::putBuiltin ( const std::shared_ptr< GraphBuiltin > &  builtin,
term_t  plTerm 
)
protected

◆ putCompound() [1/2]

bool PrologTerm::putCompound ( CompoundFormula phi,
term_t  pl_term,
const functor_t &  pl_functor 
)
protected

Definition at line 651 of file PrologTerm.cpp.

651  { //NOLINT
652  if (phi->formulae().size() == 1) {
653  return putFormula(phi->formulae()[0], pl_term);
654  } else {
655  int counter = 1;
656  term_t last_head = PL_new_term_ref();
657 
658  for (auto i = phi->formulae().size(); i > 0; --i) {
659  auto j = i - 1;
660  if (counter == 1) {
661  // create term for last formula, remember in last_head term
662  if (!putFormula(phi->formulae()[j], last_head)) return false;
663  } else {
664  // create a 2-ary predicate using last_head as second argument
665  term_t pl_arg = PL_new_term_refs(2);
666  if (!putFormula(phi->formulae()[j], pl_arg) ||
667  !PL_put_term(pl_arg + 1, last_head) ||
668  !PL_cons_functor_v((j == 0 ? pl_term : last_head), pl_functor, pl_arg)) {
669  return false;
670  }
671 
672  }
673  counter += 1;
674  }
675 
676  return true;
677  }
678 }
const std::vector< FormulaPtr > & formulae() const

◆ putCompound() [2/2]

bool knowrob::PrologTerm::putCompound ( CompoundFormula phi,
term_t  pl_term,
const functor_t &  pl_functor 
)
protected

◆ putCompoundTerm() [1/2]

bool PrologTerm::putCompoundTerm ( term_t  plTerm,
std::string_view  functor,
const std::vector< PrologTerm > &  args 
)
protected

Definition at line 214 of file PrologTerm.cpp.

214  {
215  // construct argument terms
216  term_t plArgs = PL_new_term_refs(args.size());
217  for (uint32_t i = 0; i < args.size(); i++) {
218  auto &arg_i = args[i];
219  if (!PL_put_term(plArgs + i, arg_i())) {
220  PL_reset_term_refs(plArgs);
221  KB_WARN("Failed to put argument {} into PrologTerm.", i);
222  return false;
223  }
224  unifyVars(arg_i);
225  }
226  // construct compound term
227  if (!PL_cons_functor_v(plTerm,
228  PL_new_functor(PL_new_atom(functor.data()), args.size()),
229  plArgs)) {
230  PL_reset_term_refs(plArgs);
231  KB_WARN("Failed to put functor into PrologTerm.");
232  return false;
233  }
234  return true;
235 }
#define KB_WARN
Definition: Logger.h:27

◆ putCompoundTerm() [2/2]

bool knowrob::PrologTerm::putCompoundTerm ( term_t  plTerm,
std::string_view  functor,
const std::vector< PrologTerm > &  args 
)
protected

◆ putFormula() [1/4]

bool PrologTerm::putFormula ( const FormulaPtr phi)
Parameters
phia KnowRob formula
Returns
true if the term was assigned to the KnowRob formula

Definition at line 286 of file PrologTerm.cpp.

286  {
287  // reset vars before putting a new formula
288  vars_.clear();
289  return putFormula(phi, plTerm_);
290 }

◆ putFormula() [2/4]

bool knowrob::PrologTerm::putFormula ( const FormulaPtr phi)
Parameters
phia KnowRob formula
Returns
true if the term was assigned to the KnowRob formula

◆ putFormula() [3/4]

bool PrologTerm::putFormula ( const FormulaPtr phi,
term_t  plTerm 
)
protected

Definition at line 292 of file PrologTerm.cpp.

292  { //NOLINT
293  static auto negationFun = PL_new_functor(PL_new_atom("\\+"), 1);
294  static auto implicationFun = PL_new_functor(PL_new_atom(":-"), 2);
295 
296  switch (phi->type()) {
298  return putTerm(Predicate::toFunction(std::static_pointer_cast<Predicate>(phi)), plTerm);
299 
300  case FormulaType::NEGATION: {
301  auto negated = std::static_pointer_cast<Negation>(phi)->negatedFormula();
302  auto negated_t = PL_new_term_ref();
303  return putFormula(negated, negated_t) &&
304  PL_cons_functor_v(plTerm, negationFun, negated_t);
305  }
306 
308  auto implication = std::static_pointer_cast<Implication>(phi);
309  auto args = PL_new_term_refs(2);
310  return putFormula(implication->consequent(), args) &&
311  putFormula(implication->antecedent(), args + 1) &&
312  PL_cons_functor_v(plTerm, implicationFun, args);
313  }
314 
316  return putCompound((CompoundFormula *) phi.get(), plTerm, PrologTerm::FUNCTOR_comma());
317 
319  return putCompound((CompoundFormula *) phi.get(), plTerm, PrologTerm::FUNCTOR_semicolon());
320 
321  case FormulaType::MODAL:
322  KB_WARN("Modal formula cannot be mapped to Prolog terms.");
323  return false;
324  }
325  return false;
326 }
static FunctionPtr toFunction(const std::shared_ptr< Predicate > &predicate)
Definition: Predicate.cpp:85
bool putCompound(CompoundFormula *phi, term_t pl_term, const functor_t &pl_functor)
Definition: PrologTerm.cpp:651

◆ putFormula() [4/4]

bool knowrob::PrologTerm::putFormula ( const FormulaPtr phi,
term_t  plTerm 
)
protected

◆ putFunction() [1/2]

bool PrologTerm::putFunction ( Function fn,
term_t  pl_term 
)
protected

Definition at line 340 of file PrologTerm.cpp.

340  { //NOLINT
341  if (fn->arity() > 0) {
342  // create a term reference for each argument of qa_pred
343  term_t pl_arg = PL_new_term_refs(fn->arity());
344  term_t pl_arg0 = pl_arg;
345  // construct argument terms
346  for (const auto &qa_arg: fn->arguments()) {
347  if (!putTerm(qa_arg, pl_arg)) {
348  return false;
349  }
350  pl_arg += 1;
351  }
352  // construct output term
353  return PL_cons_functor_v(pl_term,
354  PL_new_functor(
355  PL_new_atom(fn->functor()->stringForm().data()),
356  fn->arity()),
357  pl_arg0);
358  } else {
359  // 0-ary predicates are atoms
360  return PL_put_atom_chars(pl_term,
361  fn->functor()->stringForm().data());
362  }
363 }
auto arity() const
Definition: Function.h:52
auto & arguments() const
Definition: Function.h:47
auto & functor() const
Definition: Function.h:42

◆ putFunction() [2/2]

bool knowrob::PrologTerm::putFunction ( Function fn,
term_t  pl_term 
)
protected

◆ putList() [1/2]

bool PrologTerm::putList ( ListTerm list,
term_t  pl_term 
)
protected

Definition at line 365 of file PrologTerm.cpp.

365  { //NOLINT
366  if (!PL_put_nil(pl_term)) return false;
367  term_t pl_elem = PL_new_term_ref();
368  for (auto &elem: list->elements()) {
369  if (!putTerm(elem, pl_elem) ||
370  !PL_cons_list(pl_term, pl_elem, pl_term)) {
371  return false;
372  }
373  }
374  return true;
375 }
auto & elements() const
Definition: ListTerm.h:39

◆ putList() [2/2]

bool knowrob::PrologTerm::putList ( ListTerm list,
term_t  pl_term 
)
protected

◆ putNativeAtomic() [1/2]

bool PrologTerm::putNativeAtomic ( const std::shared_ptr< Atomic > &  atomic,
term_t  pl_term 
)
staticprotected

Definition at line 611 of file PrologTerm.cpp.

611  {
612  switch (atomic->atomicType()) {
613  case AtomicType::ATOM:
614  return PL_put_atom_chars(pl_term, atomic->stringForm().data());
615 
616  case AtomicType::STRING:
617  return PL_put_string_chars(pl_term, atomic->stringForm().data());
618 
619  case AtomicType::NUMERIC: {
620  auto *numeric = (Numeric *) atomic.get();
621  switch (numeric->xsdType()) {
622  case XSDType::DOUBLE:
623  return PL_put_float(pl_term, numeric->asDouble());
624  case XSDType::FLOAT:
625  return PL_put_float(pl_term, numeric->asFloat());
627  case XSDType::INTEGER:
628  return PL_put_integer(pl_term, numeric->asInteger());
629  case XSDType::LONG:
630  return PL_put_int64(pl_term, numeric->asLong());
631  case XSDType::SHORT:
632  return PL_put_integer(pl_term, numeric->asShort());
634  return PL_put_uint64(pl_term, numeric->asUnsignedLong());
636  return PL_put_integer(pl_term, numeric->asUnsignedInteger());
638  return PL_put_integer(pl_term, numeric->asUnsignedShort());
639  case XSDType::BOOLEAN:
640  return PL_put_bool(pl_term, numeric->asBoolean());
641  case XSDType::STRING:
642  case XSDType::LAST:
643  break;
644  }
645  break;
646  }
647  }
648  return false;
649 }
TermRule & atomic()
Definition: terms.cpp:79

◆ putNativeAtomic() [2/2]

static bool knowrob::PrologTerm::putNativeAtomic ( const std::shared_ptr< Atomic > &  atomic,
term_t  pl_term 
)
staticprotected

◆ putRDFAtomic() [1/2]

bool PrologTerm::putRDFAtomic ( const std::shared_ptr< Atomic > &  atomic,
term_t  pl_term 
) const
protected

Definition at line 596 of file PrologTerm.cpp.

596  {
597  if (atomic->isIRI() || atomic->isBlank()) {
598  return PL_put_atom_chars(pl_term, atomic->stringForm().data());
599  } else if (atomic->isNumeric() || atomic->isString()) {
600  auto xsdAtomic = std::static_pointer_cast<XSDAtomic>(atomic);
601  auto xsdType = xsdTypeToIRI(xsdAtomic->xsdType());
602  if (isRDFFilter_) {
603  return putTyped(pl_term, atomic->stringForm(), xsdType);
604  } else {
605  return putTypedLiteral(pl_term, atomic->stringForm(), xsdType);
606  }
607  }
608  return false;
609 }
std::string_view xsdTypeToIRI(XSDType type)
Definition: XSDAtomic.cpp:70

◆ putRDFAtomic() [2/2]

bool knowrob::PrologTerm::putRDFAtomic ( const std::shared_ptr< Atomic > &  atomic,
term_t  pl_term 
) const
protected

◆ putTerm() [1/8]

bool PrologTerm::putTerm ( const std::shared_ptr< GraphTerm > &  kbTerm)
Parameters
kbTerma KnowRob term
Returns
true if the term was assigned to the KnowRob term

Definition at line 334 of file PrologTerm.cpp.

334  { //NOLINT(misc-no-recursion)
335  // reset vars before putting a new term
336  vars_.clear();
337  return putTerm(graphTerm, plTerm_);
338 }
GraphTermRule & graphTerm()
Definition: graph.cpp:77

◆ putTerm() [2/8]

bool knowrob::PrologTerm::putTerm ( const std::shared_ptr< GraphTerm > &  kbTerm)
Parameters
kbTerma KnowRob term
Returns
true if the term was assigned to the KnowRob term

◆ putTerm() [3/8]

bool PrologTerm::putTerm ( const std::shared_ptr< GraphTerm > &  term,
term_t  plTerm 
)
protected

Definition at line 378 of file PrologTerm.cpp.

378  { //NOLINT(misc-no-recursion)
379  static const auto rdf_has_f = "rdf_has";
380 
381  switch (kb_term->termType()) {
383  return putTriplePattern(*std::static_pointer_cast<GraphPattern>(kb_term)->value(), rdf_has_f, pl_term);
384 
386  return putBuiltin(std::static_pointer_cast<GraphBuiltin>(kb_term), pl_term);
387 
388  case GraphTermType::Union: {
389  PrologTerm goal;
390  for (const auto &subTerm: std::static_pointer_cast<GraphConnective>(kb_term)->terms()) {
391  goal = (goal | PrologTerm(subTerm));
392  }
393  unifyVars(goal);
394  return PL_put_term(pl_term, goal());
395  }
397  PrologTerm goal;
398  for (const auto &subTerm: std::static_pointer_cast<GraphConnective>(kb_term)->terms()) {
399  goal = (goal & PrologTerm(subTerm));
400  }
401  unifyVars(goal);
402  return PL_put_term(pl_term, goal());
403  }
404  }
405  return false;
406 }
bool putBuiltin(const std::shared_ptr< GraphBuiltin > &builtin, term_t plTerm)
Definition: PrologTerm.cpp:476

◆ putTerm() [4/8]

bool knowrob::PrologTerm::putTerm ( const std::shared_ptr< GraphTerm > &  term,
term_t  plTerm 
)
protected

◆ putTerm() [5/8]

bool PrologTerm::putTerm ( const TermPtr kbTerm)
Parameters
kbTerma KnowRob term
Returns
true if the term was assigned to the KnowRob term

Definition at line 328 of file PrologTerm.cpp.

328  {
329  // reset vars before putting a new term
330  vars_.clear();
331  return putTerm(kbTerm, plTerm_);
332 }

◆ putTerm() [6/8]

bool knowrob::PrologTerm::putTerm ( const TermPtr kbTerm)
Parameters
kbTerma KnowRob term
Returns
true if the term was assigned to the KnowRob term

◆ putTerm() [7/8]

bool PrologTerm::putTerm ( const TermPtr kbTerm,
term_t  plTerm 
)
protected

Definition at line 556 of file PrologTerm.cpp.

556  { //NOLINT
557  switch (kbTerm->termType()) {
558  case TermType::FUNCTION: {
559  auto *fn = (Function *) kbTerm.get();
560  if (*fn->functor() == *ListTerm::listFunctor()) {
561  return putList((ListTerm *) fn, pl_term);
562  } else {
563  return putFunction(fn, pl_term);
564  }
565  }
566  case TermType::VARIABLE: {
567  auto *qa_var = (Variable *) kbTerm.get();
568  auto it = vars_.find(*qa_var);
569  if (it != vars_.end()) {
570  // try to use previously created term_t
571  return PL_put_term(pl_term, it->second);
572  } else if (PL_put_variable(pl_term)) {
573  // create a new variable
574  // Note: it is apparently impossible to assign a name to a variable.
575  // This is why we have to store the variable name in the map, and potentially create
576  // unneeded term_t references for variables that must be unified before querying anyway.
577  // the alternative would be to carry around a shared variable map which would make this
578  // interface slightly ugly
579  vars_[*qa_var] = pl_term;
580  return true;
581  } else {
582  return false;
583  }
584  }
585  case TermType::ATOMIC:
586  if (isRDFTerm_ || isRDFFilter_) {
587  return putRDFAtomic(std::static_pointer_cast<Atomic>(kbTerm), pl_term);
588  } else {
589  return putNativeAtomic(std::static_pointer_cast<Atomic>(kbTerm), pl_term);
590  }
591  }
592 
593  return false;
594 }
static const AtomPtr & listFunctor()
Definition: ListTerm.cpp:17
bool putFunction(Function *fn, term_t pl_term)
Definition: PrologTerm.cpp:340
bool putRDFAtomic(const std::shared_ptr< Atomic > &atomic, term_t pl_term) const
Definition: PrologTerm.cpp:596
static bool putNativeAtomic(const std::shared_ptr< Atomic > &atomic, term_t pl_term)
Definition: PrologTerm.cpp:611
bool putList(ListTerm *list, term_t pl_term)
Definition: PrologTerm.cpp:365

◆ putTerm() [8/8]

bool knowrob::PrologTerm::putTerm ( const TermPtr kbTerm,
term_t  plTerm 
)
protected

◆ putTriple() [1/2]

bool PrologTerm::putTriple ( std::string_view  functor,
const Triple triple 
)
Parameters
functorthe functor of the term
triplea triple
Returns
true if the term was assigned to the triple

Definition at line 237 of file PrologTerm.cpp.

237  {
238  term_t pl_arg = PL_new_term_refs(4);
239  if (!PL_put_atom_chars(pl_arg, triple.subject().data()) ||
240  !PL_put_atom_chars(pl_arg + 1, triple.predicate().data())) {
241  PL_reset_term_refs(pl_arg);
242  return false;
243  }
244  bool o_put = false;
245  if (triple.isObjectIRI() || triple.isObjectBlank()) {
246  o_put = PL_put_atom_chars(pl_arg + 2, triple.valueAsString().data());
247  } else if (triple.xsdType()) {
248  auto xsdType = xsdTypeToIRI(triple.xsdType().value());
249  switch (triple.xsdType().value()) {
250  case XSDType::STRING:
251  o_put = putTypedLiteral(pl_arg + 2, triple.valueAsString().data(), xsdType);
252  break;
253  default:
254  o_put = putTypedLiteral(pl_arg + 2, triple.createStringValue(), xsdType);
255  break;
256  }
257  }
258  if (!o_put) {
259  PL_reset_term_refs(pl_arg);
260  return false;
261  }
262 
263  if (triple.graph()) {
264  if (!PL_put_atom_chars(pl_arg + 3, (*triple.graph()).data())) {
265  PL_reset_term_refs(pl_arg);
266  return false;
267  }
268  } else {
269  // triple has no graph, using fallback origin ImportHierarchy::ORIGIN_USER
270  if (!PL_put_atom_chars(pl_arg + 3, ImportHierarchy::ORIGIN_USER.data())) {
271  PL_reset_term_refs(pl_arg);
272  return false;
273  }
274  }
275 
276  auto pl_functor = PL_new_functor(PL_new_atom(functor.data()), 4);
277  if (!PL_cons_functor_v(plTerm_, pl_functor, pl_arg)) {
278  PL_reset_term_refs(pl_arg);
279  throw QueryError("Failed to put triple into PrologTerm.");
280  }
281  vars_.clear();
282 
283  return true;
284 }
static constexpr std::string_view ORIGIN_USER
virtual std::string_view valueAsString() const =0
auto xsdType() const
Definition: Triple.h:64
virtual std::optional< std::string_view > graph() const =0
virtual std::string_view subject() const =0
virtual std::string_view predicate() const =0
std::string createStringValue() const
Definition: Triple.cpp:71
bool isObjectIRI() const
Definition: Triple.h:54
bool isObjectBlank() const
Definition: Triple.h:49

◆ putTriple() [2/2]

bool knowrob::PrologTerm::putTriple ( std::string_view  functor,
const Triple triple 
)
Parameters
functorthe functor of the term
triplea triple
Returns
true if the term was assigned to the triple

◆ putTriplePattern() [1/2]

bool PrologTerm::putTriplePattern ( const TriplePattern pattern,
const char *  functor,
term_t  plTerm 
)
protected

Definition at line 408 of file PrologTerm.cpp.

408  {
409  static const auto less_f = "lt";
410  static const auto less_or_equal_f = "le";
411  static const auto greater_f = "gt";
412  static const auto greater_or_equal_f = "ge";
413  static const auto equal_f = "eq";
414 
415  PrologTerm objectTerm, patternTerm;
416  if (pat.objectOperator() == FilterType::EQ) {
417  // make sure that `literal(type(Type,Value))` is used for typed literals.
418  objectTerm.isRDFTerm_ = true;
419  objectTerm.putTerm(pat.objectTerm());
420  patternTerm = PrologTerm(functor, pat.subjectTerm(), pat.propertyTerm(), objectTerm);
421  if (pat.isNegated()) {
422  patternTerm = ~patternTerm;
423  }
424  if (pat.isOptional()) {
425  patternTerm = PrologTerm("ignore", patternTerm);
426  }
427  } else {
428  // make sure that `type(Type,Value)` is used for typed literals.
429  objectTerm.isRDFFilter_ = true;
430  objectTerm.putTerm(pat.objectTerm());
431  bool negate = pat.isNegated();
432  PrologTerm filter;
433  switch (pat.objectOperator()) {
434  case FilterType::EQ:
435  filter = PrologTerm(equal_f, objectTerm);
436  break;
437  case FilterType::NEQ:
438  filter = PrologTerm(equal_f, objectTerm);
439  negate = !negate;
440  break;
441  case FilterType::LT:
442  filter = PrologTerm(less_f, objectTerm);
443  break;
444  case FilterType::LEQ:
445  filter = PrologTerm(less_or_equal_f, objectTerm);
446  break;
447  case FilterType::GT:
448  filter = PrologTerm(greater_f, objectTerm);
449  break;
450  case FilterType::GEQ:
451  filter = PrologTerm(greater_or_equal_f, objectTerm);
452  break;
453  }
454  // Note: Object can be of the form literal(+Query, -Value)
455  PrologTerm objectVar = (pat.objectVariable() ? PrologTerm(pat.objectVariable()) : PrologTerm());
456  if (pat.objectVariable()) {
457  vars_[*pat.objectVariable()] = objectVar();
458  }
459  patternTerm = PrologTerm(functor,
460  pat.subjectTerm(),
461  pat.propertyTerm(),
462  PrologTerm("literal", filter, objectVar));
463  if (negate) {
464  patternTerm = ~patternTerm;
465  }
466  if (pat.isOptional()) {
467  PrologTerm negatedTerm(functor, pat.subjectTerm(), pat.propertyTerm(), PrologTerm());
468  patternTerm = PrologTerm("\\+", negatedTerm) | patternTerm;
469  }
470  }
471 
472  unifyVars(patternTerm);
473  return PL_put_term(plTerm, patternTerm());
474 }

◆ putTriplePattern() [2/2]

bool knowrob::PrologTerm::putTriplePattern ( const TriplePattern pattern,
const char *  functor,
term_t  plTerm 
)
protected

◆ readArgs() [1/2]

template<typename ... Args>
std::vector<PrologTerm> knowrob::PrologTerm::readArgs ( Args &&...  args)
inlineprotected

Definition at line 297 of file PrologTerm.h.

297  {
298  // use a fold expression to convert the arguments to a vector
299  // @see https://en.cppreference.com/w/cpp/language/fold
300  std::vector<PrologTerm> argTerms;
301  ([&args, &argTerms] { argTerms.push_back(PrologTerm(args)); }(), ...);
302  return argTerms;
303  }

◆ readArgs() [2/2]

template<typename ... Args>
std::vector<PrologTerm> knowrob::PrologTerm::readArgs ( Args &&...  args)
inlineprotected

Definition at line 297 of file PrologTerm.h.

297  {
298  // use a fold expression to convert the arguments to a vector
299  // @see https://en.cppreference.com/w/cpp/language/fold
300  std::vector<PrologTerm> argTerms;
301  ([&args, &argTerms] { argTerms.push_back(PrologTerm(args)); }(), ...);
302  return argTerms;
303  }

◆ setModule() [1/2]

void knowrob::PrologTerm::setModule ( std::string_view  module)
inline
Parameters
modulethe module to query

Definition at line 166 of file PrologTerm.h.

166 { module_ = module; }
auto module() const
Definition: PrologTerm.h:171

◆ setModule() [2/2]

void knowrob::PrologTerm::setModule ( std::string_view  module)
inline
Parameters
modulethe module to query

Definition at line 166 of file PrologTerm.h.

166 { module_ = module; }

◆ toKnowRobFormula() [1/4]

FormulaPtr PrologTerm::toKnowRobFormula ( ) const
Returns
translates the term to a KnowRob formula

Definition at line 836 of file PrologTerm.cpp.

836  {
838 }
FormulaPtr toKnowRobFormula() const
Definition: PrologTerm.cpp:836

◆ toKnowRobFormula() [2/4]

FormulaPtr knowrob::PrologTerm::toKnowRobFormula ( ) const
Returns
translates the term to a KnowRob formula

◆ toKnowRobFormula() [3/4]

FormulaPtr PrologTerm::toKnowRobFormula ( const TermPtr t)
static
Parameters
ta Prolog term
Returns
translates the term to a KnowRob formula

Definition at line 840 of file PrologTerm.cpp.

841 {
842  static std::string comma_functor = ",";
843  static std::string semicolon_functor = ";";
844 
845  std::shared_ptr<Predicate> p = (
846  t->termType() == TermType::FUNCTION ?
847  Predicate::fromFunction(std::static_pointer_cast<Function>(t)) :
848  Bottom::get());
849 
850  if (p->functor()->stringForm() == comma_functor) {
851  std::vector<FormulaPtr> formulas(p->arity());
852  for (std::size_t i = 0; i < formulas.size(); i++) {
853  formulas[i] = toKnowRobFormula(p->arguments()[i]);
854  }
855  return std::make_shared<Conjunction>(formulas);
856  } else if (p->functor()->stringForm() == semicolon_functor) {
857  std::vector<FormulaPtr> formulas(p->arity());
858  for (std::size_t i = 0; i < formulas.size(); i++) {
859  formulas[i] = toKnowRobFormula(p->arguments()[i]);
860  }
861  return std::make_shared<Disjunction>(formulas);
862  } else {
863  return p;
864  }
865 }
static std::shared_ptr< Predicate > fromFunction(const FunctionPtr &fn)
Definition: Predicate.cpp:89

◆ toKnowRobFormula() [4/4]

static FormulaPtr knowrob::PrologTerm::toKnowRobFormula ( const TermPtr t)
static
Parameters
ta Prolog term
Returns
translates the term to a KnowRob formula

◆ toKnowRobTerm() [1/4]

TermPtr PrologTerm::toKnowRobTerm ( ) const
Returns
translates the term to a KnowRob term

Definition at line 691 of file PrologTerm.cpp.

691  {
692  return toKnowRobTerm(plTerm_);
693 }

◆ toKnowRobTerm() [2/4]

TermPtr knowrob::PrologTerm::toKnowRobTerm ( ) const
Returns
translates the term to a KnowRob term

◆ toKnowRobTerm() [3/4]

TermPtr PrologTerm::toKnowRobTerm ( const term_t &  t)
static
Parameters
ta Prolog term
Returns
translates the term to a KnowRob term

Definition at line 740 of file PrologTerm.cpp.

740  { //NOLINT
741  switch (PL_term_type(t)) {
742  case PL_TERM: {
743  size_t arity;
744  atom_t name;
745  if (!PL_get_name_arity(t, &name, &arity)) break;
746  std::string functorName(PL_atom_chars(name));
747 
748  // Special handling for typed literals. whenever a term is of the form `literal(type(Type,Value))`
749  // it is considered a typed literal, and converted to a corresponding Term type.
750  if (functorName == "literal" && arity == 1) {
751  auto xsdLiteral = toKnowRobLiteral(t);
752  if (xsdLiteral) return xsdLiteral;
753  }
754  if (functorName == "type" && arity == 2) {
755  auto xsdLiteral = toKnowRobLiteral1(t);
756  if (xsdLiteral) return xsdLiteral;
757  }
758 
759  // construct arguments
760  std::vector<TermPtr> arguments(arity);
761  term_t arg = PL_new_term_ref();
762  for (std::size_t n = 1; n <= arity; n++) {
763  if (PL_get_arg(n, t, arg)) {
764  arguments[n - 1] = toKnowRobTerm(arg);
765  } else {
766  KB_WARN("Failed to construct argument {} of predicate {}.", n, functorName);
767  }
768  }
769  // construct Predicate object
770  return std::make_shared<Function>(functorName, arguments);
771  }
772 
773  case PL_VARIABLE:
774  return std::make_shared<Variable>(getVarName(t));
775 
776  case PL_ATOM: {
777  atom_t atom;
778  if (!PL_get_atom(t, &atom)) break;
779  // map `fail/0` and `false/0` to BottomTerm
781  return Bottom::get()->functor();
782  }
783  // map `true/0` to TopTerm
784  else if (atom == PrologTerm::ATOM_true()) {
785  return Top::get()->functor();
786  } else {
787  std::string_view charForm = PL_atom_chars(atom);
788  switch (rdfNodeTypeGuess(charForm)) {
789  case RDFNodeType::BLANK:
790  return Blank::Tabled(charForm);
791  case RDFNodeType::IRI:
792  return IRIAtom::Tabled(charForm);
794  return Atom::Tabled(charForm);
795  }
796  }
797  break;
798  }
799  case PL_INTEGER: {
800  long val = 0;
801  if (!PL_get_long(t, &val)) break;
802  return std::make_shared<Long>(val);
803  }
804  case PL_FLOAT: {
805  double val = 0.0;
806  if (!PL_get_float(t, &val)) break;
807  return std::make_shared<Double>(val);
808  }
809  case PL_STRING: {
810  char *s;
811  if (!PL_get_chars(t, &s, CVT_ALL|BUF_MALLOC)) break;
812  auto str = std::make_shared<String>(s);
813  PL_free(s);
814  return str;
815  }
816  case PL_NIL:
817  return ListTerm::nil();
818  case PL_LIST_PAIR: {
819  term_t head = PL_new_term_ref();
820  std::list<TermPtr> elements;
821  while (PL_get_list(t, head, t)) {
822  elements.push_back(toKnowRobTerm(head));
823  }
824  return std::make_shared<ListTerm>(
825  std::vector<TermPtr>(elements.begin(), elements.end()));
826  }
827  default:
828  KB_WARN("Unknown Prolog term type {}.", PL_term_type(t));
829  break;
830  }
831 
832  KB_WARN("Failed to read Prolog term of type {}.", PL_term_type(t));
833  return Bottom::get()->functor();
834 }
static std::shared_ptr< Blank > Tabled(std::string_view stringForm)
Definition: Blank.cpp:12
static const std::shared_ptr< Bottom > & get()
Definition: Bottom.cpp:12
static std::shared_ptr< IRIAtom > Tabled(std::string_view stringForm)
Definition: IRIAtom.cpp:25
static std::shared_ptr< ListTerm > nil()
Definition: ListTerm.cpp:22
static const atom_t & ATOM_false()
Definition: PrologTerm.cpp:876
static const atom_t & ATOM_true()
Definition: PrologTerm.cpp:881
static const atom_t & ATOM_fail()
Definition: PrologTerm.cpp:871
static const std::shared_ptr< Top > & get()
Definition: Top.cpp:11
StringRule & atom()
Definition: strings.cpp:54
RDFNodeType rdfNodeTypeGuess(std::string_view str)
Definition: RDFNode.cpp:11

◆ toKnowRobTerm() [4/4]

static TermPtr knowrob::PrologTerm::toKnowRobTerm ( const term_t &  t)
static
Parameters
ta Prolog term
Returns
translates the term to a KnowRob term

◆ unifyVars() [1/2]

void PrologTerm::unifyVars ( const PrologTerm other)
protected

Definition at line 123 of file PrologTerm.cpp.

123  {
124  for (auto &pair : other.vars_) {
125  if (vars_.find(pair.first) == vars_.end()) {
126  vars_[pair.first] = pair.second;
127  } else {
128  if(!PL_unify(vars_[pair.first], pair.second)) {
129  KB_WARN("failed to unify two variables.");
130  }
131  }
132  }
133 }

◆ unifyVars() [2/2]

void knowrob::PrologTerm::unifyVars ( const PrologTerm other)
protected

◆ vars() [1/2]

auto& knowrob::PrologTerm::vars ( ) const
inline
Returns
the variables of the term

Definition at line 206 of file PrologTerm.h.

206 { return vars_; }

◆ vars() [2/2]

auto& knowrob::PrologTerm::vars ( ) const
inline
Returns
the variables of the term

Definition at line 206 of file PrologTerm.h.

206 { return vars_; }

◆ write() [1/2]

void PrologTerm::write ( std::ostream &  os) const
overridevirtual

Print this object to a stream.

Parameters
osthe stream to print to.

Implements knowrob::Printable.

Definition at line 976 of file PrologTerm.cpp.

976  {
977  display(os, plTerm_);
978 }

◆ write() [2/2]

void knowrob::PrologTerm::write ( std::ostream &  os) const
overridevirtual

Print this object to a stream.

Parameters
osthe stream to print to.

Implements knowrob::Printable.

Friends And Related Function Documentation

◆ PrologList

PrologList
friend

Definition at line 305 of file PrologTerm.h.

Member Data Documentation

◆ isRDFFilter_

bool knowrob::PrologTerm::isRDFFilter_ = false
protected

Definition at line 270 of file PrologTerm.h.

◆ isRDFTerm_

bool knowrob::PrologTerm::isRDFTerm_ = false
protected

Definition at line 269 of file PrologTerm.h.

◆ module_

std::optional< std::string_view > knowrob::PrologTerm::module_
protected

Definition at line 267 of file PrologTerm.h.

◆ plTerm_

term_t knowrob::PrologTerm::plTerm_
protected

Definition at line 268 of file PrologTerm.h.

◆ vars_

std::map< Variable, term_t, std::less<> > knowrob::PrologTerm::vars_
protected

Definition at line 266 of file PrologTerm.h.


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