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

#include <SPARQLQuery.h>

Public Member Functions

 SPARQLQuery (const TriplePattern &triplePattern, SPARQLFlags flags=SPARQLFlag::NOTHING)
 
 SPARQLQuery (const std::shared_ptr< GraphQuery > &query, SPARQLFlags flags=SPARQLFlag::NOTHING)
 
std::string_view operator() () const
 
const unsigned char * asUnsignedString () const
 
 SPARQLQuery (const TriplePattern &triplePattern, SPARQLFlags flags=SPARQLFlag::NOTHING)
 
 SPARQLQuery (const std::shared_ptr< GraphQuery > &query, SPARQLFlags flags=SPARQLFlag::NOTHING)
 
std::string_view operator() () const
 
const unsigned char * asUnsignedString () const
 

Protected Member Functions

void selectBegin (std::ostream &os)
 
void appendPrefixes (std::ostream &os)
 
void add (std::ostream &os, const TriplePattern &triplePattern)
 
void add (std::ostream &os, const GraphBuiltin &builtin)
 
void add (std::ostream &os, const std::shared_ptr< GraphTerm > &graphTerm)
 
bool optional (std::ostream &os, const TriplePattern &triplePattern)
 
void iri (std::ostream &os, std::string_view iri)
 
void negationViaNotExists (std::ostream &os, const TriplePattern &triplePattern)
 
void negationViaOptional (std::ostream &os, const TriplePattern &triplePattern)
 
bool where (std::ostream &os, const TriplePattern &triplePattern)
 
void where_with_filter (std::ostream &os, const TriplePattern &triplePattern)
 
void where (std::ostream &os, const TermPtr &term)
 
void comparison (std::ostream &os, const GraphBuiltin &builtin, const char *comparisonOperator)
 
void bindOneOfIf (std::ostream &os, const GraphBuiltin &builtin, const char *comparisonOperator)
 
void filter (std::ostream &os, std::string_view varName, const TermPtr &term, FilterType operatorType)
 
void filter_optional (std::ostream &os, std::string_view varName, const TermPtr &term, FilterType operatorType)
 
void doFilter (std::ostream &os, std::string_view varName, const std::shared_ptr< Atomic > &atomic, FilterType operatorType)
 
void selectBegin (std::ostream &os)
 
void appendPrefixes (std::ostream &os)
 
void add (std::ostream &os, const TriplePattern &triplePattern)
 
void add (std::ostream &os, const GraphBuiltin &builtin)
 
void add (std::ostream &os, const std::shared_ptr< GraphTerm > &graphTerm)
 
bool optional (std::ostream &os, const TriplePattern &triplePattern)
 
void iri (std::ostream &os, std::string_view iri)
 
void negationViaNotExists (std::ostream &os, const TriplePattern &triplePattern)
 
void negationViaOptional (std::ostream &os, const TriplePattern &triplePattern)
 
bool where (std::ostream &os, const TriplePattern &triplePattern)
 
void where_with_filter (std::ostream &os, const TriplePattern &triplePattern)
 
void where (std::ostream &os, const TermPtr &term)
 
void comparison (std::ostream &os, const GraphBuiltin &builtin, const char *comparisonOperator)
 
void bindOneOfIf (std::ostream &os, const GraphBuiltin &builtin, const char *comparisonOperator)
 
void filter (std::ostream &os, std::string_view varName, const TermPtr &term, FilterType operatorType)
 
void filter_optional (std::ostream &os, std::string_view varName, const TermPtr &term, FilterType operatorType)
 
void doFilter (std::ostream &os, std::string_view varName, const std::shared_ptr< Atomic > &atomic, FilterType operatorType)
 

Static Protected Member Functions

static void selectEnd (std::ostream &os)
 
static void selectEnd (std::ostream &os)
 

Protected Attributes

SPARQLFlags flags_
 
uint32_t varCounter_
 
std::map< std::string_view, std::string_view > aliases_
 
std::set< std::string_view > variables_
 
std::string queryString_
 
std::string lastVar_
 

Detailed Description

A SPARQL query using 1.1 syntax. But since some backends do not support all features, this class allows to specify some features that cannot be used.

Definition at line 45 of file SPARQLQuery.h.

Constructor & Destructor Documentation

◆ SPARQLQuery() [1/4]

