How to create Object and Data Properties dynamically ?

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

How to create Object and Data Properties dynamically ?

Driss
Hello,

I'm creating an ontology from a CSV file and I wonder if there is a way to create a property class dynamically as we do for concepts using the for example the following call :

ontology_concept = types.new_class(concept_name, (Thing, ontology_super_concept,))

Maybe something like :

ontology_data_property = types.new_class(name=property_name,  (DataProperty))

Best regards,

Driss
Reply | Threaded
Open this post in threaded view
|

Re: How to create Object and Data Properties dynamically ?

Driss
Hello again,

After removing the 'name=' parameter in :  

ontology_data_property = types.new_class(name=property_name,  (DataProperty))  (doesn't work)

ontology_data_property = types.new_class(property_name,  (DataProperty)) (works !)

It works to create the owl property. I'm still wondering the way to define the domain, range and so on.

Best,

Driss
Reply | Threaded
Open this post in threaded view
|

Re: How to create Object and Data Properties dynamically ?

Jiba
Administrator
Hello,

Yes, your method should works!

You can define the range and domain as follows:

ontology_data_property.range = [range...]
ontology_data_property.domain = [domain...]

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

Re: How to create Object and Data Properties dynamically ?

rahul_raj
Hello Jiba,
I needed to create data properties dynamically for my project. So, following this thread, I tried to implement it in the way Driss has done it. I have a list of strings where each string is a data property name.
s = ["has_PropertA", "has_PropertyB", "has_PropertyC"]

Now, I wish to create these data properties dynamically.

for i in range(len(s)):
    with onto:
        ontology_data_property = types.new_class(s[i],  (DataProperty))

But this throws an error - AttributeError: 'int' object has no attribute 'DataProperty'
I thought changing 'ontology' in the last line to the name of ontology might help. i.e.,
for i in range(len(s)):
    with onto:
        onto_data_property = types.new_class(s[i],  (DataProperty))

But this also gives the same error as above case.

Kindly put forward your views on this error and suggest a way out of this problem so as to fulfill my intended objective. I would be glad!
Reply | Threaded
Open this post in threaded view
|

Re: How to create Object and Data Properties dynamically ?

Jiba
Administrator
Hello,

You need a "," after DataProperty. (DataProperty,) is the tuple of base classes, and, in Python, you need a comma after the first element to create a on-element tuple.

for i in range(len(s)):
    with onto:
        onto_data_property = types.new_class(s[i],  (DataProperty,))

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

Re: How to create Object and Data Properties dynamically ?

jui guram
Hi,

I am also trying to create data property dynamically.Here is my code.

with onto:
        ontology_data_property = types.new_class('custNo',  (DataProperty,))
        ontology_data_property.range = [int]
        ontology_data_property.domain = ['customers']

when i run this I get error in the domain line.

AttributeError: 'str' object has no attribute 'storid'

without mentioning the domain it works well.

Regards,
Jui
Reply | Threaded
Open this post in threaded view
|

Re: How to create Object and Data Properties dynamically ?

rahul_raj
Hello jui_guram and Jiba,
Thank you for helping me out. That inclusion of a comma was what all needed. It works perfectly fine now!

 for i in range(len(s)):
    with onto:
        onto_data_property = types.new_class(s[i],  (DataProperty,))
        onto_data_property.range = [float]
        onto_data_property.domain = [Customer]

@jui_guram: You need to remove the quotes from the class name i.e., instead of ['customers'] as domain, put [customers] as domain. When you put it as 'customers', it is taken up as a new string which is just a constant fixed string and hence, it is not recognized as an ontology class. And since, you must put an ontology class as the domain, you should put it as [customers]. Then it gets recognized as an ontology class.
Reply | Threaded
Open this post in threaded view
|

Re: How to create Object and Data Properties dynamically ?

embisme
In reply to this post by jui guram
Hi,
To entry anything dynamically -

with onto:
        ontology_data_property = types.new_class('custNo',  (DataProperty,))
        ontology_data_property.range = [int]
        ontology_data_property.domain = onto['customers']

Regards,
Millennium
Reply | Threaded
Open this post in threaded view
|

Re: How to create Object and Data Properties dynamically ?

Avraheem
How to give value to existing data properties loaded from owl file that has all properties defined?
Reply | Threaded
Open this post in threaded view
|

Re: How to create Object and Data Properties dynamically ?

embisme
We can do that dynamically bu the python way - setattr()

So we would have an individual say i of a class say c
Lets say the data property is d

So the general way is
 i.d = ['str']

But by using the setattr() we can write it dynamically as-
setattr(i, d, ['str'])

Hope this solves your problem!

Millennium
Reply | Threaded
Open this post in threaded view
|

Re: How to create Object and Data Properties dynamically ?

Avraheem
Thank you! The other part of the question, can I connect one individual to multiple individuals via multiple object properties at the same time dynamically?

For example, the following will associate newJob individual, named "job1", to a new company newCompanyName via object property hasCompany:

newJob= NewJob(name = "job1", hasCompany=newCompanyName)

Can I add more object properties at the same time like hasAddress, hasNumber, etc.?
Reply | Threaded
Open this post in threaded view
|

Re: How to create Object and Data Properties dynamically ?

embisme
Yes you can ofcourse. But not in this format. Because this in itself is not that dynamic. Again use the same setattr() and it can be done. 

On Sun, 20 Sep, 2020, 9:29 pm Avraheem [via Owlready], <[hidden email]> wrote:
Thank you! The other part of the question, can I connect one individual to multiple individuals via multiple object properties at the same time dynamically?

For example, the following will associate newJob individual, named "job1", to a new company newCompanyName via object property hasCompany:

newJob= NewJob(name = "job1", hasCompany=newCompanyName)

Can I add more object properties at the same time like hasAddress, hasNumber, etc.?


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

Re: How to create Object and Data Properties dynamically ?

Avraheem
Thank you and same thing for adding object property or this method only works with data properties?
Reply | Threaded
Open this post in threaded view
|

Re: How to create Object and Data Properties dynamically ?

embisme
Yes same approach. Ofcourse. 

On Tue, 22 Sep, 2020, 1:53 am Avraheem [via Owlready], <[hidden email]> wrote:
Thank you and same thing for adding object property or this method only works with data properties?


If you reply to this email, your message will be added to the discussion below:
http://owlready.8326.n8.nabble.com/How-to-create-Object-and-Data-Properties-dynamically-tp244p2026.html
To unsubscribe from How to create Object and Data Properties dynamically ?, click here.
NAML