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

#include <OntologyFile.h>

Inheritance diagram for knowrob::OntologyFile:
Collaboration diagram for knowrob::OntologyFile:

Public Member Functions

 OntologyFile (VocabularyPtr vocabulary, const URI &uri, std::string_view format)
 
semweb::TripleFormat tripleFormat () const
 
void setOntologyLanguage (semweb::OntologyLanguage language)
 
semweb::OntologyLanguage ontologyLanguage () const
 
bool load (const TripleHandler &callback) override
 
 OntologyFile (VocabularyPtr vocabulary, const URI &uri, std::string_view format)
 
semweb::TripleFormat tripleFormat () const
 
void setOntologyLanguage (semweb::OntologyLanguage language)
 
semweb::OntologyLanguage ontologyLanguage () const
 
bool load (const TripleHandler &callback) override
 
- Public Member Functions inherited from knowrob::OntologySource
 OntologySource (const URI &uri, std::string_view format)
 
virtual ~OntologySource ()=default
 
void setFrame (const GraphSelectorPtr &frame)
 
const auto & frame () const
 
void setParentOrigin (std::string_view parentOrigin)
 
auto & parentOrigin () const
 
std::string_view origin () const
 
const auto & imports () const
 
void setImports (const std::vector< std::string > &imports)
 
 OntologySource (const URI &uri, std::string_view format)
 
virtual ~OntologySource ()=default
 
void setFrame (const GraphSelectorPtr &frame)
 
const auto & frame () const
 
void setParentOrigin (std::string_view parentOrigin)
 
auto & parentOrigin () const
 
std::string_view origin () const
 
const auto & imports () const
 
void setImports (const std::vector< std::string > &imports)
 
- Public Member Functions inherited from knowrob::DataSource
 DataSource (URI uri, std::string_view format, DataSourceType dataSourceType)
 
const auto & uri () const
 
const auto & path () const
 
const auto & format () const
 
DataSourceType dataSourceType () const
 
auto name () const
 
auto version () const
 
 DataSource (URI uri, std::string_view format, DataSourceType dataSourceType)
 
const auto & uri () const
 
const auto & path () const
 
const auto & format () const
 
DataSourceType dataSourceType () const
 
auto name () const
 
auto version () const
 

Protected Attributes

VocabularyPtr vocabulary_
 
semweb::TripleFormat tripleFormat_
 
semweb::OntologyLanguage ontologyLanguage_
 
- Protected Attributes inherited from knowrob::OntologySource
GraphSelectorPtr frame_
 
std::optional< std::string > parentOrigin_
 
std::string origin_
 
std::vector< std::string > imports_
 
- Protected Attributes inherited from knowrob::DataSource
DataSourceType dataSourceType_
 
std::string format_
 
URI uri_
 

Additional Inherited Members

- Static Public Member Functions inherited from knowrob::DataSource
static std::string getNameFromURI (const std::string &uriString)
 
static std::string getVersionFromURI (const std::string &uriString)
 
static bool isVersionString (const std::string &versionString)
 
static std::shared_ptr< DataSourcecreate (const VocabularyPtr &vocabulary, const boost::property_tree::ptree &config)
 
static std::string getNameFromURI (const std::string &uriString)
 
static std::string getVersionFromURI (const std::string &uriString)
 
static bool isVersionString (const std::string &versionString)
 
static std::shared_ptr< DataSourcecreate (const VocabularyPtr &vocabulary, const boost::property_tree::ptree &config)
 

Detailed Description

An ontology file is a data source that provides ontology data in a file.

Definition at line 19 of file OntologyFile.h.

Constructor & Destructor Documentation

◆ OntologyFile() [1/2]

OntologyFile::OntologyFile ( VocabularyPtr  vocabulary,
const URI uri,
std::string_view  format 
)
Parameters
vocabularythe vocabulary of the ontology.
uriURI of the data source.
formatstring identifier of the data format.

