Login  Register

How to get a list of all classes in a namespace.

classic Classic list List threaded Threaded
2 messages Options Options
Embed post
Permalink
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

How to get a list of all classes in a namespace.

Babroo
9 posts
Hello,

I have to use the namespace to define my ontology. I used a namespace for defining several classes in one ontology and then use ".classes()" and other attributes to return result from this namespace. I can have access to each class individually but I could not get a list of all classes through namespace. Is there any solution?


>>> from owlready2 import *

>>> onto1 = get_ontology("http://test.org/onto1.owl")
>>> obo1 = onto1.get_namespace('http://test.org/')

>>> with obo1:
    class MyNewClass1(Thing): pass
    class MyNewClass11(Thing): pass
    class MyNewClass111(Thing): pass

 
>>> print(obo1.base_iri)
http://test.org/

>>> print(obo1.MyNewClass1)
test.org.MyNewClass1

>>> print(list(obo1.classes()))
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    print(list(obo1.classes()))
TypeError: 'NoneType' object is not callable
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: How to get a list of all classes in a namespace.

Jiba
Administrator
1186 posts
Hello,

The namespace defines an easy way to access to entities, but it does not maintain a separate list of entities and RDF triples (as an ontology does). This is why namespaces do not have .classes() and the likes.

The only solution is to search entities by IRI and to filter classes, as follows:

>>> [i for i in default_world.search(iri = "http://test.org/*") if isinstance(i, ThingClass)]
[test.org.MyNewClass1, test.org.MyNewClass11, test.org.MyNewClass111]

Best regards,
Jiba