knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
RaptorContainer.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/redland/RaptorContainer.h"
7 #include "knowrob/terms/XSDAtomic.h"
8 #include "knowrob/Logger.h"
9 
10 using namespace knowrob;
11 
12 RaptorContainer::RaptorContainer(uint32_t size, std::string_view origin)
13  : raptorData_(size),
14  mappedData_(size),
15  actualSize_(0),
16  origin_(origin) {
17  for (auto &triple: mappedData_) {
18  triple.ptr = new TripleView();
19  triple.owned = true;
20  }
21 }
22 
24  : raptorData_(size),
25  mappedData_(size),
26  actualSize_(0) {
27 }
28 
30  reset();
31 }
32 
34  return [this, i = std::size_t(0)]() mutable -> const TriplePtr * {
35  if (i < actualSize_) return &mappedData_[i++];
36  return nullptr;
37  };
38 }
39 
41  return [this, i = std::size_t(0)]() mutable -> TriplePtr * {
42  if (i < actualSize_) return &mappedData_[i++];
43  return nullptr;
44  };
45 }
46 
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 }
55 
56 Triple *RaptorContainer::add(raptor_term *s, raptor_term *p, raptor_term *o, librdf_node *context) {
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 }
112 
113 Triple *RaptorContainer::add(raptor_statement *statement, librdf_node *context) {
114  return add(statement->subject, statement->predicate, statement->object, context);
115 }
116 
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 }
125 
127  mappedData_.resize(actualSize_);
128  raptorData_.resize(actualSize_);
129 }
#define KB_WARN
Definition: Logger.h:27
std::function< TriplePtr *()> MutableGenerator
std::optional< std::string_view > origin_
RaptorContainer(uint32_t size, std::string_view origin)
std::vector< mapped_statement > raptorData_
ConstGenerator cgenerator() const override
Triple * add(raptor_term *s, raptor_term *p, raptor_term *o, librdf_node *context=nullptr)
std::vector< TriplePtr > mappedData_
MutableGenerator generator() override
std::function< const TriplePtr *()> ConstGenerator
XSDType xsdTypeFromIRI(std::string_view iri)
Definition: XSDAtomic.cpp:48
XSDType
The XSDType enum Enumeration of the XSD types.
Definition: XSDType.h:16
TripleTemplate< std::string_view > TripleView
Definition: Triple.h:581