Nedson Soares
Nedson Soares

Reputation: 3

How do I assimilate to an individual the same data property n times

For example, I have the data property "name" in my ontology and I want to assimilate to an individual more than one data property "name".

onto = get_ontology("teste.owl").load()
for line in list:
    individual = onto.Class('individuo1')
    individual.nome = [line['name']]

I do this and it doesn't work and creates just one data property with the last value in the list.

Upvotes: 0

Views: 421

Answers (2)

mgc
mgc

Reputation: 5443

Here is a complete example of what (i guess) you are trying to achieve. I renamed your name DataProperty to hasName because owlready2 already uses the 'name' property so the example would not works.

from owlready2 import get_ontology, DataProperty, Thing

list_names = [
    {'name': 'name1'},
    {'name': 'name2'},
    {'name': 'name3'},
]
onto = get_ontology("http://example.org/ns")
        
with onto:
    # A simple model:
    class Class(Thing): pass
    class hasName(DataProperty): pass

    # you have to define the `hasName` property
    # when creating the individual:
    indiv = onto.Class('Indiv0', hasName=[])
    
    # you can now append to this list:
    for line in list_names:
        indiv.hasName.append(line['name'])
        
onto.save('test.owl')

Which should yield the expected rdf/xml representation :

<Class rdf:about="#Indiv0">
  <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
  <hasName rdf:datatype="http://www.w3.org/2001/XMLSchema#string">name1</hasName>
  <hasName rdf:datatype="http://www.w3.org/2001/XMLSchema#string">name2</hasName>
  <hasName rdf:datatype="http://www.w3.org/2001/XMLSchema#string">name3</hasName>
</Class>

Upvotes: 1

Red
Red

Reputation: 27577

Instead of:

individual.name = [line['name']]

try:

individual.name = []

and then, in each iteration:

individual.name.append([line['name'])

Upvotes: 1

Related Questions