SPARQLQuery::SPARQLQuery ( const TriplePattern triplePattern,
SPARQLFlags  flags = SPARQLFlag::NOTHING 
)
explicit

Create a SPARQL query that selects all triples matching the given pattern.

Parameters
triplePatternthe pattern to match.
flagsthe flags that indicate which features are supported.

Definition at line 15 of file SPARQLQuery.cpp.

16  : flags_(flags), varCounter_(0) {
17  std::stringstream os, os_query;
18  selectBegin(os_query);
19  add(os_query, triplePattern);
20  selectEnd(os_query);
21 
22  appendPrefixes(os);
23  os << os_query.str();
24  queryString_ = os.str();
25 }
void selectBegin(std::ostream &os)
SPARQLFlags flags_
Definition: SPARQLQuery.h:72
void appendPrefixes(std::ostream &os)
Definition: SPARQLQuery.cpp:44
static void selectEnd(std::ostream &os)
void add(std::ostream &os, const TriplePattern &triplePattern)
Definition: SPARQLQuery.cpp:85
std::string queryString_
Definition: SPARQLQuery.h:76

◆ SPARQLQuery() [2/4]

SPARQLQuery::SPARQLQuery ( const std::shared_ptr< GraphQuery > &  query,
SPARQLFlags  flags = SPARQLFlag::NOTHING 
)
explicit
Parameters
querythe query to execute.
flagsthe flags that indicate which features are supported.

Definition at line 27 of file SPARQLQuery.cpp.

28  : flags_(flags), varCounter_(0) {
29  std::stringstream os_query;
30  add(os_query, query->term());
31 
32  std::stringstream os;
33  appendPrefixes(os);
34  selectBegin(os);
35  os << os_query.str();
36  selectEnd(os);
37  if (query->ctx()->queryFlags & QUERY_FLAG_ONE_SOLUTION) {
38  os << "\nLIMIT 1";
39  }
40 
41  queryString_ = os.str();
42 }
@ QUERY_FLAG_ONE_SOLUTION
Definition: QueryFlag.h:17

◆ SPARQLQuery() [3/4]

knowrob::SPARQLQuery::SPARQLQuery ( const TriplePattern triplePattern,
SPARQLFlags  flags = SPARQLFlag::NOTHING 
)
explicit

Create a SPARQL query that selects all triples matching the given pattern.

Parameters
triplePatternthe pattern to match.
flagsthe flags that indicate which features are supported.

◆ SPARQLQuery() [4/4]

knowrob::SPARQLQuery::SPARQLQuery ( const std::shared_ptr< GraphQuery > &  query,
SPARQLFlags  flags = SPARQLFlag::NOTHING 
)
explicit
Parameters
querythe query to execute.
flagsthe flags that indicate which features are supported.

Member Function Documentation

◆ add() [1/6]

void SPARQLQuery::add ( std::ostream &  os,
const GraphBuiltin builtin 
)
protected

Definition at line 154 of file SPARQLQuery.cpp.

154  {
155  switch (builtin.builtinType()) {
157  comparison(os, builtin, "=");
158  break;
160  comparison(os, builtin, "!=");
161  break;
163  comparison(os, builtin, "<");
164  break;
166  comparison(os, builtin, ">");
167  break;
169  comparison(os, builtin, "<=");
170  break;
172  comparison(os, builtin, ">=");
173  break;
175  os << " BIND (";
176  where(os, builtin.arguments()[0]);
177  os << "AS ?" << builtin.bindVar()->name();
178  os << ")\n";
179  break;
181  bindOneOfIf(os, builtin, ">");
182  break;
184  bindOneOfIf(os, builtin, "<");
185  break;
186  }
187 }
auto & arguments() const
Definition: Function.h:47
auto bindVar() const
Definition: GraphBuiltin.h:113
auto builtinType() const
Definition: GraphBuiltin.h:108
void comparison(std::ostream &os, const GraphBuiltin &builtin, const char *comparisonOperator)
void bindOneOfIf(std::ostream &os, const GraphBuiltin &builtin, const char *comparisonOperator)
bool where(std::ostream &os, const TriplePattern &triplePattern)

◆ add() [2/6]

void knowrob::SPARQLQuery::add ( std::ostream &  os,
const GraphBuiltin builtin 
)
protected

