Reset sync_reasoner inferences

702 views Asked by At

I am using the owlready2 python module on a local ontology.
I have connected an API endpoint, to submit queries on this ontology.
I need to submit some queries on the original ontology and some other on the updated (with the inferences) ontology.

When I use the sync_reasoner() function, the ontology is updated with the inferences made by HermiT (i.e. the default reasoner).

My issue is, that the inferences made by the reasoner persist among different calls to the attached function.

Is there a workaround to force reset the inferred properties?

def function():
    onto = get_ontology("file:///path/file.owl").load()
    namespace = onto.get_namespace("http://namespace")
    do_operations_with_original_ontology()
    with namespace:
       sync_reasoner()
       do_operations_with_UPDATED_ontology()
       return None

Thank you for considering my question,
Argyris

1

There are 1 answers

1
gpotdevin On

Though I did not use extensively the reasoner functionalities of owlready2 I believe this is the same as for any ontology update using owlready2.

Basically in owlready2, to separate different ontologies or different versions of the same ontology (potentially making use of different namespaces) you need to put them in different "worlds". The syntax is described here.

Here is some code based on the documentation examples to give you an idea of the syntax

from owlready2 import *

world = World()
onto = world.get_ontology("http://test.org/onto.owl")

with onto:
    class Drug(Thing):
        pass
    class ActivePrinciple(Thing):
        pass
    class has_for_active_principle(Drug >> ActivePrinciple):
        pass
    class someActivePrinciple(ActivePrinciple):
        pass
    class MyDrug(Drug):
        has_for_active_principle = [someActivePrinciple] #this one has some property restriction

# let's separate the worlds
world2 = World()
onto2 = world2.get_ontology("http://test.org/onto.owl")

with onto2:
    class Drug(Thing):
        pass
    class ActivePrinciple(Thing):
        pass
    class has_for_active_principle(Drug >> ActivePrinciple):
        pass
    class someActivePrinciple(ActivePrinciple):
        pass
    class MyDrug(Thing): # not a subClass of Drug
        pass  # missing the has_for_active_principle restriction


# now we can save without mixing the ontologies
onto.save(file=r"c:\temp\owlready.rdf", format="rdfxml")
onto2.save(file=r"c:\temp\owlready2.rdf", format="rdfxml") 

Note that there is currently a bug which prevents from directly saving the "worlds", only the ontology can be saved, but the bug was already corrected in the development version. See the owlready forum relevant discussion