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

#include <MongoTriple.h>

Collaboration diagram for knowrob::mongo::MongoTriple:

Public Member Functions

 MongoTriple (const VocabularyPtr &vocabulary, const Triple &tripleData, const std::string &fallbackOrigin, bool isTaxonomic)
 
auto & document () const
 
 MongoTriple (const VocabularyPtr &vocabulary, const Triple &tripleData, const std::string &fallbackOrigin, bool isTaxonomic)
 
auto & document () const
 

Static Protected Member Functions

static bson_t * createDocument (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)
 

Protected Attributes

Document document_
 

Detailed Description

Transforms a triple into a MongoDB document.

Definition at line 17 of file MongoTriple.h.

Constructor & Destructor Documentation

◆ MongoTriple() [1/2]

MongoTriple::MongoTriple ( const VocabularyPtr vocabulary,
const Triple tripleData,
const std::string &  fallbackOrigin,
bool  isTaxonomic 
)

Definition at line 11 of file MongoTriple.cpp.

15  : document_(createDocument(vocabulary, tripleData, fallbackOrigin, isTaxonomic)) {
16 }
static bson_t * createDocument(const VocabularyPtr &vocabulary, const Triple &tripleData, const std::string &fallbackOrigin, bool isTaxonomic)
Definition: MongoTriple.cpp:57

◆ MongoTriple() [2/2]

knowrob::mongo::MongoTriple::MongoTriple ( const VocabularyPtr vocabulary,
const Triple tripleData,
const std::string &  fallbackOrigin,
bool  isTaxonomic 
)

Member Function Documentation

◆ createDocument() [1/2]

bson_t * MongoTriple::createDocument ( const VocabularyPtr vocabulary,
const Triple tripleData,
const std::string &  fallbackOrigin,
bool  isTaxonomic 
)
staticprotected

Definition at line 57 of file MongoTriple.cpp.

61  {
62  bson_t parentsArray;
63  uint32_t arrIndex = 0;
64  auto counterPtr = &arrIndex;
65 
66  bson_t *tripleDoc = bson_new();
67  BSON_APPEND_UTF8(tripleDoc, "s", tripleData.subject().data());
68  BSON_APPEND_UTF8(tripleDoc, "p", tripleData.predicate().data());
69 
70  if (isTaxonomic) {
71  if (tripleData.isObjectIRI() || tripleData.isObjectBlank()) {
72  auto objectIRI = tripleData.valueAsString();
73  BSON_APPEND_UTF8(tripleDoc, "o", objectIRI.data());
74  // also create a field "o*" with the parents of the object
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());
82  });
83  } else if (vocabulary->isDefinedClass(objectIRI)) {
84  // read parents array
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());
89  });
90  } else {
91  BSON_APPEND_UTF8(&parentsArray, "0", objectIRI.data());
92  }
93  bson_append_array_end(tripleDoc, &parentsArray);
94  } else {
95  appendXSDLiteral(tripleDoc, tripleData);
96  }
97  } else {
98  if (tripleData.isObjectIRI() || tripleData.isObjectBlank()) {
99  BSON_APPEND_UTF8(tripleDoc, "o", tripleData.valueAsString().data());
100  } else {
101  appendXSDLiteral(tripleDoc, tripleData);
102  }
103  // read parents array
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());
110  });
111  bson_append_array_end(tripleDoc, &parentsArray);
112  }
113 
114  if (tripleData.graph()) {
115  BSON_APPEND_UTF8(tripleDoc, "graph", tripleData.graph().value().data());
116  } else {
117  BSON_APPEND_UTF8(tripleDoc, "graph", fallbackOrigin.c_str());
118  }
119 
120  if (tripleData.perspective())
121  BSON_APPEND_UTF8(tripleDoc, "agent", tripleData.perspective().value().data());
122 
123  bool isBelief;
124  if (tripleData.confidence().has_value()) {
125  BSON_APPEND_DOUBLE(tripleDoc, "confidence", tripleData.confidence().value());
126  isBelief = true;
127  } else {
128  isBelief = tripleData.isUncertain();
129  }
130  if (isBelief) {
131  // flag the statement as "uncertain"
132  BSON_APPEND_BOOL(tripleDoc, "uncertain", true);
133  }
134 
135  if (tripleData.isOccasional()) {
136  // flag the statement as "occasional", meaning it is only known that it was true at some past instants
137  BSON_APPEND_BOOL(tripleDoc, "occasional", true);
138  }
139 
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);
148  }
149 
150  return tripleDoc;
151 }
virtual std::string_view valueAsString() const =0
virtual std::optional< std::string_view > perspective() const =0
bool isUncertain() const
Definition: Triple.h:267
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
virtual std::string_view predicate() const =0
bool isObjectIRI() const
Definition: Triple.h:54
bool isOccasional() const
Definition: Triple.h:262
bool isObjectBlank() const
Definition: Triple.h:49

◆ createDocument() [2/2]

static bson_t* knowrob::mongo::MongoTriple::createDocument ( const VocabularyPtr vocabulary,
const Triple tripleData,
const std::string &  fallbackOrigin,
bool  isTaxonomic 
)
staticprotected

◆ document() [1/2]

auto& knowrob::mongo::MongoTriple::document ( ) const
inline
Returns
the document.

Definition at line 27 of file MongoTriple.h.

27 { return document_; }

◆ document() [2/2]

auto& knowrob::mongo::MongoTriple::document ( ) const
inline
Returns
the document.

Definition at line 27 of file MongoTriple.h.

27 { return document_; }

Member Data Documentation

◆ document_

Document knowrob::mongo::MongoTriple::document_
protected

Definition at line 30 of file MongoTriple.h.


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