Ines
Ines

Reputation: 59

How can I return different graphs based on a Construct Sparql Query?

I have a sparql construct query that I use to construct various graphs based on the where clause. The sparql construct query works like this: it returns a model with all the different instances (or mappings). I'd like to return a graph for each of the mappings that could be found (simple example described below). The where clause of the construct query could be very complex (inlc. FILTER, UNION, etc. ).

How can I do this using Java Jena?

Example this is a Knowledge graph

ex:johnSmith  dpvm:access   ex:healthRecord-1 .
ex:johnSmith    rdf:type    dpvm:Pharmacist.

ex:saraParker  dpvm:access   ex:healthRecord-2 .
ex:saraParker  rdf:type    dpvm:Pharmacist.

ex:alicePark  dpvm:access   ex:healthRecord-3 .
ex:alicePark  rdf:type    dpvm:Pharmacist.

This is a construct query

CONSTRUCT
{
?x dpvm:access ?y .
}
WHERE 
{
?x dpvm:access ?y.
?x rdf:type dpvm:Pharmacist .
}

I want to return separate graphs for each of the set of bindings such as:

graph1: ex:johnSmith dpvm:access ex:healthRecord-1 .

graph2: ex:saraParker dpvm:access ex:healthRecord-2 .

graph3: ex:alicePark dpvm:access ex:healthRecord-3 .

Upvotes: 1

Views: 43

Answers (1)

AndyS
AndyS

Reputation: 16630

As an extension, Apache Jena GRAPH in the construct templates. The client code must give a media type for a quads format or for a local query use execConstructDataset. The WHERE clause should set a variable that gives a URI name for each person (may their URI with "-graph" appended)

Example:

CONSTRUCT
{
    GRAPH ?graph { ?x dpvm:access ?y . }
} WHERE {
   ...
   BIND (uri(concat(str(?x), "-graph")) AS ?graph)
}

The exact expression may be different depending on the naming in the data.

Upvotes: 0

Related Questions