owl_inverse_property problem

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

owl_inverse_property problem

ZhaoPengya2
hi, I tried the example of "Drug and Ingredient" on the official website, but there is a mistake. According to the documentation of the official website, after writing the statement of "inverse_property = has_for_ingredient", we can use print(aspirin.is_ingredient_of) to get the relationship, but it got null. Many thanks.

Code:
my_drug2 = Drug("my_drug2")
aspirin = Ingredient("aspirin")
my_drug2.has_for_ingredient.append(aspirin)
print(my_drug2.has_for_ingredient)
print(aspirin.is_ingredient_of)

Output:
[owl.aspirin]
[] #this value should be [owl.my_drug2], right?
Reply | Threaded
Open this post in threaded view
|

Re: owl_inverse_property problem

Jiba
Administrator
Hello,

I tried the following program:


from owlready2 import *

#import owlready2
#owlready2.set_log_level(9)

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

with onto:
    class Drug(Thing):
        pass
    class Ingredient(Thing):
        pass
    class has_for_ingredient(ObjectProperty):
        domain    = [Drug]
        range     = [Ingredient]
    class is_ingredient_of(ObjectProperty):
        domain           = [Ingredient]
        range            = [Drug]
        inverse_property = has_for_ingredient

my_drug2 = Drug("my_drug2")
aspirin = Ingredient("aspirin")
my_drug2.has_for_ingredient.append(aspirin)
print(my_drug2.has_for_ingredient)
print(aspirin.is_ingredient_of)


But I obtain the expected results:

[onto.aspirin]
[onto.my_drug2]


Are you sure you do not miss some step in your program?

Best regards,
Jiba
Reply | Threaded
Open this post in threaded view
|

Re: owl_inverse_property problem

ZhaoPengya2
Compare to your code, I found I didn't write the statment of "with onto: " before classes. I got the same output by adding this line. Many thanks.

and I have a new question, if i don't write the "with onto:" before classes,run the blow code:
class has_subsidiary(ObjectProperty):
    domain = [Company]
    range = [Company]

class is_subsidiary_of(ObjectProperty):
    domain = [Company]
    range = [Company]
    inverse_property = has_subsidiary

will get a error:TypeError: 'NoneType' object is not callable
if i change the "inverse_property" to "owl_inverse_property", the error is gone, but the inverse_property feature is not effective.
c1 = Company('海航集团有限公司')
c2 = Company('海南航空控股股份有限公司')
c1.has_subsidiary.append(c2)

print(list(c1.has_subsidiary))
print(list(c2.is_subsidiary_of))

console:
[owl.海南航空控股股份有限公司]
[]

thanks o lot !!!!!
Reply | Threaded
Open this post in threaded view
|

Re: owl_inverse_property problem

Jiba
Administrator
Yes, you need the "with onto:" block. It is mandatory in order to specify in which ontology are defined the classes and properties.

Best regards,
Jiba