简体   繁体   中英

Owlready: subclasses of Thing in worlds different from default_world?

I have the following code (minimal example):

import owlready2 as owr

# create ontology in the default_world
onto0 = owr.get_ontology("https://w3id.org/yet/undefined/onto0#")

with onto0:
    class Animal(owr.Thing):
        pass

# create a new world for a new ontology
w1 = owr.World()
onto1 = w1.get_ontology("https://w3id.org/yet/undefined/onto1#")

with onto1:
    class Plant(owr.Thing):
        pass

print(list(owr.Thing.subclasses()))

resulting in [onto0.Animal] . In other words, the class defined in the new world w1 is not recognized as a subclass of Thing , despite being defined as such.

→ So, how can I get the subclasses defined in non-default worlds?

After writing down the question I found the answer in the book Ontologies with Python at the end of section 11.7:

[...] the subclasses() and descendants() methods of the OWL Thing and Nothing classes assume that they are called for default_world (indeed, these classes are shared by all worlds). If it is not the case, it is necessary to pass as a parameter the desired world [...].

Eg

print(list(owr.Thing.subclasses(world=w1)))

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