#include <TripleFormatter.h>
A class that is responsible for exporting triples to different formats.
Definition at line 21 of file TripleFormatter.h.
◆ exportRDF_XML() [1/2]
bool knowrob::semweb::TripleFormatter::exportRDF_XML |
( |
const ExportedTriples & |
triples, |
|
|
const std::string & |
filename |
|
) |
| |
|
static |
- Parameters
-
triples | a map of triples |
filename | the name of the file to export to |
- Returns
- true if the export was successful
Definition at line 35 of file TripleFormatter.cpp.
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";
static PrefixRegistry & get()
constexpr std::string_view prefix
std::string_view xsdTypeToIRI(XSDType type)
◆ exportRDF_XML() [2/2]
static bool knowrob::semweb::TripleFormatter::exportRDF_XML |
( |
const ExportedTriples & |
triples, |
|
|
const std::string & |
filename |
|
) |
| |
|
static |
- Parameters
-
triples | a map of triples |
filename | the name of the file to export to |
- Returns
- true if the export was successful
◆ exportTo() [1/2]
bool knowrob::semweb::TripleFormatter::exportTo |
( |
const ExportedTriples & |
triples, |
|
|
const std::string & |
filename, |
|
|
TripleFormat |
format = TripleFormat::RDF_XML |
|
) |
| |
|
static |
- Parameters
-
triples | a map of triples |
filename | the name of the file to export to |
format | the format of the output file |
- Returns
- true if the export was successful
Definition at line 12 of file TripleFormatter.cpp.
std::string_view tripleFormatToString(TripleFormat format)
◆ exportTo() [2/2]
static bool knowrob::semweb::TripleFormatter::exportTo |
( |
const ExportedTriples & |
triples, |
|
|
const std::string & |
filename, |
|
|
TripleFormat |
format = TripleFormat::RDF_XML |
|
) |
| |
|
static |
- Parameters
-
triples | a map of triples |
filename | the name of the file to export to |
format | the format of the output file |
- Returns
- true if the export was successful
◆ exportTurtle() [1/2]
bool knowrob::semweb::TripleFormatter::exportTurtle |
( |
const ExportedTriples & |
triples, |
|
|
const std::string & |
filename |
|
) |
| |
|
static |
- Parameters
-
triples | a map of triples |
filename | the name of the file to export to |
- Returns
- true if the export was successful
Definition at line 83 of file TripleFormatter.cpp.
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 <<
"\" ";
◆ exportTurtle() [2/2]
static bool knowrob::semweb::TripleFormatter::exportTurtle |
( |
const ExportedTriples & |
triples, |
|
|
const std::string & |
filename |
|
) |
| |
|
static |
- Parameters
-
triples | a map of triples |
filename | the name of the file to export to |
- Returns
- true if the export was successful
The documentation for this class was generated from the following files: