patch¶
This module injects some additional methods into owlready2 classes.
disjoint_with(self, reduce=False)
¶
Returns a generator with all classes that are disjoint with self
.
If reduce
is true, all classes that are a descendant of another class
will be excluded.
Source code in ontopy/patch.py
def disjoint_with(self, reduce=False):
"""Returns a generator with all classes that are disjoint with `self`.
If `reduce` is true, all classes that are a descendant of another class
will be excluded."""
if reduce:
s = set(self.disjoint_with())
for e in s.copy():
s.difference_update(e.descendants(include_self=False))
for e in s:
yield e
else:
for d in self.disjoints():
for e in d.entities:
if e is not self:
yield e
get_class_annotations(self, all=False, imported=True)
¶
Returns a dict with non-empty annotations.
If all
is true, also annotations with no value are included.
If imported
is true, also include annotations defined in
imported ontologies.
Source code in ontopy/patch.py
def get_class_annotations(self, all=False, imported=True):
"""Returns a dict with non-empty annotations.
If `all` is true, also annotations with no value are included.
If `imported` is true, also include annotations defined in
imported ontologies.
"""
onto = self.namespace.ontology
d = {get_preferred_label(a): a._get_values_for_class(self)
for a in onto.annotation_properties(imported=imported)}
if all:
return d
else:
return {k: v for k, v in d.items() if v}
get_indirect_is_a(self, skip_classes=True)
¶
Returns the set of all isSubclassOf relations of self and its
ancestors. If skip_classes
is true, indirect classes are not
included in the returned set.
Source code in ontopy/patch.py
def get_indirect_is_a(self, skip_classes=True):
"""Returns the set of all isSubclassOf relations of self and its
ancestors. If `skip_classes` is true, indirect classes are not
included in the returned set.
"""
s = set()
for e in reversed(self.mro()):
if hasattr(e, 'is_a'):
if skip_classes:
s.update(r for r in e.is_a
if not isinstance(r, owlready2.ThingClass))
else:
s.update(e.is_a)
s.update(self.is_a)
return s
get_individual_annotations(self, all=False, imported=True)
¶
Returns a dict with non-empty individual annotations.
If all
is true, also annotations with no value are included.
If imported
is true, also include annotations defined in
imported ontologies.
Source code in ontopy/patch.py
def get_individual_annotations(self, all=False, imported=True):
"""Returns a dict with non-empty individual annotations.
If `all` is true, also annotations with no value are included.
If `imported` is true, also include annotations defined in
imported ontologies.
"""
onto = self.namespace.ontology
d = {get_preferred_label(a): a._get_values_for_individual(self)
for a in onto.annotation_properties(imported=imported)}
if all:
return d
else:
return {k: v for k, v in d.items() if v}
get_parents(self, strict=False)
¶
Returns a list of all parents. If strict
is true, parents that are
parents of other parents are excluded.
Source code in ontopy/patch.py
def get_parents(self, strict=False):
"""Returns a list of all parents. If `strict` is true, parents that are
parents of other parents are excluded."""
if strict:
s = self.get_parents()
for e in s.copy():
s.difference_update(e.ancestors(include_self=False))
return s
elif isinstance(self, ThingClass):
return {cls for cls in self.is_a
if isinstance(cls, ThingClass)}
elif isinstance(self, owlready2.ObjectPropertyClass):
return {cls for cls in self.is_a
if isinstance(cls, owlready2.ObjectPropertyClass)}
else:
assert 0
get_preferred_label(self)
¶
Returns the preferred label as a string (not list).
The following heuristics is used: - if prefLabel annotation property exists, returns the first prefLabel - if label annotation property exists, returns the first label - otherwise return the name
Source code in ontopy/patch.py
def get_preferred_label(self):
"""Returns the preferred label as a string (not list).
The following heuristics is used:
- if prefLabel annotation property exists, returns the first prefLabel
- if label annotation property exists, returns the first label
- otherwise return the name
"""
if hasattr(self, 'prefLabel') and self.prefLabel:
return self.prefLabel[0]
elif hasattr(self, 'label') and self.label:
return self.label.first()
else:
return self.name
get_property_annotations(self, all=False, imported=True)
¶
Returns a dict with non-empty property annotations.
If all
is true, also annotations with no value are included.
If imported
is true, also include annotations defined in
imported ontologies.
Source code in ontopy/patch.py
def get_property_annotations(self, all=False, imported=True):
"""Returns a dict with non-empty property annotations.
If `all` is true, also annotations with no value are included.
If `imported` is true, also include annotations defined in
imported ontologies.
"""
onto = self.namespace.ontology
d = {get_preferred_label(a): a._get_values_for_class(self)
for a in onto.annotation_properties(imported=imported)}
if all:
return d
else:
return {k: v for k, v in d.items() if v}
has(self, name)
¶
Returns true if name
Source code in ontopy/patch.py
def has(self, name):
"""Returns true if `name`"""
return name in set(self.keys())
items(self)
¶
Return a generator over annotation property (name, value_list) pairs associates with this ontology.
Source code in ontopy/patch.py
def items(self):
"""Return a generator over annotation property (name, value_list)
pairs associates with this ontology."""
ns = self.namespace
for a in ns.annotation_properties():
if ns._has_data_triple_spod(s=ns.storid, p=a.storid):
yield a, self.__getattr__(a.name)
keys(self)
¶
Return a generator over annotation property names associates with this ontology.
Source code in ontopy/patch.py
def keys(self):
"""Return a generator over annotation property names associates
with this ontology."""
ns = self.namespace
for a in ns.annotation_properties():
if ns._has_data_triple_spod(s=ns.storid, p=a.storid):
yield a