From 2e80feb9f346ce3ad815fd51a934568644581f2e Mon Sep 17 00:00:00 2001 From: francescalb Date: Wed, 14 Feb 2024 09:34:13 +0100 Subject: [PATCH 1/2] Added keep-python_names as arguemtn in save. This is done, because from owlready0.45 python names are added as triplets automatically. This is something we usually do not want to save. --- ontopy/excelparser.py | 11 +++--- ontopy/ontology.py | 34 ++++++++++++++++++- .../result_ontology/fromexcelonto.ttl | 4 ++- .../fromexcelonto_only_classes.ttl | 4 ++- tests/test_excelparser/test_excelparser.py | 13 ++++++- 5 files changed, 56 insertions(+), 10 deletions(-) diff --git a/ontopy/excelparser.py b/ontopy/excelparser.py index eff2f5657..af22591de 100755 --- a/ontopy/excelparser.py +++ b/ontopy/excelparser.py @@ -550,15 +550,14 @@ def get_metadata_from_dataframe( # pylint: disable=too-many-locals,too-many-bra # Add versionInfo try: - _add_literal( - metadata, - onto.metadata.versionInfo, - "Ontology version Info", - metadata=True, - only_one=True, + name_list = _parse_literal( + metadata, "Ontology version Info", metadata=True ) + onto.metadata.versionInfo.append(name_list[0]) except AttributeError: pass + + onto.set_version(onto.get_version()) return onto, catalog diff --git a/ontopy/ontology.py b/ontopy/ontology.py index bb0dd7f42..977480fea 100644 --- a/ontopy/ontology.py +++ b/ontopy/ontology.py @@ -97,7 +97,7 @@ def get_ontology( version of EMMO. Until first stable release emmo-inferred and emmo-development will be the same. OntologyClass: If given and `base_iri` doesn't correspond - to an existing ontology, a new ontology is created of + an existing ontology, a new ontology is created of this Ontology subclass. Defaults to `ontopy.Ontology`. label_annotations: Sequence of label IRIs used for accessing entities in the ontology given that they are in the ontology. @@ -871,6 +871,7 @@ def save( write_catalog_file=False, append_catalog=False, catalog_file="catalog-v001.xml", + keep_python_names=False, ) -> Path: """Writes the ontology to file. @@ -931,6 +932,29 @@ def save( "'Known issues' section of the README." ) ) + if not keep_python_names: + ontocopy = self.copy() + ontocopy._del_data_triple_spod( + p=ontocopy._abbreviate( + "http://www.lesfleursdunormal.fr/static/_downloads/" + "owlready_ontology.owl#python_name" + ) + ) + returnpath = ontocopy.save( + filename=filename, + format=format, + dir=dir, + mkdir=mkdir, + overwrite=overwrite, + recursive=recursive, + squash=squash, + write_catalog_file=write_catalog_file, + append_catalog=append_catalog, + catalog_file=catalog_file, + keep_python_names=True, + ) + return returnpath + revmap = {value: key for key, value in FMAP.items()} if filename is None: if format: @@ -980,6 +1004,7 @@ def save( recursive=False, squash=False, write_catalog_file=False, + keep_python_names=keep_python_names, ) if write_catalog_file: @@ -1070,7 +1095,14 @@ def copy(self): recursive=True, write_catalog_file=True, mkdir=True, + keep_python_names=True, ) + # copy all in dirname to new directory + newdir = Path("/home/flb/projects/Team4.0/EMMOntoPy/temp/newonto") + import shutil + + shutil.copytree(dirname, newdir) + print(filename) ontology = get_ontology(filename).load() ontology.name = self.name return ontology diff --git a/tests/test_excelparser/result_ontology/fromexcelonto.ttl b/tests/test_excelparser/result_ontology/fromexcelonto.ttl index 56c85c7bc..5c77febfb 100644 --- a/tests/test_excelparser/result_ontology/fromexcelonto.ttl +++ b/tests/test_excelparser/result_ontology/fromexcelonto.ttl @@ -4,6 +4,7 @@ @prefix owl: . @prefix rdfs: . @prefix skos: . +@prefix xsd: . a owl:Ontology ; dcterms:contributor "SINTEF"@en, @@ -14,7 +15,8 @@ dcterms:title "A test domain ontology"@en ; owl:imports , ; - owl:versionInfo "0.01"@en . + owl:versionIRI ; + owl:versionInfo "0.01"^^xsd:string . :EMMO_0264be35-e8ad-5b35-a1a3-84b37bde22d1 a owl:Class ; emmo:EMMO_967080e5_2f42_4eb2_a3a9_c58143e835f9 "Temporal pattern occurring in a time interval"@en ; diff --git a/tests/test_excelparser/result_ontology/fromexcelonto_only_classes.ttl b/tests/test_excelparser/result_ontology/fromexcelonto_only_classes.ttl index 0b73b8ee3..cf8c0908a 100644 --- a/tests/test_excelparser/result_ontology/fromexcelonto_only_classes.ttl +++ b/tests/test_excelparser/result_ontology/fromexcelonto_only_classes.ttl @@ -4,6 +4,7 @@ @prefix owl: . @prefix rdfs: . @prefix skos: . +@prefix xsd: . a owl:Ontology ; dcterms:contributor "SINTEF"@en, @@ -14,7 +15,8 @@ dcterms:title "A test domain ontology"@en ; owl:imports , ; - owl:versionInfo "0.01"@en . + owl:versionIRI ; + owl:versionInfo "0.01"^^xsd:string . :EMMO_0264be35-e8ad-5b35-a1a3-84b37bde22d1 a owl:Class ; emmo:EMMO_967080e5_2f42_4eb2_a3a9_c58143e835f9 "Temporal pattern occurring in a time interval"@en ; diff --git a/tests/test_excelparser/test_excelparser.py b/tests/test_excelparser/test_excelparser.py index 7d823304d..48ed4594b 100644 --- a/tests/test_excelparser/test_excelparser.py +++ b/tests/test_excelparser/test_excelparser.py @@ -32,7 +32,18 @@ def test_excelparser(repo_dir: "Path") -> None: repo_dir / "tests" / "test_excelparser" / "onto_update.xlsx" ) ontology, catalog, errors = create_ontology_from_excel(xlspath, force=True) - # ontology.save("test.ttl") # used for printing new ontology when debugging + # ontology.save("test.ttl", keep_python_names=False) # used for printing new ontology when debugging + ontology._del_data_triple_spod( + p=ontology._abbreviate( + "http://www.lesfleursdunormal.fr/static/_downloads/" + "owlready_ontology.owl#python_name" + ) + ) + + ontology.save( + "test.ttl", keep_python_names=True + ) # used for printing new ontology when debugging + assert onto == ontology assert errors.keys() == { "already_defined", From ae9d1d71bdc2366d961444821a614d6d4158d19b Mon Sep 17 00:00:00 2001 From: francescalb Date: Thu, 15 Feb 2024 16:54:55 +0100 Subject: [PATCH 2/2] Commented out some saving statements --- ontopy/ontology.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ontopy/ontology.py b/ontopy/ontology.py index 977480fea..ed5ea4c8c 100644 --- a/ontopy/ontology.py +++ b/ontopy/ontology.py @@ -1098,13 +1098,15 @@ def copy(self): keep_python_names=True, ) # copy all in dirname to new directory - newdir = Path("/home/flb/projects/Team4.0/EMMOntoPy/temp/newonto") - import shutil + # newdir = Path("/home/flb/projects/Team4.0/EMMOntoPy/temp/newonto") + # import shutil - shutil.copytree(dirname, newdir) - print(filename) + # shutil.copytree(dirname, newdir) + # print(filename) ontology = get_ontology(filename).load() ontology.name = self.name + ontology.iri = self.iri + ontology.base_iri = self.base_iri return ontology def get_imported_ontologies(self, recursive=False):