Skip to content

Commit

Permalink
Removing OntologyReasoner, renaming FIC to StructuralReasoner
Browse files Browse the repository at this point in the history
  • Loading branch information
alkidbaci committed Oct 29, 2024
1 parent 70bcddd commit ea15d30
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 71 deletions.
5 changes: 2 additions & 3 deletions examples/ontology_reasoning.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from owlapy.owl_individual import OWLNamedIndividual
from owlapy.owl_ontology_manager import OntologyManager
from owlapy.owl_property import OWLDataProperty, OWLObjectProperty
from owlapy.owl_reasoner import OntologyReasoner, FastInstanceCheckerReasoner
from owlapy.owl_reasoner import StructuralReasoner

data_file = '../KGs/Test/test_ontology.owl'
NS = 'http://www.semanticweb.org/stefan/ontologies/2023/1/untitled-ontology-11#'
Expand Down Expand Up @@ -126,11 +126,10 @@
onto.add_axiom(OWLSubClassOfAxiom(R, r5Q))
onto.add_axiom(OWLSubClassOfAxiom(ST, U))

base_reasoner = OntologyReasoner(onto)

# ---------------------------------------- Reasoning ----------------------------------------

reasoner = FastInstanceCheckerReasoner(onto, base_reasoner)
reasoner = StructuralReasoner(onto)

# Instances
t1 = list(reasoner.instances(N))
Expand Down
17 changes: 1 addition & 16 deletions owlapy/owl_reasoner.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,7 @@
_P = TypeVar('_P', bound=OWLPropertyExpression)


class OntologyReasoner(AbstractOWLReasoner):
__slots__ = '_ontology', '_world'
# TODO: CD: We will remove owlready2 from owlapy
_ontology: Ontology
_world: owlready2.World

def __init__(self, ontology: Ontology):
"""
Base reasoner in Ontolearn, used to reason in the given ontology.
Args:
ontology: The ontology that should be used by the reasoner.
"""


class FastInstanceCheckerReasoner(AbstractOWLReasoner):
class StructuralReasoner(AbstractOWLReasoner):
"""Tries to check instances fast (but maybe incomplete)."""
__slots__ = '_ontology', '_world'\
'_ind_set', '_cls_to_ind', \
Expand Down
30 changes: 13 additions & 17 deletions tests/test_owlapy_ontology_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import unittest

from pandas import Timedelta
from owlapy.owl_reasoner import FastInstanceCheckerReasoner, OntologyReasoner, SyncReasoner
from owlapy.owl_reasoner import StructuralReasoner, SyncReasoner
from owlapy.providers import owl_datatype_max_inclusive_restriction, owl_datatype_min_inclusive_restriction, \
owl_datatype_min_max_exclusive_restriction, owl_datatype_min_max_inclusive_restriction

Expand Down Expand Up @@ -36,7 +36,7 @@ def test_equivalent_classes(self):
ns = "http://dl-learner.org/mutagenesis#"
mgr = OntologyManager()
onto = mgr.load_ontology(IRI.create("file://KGs/Mutagenesis/mutagenesis.owl"))
reasoner = OntologyReasoner(onto)
reasoner = StructuralReasoner(onto)

atom = OWLClass(IRI(ns, 'Atom'))
bond = OWLClass(IRI(ns, 'Bond'))
Expand Down Expand Up @@ -80,7 +80,7 @@ def test_sub_classes(self):
ns = "http://dl-learner.org/mutagenesis#"
mgr = OntologyManager()
onto = mgr.load_ontology(IRI.create("file://KGs/Mutagenesis/mutagenesis.owl"))
reasoner = OntologyReasoner(onto)
reasoner = StructuralReasoner(onto)

bond = OWLClass(IRI(ns, 'Bond'))
ball3 = OWLClass(IRI(ns, 'Ball3'))
Expand Down Expand Up @@ -141,7 +141,7 @@ def test_super_classes(self):
ns = "http://dl-learner.org/mutagenesis#"
mgr = OntologyManager()
onto = mgr.load_ontology(IRI.create("file://KGs/Mutagenesis/mutagenesis.owl"))
reasoner = OntologyReasoner(onto)
reasoner = StructuralReasoner(onto)

bond = OWLClass(IRI(ns, 'Bond'))
ball3 = OWLClass(IRI(ns, 'Ball3'))
Expand Down Expand Up @@ -203,7 +203,7 @@ def test_sub_object_properties(self):
ns = "http://www.biopax.org/examples/glycolysis#"
mgr = OntologyManager()
onto = mgr.load_ontology(IRI.create("file://KGs/Biopax/biopax.owl"))
reasoner = OntologyReasoner(onto)
reasoner = StructuralReasoner(onto)

participants = OWLObjectProperty(IRI.create(ns, 'PARTICIPANTS'))
target_props = frozenset({OWLObjectProperty(IRI(ns, 'COFACTOR')),
Expand All @@ -218,7 +218,7 @@ def test_instances(self):
ns = "http://example.com/father#"
mgr = OntologyManager()
onto = mgr.load_ontology(IRI.create("file://KGs/Family/father.owl"))
reasoner = OntologyReasoner(onto)
reasoner = StructuralReasoner(onto)

inst = frozenset(reasoner.instances(OWLThing))
target_inst = frozenset({OWLNamedIndividual(IRI(ns, 'anna')),
Expand All @@ -238,7 +238,7 @@ def test_types(self):
ns = "http://example.com/father#"
mgr = OntologyManager()
onto = mgr.load_ontology(IRI.create("file://KGs/Family/father.owl"))
reasoner = OntologyReasoner(onto)
reasoner = StructuralReasoner(onto)

types = frozenset(reasoner.types(OWLNamedIndividual(IRI.create(ns, 'stefan'))))
target_types = frozenset({OWLThing, OWLClass(IRI(ns, 'male')), OWLClass(IRI(ns, 'person'))})
Expand All @@ -248,7 +248,7 @@ def test_object_values(self):
ns = "http://example.com/father#"
mgr = OntologyManager()
onto = mgr.load_ontology(IRI.create("file://KGs/Family/father.owl"))
reasoner = OntologyReasoner(onto)
reasoner = StructuralReasoner(onto)

stefan = OWLNamedIndividual(IRI.create(ns, 'stefan'))
markus = OWLNamedIndividual(IRI.create(ns, 'markus'))
Expand Down Expand Up @@ -308,8 +308,7 @@ def test_data_values(self):
ns = "http://dl-learner.org/mutagenesis#"
mgr = OntologyManager()
onto = mgr.load_ontology(IRI.create("file://KGs/Mutagenesis/mutagenesis.owl"))
base_reasoner = OntologyReasoner(onto)
reasoner = FastInstanceCheckerReasoner(onto, base_reasoner)
reasoner = StructuralReasoner(onto)

d100_1 = OWLNamedIndividual(IRI.create(ns, 'd100_1'))
charge = OWLDataProperty(IRI.create(ns, 'charge'))
Expand Down Expand Up @@ -338,8 +337,7 @@ def test_all_data_values(self):
ns = "http://dl-learner.org/mutagenesis#"
mgr = OntologyManager()
onto = mgr.load_ontology(IRI.create("file://KGs/Mutagenesis/mutagenesis.owl"))
base_reasoner = OntologyReasoner(onto)
reasoner = FastInstanceCheckerReasoner(onto, base_reasoner)
reasoner = StructuralReasoner(onto)

charge = OWLDataProperty(IRI.create(ns, 'charge'))
super_charge = OWLDataProperty(IRI.create(ns, 'super_charge'))
Expand All @@ -359,8 +357,7 @@ def test_ind_object_properties(self):
ns = "http://example.com/father#"
mgr = OntologyManager()
onto = mgr.load_ontology(IRI.create("file://KGs/Family/father.owl"))
base_reasoner = OntologyReasoner(onto)
reasoner = FastInstanceCheckerReasoner(onto, base_reasoner)
reasoner = StructuralReasoner(onto)

stefan = OWLNamedIndividual(IRI.create(ns, 'stefan'))
has_child = OWLObjectProperty(IRI.create(ns, 'hasChild'))
Expand All @@ -380,8 +377,7 @@ def test_ind_data_properties(self):
ns = "http://dl-learner.org/mutagenesis#"
mgr = OntologyManager()
onto = mgr.load_ontology(IRI.create("file://KGs/Mutagenesis/mutagenesis.owl"))
base_reasoner = OntologyReasoner(onto)
reasoner = FastInstanceCheckerReasoner(onto, base_reasoner)
reasoner = StructuralReasoner(onto)

d100_1 = OWLNamedIndividual(IRI.create(ns, 'd100_1'))
charge = OWLDataProperty(IRI.create(ns, 'charge'))
Expand All @@ -401,7 +397,7 @@ def test_add_remove_axiom(self):
ns = "http://example.com/father#"
mgr = OntologyManager()
onto = mgr.load_ontology(IRI.create("file://KGs/Family/father.owl"))
reasoner = OntologyReasoner(onto)
reasoner = StructuralReasoner(onto)

markus = OWLNamedIndividual(IRI.create(ns, 'markus'))
michelle = OWLNamedIndividual(IRI.create(ns, 'michelle'))
Expand Down
17 changes: 6 additions & 11 deletions tests/test_owlapy_owl2sparql_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from owlapy.owl_property import OWLObjectProperty

from owlapy.owl_ontology_manager import OntologyManager
from owlapy.owl_reasoner import OntologyReasoner, FastInstanceCheckerReasoner
from owlapy.owl_reasoner import StructuralReasoner
from owlapy.parser import DLSyntaxParser
from rdflib import Graph
from owlapy.converter import Owl2SparqlConverter
Expand Down Expand Up @@ -100,8 +100,7 @@ def test_Single(self):
# knowledge base - using OWLReasoner
mgr = OntologyManager()
onto = mgr.load_ontology(IRI.create(PATH_FAMILY))
base_reasoner = OntologyReasoner(onto)
family_kb_reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, negation_default=True)
family_kb_reasoner = StructuralReasoner(onto, negation_default=True)

ce_str = "Brother"
ce_parsed = DLSyntaxParser(namespace="http://www.benchmark.org/family#").parse_expression(expression_str=ce_str)
Expand Down Expand Up @@ -144,8 +143,7 @@ def test_Intersection(self):
# knowledge base - using OWLReasoner
mgr = OntologyManager()
onto = mgr.load_ontology(IRI.create(PATH_FAMILY))
base_reasoner = OntologyReasoner(onto)
family_kb_reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, negation_default=True)
family_kb_reasoner = StructuralReasoner(onto, negation_default=True)

ce_str = "Brother ⊓ Father"
ce_parsed = DLSyntaxParser(namespace="http://www.benchmark.org/family#").parse_expression(expression_str=ce_str)
Expand All @@ -172,8 +170,7 @@ def test_Union(self):
# knowledge base - using OWLReasoner
mgr = OntologyManager()
onto = mgr.load_ontology(IRI.create(PATH_FAMILY))
base_reasoner = OntologyReasoner(onto)
family_kb_reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, negation_default=True)
family_kb_reasoner = StructuralReasoner(onto, negation_default=True)

ce_str = "Sister ⊔ Mother"
ce_parsed = DLSyntaxParser(namespace="http://www.benchmark.org/family#").parse_expression(expression_str=ce_str)
Expand Down Expand Up @@ -201,8 +198,7 @@ def test_Complement(self):
# knowledge base - using OWLReasoner
mgr = OntologyManager()
onto = mgr.load_ontology(IRI.create(PATH_FAMILY))
base_reasoner = OntologyReasoner(onto)
family_kb_reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, negation_default=True)
family_kb_reasoner = StructuralReasoner(onto, negation_default=True)

ce_str = "¬Mother"
ce_parsed = DLSyntaxParser(namespace="http://www.benchmark.org/family#").parse_expression(expression_str=ce_str)
Expand Down Expand Up @@ -230,8 +226,7 @@ def test_Exists(self):
# knowledge base - using OWLReasoner
mgr = OntologyManager()
onto = mgr.load_ontology(IRI.create(PATH_FAMILY))
base_reasoner = OntologyReasoner(onto)
family_kb_reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, negation_default=True)
family_kb_reasoner = StructuralReasoner(onto, negation_default=True)

ce_str = "∃hasChild.Male"
ce_parsed = DLSyntaxParser(namespace="http://www.benchmark.org/family#").parse_expression(expression_str=ce_str)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
from owlapy.owl_property import OWLObjectInverseOf, OWLObjectProperty, OWLDataProperty
from owlready2.prop import DataProperty

from owlapy.owl_reasoner import FastInstanceCheckerReasoner, OntologyReasoner
from owlapy.owl_reasoner import StructuralReasoner

from owlapy.providers import owl_datatype_min_max_inclusive_restriction, owl_datatype_min_max_exclusive_restriction, \
owl_datatype_max_inclusive_restriction


class Owlapy_FastInstanceChecker_Test(unittest.TestCase):
class Owlapy_StructuralReasoner_Test(unittest.TestCase):
# noinspection DuplicatedCode
def test_instances(self):
NS = "http://example.com/father#"
Expand All @@ -31,8 +31,7 @@ def test_instances(self):
female = OWLClass(IRI.create(NS, 'female'))
has_child = OWLObjectProperty(IRI(NS, 'hasChild'))

base_reasoner = OntologyReasoner(onto)
reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner)
reasoner = StructuralReasoner(onto)

self.assertEqual([], list(reasoner.sub_object_properties(has_child, direct=True)))

Expand Down Expand Up @@ -82,9 +81,8 @@ def test_complement(self):
female = OWLClass(IRI.create(NS, 'female'))
has_child = OWLObjectProperty(IRI(NS, 'hasChild'))

base_reasoner = OntologyReasoner(onto)
reasoner_nd = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, negation_default=True)
reasoner_open = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, negation_default=False)
reasoner_nd = StructuralReasoner(onto, negation_default=True)
reasoner_open = StructuralReasoner(onto, negation_default=False)

self.assertEqual(set(reasoner_nd.instances(male)), set(reasoner_nd.instances(OWLObjectComplementOf(female))))
self.assertEqual(set(reasoner_nd.instances(female)), set(reasoner_nd.instances(OWLObjectComplementOf(male))))
Expand All @@ -108,8 +106,7 @@ def test_all_values(self):

has_child = OWLObjectProperty(IRI(NS, 'hasChild'))

base_reasoner = OntologyReasoner(onto)
reasoner_nd = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, negation_default=True)
reasoner_nd = StructuralReasoner(onto, negation_default=True)

# note, these answers are all wrong under OWA
no_child = frozenset(reasoner_nd.instances(OWLObjectAllValuesFrom(property=has_child, filler=OWLNothing)))
Expand All @@ -125,8 +122,7 @@ def test_complement2(self):
male = OWLClass(IRI.create(NS, 'male'))
female = OWLClass(IRI.create(NS, 'female'))

base_reasoner = OntologyReasoner(onto)
reasoner_open = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, negation_default=False)
reasoner_open = StructuralReasoner(onto, negation_default=False)

# Should be empty under open world assumption
self.assertEqual(set(), set(reasoner_open.instances(OWLObjectComplementOf(female))))
Expand All @@ -141,8 +137,7 @@ def test_cardinality_restrictions(self):
atom = OWLClass(IRI.create(NS, 'Atom'))
has_atom = OWLObjectProperty(IRI(NS, 'hasAtom'))

base_reasoner = OntologyReasoner(onto)
reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, negation_default=True)
reasoner = StructuralReasoner(onto, negation_default=True)

inst = frozenset(reasoner.instances(OWLObjectExactCardinality(cardinality=2,
property=has_atom,
Expand Down Expand Up @@ -178,8 +173,7 @@ def test_data_properties(self):
logp = OWLDataProperty(IRI(NS, 'logp'))
charge = OWLDataProperty(IRI(NS, 'charge'))

base_reasoner = OntologyReasoner(onto)
reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, negation_default=True)
reasoner = StructuralReasoner(onto, negation_default=True)

self.assertEqual([], list(reasoner.sub_data_properties(act, direct=True)))

Expand Down Expand Up @@ -274,8 +268,7 @@ class birthDateTime(DataProperty):
michelle = OWLNamedIndividual(IRI(NS, 'michelle'))
martin = OWLNamedIndividual(IRI(NS, 'martin'))

base_reasoner = OntologyReasoner(onto)
reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner)
reasoner = StructuralReasoner(onto)

restriction = owl_datatype_min_max_exclusive_restriction(date(year=1995, month=6, day=12),
date(year=1999, month=3, day=2))
Expand Down Expand Up @@ -312,8 +305,7 @@ def test_sub_property_inclusion(self):
onto.add_axiom(OWLSubDataPropertyOfAxiom(charge, super_charge))

# sub_property = True
base_reasoner = OntologyReasoner(onto)
reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, sub_properties=True)
reasoner = StructuralReasoner(onto, sub_properties=True)

# object property
ce = OWLObjectIntersectionOf([compound, OWLObjectSomeValuesFrom(super_has_structure, benzene)])
Expand All @@ -326,8 +318,7 @@ def test_sub_property_inclusion(self):
self.assertEqual(len(individuals), 75)

# sub_property = False
base_reasoner = OntologyReasoner(onto)
reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, sub_properties=False)
reasoner = StructuralReasoner(onto, sub_properties=False)

# object property
ce = OWLObjectIntersectionOf([compound, OWLObjectSomeValuesFrom(super_has_structure, benzene)])
Expand Down Expand Up @@ -356,8 +347,7 @@ def test_inverse(self):
OWLNamedIndividual(IRI.create(ns, 'stefan')),
OWLNamedIndividual(IRI.create(ns, 'markus'))}

base_reasoner = OntologyReasoner(onto)
reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, sub_properties=False)
reasoner = StructuralReasoner(onto, sub_properties=False)

expr = OWLObjectSomeValuesFrom(has_child, OWLThing)
expr_inverse = OWLObjectSomeValuesFrom(OWLObjectInverseOf(has_child_inverse), OWLThing)
Expand All @@ -383,7 +373,7 @@ def test_inverse(self):
self.assertEqual(parents_expr_inverse, frozenset())

# True (sub properties taken into account)
reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, sub_properties=True)
reasoner = StructuralReasoner(onto, sub_properties=True)
expr = OWLObjectSomeValuesFrom(super_has_child, OWLThing)
expr_inverse = OWLObjectSomeValuesFrom(OWLObjectInverseOf(super_has_child_inverse), OWLThing)
parents_expr = frozenset(reasoner.instances(expr))
Expand Down

0 comments on commit ea15d30

Please sign in to comment.