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:
disjoint_set = set(self.disjoint_with())
for entity in disjoint_set.copy():
disjoint_set.difference_update(
entity.descendants(include_self=False)
)
yield from disjoint_set
else:
for disjoint in self.disjoints():
for entity in disjoint.entities:
if entity is not self:
yield entity
get_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_annotations(
self, all=False, imported=True
): # pylint: disable=redefined-builtin
"""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
def extend(key, values):
"""Extend annotations with a sequence of values."""
if key in annotations:
annotations[key].extend(values)
else:
annotations[key] = values
annotations = {
str(get_preferred_label(a)): a._get_values_for_class(self)
for a in onto.annotation_properties(imported=imported)
}
extend("comment", self.comment)
extend("label", self.label)
if all:
return annotations
return {key: value for key, value in annotations.items() if value}
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.
"""
subclass_relations = set()
for entity in reversed(self.mro()):
for attr in "is_a", "equivalent_to":
if hasattr(entity, attr):
lst = getattr(entity, attr)
if skip_classes:
subclass_relations.update(
r
for r in lst
if not isinstance(r, owlready2.ThingClass)
)
else:
subclass_relations.update(lst)
subclass_relations.update(self.is_a)
return subclass_relations
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:
parents = self.get_parents()
for entity in parents.copy():
parents.difference_update(entity.ancestors(include_self=False))
return parents
if isinstance(self, ThingClass):
return {cls for cls in self.is_a if isinstance(cls, ThingClass)}
if isinstance(self, owlready2.ObjectPropertyClass):
return {
cls
for cls in self.is_a
if isinstance(cls, owlready2.ObjectPropertyClass)
}
raise EMMOntoPyException(
"self has no parents - this should not be possible!"
)
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]
if hasattr(self, "label") and self.label:
return self.label.first()
return self.name
get_typename(self)
¶
Get restriction type label/name.
Source code in ontopy/patch.py
def get_typename(self):
"""Get restriction type label/name."""
return owlready2.class_construct._restriction_type_2_label[self.type]
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."""
namespace = self.namespace
for annotation in namespace.annotation_properties():
if namespace._has_data_triple_spod(
s=namespace.storid, p=annotation.storid
):
yield annotation, getattr(self, annotation.name)
keys(self)
¶
Return a generator over annotation property names associated with this ontology.
Source code in ontopy/patch.py
def keys(self):
"""Return a generator over annotation property names associated
with this ontology."""
namespace = self.namespace
for annotation in namespace.annotation_properties():
if namespace._has_data_triple_spod(
s=namespace.storid, p=annotation.storid
):
yield annotation
namespace_init(self, world_or_ontology, base_iri, name=None)
¶
init function for the Namespace
class.
Source code in ontopy/patch.py
def namespace_init(self, world_or_ontology, base_iri, name=None):
"""__init__ function for the `Namespace` class."""
orig_namespace_init(self, world_or_ontology, base_iri, name)
if self.name.endswith(".ttl"):
self.name = self.name[:-4]
render_func(entity)
¶
Improve default rendering of entities.
Source code in ontopy/patch.py
def render_func(entity):
"""Improve default rendering of entities."""
if hasattr(entity, "prefLabel") and entity.prefLabel:
name = entity.prefLabel[0]
elif hasattr(entity, "label") and entity.label:
name = entity.label[0]
elif hasattr(entity, "altLabel") and entity.altLabel:
name = entity.altLabel[0]
else:
name = entity.name
return f"{entity.namespace.name}.{name}"