knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
TripleFormatter.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of KnowRob, please consult
3  * https://github.com/knowrob/knowrob for license details.
4  */
5 
6 #include <filesystem>
7 #include <fstream>
8 #include "knowrob/semweb/TripleFormatter.h"
9 #include "knowrob/semweb/PrefixRegistry.h"
10 #include "knowrob/Logger.h"
11 
13  const ExportedTriples &triples,
14  const std::string &filename,
15  TripleFormat format) {
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 }
27 
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());
32  }
33 }
34 
36  const ExportedTriples &triples,
37  const std::string &filename) {
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 }
82 
84  const ExportedTriples &triples,
85  const std::string &filename) {
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 }
#define KB_WARN
Definition: Logger.h:27
static PrefixRegistry & get()
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)
constexpr std::string_view prefix
Definition: owl.h:14
TermRule & string()
Definition: terms.cpp:63
std::string_view tripleFormatToString(TripleFormat format)
std::map< std::string_view, std::vector< std::shared_ptr< Triple > >> ExportedTriples
std::string_view xsdTypeToIRI(XSDType type)
Definition: XSDAtomic.cpp:70