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

#include <DataSource.h>

Inheritance diagram for knowrob::DataSource:
Collaboration diagram for knowrob::DataSource:

Public Member Functions

 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
 

Static Public Member Functions

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)
 

Protected Attributes

DataSourceType dataSourceType_
 
std::string format_
 
URI uri_
 

Detailed Description

A data source is a source of data that can be loaded into a subsystem of the knowledge base. This can be a file, a database, a web service, etc.

Definition at line 26 of file DataSource.h.

Constructor & Destructor Documentation

◆ DataSource() [1/2]

DataSource::DataSource ( URI  uri,
std::string_view  format,
DataSourceType  dataSourceType 
)

Create a data source.

Parameters
uriURI of the data source.
formatstring identifier of the data format.
dataSourceTypethe type of the data source.

Definition at line 30 of file DataSource.cpp.

32  format_(format),
33  uri_(std::move(uri)) {
34 }
DataSourceType dataSourceType_
Definition: DataSource.h:101
const auto & format() const
Definition: DataSource.h:49
const auto & uri() const
Definition: DataSource.h:39
DataSourceType dataSourceType() const
Definition: DataSource.h:54
std::string format_
Definition: DataSource.h:102

◆ DataSource() [2/2]

knowrob::DataSource::DataSource ( URI  uri,
std::string_view  format,
DataSourceType  dataSourceType 
)

Create a data source.

Parameters
uriURI of the data source.
formatstring identifier of the data format.
dataSourceTypethe type of the data source.

Member Function Documentation

◆ create() [1/2]

DataSourcePtr DataSource::create ( const VocabularyPtr vocabulary,
const boost::property_tree::ptree &  config 
)
static

Create a data source from a configuration.

Parameters
vocabularythe vocabulary of the knowledge base.
configa property tree used to configure this.
Returns
a data source

Definition at line 105 of file DataSource.cpp.

105  {
106  static const std::string formatDefault = {};
107 
108  // read data source settings
109  URI dataSourceURI(config);
110  auto dataSourceFormat = config.get(DATA_SOURCE_SETTING_FORMAT, formatDefault);
111  auto o_dataSourceLanguage = config.get_optional<std::string>(DATA_SOURCE_SETTING_LANG);
112  auto o_type = config.get_optional<std::string>(DATA_SOURCE_SETTING_TYPE);
113  auto isOntology = isOntologySourceType(dataSourceFormat, o_dataSourceLanguage, o_type);
114  // an optional frame can be applied to all triples in a data source
115  auto o_tripleFrame = config.get_child_optional(DATA_SOURCE_SETTING_FRAME);
116  std::shared_ptr<GraphSelector> tripleFrame;
117  if (o_tripleFrame) {
118  tripleFrame = std::make_shared<GraphSelector>();
119  tripleFrame->set(*o_tripleFrame);
120  }
121 
122  if (isOntology && o_type && o_type.value() == DATA_SOURCE_TYPE_SPARQL) {
123  auto sparqlService = std::make_shared<SPARQLService>(dataSourceURI, dataSourceFormat);
124  if (tripleFrame) {
125  sparqlService->setFrame(tripleFrame);
126  }
127  return sparqlService;
128  } else if (isOntology) {
129  auto ontoFile = std::make_shared<OntologyFile>(vocabulary, dataSourceURI, dataSourceFormat);
130  if (o_dataSourceLanguage.has_value()) {
131  ontoFile->setOntologyLanguage(semweb::ontologyLanguageFromString(o_dataSourceLanguage.value()));
132  }
133  if (tripleFrame) {
134  ontoFile->setFrame(tripleFrame);
135  }
136  return ontoFile;
137  } else {
138  return std::make_shared<DataSource>(dataSourceURI, dataSourceFormat, DataSourceType::UNSPECIFIED);
139  }
140 }
#define DATA_SOURCE_SETTING_LANG
Definition: DataSource.cpp:18
#define DATA_SOURCE_SETTING_TYPE
Definition: DataSource.cpp:19
#define DATA_SOURCE_SETTING_FORMAT
Definition: DataSource.cpp:17
#define DATA_SOURCE_TYPE_SPARQL
Definition: DataSource.cpp:22
#define DATA_SOURCE_SETTING_FRAME
Definition: DataSource.cpp:20
TermRule & string()
Definition: terms.cpp:63
OntologyLanguage ontologyLanguageFromString(std::string_view format)

◆ create() [2/2]

static std::shared_ptr<DataSource> knowrob::DataSource::create ( const VocabularyPtr vocabulary,
const boost::property_tree::ptree &  config 
)
static

Create a data source from a configuration.

Parameters
vocabularythe vocabulary of the knowledge base.
configa property tree used to configure this.
Returns
a data source

◆ dataSourceType() [1/2]

DataSourceType knowrob::DataSource::dataSourceType ( ) const
inline
Returns
the type of the data source.

Definition at line 54 of file DataSource.h.

54 { return dataSourceType_; }

◆ dataSourceType() [2/2]

DataSourceType knowrob::DataSource::dataSourceType ( ) const
inline
Returns
the type of the data source.

Definition at line 54 of file DataSource.h.

54 { return dataSourceType_; }

◆ format() [1/2]

const auto& knowrob::DataSource::format ( ) const
inline
Returns
string identifier of the data format.

Definition at line 49 of file DataSource.h.

49 { return format_; }

◆ format() [2/2]

const auto& knowrob::DataSource::format ( ) const
inline
Returns
string identifier of the data format.

Definition at line 49 of file DataSource.h.

49 { return format_; }

◆ getNameFromURI() [1/2]

std::string DataSource::getNameFromURI ( const std::string &  uriString)
static

