6 #include "knowrob/semweb/TriplePattern.h"
7 #include "knowrob/Logger.h"
8 #include "knowrob/queries/QueryError.h"
9 #include "knowrob/terms/Atomic.h"
10 #include "knowrob/terms/Numeric.h"
11 #include "knowrob/terms/Atom.h"
12 #include "knowrob/terms/IRIAtom.h"
13 #include "knowrob/terms/Blank.h"
14 #include "knowrob/integration/python/utils.h"
15 #include "knowrob/semweb/rdf.h"
16 #include "knowrob/integration/python/converter/vector.h"
17 #include <boost/python/suite/indexing/vector_indexing_suite.hpp>
41 subjectTerm_(predicate_->arguments()[0]),
42 propertyTerm_(predicate_->arguments()[1]),
43 objectTerm_(predicate_->arguments()[2]),
49 if (triple.
begin().has_value()) {
52 if (triple.
end().has_value()) {
53 endTerm_ = std::make_shared<Double>(triple.
end().value());
71 subjectTerm_(predicate_->arguments()[0]),
72 propertyTerm_(predicate_->arguments()[1]),
73 objectTerm_(predicate_->arguments()[2]),
91 if (frame.
begin.has_value()) {
94 if (frame.
end.has_value()) {
95 endTerm_ = std::make_shared<Double>(frame.
end.value());
113 frame.
begin = std::static_pointer_cast<Numeric>(
beginTerm().grounded())->asDouble();
115 if (
endTerm().has_grounding() &&
endTerm().grounded()->isNumeric()) {
116 frame.
end = std::static_pointer_cast<Numeric>(
endTerm().grounded())->asDouble();
128 frame.
graph = std::static_pointer_cast<Atom>(
graphTerm().grounded());
136 static std::map<std::string, AtomPtr, std::less<>> graphTerms;
137 if (!graphName.empty()) {
138 auto it = graphTerms.find(graphName);
139 if (it == graphTerms.end()) {
141 graphTerms[graphName.data()] =
graphTerm;
151 return std::make_shared<Predicate>(
"triple", std::vector<TermPtr>({s, p, o}));
162 throw QueryError(
"RDF literal can only be constructed from 2-ary predicates but {} is not.", *
predicate);
179 return std::make_shared<Predicate>(
"triple", std::vector<TermPtr>({s, p, o}));
183 std::vector<VariablePtr> vars;
197 if (t && t->termType() ==
TermType::VARIABLE) vars.push_back(std::static_pointer_cast<Variable>(t));
207 static bool filterString(std::string_view value,
const TriplePattern &query) {
209 if (!q_term->isAtomic())
return false;
210 auto q_atomic = std::static_pointer_cast<Atomic>(q_term);
213 return value == q_atomic->stringForm();
215 return value != q_atomic->stringForm();
217 return value <= q_atomic->stringForm();
219 return value >= q_atomic->stringForm();
221 return value > q_atomic->stringForm();
223 return value < q_atomic->stringForm();
228 template<
typename NumType>
255 if (!q_term->isNumeric())
return false;
256 auto q_numeric = std::static_pointer_cast<Numeric>(q_term);
257 switch (triple.
xsdType().value()) {
288 bool hasMissingSPO =
false;
292 bindings->get(std::static_pointer_cast<Variable>(
subjectTerm_)->name()) :
295 hasMissingSPO =
true;
296 }
else if (s->isBlank()) {
297 triple.
setSubjectBlank(std::static_pointer_cast<Blank>(s)->stringForm());
299 triple.
setSubject(std::static_pointer_cast<Atomic>(s)->stringForm());
301 hasMissingSPO =
true;
304 hasMissingSPO =
true;
309 bindings->get(std::static_pointer_cast<Variable>(
propertyTerm_)->name()) :
312 triple.
setPredicate(std::static_pointer_cast<Atomic>(p)->stringForm());
314 hasMissingSPO =
true;
317 hasMissingSPO =
true;
322 bindings->get(std::static_pointer_cast<Variable>(
objectTerm_)->name()) :
324 bindings->get(std::static_pointer_cast<Variable>(
objectVariable_)->name()) :
328 hasMissingSPO =
true;
329 }
else if (o->isNumeric()) {
330 auto numeric = std::static_pointer_cast<Numeric>(o);
331 triple.
setXSDValue(numeric->stringForm(), numeric->xsdType());
333 auto atom = std::static_pointer_cast<Atomic>(o);
336 }
else if (
atom->isBlank()) {
342 hasMissingSPO =
true;
345 hasMissingSPO =
true;
350 bindings->get(std::static_pointer_cast<Variable>(*graphTerm_)->name()) :
353 triple.
setGraph(std::static_pointer_cast<Atomic>(g)->stringForm());
358 bindings->get(std::static_pointer_cast<Variable>(*perspectiveTerm_)->name()) :
361 triple.
setPerspective(std::static_pointer_cast<Atomic>(a)->stringForm());
366 bindings->get(std::static_pointer_cast<Variable>(*confidenceTerm_)->name()) :
368 if (c && c->isNumeric()) {
369 triple.
setConfidence(std::static_pointer_cast<Numeric>(c)->asDouble());
374 bindings->get(std::static_pointer_cast<Variable>(*beginTerm_)->name()) :
376 if (b && b->isNumeric()) {
377 triple.
setBegin(std::static_pointer_cast<Numeric>(b)->asDouble());
382 bindings->get(std::static_pointer_cast<Variable>(*endTerm_)->name()) :
384 if (e && e->isNumeric()) {
385 triple.
setEnd(std::static_pointer_cast<Numeric>(e)->asDouble());
390 bindings->get(std::static_pointer_cast<Variable>(*isOccasional_)->name()) :
392 if (o && o->isNumeric()) {
393 triple.
setIsOccasional(std::static_pointer_cast<Numeric>(o)->asBoolean());
398 bindings->get(std::static_pointer_cast<Variable>(*isUncertain_)->name()) :
400 if (u && u->isNumeric()) {
401 triple.
setIsUncertain(std::static_pointer_cast<Numeric>(u)->asBoolean());
405 return !hasMissingSPO;
409 for (
auto d:
data_) {
417 data_.push_back(data);
420 if (!q->instantiateInto(*data->ptr)) {
426 return [
this, i = std::size_t(0)]()
mutable ->
const TriplePtr * {
433 return [
this, i = std::size_t(0)]()
mutable ->
TriplePtr * {
441 bool hasChanges =
false;
444 if (subject != pat->subjectTerm()) hasChanges =
true;
446 auto property =
applyBindings(pat->propertyTerm(), bindings);
447 if (property != pat->propertyTerm()) hasChanges =
true;
450 if (
object != pat->objectTerm()) hasChanges =
true;
451 else if (pat->objectVariable()) {
452 auto actualObject =
applyBindings(pat->objectVariable(), bindings);
453 if (actualObject && actualObject != pat->objectTerm()) {
454 object = actualObject;
459 auto graph = pat->graphTerm() ?
applyBindings(*pat->graphTerm(), bindings) :
nullptr;
460 if (graph && graph != *pat->graphTerm()) hasChanges =
true;
462 auto agent = pat->perspectiveTerm() ?
applyBindings(*pat->perspectiveTerm(), bindings) :
nullptr;
463 if (agent && agent != *pat->perspectiveTerm()) hasChanges =
true;
465 auto confidence = pat->confidenceTerm() ?
applyBindings(*pat->confidenceTerm(), bindings) :
nullptr;
466 if (confidence && confidence != *pat->confidenceTerm()) hasChanges =
true;
468 auto begin = pat->beginTerm() ?
applyBindings(*pat->beginTerm(), bindings) :
nullptr;
469 if (begin && begin != *pat->beginTerm()) hasChanges =
true;
471 auto end = pat->endTerm() ?
applyBindings(*pat->endTerm(), bindings) :
nullptr;
472 if (end && end != *pat->endTerm()) hasChanges =
true;
474 auto occasional = pat->isOccasionalTerm() ?
applyBindings(*pat->isOccasionalTerm(), bindings) :
nullptr;
475 if (occasional && occasional != *pat->isOccasionalTerm()) hasChanges =
true;
477 auto uncertain = pat->isUncertainTerm() ?
applyBindings(*pat->isUncertainTerm(), bindings) :
nullptr;
478 if (uncertain && uncertain != *pat->isUncertainTerm()) hasChanges =
true;
480 if (!hasChanges)
return pat;
482 auto patInstance = std::make_shared<TriplePattern>(
483 subject, property,
object, pat->isNegated());
484 patInstance->setObjectOperator(pat->objectOperator());
500 class_<TriplePattern, std::shared_ptr<TriplePattern>, bases<FirstOrderLiteral>>
501 (
"TriplePattern", init<const Triple &, bool>())
502 .def(init<const Triple &>())
530 typedef std::vector<std::shared_ptr<TriplePattern>> GoalList;
532 boost::python::class_<GoalList>(
"GoalList").def(boost::python::vector_indexing_suite<GoalList, true>());
bool filterNumeric(const NumType &a, const NumType &b, FilterType op)
static std::shared_ptr< knowrob::Atom > Tabled(std::string_view stringForm)
static std::shared_ptr< Atomic > makeTripleValue(const Triple &triple)
static std::shared_ptr< Blank > Tabled(std::string_view stringForm)
const auto & predicate() const
static std::shared_ptr< IRIAtom > Tabled(std::string_view stringForm)
std::function< TriplePtr *()> MutableGenerator
static std::shared_ptr< Numeric > trueAtom()
static std::shared_ptr< Perspective > get(std::string_view iri)
std::function< const TriplePtr *()> ConstGenerator
virtual std::string_view valueAsString() const =0
virtual short valueAsShort() const =0
virtual std::optional< std::string_view > perspective() const =0
virtual unsigned int valueAsUnsignedInt() const =0
virtual void setSubject(std::string_view subject)=0
virtual std::optional< std::string_view > graph() const =0
virtual double valueAsDouble() const =0
virtual void setGraph(std::string_view graph)=0
virtual void setStringValue(std::string_view v)=0
void setConfidence(double confidence)
virtual std::string_view subject() const =0
virtual float valueAsFloat() const =0
virtual void setPerspective(std::string_view perspective)=0
void setIsUncertain(bool isUncertain)
void setIsOccasional(bool isOccasional)
bool isSubjectBlank() const
virtual std::string_view predicate() const =0
void setBegin(double begin)
virtual long valueAsLong() const =0
virtual void setObjectIRI(std::string_view object)=0
virtual unsigned short valueAsUnsignedShort() const =0
virtual void setObjectBlank(std::string_view str)=0
virtual unsigned long valueAsUnsignedLong() const =0
virtual void setSubjectBlank(std::string_view str)=0
virtual void setPredicate(std::string_view predicate)=0
bool isOccasional() const
bool isObjectBlank() const
virtual int valueAsInt() const =0
virtual bool valueAsBoolean() const =0
void setXSDValue(std::string_view v, XSDType type)
MutableGenerator generator() override
std::vector< TriplePatternPtr > statements_
ConstGenerator cgenerator() const override
~TriplePatternContainer()
void push_back(const TriplePatternPtr &q)
std::vector< TriplePtr * > data_
auto objectOperator() const
groundable< Double > beginTerm_
void setEndTerm(const groundable< Double > &endTerm)
groundable< Double > endTerm_
VariablePtr objectVariable_
std::vector< VariablePtr > getVariables(bool includeObjectVar=true) const
static std::shared_ptr< Atom > getGraphTerm(const std::string_view &graphName)
auto & propertyTerm() const
void setConfidenceTerm(const groundable< Double > &confidenceTerm)
void getTripleFrame(GraphSelector &frame) const
void setIsOccasionalTerm(const groundable< Numeric > &isOccasional)
void setBeginTerm(const groundable< Double > &beginTerm)
void setPerspectiveTerm(const groundable< Atom > &perspectiveTerm)
void setObjectOperator(FilterType objectOperator)
groundable< Atom > graphTerm_
bool filter(const Triple &triple) const
auto & subjectTerm() const
bool instantiateInto(Triple &triple, const std::shared_ptr< const Bindings > &bindings=Bindings::emptyBindings()) const
void setGraphName(const std::string_view &graphName)
void setIsUncertainTerm(const groundable< Numeric > &isUncertain)
auto & isOccasionalTerm() const
auto & isUncertainTerm() const
static std::shared_ptr< Predicate > getRDFPredicate(const PredicatePtr &predicate)
groundable< Numeric > isOccasional_
auto & objectTerm() const
void setTripleFrame(const GraphSelector &frame)
groundable< Numeric > isUncertain_
uint32_t numVariables() const override
TriplePattern(const Triple &triple, bool isNegated=false)
groundable< Atom > perspectiveTerm_
groundable< Double > confidenceTerm_
auto & perspectiveTerm() const
auto & confidenceTerm() const
void createType< TriplePattern >()
std::shared_ptr< TriplePattern > TriplePatternPtr
std::shared_ptr< Term > TermPtr
FilterType inverseFilterType(FilterType op)
std::shared_ptr< Predicate > PredicatePtr
TripleTemplate< std::string_view > TripleView
FirstOrderLiteralPtr applyBindings(const FirstOrderLiteralPtr &lit, const Bindings &bindings)
std::optional< double > confidence
std::optional< double > end
PerspectivePtr perspective
std::optional< double > begin