Skip to content

Commit

Permalink
Merge pull request #52 from dice-group/refactor_and_fix_iri
Browse files Browse the repository at this point in the history
infer_save() implemented to infer different types of axioms
  • Loading branch information
Demirrr authored Jul 19, 2024
2 parents 7076c8f + 5103e53 commit 18a2b1e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,21 @@ class. In the above examples we have introduced 3 types of class expressions:
Like we showed in this example, you can create all kinds of class expressions using the
OWL objects in [owlapy api](https://dice-group.github.io/owlapy/autoapi/owlapy/index.html).

Missing class assertions can be easily inferred
Many axioms can automatically inferred with a selected reasoner
```python
from owlapy.owlapi_adaptor import OWLAPIAdaptor
adaptor = OWLAPIAdaptor(path="..", name_reasoner="HermiT")

adaptor = OWLAPIAdaptor(path="KGs/Family/family-benchmark_rich_background.owl", name_reasoner="Pellet")
# Infer missing class assertions
adaptor.generate_inferred_class_assertion_axioms(output="...")
adaptor.infer_and_save(output_path="KGs/Family/inferred_family-benchmark_rich_background.ttl",
output_format="ttl",
inference_types=[
"InferredClassAssertionAxiomGenerator",
"InferredEquivalentClassAxiomGenerator",
"InferredDisjointClassesAxiomGenerator",
"InferredSubClassAxiomGenerator",
"InferredInverseObjectPropertiesAxiomGenerator",
"InferredEquivalentClassAxiomGenerator"])
adaptor.stopJVM()
```

Expand Down
40 changes: 38 additions & 2 deletions owlapy/owlapi_adaptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,42 @@ def _setup(self):
# A manchester renderer to render owlapi ce to manchester syntax
self.renderer = ManchesterOWLSyntaxOWLObjectRendererImpl()

def generate_inferred_class_assertion_axioms(self, output="temp.ttl", format:str=None):
def infer_and_save(self, output_path:str=None, output_format:str=None, inference_types:list[str]=None):
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.formats import TurtleDocumentFormat, RDFXMLDocumentFormat, OWLXMLDocumentFormat

if output_format == "ttl" or output_format == "turtle":
document_format = TurtleDocumentFormat()
elif output_format == "rdf/xml":
document_format = RDFXMLDocumentFormat()
elif output_format == "owl/xml":
document_format = OWLXMLDocumentFormat()
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):
generators.add(java_object)

iog = InferredOntologyGenerator(self.reasoner, generators)
inferredAxiomsOntology = self.manager.createOntology()
iog.fillOntology(self.manager.getOWLDataFactory(), inferredAxiomsOntology);
inferredOntologyFile = File(output_path)
inferredOntologyFile = inferredOntologyFile.getAbsoluteFile()
outputStream = FileOutputStream(inferredOntologyFile)
self.manager.saveOntology(inferredAxiomsOntology, document_format, outputStream);

def generate_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.
Expand All @@ -134,7 +169,7 @@ def generate_inferred_class_assertion_axioms(self, output="temp.ttl", format:str
output : str, optional
The name of the file where the inferred axioms will be saved. Default is "temp.ttl".
format : str, optional
output_format : str, optional
The format in which to save the inferred axioms. Supported formats are:
- "ttl" or "turtle" for Turtle format
- "rdf/xml" for RDF/XML format
Expand Down Expand Up @@ -173,6 +208,7 @@ def generate_inferred_class_assertion_axioms(self, output="temp.ttl", format:str

# generators.add(InferredSubClassAxiomGenerator())
generators.add(InferredClassAssertionAxiomGenerator())

iog = InferredOntologyGenerator(self.reasoner, generators)
inferredAxiomsOntology = self.manager.createOntology()
iog.fillOntology(self.manager.getOWLDataFactory(), inferredAxiomsOntology);
Expand Down

0 comments on commit 18a2b1e

Please sign in to comment.