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

#include <OntologyParser.h>

Public Member Functions

 OntologyParser (const std::string_view &fileURI, semweb::TripleFormat format)
 
 ~OntologyParser ()
 
void setFrame (const GraphSelectorPtr &frame)
 
void setOrigin (std::string_view origin)
 
void setBlankPrefix (const std::string_view &blankPrefix)
 
void setFilter (const TripleFilter &filter)
 
bool run (const TripleHandler &callback)
 
void add (raptor_statement *statement, const TripleHandler &callback)
 
void flush (const TripleHandler &callback)
 
auto & imports () const
 
 OntologyParser (const std::string_view &fileURI, semweb::TripleFormat format)
 
 ~OntologyParser ()
 
void setFrame (const GraphSelectorPtr &frame)
 
void setOrigin (std::string_view origin)
 
void setBlankPrefix (const std::string_view &blankPrefix)
 
void setFilter (const TripleFilter &filter)
 
bool run (const TripleHandler &callback)
 
void add (raptor_statement *statement, const TripleHandler &callback)
 
void flush (const TripleHandler &callback)
 
auto & imports () const
 

Protected Member Functions

raptor_parser * createParser (knowrob::semweb::TripleFormat format)
 
void applyFrame (Triple *triple)
 
raptor_parser * createParser (knowrob::semweb::TripleFormat format)
 
void applyFrame (Triple *triple)
 

Static Protected Member Functions

static raptor_world * createWorld ()
 
static raptor_world * createWorld ()
 

Protected Attributes

raptor_uri * uri_
 
raptor_uri * uriBase_
 
raptor_parser * parser_
 
raptor_world * world_
 
GraphSelectorPtr frame_
 
TripleFilter filter_
 
std::string blankPrefix_
 
std::string origin_
 
std::vector< std::string > imports_
 
std::function< int()> doParse_
 
std::shared_ptr< RaptorContainercurrentBatch_
 

Detailed Description

A parser for RDF data.

Definition at line 22 of file OntologyParser.h.

Constructor & Destructor Documentation

◆ OntologyParser() [1/2]

OntologyParser::OntologyParser ( const std::string_view &  fileURI,
semweb::TripleFormat  format 
)
Parameters
fileURIthe URI of the file to parse.
formatthe format of the file.

Definition at line 60 of file OntologyParser.cpp.

60  {
61  world_ = createWorld();
62  parser_ = createParser(format);
63  // raptor can report namespaces
64  raptor_parser_set_namespace_handler(parser_, nullptr, procesNamespace);
65 
66  if (fs::exists(fileURI)) {
67  // Parse the content of a local file URI
68  auto escapedString = raptor_uri_filename_to_uri_string(fileURI.data());
69  uri_ = raptor_new_uri(world_, (unsigned char *) escapedString);
70  doParse_ = [this]() {
71  return raptor_parser_parse_file(parser_, uri_, uriBase_);
72  };
73  raptor_free_memory(escapedString);
74  } else {
75  // Parse the content from a remote file URI
76  uri_ = raptor_new_uri(world_, (const unsigned char *) fileURI.data());
77  doParse_ = [this]() {
78  return raptor_parser_parse_uri(parser_, uri_, uriBase_);
79  };
80  }
81  uriBase_ = raptor_uri_copy(uri_);
82 }
raptor_parser * parser_
raptor_parser * createParser(knowrob::semweb::TripleFormat format)
std::function< int()> doParse_
static raptor_world * createWorld()

◆ ~OntologyParser() [1/2]

OntologyParser::~OntologyParser ( )

Definition at line 84 of file OntologyParser.cpp.

84  {
85  raptor_free_parser(parser_);
86  raptor_free_uri(uri_);
87  raptor_free_uri(uriBase_);
88  raptor_free_world(world_);
89 }

◆ OntologyParser() [2/2]

knowrob::OntologyParser::OntologyParser ( const std::string_view &  fileURI,
semweb::TripleFormat  format 
)
Parameters
fileURIthe URI of the file to parse.
formatthe format of the file.

◆ ~OntologyParser() [2/2]

knowrob::OntologyParser::~OntologyParser ( )

Member Function Documentation

◆ add() [1/2]

void OntologyParser::add ( raptor_statement *  statement,
const TripleHandler callback 
)
Parameters
statementa raptor statement
callbackthe callback to use for handling triples.

Definition at line 129 of file OntologyParser.cpp.

