knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
PrologTerm.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_PROLOG_TERM_H
7 #define KNOWROB_PROLOG_TERM_H
8 
9 #include <SWI-Prolog.h>
10 #include "knowrob/terms/Term.h"
11 #include "knowrob/formulas/Formula.h"
12 #include "knowrob/formulas/CompoundFormula.h"
13 #include "knowrob/semweb/Triple.h"
14 #include "knowrob/semweb/TriplePattern.h"
15 #include "knowrob/semweb/GraphQuery.h"
16 #include "knowrob/semweb/GraphBuiltin.h"
17 #include "knowrob/terms/Function.h"
18 #include "knowrob/terms/ListTerm.h"
19 #include <iostream>
20 
21 //#define KNOWROB_USE_PROLOG_RDF11
22 #define KNOWROB_USE_PROLOG_RDF_DB
23 
24 namespace knowrob {
29  class PrologTerm : public Printable {
30  public:
34  static const atom_t &ATOM_fail();
35 
39  static const atom_t &ATOM_false();
40 
44  static const atom_t &ATOM_true();
45 
49  static const atom_t &ATOM_comma();
50 
54  static const atom_t &ATOM_semicolon();
55 
59  static const functor_t &FUNCTOR_comma();
60 
64  static const functor_t &FUNCTOR_semicolon();
65 
69  static const predicate_t &PREDICATE_comma();
70 
74  static const predicate_t &PREDICATE_semicolon();
75 
80 
85  explicit PrologTerm(const GraphQueryPtr &query);
86 
91  explicit PrologTerm(const std::shared_ptr<GraphTerm> &kbTerm);
92 
97  explicit PrologTerm(const TermPtr &kbTerm);
98 
103  explicit PrologTerm(const FormulaPtr &kbFormula);
104 
109  explicit PrologTerm(const TriplePattern &literal, const char *triple_functor);
110 
116  PrologTerm(const Triple &triple, std::string_view functor);
117 
123  PrologTerm(const std::vector<PrologTerm> &args, std::string_view functor);
124 
131  template<typename ... Args>
132  explicit PrologTerm(std::string_view functor, Args &&... args)
133  : PrologTerm(readArgs(std::forward<Args>(args)...), functor) {}
134 
138  PrologTerm(const PrologTerm &other) = default;
139 
143  term_t operator()() const { return plTerm_; }
144 
149  PrologTerm operator&(const PrologTerm &other) const;
150 
155  PrologTerm operator|(const PrologTerm &other) const;
156 
162 
166  void setModule(std::string_view module) { module_ = module; }
167 
171  auto module() const { return module_; }
172 
178  bool putTriple(std::string_view functor, const Triple &triple);
179 
184  bool putTerm(const TermPtr &kbTerm);
185 
190  bool putTerm(const std::shared_ptr<GraphTerm> &kbTerm);
191 
196  bool putFormula(const FormulaPtr &phi);
197 
201  bool isGround() const { return vars_.empty(); }
202 
206  auto &vars() const { return vars_; }
207 
212  qid_t openQuery(int flags) const;
213 
219  static bool nextSolution(qid_t qid);
220 
225 
230 
235  static TermPtr toKnowRobTerm(const term_t &t);
236 
242 
247  static std::string getVarName(term_t plTerm);
248 
252  static PrologTerm nil();
253 
260  static bool display(std::ostream &os, term_t t, const std::string &indent = "");
261 
262  // Printable interface
263  void write(std::ostream &os) const override;
264 
265  protected:
266  std::map<Variable, term_t, std::less<>> vars_;
267  std::optional<std::string_view> module_;
268  term_t plTerm_;
269  bool isRDFTerm_ = false;
270  bool isRDFFilter_ = false;
271 
272  bool putFormula(const FormulaPtr &phi, term_t plTerm);
273 
274  bool putFunction(Function *fn, term_t pl_term);
275 
276  bool putList(ListTerm *list, term_t pl_term);
277 
278  bool putTerm(const TermPtr &kbTerm, term_t plTerm);
279 
280  bool putCompound(CompoundFormula *phi, term_t pl_term, const functor_t &pl_functor);
281 
282  bool putCompoundTerm(term_t plTerm, std::string_view functor, const std::vector<PrologTerm> &args);
283 
284  bool putTerm(const std::shared_ptr<GraphTerm> &term, term_t plTerm);
285 
286  bool putTriplePattern(const TriplePattern &pattern, const char *functor, term_t plTerm);
287 
288  static bool putNativeAtomic(const std::shared_ptr<Atomic> &atomic, term_t pl_term);
289 
290  bool putRDFAtomic(const std::shared_ptr<Atomic> &atomic, term_t pl_term) const;
291 
292  bool putBuiltin(const std::shared_ptr<GraphBuiltin> &builtin, term_t plTerm);
293 
294  void unifyVars(const PrologTerm &other);
295 
296  template<typename ... Args>
297  std::vector<PrologTerm> readArgs(Args &&... args) {
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  }
304 
305  friend class PrologList;
306  };
307 
308  class PrologList : public PrologTerm {
309  public:
310  explicit PrologList(const std::vector<PrologTerm> &elements);
311  };
312 
313 } // knowrob
314 
315 #endif //KNOWROB_PROLOG_TERM_H
PrologList(const std::vector< PrologTerm > &elements)
static const predicate_t & PREDICATE_comma()
PrologTerm operator&(const PrologTerm &other) const
bool putTerm(const TermPtr &kbTerm, term_t plTerm)
static const functor_t & FUNCTOR_semicolon()
static const atom_t & ATOM_semicolon()
static const predicate_t & PREDICATE_semicolon()
static const functor_t & FUNCTOR_comma()
bool putTriplePattern(const TriplePattern &pattern, const char *functor, term_t plTerm)
bool putTriple(std::string_view functor, const Triple &triple)
bool putTerm(const std::shared_ptr< GraphTerm > &term, term_t plTerm)
void setModule(std::string_view module)
Definition: PrologTerm.h:166
bool putList(ListTerm *list, term_t pl_term)
static PrologTerm nil()
PrologTerm operator|(const PrologTerm &other) const
FormulaPtr toKnowRobFormula() const
bool putRDFAtomic(const std::shared_ptr< Atomic > &atomic, term_t pl_term) const
bool putCompoundTerm(term_t plTerm, std::string_view functor, const std::vector< PrologTerm > &args)
static bool putNativeAtomic(const std::shared_ptr< Atomic > &atomic, term_t pl_term)
PrologTerm(const FormulaPtr &kbFormula)
PrologTerm(const std::shared_ptr< GraphTerm > &kbTerm)
std::map< Variable, term_t, std::less<> > vars_
Definition: PrologTerm.h:266
PrologTerm(const TermPtr &kbTerm)
bool putFormula(const FormulaPtr &phi, term_t plTerm)
PrologTerm(const Triple &triple, std::string_view functor)
static const atom_t & ATOM_false()
bool putCompound(CompoundFormula *phi, term_t pl_term, const functor_t &pl_functor)
auto module() const
Definition: PrologTerm.h:171
static bool display(std::ostream &os, term_t t, const std::string &indent="")
bool isGround() const
Definition: PrologTerm.h:201
static FormulaPtr toKnowRobFormula(const TermPtr &t)
qid_t openQuery(int flags) const
PrologTerm(const GraphQueryPtr &query)
static TermPtr toKnowRobTerm(const term_t &t)
PrologTerm(const TriplePattern &literal, const char *triple_functor)
std::optional< std::string_view > module_
Definition: PrologTerm.h:267
bool putBuiltin(const std::shared_ptr< GraphBuiltin > &builtin, term_t plTerm)
std::vector< PrologTerm > readArgs(Args &&... args)
Definition: PrologTerm.h:297
void write(std::ostream &os) const override
TermPtr toKnowRobTerm() const
static const atom_t & ATOM_true()
static bool nextSolution(qid_t qid)
static const atom_t & ATOM_fail()
PrologTerm operator~() const
bool putFormula(const FormulaPtr &phi)
bool putTerm(const std::shared_ptr< GraphTerm > &kbTerm)
auto & vars() const
Definition: PrologTerm.h:206
PrologTerm(std::string_view functor, Args &&... args)
Definition: PrologTerm.h:132
PrologTerm(const PrologTerm &other)=default
static std::string getVarName(term_t plTerm)
static const atom_t & ATOM_comma()
term_t operator()() const
Definition: PrologTerm.h:143
void unifyVars(const PrologTerm &other)
bool putTerm(const TermPtr &kbTerm)
PrologTerm(const std::vector< PrologTerm > &args, std::string_view functor)
bool putFunction(Function *fn, term_t pl_term)
GraphTermRule & pattern()
Definition: graph.cpp:23
TermRule & string()
Definition: terms.cpp:63
TermRule & atomic()
Definition: terms.cpp:79
TermRule & term()
Definition: terms.cpp:136
std::shared_ptr< Term > TermPtr
Definition: Term.h:117
std::shared_ptr< Formula > FormulaPtr
Definition: Formula.h:99
std::shared_ptr< GraphQuery > GraphQueryPtr
Definition: GraphQuery.h:65