Ontology.get_namespace() returns Ontology

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

Ontology.get_namespace() returns Ontology

francesca
Hi,

I would like to make sure to always get the Namespace from a ThingClass by ThingClass.namespace, but do in some cases get the Ontology and not the Namespace.

e.g
testclass.namespace returns get_ontology(...) some times and get_ontology(....).get_namespace(...) other times.

In my case it seems to be solved by commenting out the line "if not r is None: return r" in Ontology.get_namespace, and I would like to suggest this. However, I am sure I am missing the reason why this is there in the first place.

Is there any other way to solve this? E.g, that ThingClass.namespace always returns an instance of Namespace and not Ontology.

Best regards
Francesca
Reply | Threaded
Open this post in threaded view
|

Re: Ontology.get_namespace() returns Ontology

Jiba
Administrator
Hi,

Ontology is a subclass of Namespace, so it is normal that entity.namespace sometimes returns an ontology. Is it causing a particular problem?

Jiba
Reply | Threaded
Open this post in threaded view
|

RE: Ontology.get_namespace() returns Ontology

francesca

Thank you Jiba,

 

The problem irises when I want the to have entities from different ontologies (.ttl-files) that are imported in the ontology and that have the same namespace. I want to treat
these depending on namespace without thinking about which 'file' they come from. It is important for me to have good control over the namespaces as there are other imported
ontologies with different namespaces and I need to can full control over namespaces.

 

Francesca

 

From: Jiba [via Owlready] <[hidden email]>
Sent: fredag 16. april 2021 15.26
To: Francesca Lønstad Bleken <[hidden email]>
Subject: Re: Ontology.get_namespace() returns Ontology

 

Hi,

Ontology is a subclass of Namespace, so it is normal that entity.namespace sometimes returns an ontology. Is it causing a particular problem?

Jiba


If you reply to this email, your message will be added to the discussion below:

http://owlready.8326.n8.nabble.com/Ontology-get-namespace-returns-Ontology-tp2377p2385.html

To unsubscribe from Ontology.get_namespace() returns Ontology, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

RE: Ontology.get_namespace() returns Ontology

Jiba
Administrator
Hi,

If you want to consider only the namespace and not the ontology it comes from, possibly you should just use the namespace IRI, i.e. entity.namespace.base_iri ?

Jiba
Reply | Threaded
Open this post in threaded view
|

RE: Ontology.get_namespace() returns Ontology

francesca

Hi,

 

Let me try to explain. We are working with ontologies that have several imported ontologies, some with the same namespace, some with different namespaces. We have made functions for the Ontology class so that when we are in an ontology and ask for an entity it returns the first entity it finds (looking trough all imported ontologies) without taking the namespace into account. I have also made a function for the Namespace class that returns the entity of a namespace when I am in a given Namespace. However this only works with get_namespace returns the namespace and not the ontology.

Was this clearer?

 

What is the reason that get_namespace is allowed to return det ontology instead of the namespace?

 

Best regards,

Francesca

 

From: Jiba [via Owlready] <[hidden email]>
Sent: tirsdag 20. april 2021 10.43
To: Francesca Lønstad Bleken <[hidden email]>
Subject: RE: Ontology.get_namespace() returns Ontology

 

Hi,

If you want to consider only the namespace and not the ontology it comes from, possibly you should just use the namespace IRI, i.e. entity.namespace.base_iri ?

Jiba


If you reply to this email, your message will be added to the discussion below:

http://owlready.8326.n8.nabble.com/Ontology-get-namespace-returns-Ontology-tp2377p2395.html

To unsubscribe from Ontology.get_namespace() returns Ontology, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

RE: Ontology.get_namespace() returns Ontology

Jiba
Administrator
Hi,

Ontology inherits from Namespace, because they share some common methods. As a consequence, an Ontology is considered as a particular kind of Namespace.

I think in your situation, you need to distinguish e.g. "http://test.org/onto.owl#" as an ontology and "http://test.org/onto.owl#" (same IRI) as a namespace, because some IRI can be both.

When you say "when we are in an ontology" and "when I am in a given Namespace", what is the expected result if the IRI is both a namespace (e.g. used in another ontology) and an ontology?

Possibly you need two different functions, one taking an ontology and the second a namespace IRI.

Jiba
Reply | Threaded
Open this post in threaded view
|

RE: Ontology.get_namespace() returns Ontology

francesca

Hi,

 

Yes exactly. I need to distinguish the namespace from the ontology. However, how to I access the namespace?

When the two IRIs are the same I cannot find a method that returns the namespace, as get_namespace return the ontology.

 

e.g.  ontology.get_namespace('http://test.org/onto.owl#') returns

get_ontlogy('http://test.org/onto.owl#') and not get_ontology('http://test.org/onto.owl#').get_namespace('http://test.org/onto.owl#')

 

Francesca

 

From: Jiba [via Owlready] <[hidden email]>
Sent: fredag 23. april 2021 15.43
To: Francesca Lønstad Bleken <[hidden email]>
Subject: RE: Ontology.get_namespace() returns Ontology

 

Hi,

Ontology inherits from Namespace, because they share some common methods. As a consequence, an Ontology is considered as a particular kind of Namespace.

I think in your situation, you need to distinguish e.g. "http://test.org/onto.owl#" as an ontology and "http://test.org/onto.owl#" (same IRI) as a namespace, because some IRI can be both.

When you say "when we are in an ontology" and "when I am in a given Namespace", what is the expected result if the IRI is both a namespace (e.g. used in another ontology) and an ontology?

Possibly you need two different functions, one taking an ontology and the second a namespace IRI.

Jiba


If you reply to this email, your message will be added to the discussion below:

http://owlready.8326.n8.nabble.com/Ontology-get-namespace-returns-Ontology-tp2377p2408.html

To unsubscribe from Ontology.get_namespace() returns Ontology, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

RE: Ontology.get_namespace() returns Ontology

jesper-friis
Hi Jean-Baptiste,
Owlready2 prefixes the repr() of a class with the last component of the namespace. Ex in:

```Python
In [1]: from owlready2 import get_ontology                                      
In [2]: onto = get_ontology("http://www.lesfleursdunormal.fr/static/_downloads/pizza_onto.owl").load()
In [3]: onto.Topping
Out[3]: pizza_onto.Topping
```

Here 'pizza_onto' is prefixed to Topping. We got inspired by this to use this prefix as a shorthand name for a namespace. Lets e.g. assume that onto has imported two pizza sub-ontologies http://something/pizzaA.owl and http://something/pizzaB.owl. We want to be able to refer to concepts in these sub-ontologies as follows:

```Python
>>> onto.pizzaA.FireResistance
pizzaA.FireResistance
>>> onto.pizzaB.FireResistance
pizzaB.FireResistance
```

To achieve this, we have subclassed owlready2.Ontology with a custom __getattr__() method that returns a Namespace instance if the argument is the name of a namespace. Our current implementation uses get_namespace(). This works fine when get_namespace() returns a Namespace object. But when it returns an Ontology object, it is not well-defined whether onto.pizzaA.FireResistance returns the FireResistance defined in pizzaA or in pizzaB.


I hope it became clearer. Are there better ways to achieve this?
Reply | Threaded
Open this post in threaded view
|

RE: Ontology.get_namespace() returns Ontology

Jiba
Administrator
Hi Jesper,

It's becoming clearer, but still not entirely clear. get_namespace() normally returns an Ontology only if called on an Ontology, with the IRI of the ontology itself -- in that case, the IRI namespace is the same of the ontology, so one can read/write RDF triple directly on the ontology itself.

I tried the following program, but I am still unable to see were is the problem:


from owlready2 import *

onto = get_ontology("http://www.lesfleursdunormal.fr/static/_downloads/pizza_onto.owl")
with onto:
  class Pizza(Thing): pass

onto1 = get_ontology("http://www.lesfleursdunormal.fr/static/_downloads/pizza1.owl")
with onto1:
  class PizzaFireResistance(Thing): pass
 
onto2 = get_ontology("http://www.lesfleursdunormal.fr/static/_downloads/pizza2.owl")
with onto2:
  class PizzaFireResistance(Thing): pass
 
class CustomOntology(Ontology):
  def __getattr__(self, attr):
    if attr == "pizza1": return self.get_namespace("http://www.lesfleursdunormal.fr/static/_downloads/pizza1.owl")
    if attr == "pizza2": return self.get_namespace("http://www.lesfleursdunormal.fr/static/_downloads/pizza2.owl")
    return Ontology.__getattr__(self, attr)
onto.__class__ = CustomOntology

print(onto.pizza1.PizzaFireResistance.iri)
print(onto.pizza2.PizzaFireResistance.iri)
# => http://www.lesfleursdunormal.fr/static/_downloads/pizza1.owl#PizzaFireResistance
# => http://www.lesfleursdunormal.fr/static/_downloads/pizza2.owl#PizzaFireResistance

Jiba