129  {
130  auto batchSize = GlobalSettings::batchSize();
131  if (!currentBatch_) {
132  if (origin_.empty()) {
133  KB_WARN("No origin set for ontology parser, falling back to \"user\" origin.");
134  currentBatch_ = std::make_shared<RaptorContainer>(batchSize, ImportHierarchy::ORIGIN_USER);
135  } else {
136  currentBatch_ = std::make_shared<RaptorContainer>(batchSize, origin_);
137  }
138  }
139  // add to batch, map into knowrob data structures
140  auto triple = currentBatch_->add(statement);
141  if (filter_ && !filter_(*triple)) {
142  currentBatch_->rollbackLast();
143  return;
144  }
145  applyFrame(triple);
146  // remember imports
147  if (triple && owl::imports->stringForm() == triple->predicate()) {
148  imports_.emplace_back(triple->valueAsString());
149  }
150  // flush if batch is full
151  if (currentBatch_->size() >= batchSize) {
152  flush(callback);
153  }
154 }
#define KB_WARN
Definition: Logger.h:27
static uint32_t batchSize()
Definition: knowrob.h:68
static constexpr std::string_view ORIGIN_USER
void flush(const TripleHandler &callback)
std::vector< std::string > imports_
void applyFrame(Triple *triple)
std::shared_ptr< RaptorContainer > currentBatch_
const IRIAtomPtr imports
Definition: owl.h:15

◆ add() [2/2]

void knowrob::OntologyParser::add ( raptor_statement *  statement,
const TripleHandler callback 
)
Parameters
statementa raptor statement
callbackthe callback to use for handling triples.

◆ applyFrame() [1/2]

void OntologyParser::applyFrame ( Triple triple)
protected

Definition at line 105 of file OntologyParser.cpp.

105  {
106  if (frame_) {
107  if (frame_->confidence.has_value()) {
108  triple->setConfidence(frame_->confidence.value());
109  triple->setIsUncertain(true);
110  }
111  if (frame_->perspective) {
112  triple->setPerspective(frame_->perspective->iri());
113  }
114  if (frame_->uncertain) {
115  triple->setIsUncertain(true);
116  }
117  if (frame_->occasional) {
118  triple->setIsOccasional(true);
119  }
120  if (frame_->begin.has_value()) {
121  triple->setBegin(frame_->begin.value());
122  }
123  if (frame_->end.has_value()) {
124  triple->setEnd(frame_->end.value());
125  }
126  }
127 }
GraphSelectorPtr frame_
void setConfidence(double confidence)
Definition: Triple.h:307
virtual void setPerspective(std::string_view perspective)=0
void setIsUncertain(bool isUncertain)
Definition: Triple.h:292
void setIsOccasional(bool isOccasional)
Definition: Triple.h:287
void setBegin(double begin)
Definition: Triple.h:297
void setEnd(double end)
Definition: Triple.h:302

◆ applyFrame() [2/2]

void knowrob::OntologyParser::applyFrame ( Triple triple)
protected

◆ createParser() [1/2]

raptor_parser * OntologyParser::createParser ( knowrob::semweb::TripleFormat  format)
protected

Definition at line 101 of file OntologyParser.cpp.

101  {
102  return raptor_new_parser(world_, tripleFormatMimeType(format).data());
103 }
std::string_view tripleFormatMimeType(TripleFormat format)

◆ createParser() [2/2]

raptor_parser* knowrob::OntologyParser::createParser ( knowrob::semweb::TripleFormat  format)
protected

◆ createWorld() [1/2]

raptor_world * OntologyParser::createWorld ( )
staticprotected

Definition at line 91 of file OntologyParser.cpp.

91  {
92  auto world = raptor_new_world();
93  // redirect log messages to the knowrob logger
94  raptor_world_set_log_handler(world, nullptr, raptor_log);
95  if (raptor_world_open(world) != 0) {
96  throw OntologyError("failed to initialize raptor library.");
97  }
98  return world;
99 }

◆ createWorld() [2/2]

static raptor_world* knowrob::OntologyParser::createWorld ( )
staticprotected

◆ flush() [1/2]

void OntologyParser::flush ( const TripleHandler callback)

Flush the parser, pushing all remaining triples to the callback.

Definition at line 156 of file OntologyParser.cpp.

156  {
157  if (!currentBatch_) return;
158  // reduce vector size to actual number of elements
159  currentBatch_->shrink();
160  // call the callback with the current batch, note that the callback
161  // holds a reference on the batch which is only deleted after the callback lifts the reference.
162  //KB_DEBUG("flushing {} triples with origin {}", currentBatch_->size(), currentBatch_->origin());
163  callback(currentBatch_);
164  currentBatch_ = nullptr;
165 }

◆ flush() [2/2]

void knowrob::OntologyParser::flush ( const TripleHandler callback)

Flush the parser, pushing all remaining triples to the callback.

◆ imports() [1/2]

auto& knowrob::OntologyParser::imports ( ) const
inline
Returns
the list of directly imported ontologies.

Definition at line 72 of file OntologyParser.h.

72 { return imports_; }

◆ imports() [2/2]

auto& knowrob::OntologyParser::imports ( ) const
inline
Returns
the list of directly imported ontologies.

Definition at line 72 of file OntologyParser.h.

