Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting up a KnowledgeBase with SyncReasoner, getting errors in ClassHierarchy #507

Open
CronosC opened this issue Dec 18, 2024 · 1 comment

Comments

@CronosC
Copy link

CronosC commented Dec 18, 2024

Im trying to set up my project using ontolearn, specifically a KnowledgeBase using a SyncReasoner and I'm having trouble getting it to work. Here is a minimal example on the father.owl ontology and using ontolearn 0.8.1 and owlapy 1.3.3:

from ontolearn.knowledge_base import KnowledgeBase
from owlapy.owl_reasoner import SyncReasoner
from owlapy.owl_ontology_manager import SyncOntologyManager
from owlapy.class_expression import OWLClassExpression, OWLClass
from owlapy.iri import IRI

onto_file_path = "tests/data/father.owl"
ns = "http://example.com/father#"

manager = SyncOntologyManager()
onto = manager.load_ontology(onto_file_path)
reasoner = SyncReasoner(onto, reasoner="Openllet")

kb = KnowledgeBase(
    ontology=onto,
    reasoner=reasoner,
    load_class_hierarchy=True,
)
#kb = KnowledgeBase(
#    path=onto_file_path
#)

owl_expression = OWLClass(IRI(ns, "male"))
print(reasoner.instances(owl_expression))
for i in reasoner.instances(owl_expression):
    print(i)
print(kb.individuals(owl_expression))
for i in kb.individuals(owl_expression):
    print(i)

The stacktrace I get when I run this code is the following:

Traceback (most recent call last):
  File "/home/cwrk/Work/semanticlog/tests/debugging.py", line 14, in <module>
    kb = KnowledgeBase(
  File "/home/cwrk/miniconda3/envs/semlog/lib/python3.10/site-packages/ontolearn/knowledge_base.py", line 188, in __init__
    self.data_property_hierarchy) = init_hierarchy_instances(self.reasoner,
  File "/home/cwrk/miniconda3/envs/semlog/lib/python3.10/site-packages/ontolearn/utils/static_funcs.py", line 162, in init_hierarchy_instances
    class_hierarchy = ClassHierarchy(reasoner)
  File "/home/cwrk/miniconda3/envs/semlog/lib/python3.10/site-packages/owlapy/owl_hierarchy.py", line 286, in __init__
    super().__init__(OWLClass, arg)
  File "/home/cwrk/miniconda3/envs/semlog/lib/python3.10/site-packages/owlapy/owl_hierarchy.py", line 51, in __init__
    self._init(hier_down_gen)
  File "/home/cwrk/miniconda3/envs/semlog/lib/python3.10/site-packages/owlapy/owl_hierarchy.py", line 124, in _init
    _children_transitive(self._children_map_trans, ent=ent, seen_set=set())
  File "/home/cwrk/miniconda3/envs/semlog/lib/python3.10/site-packages/owlapy/owl_hierarchy.py", line 397, in _children_transitive
    _children_transitive(hier_trans, sub_ent, seen_set | {ent})
  File "/home/cwrk/miniconda3/envs/semlog/lib/python3.10/site-packages/owlapy/owl_hierarchy.py", line 394, in _children_transitive
    sub_classes_ent = frozenset(hier_trans[ent])
KeyError: OWLClass(IRI('http://www.w3.org/2002/07/owl#', 'Nothing'))

From the error I could gather that there is something wrong with how the class hierarchy of the KnowledgeBase gets initialized? If i set load_class_hierarchy=False it works, but then fails when I try to learn something. Alternatively, if I use something besides the SyncOntology/SyncReasoner it also works just fine. However, I need to be able to set the reasoner and thus need the SyncReasoner for my project.

Have I set something up wrong? Where are my mistakes? Are there some further resources, besides the documentation, examples and tests? So far I could not figure out how to make it work. Any help would be greatly appreciated.

@alkidbaci
Copy link
Collaborator

alkidbaci commented Dec 23, 2024

Hi @CronosC, your setup is fine and there is no mistake by your side.
Although SyncReasoner has been around for a while, it is considered experimental and we have not yet tested its integration with Ontolearn for any possible issue. However the error you are dealing is not directly related with the Ontolearn integration.

As you may have noticed we define hierarchies for classes and properties where the initialization includes setting transitive links between entities within a hierarchy. The error we have in hand here emerges from owlapi(the Java library we use in owlapy) where for some reason, bottom class/properties (owl:Nothing, owl:bottomObjectProperty, owl:bottomDataProperty) are considered a subclass/subproperty but not a class/property of the classes/properties in signature. However, we will look further into this issue and provide a solid solution on the next release.

Meanwhile if you need immediate solution you can make the following change in your local owlapy package:

# site-packages/owlapy/owl_hierarchy.py

# line 116-119
for ent, sub_it in ent_to_sub_entities.items(): 
+ sub_it_without_bottom_entity = set(sub_it) - {OWLNothing, OWLBottomObjectProperty, OWLBottomDataProperty} 
-  self._children_map_trans[ent] = set(sub_it)
+ self._children_map_trans[ent] = sub_it_without_bottom_entity
  self._parents_map_trans[ent] = set()

This is a temporary solution that I don't recommend anyone doing because its not tested, may potentially come with loss of information and the problem is still being investigated, but for now it will make your code work so you may continue with your experiments until we patch this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants