knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
PrefixRegistry.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 "knowrob/semweb/PrefixRegistry.h"
7 #include "knowrob/semweb/PrefixProbe.h"
8 #include "knowrob/Logger.h"
9 
10 using namespace knowrob;
11 
12 PrefixRegistry::PrefixRegistry() {
13  registerPrefix_("owl", "http://www.w3.org/2002/07/owl");
14  registerPrefix_("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns");
15  registerPrefix_("rdfs", "http://www.w3.org/2000/01/rdf-schema");
16  registerPrefix_("xsd", "http://www.w3.org/2001/XMLSchema");
17  registerPrefix_("qudt", "http://data.nasa.gov/qudt/owl/qudt");
18  registerPrefix_("dul", "http://www.ontologydesignpatterns.org/ont/dul/DUL.owl");
19  registerPrefix_("soma", "http://www.ease-crc.org/ont/SOMA.owl");
20  registerPrefix_("knowrob", "http://knowrob.org/kb/knowrob.owl");
21 }
22 
24  static PrefixRegistry singleton;
25  return singleton;
26 }
27 
28 static inline bool isDelimiter(char c) {
29  return c == '#' || c == '/';
30 }
31 
32 void PrefixRegistry::registerPrefix_(std::string_view prefix, std::string_view uri) {
33  auto s_prefix = std::string(prefix.data());
34  auto s_uri = std::string(uri.data());
35  if (!isDelimiter(s_uri[s_uri.size() - 1])) {
36  // auto-insert delimiter if none is present
37  s_uri += '#';
38  }
39  KB_DEBUG("Registering prefix \"{}\" for URI {}", s_prefix, s_uri);
40  uriToAlias_[s_uri] = s_prefix;
41  aliasToURI_[s_prefix] = s_uri;
42 }
43 
44 void PrefixRegistry::registerPrefix(std::string_view prefix, std::string_view uri) {
45  get().registerPrefix_(prefix, uri);
46 }
47 
49  if (uri.empty()) {
50  return std::nullopt;
51  } else if (isDelimiter(uri[uri.size() - 1])) {
52  auto it = get().uriToAlias_.find(uri);
53  return it == get().uriToAlias_.end() ? std::nullopt : OptionalStringRef(it->second);
54  } else {
55  auto withDelimiter = std::string(uri) + '#';
56  auto it = get().uriToAlias_.find(withDelimiter);
57  return it == get().uriToAlias_.end() ? std::nullopt : OptionalStringRef(it->second);
58  }
59 }
60 
62  if (alias.empty()) {
63  return std::nullopt;
64  } else {
65  auto it = get().aliasToURI_.find(alias);
66  return it == get().aliasToURI_.end() ? std::nullopt : OptionalStringRef(it->second);
67  }
68 }
69 
70 std::optional<std::string> PrefixRegistry::createIRI(std::string_view alias, std::string_view entityName) {
71  auto uri = aliasToUri(alias);
72  if (uri.has_value()) {
73  std::stringstream os;
74  os << uri.value().get() << entityName;
75  return os.str();
76  } else {
77  return std::nullopt;
78  }
79 }
80 
81 std::vector<std::string_view> PrefixRegistry::getAliasesWithPrefix(std::string_view prefix) {
82  auto range_it = get().aliasToURI_.equal_range(PrefixProbe{prefix});
83  std::vector<std::string_view> result;
84  for (auto it = range_it.first; it != range_it.second; ++it) {
85  result.emplace_back(it->first.c_str());
86  }
87  return result;
88 }
#define KB_DEBUG
Definition: Logger.h:25
static std::vector< std::string_view > getAliasesWithPrefix(std::string_view prefix)
static void registerPrefix(std::string_view prefix, std::string_view uri)
static OptionalStringRef aliasToUri(std::string_view alias)
static OptionalStringRef uriToAlias(std::string_view uri)
static PrefixRegistry & get()
static std::optional< std::string > createIRI(std::string_view alias, std::string_view entityName)
constexpr std::string_view prefix
Definition: owl.h:14
TermRule & string()
Definition: terms.cpp:63
std::optional< std::reference_wrapper< const std::string > > OptionalStringRef