◆ add() [3/6]

void SPARQLQuery::add ( std::ostream &  os,
const std::shared_ptr< GraphTerm > &  graphTerm 
)
protected

Definition at line 51 of file SPARQLQuery.cpp.

51  { // NOLINT
52  switch (graphTerm->termType()) {
53  case GraphTermType::Union: {
54  auto sequence = std::static_pointer_cast<GraphSequence>(graphTerm);
55  bool isFirst = true;
56  for (const auto &term: sequence->terms()) {
57  if (!isFirst) {
58  os << " UNION\n";
59  } else {
60  isFirst = false;
61  }
62  os << " {\n";
63  add(os, term);
64  os << " }\n";
65  }
66  break;
67 
68  }
70  auto sequence = std::static_pointer_cast<GraphSequence>(graphTerm);
71  for (const auto &term: sequence->terms()) {
72  add(os, term);
73  }
74  break;
75  }
77  add(os, *std::static_pointer_cast<GraphPattern>(graphTerm)->value());
78  break;
80  add(os, *std::static_pointer_cast<GraphBuiltin>(graphTerm));
81  break;
82  }
83 }
GraphTermRule & graphTerm()
Definition: graph.cpp:77
TermRule & term()
Definition: terms.cpp:136

◆ add() [4/6]

void knowrob::SPARQLQuery::add ( std::ostream &  os,
const std::shared_ptr< GraphTerm > &  graphTerm 
)
protected

◆ add() [5/6]

void SPARQLQuery::add ( std::ostream &  os,
const TriplePattern triplePattern 
)
protected

Definition at line 85 of file SPARQLQuery.cpp.

85  {
86  os << " ";
87  if (triplePattern.isNegated()) {
88  // Handle negated patterns. There is literature available for negation in SPARQL:
89  // https://ceur-ws.org/Vol-1644/paper11.pdf
90  // The authors list some approaches:
91  // - use OPTIONAL and then check with BOUND that it failed. But that would only do the trick
92  // if the negated pattern actually has some runtime variables, as far as I understand.
93  // - use FILTER NOT EXISTS: `FILTER NOT EXISTS { ?x ?y ?z }`.
94  // - use MINUS: `MINUS { ?x ?y ?z }`.
95  // NOTE: MINUS will not work without a positive statement preceding the negated one
96  // as far as I understand because both can only be used to "eliminate" solutions that were produced before.
97  // Which is actually fine as the KB does order positive/negative literals in the query,
98  // However queries with only negated patterns will not work with this code!
100  // NOTE: redland does not support NOT-EXISTS or MINUS.
101  negationViaOptional(os, triplePattern);
102  } else {
103  negationViaNotExists(os, triplePattern);
104  }
105  } else if (triplePattern.isOptional()) {
106  if (optional(os, triplePattern)) {
107  filter_optional(os, lastVar_, triplePattern.objectTerm(), triplePattern.objectOperator());
108  }
109  } else {
110  where_with_filter(os, triplePattern);
111  }
112  os << '\n';
113 }
bool optional(std::ostream &os, const TriplePattern &triplePattern)
void where_with_filter(std::ostream &os, const TriplePattern &triplePattern)
std::string lastVar_
Definition: SPARQLQuery.h:77
void negationViaNotExists(std::ostream &os, const TriplePattern &triplePattern)
void filter_optional(std::ostream &os, std::string_view varName, const TermPtr &term, FilterType operatorType)
void negationViaOptional(std::ostream &os, const TriplePattern &triplePattern)
auto objectOperator() const
auto & objectTerm() const
Definition: TriplePattern.h:97

◆ add() [6/6]

void knowrob::SPARQLQuery::add ( std::ostream &  os,
const TriplePattern triplePattern 
)
protected

◆ appendPrefixes() [1/2]

void SPARQLQuery::appendPrefixes ( std::ostream &  os)
protected

Definition at line 44 of file SPARQLQuery.cpp.

44  {
45  for (const auto &[alias, uri]: aliases_) {
46  os << "PREFIX " << alias << ": <" << uri << "#>\n";
47  }
48 }
std::map< std::string_view, std::string_view > aliases_
Definition: SPARQLQuery.h:74

◆ appendPrefixes() [2/2]

