knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
IRIAtom.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 <boost/python.hpp>
7 #include "knowrob/terms/IRIAtom.h"
8 #include "knowrob/integration/python/utils.h"
9 #include "knowrob/semweb/Resource.h"
10 #include "knowrob/semweb/PrefixRegistry.h"
11 #include "knowrob/Logger.h"
12 
13 using namespace knowrob;
14 
15 PredicatePtr IRIAtom::operator()(const TermPtr &s, const TermPtr &o) const {
16  auto functor = IRIAtom::Tabled(stringForm());
17  return std::make_shared<Predicate>(functor, std::vector<TermPtr>{s, o});
18 }
19 
21  auto functor = IRIAtom::Tabled(stringForm());
22  return std::make_shared<Predicate>(functor, std::vector<TermPtr>{s});
23 }
24 
25 std::shared_ptr<knowrob::IRIAtom> IRIAtom::Tabled(std::string_view name) {
26  auto it = Atom::table().find(name);
27  if (it != table().end()) {
28  if (auto atomPtr = it->second.value().lock()) {
29  if (atomPtr->isIRI()) {
30  return std::static_pointer_cast<IRIAtom>(atomPtr);
31  }
32  }
33  }
34  // Atom does not exist or was destroyed, create a new one
35  auto inserted = table().emplace(name, std::nullopt);
36  auto &jt = inserted.first;
37  auto atom = std::make_shared<knowrob::IRIAtom>(jt->first);
38  jt->second = atom;
39  auto locked = jt->second.value().lock();
40  if (!locked) {
41  throw std::runtime_error("Failed to lock IRIAtom");
42  }
43  return std::static_pointer_cast<IRIAtom>(locked);
44 }
45 
46 void IRIAtom::write(std::ostream &os) const {
48  if (!ns.empty()) {
49  auto alias = PrefixRegistry::uriToAlias(ns);
50  if (alias.has_value()) {
52  if (!name.empty()) {
53  os << alias.value().get() << ":" << name;
54  return;
55  }
56  }
57  }
58  Atom::write(os);
59 }
60 
61 namespace knowrob {
62  IRIAtomPtr iri(std::string_view ns, std::string_view name) {
63  auto o_iri = PrefixRegistry::createIRI(ns, name);
64  if (o_iri.has_value()) {
65  return IRIAtom::Tabled(o_iri.value());
66  } else {
67  KB_WARN("Failed to create IRI from namespace \"{}\" and name {}.", ns, name);
68  std::string fallback = std::string(ns) + "/" + std::string(name);
69  return IRIAtom::Tabled(fallback);
70  }
71  }
72 }
73 
74 namespace knowrob::py {
75  template<>
77  using namespace boost::python;
78  class_<IRIAtom, std::shared_ptr<IRIAtom>, bases<Atom, RDFNode>>("IRIAtom", no_init)
79  .def("__init__", make_constructor(&IRIAtom::Tabled));
80  def("iri", &iri);
81  }
82 }
#define KB_WARN
Definition: Logger.h:27
void write(std::ostream &os) const override
Definition: Atom.cpp:28
static AtomTable & table()
Definition: Atom.cpp:11
std::string_view stringForm_
Definition: Atom.h:61
std::string_view stringForm() const final
Definition: Atom.h:55
void write(std::ostream &os) const override
Definition: IRIAtom.cpp:46
PredicatePtr operator()(const TermPtr &s, const TermPtr &o) const
Definition: IRIAtom.cpp:15
static std::shared_ptr< IRIAtom > Tabled(std::string_view stringForm)
Definition: IRIAtom.cpp:25
static OptionalStringRef uriToAlias(std::string_view uri)
static std::optional< std::string > createIRI(std::string_view alias, std::string_view entityName)
static std::string_view iri_name(std::string_view iri)
Definition: Resource.cpp:57
static std::string_view iri_ns(std::string_view iri, bool includeDelimiter=false)
Definition: Resource.cpp:69
StringRule & atom()
Definition: strings.cpp:54
TermRule & string()
Definition: terms.cpp:63
void createType< IRIAtom >()
Definition: IRIAtom.cpp:76
std::shared_ptr< Term > TermPtr
Definition: Term.h:117
std::shared_ptr< Predicate > PredicatePtr
Definition: Predicate.h:77
IRIAtomPtr iri(std::string_view ns, std::string_view name)
Definition: IRIAtom.cpp:62
std::shared_ptr< IRIAtom > IRIAtomPtr
Definition: IRIAtom.h:57