Neil Graham
Neil Graham

Reputation: 863

SPARQL: Converting CONSTRUCT Query w/ Named Graph to SELECT Query

You are not able to specify a GRAPH in a CONSTRUCT query with SPARQL 1.1

CONSTRUCT {
  GRAPH <graph:1> { ?s rdf:type ?o . }
}
WHERE {
  GRAPH <graph:1> { ?s rdf:type ?o . }
  VALUES (?o) { (<class:1>) (<class:2>) }
}

My thought is that it is possible to get the expected result by converting the GRAPH CONSTRUCT query to a SELECT query. It is common for us to expect an array of length 4 representing an RDF quad, therefore having the ability to CONSTRUCT with a GRAPH would be helpful.

The procedure of the conversion is less meaningful, I am more so interested in the format of the result from a GRAPH CONSTRUCT query to a SELECT query, taking into consideration all corner cases.

I would also appreciate an answer which explains why it is too difficult to do so with a SELECT query.


Question

How would a SELECT query be formatted with respect to a GRAPH CONSTRUCT query, considering all corner cases?

We will assume that the SELECT query results will roughly be converted to GRAPH CONSTRUCT results as shown below, but we will not be worrying about the format of the variables being tracked or the procedure of this conversion.

// Variables which are tracked to construct quads
const tracked = { "?V0": ["?s", "?V1", "?o"] };

// 'constructQuery' is converted to 'selectQuery'
const selectQuery = convertGraphConstructToSelect(constructQuery);

// SPARQL is executed on 'selectQuery'
const result = executeSparql(selectQuery);

// Returns expected result of GRAPH CONSTRUCT
const quads = constructFromSelect(tracked, result);

Example Answer

(In reference to the GRAPH CONSTRUCT query at the top)

SELECT ?V0 ?s ?V1 ?o
WHERE {
  GRAPH ?V0 { ?s ?V1 ?o . }
  VALUES (?o) { (<class:1>) (<class:2>) }
  VALUES (?V0 ?V1) { (<graph:1> rdf:type) }
}

Upvotes: 2

Views: 677

Answers (0)

Related Questions