void knowrob::SPARQLQuery::appendPrefixes ( std::ostream &  os)
protected

◆ asUnsignedString() [1/2]

const unsigned char* knowrob::SPARQLQuery::asUnsignedString ( ) const
inline
Returns
the query string as a unsigned C string.

Definition at line 69 of file SPARQLQuery.h.

69 { return reinterpret_cast<const unsigned char *>(queryString_.c_str()); }

◆ asUnsignedString() [2/2]

const unsigned char* knowrob::SPARQLQuery::asUnsignedString ( ) const
inline
Returns
the query string as a unsigned C string.

Definition at line 69 of file SPARQLQuery.h.

69 { return reinterpret_cast<const unsigned char *>(queryString_.c_str()); }

◆ bindOneOfIf() [1/2]

void SPARQLQuery::bindOneOfIf ( std::ostream &  os,
const GraphBuiltin builtin,
const char *  comparisonOperator 
)
protected

Definition at line 134 of file SPARQLQuery.cpp.

134  {
135  // e.g. `BIND(IF(BOUND(?optional_val) && !(?begin < ?optional_val), ?optional_val, ?begin) AS ?begin)`
136  // NOTE: Here it is assumed that only the second argument could be undefined in the evaluation context.
137  os << " BIND ( IF( (";
138  if (builtin.arguments()[1]->isVariable()) {
139  os << "BOUND(?" << std::static_pointer_cast<Variable>(builtin.arguments()[1])->name() << ") && ";
140  }
141  os << "(";
142  {
143  where(os, builtin.arguments()[0]);
144  os << ' ' << comparisonOperator << ' ';
145  where(os, builtin.arguments()[1]);
146  }
147  os << ")), ";
148  where(os, builtin.arguments()[1]);
149  os << ", ";
150  where(os, builtin.arguments()[0]);
151  os << ") AS ?" << builtin.bindVar()->name() << ")\n";
152 }

◆ bindOneOfIf() [2/2]

void knowrob::SPARQLQuery::bindOneOfIf ( std::ostream &  os,
const GraphBuiltin builtin,
const char *  comparisonOperator 
)
protected

◆ comparison() [1/2]

void SPARQLQuery::comparison ( std::ostream &  os,
const GraphBuiltin builtin,
const char *  comparisonOperator 
)
protected

Definition at line 115 of file SPARQLQuery.cpp.

115  {
116  // Filter using a comparison operator, also succeed if one of the arguments is not bound.
117  // e.g. `FILTER (!BOUND(?begin) || !BOUND(?end) || ?begin < ?end)`
118  os << " FILTER ( ";
119  if (builtin.isOptional()) {
120  if (builtin.arguments()[0]->isVariable()) {
121  os << "!BOUND(?" << std::static_pointer_cast<Variable>(builtin.arguments()[0])->name() << ") || ";
122  }
123  if (builtin.arguments()[1]->isVariable()) {
124  os << "!BOUND(?" << std::static_pointer_cast<Variable>(builtin.arguments()[1])->name() << ") || ";
125  }
126  }
127  os << '(';
128  where(os, builtin.arguments()[0]);
129  os << comparisonOperator << ' ';
130  where(os, builtin.arguments()[1]);
131  os << "))\n";
132 }
bool isOptional() const
Definition: GraphBuiltin.h:246

◆ comparison() [2/2]

void knowrob::SPARQLQuery::comparison ( std::ostream &  os,
const GraphBuiltin builtin,
const char *  comparisonOperator 
)
protected

◆ doFilter() [1/2]

void SPARQLQuery::doFilter ( std::ostream &  os,
std::string_view  varName,
const std::shared_ptr< Atomic > &  atomic,
FilterType  operatorType 
)
protected

Definition at line 222 of file SPARQLQuery.cpp.

