knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
Atom.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/terms/Atom.h"
7 #include "knowrob/integration/python/utils.h"
8 
9 using namespace knowrob;
10 
12  static Atom::AtomTable theTable;
13  return theTable;
14 }
15 
16 Atom::Atom(std::string_view stringForm, AtomType atomType)
18  stringForm_(stringForm),
19  atomType_(atomType) {
20 }
21 
22 bool Atom::isSameAtom(const Atom &other) const {
23  // Atoms can also be constructed without adding them to the table,
24  // so we need to compare the string forms.
25  return stringForm_ == other.stringForm_;
26 }
27 
28 void Atom::write(std::ostream &os) const {
29  if (stringForm_.empty()) {
30  os << "''";
31  } else if (std::islower(stringForm_[0]) &&
32  std::all_of(stringForm_.begin(), stringForm_.end(), ::isalnum)) {
33  // avoid single quotes
34  os << stringForm_;
35  } else {
36  os << '\'' << stringForm_ << '\'';
37  }
38 }
39 
40 std::shared_ptr<knowrob::Atom> Atom::Tabled(std::string_view name) {
41  auto it = table().find(name);
42  if (it != table().end()) {
43  if (auto atomPtr = it->second.value().lock()) {
44  // Atom still exists, return it
45  return atomPtr;
46  }
47  table().erase(it);
48  }
49  // Atom does not exist or was destroyed, create a new one
50  auto inserted = table().emplace(name, std::nullopt);
51  auto &jt = inserted.first;
52  auto atom = std::make_shared<knowrob::Atom>(jt->first);
53  jt->second = atom;
54  auto locked = jt->second.value().lock();
55  if (!locked) {
56  throw std::runtime_error("Failed to lock Atom");
57  }
58  return locked;
59 }
60 
61 namespace knowrob::py {
62  template<>
64  using namespace boost::python;
65  enum_<AtomType>("AtomType")
66  .value("IRI", AtomType::IRI)
67  .value("REGULAR", AtomType::REGULAR)
68  .export_values();
69  class_<Atom, std::shared_ptr<Atom>, bases<Atomic>>("Atom", no_init)
70  .def("__init__", make_constructor(&Atom::Tabled))
71  .def("Tabled", &Atom::Tabled).staticmethod("Tabled")
72  .def("__repr__", &Atom::stringForm)
73  .def("atomType", &Atom::atomType)
74  .def("isSameAtom", &Atom::isSameAtom);
75  }
76 }
std::map< std::string, std::optional< std::weak_ptr< Atom > >, std::less<> > AtomTable
Definition: Atom.h:64
static std::shared_ptr< knowrob::Atom > Tabled(std::string_view stringForm)
Definition: Atom.cpp:40
void write(std::ostream &os) const override
Definition: Atom.cpp:28
static AtomTable & table()
Definition: Atom.cpp:11
Atom(std::string_view stringForm, AtomType atomType=AtomType::REGULAR)
Definition: Atom.cpp:16
AtomType atomType() const
Definition: Atom.h:52
std::string_view stringForm_
Definition: Atom.h:61
std::string_view stringForm() const final
Definition: Atom.h:55
bool isSameAtom(const Atom &other) const
Definition: Atom.cpp:22
StringRule & atom()
Definition: strings.cpp:54
void createType< Atom >()
Definition: Atom.cpp:63
AtomType
Definition: Atom.h:18
AtomicType
Definition: Atomic.h:17