2

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>) }
}
  • I will refer to the query above as a GRAPH CONSTRUCT query

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);
  • Pseudocode roughly representing process of getting GRAPH CONSTRUCT results

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) }
}
  • All literals in the GRAPH CONSTRUCT are converted to unique variables
  • Each instance of converted literals in the WHERE are converted to those unique variable names.
  • A VALUES block is added to the end of the WHERE which represents all of the converted literals
Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
Neil Graham
  • 593
  • 1
  • 5
  • 17
  • Yes, it's not possible in SPARQL CONSTRUCT to create quads i.e. specify the graph. Andy Seaborne did some extension in Apache Jena [(link)](https://jena.apache.org/documentation/query/construct-quad.html), but you're using Allegrograph as I can see from the tags. But, what is the question now? – UninformedUser Dec 18 '19 at 18:31
  • I think it may matter *why* you want to do this, as this seems like either a theory question (which is fine, but should be flagged as such) or an [XY Problem](https://www.perlmonks.org/?node=XY+Problem) (which would be best answered with a different tactic than with a solution to your question as written). – TallTed Dec 19 '19 at 14:40
  • @TallTed I want this to be a guide for those who want to implement a `GRAPH CONSTRUCT` into their system. I don't want the question to be too open-ended, and so I gave an example of how the variables might be tracked to convert from the `SELECT` results to the expected `GRAPH CONSTRUCT` results. – Neil Graham Dec 19 '19 at 16:57

0 Answers0