I have a question regarding the destroying of an ontology.
I run the code in Spyder IDE, where I want to make multiple executions of the code. While starting a new execution of the code (in the same python instance), I always create the ontology with: owl.get_ontology( "http://test-ontology.de/pml#" ).
What I could observe was that after the first run, the ontology was extended and not newly created. Therefore, I always have to start the spyder console new.
For the next approach, I tried to destroy the ontology in the last line of the code with:
The problem with this approach is, that the code somehow does not work properly within the second execution and the ontology can not be returned after the creation.
Therefore, I wanted to ask how I could correctly implement the destroy function for multiple executions within the same python instance.
Currently, I am bound to an NDA and can not provide any specific code samples.
The normal way to destroy an ontology is Ontology.destroy(), as you did. Without it, multiple call to get_ontology() will return the same ontology object.
Ontology.destroy() removes all RDF triples from the ontology, and remove all cached entities from that ontology. However, it DOES NOT modify previously loaded entities. In particular, if you kept reference to entities, you can still access them and obtain error if you modify them.
Similarly, if the ontology destroyed modifies entities belonging to another ontology, those modification may not be removed after destroying the ontology. You can use Ontology.destroy(update_relation = True) to update relations on individuals belonging to another ontology. However, there is currently no such ability to update classes or properties.
I am sorry, I cannot help you more without additional information on the exact problem you encounter during the second execution.
thank you for your detailed answer and sorry for the late response.
In my case, I am working with the Spyder IDE and after each run (same console without a restart in the Spyder IDE) the previous ontology was used. These lead us to the case, that some instances have been put twice or more into a class (depending on the number of executions).
For example (Pseudo code): We have a Class teacher
1. Run execution: teacher.instances() -> Null
2. Run execution: teacher.instances() -> ["Mr. Peter"]
3. Run execution: teacher.instances() -> ["Mr. Peter","Mr. Peter"]
4. Run execution: teacher.instances() -> ["Mr. Peter","Mr. Peter","Mr. Peter"]
Because of this behavior, I would like to "destroy" the ontology after a run execution or at the end of the program.
I could solve my problem, by generating a random name for the ontology i.e., owl.get_ontology( "http://test-ontology.de/pml#RandomName").
But now I am running again in this problem and the random name solution is not working.
I think my best advice would be to restart the Python interpreter if possible.
Otherwise, you can destroy the ontology with onto_temp.destroy(), and then recreate it.
Another solution is to give a specific name to each individual, e.g. create them with Teacher("specific name"). When a specific name is given, Owlready does not create a new individual if an individual already exists with that name in the namespace. Instead, it returns the existing individual (and update it if property values are given too).