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

#include <BindingsCursor.h>

Inheritance diagram for knowrob::mongo::BindingsCursor:
Collaboration diagram for knowrob::mongo::BindingsCursor:

Public Member Functions

 BindingsCursor (const std::shared_ptr< Collection > &collection)
 
bool nextBindings (const std::shared_ptr< Bindings > &bindings)
 
 BindingsCursor (const std::shared_ptr< Collection > &collection)
 
bool nextBindings (const std::shared_ptr< Bindings > &bindings)
 
- 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 Member Functions

void setSubstitution (const std::shared_ptr< Bindings > &bindings)
 
void setSubstitution (const std::shared_ptr< Bindings > &bindings)
 

Protected Attributes

const bson_t * resultDocument_
 
bson_iter_t resultIter_
 
bson_iter_t varIter_
 
bson_iter_t valIter_
 

Detailed Description

A Cursor that iterates over bindings computed through an aggregation pipeline which uses a special field in documents to store variable bindings throughout the pipeline.

Definition at line 18 of file BindingsCursor.h.

Constructor & Destructor Documentation

◆ BindingsCursor() [1/2]

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

Definition at line 17 of file BindingsCursor.cpp.

18  : Cursor(collection),
19  resultDocument_(nullptr),
20  resultIter_(),
21  varIter_(),
22  valIter_() {
23 }
Cursor(const std::shared_ptr< Collection > &collection)
Definition: Cursor.cpp:15

◆ BindingsCursor() [2/2]

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

Member Function Documentation

◆ nextBindings() [1/2]

bool BindingsCursor::nextBindings ( const std::shared_ptr< Bindings > &  bindings)
Parameters
bindingsthe bindings to fill with the next result.
Returns
true if there is a next result, false otherwise.

Definition at line 73 of file BindingsCursor.cpp.

73  {
74  if (!next(&resultDocument_)) return false;
75  if (!bson_iter_init(&resultIter_, resultDocument_)) return false;
76 
77  while (bson_iter_next(&resultIter_)) {
78  std::string_view resultKey(bson_iter_key(&resultIter_));
79  // read variables from "v_VARS". each sub-field is named according to the field of
80  // a variable.
81  if (resultKey == "v_VARS" && bson_iter_recurse(&resultIter_, &varIter_)) {
82  setSubstitution(bindings);
83  }
84  }
85 
86  return true;
87 }
void setSubstitution(const std::shared_ptr< Bindings > &bindings)
bool next(const bson_t **doc, bool ignore_empty=false)
Definition: Cursor.cpp:67

◆ nextBindings() [2/2]

bool knowrob::mongo::BindingsCursor::nextBindings ( const std::shared_ptr< Bindings > &  bindings)
Parameters
bindingsthe bindings to fill with the next result.
Returns
true if there is a next result, false otherwise.

◆ setSubstitution() [1/2]

void BindingsCursor::setSubstitution ( const std::shared_ptr< Bindings > &  bindings)
protected

Definition at line 25 of file BindingsCursor.cpp.

25  {
26  while (bson_iter_next(&varIter_)) {
27  auto var = std::make_shared<Variable>(bson_iter_key(&varIter_));
28 
29  if (!bson_iter_recurse(&varIter_, &valIter_)) continue;
30  if (!bson_iter_find(&valIter_, "val")) continue;
31 
32  // read the value of the variable
33  switch (bson_iter_type(&valIter_)) {
34  case BSON_TYPE_UTF8: {
35  auto utf8 = bson_iter_utf8(&valIter_, nullptr);
36  // note: currently mongo KG does not store the type of the literal,
37  // so we cannot trivially distinguish between IRI and literal and need to guess here.
38  // note: we need to copy the string data here, as the bson_iter_utf8() returns a pointer
39  // to the internal buffer of the bson document, which is not guaranteed to be valid
40  // after the cursor is advanced.
41  switch (rdfNodeTypeGuess(utf8)) {
42  case RDFNodeType::BLANK:
43  bindings->set(var, Blank::Tabled(utf8));
44  break;
45  case RDFNodeType::IRI:
46  bindings->set(var, IRIAtom::Tabled(utf8));
47  break;
49  bindings->set(var, std::make_shared<String>(utf8));
50  break;
51  }
52  break;
53  }
54  case BSON_TYPE_INT32:
55  bindings->set(var, std::make_shared<Integer>(bson_iter_int32(&valIter_)));
56  break;
57  case BSON_TYPE_INT64:
58  bindings->set(var, std::make_shared<Long>(bson_iter_int64(&valIter_)));
59  break;
60  case BSON_TYPE_BOOL:
61  bindings->set(var, std::make_shared<Boolean>(bson_iter_bool(&valIter_)));
62  break;
63  case BSON_TYPE_DOUBLE:
64  bindings->set(var, std::make_shared<Double>(bson_iter_double(&valIter_)));
65  break;
66  default:
67  KB_WARN("unsupported type {} for predicate arguments.", bson_iter_type(&valIter_));
68  break;
69  }
70  }
71 }
#define KB_WARN
Definition: Logger.h:27
static std::shared_ptr< Blank > Tabled(std::string_view stringForm)
Definition: Blank.cpp:12
static std::shared_ptr< IRIAtom > Tabled(std::string_view stringForm)
Definition: IRIAtom.cpp:25
VariableRule & var()
Definition: terms.cpp:91
RDFNodeType rdfNodeTypeGuess(std::string_view str)
Definition: RDFNode.cpp:11

◆ setSubstitution() [2/2]

void knowrob::mongo::BindingsCursor::setSubstitution ( const std::shared_ptr< Bindings > &  bindings)
protected

Member Data Documentation

◆ resultDocument_

const bson_t * knowrob::mongo::BindingsCursor::resultDocument_
protected

Definition at line 29 of file BindingsCursor.h.

◆ resultIter_

bson_iter_t knowrob::mongo::BindingsCursor::resultIter_
protected

Definition at line 30 of file BindingsCursor.h.

◆ valIter_

bson_iter_t knowrob::mongo::BindingsCursor::valIter_
protected

Definition at line 32 of file BindingsCursor.h.

◆ varIter_

bson_iter_t knowrob::mongo::BindingsCursor::varIter_
protected

Definition at line 31 of file BindingsCursor.h.


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