CONSTRUCT and DESCRIBE
Building subgraphs from script results.
SELECT returns tables of values. CONSTRUCT and DESCRIBE return graphs: sets of triples that can be stored, merged, or passed around.
The examples on this page assume the following prefixes:
PREFIX tasks: <https://example.org/spec/tasks#>
PREFIX people: <https://example.org/people/>CONSTRUCT
CONSTRUCT builds new triples from script results. You define a template of triples with variables, and the engine fills in the variables for each result row.
PREFIX tasks: <https://example.org/spec/tasks#>
PREFIX people: <https://example.org/people/>
CONSTRUCT {
?task tasks:title ?title ;
tasks:status ?status ;
tasks:assigneeName ?name .
}
WHERE {
?task a tasks:Task ;
tasks:title ?title ;
tasks:status ?status .
OPTIONAL {
?task tasks:assignee ?person .
?person people:name ?name .
}
}The CONSTRUCT block is the output template. The WHERE block is the script. For each result row, the template variables are filled in and the resulting triples are emitted.
If a variable is unbound (from an OPTIONAL), any triple using that variable is silently skipped. In the example above, tasks without an assignee simply won't have a tasks:assigneeName triple in the output.
When to Use CONSTRUCT
CONSTRUCT is the primary output form for script handlers. When a script handler uses rars-os:construct, the constructed triples are stored in a named graph and the graph URI is returned to the caller.
It's also useful for reshaping data: selecting from one structure and outputting another.
PREFIX tasks: <https://example.org/spec/tasks#>
PREFIX people: <https://example.org/people/>
# Flatten nested structure into a simpler form
CONSTRUCT {
?task tasks:assigneeName ?name ;
tasks:assigneeEmail ?email .
}
WHERE {
?task tasks:assignee ?person .
?person people:name ?name ;
people:email ?email .
}DESCRIBE
DESCRIBE returns everything the engine knows about a resource:
DESCRIBE <https://example.org/tasks/123>This returns all triples where the resource appears as subject or object. The exact set of triples is implementation-dependent. DESCRIBE is useful for exploration but less predictable than CONSTRUCT for production use.
CONSTRUCT vs SELECT
| SELECT | CONSTRUCT | |
|---|---|---|
| Returns | Table of variable bindings | Set of triples (a graph) |
| Shape | Flat rows and columns | Arbitrary graph structure |
| Script handler output | Positional binding to caller variables | Named graph with graph URI returned |
| Validation | Raw values, no type checking | Triples can be validated against SHACL shapes |
See Also
- Basics: SELECT scripts
- UPDATE: modifying the graph with INSERT and DELETE
- Script Handlers: how CONSTRUCT output becomes named graphs
- Named Graphs: where CONSTRUCT output is stored