Importing and saving ontology files

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

Importing and saving ontology files

Waseem85
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()
        onto.imported_ontologies.append(i)
    print("saving file")
    onto.save("model_name.owl")
   
    loaded_ontology = get_ontology("file://model_name.owl").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?

Reply | Threaded
Open this post in threaded view
|

Re: Importing and saving ontology files

Jiba
Administrator
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.
Reply | Threaded
Open this post in threaded view
|

Re: Importing and saving ontology files

Waseem85
Ok. Thanks.
So when I do;

loaded_ontology = get_ontology("file://model_name.owl").load()
loaded_ontology_2 = get_ontology("file://OWL-xml_version/ABC.owl").load()
       
for j in test_model:
     bess = loaded_ontology_2.PhysicalNode("batt",namespace = loaded_ontology)
loaded_ontology.save("model_name_2.owl")

It did not save the newly instantiated object in the OWL file? Why is that or am I missing something?

Reply | Threaded
Open this post in threaded view
|

Re: Importing and saving ontology files

Jiba
Administrator
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).
Reply | Threaded
Open this post in threaded view
|

Re: Importing and saving ontology files

Waseem85
Unfortunately, both of these solutions (different name or no names at all) did not solve the problem :(

Any other ideas?
Reply | Threaded
Open this post in threaded view
|

Re: Importing and saving ontology files

Jiba
Administrator
I've tried to reproduce your problem, but without success.

I've created 2 ontologies with the following scripts:

from owlready2 import *
onto_path.append("/tmp")

onto1 = get_ontology("file:///tmp/onto1.owl")
with onto1:
        class MyClass(Thing): pass
onto1.save("/tmp/onto1.owl")
onto2 = get_ontology("file:///tmp/onto2.owl")
onto2.save("/tmp/onto2.owl")


And then I created 10 instances with a second script:

from owlready2 import *
onto_path.append("/tmp")

onto1 = get_ontology("file:///tmp/onto1.owl").load()
onto2 = get_ontology("file:///tmp/onto2.owl").load()
for j in range(10):
        bess = onto1.MyClass(namespace = onto2)
onto2.save("/tmp/onto2.owl")


But when I open onto2.owl, the 10 instances are present.

Could you adapt this test script so as it reproduce your problem ?
Reply | Threaded
Open this post in threaded view
|

Re: Importing and saving ontology files

Waseem85
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)?

Many thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Importing and saving ontology files

Jiba
Administrator
> 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?

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().

Best regards,
Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Importing and saving ontology files

Waseem85
So here is my code for creating a new ontology and then importing an already existing ontology in it.

from owlready2 import *

try:
    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
   
except:
    print("Exception in user code:")
    traceback.print_exc(file=sys.stdout)

   
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;

<?xml version="1.0"?>
<rdf:RDF xml:base="http://www.domain.com/testing"
         xmlns="http://www.domain.com/testing#"
         xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
         xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
         xmlns:owl="http://www.w3.org/2002/07/owl#">

<owl:Ontology rdf:about="http://www.domain.com/testing">
  <owl:imports rdf:resource="file://OWL-xml_version/abc.owl"/>  #### here you can see that it has written the path instead of IRI.
</owl:Ontology>


</rdf:RDF>


whereas, the IRI of the abc.owl is http://www.domainname.com/abc

Any ideas on what I am doing wrong?

Regards,
Reply | Threaded
Open this post in threaded view
|

Re: Importing and saving ontology files

Waseem85
I tried using IRI while loading the file, i.e. instead of doing

i = get_ontology("file://OWL-xml_version/abc.owl").load() ## loading an existing ontology


I did,

i = get_ontology("http://www.domainname.com/abc").load() ## loading an existing ontology


and now I can see the IRI in the onto file. Is this the right way of doing it and if yes, then how the newly created file (onto) will know the link as it does not know where abc.owl file is saved?

Regards,
Waseem
Reply | Threaded
Open this post in threaded view
|

Re: Importing and saving ontology files

Jiba
Administrator
Hello,

It is a valid way. Ontology abc will be searched in the paths in "onto_path", and, if not found, it will be downloaded from http://www.domainname.com/abc.

I still need more time to test the other way in your previous message.

Best regards,
Jean-Baptiste Lamy
MCF, LIMICS, Université Paris 13
Reply | Threaded
Open this post in threaded view
|

Re: Importing and saving ontology files

Jiba
Administrator
I have tested your script, but I cannot reproduce your problem.

Are you sure that the ontology stored in abc.owl has a real IRI (and not a filename) ? Could you send me the beginning of this ontology ? (I just need the <rdf:RDF> and <owl:Ontology> tags).

Best regards,
Jean-Baptiste Lamy
MCF, LIMICS, Université Paris 13
Reply | Threaded
Open this post in threaded view
|

Re: Importing and saving ontology files

Waseem85
Hello, here are the initial lines of the ontolgy showing the ontology IRI.

<?xml version="1.0"?>
<Ontology xmlns="http://www.w3.org/2002/07/owl#"
     xml:base="http://www.domainname.com/abc"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     ontologyIRI="http://www.domainname.com/abc">
    <Prefix name="" IRI="http://www.domainname.com/abc#"/>
    <Prefix name="owl" IRI="http://www.w3.org/2002/07/owl#"/>
    <Prefix name="rdf" IRI="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>
    <Prefix name="xml" IRI="http://www.w3.org/XML/1998/namespace"/>
    <Prefix name="xsd" IRI="http://www.w3.org/2001/XMLSchema#"/>
    <Prefix name="rdfs" IRI="http://www.w3.org/2000/01/rdf-schema#"/>


Many thanks.