I have and ontology written in OWL/RDF(using Protege). This ontology has been already populated with some individuals for each concept. I have ported it in to python using rdflib and FuXi packages. And I can successfully parse my Ontology and put in a graph. Now the only thing that I need to to do is that to print out all individuals for each concept. Does anyone know how I can do that?
Asked
Active
Viewed 2,022 times
3
1 Answers
4
When you say all the individuals for each concept I guess that you mean all the resources of rdf:type an specific class
. With rdflib
you can easily do that by traversing the graph:
from rdflib import Graph
from rdflib import URIRef
g = Graph()
g.parse("ontology.owl")
aClass = URIRef("http://www.someuri.org/for/your/class")
rdftype = URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
for triple in g.triples((None,rdfType,aClass)):
print triple
(None,rdfType,aClass)
represents a constrain to iterate over the graph g
. By setting
any of the three elements in the triple you constrain by any combination of subject,
predicate or object. In this case we only constrain by predicate rdftype
and
object aClass
.
If you wanted all individuals members and all classes you could do:
for triple in g.triples((None,rdfType,None)):
print triple
In which case we leave the object unbound to capture any OWL class.

Manuel Salvadores
- 16,287
- 5
- 37
- 56
-
@Hossein see the new version of the code. The code I posted before only works with the latest version of rdlib, this new version should work for you. Otherwise let me know what version of Python and rdflib you're running. – Manuel Salvadores Mar 09 '11 at 15:33