On Jan 18, 2012, at 6:08 AM, Andy Seaborne wrote: > On 18/01/12 08:22, Stephan Zednik wrote: >> Hi, >> >> I would like to execute a describe query on a variable that is determined by >> its id, which I would like to pass to the query programmatically using ARQ. >> >> Lets say the query would look something like this: >> >> PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> >> PREFIX xsd:<http://www.w3.org/2001/XMLSchema#> >> PREFIX dc:<http://purl.org/dc/elements/1.1/> >> PREFIX dcterms:<http://purl.org/dc/terms/> >> PREFIX ex:<http://example.com/example> >> >> DESCRIBE ?foo >> WHERE { ?foo a ex:Foo . >> ?foo dcterms:identifier ?id . >> } >> >> and I would like to load the query from a file and pass a value in for ?id. >> >> What is the best way to do this? >> >> I looked at setInitialBinding( ...) on the QueryExecution but I am not sure >> it will do what I want. >> from the javadoc: >> void setInitialBinding(QuerySolution binding) >> Set the initial association of variables and values. May not be supported by >> all QueryExecution implementations. >> I am not sure setInitialBinding( ... ) is supported by execDescribe( ... ) >> because I don't think it makes sense for the ?id to be a variable in a >> describe query that resolves to a datatype. That, and also because >> execDescribe( ... ) does not return a ResultSet. > > The QueryExecution that does not suppor tit is the remote one (because the > app probably should be using BINDINDS).
Do you mean a Binding? Like BindingHashMap? http://incubator.apache.org/jena/documentation/javadoc/arq/com/hp/hpl/jena/sparql/engine/binding/BindingHashMap.html Do you know of an example where this is used? I found the javadoc, but I am not sure in what situation I would use this, especially since the javadoc says Bindings are immutable. I made a quick implementation of the string-replace suggestion like this: public Model execDescribe(String sparql, Map<String, String> params) { if(params != null) { for(Map.Entry<String, String> entry : params.entrySet() ) { sparql = sparql.replaceAll("{"+entry.getKey()+"}", entry.getValue()); } } Query query = QueryFactory.create(sparql); QueryExecution qe = QueryExecutionFactory.create(query, model); try { return qe.execDescribe(); } finally { qe.close(); } } How could I easily modify this code to internally use a Binding? > >> >> Will using a QuerySolution with an mapping of "id" to the application >> determined value work? > > Have you tried? It should work. > > >> >> Thanks, >> --Stephan > > The other approach is to store a query template: example: > > DESCRIBE ?foo > WHERE { ?foo a ex:Foo . > ?foo dcterms:identifier ?{id} . > } > > and do a string replacement on on "?{id}" I like this idea as it is similar to the parameterized ResponseMappings in Spring. Does Jena have any existing support for this? It was easy to implement but I would rather use existing functionality if it is there. --Stephan > > Andy >
