-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from dice-group/owl_static_funcs
save_owl_class_expressions and test included
- Loading branch information
Showing
5 changed files
with
86 additions
and
9 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |