From 518ad88801d42b8e6bab968c5acde6d40cc8e655 Mon Sep 17 00:00:00 2001 From: Alkid Date: Tue, 20 Aug 2024 14:36:31 +0200 Subject: [PATCH] Updated test/examples using SyncReasoner --- examples/comparing_adaptor_reasoners.py | 29 ++++++++++++------------- examples/using_owlapi_adaptor.py | 17 ++++++++------- tests/test_sync_reasoner.py | 12 ++++------ 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/examples/comparing_adaptor_reasoners.py b/examples/comparing_adaptor_reasoners.py index d0c1c50..2522260 100644 --- a/examples/comparing_adaptor_reasoners.py +++ b/examples/comparing_adaptor_reasoners.py @@ -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" @@ -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] diff --git a/examples/using_owlapi_adaptor.py b/examples/using_owlapi_adaptor.py index 44d8203..88a6b40 100644 --- a/examples/using_owlapi_adaptor.py +++ b/examples/using_owlapi_adaptor.py @@ -1,13 +1,14 @@ -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")) @@ -15,19 +16,19 @@ 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() diff --git a/tests/test_sync_reasoner.py b/tests/test_sync_reasoner.py index 6a07c2b..3666f6c 100644 --- a/tests/test_sync_reasoner.py +++ b/tests/test_sync_reasoner.py @@ -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 @@ -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) @@ -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") @@ -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)