knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
MongoTriple.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/mongo/MongoTriple.h"
7 
8 using namespace knowrob::mongo;
9 using namespace knowrob;
10 
12  const Triple &tripleData,
13  const std::string &fallbackOrigin,
14  bool isTaxonomic)
15  : document_(createDocument(vocabulary, tripleData, fallbackOrigin, isTaxonomic)) {
16 }
17 
18 static inline void appendXSDLiteral(bson_t *tripleDoc, const Triple &tripleData) {
19  auto xsdType = tripleData.xsdType().has_value() ? tripleData.xsdType().value() : XSDType::STRING;
20  switch (xsdType) {
21  case XSDType::STRING:
22  BSON_APPEND_UTF8(tripleDoc, "o", tripleData.valueAsString().data());
23  break;
24  case XSDType::DOUBLE:
25  BSON_APPEND_DOUBLE(tripleDoc, "o", tripleData.valueAsDouble());
26  break;
27  case XSDType::FLOAT:
28  BSON_APPEND_DOUBLE(tripleDoc, "o", tripleData.valueAsFloat());
29  break;
30  case XSDType::LONG:
31  BSON_APPEND_INT64(tripleDoc, "o", tripleData.valueAsLong());
32  break;
34  case XSDType::INTEGER:
35  BSON_APPEND_INT32(tripleDoc, "o", tripleData.valueAsInt());
36  break;
37  case XSDType::SHORT:
38  BSON_APPEND_INT32(tripleDoc, "o", tripleData.valueAsShort());
39  break;
40  case XSDType::BOOLEAN:
41  BSON_APPEND_BOOL(tripleDoc, "o", tripleData.valueAsBoolean());
42  break;
44  BSON_APPEND_INT32(tripleDoc, "o", tripleData.valueAsUnsignedInt());
45  break;
47  BSON_APPEND_INT32(tripleDoc, "o", tripleData.valueAsUnsignedShort());
48  break;
50  BSON_APPEND_INT64(tripleDoc, "o", tripleData.valueAsUnsignedLong());
51  break;
52  case XSDType::LAST:
53  break;
54  }
55 }
56 
58  const VocabularyPtr &vocabulary,
59  const Triple &tripleData,
60  const std::string &fallbackOrigin,
61  bool isTaxonomic) {
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
auto xsdType() const
Definition: Triple.h:64
virtual short valueAsShort() const =0
virtual std::optional< std::string_view > perspective() const =0
bool isUncertain() const
Definition: Triple.h:267
virtual unsigned int valueAsUnsignedInt() const =0
virtual std::optional< std::string_view > graph() const =0
virtual double valueAsDouble() const =0
auto end() const
Definition: Triple.h:277
virtual std::string_view subject() const =0
virtual float valueAsFloat() const =0
auto confidence() const
Definition: Triple.h:282
auto begin() const
Definition: Triple.h:272
virtual std::string_view predicate() const =0
virtual long valueAsLong() const =0
bool isObjectIRI() const
Definition: Triple.h:54
virtual unsigned short valueAsUnsignedShort() const =0
virtual unsigned long valueAsUnsignedLong() const =0
bool isOccasional() const
Definition: Triple.h:262
bool isObjectBlank() const
Definition: Triple.h:49
virtual int valueAsInt() const =0
virtual bool valueAsBoolean() const =0
MongoTriple(const VocabularyPtr &vocabulary, const Triple &tripleData, const std::string &fallbackOrigin, bool isTaxonomic)
Definition: MongoTriple.cpp:11
static bson_t * createDocument(const VocabularyPtr &vocabulary, const Triple &tripleData, const std::string &fallbackOrigin, bool isTaxonomic)
Definition: MongoTriple.cpp:57
TermRule & string()
Definition: terms.cpp:63
std::shared_ptr< Vocabulary > VocabularyPtr
Definition: Vocabulary.h:233