AttributeError: relation class property is not defined

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

AttributeError: relation class property is not defined

caroline
Hi all,
I have a question which I could not fix it and where does the error come from?
class Food(Thing):
        pass
class Taste(Thing):
        pass
class has_taste(DataProperty):
        domain = [Food]
        range = [Taste]

apple = Fruit('apple')
orange = Fruit('orange')
apple.has_taste.append('sweet')
apple.has_taste.append('juicy')
orange.has_taste.append('sweet')
orange.has_taste.append('juicy')

Then it pops up the error:

AttributeError                            Traceback (most recent call last)
<ipython-input-9-71af2a9a3755> in <module>
     39 apple.has_taste.append('juicy')
     40
---> 41 orange.has_taste.append('sweet')
     42 orange.has_taste.append('juicy')
     43

~/anaconda3/lib/python3.7/site-packages/owlready2/individual.py in __getattr__(self, attr)
    244       if not Prop:
    245         if attr == "equivalent_to": return self.get_equivalent_to() # Needed
--> 246         raise AttributeError("'%s' property is not defined." % attr)
    247       if Prop.is_functional_for(self.__class__): self.__dict__[attr] = r = Prop._get_value_for_individual (self)
    248       else:                                      self.__dict__[attr] = r = Prop._get_values_for_individual(self)

AttributeError: 'has_taste' property is not defined.

Can someone help please?
       
Reply | Threaded
Open this post in threaded view
|

Re: AttributeError: relation class property is not defined

Jiba
Administrator
Hi,

There are several errors in your code example :

1) you need to create the classes inside an ontology, e.g. :

onto = get_ontology('http://test.org/test.owl')

with onto:
  class Food(Thing): pass


2) the Fruit class is not defined, I suppose it should be the Food class instead

3) has_taste is defined as a DataProperty, but its range is a class (Taste), and then you use strings as value for the property.

If you want to use strings, defined range as follow: range = [str]
Otherwise, use Taste individual as value, and define has_taste as an ObjectProperty.

Here is a working example:

from owlready2 import *

onto = get_ontology('http://test.org/test.owl')

with onto:
  class Food(Thing): pass
  class Taste(Thing): pass
 
  class has_taste(ObjectProperty):
        domain = [Food]
        range = [Taste]

apple = Food('apple')
orange = Food('orange')

apple.has_taste.append(Taste('sweet'))
apple.has_taste.append(Taste('juicy'))
orange.has_taste.append(Taste('sweet'))
orange.has_taste.append(Taste('juicy'))

Jiba