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

#include <TripleFormatter.h>

Static Public Member Functions

static bool exportTo (const ExportedTriples &triples, const std::string &filename, TripleFormat format=TripleFormat::RDF_XML)
 
static bool exportRDF_XML (const ExportedTriples &triples, const std::string &filename)
 
static bool exportTurtle (const ExportedTriples &triples, const std::string &filename)
 
static bool exportTo (const ExportedTriples &triples, const std::string &filename, TripleFormat format=TripleFormat::RDF_XML)
 
static bool exportRDF_XML (const ExportedTriples &triples, const std::string &filename)
 
static bool exportTurtle (const ExportedTriples &triples, const std::string &filename)
 

Detailed Description

A class that is responsible for exporting triples to different formats.

Definition at line 21 of file TripleFormatter.h.

Member Function Documentation

◆ exportRDF_XML() [1/2]

bool knowrob::semweb::TripleFormatter::exportRDF_XML ( const ExportedTriples triples,
const std::string &  filename 
)
static
Parameters
triplesa map of triples
filenamethe name of the file to export to
Returns
true if the export was successful

Definition at line 35 of file TripleFormatter.cpp.

37  {
38  ensureDirectoryExists(filename);
39  // open file for writing, overwrite if it exists
40  std::ofstream file(filename);
41  if (!file.is_open()) {
42  KB_WARN("Could not open file {} for writing.", filename);
43  return false;
44  }
45  // write RDF/XML header
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";
50  // iterate over known namespaces
51  for (const auto &[uri, prefix]: PrefixRegistry::get()) {
52  file << " xmlns:" << prefix << "=\"" << uri << "\"\n";
53  }
54  file << ">\n";
55 
56  // write triples
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()) {
64  // XSD property assertion
65  file << " <" << property << " rdf:datatype=\"" << xsdTypeToIRI(valueType.value()) << "\">";
66  file << valueString << "</" << property << ">\n";
67  } else if (triple->isObjectIRI()) {
68  // object property assertion
69  file << " <" << property << " rdf:resource=\"" << valueString << "\"/>\n";
70  } else {
71  // untyped literal
72  file << " <" << property << ">" << valueString << "</" << property << ">\n";
73  }
74  }
75  file << " </rdf:Description>\n";
76  }
77  // write RDF/XML footer
78  file << "</rdf:RDF>\n";
79  file.close();
80  return true;
81 }
#define KB_WARN
Definition: Logger.h:27
static PrefixRegistry & get()
constexpr std::string_view prefix
Definition: owl.h:14
std::string_view xsdTypeToIRI(XSDType type)
Definition: XSDAtomic.cpp:70

◆ exportRDF_XML() [2/2]

static bool knowrob::semweb::TripleFormatter::exportRDF_XML ( const ExportedTriples triples,
const std::string &  filename 
)
static
Parameters
triplesa map of triples
filenamethe 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
triplesa map of triples
filenamethe name of the file to export to
formatthe format of the output file
Returns
true if the export was successful

Definition at line 12 of file TripleFormatter.cpp.

15  {
16  switch (format) {
18  return exportRDF_XML(triples, filename);
20  return exportTurtle(triples, filename);
21  default:
22  // Unsupported format
23  KB_WARN("Unsupported format: {}", tripleFormatToString(format));
24  return false;
25  }
26 }
static bool exportTurtle(const ExportedTriples &triples, const std::string &filename)
static bool exportRDF_XML(const ExportedTriples &triples, const std::string &filename)
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
triplesa map of triples
filenamethe name of the file to export to
formatthe 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
triplesa map of triples
filenamethe name of the file to export to
Returns
true if the export was successful

Definition at line 83 of file TripleFormatter.cpp.

85  {
86  ensureDirectoryExists(filename);
87  // open file for writing, overwrite if it exists
88  std::ofstream file(filename);
89  if (!file.is_open()) {
90  KB_WARN("Could not open file {} for writing.", filename);
91  return false;
92  }
93  // write Turtle header
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";
97  // iterate over known namespaces
98  for (const auto &[uri, prefix]: PrefixRegistry::get()) {
99  file << "@prefix " << prefix << ": <" << uri << "> .\n";
100  }
101  // write triples
102  for (const auto &[subject, exportedTriples]: triples) {
103  file << subject << " ";
104  bool isFirst = true;
105  for (auto &triple : exportedTriples) {
106  auto property = triple->predicate();
107  auto valueString = triple->createStringValue();
108  auto valueType = triple->xsdType();
109  if (!isFirst) {
110  file << ";\n ";
111  }
112  isFirst = false;
113  if (triple->isXSDLiteral()) {
114  // XSD property assertion
115  file << property << " \"" << valueString << "\"^^<" << xsdTypeToIRI(valueType.value()) << "> ";
116  } else if (triple->isObjectIRI()) {
117  // object property assertion
118  file << property << " <" << valueString << "> ";
119  } else {
120  // untyped literal
121  file << property << " \"" << valueString << "\" ";
122  }
123  }
124  file << ".\n"; // end of the subject block
125  }
126  // write Turtle footer
127  file << "\n";
128  file.close();
129  return true;
130 }

◆ exportTurtle() [2/2]

static bool knowrob::semweb::TripleFormatter::exportTurtle ( const ExportedTriples triples,
const std::string &  filename 
)
static
Parameters
triplesa map of triples
filenamethe 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: