Loading multiple owl files

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

Loading multiple owl files

QooTec

Environment

* Python 3.12
* owlready2 0.47

Context

As can be seen in the code below:
* I load an ontology from an owl file
* I create "ip" to be able to save my additions at the end in a separate owl file
* I load already existing elements from two owl files. Those use the same namespace as mentioned on the ip-line

    my_ontology = get_ontology("./resources/ontology.owl").load()

    ip = get_ontology("http://mycompany.org/ontology/test")

    ip1 = get_ontology("./resources/local_ip1.owl").load()
    ip2 = get_ontology("./resources/local_ip2.owl").load()

    with ip:
        ... create some additional elements...

    ip.save("./resources/local_ip.owl")

Observed

If I execute just before the with statement:
    ip.classes()
or
    ip1.classes()
I get an empty list, while the loaded owl files do create (sub-)classes of the ontology as well as individuals.
    my_ontology.classes()
shows the classes from my_ontology.

Expected

I thought it would be possible to access all elements in ip, ip1, or ip2 just by using ip.
Then I do not need to know in which file the element is created.

Question

Am I misunderstanding how to handle this?

FYI: My ip1/2 files reference elements from my_ontology by iri. They do not "import" that ontology.
Reply | Threaded
Open this post in threaded view
|

Re: Loading multiple owl files

Jiba
Administrator
Hello,

I'm not sure I totally understand what you are doing (or trying to do).

Are you trying to create several ontologies, local_ip1.owl, local_ip2.owl,..., whose entities are defined in the same namespace http://mycompany.org/ontology/test ?

If so, you need to use the "with" statement as follows, in order to specify both the ontology that receives the RDF triple (e.g. ip1) and the namespace (http://mycompany.org/ontology/test):

    ip1 = get_ontology("./resources/local_ip1.owl")

    with ip1.get_namespace("http://mycompany.org/ontology/test"):
          ... create some additional elements...

    ip1.save("./resources/local_ip1.owl")

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Loading multiple owl files

QooTec
I understand that my intent might be confusing.
Here some additional clarification.

* One file contains my ontology (my_ontology)
* Two files already contain pre-created individuals (ip1, ip2). Also some sub-classes/properties of my_ontology are created here.
* I want to use owlready2 to create a third file with individuals (ip), containing only the additionally created ones (so no duplicates of ip1, ip2).
* All ipX files use the same namespace (different from my ontology)

The individuals in ip:
* can be instances of ontology elements
* can reference other classes/properties/individuals in ip1 or ip2
* can create some additional classes/properties based on classes/properties from my_ontology, ip1, ip2.

And essentially all this seems to work fine, since my output produced through the code above seems to be valid.

My issue occurs in my Python code when I want to automate some tasks: it seems I cannot find the expected elements in ip.
One of the strange observations in that respect is the behavior of .classes():
* If I ask for ip.classes() or ip.individuals(), it seems empty (although I created some in it)
* while my_ontology.classes() lists the expected elements

So, while ".classes()" is not the essential thing I need, it is an example of those functions that do not react as expected. Understanding why it behaves like this, and correcting my approach, will probably also solve the other programmatic issues I experience. Hence my question.