How to use .instances() method to iterate through all Instances of owl:Thing in owlready2?

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

How to use .instances() method to iterate through all Instances of owl:Thing in owlready2?

mario7111
I can iterate through all instances of a class in owlready2 by using the following code which works fine:

>>> for i in Drug.instances(): print(i)


But I need to iterate through all instances of the ontology (through owl:Thing). I tried the following but it doesn't work:

>>> for i in Thing.instances(): print(i)
>>> for i in onto.Thing.instances(): print(i)
>>> for i in Thing.subclasses.instances(): print(i)
Reply | Threaded
Open this post in threaded view
|

Re: How to use .instances() method to iterate through all Instances of owl:Thing in owlready2?

Jiba
Administrator
Hi,

Thing.instances() should work for iterating through all individuals.
You can also use ontology.individuals().

Notice that both actually search for entity of type NamedIndividual. If the instances are not declared as NamedIndividual, it will not work (Owlready declares them properly as NamedIndividual, as well as Protege, but few RDF files that lack OWL semantics do not).

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: How to use .instances() method to iterate through all Instances of owl:Thing in owlready2?

mario7111
Hi Jiba,

Thank you very much for your reply.

It works with Thing.instances() + declaring instances as NamedIndividual.

I edited the .owl file and added "<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>" for every individual and it works.

But how do I declare every instance as NamedIndividual in Protege directly instead of editing the .owl file?

Best regards,

Marius
Reply | Threaded
Open this post in threaded view
|

Re: How to use .instances() method to iterate through all Instances of owl:Thing in owlready2?

Jiba
Administrator
Hi,

Normally, Protégé automatically declare individuals as NamedIndividuals, as in the following RDF/XML example:

   

    <owl:NamedIndividual rdf:about="http://www.semanticweb.org/jiba/ontologies/2021/9/untitled-ontology-58#x">
        <rdf:type rdf:resource="http://www.semanticweb.org/jiba/ontologies/2021/9/untitled-ontology-58#C"/>
    </owl:NamedIndividual>

The owl:NamedIndividual tag implies that the instance is a NamedIndividual.

Jiba