problem with insert query, can't pars simple statements
Example: query:
PREFIX dc: <http://purl.org/dc/elements/1.1/>
INSERT DATA
{
<http://example/book1> dc:title "A new book";
dc:creator "A.N.Other" .
}
result:
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX sesame: <http://www.openrdf.org/schema/sesame#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX fn: <http://www.w3.org/2005/xpath-functions#>
<http://example/book1> dc:title "A new book" ; dc:creator "A.N.Other" .
But if I pars something more difficult all is well.
query:
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
INSERT
{ GRAPH <http://example/bookStore2> { ?book ?p ?v } }
WHERE
{ GRAPH <http://example/bookStore>
{ ?book dc:date ?date .
FILTER ( ?date > "1970-01-01T00:00:00-02:00"^^xsd:dateTime )
?book ?p ?v
} }
result:
StatementPattern FROM NAMED CONTEXT
Var (name=book)
Var (name=p)
Var (name=v)
Var (name=_const_f1165b11_uri, value=http://example/bookStore2, anonymous)
Modify
StatementPattern FROM NAMED CONTEXT
Var (name=book)
Var (name=p)
Var (name=v)
Var (name=_const_f1165b11_uri, value=http://example/bookStore2, anonymous)
Filter
Compare (>)
Var (name=date)
ValueConstant (value="1970-01-01T00:00:00-02:00"^^<http://www.w3.org/2001/XMLSchema#dateTime>)
Join
StatementPattern FROM NAMED CONTEXT
Var (name=book)
Var (name=_const_9b277d39_uri, value=http://purl.org/dc/elements/1.1/date, anonymous)
Var (name=date)
Var (name=_const_39534d41_uri, value=http://example/bookStore, anonymous)
StatementPattern FROM NAMED CONTEXT
Var (name=book)
Var (name=p)
Var (name=v)
Var (name=_const_39534d41_uri, value=http://example/bookStore, anonymous)
I need to get something like that the first case. What am I doing wrong?
Java code for executing these queries:
String query = ...
SPARQLParser parser = new SPARQLParser();
ParsedUpdate q2 = parser.parseUpdate(query, null);
Iterator var2 = q2.getUpdateExprs().iterator();
UpdateExpr updateExpr = (UpdateExpr)var2.next();
System.out.println(updateExpr);
As indicated in the comments, the data block for an INSERT DATA
SPARQL update is internally stored as an (unparsed) string. If you wish to process it further, you have a couple of options.
One option is to actually just execute the update, for example against a temporary/empty in-memory store, and then just retrieve the statements from that store:
String update = "INSERT DATA { .... }";
Repository tempRep = new SailRepository(new MemoryStore());
tempRep.init();
try(RepositoryConnection conn = tempRep.getConnection()) {
conn.prepareUpdate(update).execute());
Model statements = QueryResults.asModel(conn.getStatements(null, null, null));
}
Another option, which is perhaps more scalable, is to just extract the data block as a string and pass it through a parser. The SPARQLUpdateDataBlockParser
specifically exists for this purpose:
InsertData insertDataExpr = (InsertData)updateExpr;
RDFParser parser = new SPARQLUpdateDataBlockParser();
StatementCollector handler = new StatementCollector();
parser.setRDFHandler(handler);
parser.parse(new StringReader(insertDataExpr.getDataBlock()), "");
Collection<Statement> stmts = handler.getStatements();