Reputation: 886
I have to run a loop that retrieves the labels of all the neighbour nodes of all the classes of an rdf ontology using SPARQL queries in rdflib. I know how to get the classes:
classes=graph.query("""select ?nodes WHERE {
?nodes rdf:type owl:Class
} limit 100""")
And I know how to get the neighbour nodes of a single URI:
example = graph.query("""select ?othernodes ?othernodesLabel where {
bind (<http://human.owl#NCI_C12736> as ?mynode )
?mynode ?y ?othernodes.
?othernodes rdfs:label ?othernodesLabel
} limit 100""")
But what I need is to put these together in a for-loop that groups all the labels of the adjoining nodes of each class in one object, so that for every class there is a list that contains the labels of all the adjoining nodes.
Upvotes: 0
Views: 1236
Reputation: 8465
If I understood your question correctly, the most simple solution would be to combine both queries and process the labels via Python code. The query would be
SELECT ?node ?othernodes ?othernodesLabel WHERE {
?node rdf:type owl:Class .
?node ?y ?othernodes .
?othernodes rdfs:label ?othernodesLabel
}
LIMIT 100
In your code, you can then process the returned data per each OWL class. Indeed you still have to loop over the resultset, thus, it's similar to loop over the result of your first query and execute the second query for each class.
Upvotes: 1