222  {
223  os << '?' << varName;
224  switch (operatorType) {
225  case FilterType::LT:
226  os << " < ";
227  break;
228  case FilterType::GT:
229  os << " > ";
230  break;
231  case FilterType::LEQ:
232  os << " <= ";
233  break;
234  case FilterType::GEQ:
235  os << " >= ";
236  break;
237  case FilterType::EQ:
238  os << " = ";
239  break;
240  case FilterType::NEQ:
241  os << " != ";
242  break;
243  }
244  if (atomic->isNumeric()) {
245  os << '"' << *atomic << '"' << "^^";
246  iri(os, xsdTypeToIRI(std::static_pointer_cast<XSDAtomic>(atomic)->xsdType()));
247  }
248  else if (atomic->isString()) {
249  os << *atomic << "^^";
250  iri(os, xsdTypeToIRI(std::static_pointer_cast<XSDAtomic>(atomic)->xsdType()));
251  } else {
252  os << atomic->stringForm();
253  }
254  os << " ";
255 }
void iri(std::ostream &os, std::string_view iri)
TermRule & atomic()
Definition: terms.cpp:79
std::string_view xsdTypeToIRI(XSDType type)
Definition: XSDAtomic.cpp:70

◆ doFilter() [2/2]

void knowrob::SPARQLQuery::doFilter ( std::ostream &  os,
std::string_view  varName,
const std::shared_ptr< Atomic > &  atomic,
FilterType  operatorType 
)
protected

◆ filter() [1/2]

void SPARQLQuery::filter ( std::ostream &  os,
std::string_view  varName,
const TermPtr term,
FilterType  operatorType 
)
protected

Definition at line 214 of file SPARQLQuery.cpp.

214  {
215  if (!term->isAtomic()) return;
216  auto atomic = std::static_pointer_cast<Atomic>(term);
217  os << "FILTER (";
218  doFilter(os, varName, std::static_pointer_cast<Atomic>(term), operatorType);
219  os << ") ";
220 }
void doFilter(std::ostream &os, std::string_view varName, const std::shared_ptr< Atomic > &atomic, FilterType operatorType)

◆ filter() [2/2]

void knowrob::SPARQLQuery::filter ( std::ostream &  os,
std::string_view  varName,
const TermPtr term,
FilterType  operatorType 
)
protected

◆ filter_optional() [1/2]

void SPARQLQuery::filter_optional ( std::ostream &  os,
std::string_view  varName,
const TermPtr term,
FilterType  operatorType 
)
protected

Definition at line 206 of file SPARQLQuery.cpp.

206  {
207  if (!term->isAtomic()) return;
208  os << "FILTER (";
209  os << " !BOUND(?" << varName << ") || ";
210  doFilter(os, varName, std::static_pointer_cast<Atomic>(term), operatorType);
211  os << ") ";
212 }

◆ filter_optional() [2/2]

void knowrob::SPARQLQuery::filter_optional ( std::ostream &  os,
std::string_view  varName,
const TermPtr term,
FilterType  operatorType 
)
protected

◆ iri() [1/2]

void SPARQLQuery::iri ( std::ostream &  os,
std::string_view  iri 
)
protected

Definition at line 289 of file SPARQLQuery.cpp.

289  {
290  auto ns = semweb::Resource::iri_ns(iri);
291  bool hasAlias = false;
292  if (!ns.empty()) {
293  auto alias = PrefixRegistry::uriToAlias(ns);
294  if (alias.has_value()) {
295  auto &v_alias = alias.value().get();
296  os << v_alias << ':' << semweb::Resource::iri_name(iri);
297  aliases_[v_alias] = ns;
298  hasAlias = true;
299  }
300  }
301  if (!hasAlias) {
302  os << '<' << iri << '>';
303  }
304  os << ' ';
305 }
static OptionalStringRef uriToAlias(std::string_view uri)
static std::string_view iri_name(std::string_view iri)
Definition: Resource.cpp:57
static std::string_view iri_ns(std::string_view iri, bool includeDelimiter=false)
Definition: Resource.cpp:69

◆ iri() [2/2]

void knowrob::SPARQLQuery::iri ( std::ostream &  os,
std::string_view  iri 
)
protected

◆ negationViaNotExists() [1/2]

void SPARQLQuery::negationViaNotExists ( std::ostream &  os,
const TriplePattern triplePattern 
)
protected

Definition at line 257 of file SPARQLQuery.cpp.

257  {
258  os << "FILTER NOT EXISTS { ";
259  where_with_filter(os, triplePattern);
260  os << "} ";
261 }

◆ negationViaNotExists() [2/2]

void knowrob::SPARQLQuery::negationViaNotExists ( std::ostream &  os,
const TriplePattern triplePattern 
)
protected

◆ negationViaOptional() [1/2]