Ontologies are loaded into named sub-graphs of the knowledge graph. The name is generated from the URI in case of loading RDF files.

Parameters
uriStringA URI pointing to a RDF ontology.
Returns
a graph name for the ontology

Definition at line 36 of file DataSource.cpp.

36  {
37  return fs::path(uriString).stem();
38 }

◆ getNameFromURI() [2/2]

static std::string knowrob::DataSource::getNameFromURI ( const std::string &  uriString)
static

Ontologies are loaded into named sub-graphs of the knowledge graph. The name is generated from the URI in case of loading RDF files.

Parameters
uriStringA URI pointing to a RDF ontology.
Returns
a graph name for the ontology

◆ getVersionFromURI() [1/2]

std::string DataSource::getVersionFromURI ( const std::string &  uriString)
static

Extract a version string from an ontology URI. In case the URI points to a local file, the modification time of the file is used as version. For other URIs it is attempted to extract version information from the URI string, if this fails, then the current day is used as a version string.

Parameters
uriStringA URI pointing to a RDF ontology.
Returns
a version string

Definition at line 48 of file DataSource.cpp.

48  {
49  fs::path p(uriString);
50 
51  // check if it is a local existing file and use file modification time
52  // as version in this case.
53  if (exists(p)) {
54  std::ostringstream oss;
55  auto stamp = last_write_time(p);
56  auto tt = to_time_t(stamp);
57  auto tm = *std::localtime(&tt);
58  oss << std::put_time(&tm, "%c");
59  return oss.str();
60  }
61 
62  // try to extract version from URI
63  auto versionString = p.parent_path().filename();
64  if (isVersionString(versionString)) {
65  return versionString;
66  }
67 
68  // fallback to use the current day as version, thus causing
69  // a reload each day.
70  {
71  std::ostringstream oss;
72  auto t = std::time(nullptr);
73  auto tm = *std::localtime(&t);
74  oss << std::put_time(&tm, "%d-%m-%Y");
75  return oss.str();
76  }
77 }
std::time_t to_time_t(TP tp)
Definition: DataSource.cpp:41
static bool isVersionString(const std::string &versionString)
Definition: DataSource.cpp:79

◆ getVersionFromURI() [2/2]

static std::string knowrob::DataSource::getVersionFromURI ( const std::string &  uriString)
static

Extract a version string from an ontology URI. In case the URI points to a local file, the modification time of the file is used as version. For other URIs it is attempted to extract version information from the URI string, if this fails, then the current day is used as a version string.

Parameters
uriStringA URI pointing to a RDF ontology.
Returns
a version string

◆ isVersionString() [1/2]

bool DataSource::isVersionString ( const std::string &  versionString)
static
Parameters
versionStringa string
Returns
true if versionString is a valid version string

Definition at line 79 of file DataSource.cpp.

79  {
80  // parser rules
81  qi::rule<std::string::const_iterator> numbers = (
82  (+qi::digit >> '.' >> +qi::digit >> '.' >> +qi::digit) |
83  (+qi::digit >> '.' >> +qi::digit));
84  // parse input
85  auto first = versionString.begin();
86  auto last = versionString.end();
87  bool r = qi::phrase_parse(first,
88  last,
89  (('v' >> numbers) | numbers),
90  ascii::space);
91  // return true if parser succeeded
92  return (first == last && r);
93 }

◆ isVersionString() [2/2]

static bool knowrob::DataSource::isVersionString ( const std::string &  versionString)
static
Parameters
versionStringa string
Returns
true if versionString is a valid version string

◆ name() [1/2]

auto knowrob::DataSource::name ( ) const
inline
Returns
the name of the data source.

Definition at line 59 of file DataSource.h.

59 { return getNameFromURI(uri()); }
static std::string getNameFromURI(const std::string &uriString)
Definition: DataSource.cpp:36

◆ name() [2/2]

auto knowrob::DataSource::name ( ) const
inline
Returns
the name of the data source.

Definition at line 59 of file DataSource.h.

59 { return getNameFromURI(uri()); }

◆ path() [1/2]

const auto& knowrob::DataSource::path ( ) const
inline
Returns
string identifier of the data format.

Definition at line 44 of file DataSource.h.

44 { return uri_.path(); }
const std::string & path() const
Definition: URI.h:57

◆ path() [2/2]

const auto& knowrob::DataSource::path ( ) const
inline
Returns
string identifier of the data format.

Definition at line 44 of file DataSource.h.

44 { return uri_.path(); }

◆ uri() [1/2]

const auto& knowrob::DataSource::uri ( ) const
inline
Returns
URI of this data source.

Definition at line 39 of file DataSource.h.

39 { return uri_(); }

◆ uri() [2/2]

const auto& knowrob::DataSource::uri ( ) const
inline
Returns
URI of this data source.

Definition at line 39 of file DataSource.h.

39 { return uri_(); }

◆ version() [1/2]

auto knowrob::DataSource::version ( ) const
inline
Returns
the version of the data source.

Definition at line 64 of file DataSource.h.

64 { return getVersionFromURI(uri()); }
static std::string getVersionFromURI(const std::string &uriString)
Definition: DataSource.cpp:48

◆ version() [2/2]

auto knowrob::DataSource::version ( ) const
inline
Returns
the version of the data source.

Definition at line 64 of file DataSource.h.

64 { return getVersionFromURI(uri()); }

Member Data Documentation

◆ dataSourceType_

DataSourceType knowrob::DataSource::dataSourceType_
protected

Definition at line 101 of file DataSource.h.

◆ format_

std::string knowrob::DataSource::format_
protected

Definition at line 102 of file DataSource.h.

◆ uri_

URI knowrob::DataSource::uri_
protected

Definition at line 103 of file DataSource.h.


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