7 #include "knowrob/semweb/OntologyParser.h"
8 #include "knowrob/semweb/OntologyError.h"
9 #include "knowrob/semweb/owl.h"
10 #include "knowrob/semweb/PrefixRegistry.h"
11 #include "knowrob/semweb/ImportHierarchy.h"
12 #include "knowrob/knowrob.h"
13 #include "knowrob/Logger.h"
15 namespace fs = std::filesystem;
19 struct RaptorUserData {
25 static void processTriple(
void *userData, raptor_statement *statement) {
26 auto x = (RaptorUserData *) userData;
27 x->parser->add(statement, x->callback);
30 static void procesNamespace([[maybe_unused]]
void *userData, raptor_namespace *nspace) {
31 auto r_prefix = raptor_namespace_get_prefix(nspace);
32 if (!r_prefix)
return;
34 auto r_uri = raptor_namespace_get_uri(nspace);
36 auto r_uriString = raptor_uri_as_string(r_uri);
39 std::string_view((
const char *) r_prefix),
40 std::string_view((
const char *) r_uriString));
43 static void raptor_log(
void *, raptor_log_message *message) {
44 switch (message->level) {
45 case RAPTOR_LOG_LEVEL_ERROR:
46 case RAPTOR_LOG_LEVEL_FATAL:
47 KB_ERROR(
"[raptor] {}", message->text);
49 case RAPTOR_LOG_LEVEL_WARN:
50 KB_WARN(
"[raptor] {}", message->text);
52 case RAPTOR_LOG_LEVEL_NONE:
53 case RAPTOR_LOG_LEVEL_TRACE:
54 case RAPTOR_LOG_LEVEL_DEBUG:
55 case RAPTOR_LOG_LEVEL_INFO:
64 raptor_parser_set_namespace_handler(
parser_,
nullptr, procesNamespace);
66 if (fs::exists(fileURI)) {
68 auto escapedString = raptor_uri_filename_to_uri_string(fileURI.data());
69 uri_ = raptor_new_uri(
world_, (
unsigned char *) escapedString);
73 raptor_free_memory(escapedString);
76 uri_ = raptor_new_uri(
world_, (
const unsigned char *) fileURI.data());
86 raptor_free_uri(
uri_);
92 auto world = raptor_new_world();
94 raptor_world_set_log_handler(world,
nullptr, raptor_log);
95 if (raptor_world_open(world) != 0) {
107 if (
frame_->confidence.has_value()) {
111 if (
frame_->perspective) {
120 if (
frame_->begin.has_value()) {
123 if (
frame_->end.has_value()) {
133 KB_WARN(
"No origin set for ontology parser, falling back to \"user\" origin.");
147 if (triple &&
owl::imports->stringForm() == triple->predicate()) {
148 imports_.emplace_back(triple->valueAsString());
170 RaptorUserData userData = {
const_cast<OntologyParser *
>(
this), callback};
171 raptor_parser_set_statement_handler(
parser_, &userData, processTriple);
174 raptor_world_set_generate_bnodeid_parameters(
175 raptor_parser_get_world(
parser_),
179 if (exit_status == 0) {
184 return (exit_status == 0);
static uint32_t batchSize()
static constexpr std::string_view ORIGIN_USER
raptor_parser * createParser(knowrob::semweb::TripleFormat format)
OntologyParser(const std::string_view &fileURI, semweb::TripleFormat format)
void flush(const TripleHandler &callback)
bool run(const TripleHandler &callback)
std::function< int()> doParse_
static raptor_world * createWorld()
std::vector< std::string > imports_
void applyFrame(Triple *triple)
void add(raptor_statement *statement, const TripleHandler &callback)
std::shared_ptr< RaptorContainer > currentBatch_
static void registerPrefix(std::string_view prefix, std::string_view uri)
void setConfidence(double confidence)
virtual void setPerspective(std::string_view perspective)=0
void setIsUncertain(bool isUncertain)
void setIsOccasional(bool isOccasional)
void setBegin(double begin)
std::string_view tripleFormatMimeType(TripleFormat format)
std::function< void(const TripleContainerPtr &)> TripleHandler