8 #include "knowrob/storage/ReifiedQuery.h"
9 #include "knowrob/semweb/rdf.h"
10 #include "knowrob/storage/reification.h"
11 #include "knowrob/semweb/Property.h"
12 #include "knowrob/queries/QueryError.h"
18 vocabulary_(std::move(vocabulary)),
20 withFullFrame_(withFullFrame) {
26 vocabulary_(std::move(vocabulary)),
28 withFullFrame_(withFullFrame) {
33 switch (nonReified->termType()) {
50 const std::shared_ptr<GraphConnective> &originalConnective) {
51 for (
auto &t: originalConnective->terms()) {
52 switch (t->termType()) {
54 reifiedConnective->addMember(
reifyPattern(std::static_pointer_cast<GraphPattern>(t)));
57 reifiedConnective->addMember(
reifyUnion(std::static_pointer_cast<GraphUnion>(t)));
60 reifiedConnective->addMember(
reifySequence(std::static_pointer_cast<GraphSequence>(t)));
63 reifiedConnective->addMember(t);
70 auto reified = std::make_shared<GraphUnion>();
75 std::shared_ptr<GraphSequence>
77 auto reified = std::make_shared<GraphSequence>();
86 auto graphUnion = std::make_shared<GraphUnion>();
87 graphUnion->addMember(nonReified);
98 const std::shared_ptr<GraphSequence> &sequence,
103 auto reified = std::make_shared<TriplePattern>(s, p, o);
105 reified->setGraphName(g.
grounded()->stringForm());
107 sequence->addPattern(reified);
112 static auto fullyConfident = std::make_shared<Double>(1.0);
114 static auto b_var = std::make_shared<Variable>(
"_reified_b");
115 static auto e_var = std::make_shared<Variable>(
"_reified_e");
118 throw QueryError(
"non-reified triple does not have a property term");
120 auto seq = std::make_shared<GraphSequence>();
121 auto p_atom = std::static_pointer_cast<Atom>(nonReified.
propertyTerm());
122 auto property =
vocabulary_->defineProperty(p_atom->stringForm());
124 auto relationType =
property->reification();
127 auto name = std::make_shared<Variable>(
"_reified" + std::to_string(
varCounter_++));
138 std::shared_ptr<TriplePattern> objectQuery;
139 if (property->isObjectProperty()) {
147 bool includeOnlyCertain;
151 x->setIsOptional(
true);
153 includeOnlyCertain =
false;
156 x->setIsOptional(
true);
157 includeOnlyCertain =
false;
160 x->setIsOptional(
true);
161 includeOnlyCertain =
true;
167 x->setIsOptional(
true);
170 x->setIsOptional(
true);
171 }
else if (includeOnlyCertain) {
174 x->setIsOptional(
true);
180 x->setIsOptional(
true);
186 x->setIsOptional(
true);
189 x->setIsOptional(
true);
192 bool includeOccasional;
196 x->setIsOptional(
true);
198 includeOccasional =
true;
201 x->setIsOptional(
true);
202 includeOccasional =
true;
205 x->setIsOptional(
true);
206 includeOccasional =
false;
209 if (nonReified.
beginTerm().has_grounding()) {
210 if (includeOccasional) {
213 x->setIsOptional(
true);
217 x->setIsOptional(
true);
219 }
else if (nonReified.
beginTerm().has_variable()) {
221 x->setIsOptional(
true);
224 x->setIsNegated(
true);
227 if (nonReified.
endTerm().has_grounding()) {
228 if (includeOccasional) {
231 x->setIsOptional(
true);
235 x->setIsOptional(
true);
237 }
else if (nonReified.
endTerm().has_variable()) {
239 x->setIsOptional(
true);
242 x->setIsNegated(
true);
249 switch (
term->termType()) {
277 bool includeOriginal =
true;
278 bool includeReified =
false;
282 includeReified =
true;
286 includeReified =
true;
293 includeOriginal =
false;
294 includeReified =
true;
297 includeReified =
true;
303 if (occasional->asBoolean()) {
304 includeReified =
true;
305 includeOriginal =
false;
308 includeReified =
true;
313 includeReified =
true;
std::shared_ptr< TriplePattern > addPattern(const std::shared_ptr< GraphSequence > &sequence, const TermPtr &s, const TermPtr &p, const TermPtr &o, const groundable< Atom > &g)
std::shared_ptr< GraphTerm > term_
static std::shared_ptr< Numeric > trueAtom()
static std::shared_ptr< Numeric > falseAtom()
static std::shared_ptr< Perspective > getEgoPerspective()
static bool isEgoPerspective(std::string_view iri)
static int getReificationFlags(const TriplePattern &q)
ReifiedQuery(const std::shared_ptr< GraphQuery > &nonReified, VocabularyPtr vocabulary, bool withFullFrame=false)
std::shared_ptr< GraphTerm > reifiedPatternSequence(const TriplePattern &pattern)
void setNonReified(const std::shared_ptr< GraphTerm > &nonReified)
void reifyConnective(const std::shared_ptr< GraphConnective > &reifiedConnective, const std::shared_ptr< GraphConnective > &originalConnective)
static bool hasReifiablePattern(const std::shared_ptr< GraphQuery > &nonReified)
VocabularyPtr vocabulary_
std::shared_ptr< GraphTerm > reifyPattern(const std::shared_ptr< GraphPattern > &nonReified)
std::shared_ptr< GraphUnion > reifyUnion(const std::shared_ptr< GraphUnion > &graphUnion)
std::shared_ptr< GraphSequence > reifySequence(const std::shared_ptr< GraphSequence > &graphSequence)
auto objectOperator() const
auto & propertyTerm() const
auto & subjectTerm() const
auto & isOccasionalTerm() const
auto & isUncertainTerm() const
auto & objectTerm() const
auto & perspectiveTerm() const
auto & confidenceTerm() const
bool has_grounding() const
const IRIAtomPtr hasSubject
const IRIAtomPtr isUncertain
const IRIAtomPtr hasConfidence
const IRIAtomPtr isOccasional
const IRIAtomPtr hasEndTime
const IRIAtomPtr hasLiteral
const IRIAtomPtr hasBeginTime
const IRIAtomPtr hasPerspective
const IRIAtomPtr hasObject
std::shared_ptr< Term > TermPtr
std::shared_ptr< Vocabulary > VocabularyPtr