Loading OWL imports from local

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

Loading OWL imports from local

David
Hello,

First, thanks for this very nice library !
I'm trying to load ontologies in Owlready using the owl <import> tag.  I would like to use a local relative path. But for now, I cant manage to do it. I saw that this is possible with Protege.
Is it something that can be done with Owlready ?

Thanks !
David
Reply | Threaded
Open this post in threaded view
|

Re: Loading OWL imports from local

Peter
An ontology in owlready has an attribute ".imported_ontologies" which is a list, so you can append to it, as documented in https://pythonhosted.org/Owlready2/onto.html#importing-other-ontologies

e.g. onto.imported_ontologies.append(pizzaOnto)


a full code example for importing from a local file is something like this:





from owlready2 import *

default_world.set_backend(filename = "./importTest.file.sqlite3")

pizzaOwl = "file://C:/Users/Pedro/Documents/vocabs/owl/pizza.owl"
pizzaOnto = get_ontology(pizzaOwl).load()

onto = get_ontology("http://myOntology.org/example/one/")

# Annotation Properties
with onto:
    class myPrefLabel(AnnotationProperty):
        label = "my preferred label"

    class myAltLabel(AnnotationProperty):
        label = "my alternative label"

onto.imported_ontologies.append(pizzaOnto)
#-----------------------



default_world.save()


onto.save(file = r".\2020-01-08_owlready2_example.rdf", format = "rdfxml")
onto.save(file = r".\2020-01-08_owlready2_example.n3", format = "ntriples")

graph = default_world.as_rdflib_graph()

Query = '''prefix owl:<http://www.w3.org/2002/07/owl#>
           SELECT distinct ?ap WHERE {
           ?ap a owl:AnnotationProperty .
           } limit 20'''

results = graph.query(Query)
for i in results:
    print(i)

Reply | Threaded
Open this post in threaded view
|

Re: Loading OWL imports from local

Jiba
Administrator
In reply to this post by David
Hello,

You can use the "onto_path" global variable to define pathes were ontologies are searched.

The ontologies must be in one of the directories in onto_path, and they must have a filename corresponding to the end of their IRI (with an additional .owl if needed).

See the doc here:

https://owlready2.readthedocs.io/en/latest/onto.html#loading-an-ontology-from-owl-files

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Loading OWL imports from local

David
This post was updated on .
Thanks for your answers.
Here is a little more on the problem I don't understand.
I have this Ontology:

<rdf:RDF xmlns="https://myurl.com/ontologies/MyOntology.owl#"
     xml:base="https://myurl.com/ontologies/MyOntology.owl"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     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#"
     xmlns:MyOntology="https://myurl.com/ontologies/MyOntology.owl#">
    <owl:Ontology rdf:about="https://myurl.com/ontologies/MyOntology.owl#">
        <owl:imports rdf:resource="https://myurl.com/ontologies/MyOtherOntology.owl#"/>
    </owl:Ontology>
...
</rdf:RDF>

I want to import https://myurl.com/ontologies/MyOtherOntology.owl, but it fails. (When I load this ontology in python with get_ontology, the imported_ontologies attribute is an empty list).
If I change the value of rdf:about in <owl:Ontology rdf:about="https://myurl.com/ontologies/MyOntology.owl#"> to a non valid url, it works, my ontology is imported.
Do you know why ?

Edit: I changed the URI to https://myurl.com/ontologies/MyOntology.owl/ (With a / at the end) and it seems it work that way).
Reply | Threaded
Open this post in threaded view
|

Re: Loading OWL imports from local

Jiba
Administrator
Hi,

I think the problem is related to the trailing "#" in the ontology IRI: you should use "https://myurl.com/ontologies/MyOntology.owl" instead of "https://myurl.com/ontologies/MyOntology.owl#". For example:

<?xml version="1.0"?>
<rdf:RDF 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#"
         xml:base="https://myurl.com/ontologies/MyOntology2.owl"
         xmlns="https://myurl.com/ontologies/MyOntology2.owl#">

<owl:Ontology rdf:about="https://myurl.com/ontologies/MyOntology2.owl">
  <owl:imports rdf:resource="https://myurl.com/ontologies/MyOtherOntology2.owl"/>
</owl:Ontology>
</rdf:RDF>

Jiba