knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
Class.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 <queue>
7 #include <set>
8 #include "knowrob/semweb/Class.h"
9 #include "knowrob/Logger.h"
10 #include "knowrob/integration/python/utils.h"
11 #include "knowrob/semweb/ImportHierarchy.h"
12 
13 using namespace knowrob::semweb;
14 
15 Class::Class(std::string_view iri)
16  : Resource(iri) {}
17 
19  : Resource(iri) {}
20 
21 bool Class::ClassComparator::operator()(const std::shared_ptr<Class> &lhs, const std::shared_ptr<Class> &rhs) const {
22  return lhs->iri() < rhs->iri();
23 }
24 
25 void Class::addDirectParent(const std::shared_ptr<Class> &directParent, std::optional<std::string_view> graph) {
26  auto graphAtom = graph_atom(graph);
27  auto pair = directParents_.find(directParent);
28  if (pair != directParents_.end()) {
29  // add origin to list
30  pair->second.insert(graphAtom);
31  } else {
32  // add new entry
33  directParents_[directParent].insert(graphAtom);
34  directParent->directChildren_.insert(shared_from_this());
35  }
36 }
37 
38 void Class::removeDirectParent(const std::shared_ptr<Class> &directParent, std::optional<std::string_view> graph) {
39  auto pair = directParents_.find(directParent);
40  if (pair != directParents_.end()) {
41  // remove origin from list
42  std::string_view v_graph = (graph) ? graph.value() : ImportHierarchy::ORIGIN_SESSION;
43  for (auto it = pair->second.begin(); it != pair->second.end(); it++) {
44  if ((*it)->stringForm() == v_graph) {
45  pair->second.erase(it);
46  break;
47  }
48  }
49  // remove if no origin is left
50  if (pair->second.empty()) {
51  directParents_.erase(pair);
52  directParent->directChildren_.erase(shared_from_this());
53  }
54  }
55 }
56 
57 bool Class::isDirectSubClassOf(const std::shared_ptr<Class> &directParent) {
58  return directParents_.count(directParent) > 0;
59 }
60 
61 bool Class::isSubClassOf(const std::shared_ptr<Class> &parent, bool includeSelf) {
62  std::queue<Class *> queue_;
63  std::set<std::string_view> visited_;
64 
65  if (includeSelf && this == parent.get()) return true;
66  queue_.push(this);
67 
68  // visit each parent
69  while (!queue_.empty()) {
70  auto front = queue_.front();
71  queue_.pop();
72 
73  // visit popped property
74  if (front->directParents_.count(parent) > 0) return true;
75  // remember visited nodes
76  visited_.insert(front->iri());
77  // push parents of visited property on the queue
78  for (auto &directParent: front->directParents_) {
79  if (visited_.count(directParent.first->iri()) > 0) continue;
80  queue_.push(directParent.first.get());
81  }
82  }
83 
84  return false;
85 }
86 
87 void Class::forallParents(const ClassVisitor &visitor,
88  bool includeSelf,
89  bool skipDuplicates) {
90  std::queue<Class *> queue_;
91  std::set<std::string_view> visited_;
92 
93  // push initial elements to the queue
94  if (includeSelf) queue_.push(this);
95  else for (auto &x: directParents_) queue_.push(x.first.get());
96 
97  // visit each parent
98  while (!queue_.empty()) {
99  auto front = queue_.front();
100  queue_.pop();
101  // visit popped property
102  visitor(*front);
103  // remember visited nodes
104  if (skipDuplicates) visited_.insert(front->iri());
105  // push parents of visited property on the queue
106  for (auto &directParent: front->directParents_) {
107  if (skipDuplicates && visited_.count(directParent.first->iri()) > 0) continue;
108  queue_.push(directParent.first.get());
109  }
110  }
111 }
112 
113 void Class::forallChildren(const ClassTupleVisitor &visitor, bool skipDuplicates) {
114  std::queue<std::pair<Class *, Class *>> queue_;
115  std::set<std::string_view> visited_;
116 
117  // push initial elements to the queue
118  for (auto &x: directChildren_) queue_.emplace(x.get(), this);
119 
120  // visit each child
121  while (!queue_.empty()) {
122  auto pair = queue_.front();
123  auto front_child = pair.first;
124  auto front_parent = pair.second;
125  queue_.pop();
126  // visit popped property
127  visitor(*front_child, *front_parent);
128  // remember visited nodes
129  if (skipDuplicates) visited_.insert(front_child->iri());
130  // push children of visited property on the queue
131  for (auto &directChild: front_child->directChildren_) {
132  if (skipDuplicates && visited_.count(directChild->iri()) > 0) continue;
133  queue_.emplace(directChild.get(), front_child);
134  }
135  }
136 }
137 
139  directParents_.clear();
140  directChildren_.clear();
141 }
142 
143 namespace knowrob::py {
144  template<>
145  void createType<semweb::Class>() {
146  using namespace boost::python;
147 
148  class_<semweb::Class, bases<semweb::Resource>, std::shared_ptr<semweb::Class>, boost::noncopyable>
149  ("Class", init<std::string_view>())
150  .def(init<const IRIAtomPtr &>())
151  .def("addDirectParent", &semweb::Class::addDirectParent)
152  .def("removeDirectParent", &semweb::Class::removeDirectParent)
153  .def("directParents", &semweb::Class::directParents, return_value_policy<copy_const_reference>())
154  .def("isDirectSubClassOf", &semweb::Class::isDirectSubClassOf)
155  .def("isSubClassOf", &semweb::Class::isSubClassOf)
156  .def("forallParents", &semweb::Class::forallParents);
157  }
158 }
159 
static constexpr std::string_view ORIGIN_SESSION
Class(std::string_view iri)
Definition: Class.cpp:15
std::set< std::shared_ptr< Class >, ClassComparator > directChildren_
Definition: Class.h:94
bool isSubClassOf(const std::shared_ptr< Class > &parent, bool includeSelf=true)
Definition: Class.cpp:61
const auto & directParents() const
Definition: Class.h:53
void forallChildren(const ClassTupleVisitor &visitor, bool skipDuplicates=true)
Definition: Class.cpp:113
bool isDirectSubClassOf(const std::shared_ptr< Class > &directParent)
Definition: Class.cpp:57
void addDirectParent(const std::shared_ptr< Class > &directParent, std::optional< std::string_view > graph)
Definition: Class.cpp:25
void forallParents(const ClassVisitor &visitor, bool includeSelf=true, bool skipDuplicates=true)
Definition: Class.cpp:87
void removeDirectParent(const std::shared_ptr< Class > &directParent, std::optional< std::string_view > graph)
Definition: Class.cpp:38
std::map< std::shared_ptr< Class >, std::set< AtomPtr, AtomComparator >, ClassComparator > directParents_
Definition: Class.h:93
static AtomPtr graph_atom(std::optional< std::string_view > graph)
Definition: Resource.cpp:82
std::function< void(Class &)> ClassVisitor
Definition: Class.h:19
std::function< void(Class &, Class &)> ClassTupleVisitor
Definition: Class.h:21
IRIAtomPtr iri(std::string_view ns, std::string_view name)
Definition: IRIAtom.cpp:62
std::shared_ptr< IRIAtom > IRIAtomPtr
Definition: IRIAtom.h:57
bool operator()(const std::shared_ptr< Class > &lhs, const std::shared_ptr< Class > &rhs) const
Definition: Class.cpp:21