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

#include <RaptorContainer.h>

Inheritance diagram for knowrob::RaptorContainer:
Collaboration diagram for knowrob::RaptorContainer:

Classes

struct  mapped_statement
 

Public Member Functions

 RaptorContainer (uint32_t size, std::string_view origin)
 
 RaptorContainer (uint32_t size)
 
 ~RaptorContainer ()
 
Tripleadd (raptor_term *s, raptor_term *p, raptor_term *o, librdf_node *context=nullptr)
 
Tripleadd (raptor_statement *statement, librdf_node *context=nullptr)
 
void reset ()
 
void rollbackLast ()
 
void shrink ()
 
auto size () const
 
ConstGenerator cgenerator () const override
 
MutableGenerator generator () override
 
 RaptorContainer (uint32_t size, std::string_view origin)
 
 RaptorContainer (uint32_t size)
 
 ~RaptorContainer ()
 
Tripleadd (raptor_term *s, raptor_term *p, raptor_term *o, librdf_node *context=nullptr)
 
Tripleadd (raptor_statement *statement, librdf_node *context=nullptr)
 
void reset ()
 
void rollbackLast ()
 
void shrink ()
 
auto size () const
 
ConstGenerator cgenerator () const override
 
MutableGenerator generator () override
 
- Public Member Functions inherited from knowrob::MutableTripleContainer
iterator begin ()
 
bool isMutable () const override
 
iterator begin ()
 
bool isMutable () const override
 
- Public Member Functions inherited from knowrob::TripleContainer
virtual ~TripleContainer ()=default
 
iterator begin () const
 
bool empty () const
 
virtual ~TripleContainer ()=default
 
iterator begin () const
 
bool empty () const
 

Protected Attributes

std::vector< mapped_statementraptorData_
 
std::vector< TriplePtrmappedData_
 
std::size_t actualSize_
 
std::optional< std::string_view > origin_
 

Additional Inherited Members

- Public Types inherited from knowrob::MutableTripleContainer
using MutableGenerator = std::function< TriplePtr *()>
 
using MutableGenerator = std::function< TriplePtr *()>
 
- Public Types inherited from knowrob::TripleContainer
using ConstGenerator = std::function< const TriplePtr *()>
 
using ConstGenerator = std::function< const TriplePtr *()>
 
- Static Public Member Functions inherited from knowrob::MutableTripleContainer
static iterator end ()
 
static iterator end ()
 
- Static Public Member Functions inherited from knowrob::TripleContainer
static iterator end ()
 
static iterator end ()
 

Detailed Description

A batch of triples loaded with raptor.

Definition at line 18 of file RaptorContainer.h.

Constructor & Destructor Documentation

◆ RaptorContainer() [1/4]

RaptorContainer::RaptorContainer ( uint32_t  size,
std::string_view  origin 
)
Parameters
sizethe maximum number of triples to be stored.
originthe origin of the triples.

Definition at line 12 of file RaptorContainer.cpp.

13  : raptorData_(size),
15  actualSize_(0),
16  origin_(origin) {
17  for (auto &triple: mappedData_) {
18  triple.ptr = new TripleView();
19  triple.owned = true;
20  }
21 }
std::optional< std::string_view > origin_
std::vector< mapped_statement > raptorData_
std::vector< TriplePtr > mappedData_
TripleTemplate< std::string_view > TripleView
Definition: Triple.h:581

◆ RaptorContainer() [2/4]

RaptorContainer::RaptorContainer ( uint32_t  size)
explicit
Parameters
sizethe maximum number of triples to be stored.

Definition at line 23 of file RaptorContainer.cpp.

24  : raptorData_(size),
26  actualSize_(0) {
27 }

◆ ~RaptorContainer() [1/2]

RaptorContainer::~RaptorContainer ( )

Definition at line 29 of file RaptorContainer.cpp.

29  {
30  reset();
31 }

◆ RaptorContainer() [3/4]

knowrob::RaptorContainer::RaptorContainer ( uint32_t  size,
std::string_view  origin 
)
Parameters
sizethe maximum number of triples to be stored.
originthe origin of the triples.

◆ RaptorContainer() [4/4]

