6 #include "knowrob/storage/mongo/MongoTriple.h"
15 : document_(createDocument(vocabulary, tripleData, fallbackOrigin, isTaxonomic)) {
18 static inline void appendXSDLiteral(bson_t *tripleDoc,
const Triple &tripleData) {
22 BSON_APPEND_UTF8(tripleDoc,
"o", tripleData.
valueAsString().data());
25 BSON_APPEND_DOUBLE(tripleDoc,
"o", tripleData.
valueAsDouble());
28 BSON_APPEND_DOUBLE(tripleDoc,
"o", tripleData.
valueAsFloat());
31 BSON_APPEND_INT64(tripleDoc,
"o", tripleData.
valueAsLong());
35 BSON_APPEND_INT32(tripleDoc,
"o", tripleData.
valueAsInt());
38 BSON_APPEND_INT32(tripleDoc,
"o", tripleData.
valueAsShort());
63 uint32_t arrIndex = 0;
64 auto counterPtr = &arrIndex;
66 bson_t *tripleDoc = bson_new();
67 BSON_APPEND_UTF8(tripleDoc,
"s", tripleData.
subject().data());
68 BSON_APPEND_UTF8(tripleDoc,
"p", tripleData.
predicate().data());
73 BSON_APPEND_UTF8(tripleDoc,
"o", objectIRI.data());
75 BSON_APPEND_ARRAY_BEGIN(tripleDoc,
"o*", &parentsArray);
76 auto parentsPtr = &parentsArray;
77 if (vocabulary->isDefinedProperty(objectIRI)) {
78 vocabulary->getDefinedProperty(objectIRI)->forallParents(
79 [parentsPtr, counterPtr](
const auto &parent) {
80 auto counterKey = std::to_string((*counterPtr)++);
81 BSON_APPEND_UTF8(parentsPtr, counterKey.c_str(), parent.iri().data());
83 }
else if (vocabulary->isDefinedClass(objectIRI)) {
85 vocabulary->getDefinedClass(objectIRI)->forallParents(
86 [parentsPtr, counterPtr](
const auto &parent) {
87 auto counterKey = std::to_string((*counterPtr)++);
88 BSON_APPEND_UTF8(parentsPtr, counterKey.c_str(), parent.iri().data());
91 BSON_APPEND_UTF8(&parentsArray,
"0", objectIRI.data());
93 bson_append_array_end(tripleDoc, &parentsArray);
95 appendXSDLiteral(tripleDoc, tripleData);
99 BSON_APPEND_UTF8(tripleDoc,
"o", tripleData.
valueAsString().data());
101 appendXSDLiteral(tripleDoc, tripleData);
104 BSON_APPEND_ARRAY_BEGIN(tripleDoc,
"p*", &parentsArray);
105 auto parentsPtr = &parentsArray;
106 vocabulary->defineProperty(tripleData.
predicate())->forallParents(
107 [parentsPtr, counterPtr](
const auto &parent) {
108 auto counterKey = std::to_string((*counterPtr)++);
109 BSON_APPEND_UTF8(parentsPtr, counterKey.c_str(), parent.iri().data());
111 bson_append_array_end(tripleDoc, &parentsArray);
114 if (tripleData.
graph()) {
115 BSON_APPEND_UTF8(tripleDoc,
"graph", tripleData.
graph().value().data());
117 BSON_APPEND_UTF8(tripleDoc,
"graph", fallbackOrigin.c_str());
121 BSON_APPEND_UTF8(tripleDoc,
"agent", tripleData.
perspective().value().data());
125 BSON_APPEND_DOUBLE(tripleDoc,
"confidence", tripleData.
confidence().value());
132 BSON_APPEND_BOOL(tripleDoc,
"uncertain",
true);
137 BSON_APPEND_BOOL(tripleDoc,
"occasional",
true);
140 if (tripleData.
begin().has_value() || tripleData.
end().has_value()) {
141 bson_t scopeDoc, timeDoc;
142 BSON_APPEND_DOCUMENT_BEGIN(tripleDoc,
"scope", &scopeDoc);
143 BSON_APPEND_DOCUMENT_BEGIN(&scopeDoc,
"time", &timeDoc);
144 if (tripleData.
begin().has_value()) BSON_APPEND_DOUBLE(&timeDoc,
"since", tripleData.
begin().value());
145 if (tripleData.
end().has_value()) BSON_APPEND_DOUBLE(&timeDoc,
"until", tripleData.
end().value());
146 bson_append_document_end(&scopeDoc, &timeDoc);
147 bson_append_document_end(tripleDoc, &scopeDoc);
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 std::optional< std::string_view > graph() const =0
virtual double valueAsDouble() const =0
virtual std::string_view subject() const =0
virtual float valueAsFloat() const =0
virtual std::string_view predicate() const =0
virtual long valueAsLong() const =0
virtual unsigned short valueAsUnsignedShort() const =0
virtual unsigned long valueAsUnsignedLong() const =0
bool isOccasional() const
bool isObjectBlank() const
virtual int valueAsInt() const =0
virtual bool valueAsBoolean() const =0
MongoTriple(const VocabularyPtr &vocabulary, const Triple &tripleData, const std::string &fallbackOrigin, bool isTaxonomic)
static bson_t * createDocument(const VocabularyPtr &vocabulary, const Triple &tripleData, const std::string &fallbackOrigin, bool isTaxonomic)
std::shared_ptr< Vocabulary > VocabularyPtr