Skip to content

patch

This module injects some additional methods into owlready2 classes.

_dir(self)

Extend in dir() listing of ontology classes.

Source code in ontopy/patch.py
66
67
68
69
70
71
def _dir(self):
    """Extend in dir() listing of ontology classes."""
    set_dir = set(object.__dir__(self))
    props = self.namespace.world._props.keys()
    set_dir.update(props)
    return sorted(set_dir)

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
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
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)
            )
        for entity in disjoint_set:
            yield entity
    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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
    annotations = {
        get_preferred_label(_): _._get_values_for_class(self)
        for _ in onto.annotation_properties(imported=imported)
    }
    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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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()):
        if hasattr(entity, "is_a"):
            if skip_classes:
                subclass_relations.update(
                    _
                    for _ in entity.is_a
                    if not isinstance(_, owlready2.ThingClass)
                )
            else:
                subclass_relations.update(entity.is_a)
    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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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 Exception("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
29
30
31
32
33
34
35
36
37
38
39
40
41
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
164
165
166
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
213
214
215
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
202
203
204
205
206
207
208
209
210
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, self.__getattr__(annotation.name)

keys(self)

Return a generator over annotation property names associates with this ontology.

Source code in ontopy/patch.py
191
192
193
194
195
196
197
198
199
def keys(self):
    """Return a generator over annotation property names 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

namespace_init(self, world_or_ontology, base_iri, name=None)

init function for the Namespace class.

Source code in ontopy/patch.py
178
179
180
181
182
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
10
11
12
13
14
15
16
17
18
19
20
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}"
Back to top