knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
Resource.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/Resource.h"
7 #include "knowrob/terms/IRIAtom.h"
8 #include "knowrob/terms/Blank.h"
9 #include "knowrob/Logger.h"
10 #include "knowrob/knowrob.h"
11 #include <boost/uuid/uuid_generators.hpp>
12 #include <boost/uuid/uuid_io.hpp>
13 #include <iomanip>
14 #include "knowrob/integration/python/utils.h"
15 #include "knowrob/semweb/ImportHierarchy.h"
16 
17 using namespace knowrob::semweb;
18 
19 bool Resource::AtomComparator::operator()(const AtomPtr &lhs, const AtomPtr &rhs) const {
20  return lhs->stringForm() < rhs->stringForm();
21 }
22 
23 Resource::Resource(std::string_view iri) {
24  switch (rdfNodeTypeGuess(iri)) {
25  case RDFNodeType::BLANK:
27  break;
28  case RDFNodeType::IRI:
30  break;
32  KB_WARN("Resource created with guessed literal type: {}. Treating as IRI.", iri);
34  break;
35  }
36 }
37 
38 knowrob::IRIAtomPtr Resource::unique_iri(std::string_view ns, std::string_view name) {
39  std::stringstream ss;
40  ss << ns;
41  if (!ns.empty() && ns.back() != '#') {
42  ss << "#";
43  }
44  ss << name << '_';
45  insertUnique(ss);
46  return IRIAtom::Tabled(ss.str());
47 }
48 
49 knowrob::IRIAtomPtr Resource::unique_iri(std::string_view type_iri) {
50  std::stringstream ss;
51  ss << type_iri;
52  ss << '_';
53  insertUnique(ss);
54  return IRIAtom::Tabled(ss.str());
55 }
56 
57 std::string_view Resource::iri_name(std::string_view iri) {
58  auto pos = iri.find('#');
59  if (pos != std::string::npos) {
60  return iri.substr(pos + 1);
61  }
62  return {iri.data()};
63 }
64 
65 std::string_view Resource::name() const {
66  return iri_name(iri_->stringForm());
67 }
68 
69 std::string_view Resource::iri_ns(std::string_view iri, bool includeDelimiter) {
70  auto pos = iri.rfind('#');
71  if (pos != std::string::npos) {
72  auto pos_x = (includeDelimiter ? pos + 1 : pos);
73  return iri.substr(0, pos_x);
74  }
75  return {};
76 }
77 
78 std::string_view Resource::ns(bool includeDelimiter) const {
79  return iri_ns(iri_->stringForm(), includeDelimiter);
80 }
81 
82 knowrob::AtomPtr Resource::graph_atom(std::optional<std::string_view> graph) {
83  return graph ? IRIAtom::Tabled(graph.value()) : IRIAtom::Tabled(ImportHierarchy::ORIGIN_SESSION);
84 }
85 
86 namespace knowrob::py {
87  template<>
88  void createType<semweb::Resource>() {
89  using namespace boost::python;
90 
91  using IRIArg1 = IRIAtomPtr (*)(std::string_view);
92  using IRIArg2 = IRIAtomPtr (*)(std::string_view, std::string_view);
93 
94  class_<semweb::Resource, std::shared_ptr<semweb::Resource>, boost::noncopyable>
95  ("Resource", no_init)
96  .def("iri", &semweb::Resource::iri)
97  .def("iriAtom", &semweb::Resource::iriAtom)
98  .def("name", &semweb::Resource::name)
99  .def("ns", &semweb::Resource::ns)
100  .def("unique_iri", static_cast<IRIArg1>(&semweb::Resource::unique_iri))
101  .def("unique_iri", static_cast<IRIArg2>(&semweb::Resource::unique_iri))
102  .def("iri_name", &semweb::Resource::iri_name)
103  .def("iri_ns", &semweb::Resource::iri_ns);
104  }
105 }
#define KB_WARN
Definition: Logger.h:27
static std::shared_ptr< Blank > Tabled(std::string_view stringForm)
Definition: Blank.cpp:12
static std::shared_ptr< IRIAtom > Tabled(std::string_view stringForm)
Definition: IRIAtom.cpp:25
static constexpr std::string_view ORIGIN_SESSION
knowrob::AtomPtr iri_
Definition: Resource.h:79
auto iriAtom() const
Definition: Resource.h:31
static std::string_view iri_name(std::string_view iri)
Definition: Resource.cpp:57
std::string_view name() const
Definition: Resource.cpp:65
static IRIAtomPtr unique_iri(std::string_view ns, std::string_view name)
Definition: Resource.cpp:38
Resource(std::string_view iri)
Definition: Resource.cpp:23
std::string_view ns(bool includeDelimiter=false) const
Definition: Resource.cpp:78
static std::string_view iri_ns(std::string_view iri, bool includeDelimiter=false)
Definition: Resource.cpp:69
static AtomPtr graph_atom(std::optional< std::string_view > graph)
Definition: Resource.cpp:82
RDFNodeType rdfNodeTypeGuess(std::string_view str)
Definition: RDFNode.cpp:11
std::shared_ptr< Atom > AtomPtr
Definition: Atom.h:69
std::shared_ptr< IRIAtom > IRIAtomPtr
Definition: IRIAtom.h:57
void insertUnique(std::ostream &os)
Definition: knowrob.cpp:44
bool operator()(const AtomPtr &lhs, const AtomPtr &rhs) const
Definition: Resource.cpp:19