knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
TripleCursor.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 <boost/algorithm/string.hpp>
7 #include "knowrob/Logger.h"
8 #include "knowrob/storage/mongo/TripleCursor.h"
9 #include "knowrob/terms/RDFNode.h"
10 #include "knowrob/storage/mongo/bson-helper.h"
11 
12 using namespace knowrob::mongo;
13 
14 TripleCursor::TripleCursor(const std::shared_ptr<Collection> &collection)
15  : Cursor(collection),
16  tripleDocument_(nullptr),
17  tripleIter_() {
18 }
19 
20 bool TripleCursor::nextTriple(Triple &tripleData) //NOLINT
21 {
22  const bson_oid_t *tripleOID = nullptr;
23  return nextTriple(tripleData, &tripleOID);
24 }
25 
26 bool TripleCursor::nextTriple(Triple &tripleData, const bson_oid_t **tripleOID) //NOLINT
27 {
28  if (next(&tripleDocument_) &&
29  bson_iter_init(&tripleIter_, tripleDocument_)) {
30  tripleData.reset();
31 
32  while (bson_iter_next(&tripleIter_)) {
33  std::string_view key = bson_iter_key(&tripleIter_);
34  if (key == "_id") {
35  *tripleOID = bson_iter_oid(&tripleIter_);
36  } else if (key == "s") {
37  tripleData.setSubject(bson_iter_utf8(&tripleIter_, nullptr));
38  } else if (key == "p") {
39  tripleData.setPredicate(bson_iter_utf8(&tripleIter_, nullptr));
40  } else if (key == "graph") {
41  tripleData.setGraph(bson_iter_utf8(&tripleIter_, nullptr));
42  } else if (key == "agent") {
43  tripleData.setPerspective(bson_iter_utf8(&tripleIter_, nullptr));
44  } else if (key == "uncertain") {
45  tripleData.setIsUncertain(bson_iter_bool(&tripleIter_));
46  } else if (key == "occasional") {
47  tripleData.setIsOccasional(bson_iter_bool(&tripleIter_));
48  } else if (key == "since") {
49  tripleData.setBegin(bson_iter_double(&tripleIter_));
50  } else if (key == "until") {
51  tripleData.setEnd(bson_iter_double(&tripleIter_));
52  } else if (key == "confidence") {
53  tripleData.setConfidence(bson_iter_double(&tripleIter_));
54  tripleData.setIsUncertain(true);
55  } else if (key == "o") {
56  switch (bson_iter_type(&tripleIter_)) {
57  case BSON_TYPE_UTF8: {
58  // note: currently mongo KG does not store the type of the literal,
59  // so we cannot trivially distinguish between IRI and literal and need to guess here.
60  auto utf8 = bson_iter_utf8(&tripleIter_, nullptr);
61  switch (rdfNodeTypeGuess(utf8)) {
62  case RDFNodeType::IRI:
63  tripleData.setObjectIRI(utf8);
64  break;
66  tripleData.setStringValue(utf8);
67  break;
68  case RDFNodeType::BLANK:
69  tripleData.setObjectBlank(utf8);
70  break;
71  }
72  break;
73  }
74  case BSON_TYPE_DOUBLE:
75  tripleData.setDoubleValue(bson_iter_double(&tripleIter_));
76  break;
77  case BSON_TYPE_BOOL:
78  tripleData.setIntValue(bson_iter_bool(&tripleIter_));
79  break;
80  case BSON_TYPE_INT32:
81  tripleData.setIntValue(bson_iter_int32(&tripleIter_));
82  break;
83  case BSON_TYPE_INT64:
84  tripleData.setLongValue(bson_iter_int64(&tripleIter_));
85  break;
86  default:
87  KB_WARN("skipping triple with unexpected type '{}' of \"o\" field.",
88  bson_iter_type(&tripleIter_));
89  return nextTriple(tripleData);
90  }
91  } else if (key == "scope") {
92  bson_iter_t scopeIter, timeIter;
93  bson_iter_recurse(&tripleIter_, &scopeIter);
94  bson_iter_find(&scopeIter, "time");
95  bson_iter_recurse(&scopeIter, &timeIter);
96 
97  while (bson_iter_next(&timeIter)) {
98  std::string_view scopeKey = bson_iter_key(&timeIter);
99  if (scopeKey == "since") {
100  auto v = bson_iterOptionalDouble(&timeIter);
101  if (v && v.value() != 0) tripleData.setBegin(v.value());
102  } else if (scopeKey == "until") {
103  auto v = bson_iterOptionalDouble(&timeIter);
104  if (v && v.value() != 0) tripleData.setEnd(v.value());
105  }
106  }
107  }
108  }
109 
110  return true;
111  } else {
112  return false;
113  }
114 }
#define KB_WARN
Definition: Logger.h:27
virtual void setDoubleValue(double v)=0
virtual void reset()=0
virtual void setSubject(std::string_view subject)=0
virtual void setGraph(std::string_view graph)=0
virtual void setIntValue(int v)=0
virtual void setStringValue(std::string_view v)=0
void setConfidence(double confidence)
Definition: Triple.h:307
virtual void setPerspective(std::string_view perspective)=0
void setIsUncertain(bool isUncertain)
Definition: Triple.h:292
virtual void setLongValue(long v)=0
void setIsOccasional(bool isOccasional)
Definition: Triple.h:287
void setBegin(double begin)
Definition: Triple.h:297
void setEnd(double end)
Definition: Triple.h:302
virtual void setObjectIRI(std::string_view object)=0
virtual void setObjectBlank(std::string_view str)=0
virtual void setPredicate(std::string_view predicate)=0
bool next(const bson_t **doc, bool ignore_empty=false)
Definition: Cursor.cpp:67
bool nextTriple(Triple &tripleData, const bson_oid_t **tripleOID)
TripleCursor(const std::shared_ptr< Collection > &collection)
const bson_t * tripleDocument_
Definition: TripleCursor.h:36
std::optional< double > bson_iterOptionalDouble(const bson_iter_t *iter)
Definition: bson-helper.cpp:10
RDFNodeType rdfNodeTypeGuess(std::string_view str)
Definition: RDFNode.cpp:11