Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Owlapy mapper extension and infer methods of adapter #58

Merged
merged 6 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ from owlapy.owlapi_adaptor import OWLAPIAdaptor

adaptor = OWLAPIAdaptor(path="KGs/Family/family-benchmark_rich_background.owl", name_reasoner="Pellet")
# Infer missing class assertions
adaptor.infer_and_save(output_path="KGs/Family/inferred_family-benchmark_rich_background.ttl",
adaptor.infer_axioms_and_save(output_path="KGs/Family/inferred_family-benchmark_rich_background.ttl",
output_format="ttl",
inference_types=[
"InferredClassAssertionAxiomGenerator",
Expand Down
2 changes: 2 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@

html_favicon = '_static/images/favicon.ico'

html_extra_path = ["googlec4c425077889c69c.html"]

if stanford_theme_mod:
html_theme = 'sphinx_rtd_theme'

Expand Down
1 change: 1 addition & 0 deletions docs/googlec4c425077889c69c.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google-site-verification: googlec4c425077889c69c.html
7 changes: 4 additions & 3 deletions owlapy/owl_axiom.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,8 @@ class OWLAnnotationAssertionAxiom(OWLAnnotationAxiom):
_subject: OWLAnnotationSubject
_annotation: OWLAnnotation

def __init__(self, subject: OWLAnnotationSubject, annotation: OWLAnnotation):
def __init__(self, subject: OWLAnnotationSubject, annotation: OWLAnnotation,
annotations: Optional[Iterable['OWLAnnotation']] = None):
"""Get an annotation assertion axiom - with annotations.

Args:
Expand All @@ -732,7 +733,7 @@ def __init__(self, subject: OWLAnnotationSubject, annotation: OWLAnnotation):
"""
assert isinstance(subject, OWLAnnotationSubject)
assert isinstance(annotation, OWLAnnotation)

super().__init__(annotations)
self._subject = subject
self._annotation = annotation

Expand Down Expand Up @@ -769,7 +770,7 @@ def __hash__(self):
return hash((self._subject, self._annotation))

def __repr__(self):
return f'OWLAnnotationAssertionAxiom({self._subject}, {self._annotation})'
return f'OWLAnnotationAssertionAxiom({self._subject}, {self._annotation}, {self.annotations()})'
class OWLSubAnnotationPropertyOfAxiom(OWLAnnotationAxiom):
"""An annotation subproperty axiom SubAnnotationPropertyOf( AP1 AP2 ) states that the annotation property AP1 is
a subproperty of the annotation property AP2.
Expand Down
101 changes: 87 additions & 14 deletions owlapy/owlapi_adaptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import pkg_resources

from owlapy.class_expression import OWLClassExpression
from owlapy.owl_axiom import OWLAxiom
from owlapy.owl_individual import OWLNamedIndividual
from owlapy.owl_object import OWLEntity
from owlapy.owl_property import OWLDataProperty, OWLObjectProperty
from typing import List
from typing import List, Iterable


def to_list(stream_obj):
Expand Down Expand Up @@ -86,6 +87,30 @@ def _setup(self):
from owlapy.owlapi_mapper import OWLAPIMapper
from org.semanticweb.owlapi.apibinding import OWLManager
from java.io import File
from org.semanticweb.owlapi.util import (InferredClassAssertionAxiomGenerator, InferredSubClassAxiomGenerator,
InferredEquivalentClassAxiomGenerator,
InferredDisjointClassesAxiomGenerator,
InferredEquivalentDataPropertiesAxiomGenerator,
InferredEquivalentObjectPropertyAxiomGenerator,
InferredInverseObjectPropertiesAxiomGenerator,
InferredSubDataPropertyAxiomGenerator,
InferredSubObjectPropertyAxiomGenerator,
InferredDataPropertyCharacteristicAxiomGenerator,
InferredObjectPropertyCharacteristicAxiomGenerator)

self.inference_types_mapping = {"InferredClassAssertionAxiomGenerator": InferredClassAssertionAxiomGenerator(),
"InferredSubClassAxiomGenerator": InferredSubClassAxiomGenerator(),
"InferredDisjointClassesAxiomGenerator": InferredDisjointClassesAxiomGenerator(),
"InferredEquivalentClassAxiomGenerator": InferredEquivalentClassAxiomGenerator(),
"InferredInverseObjectPropertiesAxiomGenerator": InferredInverseObjectPropertiesAxiomGenerator(),
"InferredEquivalentDataPropertiesAxiomGenerator": InferredEquivalentDataPropertiesAxiomGenerator(),
"InferredEquivalentObjectPropertyAxiomGenerator": InferredEquivalentObjectPropertyAxiomGenerator(),
"InferredSubDataPropertyAxiomGenerator": InferredSubDataPropertyAxiomGenerator(),
"InferredSubObjectPropertyAxiomGenerator": InferredSubObjectPropertyAxiomGenerator(),
"InferredDataPropertyCharacteristicAxiomGenerator": InferredDataPropertyCharacteristicAxiomGenerator(),
"InferredObjectPropertyCharacteristicAxiomGenerator": InferredObjectPropertyCharacteristicAxiomGenerator(),
}

if self.name_reasoner == "HermiT":
from org.semanticweb.HermiT import ReasonerFactory
elif self.name_reasoner == "Pellet":
Expand Down Expand Up @@ -450,12 +475,65 @@ def types(self, i: OWLNamedIndividual, direct: bool = False):
yield from [self.mapper.map_(ind) for ind in
self.reasoner.getTypes(self.mapper.map_(i), direct).getFlattened()]

def infer_and_save(self, output_path: str = None, output_format: str = None, inference_types: list[str] = None):
def infer_axioms(self, inference_types: list[str]) -> Iterable[OWLAxiom]:
"""
Infer the specified inference type of axioms for the ontology managed by this instance's reasoner and
return them.

Args:
inference_types: Axiom inference types: Avaliable options (can set more than 1):
["InferredClassAssertionAxiomGenerator", "InferredSubClassAxiomGenerator",
"InferredDisjointClassesAxiomGenerator", "InferredEquivalentClassAxiomGenerator",
"InferredEquivalentDataPropertiesAxiomGenerator","InferredEquivalentObjectPropertyAxiomGenerator",
"InferredInverseObjectPropertiesAxiomGenerator","InferredSubDataPropertyAxiomGenerator",
"InferredSubObjectPropertyAxiomGenerator","InferredDataPropertyCharacteristicAxiomGenerator",
"InferredObjectPropertyCharacteristicAxiomGenerator"
]

Returns:
Iterable of inferred axioms.
"""
from java.util import ArrayList
from org.semanticweb.owlapi.util import InferredOntologyGenerator

generators = ArrayList()
for i in inference_types:
if java_object := self.inference_types_mapping.get(i, None):
generators.add(java_object)
iog = InferredOntologyGenerator(self.reasoner, generators)
inferred_axioms = list(iog.getAxiomGenerators())
for ia in inferred_axioms:
for axiom in ia.createAxioms(self.manager.getOWLDataFactory(), self.reasoner):
yield self.mapper.map_(axiom)

def infer_axioms_and_save(self, output_path: str = None, output_format: str = None, inference_types: list[str] = None):
"""
Generates inferred axioms for the ontology managed by this instance's reasoner and saves them to a file.
This function uses the OWL API to generate inferred class assertion axioms based on the ontology and reasoner
associated with this instance. The inferred axioms are saved to the specified output file in the desired format.

Args:
output_path : The name of the file where the inferred axioms will be saved.
output_format : The format in which to save the inferred axioms. Supported formats are:
- "ttl" or "turtle" for Turtle format
- "rdf/xml" for RDF/XML format
- "owl/xml" for OWL/XML format
If not specified, the format of the original ontology is used.
inference_types: Axiom inference types: Avaliable options (can set more than 1):
["InferredClassAssertionAxiomGenerator", "InferredSubClassAxiomGenerator",
"InferredDisjointClassesAxiomGenerator", "InferredEquivalentClassAxiomGenerator",
"InferredEquivalentDataPropertiesAxiomGenerator","InferredEquivalentObjectPropertyAxiomGenerator",
"InferredInverseObjectPropertiesAxiomGenerator","InferredSubDataPropertyAxiomGenerator",
"InferredSubObjectPropertyAxiomGenerator","InferredDataPropertyCharacteristicAxiomGenerator",
"InferredObjectPropertyCharacteristicAxiomGenerator"
]

Returns:
None (the file is saved to the specified directory)
"""
from java.io import File, FileOutputStream
from java.util import ArrayList
from org.semanticweb.owlapi.util import InferredSubClassAxiomGenerator, InferredClassAssertionAxiomGenerator
from org.semanticweb.owlapi.util import InferredOntologyGenerator, InferredEquivalentClassAxiomGenerator,InferredInverseObjectPropertiesAxiomGenerator
from org.semanticweb.owlapi.util import InferredDisjointClassesAxiomGenerator
from org.semanticweb.owlapi.util import InferredOntologyGenerator
from org.semanticweb.owlapi.formats import TurtleDocumentFormat, RDFXMLDocumentFormat, OWLXMLDocumentFormat
if output_format == "ttl" or output_format == "turtle":
document_format = TurtleDocumentFormat()
Expand All @@ -466,14 +544,9 @@ def infer_and_save(self, output_path: str = None, output_format: str = None, inf
else:
document_format = self.manager.getOntologyFormat(self.ontology)
generators = ArrayList()
inference_types_mapping = {"InferredClassAssertionAxiomGenerator": InferredClassAssertionAxiomGenerator(),
"InferredSubClassAxiomGenerator": InferredSubClassAxiomGenerator(),
"InferredDisjointClassesAxiomGenerator":InferredDisjointClassesAxiomGenerator(),
"InferredEquivalentClassAxiomGenerator":InferredEquivalentClassAxiomGenerator(),
"InferredInverseObjectPropertiesAxiomGenerator":InferredInverseObjectPropertiesAxiomGenerator(),
"InferredEquivalentClassAxiomGenerator":InferredEquivalentClassAxiomGenerator()}

for i in inference_types:
if java_object := inference_types_mapping.get(i, None):
if java_object := self.inference_types_mapping.get(i, None):
generators.add(java_object)
iog = InferredOntologyGenerator(self.reasoner, generators)
inferred_axioms_ontology = self.manager.createOntology()
Expand All @@ -482,7 +555,7 @@ def infer_and_save(self, output_path: str = None, output_format: str = None, inf
output_stream = FileOutputStream(inferred_ontology_file)
self.manager.saveOntology(inferred_axioms_ontology, document_format, output_stream)

def generate_inferred_class_assertion_axioms(self, output="temp.ttl", output_format: str = None):
def generate_and_save_inferred_class_assertion_axioms(self, output="temp.ttl", output_format: str = None):
"""
Generates inferred class assertion axioms for the ontology managed by this instance's reasoner and saves them to a file.
This function uses the OWL API to generate inferred class assertion axioms based on the ontology and reasoner
Expand All @@ -508,4 +581,4 @@ def generate_inferred_class_assertion_axioms(self, output="temp.ttl", output_for
>>> instance.generate_inferred_class_assertion_axioms(output="inferred_axioms.ttl", format="ttl")
This will save the inferred class assertion axioms to the file "inferred_axioms.ttl" in Turtle format.
"""
self.infer_and_save(output, output_format, ["InferredClassAssertionAxiomGenerator"])
self.infer_axioms_and_save(output, output_format, ["InferredClassAssertionAxiomGenerator"])
Loading
Loading