Frage

I was wondering if it was possible to create a SPARQL UpdateRequest in Jena by using ARQ Op objects. I would be interested to create programmatically updates like this:

DELETE {?s :predicate <http://example.org#old> }    
INSERT {?s :predicate <http://example.org#toAdd>} 
WHERE  {?s :predicate <http://example.org#old> } 

by creating the patterns in the DELETE, INSERT, and WHERE clauses from the ARQ API. So far the only ways I have found to create SPARQL Update requests require to parse a SPARQL string or to create a com.hp.hpl.jena.update.Update object (which uses QuadAcc objects for which I couldn't find examples of use.

My fear is that the management of SPARQL UPDATE requests and the one of SPARQL SELECT queries are separated and that ARQ cannot be used to 'assemble' queries on the fly.

Thanks in advance

War es hilfreich?

Lösung

I haven't tried this myself, but it looks like creating Update objects and assembling them into an UpdateRequest is indeed the way to go.

After a short look, QuadAcc doesn't seem particularly difficult, just use addTriple() with triples that contain variables.

The UpdateModify subclass of Update looks particularly interesting, it corresponds to the DELETE … INSERT … WHERE pattern in your example. Unfortunately the WHERE clause is initialised with an Element (syntactic representation of a query part) rather than an Op (algebraic representation).

Andere Tipps

This question also burned me. I wanted to compose an UpdateRequest from ElementGroup objects and ElementTriplesBlock objects. This are the two main classes used to construct a Query. For example:

ElementGroup queryPattern = ...
ElementTriplesBlock constructTriples = ...

Query query = new Query();
query.setQueryConstructType();

// set CONSTRUCT clause
query.setConstructTemplate(new Template(constructTriples.getPattern()));

// set WHERE clause
query.setQueryPattern(queryPattern);

I tried the Jena mailing-list and received this answer:

The Update API is designed to deal with streaming arbitrarily large unbounded INSERT and DELETE data hence the use of QuadAcc rather than an Element for the INSERT/DELETE portion of the update.

Eventually I implemented this using a ParametrizedSparqlString:

ElementGroup queryPattern = ...
ElementTriplesBlock deleteTriples = ...
ElementTriplesBlock insertTriples = ...

ParameterizedSparqlString qstring = new ParameterizedSparqlString();

// Set DELETE clause
qstring.append("DELETE {");
qstring.append(deleteTriples.toString());
qstring.append("}");

// Set INSERT clause
qstring.append("INSERT {");
qstring.append(insertTriples.toString());
qstring.append("}");

// Set WHERE clause
qstring.append("WHERE {");
qstring.append(queryPattern.toString());
qstring.append("}");

// Construct an update query
UpdateRequest request = qstring.asUpdate();
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top