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

#include <TripleCursor.h>

Inheritance diagram for knowrob::mongo::TripleCursor:
Collaboration diagram for knowrob::mongo::TripleCursor:

Public Member Functions

 TripleCursor (const std::shared_ptr< Collection > &collection)
 
bool nextTriple (Triple &tripleData, const bson_oid_t **tripleOID)
 
bool nextTriple (Triple &tripleData)
 
auto tripleDocument ()
 
 TripleCursor (const std::shared_ptr< Collection > &collection)
 
bool nextTriple (Triple &tripleData, const bson_oid_t **tripleOID)
 
bool nextTriple (Triple &tripleData)
 
auto tripleDocument ()
 
- Public Member Functions inherited from knowrob::mongo::Cursor
 Cursor (const std::shared_ptr< Collection > &collection)
 
 Cursor (const Cursor &)=delete
 
 ~Cursor ()
 
const auto & id ()
 
void limit (unsigned int limit)
 
void ascending (const char *key)
 
void descending (const char *key)
 
void filter (const bson_t *query_doc)
 
void aggregate (const bson_t *query_doc)
 
bool next (const bson_t **doc, bool ignore_empty=false)
 
bool erase ()
 
const auto * query () const
 
 Cursor (const std::shared_ptr< Collection > &collection)
 
 Cursor (const Cursor &)=delete
 
 ~Cursor ()
 
const auto & id ()
 
void limit (unsigned int limit)
 
void ascending (const char *key)
 
void descending (const char *key)
 
void filter (const bson_t *query_doc)
 
void aggregate (const bson_t *query_doc)
 
bool next (const bson_t **doc, bool ignore_empty=false)
 
bool erase ()
 
const auto * query () const
 

Protected Attributes

const bson_t * tripleDocument_
 
bson_iter_t tripleIter_
 

Detailed Description

A getAnswerCursor that iterates over different results of a query, and fills a StatementData structure with the data.

Definition at line 17 of file TripleCursor.h.

Constructor & Destructor Documentation

◆ TripleCursor() [1/2]

TripleCursor::TripleCursor ( const std::shared_ptr< Collection > &  collection)
explicit

Definition at line 14 of file TripleCursor.cpp.

15  : Cursor(collection),
16  tripleDocument_(nullptr),
17  tripleIter_() {
18 }
Cursor(const std::shared_ptr< Collection > &collection)
Definition: Cursor.cpp:15
const bson_t * tripleDocument_
Definition: TripleCursor.h:36

◆ TripleCursor() [2/2]

knowrob::mongo::TripleCursor::TripleCursor ( const std::shared_ptr< Collection > &  collection)
explicit

Member Function Documentation

◆ nextTriple() [1/4]

bool TripleCursor::nextTriple ( Triple tripleData)

Get the next triple from this getAnswerCursor if any.

Parameters
tripleDataa triple data structure
Returns
true on success

Definition at line 20 of file TripleCursor.cpp.

21 {
22  const bson_oid_t *tripleOID = nullptr;
23  return nextTriple(tripleData, &tripleOID);
24 }
bool nextTriple(Triple &tripleData, const bson_oid_t **tripleOID)

◆ nextTriple() [2/4]

bool knowrob::mongo::TripleCursor::nextTriple ( Triple tripleData)

Get the next triple from this getAnswerCursor if any.

Parameters
tripleDataa triple data structure
Returns
true on success

◆ nextTriple() [3/4]

bool TripleCursor::nextTriple ( Triple tripleData,
const bson_oid_t **  tripleOID 
)

Definition at line 26 of file TripleCursor.cpp.

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
bool next(const bson_t **doc, bool ignore_empty=false)
Definition: Cursor.cpp:67
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

◆ nextTriple() [4/4]

bool knowrob::mongo::TripleCursor::nextTriple ( Triple tripleData,
const bson_oid_t **  tripleOID 
)

◆ tripleDocument() [1/2]

auto knowrob::mongo::TripleCursor::tripleDocument ( )
inline
Returns
the last document fetched by this getAnswerCursor, or null if no document was fetched before.

Definition at line 33 of file TripleCursor.h.

33 { return tripleDocument_; }

◆ tripleDocument() [2/2]

auto knowrob::mongo::TripleCursor::tripleDocument ( )
inline
Returns
the last document fetched by this getAnswerCursor, or null if no document was fetched before.

Definition at line 33 of file TripleCursor.h.

33 { return tripleDocument_; }

Member Data Documentation

◆ tripleDocument_

const bson_t * knowrob::mongo::TripleCursor::tripleDocument_
protected

Definition at line 36 of file TripleCursor.h.

◆ tripleIter_

bson_iter_t knowrob::mongo::TripleCursor::tripleIter_
protected

Definition at line 37 of file TripleCursor.h.


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