Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Standard methods to Ontology #246

Merged
merged 16 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions ontopy/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,37 @@ def __objclass__(self):
# Play nice with inspect...
pass

def __hash__(self):
""" Returns hash based on base_iri
This is done to keep Ontology hashable when defining __eq__.
"""
return hash(self.base_iri)

def __eq__(self, other):
"""Checks if this ontology is equal to other.

Equality of all triples obtained from self.get_triples(),
i.e. blank nodes are not distinguished, but relations
to blank nodes are included.
"""
return set(self.get_triples()) == set(other.get_triples())

def get_triples(self):
""" Returns all triples unabbreviated
"""
def _unabbreviate(i):
if isinstance(i, int):
if i >= 0:
return self._unabbreviate(i)
return "_:" # blank nodes are given random neg. storid
return i

for s, p, o in self.world.get_triples():
s = _unabbreviate(s)
p = _unabbreviate(p)
o = _unabbreviate(o)
yield s, p, o
francescalb marked this conversation as resolved.
Show resolved Hide resolved

def get_by_label(self, label, label_annotations=None, namespace=None):
"""Returns entity with label annotation `label`.

Expand Down
12 changes: 11 additions & 1 deletion tests/test_load_emmo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@ def test_load_emmo() -> None:
from emmopy import get_emmo

EMMO_inferred = get_emmo()
EMMO_inferred_again = get_emmo(None)
EMMO = get_emmo(inferred=False)

assert EMMO_inferred
assert EMMO_inferred_again
assert EMMO
francescalb marked this conversation as resolved.
Show resolved Hide resolved

assert EMMO_inferred.base_iri == get_emmo(None).base_iri
assert EMMO_inferred == EMMO_inferred_again
assert EMMO != EMMO_inferred

EMMO_inferred.new_entity('HydrogenAtom', EMMO_inferred.Atom)
EMMO_inferred.sync_attributes()
assert EMMO_inferred != EMMO_inferred_again

EMMO_inferred2 = get_emmo()
EMMO_inferred2.Atom.comment.append('New triple')
assert EMMO_inferred2 != EMMO_inferred_again