How can I enumerate all classess defined in ontology using DotNetRDF? I load ontology to start with and then need a way to get (enumerate) all classes. To load the ontology:

Dim g As New OntologyGraph
FileLoader.Load(g, strURI)
Now, I would need something like dim enmClasses as OntologyClasses = g.GetAllClasses(g).

I use VB, but have no problems if you use C# to help me with this :)

asked 11 Dec '12, 02:47

Boranin's gravatar image

accept rate: 0%

edited 11 Dec '12, 18:45

Rob%20Vesse's gravatar image

Rob Vesse ♦

You made a comment about number of classes reported in Protege vs dotNetRDF but that appears to have been removed. From what I can see of the data you posted the problem is one of OWL vs RDF. The OWL defines lots of classes in terms of computations on lots of other classes so there are a lot of <x> a owl:Class type triples which means dotNetRDF identifies lots of classes. As Protege works in terms of OWL it boils the ontology down to its definitions and so gives you the much smaller number of classes.

(02 Jan '13, 05:49) Rob Vesse ♦ Rob%20Vesse's gravatar image

Thanks Rob, the approach I've taken is to find the distinct class URIs searching through the subjects and objects of the triples returned by the above function you suggested. Still have to think how to eliminate anonymous classes, but that shouldn't be a big issue.

Great addition in your documentation would be a graphic representation of the terms you used in the library (OntologyResource, OntologyClass, Ontology and similar). Or at least something like, "OntologyResource as defined in RFC....". Some terms (not just as used in your library, but in the whole field in general) are used interchangeably in the various related texts and can confuse a user.

Thanks for your continious help.

(02 Jan '13, 06:16) Boranin Boranin's gravatar image

Ah, if the problem is getting the same class reported multiple times then that is likely something that can be fairly easily fixed in the library. I will take a closer look and see if I can't get that fixed for the next release if it is a bug. The documentation suggestion is a good idea as well, thanks for that.

(02 Jan '13, 06:21) Rob Vesse ♦ Rob%20Vesse's gravatar image

Hmmm, not sure that there is a problem here. When I try the code I gave on the data you gave I get 95 classes reported and all are distinct. Can you share your exact code please?

(02 Jan '13, 06:57) Rob Vesse ♦ Rob%20Vesse's gravatar image

Assuming all your classes are defined as belonging to some common type like rdfs:Class or owl:Class you can do the following with an OntologyGraph:

//This gets the class for things which are declared to be a class
OntologyClass classOfClasses = g.CreateOntologyClass(g.CreateUriNode("owl:Class"));

//This iterates over the things that are a class
foreach (OntologyResource class in classOfClasses.Instances)
  //Do what you want with the class

We'll look at adding a Classes property/method to the OntologyGraph in a future release. And adding some easier casting between OntologyResource and OntologyClass

permanent link

answered 11 Dec '12, 20:09

Rob%20Vesse's gravatar image

Rob Vesse ♦
accept rate: 29%

Notes for improvements filed as CORE-293 ( - please register and add further comments if there are other missing features you'd like to see

(11 Dec '12, 20:13) Rob Vesse ♦ Rob%20Vesse's gravatar image

See for some new methods and properties already added, the code there should provide alternative answers to your question

(12 Dec '12, 00:44) Rob Vesse ♦ Rob%20Vesse's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:


question asked: 11 Dec '12, 02:47

question was seen: 1,794 times

last updated: 02 Jan '13, 06:57