diff --git a/examples/ontology-from-excel/README.md b/examples/ontology-from-excel/README.md new file mode 100644 index 000000000..564100cb9 --- /dev/null +++ b/examples/ontology-from-excel/README.md @@ -0,0 +1,25 @@ +# Generate an ontology from excel + +This directory contains an example xlsx-file for how to document ontology entities (classes, object properties, annotation properties and data properties) in an Excel workbook. +This workbook can then be used to generate a new ontology or update an already existing ontology with new entities (existing entities are not updated). + +Please refer to the (documentation)[https://emmo-repo.github.io/EMMOntoPy/latest/api_reference/ontopy/excelparser/] for full explanation of capabilities. + +The file `tool/onto.xlsx` contains examples on how to do things correctly as well as incorrectly. +The tool will by default exit without generating the ontology if it detects concepts defined incorrectly. +However, if the argument force is set to True, it will skip concepts that are erroneously defined +and generate the ontology with what is availble. + +To run the tool directly +```console +cd tool # Since the excel file provides a relative path to an imported ontology +excel2onto onto.xlsx # This will fail +excel2onto --force onto.xlsx +``` +We suggest developing your excelsheet without fails as once it starts getting big it is difficult to see what is wrong or correct. + +It is also possible to generate the ontology in python. +Look at the script make_onto.py for an example. + +That should be it. +Good luck! diff --git a/examples/ontology-from-excel/make_microstructure_onto.py b/examples/ontology-from-excel/make_onto.py similarity index 54% rename from examples/ontology-from-excel/make_microstructure_onto.py rename to examples/ontology-from-excel/make_onto.py index 9e29fe073..0c644424c 100755 --- a/examples/ontology-from-excel/make_microstructure_onto.py +++ b/examples/ontology-from-excel/make_onto.py @@ -9,8 +9,14 @@ thisdir = Path(__file__).resolve().parent ontology, catalog, errdict = create_ontology_from_excel( - thisdir / "tool/microstructure.xlsx" + thisdir / "tool/onto.xlsx", + force=True, # Note will force generation of the ontology. ) -ontology.save("microstructure_ontology.ttl", format="turtle", overwrite=True) + +# Save the ontology and write out the catalog +ontology.save("onto.ttl", format="turtle", overwrite=True) write_catalog(catalog) + +# Look at the error dictionary +print(errdict.keys()) diff --git a/examples/ontology-from-excel/tool/imported_onto/catalog-v001.xml b/examples/ontology-from-excel/tool/imported_onto/catalog-v001.xml new file mode 100644 index 000000000..f97363ba2 --- /dev/null +++ b/examples/ontology-from-excel/tool/imported_onto/catalog-v001.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/examples/ontology-from-excel/tool/imported_onto/ontology.ttl b/examples/ontology-from-excel/tool/imported_onto/ontology.ttl new file mode 100644 index 000000000..1f16f8bf7 --- /dev/null +++ b/examples/ontology-from-excel/tool/imported_onto/ontology.ttl @@ -0,0 +1,18 @@ +@prefix : . +@prefix owl: . +@prefix rdf: . +@prefix xml: . +@prefix xsd: . +@prefix rdfs: . +@prefix skos: . +@base . + + rdf:type owl:Ontology ; + owl:versionIRI ; + owl:imports ; + owl:versionInfo "0.1.0" . + + +:testclass rdf:type owl:Class ; + rdfs:subClassOf owl:Thing ; + skos:prefLabel "TestClass"@en . diff --git a/examples/ontology-from-excel/tool/imported_onto/subontology.ttl b/examples/ontology-from-excel/tool/imported_onto/subontology.ttl new file mode 100644 index 000000000..023a198d3 --- /dev/null +++ b/examples/ontology-from-excel/tool/imported_onto/subontology.ttl @@ -0,0 +1,21 @@ +@prefix : . +@prefix owl: . +@prefix rdf: . +@prefix xml: . +@prefix xsd: . +@prefix rdfs: . +@prefix skos: . +@base . + + rdf:type owl:Ontology ; + owl:versionIRI . + + +# Annotations +skos:prefLabel rdf:type owl:AnnotationProperty . +skos:altLabel rdf:type owl:AnnotationProperty . + + +:testclass2 rdf:type owl:Class ; + rdfs:subClassOf owl:Thing ; + skos:prefLabel "TestClass2"@en . diff --git a/examples/ontology-from-excel/tool/microstructure.xlsx b/examples/ontology-from-excel/tool/microstructure.xlsx deleted file mode 100755 index 0e786cb02..000000000 Binary files a/examples/ontology-from-excel/tool/microstructure.xlsx and /dev/null differ diff --git a/mkdocs.yml b/mkdocs.yml index 93a8beb1d..58e2dc826 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -90,6 +90,7 @@ nav: - Examples: - EMMOdoc: examples/emmodoc/README.md - Jupyter visualization: examples/jupyter-visualization/README.md + - Ontology generation from Excel: examples/ontology-from-excel/README.md - ... | developers/** - Changelog: CHANGELOG.md - ... | api_reference/** diff --git a/tests/tools/test_excel2onto.py b/tests/tools/test_excel2onto.py index cd81bf325..3a047a75a 100644 --- a/tests/tools/test_excel2onto.py +++ b/tests/tools/test_excel2onto.py @@ -55,3 +55,10 @@ def test_run(get_tool: "Callable[[str], ModuleType]", tmpdir: "Path") -> None: str(test_file), ] ) + + # Test Error raised if ontology to be updated does not exist + with pytest.raises( + FileNotFoundError, + match="The output ontology to be updated does not exist", + ): + excel2onto.main([f"--output=onto_not_created.ttl", str(test_file)]) diff --git a/tools/excel2onto b/tools/excel2onto index 525fae061..8b144d1a1 100755 --- a/tools/excel2onto +++ b/tools/excel2onto @@ -80,12 +80,16 @@ def main(argv: list = None): except FileNotFoundError as err: if args.force: warnings.warn( - "Did not find the input ontology, " + "Did not find the output ontology to be updated, " "will fully generate a new one." ) input_ontology = None else: - raise err + raise FileNotFoundError( + "The output ontology to be updated " + "does not exist. Missing file is: ", + args.output, + ) from err try: ontology, catalog, _ = create_ontology_from_excel(