I am trying to import ontology files into an empty ontology file, then save this with a different name (model_name.owl in the following case). But the problem is I can't use those imported ontologies to instantiate. Below is my code;
onto = get_ontology("file://test_onto.owl")
onto.imported_ontologies = 
for i in glob.glob("OWL-xml_version/*.owl"):
i = get_ontology("file://%s"%i).load()
for j in test_model:
bess = loaded_ontology.PhysicalNode("batt",namespace = loaded_ontology)
However, it gives an error that "model_name has not object 'PhysicalNode'!". However, it is a class of one of the imported ontologies. Do I have to load imported ontologies separately or any idea what I am doing wrong?
Yes, you need to obtain PhysicalNode from its original ontology. Ontology import works like Python module importation with the syntax "import xxx", but not the syntax "from xxx import *". Therefore, the content of the imported ontology is not copied or duplicated in the importing ontology. It just mean that the imported ontology must be loaded when the importing ontology is loaded.
Your problem is possibly related to the fact that all the instances you create have the same name ("batt"), and thus the same IRI ("file://model_name.owl#batt"). In this case, Owlready does not create a new instance: it returns the previously created one. So you should have a single instance named "batt".
You can solve the problem by providing different names, or no names at all (Owlready will create a new one automatically).
Many thanks, this has worked to some extent.
Two main things;
1) imported IRIs are file paths not the base IRI in the imported files;
e.g. base IRI of the imported file is http://www.domain.com/core_onto However, in the saved owl file by owlready2 it is shown as
<owl:imports rdf:resource="file://C:\Users\ABC\Desktop\Excel_parser\OWL-xml_version\coreonto.owl"/> which is the path of that file, not the IRI.
Any idea why this is happening?
2) the saved owl file keeps updated with the instances. For my case, I will be saving with different names but how to avoid that? Shall we clear contents before creating onto2 (i.e. in your first program)?
> 1) imported IRIs are file paths not the base IRI in the imported files;
> e.g. base IRI of the imported file is http://www.domain.com/core_onto > However, in the saved owl file by owlready2 it is shown as
> which is the path of that file, not the IRI.
> Any idea why this is happening?
You need to provide the full IRI (and not a filename) at least the first time you create the ontology.
On the contrary, when calling .load(), the IRI will be extracted from the OWL file.
> 2) the saved owl file keeps updated with the instances. For my case, I will
> be saving with different names but how to avoid that? Shall we clear
> contents before creating onto2 (i.e. in your first program)?
Do you mean you want to create a new ontology, and to erase / overwrite the previous one ? In this case, do not call .load() after get_ontology().
So here is my code for creating a new ontology and then importing an already existing ontology in it.
from owlready2 import *
onto = get_ontology("http://www.domain.com/testing") ##creating a new ontology
onto.imported_ontologies = 
i = get_ontology("file://OWL-xml_version/abc.owl").load() ## loading an existing ontology
onto.imported_ontologies.append(i) # appending imported to the newly created owl file (onto).
onto.save("testing.owl") ## saving onto file
print("Exception in user code:")
The output (saved testing.owl) should have the imported IRI, instead of the file path. However, the onto file (i.e testing.owl) looks like this;