Reload ontology from database / clear cache

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Reload ontology from database / clear cache

humorloos
I am modifying my ontology directly in the database using triggers. However, due to owlready2's caching mechanism the removed entities are not removed from the ontology when I remove them from the database. How can I clear owlready2's cache or reload the ontology from the database to bring the ontology into a state where it reflects the changes made to the database?
Reply | Threaded
Open this post in threaded view
|

Re: Reload ontology from database / clear cache

humorloos
Found a way after almost a day of trying:

    def reload(self) -> None:
        """
        Reloads the world from the sqlite backend
        """
        self.save()
        self.close()
        self.graph = None
        self.set_backend(filename=self.filename)

Is there a better way to do it?
Reply | Threaded
Open this post in threaded view
|

Re: Reload ontology from database / clear cache

Jiba
Administrator
Hi,

Closing the database and reloading it is probably the safest solution.

Otherwise, depending on what you modified in the database, you can clear the cached value manually:

 * if you added / removed relation (on individuals), you can simply delete the individual's attributes (e.g. del my_individual.my_prop). Owlready will reload the values from the database automatically.

 * if you added / removed is-a relation on classes, you need to manually update the list of parent classes (.is_a), or to discard the cached Python object copy (which is in the dict default_world._entities, mapping storid to cached entities).

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Reload ontology from database / clear cache

humorloos
I just found out that there was still a problem with reloading. In addition to the method in my world subclass above, I had to add this method to my ontology subclass to avoid entities in the ontology's cache to cause problems:

    def reload(self) -> None:
        """
        Reloads the ontology from the smr world
        """
        self._destroy_cached_entities()
        self.world.reload()

Manually clearing the cache is out of question for me since the deletion of entities is done by triggers in my sqlite database and there are conditions that are too expensive to check in the code again.