Definition at line 14 of file OntologyFile.cpp.

16  vocabulary_(std::move(vocabulary)),
19 }
const auto & format() const
Definition: DataSource.h:49
const auto & uri() const
Definition: DataSource.h:39
VocabularyPtr vocabulary_
Definition: OntologyFile.h:47
semweb::OntologyLanguage ontologyLanguage_
Definition: OntologyFile.h:49
semweb::TripleFormat tripleFormat_
Definition: OntologyFile.h:48
OntologySource(const URI &uri, std::string_view format)
TripleFormat tripleFormatFromString(std::string_view format)

◆ OntologyFile() [2/2]

knowrob::OntologyFile::OntologyFile ( VocabularyPtr  vocabulary,
const URI uri,
std::string_view  format 
)
Parameters
vocabularythe vocabulary of the ontology.
uriURI of the data source.
formatstring identifier of the data format.

Member Function Documentation

◆ load() [1/2]

bool OntologyFile::load ( const TripleHandler callback)
overridevirtual

Load triples from the SPARQL endpoint.

Parameters
callbackthe callback to handle the triples.
Returns
true if the triples were loaded successfully.

Implements knowrob::OntologySource.

Definition at line 21 of file OntologyFile.cpp.

21  {
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
semweb::TripleFormat tripleFormat() const
Definition: OntologyFile.h:31
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
std::shared_ptr< TripleContainer > TripleContainerPtr

◆ load() [2/2]

bool knowrob::OntologyFile::load ( const TripleHandler callback)
overridevirtual

Load triples from the SPARQL endpoint.

Parameters
callbackthe callback to handle the triples.
Returns
true if the triples were loaded successfully.

Implements knowrob::OntologySource.

◆ ontologyLanguage() [1/2]

semweb::OntologyLanguage knowrob::OntologyFile::ontologyLanguage ( ) const
inline
Returns
the language of the ontology.

Definition at line 41 of file OntologyFile.h.

41 { return ontologyLanguage_; }

◆ ontologyLanguage() [2/2]

semweb::OntologyLanguage knowrob::OntologyFile::ontologyLanguage ( ) const
inline
Returns
the language of the ontology.

Definition at line 41 of file OntologyFile.h.

41 { return ontologyLanguage_; }

◆ setOntologyLanguage() [1/2]

void knowrob::OntologyFile::setOntologyLanguage ( semweb::OntologyLanguage  language)
inline
Parameters
languagethe language of the ontology.

Definition at line 36 of file OntologyFile.h.

36 { ontologyLanguage_ = language; }

◆ setOntologyLanguage() [2/2]

void knowrob::OntologyFile::setOntologyLanguage ( semweb::OntologyLanguage  language)
inline
Parameters
languagethe language of the ontology.

Definition at line 36 of file OntologyFile.h.

36 { ontologyLanguage_ = language; }

◆ tripleFormat() [1/2]

semweb::TripleFormat knowrob::OntologyFile::tripleFormat ( ) const
inline
Returns
the format of the triples in the file.

Definition at line 31 of file OntologyFile.h.

31 { return tripleFormat_; }

◆ tripleFormat() [2/2]

semweb::TripleFormat knowrob::OntologyFile::tripleFormat ( ) const
inline
Returns
the format of the triples in the file.

Definition at line 31 of file OntologyFile.h.

31 { return tripleFormat_; }

Member Data Documentation

◆ ontologyLanguage_

semweb::OntologyLanguage knowrob::OntologyFile::ontologyLanguage_
protected

Definition at line 49 of file OntologyFile.h.

◆ tripleFormat_

semweb::TripleFormat knowrob::OntologyFile::tripleFormat_
protected

Definition at line 48 of file OntologyFile.h.

◆ vocabulary_

VocabularyPtr knowrob::OntologyFile::vocabulary_
protected

Definition at line 47 of file OntologyFile.h.


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