Adding Individuals to Existing Ontology and Store Indivudals

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

Adding Individuals to Existing Ontology and Store Indivudals

Avraheem
Hello,

For a given ontology created in Protege, for example, with all classes and properties as will as restrictions already defined between them in Protege. How to load such ontology and create instances of those classes based on data incoming from other sources and then store those instances in knowledge base? Is it possible to do that in Owlready2 and can you please give one simple example how to do that?

Thanks.

 
Reply | Threaded
Open this post in threaded view
|

Re: Adding Individuals to Existing Ontology and Store Indivudals

Avraheem
I tried to create it dynamically with types module in Python. I loaded my ontology from Protege and without creating any class, I just used:

onto = get_ontology("ontology_here.owl").load()

with onto:
      class Entity(Thing):
        pass
      requestClass = "RequestData"
      NewRequest= type(requestClass, (Entity,), {"namespace" : onto})
      newRequest= NewRequest(name = "request1")
      print(newRequest)

Is this the right thing to do?
Reply | Threaded
Open this post in threaded view
|

Re: Adding Individuals to Existing Ontology and Store Indivudals

rmitanch
Hummm... Not sure to be a model to follow, but below is my own approach to peuplate my ontology with Individuals :

"""
As I don't have resolvable namespace yet like http://mydomain/.../myonto/, I use a base ontology using the local C:/path/to/local/ontologies/myonto.owl (and I'm using Stanford Protégé to describe the logic)
"""
onto = get_ontology('file://C:/path/to/local/ontologies/myonto.owl').load()
skos = get_ontology("http://www.w3.org/2004/02/skos/core#").load()
onto.imported_ontologies.append(skos)

#I want to create individuals of http://mydomain/.../myonto/MyClass

MyClass = IRIS['http://mydomain/.../myonto/MyClass']

with onto:
    Individual1 = MyClass('MyClass/1') # thus creating http://mydomain/.../myonto/MyClass/1 Individual
    Individual1.label = 'My Class Individual 1' # giving a label to it
    Individual1.prefLabel = 'a preferred label' # remember that skos:prefLabel id defined in SKOS !
    Individual1.other = [something] # assuming namespace:other is not functional and already defined in myonto.owl or any imported ontology
    Individual1.other.append(something else to add to other) # wanted to add another value to other property
    #... and so on

# and saving the result as RDF/XML file (to open/inspect using Stanford Protégé for example)
onto.save(file='C:/path/to/local/ontologies/myonto.rdf', format='rdfxml')

But things could be different if you are more interested in creating the classes using owlready2

Hope this helps
Richard
Reply | Threaded
Open this post in threaded view
|

Re: Adding Individuals to Existing Ontology and Store Indivudals

Avraheem
Great. So your approach is basically adding all instantiations at the top of your script through their IRIs and then just use them by their names whenever you need them.

This is how I added relation between two individuals based given that all properties are already predefined in Protege. Is this how you also did that?

onto = get_ontology("ontology_here.owl").load()

with onto:
      class Entity(Thing):
        pass
      requestClass = "RequestData"
      NewRequest= type(requestClass, (Entity,), {"namespace" : onto})
      newRequest= NewRequest(name = "request1")
      print(newRequest)
     
      tcpRequest = "TCPRequest"
      NewTCPRequest = type(tcpRequest, (Entity,), {"namespace" : onto})
      newTCPRequest = NewTCPRequest (name = "tcpRequest1", hasRequestData=newRequest)

Reply | Threaded
Open this post in threaded view
|

Re: Adding Individuals to Existing Ontology and Store Indivudals

Jiba
Administrator
Hi,

It seems ok to me.

Note that type() is for creating new classes, not new individual.
For creating individuals, you can use directly the class: onto.ClassName()

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Adding Individuals to Existing Ontology and Store Indivudals

Avraheem
Thank you Jiba.

How to set a hex value to a data property with setatt() ?

    individual= onto.ValueClass(name = "value")
    setattr(individual, dataAttribute,  [hex(255)])

Does not work and give me string representation of 255. In ontology, I found that the value written is "0xff"^^xsd:String not "0xff"^^xsd:hexBinary.
Reply | Threaded
Open this post in threaded view
|

Re: Adding Individuals to Existing Ontology and Store Indivudals

Jiba
Administrator
Hi,

hexBinary datatype is not supported by default in Owlready.

However, I used it as an example of user-defined datatype here:

http://owlready.8326.n8.nabble.com/Assign-missing-datatypes-td1845.html#a1850

so you can easily use it with this example.

Jiba