void SPARQLQuery::negationViaOptional ( std::ostream &  os,
const TriplePattern triplePattern 
)
protected

Definition at line 263 of file SPARQLQuery.cpp.

263  {
264  if (triplePattern.objectTerm()->isVariable()) {
265  bool hasObjectOperator = optional(os, triplePattern);
266  os << "FILTER (";
267  if (hasObjectOperator) {
268  auto inverseOperator = inverseFilterType(triplePattern.objectOperator());
269  auto atomic = std::static_pointer_cast<Atomic>(triplePattern.objectTerm());
270  doFilter(os, lastVar_, atomic, inverseOperator);
271  } else {
272  os << " !BOUND(";
273  where(os, triplePattern.objectTerm());
274  os << ") ";
275  }
276  os << ") ";
277  } else {
278  KB_WARN("Negation via optional is only supported for variable objects.");
279  }
280 }
#define KB_WARN
Definition: Logger.h:27
FilterType inverseFilterType(FilterType op)

◆ negationViaOptional() [2/2]

void knowrob::SPARQLQuery::negationViaOptional ( std::ostream &  os,
const TriplePattern triplePattern 
)
protected

◆ operator()() [1/2]

std::string_view knowrob::SPARQLQuery::operator() ( ) const
inline
Returns
the query string.

Definition at line 63 of file SPARQLQuery.h.

63 { return queryString_; }

◆ operator()() [2/2]

std::string_view knowrob::SPARQLQuery::operator() ( ) const
inline
Returns
the query string.

Definition at line 63 of file SPARQLQuery.h.

63 { return queryString_; }

◆ optional() [1/2]

bool SPARQLQuery::optional ( std::ostream &  os,
const TriplePattern triplePattern 
)
protected

Definition at line 282 of file SPARQLQuery.cpp.

282  {
283  os << "OPTIONAL { ";
284  bool needsFilter = where(os, triplePattern);
285  os << "} ";
286  return needsFilter;
287 }

◆ optional() [2/2]

bool knowrob::SPARQLQuery::optional ( std::ostream &  os,
const TriplePattern triplePattern 
)
protected

◆ selectBegin() [1/2]

void SPARQLQuery::selectBegin ( std::ostream &  os)
protected

Definition at line 189 of file SPARQLQuery.cpp.

189  {
190  if (variables_.empty()) {
191  os << "SELECT * ";
192  } else {
193  os << "SELECT ";
194  for (const auto &var: variables_) {
195  os << '?' << var << ' ';
196  }
197  }
198  os << '\n';
199  os << "WHERE {\n";
200 }
std::set< std::string_view > variables_
Definition: SPARQLQuery.h:75
VariableRule & var()
Definition: terms.cpp:91

◆ selectBegin() [2/2]

void knowrob::SPARQLQuery::selectBegin ( std::ostream &  os)
protected

◆ selectEnd() [1/2]

void SPARQLQuery::selectEnd ( std::ostream &  os)
staticprotected

Definition at line 202 of file SPARQLQuery.cpp.

202  {
203  os << "}";
204 }

◆ selectEnd() [2/2]

static void knowrob::SPARQLQuery::selectEnd ( std::ostream &  os)
staticprotected

◆ where() [1/4]

void SPARQLQuery::where ( std::ostream &  os,
const TermPtr term 
)
protected

Definition at line 345 of file SPARQLQuery.cpp.

345  {
346  switch (term->termType()) {
347  case TermType::VARIABLE: {
348  auto var = std::static_pointer_cast<Variable>(term);
349  variables_.insert(var->name());
350  os << "?" << var->name() << " ";
351  break;
352  }
353  case TermType::ATOMIC: {
354  if (term->isIRI()) {
355  iri(os, std::static_pointer_cast<Atomic>(term)->stringForm());
356  break;
357  } else if (term->isBlank()) {
358  os << "_:" << std::static_pointer_cast<Atomic>(term)->stringForm() << " ";
359  break;
360  }
361  [[fallthrough]];
362  }
363  default:
364  if (term->isNumeric() || term->isString()) {
365  auto xsdAtomic = std::static_pointer_cast<XSDAtomic>(term);
366  if (xsdAtomic->xsdType() == XSDType::BOOLEAN) {
367  auto numeric = std::static_pointer_cast<Numeric>(term);
368  os << (numeric->asBoolean() ? "\"true\"" : "\"false\"");
369  } else if (xsdAtomic->isString()) {
370  os << *term;
371  } else {
372  os << '"' << *term << '"';
373  }
374  os << "^^";
375  iri(os, xsdTypeToIRI(xsdAtomic->xsdType()));
376  os << " ";
377  } else if (term->termType() == TermType::ATOMIC) {
378  // handle the case that an IRI was not handed in as an IRIAtom
379  iri(os, std::static_pointer_cast<Atomic>(term)->stringForm());
380  } else {
381  KB_WARN("Invalid term `{}` in SPARQL query.", *term);
382  }
383  break;
384  }
385 }

