knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
Property.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/Property.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 Property::Property(std::string_view iri)
16  : Resource(iri), reification_(std::make_shared<Class>(reifiedIRI(iri))), flags_(0) {}
17 
19  : Resource(iri), reification_(std::make_shared<Class>(reifiedIRI(iri->stringForm()))), flags_(0) {}
20 
21 bool Property::PropertyComparator::operator()(const std::shared_ptr<Property> &lhs,
22  const std::shared_ptr<Property> &rhs) const {
23  return lhs->iri() < rhs->iri();
24 }
25 
27  // split the IRI at the last '#' and insert 'Reified' in between
28  auto pos = iri.rfind('#');
29  char delimiter = '#';
30  if (pos == std::string::npos) {
31  pos = iri.rfind('/');
32  delimiter = '/';
33  }
34  std::stringstream ss;
35  if (pos == std::string::npos) {
36  ss << "Reified_" << iri;
37  return IRIAtom::Tabled(ss.str());
38  }
39  ss << iri.substr(0, pos) << delimiter << "Reified_" << iri.substr(pos + 1);
40  return IRIAtom::Tabled(ss.str());
41 }
42 
44  // split the IRI at the last '#' and remove 'Reified' in between
45  auto pos = iri.rfind('#');
46  char delimiter = '#';
47  if (pos == std::string::npos) {
48  pos = iri.rfind('/');
49  delimiter = '/';
50  }
51  if (pos == std::string::npos) {
52  return IRIAtom::Tabled(iri);
53  }
54  auto reified = iri.substr(pos + 1);
55  if (reified.find("Reified_") != 0) {
56  return IRIAtom::Tabled(iri);
57  }
58  std::stringstream ss;
59  ss << iri.substr(0, pos) << delimiter << reified.substr(8);
60  return IRIAtom::Tabled(ss.str());
61 }
62 
63 void Property::addDirectParent(const std::shared_ptr<Property> &directParent, std::optional<std::string_view> graph) {
64  auto graphAtom = graph_atom(graph);
65  auto pair = directParents_.find(directParent);
66  if (pair != directParents_.end()) {
67  // add origin to list
68  pair->second.insert(graphAtom);
69  } else {
70  // add new entry
71  directParents_[directParent].insert(graphAtom);
72  directParent->directChildren_.insert(shared_from_this());
73  }
74  reification_->addDirectParent(directParent->reification_, graph);
75 }
76 
77 void
78 Property::removeDirectParent(const std::shared_ptr<Property> &directParent, std::optional<std::string_view> graph) {
79  auto pair = directParents_.find(directParent);
80  if (pair != directParents_.end()) {
81  // remove origin from list
82  std::string_view v_graph = (graph) ? graph.value() : ImportHierarchy::ORIGIN_SESSION;
83  for (auto it = pair->second.begin(); it != pair->second.end(); it++) {
84  if ((*it)->stringForm() == v_graph) {
85  pair->second.erase(it);
86  break;
87  }
88  }
89  // remove if no origin is left
90  if (pair->second.empty()) {
91  directParents_.erase(pair);
92  directParent->directChildren_.erase(shared_from_this());
93  reification_->removeDirectParent(directParent->reification_, graph);
94  }
95  }
96 }
97 
98 void Property::setInverse(const std::shared_ptr<Property> &inverse) {
99  inverse_ = inverse;
100 }
101 
102 bool Property::hasFlag(PropertyFlag flag) const {
103  return flags_ & flag;
104 }
105 
107  flags_ |= flag;
108 }
109 
111  bool includeSelf,
112  bool skipDuplicates) {
113  std::queue<Property *> queue_;
114  std::set<std::string_view> visited_;
115 
116  // push initial elements to the queue
117  if (includeSelf) queue_.push(this);
118  else for (auto &x: directParents_) queue_.push(x.first.get());
119 
120  // visit each parent
121  while (!queue_.empty()) {
122  auto front = queue_.front();
123  queue_.pop();
124  // visit popped property
125  visitor(*front);
126  // remember visited nodes
127  if (skipDuplicates) visited_.insert(front->iri());
128  // push parents of visited property on the queue
129  for (auto &directParent: front->directParents_) {
130  if (skipDuplicates && visited_.count(directParent.first->iri()) > 0) continue;
131  queue_.push(directParent.first.get());
132  }
133  }
134 }
135 
136 void Property::forallChildren(const PropertyTupleVisitor &visitor, bool skipDuplicates) {
137  std::queue<std::pair<Property *, Property *>> queue_;
138  std::set<std::string_view> visited_;
139 
140  // push initial elements to the queue
141  for (auto &x: directChildren_) queue_.emplace(x.get(), this);
142 
143  // visit each child
144  while (!queue_.empty()) {
145  auto pair = queue_.front();
146  auto front_child = pair.first;
147  auto front_parent = pair.second;
148  queue_.pop();
149  // visit popped property
150  visitor(*front_child, *front_parent);
151  // remember visited nodes
152  if (skipDuplicates) visited_.insert(front_child->iri());
153  // push children of visited property on the queue
154  for (auto &directChild: front_child->directChildren_) {
155  if (skipDuplicates && visited_.count(directChild->iri()) > 0) continue;
156  queue_.emplace(directChild.get(), front_child);
157  }
158  }
159 }
160 
161 bool Property::isSubPropertyOf(const std::shared_ptr<Property> &parent, bool includeSelf) {
162  std::queue<Property *> queue_;
163  std::set<std::string_view> visited_;
164 
165  if (includeSelf && this == parent.get()) return true;
166  queue_.push(this);
167 
168  // visit each parent
169  while (!queue_.empty()) {
170  auto front = queue_.front();
171  queue_.pop();
172 
173  // visit popped property
174  if (front->directParents_.count(parent) > 0) return true;
175  // remember visited nodes
176  visited_.insert(front->iri());
177  // push parents of visited property on the queue
178  for (auto &directParent: front->directParents_) {
179  if (visited_.count(directParent.first->iri()) > 0) continue;
180  queue_.push(directParent.first.get());
181  }
182  }
183 
184  return false;
185 }
186 
188  directParents_.clear();
189  directChildren_.clear();
190  inverse_.reset();
191  reification_->detach();
192 }
193 
194 namespace knowrob::py {
195  template<>
196  void createType<semweb::Property>() {
197  using namespace boost::python;
198 
199  enum_<PropertyFlag>("PropertyFlag")
200  .value("DATATYPE_PROPERTY", DATATYPE_PROPERTY)
201  .value("ANNOTATION_PROPERTY", ANNOTATION_PROPERTY)
202  .value("OBJECT_PROPERTY", OBJECT_PROPERTY)
203  .value("TRANSITIVE_PROPERTY", TRANSITIVE_PROPERTY)
204  .value("REFLEXIVE_PROPERTY", REFLEXIVE_PROPERTY)
205  .value("SYMMETRIC_PROPERTY", SYMMETRIC_PROPERTY)
206  .export_values();
207 
208  class_<semweb::Property, bases<semweb::Resource>, std::shared_ptr<semweb::Property>, boost::noncopyable>
209  ("Property", init<std::string_view>())
210  .def(init<const IRIAtomPtr &>())
211  .def("addDirectParent", &semweb::Property::addDirectParent)
212  .def("removeDirectParent", &semweb::Property::removeDirectParent)
213  .def("directParents", &semweb::Property::directParents, return_value_policy<copy_const_reference>())
214  .def("setInverse", &semweb::Property::setInverse)
215  .def("inverse", &semweb::Property::inverse, return_value_policy<copy_const_reference>())
216  .def("hasFlag", &semweb::Property::hasFlag)
217  .def("setFlag", &semweb::Property::setFlag)
218  .def("isDatatypeProperty", &semweb::Property::isDatatypeProperty)
219  .def("isAnnotationProperty", &semweb::Property::isAnnotationProperty)
220  .def("isObjectProperty", &semweb::Property::isObjectProperty)
221  .def("isTransitiveProperty", &semweb::Property::isTransitiveProperty)
222  .def("isReflexiveProperty", &semweb::Property::isReflexiveProperty)
223  .def("isSymmetricProperty", &semweb::Property::isSymmetricProperty)
224  .def("forallParents", &semweb::Property::forallParents)
225  .def("reification", &semweb::Property::reification)
226  .def("reifiedIRI", &semweb::Property::reifiedIRI)
227  .def("unReifiedIRI", &semweb::Property::unReifiedIRI);
228  }
229 }
static std::shared_ptr< IRIAtom > Tabled(std::string_view stringForm)
Definition: IRIAtom.cpp:25
static constexpr std::string_view ORIGIN_SESSION
bool isSubPropertyOf(const std::shared_ptr< Property > &parent, bool includeSelf=true)
Definition: Property.cpp:161
void setInverse(const std::shared_ptr< Property > &inverse)
Definition: Property.cpp:98
auto reification() const
Definition: Property.h:140
bool isAnnotationProperty() const
Definition: Property.h:94
static knowrob::IRIAtomPtr unReifiedIRI(std::string_view iri)
Definition: Property.cpp:43
bool isSymmetricProperty() const
Definition: Property.h:114
void forallParents(const PropertyVisitor &visitor, bool includeSelf=true, bool skipDuplicates=true)
Definition: Property.cpp:110
std::shared_ptr< Property > inverse_
Definition: Property.h:166
Property(std::string_view iri)
Definition: Property.cpp:15
bool hasFlag(PropertyFlag flag) const
Definition: Property.cpp:102
bool isDatatypeProperty() const
Definition: Property.h:89
bool isReflexiveProperty() const
Definition: Property.h:109
void setFlag(PropertyFlag flag)
Definition: Property.cpp:106
const auto & directParents() const
Definition: Property.h:61
std::shared_ptr< Class > reification_
Definition: Property.h:171
bool isObjectProperty() const
Definition: Property.h:99
std::set< std::shared_ptr< Property >, PropertyComparator > directChildren_
Definition: Property.h:170
bool isTransitiveProperty() const
Definition: Property.h:104
static knowrob::IRIAtomPtr reifiedIRI(std::string_view iri)
Definition: Property.cpp:26
void forallChildren(const PropertyTupleVisitor &visitor, bool skipDuplicates=true)
Definition: Property.cpp:136
const auto & inverse() const
Definition: Property.h:72
void removeDirectParent(const std::shared_ptr< Property > &directParent, std::optional< std::string_view > graph)
Definition: Property.cpp:78
std::map< std::shared_ptr< Property >, std::set< AtomPtr, AtomComparator >, PropertyComparator > directParents_
Definition: Property.h:169
void addDirectParent(const std::shared_ptr< Property > &directParent, std::optional< std::string_view > graph)
Definition: Property.cpp:63
static AtomPtr graph_atom(std::optional< std::string_view > graph)
Definition: Resource.cpp:82
@ SYMMETRIC_PROPERTY
Definition: Property.h:26
@ TRANSITIVE_PROPERTY
Definition: Property.h:24
@ ANNOTATION_PROPERTY
Definition: Property.h:22
@ REFLEXIVE_PROPERTY
Definition: Property.h:25
std::function< void(Property &)> PropertyVisitor
Definition: Property.h:33
std::function< void(Property &, Property &)> PropertyTupleVisitor
Definition: Property.h:35
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< Property > &lhs, const std::shared_ptr< Property > &rhs) const
Definition: Property.cpp:21