8 #include <boost/spirit/include/qi.hpp>
9 #include <boost/spirit/include/phoenix_operator.hpp>
10 #include "knowrob/integration/python/utils.h"
11 #include "knowrob/DataSource.h"
12 #include "knowrob/semweb/OntologyLanguage.h"
13 #include "knowrob/semweb/OntologyFile.h"
14 #include "knowrob/semweb/SPARQLService.h"
15 #include "knowrob/semweb/GraphSelector.h"
17 #define DATA_SOURCE_SETTING_FORMAT "format"
18 #define DATA_SOURCE_SETTING_LANG "language"
19 #define DATA_SOURCE_SETTING_TYPE "type"
20 #define DATA_SOURCE_SETTING_FRAME "frame"
22 #define DATA_SOURCE_TYPE_SPARQL "sparql"
23 #define DATA_SOURCE_TYPE_ONTOLOGY "ontology"
26 namespace fs = std::filesystem;
27 namespace qi = boost::spirit::qi;
28 namespace ascii = boost::spirit::ascii;
31 : dataSourceType_(dataSourceType),
33 uri_(std::move(uri)) {
37 return fs::path(uriString).stem();
43 using namespace std::chrono;
49 fs::path p(uriString);
54 std::ostringstream oss;
55 auto stamp = last_write_time(p);
57 auto tm = *std::localtime(&tt);
58 oss << std::put_time(&tm,
"%c");
63 auto versionString = p.parent_path().filename();
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");
81 qi::rule<std::string::const_iterator> numbers = (
82 (+qi::digit >>
'.' >> +qi::digit >>
'.' >> +qi::digit) |
83 (+qi::digit >>
'.' >> +qi::digit));
85 auto first = versionString.begin();
86 auto last = versionString.end();
87 bool r = qi::phrase_parse(first,
89 ((
'v' >> numbers) | numbers),
92 return (first == last && r);
95 static bool isOntologySourceType(
97 const boost::optional<std::string> &language,
98 const boost::optional<std::string> &
type) {
109 URI dataSourceURI(config);
113 auto isOntology = isOntologySourceType(dataSourceFormat, o_dataSourceLanguage, o_type);
116 std::shared_ptr<GraphSelector> tripleFrame;
118 tripleFrame = std::make_shared<GraphSelector>();
119 tripleFrame->set(*o_tripleFrame);
123 auto sparqlService = std::make_shared<SPARQLService>(dataSourceURI, dataSourceFormat);
125 sparqlService->setFrame(tripleFrame);
127 return sparqlService;
128 }
else if (isOntology) {
129 auto ontoFile = std::make_shared<OntologyFile>(vocabulary, dataSourceURI, dataSourceFormat);
130 if (o_dataSourceLanguage.has_value()) {
134 ontoFile->setFrame(tripleFrame);
146 class_<DataSource, std::shared_ptr<DataSource>>
147 (
"DataSource", no_init)
149 .def(
"uri", &
DataSource::uri, return_value_policy<copy_const_reference>())
150 .def(
"path", &
DataSource::path, return_value_policy<copy_const_reference>())
155 .staticmethod(
"isVersionString")
157 .staticmethod(
"getNameFromURI")
159 .staticmethod(
"getVersionFromURI")
161 .staticmethod(
"create");
#define DATA_SOURCE_SETTING_LANG
#define DATA_SOURCE_SETTING_TYPE
#define DATA_SOURCE_TYPE_ONTOLOGY
#define DATA_SOURCE_SETTING_FORMAT
#define DATA_SOURCE_TYPE_SPARQL
#define DATA_SOURCE_SETTING_FRAME
std::time_t to_time_t(TP tp)
DataSource(URI uri, std::string_view format, DataSourceType dataSourceType)
static std::string getNameFromURI(const std::string &uriString)
static std::string getVersionFromURI(const std::string &uriString)
const auto & path() const
const auto & format() const
DataSourceType dataSourceType() const
static bool isVersionString(const std::string &versionString)
static std::shared_ptr< DataSource > create(const VocabularyPtr &vocabulary, const boost::property_tree::ptree &config)
void createType< DataSource >()
bool isTripleFormatString(std::string_view format)
bool isOntologyLanguageString(std::string_view format)
OntologyLanguage ontologyLanguageFromString(std::string_view format)
std::shared_ptr< DataSource > DataSourcePtr
std::shared_ptr< Vocabulary > VocabularyPtr