From d3f8e88243a86c422f66653415e1fc662bc12b1b Mon Sep 17 00:00:00 2001 From: "Francesca L. Bleken" <48128015+francescalb@users.noreply.github.com> Date: Tue, 29 Oct 2024 14:33:58 +0100 Subject: [PATCH] Handle owlready2:python_names in genated triples in excelparser (#795) # Description EMMOntoPy tests fail with owlready0.45 and higher. I want to fix one owlready release at a time, since there seem to be different things that are incompatible going up to v0.48. In v 0.45 owlready2 introduced the python names defined with the predicate "http://www.lesfleursdunormal.fr/static/_downloads/owlready_ontology.owl#python_name" in the ontology. These should not be part of the generated ontology and have therefore been removed in the ontology generated with the excelparser. --- ontopy/excelparser.py | 24 ++++++++++++++++ requirements.txt | 2 +- tests/test_excelparser/test_excelparser.py | 33 ++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/ontopy/excelparser.py b/ontopy/excelparser.py index a537a286e..cb22ea59e 100755 --- a/ontopy/excelparser.py +++ b/ontopy/excelparser.py @@ -313,6 +313,15 @@ def create_ontology_from_pandas( # pylint:disable=too-many-locals,too-many-bran if input_ontology: onto = input_ontology catalog = {} + # Since we will remove newly created python_name added + # by owlready2 in the triples, we keep track of those + # that come from the input ontology + pyname_triples_to_keep = list( + onto.get_unabbreviated_triples( + predicate="http://www.lesfleursdunormal.fr/static/_downloads/" + "owlready_ontology.owl#python_name" + ) + ) else: # Create new ontology onto, catalog = get_metadata_from_dataframe( metadata, base_iri, imports=imports @@ -456,6 +465,21 @@ def create_ontology_from_pandas( # pylint:disable=too-many-locals,too-many-bran entities_with_errors = { key: set(value) for key, value in entities_with_errors.items() } + + # Remove triples with predicate 'python_name' added by owlready2 + onto._del_data_triple_spod( # pylint: disable=protected-access + p=onto._abbreviate( # pylint: disable=protected-access + "http://www.lesfleursdunormal.fr/static/_downloads/" + "owlready_ontology.owl#python_name" + ) + ) + # Add back the triples python name triples that were in the input_ontology. + if input_ontology: + for triple in pyname_triples_to_keep: + onto._add_data_triple_spod( # pylint: disable=protected-access + s=triple[0], p=triple[1], o=triple[2] + ) + return onto, catalog, entities_with_errors diff --git a/requirements.txt b/requirements.txt index 8576bf3d4..77559d07d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,7 +4,7 @@ defusedxml>=0.7.1,<1 graphviz>=0.16,<0.21 numpy>=1.19.5,<3 openpyxl>=3.0.9,<3.2 -Owlready2>=0.28,!=0.32,!=0.34,<0.45 +Owlready2>=0.28,!=0.32,!=0.34,<0.46 packaging>=21.0,<25 pandas>=1.2,<2.3 Pygments>=2.7.4,<3 diff --git a/tests/test_excelparser/test_excelparser.py b/tests/test_excelparser/test_excelparser.py index 58d6da30a..571b00d9b 100644 --- a/tests/test_excelparser/test_excelparser.py +++ b/tests/test_excelparser/test_excelparser.py @@ -95,6 +95,39 @@ def test_excelparser(repo_dir: "Path") -> None: assert updated_onto.FinitePattern.iri == onto.FinitePattern.iri assert len(list(onto.classes())) + 1 == len(list(updated_onto.classes())) + # check that the owlready2 generated python names are not in the triples + assert ( + list( + ontology.get_unabbreviated_triples( + predicate="http://www.lesfleursdunormal.fr/static/_downloads/" + "owlready_ontology.owl#python_name" + ) + ) + == [] + ) + # check that the owlready2 generated python names are not in the triples + assert ( + list( + updated_onto.get_unabbreviated_triples( + predicate="http://www.lesfleursdunormal.fr/static/_downloads/" + "owlready_ontology.owl#python_name" + ) + ) + == [] + ) + + # Just to be sure that the method of getting the correct triples is OK + assert ( + len( + list( + ontology.get_unabbreviated_triples( + predicate="http://www.w3.org/2000/01/rdf-schema#subClassOf" + ) + ) + ) + > 1 + ) + def test_excelparser_only_classes(repo_dir: "Path") -> None: """This loads the excelfile used and tests that the resulting ontology prior