My data is partitioned using named graphs. One graph contains my ontology and human-readable labels for classes. The data in turn is divided up into a number of graphs, ex:data1
, ex:data2
, ... ex:data5000
.
Now I wish to query each of the data graphs independently, while using the labels from the ontologies. First I thought this would be trivial but then I found that scoping in SPARQL can be a bit confusing. As a simple example:
Select all instances with their respective instance label. Also retrieve the optional class associated with each instance and the class label from the ontology.
If the scope of class
stretched across both groups I would expect the following query to do the trick:
SELECT ?instance ?instanceLabel ?class ?classLabel
WHERE {
GRAPH ?data {
?instance rdfs:label ?instanceLabel .
OPTIONAL { ?instance a ?class }
}
GRAPH <http://myontology> {
OPTIONAL {
?class rdfs:label ?classLabel
}
}
}
If the class is bound the result will be what I wanted, since the two groups are joined; however, due to scoping ?class
can bind to anything in the ontology if its not bound in the first group.
The query I'm writing has about 15 similar optional fields (terribly expensive join...). When I tried re-writing with UNION
it quickly became quite unwieldy due to the size of the original query. I've also tried nesting queries but that gives me the same issue with scoping. Is there anyone who has some SPARQL-trick up their sleeve that I've overlooked? Any suggestions on how to approach this would be appreciated.