From 60e113ff47e666c6dd38b27e79ad9771bfed8208 Mon Sep 17 00:00:00 2001 From: Caglar Demir Date: Fri, 8 Nov 2024 09:58:38 +0100 Subject: [PATCH] save_owl_class_expressions and test included --- owlapy/util_owl_static_funcs.py | 46 ++++++++++++++++++++++++++++++ tests/test_save_owl_expressions.py | 25 ++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 owlapy/util_owl_static_funcs.py create mode 100644 tests/test_save_owl_expressions.py diff --git a/owlapy/util_owl_static_funcs.py b/owlapy/util_owl_static_funcs.py new file mode 100644 index 00000000..3ed34a92 --- /dev/null +++ b/owlapy/util_owl_static_funcs.py @@ -0,0 +1,46 @@ +from .owl_ontology import Ontology +from .owl_ontology_manager import OntologyManager +from typing import List +from .class_expression import OWLClassExpression, OWLClass +from .iri import IRI +from .owl_axiom import OWLEquivalentClassesAxiom + +def save_owl_class_expressions(expressions: OWLClassExpression | List[OWLClassExpression], + path: str = 'predictions', + rdf_format: str = 'rdfxml', + namespace:str=None) -> None: + """ + + """ + + assert isinstance(expressions, OWLClassExpression) or isinstance(expressions[0], + OWLClassExpression), "expressions must be either OWLClassExpression or a list of OWLClassExpression" + assert rdf_format == 'rdfxml', f'Format {rdf_format} not implemented. Please use rdfxml' + + if isinstance(expressions, OWLClassExpression): + expressions = [expressions] + + namespace= 'https://dice-research.org/predictions#' if namespace is None else namespace + assert "#" == namespace[-1], "namespace must end with #" + # () + manager = OntologyManager() + # () + ontology:Ontology = manager.create_ontology(namespace) + # () Iterate over concepts + for th, i in enumerate(expressions): + cls_a = OWLClass(IRI.create(namespace, str(th))) + equivalent_classes_axiom = OWLEquivalentClassesAxiom([cls_a, i]) + try: + ontology.add_axiom(equivalent_classes_axiom) + except AttributeError: + print(traceback.format_exc()) + print("Exception at creating OWLEquivalentClassesAxiom") + print(equivalent_classes_axiom) + print(cls_a) + print(i) + print(expressions) + exit(1) + print(ontology) + ontology.save(path=path, inplace=False, rdf_format=rdf_format) + + # ontology.save(IRI.create(path)) diff --git a/tests/test_save_owl_expressions.py b/tests/test_save_owl_expressions.py new file mode 100644 index 00000000..3beda87b --- /dev/null +++ b/tests/test_save_owl_expressions.py @@ -0,0 +1,25 @@ +from owlapy.util_owl_static_funcs import save_owl_class_expressions +from owlapy.class_expression import OWLClass, OWLObjectIntersectionOf, OWLObjectSomeValuesFrom +from owlapy.owl_property import OWLObjectProperty +from owlapy import owl_expression_to_sparql, owl_expression_to_dl +from owlapy.owl_ontology_manager import OntologyManager +from owlapy.owl_axiom import OWLDeclarationAxiom, OWLClassAssertionAxiom +from owlapy.owl_individual import OWLNamedIndividual, IRI +import rdflib + +class TestRunningExamples: + def test_readme(self): + # Using owl classes to create a complex class expression + male = OWLClass("http://example.com/society#male") + hasChild = OWLObjectProperty("http://example.com/society#hasChild") + hasChild_male = OWLObjectSomeValuesFrom(hasChild, male) + teacher = OWLClass("http://example.com/society#teacher") + teacher_that_hasChild_male = OWLObjectIntersectionOf([hasChild_male, teacher]) + + expressions= [male, teacher_that_hasChild_male] + save_owl_class_expressions(expressions=expressions, + namespace="https://ontolearn.org/predictions#", + path="owl_class_expressions.owl", + rdf_format= 'rdfxml') + g=rdflib.Graph().parse("owl_class_expressions.owl") + assert len(g)==22 \ No newline at end of file