Adding properties to individual given its python instance (ie. of the property)

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

Adding properties to individual given its python instance (ie. of the property)

gp
Hi JB,

I have been struggling to dynamically add properties to individuals given the python instances of the property (i.e. not using the IRI and using the individualInstance.IRI syntax).

I came to the following solution but wondered if there is a better approach:

Assuming one of the many examples of the documentation
from owlready2 import *
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]


my_drug = Drug("my_drug") # got my instance
acetaminophen = Ingredient("acetaminophen")
bla = Ingredient("bla")

hasForIngredient2 = onto['has_for_ingredient'] #i.e. we get our property by searching for a label, etc. but we do not want to hard code the IRI in the code

# Here comes what feels as a hack
hasForIngredient2.python_name = 'hasForIng' 
my_drug.hasForIng.append(bla)
my_drug.hasForIng.append(acetaminophen)

#then things seem to be ok
for prop in onto.my_drug.get_properties():
    for value in prop[onto.my_drug]:
        print(".%s == %s" % (prop.python_name, value))


Any comment, better approach ?

Cheers,
Guillaume
gp
Reply | Threaded
Open this post in threaded view
|

Re: Adding properties to individual given its python instance (ie. of the property)

gp
another related question:

Could it be that the python_name property is defined only for ObjectProperty and not for DataProperty ?
gp
Reply | Threaded
Open this post in threaded view
|

Re: Adding properties to individual given its python instance (ie. of the property)

gp
Ok my bad,

DataProperty does have the property python_name, it is actually clear from the code (as it also inherit from PropertyClass).

Sorry for the noise...
Reply | Threaded
Open this post in threaded view
|

Re: Adding properties to individual given its python instance (ie. of the property)

Jiba
Administrator
In reply to this post by gp
Hi,

Prop.python_name is for using a different attribute name than the IRI. For example the IRI is http://test.org/onto.owl#ma-prop, and you want to use .ma_prop in Python (notice that the "-"
cannot be used in a valid Python attribute name).

I think it is not what you need in your case (add properties to individuals given the python instances of the property).

You should use the Prop[individual] syntax. Properties can be indexed by an individual. Notice that Prop[individual] always return a list, even for functional properties, so you should do :

        Prop[individual].append(new_value)

Best regards,
Jean-Baptiste Lamy
MCF, LIMICS, Université Paris 13