Skip to content

Commit

Permalink
Merge pull request #613 from emmo-repo/item-access-for-things
Browse files Browse the repository at this point in the history
Item access to classes
  • Loading branch information
francescalb authored Jun 5, 2023
2 parents c28d8a4 + 8e286b4 commit 64b8e4e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
47 changes: 44 additions & 3 deletions ontopy/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import types

import owlready2
from owlready2 import ThingClass, PropertyClass, Thing, Restriction, Namespace
from owlready2 import Metadata
from owlready2 import AnnotationPropertyClass, ThingClass, PropertyClass
from owlready2 import Metadata, Thing, Restriction, Namespace
from ontopy.utils import EMMOntoPyException


Expand Down Expand Up @@ -67,13 +67,51 @@ def get_parents(self, strict=False):


def _dir(self):
"""Extend in dir() listing of ontology classes."""
"""Extend 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)


def _getitem(self, name):
"""Provide item access to annotation properties."""
prop = self.namespace.ontology.get_by_label(name)
if isinstance(prop, AnnotationPropertyClass):
return getattr(self, name)
raise KeyError(f"no such annotation property: {name}")


def _setitem(self, name, value):
"""Provide item asignment for annotation properties.
Note, this appends `value` to the property instead of replacing the
property. This is consistent with Owlready2, but may be little
unintuitive.
Example:
>>> from emmopy import get_emmo
>>> emmo = get_emmo()
>>> emmo.Atom['altLabel']
['ChemicalElement']
emmo.Atom['altLabel'] = 'Element'
>>> emmo.Atom['altLabel']
['ChemicalElement', 'Element']
"""
item = _getitem(self, name)
item.append(value)


def _delitem(self, name):
"""Provide item deletion for annotation properties.
Note, this simply clears the named property.
"""
item = _getitem(self, name)
item.clear()


def get_annotations(
self, all=False, imported=True
): # pylint: disable=redefined-builtin
Expand Down Expand Up @@ -155,6 +193,9 @@ def get_indirect_is_a(self, skip_classes=True):

# Inject methods into ThingClass
setattr(ThingClass, "__dir__", _dir)
setattr(ThingClass, "__getitem__", _getitem)
setattr(ThingClass, "__setitem__", _setitem)
setattr(ThingClass, "__delitem__", _delitem)
setattr(ThingClass, "get_preferred_label", get_preferred_label)
setattr(ThingClass, "get_parents", get_parents)
setattr(ThingClass, "get_annotations", get_annotations)
Expand Down
19 changes: 19 additions & 0 deletions tests/ontopy_tests/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Implemented as a script, such that it easy to understand and use for debugging.
"""
import pytest

from ontopy import get_ontology

from owlready2 import owl, Inverse
Expand All @@ -25,6 +27,23 @@
"elucidation",
}


# Test item access/assignment/deletion for classes
assert emmo.Atom["altLabel"] == ["ChemicalElement"]

with pytest.raises(KeyError):
emmo.Atom["hasPart"]

emmo.Atom["altLabel"] = "Element"
assert emmo.Atom["altLabel"] == ["ChemicalElement", "Element"]

del emmo.Atom["altLabel"]
assert emmo.Atom["altLabel"] == []

emmo.Atom["altLabel"] = "ChemicalElement"
assert emmo.Atom["altLabel"] == ["ChemicalElement"]


# TODO: Fix disjoint_with().
# It seems not to take into account disjoint unions.
# assert set(emmo.Collection.disjoint_with()) == {emmo.Item}
Expand Down

0 comments on commit 64b8e4e

Please sign in to comment.