knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
ReifiedTriple.cpp
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 #include "knowrob/storage/ReifiedTriple.h"
7 #include "knowrob/storage/reification.h"
8 #include "knowrob/semweb/rdf.h"
9 #include "knowrob/semweb/Property.h"
10 #include "knowrob/semweb/Perspective.h"
11 
12 using namespace knowrob;
13 
14 ReifiedTriple::ReifiedTriple(const Triple &triple, const VocabularyPtr &vocabulary,
15  const IRIAtomPtr &reifiedName) {
16  auto property = vocabulary->defineProperty(triple.predicate());
17  // map the property to a Relation concept
18  auto relationType = property->reification();
19  // generate a unique individual name
20  name_ = reifiedName ? reifiedName : semweb::Resource::unique_iri(
21  reification::individualPrefix->stringForm(),
22  semweb::Resource::iri_name(relationType->iri()));
23  const auto &name = name_->stringForm();
24  // set origin of the reified triples
25  auto g = triple.graph();
26 
27  // need to avoid copy of TriplePtr, so we reserve space in the vector to fit them exactly
28  uint32_t triple_count = 3;
29  if (triple.perspective()) triple_count += 1;
30  if (triple.isUncertain() || triple.confidence()) triple_count += 1;
31  if (triple.isOccasional()) triple_count += 1;
32  if (triple.confidence()) triple_count += 1;
33  if (triple.begin()) triple_count += 1;
34  if (triple.end()) triple_count += 1;
35  reified_.reserve(triple_count);
36 
37  // create a rdf:type assertion for the relation individual
38  create(name, rdf::type, g)->setObjectIRI(relationType->iri());
39 
40  // set the subject, and object of the reified triple
41  if (triple.isSubjectBlank()) {
43  } else {
45  }
46  if (triple.isObjectBlank()) {
48  } else if (triple.isObjectIRI()) {
50  } else if (triple.xsdType()) {
51  if (triple.xsdType().value() == XSDType::STRING) {
53  } else {
56  }
57  }
58 
59  // set the optional properties of the reified triple
60  if (triple.perspective()) {
61  create(name, reification::hasPerspective, g)->setObjectIRI(triple.perspective().value());
62  }
63  if (triple.isUncertain() || triple.confidence()) {
65  }
66  if (triple.isOccasional()) {
68  }
69  if (triple.confidence()) {
70  create(name, reification::hasConfidence, g)->setDoubleValue(triple.confidence().value());
71  }
72  if (triple.begin()) {
73  create(name, reification::hasBeginTime, g)->setDoubleValue(triple.begin().value());
74  }
75  if (triple.end()) {
76  create(name, reification::hasEndTime, g)->setDoubleValue(triple.end().value());
77  }
78 }
79 
80 Triple *
81 ReifiedTriple::create(std::string_view subject, const AtomPtr &property, const std::optional<std::string_view> &g) {
82  auto &reified = reified_.emplace_back(new TripleView());
83  reified->setSubject(subject);
84  reified->setPredicate(property->stringForm());
85  if (g.has_value()) {
86  reified->setGraph(g.value());
87  }
88  return reified.ptr;
89 }
90 
92  return triple.subject().compare(
93  0,
94  reification::individualPrefix->stringForm().size(),
95  reification::individualPrefix->stringForm()) == 0;
96 }
97 
98 bool ReifiedTriple::isReifiable(const Triple &triple) {
99  bool hasNonEgoPerspective = triple.perspective() && !Perspective::isEgoPerspective(triple.perspective().value());
100  return hasNonEgoPerspective ||
101  triple.isUncertain() ||
102  triple.isOccasional() ||
103  triple.confidence() ||
104  triple.begin() ||
105  triple.end();
106 }
static bool isEgoPerspective(std::string_view iri)
Definition: Perspective.cpp:27
knowrob::IRIAtomPtr name_
Definition: ReifiedTriple.h:68
static bool isPartOfReification(const Triple &triple)
ReifiedTriple(const Triple &triple, const VocabularyPtr &vocabulary, const IRIAtomPtr &reifiedName=nullptr)
Triple * create(std::string_view subject, const AtomPtr &property, const std::optional< std::string_view > &g)
std::vector< TriplePtr > reified_
Definition: ReifiedTriple.h:67
std::string generatedString_
Definition: ReifiedTriple.h:69
static bool isReifiable(const Triple &triple)
virtual std::string_view valueAsString() const =0
auto xsdType() const
Definition: Triple.h:64
virtual std::optional< std::string_view > perspective() const =0
bool isUncertain() const
Definition: Triple.h:267
virtual void setDoubleValue(double v)=0
virtual std::optional< std::string_view > graph() const =0
auto end() const
Definition: Triple.h:277
virtual std::string_view subject() const =0
auto confidence() const
Definition: Triple.h:282
auto begin() const
Definition: Triple.h:272
bool isSubjectBlank() const
Definition: Triple.h:44
virtual std::string_view predicate() const =0
std::string createStringValue() const
Definition: Triple.cpp:71
virtual void setObjectIRI(std::string_view object)=0
bool isObjectIRI() const
Definition: Triple.h:54
virtual void setObjectBlank(std::string_view str)=0
bool isOccasional() const
Definition: Triple.h:262
bool isObjectBlank() const
Definition: Triple.h:49
virtual void setBooleanValue(bool v)=0
void setXSDValue(std::string_view v, XSDType type)
Definition: Triple.cpp:32
static std::string_view iri_name(std::string_view iri)
Definition: Resource.cpp:57
static IRIAtomPtr unique_iri(std::string_view ns, std::string_view name)
Definition: Resource.cpp:38
const IRIAtomPtr type
Definition: rdf.h:15
const IRIAtomPtr hasSubject
Definition: reification.h:15
const IRIAtomPtr isUncertain
Definition: reification.h:18
const IRIAtomPtr hasConfidence
Definition: reification.h:21
const AtomPtr individualPrefix
Definition: reification.h:13
const IRIAtomPtr isOccasional
Definition: reification.h:19
const IRIAtomPtr hasEndTime
Definition: reification.h:23
const IRIAtomPtr hasLiteral
Definition: reification.h:17
const IRIAtomPtr hasBeginTime
Definition: reification.h:22
const IRIAtomPtr hasPerspective
Definition: reification.h:20
const IRIAtomPtr hasObject
Definition: reification.h:16
std::shared_ptr< Atom > AtomPtr
Definition: Atom.h:69
std::shared_ptr< IRIAtom > IRIAtomPtr
Definition: IRIAtom.h:57
TripleTemplate< std::string_view > TripleView
Definition: Triple.h:581
std::shared_ptr< Vocabulary > VocabularyPtr
Definition: Vocabulary.h:233