Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Demirrr committed Sep 18, 2024
1 parent 2a1243a commit 628bb22
Showing 1 changed file with 57 additions and 51 deletions.
108 changes: 57 additions & 51 deletions examples/ontology_reasoning_retrieval_agreements_and_runtimes.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,28 @@
""" A script to show that OWL Reasoners return the same retrieval results in different runtimes """
import time

from owlapy.class_expression import OWLClass, OWLObjectSomeValuesFrom, OWLObjectAllValuesFrom
from owlapy.class_expression import (OWLClass, OWLObjectSomeValuesFrom, OWLObjectAllValuesFrom,
OWLObjectIntersectionOf, OWLObjectUnionOf)
from owlapy.owl_ontology_manager import OntologyManager
from owlapy.owl_reasoner import SyncReasoner
from owlapy.utils import concept_reducer_properties

from owlapy.utils import concept_reducer_properties, concept_reducer
from owlapy import owl_expression_to_dl
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from tqdm import tqdm

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

owl_reasoners = dict()
owl_reasoners["HermiT"] = SyncReasoner(ontology=ontology_path, reasoner="HermiT")
owl_reasoners["Pellet"] = SyncReasoner(ontology=ontology_path, reasoner="Pellet")
owl_reasoners["JFact"] = SyncReasoner(ontology=ontology_path, reasoner="JFact")
owl_reasoners["Openllet"] = SyncReasoner(ontology=ontology_path, reasoner="Openllet")
onto = OntologyManager().load_ontology(ontology_path)
c: OWLClass
###################################################################
# GENERATE ALCQ CONCEPTS TO EVALUATE RETRIEVAL PERFORMANCES
# (3) R: Extract object properties.
object_properties = {i for i in onto.object_properties_in_signature()}
# (4) R⁻: Inverse of object properties.
object_properties_inverse = {i.get_inverse_property() for i in object_properties}
# (5) R*: R UNION R⁻.
object_properties_and_inverse = object_properties.union(object_properties_inverse)
# (6) NC: Named owl concepts.
nc = {i for i in onto.classes_in_signature()}
# (7) NC⁻: Complement of NC.
nnc = {i.get_object_complement_of() for i in nc}
# (8) \exist r. C s.t. C \in NC and r \in R* .
exist_nc = concept_reducer_properties(
concepts=nc,
properties=object_properties_and_inverse,
cls=OWLObjectSomeValuesFrom)
# (9) \forall r. C s.t. C \in NC and r \in R* .
forall_nc = concept_reducer_properties(
concepts=nc,
properties=object_properties_and_inverse,
cls=OWLObjectAllValuesFrom,
)


def eval_reasoners(iter_owl_exp, mapping):
def eval_reasoners(iter_owl_exp, mapping,info:str=""):
results = dict()
runtime_results = dict()
for c in tqdm(iter_owl_exp):
for c in (tqdm_bar:=tqdm(iter_owl_exp)):
for name_i, reasoner_i in mapping.items():
start_time_i = time.time()
result_reasoner_i = {i.str for i in reasoner_i.instances(c)}
runtime_results.setdefault(name_i, []).append(time.time() - start_time_i)

tqdm_bar.set_description_str(f"{owl_expression_to_dl(c)}\t")

for name_j, reasoner_j in mapping.items():
if name_i == name_j:
continue # Skip self-comparison
Expand Down Expand Up @@ -108,21 +78,57 @@ def plot_similarity_btw_reasoners(results):
plt.ylabel('Reasoner')
plt.show()

ontology_path = "../KGs/Family/father.owl"

# EVAL Named Concepts
print("Evaluation over named concepts...")
similarity_results, average_runtime_owl_reasoners = eval_reasoners(nc, owl_reasoners)
owl_reasoners = dict()
owl_reasoners["HermiT"] = SyncReasoner(ontology=ontology_path, reasoner="HermiT")
owl_reasoners["Pellet"] = SyncReasoner(ontology=ontology_path, reasoner="Pellet")
owl_reasoners["JFact"] = SyncReasoner(ontology=ontology_path, reasoner="JFact")
owl_reasoners["Openllet"] = SyncReasoner(ontology=ontology_path, reasoner="Openllet")
onto = OntologyManager().load_ontology(ontology_path)
c: OWLClass
# () C: OWL Class.
c = {i for i in onto.classes_in_signature()}
similarity_results, average_runtime_owl_reasoners = eval_reasoners(c, owl_reasoners)
plot_similarity_btw_reasoners(similarity_results)
plot_runtimes(average_runtime_owl_reasoners, title="Avg Runtime of Reasoners on Named Concepts")
plot_runtimes(average_runtime_owl_reasoners, title="Avg Runtime of Reasoners on Classes")

