On Thu, Aug 1, 2013 at 10:12 AM, Andy Seaborne <[email protected]> wrote:
> On 01/08/13 01:52, Holger Knublauch wrote:
>>
>> I am upgrading our code base to the latest Jena snapshot, and noticed
>> that UpdateProcessor.setInitialBindings is no longer around. What is the
>> replacement for this functionality?
>>
>> Thanks
>> Holger
>>
>
> Holger,
>
> https://jena.apache.org/documentation/query/parameterized-sparql-strings.html
> http://jena.apache.org/documentation/javadoc/arq/com/hp/hpl/jena/query/ParameterizedSparqlString.html

It's also possible to simulate the initial bindings by using a VALUES
block on the query.  This also has the advantage that it can be used
for binding a number of solutions at once.  Here's an example:

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.sparql.core.Var;
import com.hp.hpl.jena.sparql.engine.binding.Binding;
import com.hp.hpl.jena.sparql.engine.binding.BindingFactory;


public class ValuesInsteadOfInitialBindings {
        @SuppressWarnings("serial")
        public static void main(String[] args) {
                final String ttlContent = "" +
                                "@prefix : <http://example.org/> .\n" +
                                "\n" +
                                ":a :p :x ;\n" +
                                "   :q :aa .\n" +
                                ":b :p :x ;\n" +
                                "   :q :bb .\n" +
                                ":c :p :y.\n" +
                                "";
                
                // Setup a model with the data above
                final Model model = ModelFactory.createDefaultModel();
                model.read( new ByteArrayInputStream( ttlContent.getBytes() ), 
null, "TTL" );
                
                final String thingsThatPX = ""+
                                "prefix : <http://example.org/>\n" +
                                "select * where {\n" +
                                "  ?thing :p :x .\n" +
                                "}\n" +
                                "";
                
                // Get a result set that binds ?thing to each value that :x
                // as a value for :p
                final ResultSet results = QueryExecutionFactory.create(
thingsThatPX, model ).execSelect();
                
                // A query that retrieves the ?qValue of each ?thing. ?thing 
here
has no binding
                // and would match *everything* that has a value for :q.
                final String qOfThings = "" +
                                "prefix : <http://example.org/>\n" +
                                "select * where {\n" +
                                "  ?thing :q ?qValue .\n" +
                                "}\n" +
                                "";
                
                // Create the query and show it, before setting up
                // any VALUES.
                final Query query = QueryFactory.create( qOfThings );
                System.out.println( query );
                
                // Create variables and bindings for the VALUES block
                final List<Var> variables = new ArrayList<Var>() {{
                        for ( final String varName : results.getResultVars() ) {
                                add( Var.alloc( varName ));
                        }
                }};
                final List<Binding> bindings = new ArrayList<Binding>() {{
                        while ( results.hasNext() ) {
                                final QuerySolution solution = results.next();
                                for ( final Var var : variables ) {
                                        add( BindingFactory.binding( var, 
solution.get( var.getName()
).asNode() ));
                                }
                        }
                }};

                // Set the VALUES block and show the query again.
                query.setValuesDataBlock( variables, bindings );
                System.out.println( query );
                
                // Show the final results
                ResultSetFormatter.out( QueryExecutionFactory.create( query, 
model
).execSelect() );
        }
}



-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/

Reply via email to