knowrob::RaptorContainer::RaptorContainer ( uint32_t  size)
explicit
Parameters
sizethe maximum number of triples to be stored.

◆ ~RaptorContainer() [2/2]

knowrob::RaptorContainer::~RaptorContainer ( )

Member Function Documentation

◆ add() [1/4]

Triple * RaptorContainer::add ( raptor_statement *  statement,
librdf_node *  context = nullptr 
)

Add a triple to the batch.

Parameters
statementa raptor statement.
contextthe context of the statement.
Returns
the added statement.

Definition at line 113 of file RaptorContainer.cpp.

113  {
114  return add(statement->subject, statement->predicate, statement->object, context);
115 }
Triple * add(raptor_term *s, raptor_term *p, raptor_term *o, librdf_node *context=nullptr)

◆ add() [2/4]

Triple* knowrob::RaptorContainer::add ( raptor_statement *  statement,
librdf_node *  context = nullptr 
)

Add a triple to the batch.

Parameters
statementa raptor statement.
contextthe context of the statement.
Returns
the added statement.

◆ add() [3/4]

Triple * RaptorContainer::add ( raptor_term *  s,
raptor_term *  p,
raptor_term *  o,
librdf_node *  context = nullptr 
)

Add a triple to the batch.

Parameters
sa raptor term.
pa raptor term.
oa raptor term.
contextthe context of the statement.
Returns
the added statement.

Definition at line 56 of file RaptorContainer.cpp.

56  {
57  // validate input from raptor
58  if (!s || !p || !o) {
59  KB_WARN("received malformed data from raptor, skipping statement.");
60  return nullptr;
61  }
62 
63  // keep a reference on the data
64  auto c_s = raptor_term_copy(s);
65  auto c_p = raptor_term_copy(p);
66  auto c_o = raptor_term_copy(o);
67  auto &endpoint_triple = raptorData_[actualSize_];
68  endpoint_triple.s = c_s;
69  endpoint_triple.p = c_p;
70  endpoint_triple.o = c_o;
71 
72  // map statement to KnowRob datatype
73  auto triple = mappedData_[actualSize_];
74  triple->reset();
75 
76  // read graph name
77  if (context) {
78  // the context node is a string literal with the graph name
79  triple->setGraph((const char *) librdf_node_get_literal_value(context));
80  } else if (origin_.has_value()) {
81  // user specified origin
82  triple->setGraph(origin_.value().data());
83  }
84 
85  // read predicate
86  triple->setPredicate((const char *) raptor_uri_as_string(c_p->value.uri));
87  // read subject
88  if (c_s->type == RAPTOR_TERM_TYPE_BLANK)
89  triple->setSubjectBlank((const char *) c_s->value.blank.string);
90  else
91  triple->setSubject((const char *) raptor_uri_as_string(c_s->value.uri));
92 
93  // read object
94  if (c_o->type == RAPTOR_TERM_TYPE_BLANK) {
95  triple->setObjectBlank((const char *) c_o->value.blank.string);
96  } else if (c_o->type == RAPTOR_TERM_TYPE_LITERAL) {
97  auto stringForm = (const char *) c_o->value.literal.string;
98  // parse literal type
99  XSDType xsdType = XSDType::STRING;
100  if (c_o->value.literal.datatype) {
101  auto typeURI = (const char *) raptor_uri_as_string(c_o->value.literal.datatype);
102  xsdType = xsdTypeFromIRI(typeURI);
103  }
104  triple->setXSDValue(stringForm, xsdType);
105  } else {
106  triple->setObjectIRI((const char *) raptor_uri_as_string(c_o->value.uri));
107  }
108 
109  actualSize_ += 1;
110  return triple.ptr;
111 }
#define KB_WARN
Definition: Logger.h:27
XSDType xsdTypeFromIRI(std::string_view iri)
Definition: XSDAtomic.cpp:48
XSDType
The XSDType enum Enumeration of the XSD types.
Definition: XSDType.h:16

◆ add() [4/4]

Triple* knowrob::RaptorContainer::add ( raptor_term *  s,
raptor_term *  p,
raptor_term *  o,
librdf_node *  context = nullptr 
)

Add a triple to the batch.

Parameters
sa raptor term.
pa raptor term.
oa raptor term.
contextthe context of the statement.
Returns
the added statement.

◆ cgenerator() [1/2]

TripleContainer::ConstGenerator RaptorContainer::cgenerator ( ) const
overridevirtual
Returns
a generator function that returns a const pointer to a triple.

Implements knowrob::TripleContainer.

Definition at line 33 of file RaptorContainer.cpp.

33  {
34  return [this, i = std::size_t(0)]() mutable -> const TriplePtr * {
35  if (i < actualSize_) return &mappedData_[i++];
36  return nullptr;
37  };
38 }

◆ cgenerator() [2/2]

ConstGenerator knowrob::RaptorContainer::cgenerator ( ) const
overridevirtual
Returns
a generator function that returns a const pointer to a triple.

Implements knowrob::TripleContainer.

◆ generator() [1/2]

MutableTripleContainer::MutableGenerator RaptorContainer::generator ( )
overridevirtual
Returns
a generator function that returns a pointer to a triple.

Implements knowrob::MutableTripleContainer.

Definition at line 40 of file RaptorContainer.cpp.

40  {
41  return [this, i = std::size_t(0)]() mutable -> TriplePtr * {
42  if (i < actualSize_) return &mappedData_[i++];
43  return nullptr;
44  };
45 }

◆ generator() [2/2]

MutableGenerator knowrob::RaptorContainer::generator ( )
overridevirtual
Returns
a generator function that returns a pointer to a triple.

Implements knowrob::MutableTripleContainer.

◆ reset() [1/2]

void RaptorContainer::reset ( )

Reset the batch to be empty.

Definition at line 47 of file RaptorContainer.cpp.

47  {
48  for (std::size_t i = 0; i < actualSize_; i++) {
49  raptor_free_term(raptorData_[i].s);
50  raptor_free_term(raptorData_[i].p);
51  raptor_free_term(raptorData_[i].o);
52  }
53  actualSize_ = 0;
54 }

◆ reset() [2/2]

void knowrob::RaptorContainer::reset ( )

Reset the batch to be empty.

◆ rollbackLast() [1/2]

void RaptorContainer::rollbackLast ( )

Rollback the last added statement.

Definition at line 117 of file RaptorContainer.cpp.

117  {
118  if (actualSize_ > 0) {
119  actualSize_ -= 1;
120  raptor_free_term(raptorData_[actualSize_].s);
121  raptor_free_term(raptorData_[actualSize_].p);
122  raptor_free_term(raptorData_[actualSize_].o);
123  }
124 }

◆ rollbackLast() [2/2]

void knowrob::RaptorContainer::rollbackLast ( )

Rollback the last added statement.

◆ shrink() [1/2]

void RaptorContainer::shrink ( )

Shrink the batch to the actual size.

Definition at line 126 of file RaptorContainer.cpp.

126  {
127  mappedData_.resize(actualSize_);
128  raptorData_.resize(actualSize_);
129 }

◆ shrink() [2/2]

void knowrob::RaptorContainer::shrink ( )

Shrink the batch to the actual size.

◆ size() [1/2]

auto knowrob::RaptorContainer::size ( ) const
inline
Returns
the actual size of the batch.

Definition at line 69 of file RaptorContainer.h.

69 { return actualSize_; }

◆ size() [2/2]

auto knowrob::RaptorContainer::size ( ) const
inline
Returns
the actual size of the batch.

Definition at line 69 of file RaptorContainer.h.

69 { return actualSize_; }

Member Data Documentation

◆ actualSize_

std::size_t knowrob::RaptorContainer::actualSize_
protected

Definition at line 85 of file RaptorContainer.h.

◆ mappedData_

std::vector< TriplePtr > knowrob::RaptorContainer::mappedData_
protected

Definition at line 84 of file RaptorContainer.h.

◆ origin_

std::optional< std::string_view > knowrob::RaptorContainer::origin_
protected

Definition at line 86 of file RaptorContainer.h.

◆ raptorData_

std::vector< mapped_statement > knowrob::RaptorContainer::raptorData_
protected

Definition at line 83 of file RaptorContainer.h.


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