简体   繁体   中英

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

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)

This works for me:

from owlready2 import *

onto = get_ontology("http://test.org/onto.owl")

with onto:

    class Person(Thing):
        pass
    
    class Building(Thing):
        pass
    
Person("Anna")
Person("Bella")
Person("Cercei")
Person("Dora")

Building("Eiffel_Tower")

print(list(Thing.instances()))

I think you would just reference your ontology and not your class.

onto = get_ontology("test.owl")
for i in onto.instances():
    print(i)

what you're doing is grabbing the class and iterating over the class-specific instances.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM