Skip to content

Commit

Permalink
Merge pull request #99 from dice-group/owl_static_funcs
Browse files Browse the repository at this point in the history
save_owl_class_expressions and test included
  • Loading branch information
Demirrr authored Nov 8, 2024
2 parents 56888c7 + 7eaf5be commit 5367a14
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 9 deletions.
8 changes: 0 additions & 8 deletions .github/workflows/ruff.yml

This file was deleted.

4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -e .
- name: Lint with ruff
run: |
ruff check owlapy --line-length=200
- name: Test with pytest
run: |
wget https://files.dice-research.org/projects/Ontolearn/KGs.zip
Expand Down
57 changes: 57 additions & 0 deletions owlapy/util_owl_static_funcs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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:
"""
Saves a set of OWL class expressions to an ontology file in RDF/XML format.
This function takes one or more OWL class expressions, creates an ontology,
and saves the expressions as OWL equivalent class axioms in the specified RDF format.
By default, it saves the file to the specified path using the 'rdfxml' format.
Args:
expressions (OWLClassExpression | List[OWLClassExpression]): A single or a list of OWL class expressions
to be saved as equivalent class axioms.
path (str, optional): The file path where the ontology will be saved. Defaults to 'predictions'.
rdf_format (str, optional): RDF serialization format for saving the ontology. Currently only
supports 'rdfxml'. Defaults to 'rdfxml'.
namespace (str, optional): The namespace URI used for the ontology. If None, defaults to
'https://dice-research.org/predictions#'. Must end with '#'.
Raises:
AssertionError: If `expressions` is neither an OWLClassExpression nor a list of OWLClassExpression.
AssertionError: If `rdf_format` is not 'rdfxml'.
AssertionError: If `namespace` does not end with a '#'.
Example:
>>> from some_module import OWLClassExpression
>>> expr1 = OWLClassExpression("SomeExpression1")
>>> expr2 = OWLClassExpression("SomeExpression2")
>>> save_owl_class_expressions([expr1, expr2], path="my_ontology.owl", rdf_format="rdfxml")
"""
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])
ontology.add_axiom(equivalent_classes_axiom)
ontology.save(path=path, inplace=False, rdf_format=rdf_format)
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"pandas>=1.5.0",
"requests>=2.32.3",
"rdflib>=6.0.2",
"ruff>=0.7.2",
"parsimonious>=0.8.1",
"pytest>=8.1.1",
"sortedcontainers>=2.4.0",
Expand Down
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 5367a14

Please sign in to comment.