8 #include "knowrob/semweb/TripleFormatter.h"
9 #include "knowrob/semweb/PrefixRegistry.h"
10 #include "knowrob/Logger.h"
28 static void ensureDirectoryExists(
const std::string &filename) {
29 std::filesystem::path path(filename);
30 if (path.has_parent_path()) {
31 std::filesystem::create_directories(path.parent_path());
38 ensureDirectoryExists(filename);
40 std::ofstream file(filename);
41 if (!file.is_open()) {
42 KB_WARN(
"Could not open file {} for writing.", filename);
46 file <<
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
47 file <<
"<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n";
48 file <<
" xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\"\n";
49 file <<
" xmlns:owl=\"http://www.w3.org/2002/07/owl#\"\n";
52 file <<
" xmlns:" <<
prefix <<
"=\"" << uri <<
"\"\n";
57 for (
const auto &[subject,exportedTriples]: triples) {
58 file <<
" <rdf:Description rdf:about=\"" << subject <<
"\">\n";
59 for (
auto &triple : exportedTriples) {
60 auto property = triple->predicate();
61 auto valueString = triple->createStringValue();
62 auto valueType = triple->xsdType();
63 if (triple->isXSDLiteral()) {
65 file <<
" <" <<
property <<
" rdf:datatype=\"" <<
xsdTypeToIRI(valueType.value()) <<
"\">";
66 file << valueString <<
"</" <<
property <<
">\n";
67 }
else if (triple->isObjectIRI()) {
69 file <<
" <" <<
property <<
" rdf:resource=\"" << valueString <<
"\"/>\n";
72 file <<
" <" <<
property <<
">" << valueString <<
"</" <<
property <<
">\n";
75 file <<
" </rdf:Description>\n";
78 file <<
"</rdf:RDF>\n";
86 ensureDirectoryExists(filename);
88 std::ofstream file(filename);
89 if (!file.is_open()) {
90 KB_WARN(
"Could not open file {} for writing.", filename);
94 file <<
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n";
95 file <<
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
96 file <<
"@prefix owl: <http://www.w3.org/2002/07/owl#> .\n";
99 file <<
"@prefix " <<
prefix <<
": <" << uri <<
"> .\n";
102 for (
const auto &[subject, exportedTriples]: triples) {
103 file << subject <<
" ";
105 for (
auto &triple : exportedTriples) {
106 auto property = triple->predicate();
107 auto valueString = triple->createStringValue();
108 auto valueType = triple->xsdType();
113 if (triple->isXSDLiteral()) {
115 file <<
property <<
" \"" << valueString <<
"\"^^<" <<
xsdTypeToIRI(valueType.value()) <<
"> ";
116 }
else if (triple->isObjectIRI()) {
118 file <<
property <<
" <" << valueString <<
"> ";
121 file <<
property <<
" \"" << valueString <<
"\" ";
static PrefixRegistry & get()
constexpr std::string_view prefix
std::string_view tripleFormatToString(TripleFormat format)
std::map< std::string_view, std::vector< std::shared_ptr< Triple > >> ExportedTriples
std::string_view xsdTypeToIRI(XSDType type)