72 { return imports_; }

◆ run() [1/2]

bool OntologyParser::run ( const TripleHandler callback)
Parameters
callbackthe callback to use for handling triples.
Returns
true if the parsing was successful, false otherwise.

Definition at line 168 of file OntologyParser.cpp.

168  {
169  // call processTriple for each loaded triple
170  RaptorUserData userData = {const_cast<OntologyParser *>(this), callback};
171  raptor_parser_set_statement_handler(parser_, &userData, processTriple);
172 
173  // make sure blanks are generated with proper prefix.
174  raptor_world_set_generate_bnodeid_parameters(
175  raptor_parser_get_world(parser_),
176  blankPrefix_.data(), 1);
177 
178  auto exit_status = doParse_();
179  if (exit_status == 0) {
180  flush(callback);
181  } else {
182  currentBatch_ = nullptr;
183  }
184  return (exit_status == 0);
185 }

◆ run() [2/2]

bool knowrob::OntologyParser::run ( const TripleHandler callback)
Parameters
callbackthe callback to use for handling triples.
Returns
true if the parsing was successful, false otherwise.

◆ setBlankPrefix() [1/2]

void knowrob::OntologyParser::setBlankPrefix ( const std::string_view &  blankPrefix)
inline
Parameters
blankPrefixthe prefix to use for blank nodes.

Definition at line 45 of file OntologyParser.h.

45 { blankPrefix_ = blankPrefix; }

◆ setBlankPrefix() [2/2]

void knowrob::OntologyParser::setBlankPrefix ( const std::string_view &  blankPrefix)
inline
Parameters
blankPrefixthe prefix to use for blank nodes.

Definition at line 45 of file OntologyParser.h.

45 { blankPrefix_ = blankPrefix; }

◆ setFilter() [1/2]

void knowrob::OntologyParser::setFilter ( const TripleFilter filter)
inline
Parameters
filterthe filter to use for filtering triples.

Definition at line 50 of file OntologyParser.h.

50 { filter_ = filter; }

◆ setFilter() [2/2]

void knowrob::OntologyParser::setFilter ( const TripleFilter filter)
inline
Parameters
filterthe filter to use for filtering triples.

Definition at line 50 of file OntologyParser.h.

50 { filter_ = filter; }

◆ setFrame() [1/2]

void knowrob::OntologyParser::setFrame ( const GraphSelectorPtr frame)
inline
Parameters
framethe graph selector to use for filtering triples.

Definition at line 35 of file OntologyParser.h.

35 { frame_ = frame; }

◆ setFrame() [2/2]

void knowrob::OntologyParser::setFrame ( const GraphSelectorPtr frame)
inline
Parameters
framethe graph selector to use for filtering triples.

Definition at line 35 of file OntologyParser.h.

35 { frame_ = frame; }

◆ setOrigin() [1/2]

void knowrob::OntologyParser::setOrigin ( std::string_view  origin)
inline
Parameters
originthe origin to use for triples.

Definition at line 40 of file OntologyParser.h.

40 { origin_ = origin; }

◆ setOrigin() [2/2]

void knowrob::OntologyParser::setOrigin ( std::string_view  origin)
inline
Parameters
originthe origin to use for triples.

Definition at line 40 of file OntologyParser.h.

40 { origin_ = origin; }

Member Data Documentation

◆ blankPrefix_

std::string knowrob::OntologyParser::blankPrefix_
protected

Definition at line 81 of file OntologyParser.h.

◆ currentBatch_

std::shared_ptr< RaptorContainer > knowrob::OntologyParser::currentBatch_
protected

Definition at line 92 of file OntologyParser.h.

◆ doParse_

std::function< int()> knowrob::OntologyParser::doParse_
protected

Definition at line 84 of file OntologyParser.h.

◆ filter_

TripleFilter knowrob::OntologyParser::filter_
protected

Definition at line 80 of file OntologyParser.h.

◆ frame_

GraphSelectorPtr knowrob::OntologyParser::frame_
protected

Definition at line 79 of file OntologyParser.h.

◆ imports_

std::vector< std::string > knowrob::OntologyParser::imports_
protected

Definition at line 83 of file OntologyParser.h.

◆ origin_

std::string knowrob::OntologyParser::origin_
protected

Definition at line 82 of file OntologyParser.h.

◆ parser_

raptor_parser * knowrob::OntologyParser::parser_
protected

Definition at line 77 of file OntologyParser.h.

◆ uri_

raptor_uri * knowrob::OntologyParser::uri_
protected

Definition at line 75 of file OntologyParser.h.

◆ uriBase_

raptor_uri * knowrob::OntologyParser::uriBase_
protected

Definition at line 76 of file OntologyParser.h.

◆ world_

raptor_world * knowrob::OntologyParser::world_
protected

Definition at line 78 of file OntologyParser.h.


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