From: <tho...@us...> - 2011-03-29 12:40:00
|
Revision: 4345 http://bigdata.svn.sourceforge.net/bigdata/?rev=4345&view=rev Author: thompsonbry Date: 2011-03-29 12:39:54 +0000 (Tue, 29 Mar 2011) Log Message: ----------- I've modified the BigdataValueResolver to track the variables used in the query and drop bindings whose variable name does not appear in the query. The unit test has been updated to verify the expected solution and now passes. Modified Paths: -------------- branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataValueReplacer.java branches/QUADS_QUERY_BRANCH/bigdata-sails/src/test/com/bigdata/rdf/sail/TestBigdataValueReplacer.java Modified: branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataValueReplacer.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataValueReplacer.java 2011-03-29 12:13:40 UTC (rev 4344) +++ branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataValueReplacer.java 2011-03-29 12:39:54 UTC (rev 4345) @@ -29,6 +29,8 @@ import java.util.HashMap; import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; import org.apache.log4j.Logger; import org.openrdf.model.URI; @@ -97,12 +99,17 @@ /* * Resolve the values used by this query. * - * Note: If any value can not be resolved, then its term identifer + * Note: If any value can not be resolved, then its term identifier * will remain ZERO (0L) (aka NULL). Except within OPTIONALs, this - * indicates that the query CAN NOT be satisified by the data since + * indicates that the query CAN NOT be satisfied by the data since * one or more required terms are unknown to the database. */ final HashMap<Value, BigdataValue> values = new HashMap<Value, BigdataValue>(); + + /* + * The set of variables encountered in the query. + */ + final Map<String/* name */, Var> vars = new LinkedHashMap<String, Var>(); final BigdataValueFactory valueFactory = database.getValueFactory(); @@ -121,6 +128,8 @@ @Override public void meet(final Var var) { + vars.put(var.getName(), var); + if (var.hasValue()) { final Value val = var.getValue(); @@ -162,7 +171,7 @@ if (bindings != null) { - Iterator<Binding> it = bindings.iterator(); + final Iterator<Binding> it = bindings.iterator(); while (it.hasNext()) { @@ -247,7 +256,7 @@ // the Sesame Value object. final Value val = constant.getValue(); - // Lookup the resolve BigdataValue object. + // Lookup the resolved BigdataValue object. final BigdataValue val2 = values.get(val); assert val2 != null : "value not found: "+constant.getValue(); @@ -277,17 +286,28 @@ if (bindings != null) { - MapBindingSet bindings2 = new MapBindingSet(); + final MapBindingSet bindings2 = new MapBindingSet(); - Iterator<Binding> it = bindings.iterator(); + final Iterator<Binding> it = bindings.iterator(); while (it.hasNext()) { final BindingImpl binding = (BindingImpl) it.next(); + + if (!vars.containsKey(binding.getName())) { + + // Drop bindings which are not used within the query. + + if (log.isInfoEnabled()) + log.info("Dropping unused binding: var=" + binding); + + continue; + + } final Value val = binding.getValue(); -// Lookup the resolve BigdataValue object. + // Lookup the resolved BigdataValue object. final BigdataValue val2 = values.get(val); assert val2 != null : "value not found: "+binding.getValue(); Modified: branches/QUADS_QUERY_BRANCH/bigdata-sails/src/test/com/bigdata/rdf/sail/TestBigdataValueReplacer.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-sails/src/test/com/bigdata/rdf/sail/TestBigdataValueReplacer.java 2011-03-29 12:13:40 UTC (rev 4344) +++ branches/QUADS_QUERY_BRANCH/bigdata-sails/src/test/com/bigdata/rdf/sail/TestBigdataValueReplacer.java 2011-03-29 12:39:54 UTC (rev 4345) @@ -27,16 +27,22 @@ package com.bigdata.rdf.sail; +import java.util.Collection; +import java.util.LinkedList; import java.util.Properties; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; import org.openrdf.model.Value; import org.openrdf.model.impl.LiteralImpl; import org.openrdf.model.impl.URIImpl; +import org.openrdf.query.BindingSet; import org.openrdf.query.MalformedQueryException; import org.openrdf.query.QueryEvaluationException; import org.openrdf.query.QueryLanguage; import org.openrdf.query.TupleQuery; import org.openrdf.query.TupleQueryResult; +import org.openrdf.query.impl.MapBindingSet; import org.openrdf.repository.RepositoryException; import org.openrdf.sail.SailException; @@ -70,8 +76,10 @@ @Override public Properties getProperties() { - Properties props = super.getProperties(); +// Logger.getLogger(BigdataValueReplacer.class).setLevel(Level.ALL); + final Properties props = super.getProperties(); + props.setProperty(BigdataSail.Options.TRUTH_MAINTENANCE, "false"); props.setProperty(BigdataSail.Options.AXIOMS_CLASS, NoAxioms.class.getName()); props.setProperty(BigdataSail.Options.VOCABULARY_CLASS, NoVocabulary.class.getName()); @@ -93,8 +101,8 @@ * * @see https://sourceforge.net/apps/trac/bigdata/ticket/271 */ - public void test_bug() throws RepositoryException, SailException, - MalformedQueryException, QueryEvaluationException { + public void test_dropUnusedBindings() throws RepositoryException, + SailException, MalformedQueryException, QueryEvaluationException { final BigdataSail sail = getSail(); @@ -125,18 +133,26 @@ * Setup some bindings. */ // bind to a term in the database. - q.setBinding("a", new URIImpl("s:2")); + q.setBinding("a", new URIImpl("s:1")); // bind to a term in the database. q.setBinding("b", new URIImpl("s:2")); // bind to a term NOT found in the database. q.setBinding("notused", new LiteralImpl("lit")); /* - * Evaluate the query. + * Evaluate the query and verify that the correct solution + * is produced. */ + final Collection<BindingSet> expected = new LinkedList<BindingSet>(); + { + final MapBindingSet bset = new MapBindingSet(); + bset.addBinding("a", new URIImpl("s:1")); + bset.addBinding("b", new URIImpl("s:2")); + expected.add(bset); + } final TupleQueryResult result = q.evaluate(); try { - // @todo verify that the binding was passed along unchanged. + compare(result, expected); } finally { result.close(); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |