dice-group / owlapy

OWLAPY is a Python Framework for creating and manipulating OWL Ontologies.
MIT License
16 stars 1 forks source link

A function to check the intances of OWLClass or OWLCLassExpression #80

Open sapkotaruz11 opened 21 hours ago

sapkotaruz11 commented 21 hours ago

Is there a function which we can directly use to determine if an individual is an instance of a OWLClass or OWL-Class Expression? Something similar to isinstance function in python?

Example use case:

individual = OWLNamedIndividual(IRI('http://www.benchmark.org/family#','F2F36'))
parent = OWLClass(IRI('http://www.benchmark.org/family#','Parent'))
is_instance_of(individual, parent)
Demirrr commented 15 hours ago

Yes indeed. For example, the following example is provided in the README.

from owlapy.owl_ontology_manager import OntologyManager
from owlapy.owl_reasoner import SyncReasoner
from owlapy.static_funcs import stopJVM

ontology_path = "KGs/Family/family-benchmark_rich_background.owl"
# Available OWL Reasoners: 'HermiT', 'Pellet', 'JFact', 'Openllet'
sync_reasoner = SyncReasoner(ontology = ontology_path, reasoner="Pellet")
onto = OntologyManager().load_ontology(ontology_path)
# Iterate over defined owl Classes in the signature
for i in onto.classes_in_signature():
    # Performing type inference with Pellet
    instances=sync_reasoner.instances(i,direct=False)
    print(f"Class:{i}\t Num instances:{len(instances)}")
stopJVM()

You just need to check whether individual is an element of sync_reasoner.instances(parent) .

We have also few more examples ,e.g.

  1. Our doc finding instances
  2. You can also find few examples in our tests (e.g. this one ).
alkidbaci commented 14 hours ago

In addition to Demir's suggestion you can also check if the class assertion axiom for that individual + ce is entailed by the ontology axioms.

ontology_location = "../KGs/Family/family-benchmark_rich_background.owl"
reasoner = SyncReasoner(ontology_location)
individual = OWLNamedIndividual(IRI('http://www.benchmark.org/family#','F2F36'))
assertion_axiom = OWLClassAssertionAxiom(individual, OWLClass(IRI('http://www.benchmark.org/family#','Parent')))
is_entailed = reasoner.is_entailed(assertion_axiom)
print(is_entailed)

Keep in mind that SyncReasoner uses open world assumption and it may not entail an axiom that would otherwise be entailed in CWA. I suggest testing it with some complex class expressions before you continue with your experiments.