SPARQL: abbreviating redundant CONSTRUCT/WHERE

Hi,

Is there a way to extract a subgraph of a dataset with SPARQL ? I can see two possibilities :

  • Using DESCRIBE, but it is not standardized and not easy to control
  • Using CONSTRUCT, but we have to explicitly write all triples that will be created (redundant, difficult with inferred statements, etc.)

What I really want is get the whole subgraph identified by the WHERE clause as an output. Something like :

CONSTRUCT *
WHERE{...}

(I know, it doesn't make much sense... but you get the idea)

Thank you !


To clarify, a little example of how I would do it now (which is probably silly, but I have no idea how to do it better) :

CONSTRUCT{
  ?resource a ?type .
  ?resource :property1 ?value1 .
  ?resource :property2 ?anotherresource .
  ?anotherresource :property3 ?value2 .
}
WHERE{
  ?resource a ?type .
  ?resource :property1 ?value1 .
  ?resource :property2 ?anotherresource .
  ?anotherresource :property3 ?value2 .
}

The problem seems quite clear to me : everything is specified twice. Is there a way that I could get everything directly from the WHERE clause ?

You'll love ยง 16.2.4 of the SPARQL 1.1 Query Language spec:

CONSTRUCT WHERE {
  ?resource a ?type .
  ?resource :property1 ?value1 .
  ?resource :property2 ?anotherresource .
  ?anotherresource :property3 ?value2 .
}

Two caveats:

  • This shortcut syntax is only defined for WHERE clauses with simple BGPs. More complex WHERE clauses may introduce ambiguity with respect to what should be CONSTRUCTed and what not. As such, the syntax is only intended for use in simple cases (as above).
  • Comparing the grammars, it seems that this is only possible for SPARQL 1.1 and not for SPARQL. This syntax is covered by the second disjunct (the bit after the '|') of the SPARQL 1.1 grammar.