◆ where() [2/4]

void knowrob::SPARQLQuery::where ( std::ostream &  os,
const TermPtr term 
)
protected

◆ where() [3/4]

bool SPARQLQuery::where ( std::ostream &  os,
const TriplePattern triplePattern 
)
protected

Definition at line 313 of file SPARQLQuery.cpp.

313  {
314  where(os, triplePattern.subjectTerm());
315  where(os, triplePattern.propertyTerm());
316  if (triplePattern.objectTerm()->isVariable() || triplePattern.objectOperator() == FilterType::EQ) {
317  if (!triplePattern.objectTerm()->isVariable() && triplePattern.objectVariable()) {
318  where(os, triplePattern.objectVariable());
319  os << ". ";
320  lastVar_ = triplePattern.objectVariable()->name();
321  return true;
322  } else {
323  where(os, triplePattern.objectTerm());
324  os << ". ";
325  return false;
326  }
327  } else if (triplePattern.objectVariable()) {
328  // value, operator and variable are provided in the pattern
329  lastVar_ = triplePattern.objectVariable()->name();
330  variables_.insert(triplePattern.objectVariable()->name());
331  os << "?" << lastVar_ << " . ";
332  return true;
333  } else {
334  // we need to introduce a temporary variable to handle value expressions like `<(5)` such
335  // that they can be filtered after the match.
336  static const std::string adhocVarPrefix = "v_adhoc";
337  std::stringstream varName_os;
338  varName_os << adhocVarPrefix << (varCounter_++);
339  lastVar_ = varName_os.str();
340  os << "?" << lastVar_ << " . ";
341  return true;
342  }
343 }
auto & propertyTerm() const
Definition: TriplePattern.h:92
auto & subjectTerm() const
Definition: TriplePattern.h:81
auto & objectVariable() const
TermRule & string()
Definition: terms.cpp:63

◆ where() [4/4]

bool knowrob::SPARQLQuery::where ( std::ostream &  os,
const TriplePattern triplePattern 
)
protected

◆ where_with_filter() [1/2]

void SPARQLQuery::where_with_filter ( std::ostream &  os,
const TriplePattern triplePattern 
)
protected

Definition at line 307 of file SPARQLQuery.cpp.

307  {
308  if (where(os, triplePattern)) {
309  filter(os, lastVar_, triplePattern.objectTerm(), triplePattern.objectOperator());
310  }
311 }
void filter(std::ostream &os, std::string_view varName, const TermPtr &term, FilterType operatorType)

◆ where_with_filter() [2/2]

void knowrob::SPARQLQuery::where_with_filter ( std::ostream &  os,
const TriplePattern triplePattern 
)
protected

Member Data Documentation

◆ aliases_

std::map< std::string_view, std::string_view > knowrob::SPARQLQuery::aliases_
protected

Definition at line 74 of file SPARQLQuery.h.

◆ flags_

SPARQLFlags knowrob::SPARQLQuery::flags_
protected

Definition at line 72 of file SPARQLQuery.h.

◆ lastVar_

std::string knowrob::SPARQLQuery::lastVar_
protected

Definition at line 77 of file SPARQLQuery.h.

◆ queryString_

std::string knowrob::SPARQLQuery::queryString_
protected

Definition at line 76 of file SPARQLQuery.h.

◆ varCounter_

uint32_t knowrob::SPARQLQuery::varCounter_
protected

Definition at line 73 of file SPARQLQuery.h.

◆ variables_

std::set< std::string_view > knowrob::SPARQLQuery::variables_
protected

Definition at line 75 of file SPARQLQuery.h.


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