Skip to content

Commit

Permalink
Updated test/examples using SyncReasoner
Browse files Browse the repository at this point in the history
  • Loading branch information
alkidbaci committed Aug 20, 2024
1 parent 78d9aef commit 518ad88
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 31 deletions.
29 changes: 14 additions & 15 deletions examples/comparing_adaptor_reasoners.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from owlapy.owl_property import OWLObjectProperty
from owlapy.owlapi_adaptor import OWLAPIAdaptor
from owlapy.owl_reasoner import SyncReasoner
from owlapy.iri import IRI
from owlapy.class_expression import OWLClass, OWLObjectIntersectionOf, OWLObjectAllValuesFrom, OWLObjectComplementOf
from owlapy.providers import owl_datatype_min_exclusive_restriction
from owlapy.class_expression import OWLClass, OWLObjectAllValuesFrom, OWLObjectComplementOf
import time
ontology_location = "../KGs/Carcinogenesis/carcinogenesis.owl"

Expand All @@ -11,21 +10,21 @@
i3 = set()
i4 = set()

for reasoner in ["HermiT", "Pellet", "JFact", "Openllet"]:
adaptor = OWLAPIAdaptor(ontology_location, reasoner)

ce = OWLObjectAllValuesFrom(property=OWLObjectProperty(IRI('http://dl-learner.org/carcinogenesis#','hasAtom')),
for rsn in ["HermiT", "Pellet", "JFact", "Openllet"]:
reasoner = SyncReasoner(ontology_location, rsn)
# TODO AB: needs a more complex class expression to show the specific differences of the reasoners
ce = OWLObjectAllValuesFrom(property=OWLObjectProperty(IRI('http://dl-learner.org/carcinogenesis#', 'hasAtom')),
filler=OWLObjectComplementOf(OWLClass(IRI('http://dl-learner.org/carcinogenesis#',
'Sulfur-75'))))

if reasoner == "HermiT":
i1 = set(adaptor.instances(ce))
elif reasoner == "Pellet":
i2 = set(adaptor.instances(ce))
elif reasoner == "JFact":
i3 = set(adaptor.instances(ce))
elif reasoner == "Openllet":
i4 = set(adaptor.instances(ce))
if rsn == "HermiT":
i1 = set(reasoner.instances(ce))
elif rsn == "Pellet":
i2 = set(reasoner.instances(ce))
elif rsn == "JFact":
i3 = set(reasoner.instances(ce))
elif rsn == "Openllet":
i4 = set(reasoner.instances(ce))

print("Hermit-Pellet:")
[print(_) for _ in i1-i2]
Expand Down
17 changes: 9 additions & 8 deletions examples/using_owlapi_adaptor.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
from owlapy.owlapi_adaptor import OWLAPIAdaptor
from owlapy.owl_reasoner import SyncReasoner
from owlapy.iri import IRI
from owlapy.class_expression import OWLClass, OWLObjectIntersectionOf
from owlapy.static_funcs import stopJVM

ontology_location = "../KGs/Family/family-benchmark_rich_background.owl"

# Start an adaptor session and perform your operations.
adaptor = OWLAPIAdaptor(ontology_location, "HermiT")
# Create an SyncReasoner
reasoner = SyncReasoner(ontology_location, "HermiT")
# Check ontology consistency
print(f"Is the given ontology consistent? --> {adaptor.has_consistent_ontology()}")
print(f"Is the given ontology consistent? --> {reasoner.has_consistent_ontology()}")

# Construct an owlapy class expression
brother = OWLClass(IRI.create("http://www.benchmark.org/family#Brother"))
father = OWLClass(IRI.create("http://www.benchmark.org/family#Father"))
brother_and_father = OWLObjectIntersectionOf([brother, father])

# Find individual belonging to that class expression
instances = adaptor.instances(brother_and_father)
instances = reasoner.instances(brother_and_father)
print("----------------------")
print("Individuals that are brother and father at the same time:")
[print(_) for _ in instances]

# Map the class expression from owlapy to owlapi
py_to_pi = adaptor.mapper.map_(brother_and_father)
py_to_pi = reasoner.mapper.map_(brother_and_father)

# Map the class expression from owlapi to owlapy
pi_to_py = adaptor.mapper.map_(py_to_pi)
pi_to_py = reasoner.mapper.map_(py_to_pi)
print("----------------------")
print(f"Owlapy ce: {pi_to_py}")

# Stop the JVM to free the associated resources.
adaptor.stopJVM() # or jpype.shutdownJVM()
stopJVM() # or jpype.shutdownJVM()

12 changes: 4 additions & 8 deletions tests/test_sync_reasoner.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from owlapy.iri import IRI
from owlapy.owl_axiom import OWLDisjointClassesAxiom, OWLDeclarationAxiom, OWLClassAssertionAxiom
from owlapy.owl_individual import OWLNamedIndividual
from owlapy.owl_ontology_manager import OntologyManager, SyncOntologyManager
from owlapy.owl_ontology_manager import OntologyManager
from owlapy.owl_property import OWLDataProperty
from owlapy.owl_reasoner import SyncReasoner
from owlapy.providers import owl_datatype_min_inclusive_restriction
Expand All @@ -20,9 +20,7 @@ class TestSyncReasoner(unittest.TestCase):
charge = OWLDataProperty(IRI.create(ns, "charge"))
has_charge_more_than_0_85 = OWLDataSomeValuesFrom(charge, owl_datatype_min_inclusive_restriction(0.85))
ce = OWLObjectIntersectionOf([nitrogen38, has_charge_more_than_0_85])
manager = SyncOntologyManager()
onto = manager.load_ontology(IRI.create(ontology_path))
reasoner = SyncReasoner(onto)
reasoner = SyncReasoner(ontology_path)

def test_consistency_check(self):
self.assertEqual(self.reasoner.has_consistent_ontology(), True)
Expand All @@ -40,9 +38,7 @@ def test_inconsistency_check(self):
manager.add_axiom(onto, OWLClassAssertionAxiom(new_individual, carbon230))

manager.save_ontology(onto, IRI.create("file:/test.owl"))
som = SyncOntologyManager()
onto2 = som.load_ontology(IRI.create("test.owl"))
reasoner = SyncReasoner(onto2)
reasoner = SyncReasoner("test.owl")
self.assertEqual(reasoner.has_consistent_ontology(), False)
os.remove("test.owl")

Expand Down Expand Up @@ -79,7 +75,7 @@ def test_conversion(self):

class_expression = data_factory.getOWLObjectIntersectionOf(nitrogen_class, some_values_from)

# compare them with the adaptor converted expression
# compare them with the converted expression
ce_converted = self.reasoner.mapper.map_(self.ce)
print(ce_converted)
print(class_expression)
Expand Down

0 comments on commit 518ad88

Please sign in to comment.