# EVAL Negated Concepts
print("Evaluation over negated named concepts...")
similarity_results, average_runtime_owl_reasoners = eval_reasoners(nnc, owl_reasoners)
# () Negated OWL CLasses.
nc = {i.get_object_complement_of() for i in c}
similarity_results, average_runtime_owl_reasoners = eval_reasoners(nc, owl_reasoners)
plot_similarity_btw_reasoners(similarity_results)
plot_runtimes(average_runtime_owl_reasoners, title="Avg Runtime of Reasoners on Negated Named Concepts")
plot_runtimes(average_runtime_owl_reasoners, title="Avg Runtime of Reasoners on Negated Classes")
# () Intersection of OWL Classes
intersections_classes = concept_reducer(c, opt=OWLObjectIntersectionOf)
similarity_results, average_runtime_owl_reasoners = eval_reasoners(intersections_classes, owl_reasoners)
plot_similarity_btw_reasoners(similarity_results)
plot_runtimes(average_runtime_owl_reasoners, title="Avg Runtime of Reasoners on Intersection of Classes")
# () Union of OWL Classes
unions_classes = concept_reducer(c, opt=OWLObjectUnionOf)
similarity_results, average_runtime_owl_reasoners = eval_reasoners(unions_classes, owl_reasoners)
plot_similarity_btw_reasoners(similarity_results)
plot_runtimes(average_runtime_owl_reasoners, title="Avg Runtime of Reasoners on Union of Classes")
# () Object Property Restrictions - Existential Quantification
object_properties = {i for i in onto.object_properties_in_signature()}
exist_c = concept_reducer_properties(concepts=c, properties=object_properties, cls=OWLObjectSomeValuesFrom)
similarity_results, average_runtime_owl_reasoners = eval_reasoners(exist_c, owl_reasoners)
plot_similarity_btw_reasoners(similarity_results)
plot_runtimes(average_runtime_owl_reasoners, title="Avg Runtime of Reasoners on Existential Quantifiers")
# () Object Property Restrictions - Universal Quantification
forall_c = concept_reducer_properties(concepts=c, properties=object_properties, cls=OWLObjectAllValuesFrom)
similarity_results, average_runtime_owl_reasoners = eval_reasoners(forall_c, owl_reasoners)
plot_similarity_btw_reasoners(similarity_results)
plot_runtimes(average_runtime_owl_reasoners, title="Avg Runtime of Reasoners on Universal Quantifiers")

# EVAL Exist R. NC
print("Evaluation over existential object property restrictions... takes some time")
similarity_results, average_runtime_owl_reasoners = eval_reasoners(exist_nc, owl_reasoners)
# () Object Property Restrictions - Existential Quantification
inverse_object_properties = {i.get_inverse_property() for i in onto.object_properties_in_signature()}
exist_inverse_c = concept_reducer_properties(concepts=c, properties=inverse_object_properties, cls=OWLObjectSomeValuesFrom)
similarity_results, average_runtime_owl_reasoners = eval_reasoners(exist_inverse_c, owl_reasoners)
plot_similarity_btw_reasoners(similarity_results)
plot_runtimes(average_runtime_owl_reasoners, title="Avg Runtime of Reasoners on Existential Quantifiers with inverse properties")
# () Object Property Restrictions - Universal Quantification
forall_inverse_c = concept_reducer_properties(concepts=c, properties=inverse_object_properties, cls=OWLObjectAllValuesFrom)
similarity_results, average_runtime_owl_reasoners = eval_reasoners(forall_inverse_c, owl_reasoners)
plot_similarity_btw_reasoners(similarity_results)
plot_runtimes(average_runtime_owl_reasoners, title="Avg Runtime of Reasoners on OWLObjectSomeValuesFrom")
plot_runtimes(average_runtime_owl_reasoners, title="Avg Runtime of Reasoners on Universal Quantifiers with inverse properties")

0 comments on commit 628bb22

Please sign in to comment.