knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
OntologyFile.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 <filesystem>
7 #include <utility>
8 #include "knowrob/semweb/OntologyFile.h"
9 #include "knowrob/Logger.h"
10 #include "knowrob/semweb/OntologyParser.h"
11 
12 using namespace knowrob;
13 
14 OntologyFile::OntologyFile(VocabularyPtr vocabulary, const URI &uri, std::string_view format)
15  : OntologySource(uri, format),
16  vocabulary_(std::move(vocabulary)),
17  tripleFormat_(semweb::tripleFormatFromString(format)),
18  ontologyLanguage_(semweb::OntologyLanguage::OWL) {
19 }
20 
21 bool OntologyFile::load(const TripleHandler &callback) {
22  auto resolved = URI::resolve(uri());
23  auto newVersion = DataSource::getVersionFromURI(resolved);
24 
25  // some OWL files are downloaded compile-time via CMake,
26  // they are downloaded into owl/external e.g. there are SOMA.owl and DUL.owl.
27  auto external_path = std::filesystem::path("owl") / "external" /
28  std::filesystem::path(resolved).filename();
29  auto knowrob_path = std::filesystem::path("owl") /
30  std::filesystem::path(resolved).filename();
31  auto external_p = std::filesystem::path(URI::resolve(external_path.u8string()));
32  auto knowrob_p = std::filesystem::path(URI::resolve(knowrob_path.u8string()));
33  const std::string *importURI;
34  if (exists(external_p)) {
35  KB_DEBUG("Using downloaded ontology at '{}'.", external_p.u8string());
36  importURI = &external_p.native();
37  } else if (exists(knowrob_p)) {
38  KB_DEBUG("Using built-in ontology at '{}'.", knowrob_p.u8string());
39  importURI = &knowrob_p.native();
40  } else {
41  importURI = &resolved;
42  }
43 
44  KB_INFO("Loading ontology at '{}' with version "
45  "\"{}\" and origin \"{}\".", *importURI, newVersion, origin_);
46 
47  OntologyParser parser(*importURI, tripleFormat());
48  parser.setOrigin(origin_);
49  parser.setFrame(frame());
50  // filter is called for each triple, if it returns false, the triple is skipped
51  parser.setFilter([this](const Triple &triple) {
52  return !vocabulary_->isAnnotationProperty(triple.predicate());
53  });
54  // define a prefix for naming blank nodes
55  parser.setBlankPrefix(std::string("_") + origin_);
56  auto result = parser.run([&callback](const TripleContainerPtr &triples) {
57  callback(triples);
58  });
59  if (!result) {
60  KB_WARN("Failed to parse ontology {} ({})", *importURI, uri());
61  return false;
62  }
63  // remember owl:imports
64  setImports(parser.imports());
65 
66  return true;
67 }
#define KB_DEBUG
Definition: Logger.h:25
#define KB_INFO
Definition: Logger.h:26
#define KB_WARN
Definition: Logger.h:27
static std::string getVersionFromURI(const std::string &uriString)
Definition: DataSource.cpp:48
const auto & uri() const
Definition: DataSource.h:39
OntologyFile(VocabularyPtr vocabulary, const URI &uri, std::string_view format)
semweb::TripleFormat tripleFormat() const
Definition: OntologyFile.h:31
VocabularyPtr vocabulary_
Definition: OntologyFile.h:47
bool load(const TripleHandler &callback) override
auto & imports() const
bool run(const TripleHandler &callback)
void setFrame(const GraphSelectorPtr &frame)
void setOrigin(std::string_view origin)
void setBlankPrefix(const std::string_view &blankPrefix)
void setFilter(const TripleFilter &filter)
const auto & frame() const
void setImports(const std::vector< std::string > &imports)
virtual std::string_view predicate() const =0
static std::string resolve(const std::string_view &uriString)
Definition: URI.cpp:79
TermRule & string()
Definition: terms.cpp:63
TripleFormat tripleFormatFromString(std::string_view format)
std::shared_ptr< TripleContainer > TripleContainerPtr
std::function< void(const TripleContainerPtr &)> TripleHandler
std::shared_ptr< Vocabulary > VocabularyPtr
Definition: Vocabulary.h:233