Skip to content

Commit

Permalink
save_owl_class_expressions and test included
Browse files Browse the repository at this point in the history
  • Loading branch information
Demirrr committed Nov 8, 2024
1 parent 56888c7 commit 60e113f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
46 changes: 46 additions & 0 deletions owlapy/util_owl_static_funcs.py
Original file line number Diff line number Diff line change
@@ -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))
25 changes: 25 additions & 0 deletions tests/test_save_owl_expressions.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 60e113f

Please sign in to comment.