Adding properties to individual given its python instance (ie. of the property)
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")
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'
#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))
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 :
MCF, LIMICS, Université Paris 13