How to dynamically create a relation

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

How to dynamically create a relation

SPJ
Hi,

I am working on a use case where I need to dynamically create data properties, object properties and individuals and then dynamically create relations among those created properties and individuals.

I want to do this to an already created owl ontology file.(Importing it and making the edits)

It's kind of urgent and Really Appreciate any suggestions to make this work.

Best Regards,
SPJ
Reply | Threaded
Open this post in threaded view
|

Re: How to dynamically create a relation

KayJay
Hello,

Have you read the doc? (https://owlready2.readthedocs.io/en/latest/)
You already have examples on how to create dynamically anything. Plus, you have a whole paragraph on loading an existing ontology.

Best,
KayJay
SPJ
Reply | Threaded
Open this post in threaded view
|

Re: How to dynamically create a relation

SPJ
Hello KayJay,

I read the doc. It is true that there are examples to dynamically create classes,properties and individuals. But I'm looking for a way to dynamically create a relation between an Individual and a Data property.

for example, I want to do something like this

def addDataProperty(individual_name,data_property):
    onto.individual_name.data_property = ["Data Property"]

and then when calling the function 'addDataProperty' passing the the required individual and the data property, the data property should be added to that individual.

I'm looking for a way to do this

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: How to dynamically create a relation

Jiba
Administrator
Hi,

I think you need the getattr() / setattr() Python functions. These functions can get or set a property from a string containing the property name, e.g.

individual_name = "your_name"
individual = getattr(onto, individual_name)

prop_name = prop.python_name # If you have the property as object and not as a name
setattr(individual, prop_name, new_value)

Jiba
SPJ
Reply | Threaded
Open this post in threaded view
|

Re: How to dynamically create a relation

SPJ
Hi Jiba,

Thanks for the reply. First of all I must say that I don't have much experience to these stuff. I tried your suggestion but encountered an error.
I have mentioned below the code I have used and the Error message.

myonto=get_ontology("file:///home/sharada/main/test_Template.owl").load()

def createDatProp(a):
    myonto=get_ontology("file:///home/sharada/main/test_Template.owl").load()
    with myonto:
        datProp=types.new_class(a, (DataProperty,))

createDatProp("TestDataProp_1")
createDatProp("TestDataProp_2")

individ=getattr(myonto,"SPJ")
setattr(individ,"TestDataProp_2","Test")

Error Message

setattr(individ,"TestDataProp_2","Test")
  File "/home/sharada/.local/lib/python3.6/site-packages/owlready2/individual.py", line 295, in __setattr__
    raise ValueError("Property '%s' is not functional, cannot assign directly (use .append() or assign a list)." % attr)
ValueError: Property 'TestDataProp_2' is not functional, cannot assign directly (use .append() or assign a list).

Really appreciate if you could help to counter this error.

Thank you.

On Thu, Aug 8, 2019 at 6:14 PM Jiba [via Owlready] <[hidden email]> wrote:
Hi,

I think you need the getattr() / setattr() Python functions. These functions can get or set a property from a string containing the property name, e.g.

individual_name = "your_name"
individual = getattr(onto, individual_name)

prop_name = prop.python_name # If you have the property as object and not as a name
setattr(individual, prop_name, new_value)

Jiba


If you reply to this email, your message will be added to the discussion below:
http://owlready.8326.n8.nabble.com/How-to-dynamically-create-a-relation-tp1282p1289.html
To unsubscribe from How to dynamically create a relation, click here.
NAML
Reply | Threaded
Open this post in threaded view
|

Re: How to dynamically create a relation

Jiba
Administrator
Hi,

If your property is not functional, you need to set it to a list, for example :

setattr(individ,"TestDataProp_2", [ "Test" ] )

Jiba
SPJ
Reply | Threaded
Open this post in threaded view
|

Re: How to dynamically create a relation

SPJ
Hi Jiba,

It worked. Thank you very much.

SPJ