This query generates an RDF list from an RDF graph that is a linear path. To set up initial data:
stardog query test "insert data { :a :p :b . :b :p :c . :c :p :d }"
The list generation query:
construct {
?start rdf:first ?s ;
rdf:rest ?end .
?end rdf:first ?o .
?last rdf:rest rdf:nil .
}
where {
{
?s :p ?o
bind(bnode(str(?s)) as ?start)
bind(bnode(str(?o)) as ?end)
}
UNION {
?s :p ?o
FILTER NOT EXISTS { ?o :p [] }
BIND(bnode(str(?o)) as ?last)
}
}
The result (note the pretty Turtle formatting to validate the well-formedness):
stardog query test -f PRETTY_TURTLE ~/cp/tmp/list.rq
@prefix ...
( :a :b :c :d ) .
PS. The awkward bit is appending the rdf:nil terminator, probably can be done more efficiently...
Just for the lolz, here's how you can do it as a SPARQL Update procedure:
The result of that should be
( :a :b :c :d ).