-
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.
save_owl_class_expressions and test included
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
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,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)) |
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 |