how to access to ancestors or superclass of classes?

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

how to access to ancestors or superclass of classes?

MV
hi,
I'm trying to access to ancestors of special classes. when i type:
>>> print(list(pizza2.classes()))
it give me a list of classes. but when i type: print(pizza2.PizzaTopping), it return : None!!
this is my codes:

>>> print(getattr(pizza1, "AnchovyTopping"))
None
>>> pizza1.search(subclass_of = pizza1.get_namespace("AnchovyTopping"))
Traceback (most recent call last):
  File "<pyshell#111>", line 1, in <module>
    pizza1.search(subclass_of = pizza1.get_namespace("AnchovyTopping"))
  File "C:\Users\Ali\AppData\Local\Programs\Python\Python35\lib\site-packages\owlready2\namespace.py", line 191, in search
    v2 = [child.storid for child in v.descendants()]
TypeError: 'NoneType' object is not callable
>>> print(namespace.AnchovyTopping)
None
>>> pizza2.search(subclass_of = "PizzaTopping")
Traceback (most recent call last):
  File "<pyshell#103>", line 1, in <module>
    pizza2.search(subclass_of = "PizzaTopping")
  File "C:\Users\Ali\AppData\Local\Programs\Python\Python35\lib\site-packages\owlready2\namespace.py", line 191, in search
    v2 = [child.storid for child in v.descendants()]
AttributeError: 'str' object has no attribute 'descendants'
>>> pizza2.search(subclass_of = PizzaTopping)
Traceback (most recent call last):
  File "<pyshell#104>", line 1, in <module>
    pizza2.search(subclass_of = PizzaTopping)
NameError: name 'PizzaTopping' is not defined
>>> pizza2.search(is_a = pizza2.Pizza)
Traceback (most recent call last):
  File "<pyshell#101>", line 1, in <module>
    pizza2.search(is_a = pizza2.Pizza)
  File "C:\Users\Ali\AppData\Local\Programs\Python\Python35\lib\site-packages\owlready2\namespace.py", line 191, in search
    v2 = [child.storid for child in v.descendants()]
AttributeError: 'NoneType' object has no attribute 'descendants'
>>> pizza2.Veneziana.ancestors()
Traceback (most recent call last):
  File "<pyshell#94>", line 1, in <module>
    pizza2.Veneziana.ancestors()
AttributeError: 'NoneType' object has no attribute 'ancestors'
>>> Veneziana.ancestors()
Traceback (most recent call last):
  File "<pyshell#95>", line 1, in <module>
    Veneziana.ancestors()
NameError: name 'Veneziana' is not defined

and ...

please help me! How to access to ancestors of predefined classes??!!
excuse me for poor english!
Reply | Threaded
Open this post in threaded view
|

Re: how to access to ancestors or superclass of classes?

Jiba
Administrator
Hello,

>>> from owlready2 import *
>>> onto = get_ontology("https://protege.stanford.edu/ontologies/pizza/pizza.owl").load()
>>> onto.Pizza
>>> namespace = onto.get_namespace("http://www.co-ode.org/ontologies/pizza/pizza.owl#")

# Namespace is needed because classes are named "http://www.co-ode.org/ontologies/pizza/pizza.owl#Veneziana" and not "https://protege.stanford.edu/ontologies/pizza/pizza.owl#Veneziana"!

>>> namespace.Veneziana
pizza.Veneziana

>>> namespace.Veneziana.is_a
[pizza.NamedPizza, pizza.hasTopping.some(pizza.CaperTopping), pizza.hasTopping.some(pizza.MozzarellaTopping), pizza.hasTopping.some(pizza.OliveTopping), pizza.hasTopping.some(pizza.OnionTopping), pizza.hasTopping.some(pizza.PineKernels), pizza.hasTopping.some(pizza.SultanaTopping), pizza.hasTopping.some(pizza.TomatoTopping), pizza.hasTopping.only(pizza.CaperTopping | pizza.MozzarellaTopping | pizza.OliveTopping | pizza.OnionTopping | pizza.PineKernels | pizza.SultanaTopping | pizza.TomatoTopping), pizza.hasCountryOfOrigin.value(pizza.Italy)]

>>> list(namespace.Veneziana.ancestors())
[pizza.DomainConcept, owl.Thing, pizza.Pizza, pizza.Food, pizza.Veneziana, pizza.NamedPizza]


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

Re: how to access to ancestors or superclass of classes?

mary
This post was updated on .
thank's alot!
I want to access the ancestors of this list. By the variable that holds the class name. Can you help me?this is my code: but give an error.

>>>l1=(list(pizza1.classes()))
>>> for i in l1:
        print(list(namespace.i.ancestor()))

       
Traceback (most recent call last):
  File "<pyshell#3>", line 2, in <module>
    print(list(namespace1.i.ancestor()))
AttributeError: 'NoneType' object has no attribute 'ancestor'


thank's for your attention.
Reply | Threaded
Open this post in threaded view
|

Re: how to access to ancestors or superclass of classes?

Jiba
Administrator
Hello,


>>>pizza2 = get_ontology("file://C:/Users/Ali/Desktop/pizza3.owl").load()
>>> namespace=pizza2.get_namespace("file://C:/Users/Ali/Desktop/pizza3.owl")

Your namespace should not depend on your local copy of the ontology, but on the IRI prefix in the OWL file. If C:/Users/Ali/Desktop/pizza3.owl is a copy of https://protege.stanford.edu/ontologies/pizza/pizza.owl , you can keep:

>>> namespace = onto.get_namespace("http://www.co-ode.org/ontologies/pizza/pizza.owl#")  

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

Re: how to access to ancestors or superclass of classes?

mary
thank's alot!
I want to access the ancestors of this list. By the variable that holds the class name. Can you help me?this is my code: but give an error.

>>>l1=(list(pizza1.classes()))
>>> for i in l1:
        print(list(namespace.i.ancestor()))

       
Traceback (most recent call last):
  File "<pyshell#3>", line 2, in <module>
    print(list(namespace1.i.ancestor()))
AttributeError: 'NoneType' object has no attribute 'ancestor'


thank's for your attention.
Reply | Threaded
Open this post in threaded view
|

Re: how to access to ancestors or superclass of classes?

Jiba
Administrator
Hello,

Here, the variable i is one of your classes. So you don't need the namespace in this context : pizza1.classes() already gather all classes defined in the ontology, whatever their namespace is.

So you should write:

l1=(list(pizza1.classes()))
for i in l1:
        print(list(i.ancestor()))

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: how to access to ancestors or superclass of classes?

JSekulow
Hello,
I am trying to find my way around  owlready2, but I don't understand it.
 Thanks for advice, I am a confused beginer. JJ


# I reproduced the code above, I am getting the classes, but ...
l1=(list(pizza1.classes()))
for i in l1:
        print(list(i.ancestor()))

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-50-d374196b5b84> in <module>
      1 l1=(list(pizza1.classes()))
      2 for i in l1:
----> 3         print(list(i.ancestor()))

~/anaconda2/envs/py3/lib/python3.7/site-packages/owlready2/entity.py in __getattr__(Class, attr)
    552     else:
    553       Prop = Class.namespace.world._props.get(attr)
--> 554       if not Prop: raise AttributeError("'%s' property is not defined." % attr)
    555       if issubclass_python(Prop, AnnotationProperty):
    556         attr = "__%s" % attr # Do NOT cache as such in __dict__, to avoid inheriting annotations

AttributeError: 'ancestor' property is not defined.


---------------------------
Another issue from tutorial: (without namespace part)
onto_path.append("PATH")
pizza1 = get_ontology("http://www.lesfleursdunormal.fr/static/_downloads/pizza_onto.owl").load()


pizza1.search(iri = "*Topping") # OK

pizza1.search(has_topping = "*")
> [ ] # is it OK?

pizza1.search(iri = "*Topping")
> [ ] # is it OK?

pizza1.search_one(is_a = pizza1.Pizza)
> pizza_onto.Pizza


pizza1.search(is_a = pizza1.Pizza, has_topping = pizza1.search(is_a = pizza1.Tomato))
>

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-53-1abdfcc7187e> in <module>
----> 1 pizza1.search(is_a = pizza1.Pizza, has_topping = pizza1.search(is_a = pizza1.Tomato))

~/anaconda2/envs/py3/lib/python3.7/site-packages/owlready2/namespace.py in search(self, _use_str_as_loc_str, _case_sensitive, **kargs)
    347           if   isinstance(v, (_SearchMixin, Or)): v2 = v
    348           elif isinstance(v, int):                v2 = v
--> 349           else:                                   v2 = v.storid
    350           prop_vals.append((" %s" % k, v2, None))
    351         else:

AttributeError: 'NoneType' object has no attribute 'storid'


# if I try pizza1.search(is_a = pizza1.Tomato)  the same error


# ALSO if I use tutorial example (i.e. no different namespaces)
list(pizza1.Veneziana.ancestors())
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-54-1efe9412251f> in <module>
----> 1 list(pizza1.Veneziana.ancestors())

AttributeError: 'NoneType' object has no attribute 'ancestors'


Thankforhelp
Reply | Threaded
Open this post in threaded view
|

Re: how to access to ancestors or superclass of classes?

Jiba
Administrator
Hi,

JSekulow wrote
# I reproduced the code above, I am getting the classes, but ...
l1=(list(pizza1.classes()))
for i in l1:
        print(list(i.ancestor()))
It is "ancestors" (with a "s" at the end).

JSekulow wrote
pizza1.search(has_topping = "*")
> [ ] # is it OK?
Yes, because there is no Pizza instance (and thus no Pizza instance with a topping).

You may create one as follows:

>>> p = pizza1.Pizza()
>>> p.has_topping.append(pizza1.TomatoTopping())
>>> pizza1.search(has_topping = "*")
[pizza_onto.pizza1]

JSekulow wrote
pizza1.search(is_a = pizza1.Pizza, has_topping = pizza1.search(is_a = pizza1.Tomato))
It should be TomatoTopping and not Tomato.
I'll fix it in the documentation!

JSekulow wrote
list(pizza1.Veneziana.ancestors())
There is no "Veneziana" pizza class (NB the "pizza_onto.owl" file I designed for the tutorial is not the entire pizza ontology from the usual OWL tutorial, but a simplified version).

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: how to access to ancestors or superclass of classes?

Armin_nbv
Hi and thanks for your guidances
 onto[MyClass].ancestors() returns all of the ancestors above "MyClass". but I only need the father of "MyClass" (just one layer above that). I've searched a lot but couldn't find anything. I'd be so grateful if you could help me with it.
Thank you
Reply | Threaded
Open this post in threaded view
|

Re: how to access to ancestors or superclass of classes?

Jiba
Administrator
Hi,

You can use MyClass.is_a to access direct parents.

If you want only named classes (and not constructs), you can filter the list as follows:

[x for x in MyClass.is_a if not isinstance(x, Construct)]

Jiba