Support load() to provide explicit ontology file locations

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

Support load() to provide explicit ontology file locations

jo-fra
This post was updated on .
Hi and first of all thanks for providing this library!

I am facing some limitations with the loading mechanism that is currently implemented in Owlready2.
It would be great to provide explicit location paths for the ontology files that should be loaded.

E.g. I have an ontology importing-ontology.rdf that is importing the top level ontology http://rds.posccaesar.org/ontology/lis14/ont/core via the version IRI http://rds.posccaesar.org/ontology/lis14/ont/core/1.0:

<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.example.com/importing-ontology/"
     xml:base="http://www.example.com/importing-ontology/"
     ...
    <owl:Ontology rdf:about="http://www.example.com/importing-ontology">
        <owl:imports rdf:resource="http://rds.posccaesar.org/ontology/lis14/ont/core/1.0"/>
    </owl:Ontology>
</rdf:RDF>

Loading it via
from owlready2 import get_ontology
onto = get_ontology("file://importing-ontology.rdf").load()
would fail because the version IRI does not resolve to the actual RDF/XML file that would be available at the URL http://rds.posccaesar.org/ontology/lis14/ont/core/1.0/LIS-14.rdf

As I read in the documentation and other threads there are two options currently:

1) Adapt the import to reference the actual RDF/XML file url:
       
<owl:imports rdf:resource="http://rds.posccaesar.org/ontology/lis14/ont/core/1.0/LIS-14.rdf"/>

2) Save the ontology locally under the filename 1.0.rdf and append the local directory to onto_path

I think both solutions are not ideal.

For 1) if I'm not responsible for importing-ontology but just consuming it, and it is using the original version IRI import, I don't like to modify the import statement, since I would have to save (and keep in sync) a duplicated version of the importing-ontology.

For 2) the versioning scheme forces us to save it locally with the filename 1.0.rdf
. If there is another imported ontology, that is using the same versioning scheme, it has to be saved under the same filename. Although the files could be located in different directories, only the first 1.0.rdf would be considered during loading.

Therefore, I think it would be better to have the possibility to provide (additional) explicit paths to local files or URLs e.g. by extending the API of the load() method. I gave it a quick try by extending it with an additional argument that expects a dictionary that maps the IRI to a file path, similar like the PREDEFINED_ONTOLOGIES constant.
Usage example:

from owlready2 import get_ontology

# For now append # character to the IRI,
# because during loading of imported ontology
# the method get_ontology() appends # by default.
# This is not ideal for this case and should be resolved somehow...
paths = {
    "http://rds.posccaesar.org/ontology/lis14/ont/core/1.0#": "http://rds.posccaesar.org/ontology/lis14/ont/core/1.0/LIS-14.rdf",  # Mapping to remote url of RDF/XML file
    # "http://rds.posccaesar.org/ontology/lis14/ont/core/1.0#": "./LIS-14_1.0.rdf",  # Works also for local files
}
onto = get_ontology("file://importing-ontology.rdf").load(ontology_paths=paths)

What do you think?
Reply | Threaded
Open this post in threaded view
|

Re: Support load() to provide explicit ontology file locations

Jiba
Administrator
Hi,

A third option is to manually preload the imported ontology before loading the ontology itself. But I admit that it is not fully satisfying.

Possibly a simpler solution would be to modify PREDEFINED_ONTOLOGIES so as it also accepts absolute path (and not only path relative to owlready2/ontos), so as one can just update the dictionary with additional mappings?

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Support load() to provide explicit ontology file locations

jo-fra
Thanks for the suggestions. I would also prefer the solution of updating PREDEFINED_ONTOLOGIES with additional mappings.

Reply | Threaded
Open this post in threaded view
|

Re: Support load() to provide explicit ontology file locations

Jiba
Administrator
Hello,

I (finally) implemented that solution in the development version of Owlready on BitBucket.

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Support load() to provide explicit ontology file locations

jo-fra
I tried it and it works for absolute file paths. Great!

However, additional support for remote URLs would be awesome, e.g.

from owlready2 import PREDEFINED_ONTOLOGIES, get_ontology

# Absolute path works
# PREDEFINED_ONTOLOGIES[
#     "http://rds.posccaesar.org/ontology/lis14/ont/core/1.0"
# ] = "C:\work\code\owlready2-import-ontologies\LIS-14_1.0.rdf"

# Remote url does not work
PREDEFINED_ONTOLOGIES[
    "http://rds.posccaesar.org/ontology/lis14/ont/core/1.0"
] = "http://rds.posccaesar.org/ontology/lis14/ont/core/1.0/LIS-14.rdf"

onto = get_ontology("file://importing-ontology.rdf").load()

ehe
Reply | Threaded
Open this post in threaded view
|

Re: Support load() to provide explicit ontology file locations

ehe
For this I use a config file in my projects and save the import paths for each ontology, which are then always loaded before the actual ontology. When loading for the first time, the user is asked to specify the import paths.

    def load_file_ontology_with_dependencies(self, file_name, world = default_world):
        try:
            for imp in self.current_project["ontologies"][file_name]["imports"]:
                world.get_ontology(f"file://{imp}").load(only_local=True)
                log.info(f"Loading import: {imp}")
            onto = world.get_ontology(f"file://{file_name}").load(only_local=True)
            log.info(f"Ontology loaded: {onto.base_iri}")
            return onto
        except OwlReadyOntologyParsingError as e:
            dlg = wx.MessageDialog(None, f"{e} \n Would you like to specify a local path to this ontology?", 'Load Missing ontologies', wx.YES_NO | wx.ICON_QUESTION)
            if dlg.ShowModal() == wx.ID_YES:
                with wx.FileDialog(None, "Open Ontology", wildcard="Ontology files|*.owl;*.rdf",
                                   style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:

                    if fileDialog.ShowModal() == wx.ID_CANCEL:
                        return
                    pathname = fileDialog.GetPath()
                    self.current_project["ontologies"][file_name]["imports"].insert(0, pathname)
                    onto_path.append(pathname)
                    self.save_config()
                    self.load_file_ontology_with_dependencies(file_name)
            else:
                return
Reply | Threaded
Open this post in threaded view
|

Re: Support load() to provide explicit ontology file locations

Jiba
Administrator
In reply to this post by jo-fra
I fully agree that it would be useful. I implemented it in the development version, thank you for the suggestion !

Jiba