knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
Vocabulary.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/Logger.h"
7 #include "knowrob/semweb/Vocabulary.h"
8 #include "knowrob/semweb/rdf.h"
9 #include "knowrob/semweb/rdfs.h"
10 #include "knowrob/semweb/owl.h"
11 #include "knowrob/semweb/PrefixProbe.h"
12 #include "knowrob/storage/reification.h"
13 #include "knowrob/integration/python/utils.h"
14 
15 using namespace knowrob;
16 using namespace knowrob::semweb;
17 
19  : importHierarchy_(std::make_shared<ImportHierarchy>()) {
24 
29 }
30 
32  for (auto &c : definedClasses_) {
33  c.second->detach();
34  }
35  for (auto &p : definedProperties_) {
36  p.second->detach();
37  p.second->reification()->detach();
38  }
39 }
40 
41 bool Vocabulary::isDefinedClass(const std::string_view &iri) {
42  return definedClasses_.count(iri) > 0;
43 }
44 
45 ClassPtr Vocabulary::getDefinedClass(const std::string_view &iri) const {
46  auto it = definedClasses_.find(iri);
47  if (it == definedClasses_.end()) {
48  return {};
49  } else {
50  return it->second;
51  }
52 }
53 
54 std::vector<ClassPtr> Vocabulary::getDefinedClassesWithPrefix(const std::string_view &prefix) const {
55  auto range_it = definedClasses_.equal_range(PrefixProbe{prefix});
56  std::vector<ClassPtr> result;
57  for (auto it = range_it.first; it != range_it.second; ++it) {
58  result.push_back(it->second);
59  }
60  return result;
61 }
62 
63 std::vector<std::string_view> Vocabulary::getDefinedClassNamesWithPrefix(const std::string_view &prefix) const {
64  auto range_it = definedClasses_.equal_range(PrefixProbe{prefix});
65  std::vector<std::string_view> result;
66  for (auto it = range_it.first; it != range_it.second; ++it) {
67  result.push_back(it->first);
68  }
69  return result;
70 }
71 
72 ClassPtr Vocabulary::defineClass(const std::string_view &iri) {
73  auto it = definedClasses_.find(iri);
74  if (it == definedClasses_.end()) {
75  auto newClass = std::make_shared<Class>(iri);
76  definedClasses_[newClass->iri()] = newClass;
77  return newClass;
78  } else {
79  return it->second;
80  }
81 }
82 
83 void Vocabulary::addSubClassOf(const std::string_view &subClass, const std::string_view &superClass,
84  std::optional<std::string_view> graph) {
85  defineClass(subClass)->addDirectParent(defineClass(superClass), graph);
86 }
87 
88 bool Vocabulary::isSubClassOf(const std::string_view &subClass, const std::string_view &superClass) {
89  return defineClass(subClass)->isSubClassOf(defineClass(superClass));
90 }
91 
92 bool Vocabulary::isDefinedProperty(const std::string_view &iri) {
93  return definedProperties_.count(iri) > 0;
94 }
95 
96 bool Vocabulary::isDefinedReification(const std::string_view &iri) {
97  return definedReifications_.count(iri) > 0;
98 }
99 
100 PropertyPtr Vocabulary::getDefinedProperty(const std::string_view &iri) const {
101  auto it = definedProperties_.find(iri);
102  if (it == definedProperties_.end()) {
103  return {};
104  } else {
105  return it->second;
106  }
107 }
108 
109 PropertyPtr Vocabulary::getDefinedReification(const std::string_view &iri) const {
110  auto it = definedReifications_.find(iri);
111  if (it == definedReifications_.end()) {
112  return {};
113  } else {
114  return it->second;
115  }
116 }
117 
118 std::vector<PropertyPtr> Vocabulary::getDefinedPropertiesWithPrefix(const std::string_view &prefix) const {
119  auto range_it = definedProperties_.equal_range(PrefixProbe{prefix});
120  std::vector<PropertyPtr> result;
121  for (auto it = range_it.first; it != range_it.second; ++it) {
122  result.push_back(it->second);
123  }
124  return result;
125 }
126 
127 std::vector<std::string_view> Vocabulary::getDefinedPropertyNamesWithPrefix(const std::string_view &prefix) const {
128  auto range_it = definedProperties_.equal_range(PrefixProbe{prefix});
129  std::vector<std::string_view> result;
130  for (auto it = range_it.first; it != range_it.second; ++it) {
131  result.push_back(it->first);
132  }
133  return result;
134 }
135 
136 PropertyPtr Vocabulary::defineProperty(const std::string_view &iri) {
137  auto it = definedProperties_.find(iri);
138  if (it == definedProperties_.end()) {
139  return defineProperty_(std::make_shared<Property>(iri));
140  } else {
141  return it->second;
142  }
143 }
144 
146  auto it = definedProperties_.find(iri->stringForm());
147  if (it == definedProperties_.end()) {
148  return defineProperty_(std::make_shared<Property>(iri));
149  } else {
150  return it->second;
151  }
152 }
153 
154 semweb::PropertyPtr Vocabulary::defineProperty_(const std::shared_ptr<Property> &p) {
155  definedProperties_[p->iri()] = p;
156  definedReifications_[p->reification()->iri()] = p;
157  // define the reification of the property as a concept which inherits knowrob:ReifiedRelation
158  // note further superclasses will be added through Property::addDirectParent
159  auto reification = p->reification();
160  definedClasses_[reification->iri()] = reification;
161  reification->addDirectParent(defineClass(reification::ReifiedRelation->stringForm()),
163  return p;
164 }
165 
166 void Vocabulary::setPropertyFlag(const std::string_view &iri, PropertyFlag flag) {
167  defineProperty(iri)->setFlag(flag);
168 }
169 
171  defineProperty(iri)->setFlag(flag);
172 }
173 
174 void Vocabulary::addSubPropertyOf(const std::string_view &subProperty, const std::string_view &superProperty,
175  std::optional<std::string_view> graph) {
176  defineProperty(subProperty)->addDirectParent(defineProperty(superProperty), graph);
177 }
178 
179 void Vocabulary::setInverseOf(const std::string_view &a, const std::string_view &b) {
180  auto a1 = defineProperty(a);
181  auto b1 = defineProperty(b);
182  a1->setInverse(b1);
183  b1->setInverse(a1);
184 }
185 
186 bool Vocabulary::isAnnotationProperty(const std::string_view &iri) {
187  auto it = definedProperties_.find(iri);
188  return it != definedProperties_.end() && it->second->hasFlag(ANNOTATION_PROPERTY);
189 }
190 
191 bool Vocabulary::isObjectProperty(const std::string_view &iri) {
192  auto it = definedProperties_.find(iri);
193  return it != definedProperties_.end() && it->second->hasFlag(OBJECT_PROPERTY);
194 }
195 
196 bool Vocabulary::isDatatypeProperty(const std::string_view &iri) {
197  auto it = definedProperties_.find(iri);
198  return it != definedProperties_.end() && it->second->hasFlag(DATATYPE_PROPERTY);
199 }
200 
201 bool Vocabulary::isTaxonomicProperty(const std::string_view &iri) {
202  return isSubClassOfIRI(iri) ||
204  isTypeIRI(iri);
205 }
206 
207 void Vocabulary::setFrequency(const std::string_view &iri, uint64_t frequency) {
208  if (frequency == 0) {
209  frequency_.erase(iri);
210  } else {
211  auto p_it = definedProperties_.find(iri);
212  if (p_it != definedProperties_.end()) {
213  frequency_[p_it->second->iri()] = frequency;
214  return;
215  }
216  auto c_it = definedClasses_.find(iri);
217  if (c_it != definedClasses_.end()) {
218  frequency_[c_it->second->iri()] = frequency;
219  return;
220  }
221  KB_WARN("cannot set frequency of unknown resource '{}'", iri);
222  }
223 }
224 
225 void Vocabulary::increaseFrequency(const std::string_view &iri) {
226  auto it = frequency_.find(iri);
227  if (it == frequency_.end()) {
228  setFrequency(iri, 1);
229  } else {
230  it->second++;
231  }
232 }
233 
234 uint64_t Vocabulary::frequency(const std::string_view &iri) const {
235  auto it = frequency_.find(iri);
236  if (it == frequency_.end()) return 0;
237  else return it->second;
238 }
239 
240 void Vocabulary::addResourceType(const std::string_view &resource_iri, const std::string_view &type_iri) {
241  if (isObjectPropertyIRI(type_iri))
242  setPropertyFlag(resource_iri, OBJECT_PROPERTY);
243  else if (isDatatypePropertyIRI(type_iri))
244  setPropertyFlag(resource_iri, DATATYPE_PROPERTY);
245  else if (isAnnotationPropertyIRI(type_iri))
246  setPropertyFlag(resource_iri, ANNOTATION_PROPERTY);
247  else if (isReflexivePropertyIRI(type_iri))
248  setPropertyFlag(resource_iri, REFLEXIVE_PROPERTY);
249  else if (isTransitivePropertyIRI(type_iri))
250  setPropertyFlag(resource_iri, TRANSITIVE_PROPERTY);
251  else if (isSymmetricPropertyIRI(type_iri))
252  setPropertyFlag(resource_iri, SYMMETRIC_PROPERTY);
253  else if (isPropertyIRI(type_iri))
254  defineProperty(resource_iri);
255  else if (isClassIRI(type_iri))
256  defineClass(resource_iri);
257 }
258 
259 namespace knowrob {
260  template<>
261  std::shared_ptr<semweb::Class>
262  Vocabulary::define<semweb::Class>(const std::string_view &iri) { return defineClass(iri); }
263 
264  template<>
265  std::shared_ptr<semweb::Property>
266  Vocabulary::define<semweb::Property>(const std::string_view &iri) { return defineProperty(iri); }
267 }
268 
269 namespace knowrob::py {
270  template<>
272  using namespace boost::python;
273 
274  using PropertyFun = semweb::PropertyPtr (Vocabulary::*)(const std::string_view &);
275  using SetFlag = void (Vocabulary::*)(const std::string_view &, semweb::PropertyFlag);
276 
277  createType<semweb::Resource>();
278  createType<semweb::Property>();
279  createType<semweb::Class>();
281 
282  class_<Vocabulary, std::shared_ptr<Vocabulary>, boost::noncopyable>
283  ("Vocabulary", init<>())
284  .def("addResourceType", &Vocabulary::addResourceType)
285  .def("isDefinedClass", &Vocabulary::isDefinedClass)
286  .def("getDefinedClass", &Vocabulary::getDefinedClass)
287  .def("getDefinedClassesWithPrefix", &Vocabulary::getDefinedClassesWithPrefix)
288  .def("getDefinedClassNamesWithPrefix", &Vocabulary::getDefinedClassNamesWithPrefix)
289  .def("defineClass", &Vocabulary::defineClass)
290  .def("addSubClassOf", &Vocabulary::addSubClassOf)
291  .def("isSubClassOf", &Vocabulary::isSubClassOf)
292  .def("isDefinedProperty", &Vocabulary::isDefinedProperty)
293  .def("isDefinedReification", &Vocabulary::isDefinedReification)
294  .def("getDefinedProperty", &Vocabulary::getDefinedProperty)
295  .def("getDefinedReification", &Vocabulary::getDefinedReification)
296  .def("getDefinedPropertiesWithPrefix", &Vocabulary::getDefinedPropertiesWithPrefix)
297  .def("getDefinedPropertyNamesWithPrefix", &Vocabulary::getDefinedPropertyNamesWithPrefix)
298  .def("defineProperty", static_cast<PropertyFun>(&Vocabulary::defineProperty))
299  .def("addSubPropertyOf", &Vocabulary::addSubPropertyOf)
300  .def("setInverseOf", &Vocabulary::setInverseOf)
301  .def("setPropertyFlag", static_cast<SetFlag>(&Vocabulary::setPropertyFlag))
302  .def("isAnnotationProperty", &Vocabulary::isAnnotationProperty)
303  .def("isObjectProperty", &Vocabulary::isObjectProperty)
304  .def("isDatatypeProperty", &Vocabulary::isDatatypeProperty)
305  .def("isTaxonomicProperty", &Vocabulary::isTaxonomicProperty)
306  .def("setFrequency", &Vocabulary::setFrequency)
307  .def("increaseFrequency", &Vocabulary::increaseFrequency)
308  .def("frequency", &Vocabulary::frequency)
309  .def("importHierarchy", &Vocabulary::importHierarchy, return_value_policy<copy_const_reference>());
310  }
311 }
#define KB_WARN
Definition: Logger.h:27
static constexpr std::string_view ORIGIN_SYSTEM
std::vector< std::string_view > getDefinedPropertyNamesWithPrefix(const std::string_view &prefix) const
Definition: Vocabulary.cpp:127
bool isDefinedClass(const std::string_view &iri)
Definition: Vocabulary.cpp:41
std::map< std::string_view, semweb::PropertyPtr, std::less<> > definedReifications_
Definition: Vocabulary.h:226
std::vector< semweb::PropertyPtr > getDefinedPropertiesWithPrefix(const std::string_view &prefix) const
Definition: Vocabulary.cpp:118
std::vector< std::string_view > getDefinedClassNamesWithPrefix(const std::string_view &prefix) const
Definition: Vocabulary.cpp:63
std::map< std::string_view, semweb::ClassPtr, std::less<> > definedClasses_
Definition: Vocabulary.h:224
semweb::ClassPtr getDefinedClass(const std::string_view &iri) const
Definition: Vocabulary.cpp:45
bool isObjectProperty(const std::string_view &iri)
Definition: Vocabulary.cpp:191
bool isDatatypeProperty(const std::string_view &iri)
Definition: Vocabulary.cpp:196
std::map< std::string_view, uint64_t > frequency_
Definition: Vocabulary.h:227
bool isSubClassOf(const std::string_view &subClass, const std::string_view &superClass)
Definition: Vocabulary.cpp:88
std::vector< semweb::ClassPtr > getDefinedClassesWithPrefix(const std::string_view &prefix) const
Definition: Vocabulary.cpp:54
void increaseFrequency(const std::string_view &iri)
Definition: Vocabulary.cpp:225
semweb::PropertyPtr defineProperty_(const std::shared_ptr< semweb::Property > &p)
void addResourceType(const std::string_view &resource_iri, const std::string_view &type_iri)
Definition: Vocabulary.cpp:240
semweb::PropertyPtr getDefinedProperty(const std::string_view &iri) const
Definition: Vocabulary.cpp:100
void setPropertyFlag(const std::string_view &iri, semweb::PropertyFlag flag)
Definition: Vocabulary.cpp:166
bool isDefinedProperty(const std::string_view &iri)
Definition: Vocabulary.cpp:92
uint64_t frequency(const std::string_view &iri) const
Definition: Vocabulary.cpp:234
void addSubClassOf(const std::string_view &subClass, const std::string_view &superClass, std::optional< std::string_view > graph)
Definition: Vocabulary.cpp:83
std::map< std::string_view, semweb::PropertyPtr, std::less<> > definedProperties_
Definition: Vocabulary.h:225
static bool isTaxonomicProperty(const std::string_view &iri)
Definition: Vocabulary.cpp:201
bool isAnnotationProperty(const std::string_view &iri)
Definition: Vocabulary.cpp:186
const std::shared_ptr< ImportHierarchy > & importHierarchy() const
Definition: Vocabulary.h:212
void setFrequency(const std::string_view &iri, uint64_t frequency)
Definition: Vocabulary.cpp:207
void setInverseOf(const std::string_view &a, const std::string_view &b)
Definition: Vocabulary.cpp:179
void addSubPropertyOf(const std::string_view &subProperty, const std::string_view &superProperty, std::optional< std::string_view > graph)
Definition: Vocabulary.cpp:174
semweb::PropertyPtr getDefinedReification(const std::string_view &iri) const
Definition: Vocabulary.cpp:109
bool isDefinedReification(const std::string_view &iri)
Definition: Vocabulary.cpp:96
semweb::ClassPtr defineClass(const std::string_view &iri)
Definition: Vocabulary.cpp:72
semweb::PropertyPtr defineProperty(const std::string_view &iri)
Definition: Vocabulary.cpp:136
const IRIAtomPtr inverseOf
Definition: owl.h:17
const IRIAtomPtr versionInfo
Definition: owl.h:16
constexpr std::string_view prefix
Definition: owl.h:14
void createType< Vocabulary >()
Definition: Vocabulary.cpp:271
void createType< ImportHierarchy >()
const IRIAtomPtr type
Definition: rdf.h:15
const IRIAtomPtr subPropertyOf
Definition: rdfs.h:16
const IRIAtomPtr seeAlso
Definition: rdfs.h:18
const IRIAtomPtr label
Definition: rdfs.h:19
const IRIAtomPtr comment
Definition: rdfs.h:17
const IRIAtomPtr subClassOf
Definition: rdfs.h:15
const IRIAtomPtr ReifiedRelation
Definition: reification.h:14
std::shared_ptr< Class > ClassPtr
Definition: Class.h:97
@ 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::shared_ptr< Property > PropertyPtr
Definition: Property.h:175
bool isSubClassOfIRI(std::string_view iri)
Definition: rdfs.cpp:9
bool isClassIRI(std::string_view iri)
Definition: owl.cpp:9
bool isTypeIRI(std::string_view iri)
Definition: rdf.cpp:9
bool isSymmetricPropertyIRI(std::string_view iri)
Definition: owl.cpp:27
bool isDatatypePropertyIRI(std::string_view iri)
Definition: owl.cpp:18
bool isSubPropertyOfIRI(std::string_view iri)
Definition: rdfs.cpp:12
IRIAtomPtr iri(std::string_view ns, std::string_view name)
Definition: IRIAtom.cpp:62
std::shared_ptr< IRIAtom > IRIAtomPtr
Definition: IRIAtom.h:57
bool isPropertyIRI(std::string_view iri)
Definition: rdf.cpp:12
bool isTransitivePropertyIRI(std::string_view iri)
Definition: owl.cpp:24
bool isReflexivePropertyIRI(std::string_view iri)
Definition: owl.cpp:30
bool isAnnotationPropertyIRI(std::string_view iri)
Definition: owl.cpp:21
bool isObjectPropertyIRI(std::string_view iri)
Definition: owl.cpp:15