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

WIP: Fix issues with changed IRIs effecting test_excelparser #697

Merged
merged 6 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 20 additions & 12 deletions ontopy/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -1941,6 +1941,14 @@ def new_annotation_property(
"""
return self.new_entity(name, parent, "annotation_property")

def difference(self, other: owlready2.Ontology) -> set:
"""Return a set of triples that are in this, but not in the
`other` ontology."""
# pylint: disable=invalid-name
s1 = set(self.get_unabbreviated_triples(blank="_:b"))
s2 = set(other.get_unabbreviated_triples(blank="_:b"))
return s1.difference(s2)

Comment on lines +1944 to +1951
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.


class BlankNode:
"""Represents a blank node.
Expand Down Expand Up @@ -2008,31 +2016,31 @@ def _unabbreviate(


def _get_unabbreviated_triples(
self, subject=None, predicate=None, obj=None, blank=None
onto, subject=None, predicate=None, obj=None, blank=None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well spotted!

):
"""Help function returning all matching triples unabbreviated.

If `blank` is given, it will be used to represent blank nodes.
"""
# pylint: disable=invalid-name
abb = (
None if subject is None else self._abbreviate(subject),
None if predicate is None else self._abbreviate(predicate),
None if obj is None else self._abbreviate(obj),
None if subject is None else onto._abbreviate(subject),
None if predicate is None else onto._abbreviate(predicate),
None if obj is None else onto._abbreviate(obj),
)
for s, p, o in self._get_obj_triples_spo_spo(*abb):
for s, p, o in onto._get_obj_triples_spo_spo(*abb):
yield (
_unabbreviate(self, s, blank=blank),
_unabbreviate(self, p, blank=blank),
_unabbreviate(self, o, blank=blank),
_unabbreviate(onto, s, blank=blank),
_unabbreviate(onto, p, blank=blank),
_unabbreviate(onto, o, blank=blank),
)
for s, p, o, d in self._get_data_triples_spod_spod(*abb, d=None):
for s, p, o, d in onto._get_data_triples_spod_spod(*abb, d=None):
yield (
_unabbreviate(self, s, blank=blank),
_unabbreviate(self, p, blank=blank),
_unabbreviate(onto, s, blank=blank),
_unabbreviate(onto, p, blank=blank),
f'"{o}"{d}'
if isinstance(d, str)
else f'"{o}"^^{_unabbreviate(self, d)}'
else f'"{o}"^^{_unabbreviate(onto, d)}'
if d
else o,
)
Binary file modified tests/test_excelparser/onto_only_classes.xlsx
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/test_excelparser/result_ontology/fromexcelonto.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"Jesper Friis"@en,
"Sylvain Gouttebroze"@en ;
dcterms:title "A test domain ontology"@en ;
owl:imports <http://emmo.info/emmo-inferred>,
owl:imports <http://emmo.info/emmo>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not understand why. We import beta-4 which should not have changed as development is now happening in beta5.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is due to backward corrections of ontology IRIs on GitHub Pages. Probably a bad decision, but now it is done and should not be reverted again...

<http://ontology.info/ontology> ;
owl:versionInfo "0.01"@en .

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"Jesper Friis"@en,
"Sylvain Gouttebroze"@en ;
dcterms:title "A test domain ontology"@en ;
owl:imports <http://emmo.info/emmo-inferred>,
owl:imports <http://emmo.info/emmo>,
<http://ontology.info/ontology> ;
owl:versionInfo "0.01"@en .

Expand Down
14 changes: 12 additions & 2 deletions tests/test_excelparser/test_excelparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,20 @@ def test_excelparser(repo_dir: "Path") -> None:
update_xlspath, force=True, input_ontology=ontology
)
assert updated_onto.ATotallyNewPattern
assert updated_onto.Pattern.iri == onto.Pattern.iri
assert updated_onto.FinitePattern.iri == onto.FinitePattern.iri
assert len(list(onto.classes())) + 1 == len(list(updated_onto.classes()))


def test_excelparser_only_classes(repo_dir: "Path") -> None:
"""This loads the excelfile used and tests that the resulting ontology prior
to version 0.5.2 in which only classes where considered, but with empty sheets
for properties."""

# Useful for debugging with ipython
# if True:
# from pathlib import Path
# repo_dir = Path(__file__).resolve().parent.parent.parent

ontopath = (
repo_dir
/ "tests"
Expand All @@ -119,6 +125,10 @@ def test_excelparser_only_classes(repo_dir: "Path") -> None:
# Used for printing new ontology when debugging
# ontology.save("test_only_classes.ttl")

# Useful for debugging
# print("----- only in onto -----")
# print(onto.difference(ontology))

assert onto == ontology
assert errors["already_defined"] == {"SpecialPattern"}
assert errors["in_imported_ontologies"] == {"Atom"}
Expand All @@ -143,5 +153,5 @@ def test_excelparser_only_classes(repo_dir: "Path") -> None:
update_xlspath, force=True, input_ontology=ontology
)
assert updated_onto.ATotallyNewPattern
assert updated_onto.Pattern.iri == onto.Pattern.iri
assert updated_onto.FinitePattern.iri == onto.FinitePattern.iri
Comment on lines -146 to +156
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should check both Pattern (in the imported ontology) and FinitePattern (newly created)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pattern doesn't exists in 1.0.0-beta5, which is the one we have here.

assert len(list(onto.classes())) + 1 == len(list(updated_onto.classes()))
25 changes: 25 additions & 0 deletions tests/test_ontology_difference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Test the Ontology.difference() methode"""


if True:
from pathlib import Path
from ontopy import get_ontology

repo_dir = Path(__file__).resolve().parent.parent
onto_dir = repo_dir / "tests" / "testonto"
print(repo_dir)

testonto = get_ontology(onto_dir / "testonto.ttl").load()
testontowi = get_ontology(onto_dir / "testonto_w_individual.ttl").load()

diff = testonto.difference(testontowi)
diffwi = testontowi.difference(testonto)
assert not diff.intersection(diffwi)

triple1 = (
"http://emmo.info/testonto#testindividual",
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
"http://www.w3.org/2002/07/owl#NamedIndividual",
)
assert triple1 in diffwi
assert triple1 not in diff