From: <mrp...@us...> - 2011-02-22 20:58:01
|
Revision: 4225 http://bigdata.svn.sourceforge.net/bigdata/?rev=4225&view=rev Author: mrpersonick Date: 2011-02-22 20:57:55 +0000 (Tue, 22 Feb 2011) Log Message: ----------- isLiteral support Modified Paths: -------------- branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataEvaluationStrategyImpl3.java branches/QUADS_QUERY_BRANCH/bigdata-sails/src/test/com/bigdata/rdf/sail/TestInlineValues.java Modified: branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataEvaluationStrategyImpl3.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataEvaluationStrategyImpl3.java 2011-02-22 20:56:49 UTC (rev 4224) +++ branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataEvaluationStrategyImpl3.java 2011-02-22 20:57:55 UTC (rev 4225) @@ -21,6 +21,7 @@ import org.openrdf.model.Literal; import org.openrdf.model.URI; import org.openrdf.model.Value; +import org.openrdf.model.impl.BooleanLiteralImpl; import org.openrdf.query.BindingSet; import org.openrdf.query.Dataset; import org.openrdf.query.QueryEvaluationException; @@ -29,6 +30,7 @@ import org.openrdf.query.algebra.Compare; import org.openrdf.query.algebra.Filter; import org.openrdf.query.algebra.Group; +import org.openrdf.query.algebra.IsLiteral; import org.openrdf.query.algebra.Join; import org.openrdf.query.algebra.LeftJoin; import org.openrdf.query.algebra.MathExpr; @@ -84,10 +86,12 @@ import com.bigdata.btree.keys.IKeyBuilderFactory; import com.bigdata.rdf.internal.DummyIV; import com.bigdata.rdf.internal.IV; +import com.bigdata.rdf.internal.XSDBooleanIV; import com.bigdata.rdf.internal.constraints.AndBOp; import com.bigdata.rdf.internal.constraints.CompareBOp; import com.bigdata.rdf.internal.constraints.EBVBOp; import com.bigdata.rdf.internal.constraints.IsBoundBOp; +import com.bigdata.rdf.internal.constraints.IsLiteralBOp; import com.bigdata.rdf.internal.constraints.MathBOp; import com.bigdata.rdf.internal.constraints.NotBOp; import com.bigdata.rdf.internal.constraints.OrBOp; @@ -2059,6 +2063,7 @@ */ private IValueExpression<IV> toVE(final ValueExpr ve) throws UnsupportedOperatorException { + if (ve instanceof Var) { return toVE((Var) ve); } else if (ve instanceof ValueConstant) { @@ -2077,6 +2082,8 @@ return toVE((Compare) ve); } else if (ve instanceof Bound) { return toVE((Bound) ve); + } else if (ve instanceof IsLiteral) { + return toVE((IsLiteral) ve); } throw new UnsupportedOperatorException(ve); @@ -2158,6 +2165,11 @@ return new IsBoundBOp(var); } + private IValueExpression<IV> toVE(final IsLiteral isLiteral) { + final IVariable<IV> var = (IVariable<IV>) toVE(isLiteral.getArg()); + return new IsLiteralBOp(var); + } + /** * Generate a bigdata term from a Sesame term. * <p> @@ -2189,7 +2201,14 @@ * value does not exist in the lexicon. */ private IConstant<IV> toVE(final ValueConstant vc) { - final IV iv = ((BigdataValue) vc.getValue()).getIV(); + final IV iv; + final Value v = vc.getValue(); + if (v instanceof BooleanLiteralImpl) { + final BooleanLiteralImpl bl = (BooleanLiteralImpl) v; + iv = XSDBooleanIV.valueOf(bl.booleanValue()); + } else { + iv = ((BigdataValue) vc.getValue()).getIV(); + } if (iv == null) throw new UnrecognizedValueException(vc.getValue()); return new Constant<IV>(iv); Modified: branches/QUADS_QUERY_BRANCH/bigdata-sails/src/test/com/bigdata/rdf/sail/TestInlineValues.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-sails/src/test/com/bigdata/rdf/sail/TestInlineValues.java 2011-02-22 20:56:49 UTC (rev 4224) +++ branches/QUADS_QUERY_BRANCH/bigdata-sails/src/test/com/bigdata/rdf/sail/TestInlineValues.java 2011-02-22 20:57:55 UTC (rev 4225) @@ -29,6 +29,8 @@ import java.util.Collection; import java.util.LinkedList; import java.util.Properties; + +import org.apache.log4j.Logger; import org.openrdf.model.Literal; import org.openrdf.model.URI; import org.openrdf.model.ValueFactory; @@ -48,6 +50,8 @@ */ public class TestInlineValues extends ProxyBigdataSailTestCase { + protected static final Logger log = Logger.getLogger(TestInlineValues.class); + @Override public Properties getProperties() { @@ -210,4 +214,127 @@ } + public void testIsLiteral() throws Exception { + + final BigdataSail sail = getSail(); + sail.initialize(); + final BigdataSailRepository repo = new BigdataSailRepository(sail); + final BigdataSailRepositoryConnection cxn = + (BigdataSailRepositoryConnection) repo.getConnection(); + cxn.setAutoCommit(false); + + try { + + final ValueFactory vf = sail.getValueFactory(); + + URI A = vf.createURI("_:A"); + URI B = vf.createURI("_:B"); + URI X = vf.createURI("_:X"); + URI AGE = vf.createURI("_:AGE"); + Literal _25 = vf.createLiteral(25); + Literal _45 = vf.createLiteral(45); + + cxn.add(A, RDF.TYPE, X); + cxn.add(B, RDF.TYPE, X); + cxn.add(A, AGE, _25); + cxn.add(B, AGE, _45); + + /* + * Note: The either flush() or commit() is required to flush the + * statement buffers to the database before executing any operations + * that go around the sail. + */ + cxn.flush();//commit(); + + if (log.isInfoEnabled()) { + log.info("\n" + sail.getDatabase().dumpStore()); + } + + { + + String query = + "select ?s ?age " + + "WHERE { " + + " ?s <"+RDF.TYPE+"> <"+X+"> . " + + " ?s <"+AGE+"> ?age . " + + " FILTER( isLiteral(?age) ) . " + + "}"; + + final TupleQuery tupleQuery = + cxn.prepareTupleQuery(QueryLanguage.SPARQL, query); + + if (log.isInfoEnabled()) { + final TupleQueryResult result = tupleQuery.evaluate(); + log.info("results:"); + if (!result.hasNext()) { + log.info("no results."); + } + while (result.hasNext()) { + log.info(result.next()); + } + } + + final TupleQueryResult result = tupleQuery.evaluate(); + + Collection<BindingSet> solution = new LinkedList<BindingSet>(); + solution.add(createBindingSet(new Binding[] { + new BindingImpl("s", A), + new BindingImpl("age", _25) + })); + solution.add(createBindingSet(new Binding[] { + new BindingImpl("s", B), + new BindingImpl("age", _45) + })); + + compare(result, solution); + + } + + { + + String query = + "select ?s ?age " + + "WHERE { " + + " ?s <"+RDF.TYPE+"> <"+X+"> . " + + " ?s <"+AGE+"> ?age . " + + " FILTER( isLiteral("+_25.toString()+") ) . " + + "}"; + + final TupleQuery tupleQuery = + cxn.prepareTupleQuery(QueryLanguage.SPARQL, query); + + if (log.isInfoEnabled()) { + final TupleQueryResult result = tupleQuery.evaluate(); + log.info("results:"); + if (!result.hasNext()) { + log.info("no results."); + } + while (result.hasNext()) { + log.info(result.next()); + } + } + + final TupleQueryResult result = tupleQuery.evaluate(); + + Collection<BindingSet> solution = new LinkedList<BindingSet>(); + solution.add(createBindingSet(new Binding[] { + new BindingImpl("s", A), + new BindingImpl("age", _25) + })); + solution.add(createBindingSet(new Binding[] { + new BindingImpl("s", B), + new BindingImpl("age", _45) + })); + + compare(result, solution); + + } + + } finally { + cxn.close(); + sail.__tearDownUnitTest(); + } + + } + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |