From: <mrp...@us...> - 2011-05-20 00:12:22
|
Revision: 4529 http://bigdata.svn.sourceforge.net/bigdata/?rev=4529&view=rev Author: mrpersonick Date: 2011-05-20 00:12:12 +0000 (Fri, 20 May 2011) Log Message: ----------- lex joins and full sparql 1.0 coverage Modified Paths: -------------- branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/bop/ap/Predicate.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/AbstractIV.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/IV.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/IVUtility.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/VTE.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/AndBOp.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/CompareBOp.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IVValueExpression.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IsBoundBOp.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IsInlineBOp.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IsLiteralBOp.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IsURIBOp.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/MathBOp.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/NotBOp.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/OrBOp.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/SPARQLConstraint.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/SameTermBOp.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/StrBOp.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/XSDBooleanIVValueExpression.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/lexicon/LexiconRelation.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/model/BigdataValue.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/model/BigdataValueFactoryImpl.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/model/BigdataValueImpl.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/test/com/bigdata/bop/rdf/joinGraph/TestJoinGraphOnBSBMData.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/test/com/bigdata/rdf/internal/constraints/TestInlineConstraints.java 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/BigdataSail.java 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/Rule2BOpUtility.java branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/sop/SOp2BOpUtility.java branches/QUADS_QUERY_BRANCH/bigdata-sails/src/test/com/bigdata/rdf/sail/TestSids.java Added Paths: ----------- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/bop/rdf/join/InlineMaterializeOp.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/WrappedIV.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/INeedsMaterialization.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IsMaterializedBOp.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/RegexBOp.java branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/lexicon/LexPredicate.java branches/QUADS_QUERY_BRANCH/bigdata-sails/src/test/com/bigdata/rdf/sail/TestStrBOp.java Removed Paths: ------------- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/StrIV.java Modified: branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/bop/ap/Predicate.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/bop/ap/Predicate.java 2011-05-19 20:03:02 UTC (rev 4528) +++ branches/QUADS_QUERY_BRANCH/bigdata/src/java/com/bigdata/bop/ap/Predicate.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -41,6 +41,7 @@ import com.bigdata.bop.IVariableOrConstant; import com.bigdata.bop.NV; import com.bigdata.bop.bindingSet.ArrayBindingSet; +import com.bigdata.journal.ITx; import com.bigdata.relation.accesspath.ElementFilter; import com.bigdata.relation.accesspath.IElementFilter; import com.bigdata.relation.rule.IAccessPathExpander; @@ -70,8 +71,8 @@ /** * Required shallow copy constructor. */ - public Predicate(final BOp[] values, final Map<String, Object> annotations) { - super(values, annotations); + public Predicate(final BOp[] args, final Map<String, Object> annotations) { + super(args, annotations); } /** @@ -86,9 +87,9 @@ * @param vars * @param annotations */ - public Predicate(final BOp[] vars, final NV... annotations) { + public Predicate(final BOp[] args, final NV... annotations) { - super(vars, NV.asMap(annotations)); + super(args, NV.asMap(annotations)); } @@ -104,22 +105,22 @@ } } -// /** -// * Simplified ctor. -// * -// * @param values -// * The values (order is important!). -// * @param relationName -// * Identifies the relation to be queried. -// */ -// public Predicate(final IVariableOrConstant<?>[] values, -// final String relationName) { -// -// this(values, relationName, -1/* partitionId */, false/* optional */, -// null/* constraint */, null/* expander */, ITx.READ_COMMITTED); -// -// } + /** + * Simplified ctor (for lex predicates). + * + * @param values + * The values (order is important!). + * @param relationName + * Identifies the relation to be queried. + */ + public Predicate(final IVariableOrConstant<?>[] values, + final String relationName, final long timestamp) { + this(values, relationName, -1/* partitionId */, false/* optional */, + null/* constraint */, null/* expander */, ITx.READ_COMMITTED); + + } + /** * * @param relationName Added: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/bop/rdf/join/InlineMaterializeOp.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/bop/rdf/join/InlineMaterializeOp.java (rev 0) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/bop/rdf/join/InlineMaterializeOp.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -0,0 +1,232 @@ +/** + +Copyright (C) SYSTAP, LLC 2006-2011. All rights reserved. + +Contact: + SYSTAP, LLC + 4501 Tower Road + Greensboro, NC 27410 + lic...@bi... + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; version 2 of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +package com.bigdata.bop.rdf.join; + +import java.util.Arrays; +import java.util.Map; +import java.util.concurrent.Callable; +import java.util.concurrent.FutureTask; + +import org.apache.log4j.Logger; + +import com.bigdata.bop.BOp; +import com.bigdata.bop.BOpContext; +import com.bigdata.bop.IBindingSet; +import com.bigdata.bop.IPredicate; +import com.bigdata.bop.IVariable; +import com.bigdata.bop.PipelineOp; +import com.bigdata.bop.engine.BOpStats; +import com.bigdata.rdf.internal.IV; +import com.bigdata.rdf.internal.NotMaterializedException; +import com.bigdata.rdf.lexicon.LexiconRelation; +import com.bigdata.relation.accesspath.IAsynchronousIterator; +import com.bigdata.relation.accesspath.IBlockingBuffer; + +/** + * This operator is used as part of the BigdataValue materialization step inside + * the pipeline. Inline IVs are routes to this bop to be materialized and have + * their BigdataValue cached on them. The inline IVs need access to the + * LexiconRelation to materialize themselves, but only to the class itself, not + * to the data in its indices. The lexicon's LexiconConfiguration is used by the + * ExtensionIVs in the materialization process. + * + * @author mikepersonick + */ +public class InlineMaterializeOp<E> extends PipelineOp { + + /** + * + */ + private static final long serialVersionUID = 3371029059242171846L; + + private static final transient Logger log = Logger.getLogger(InlineMaterializeOp.class); + + + public interface Annotations extends PipelineOp.Annotations { + + /** + * The {@link IPredicate} contains information about the how to find + * the lexicon relation and which variable in the incoming binding sets + * needs materializing. + */ + String PREDICATE = (InlineMaterializeOp.class.getName() + ".predicate").intern(); + + } + + /** + * Deep copy constructor. + * + * @param op + */ + public InlineMaterializeOp(final InlineMaterializeOp op) { + super(op); + } + + /** + * Shallow copy constructor. + * + * @param args + * @param anns + */ + public InlineMaterializeOp(final BOp[] args, + final Map<String, Object> anns) { + super(args, anns); + } + + /** + * {@inheritDoc} + * + * @see Annotations#PREDICATE + */ + @SuppressWarnings("unchecked") + public IPredicate<E> getPredicate() { + + return (IPredicate<E>) getRequiredProperty(Annotations.PREDICATE); + + } + + public FutureTask<Void> eval(final BOpContext<IBindingSet> context) { + + return new FutureTask<Void>(new MaterializeTask(this, context)); + + } + + /** + * Copy the source to the sink after materializing the BigdataValues. + */ + static private class MaterializeTask implements Callable<Void> { + + private final BOpStats stats; + + private final IVariable<IV> v; + + private final LexiconRelation lex; + + private final IAsynchronousIterator<IBindingSet[]> source; + + private final IBlockingBuffer<IBindingSet[]> sink; + + MaterializeTask(final InlineMaterializeOp op, + final BOpContext<IBindingSet> context) { + + this.stats = context.getStats(); + + final IPredicate predicate = op.getPredicate(); + + if (predicate == null) + throw new IllegalArgumentException(); + + this.v = (IVariable<IV>) predicate.get(1); + + this.lex = (LexiconRelation) context.getRelation(predicate); + + this.source = context.getSource(); + + this.sink = context.getSink(); + + } + + public Void call() throws Exception { + + try { + + if (log.isDebugEnabled()) { + log.debug("starting inline materialization"); + } + + while (source.hasNext()) { + + final IBindingSet[] chunk = source.next(); + + if (log.isDebugEnabled()) { + log.debug("chunk length: " + chunk.length); + } + + stats.chunksIn.increment(); + stats.unitsIn.add(chunk.length); + + final IBindingSet[] def = new IBindingSet[chunk.length]; + + int ndef = 0, nalt = 0; + + for(int i=0; i<chunk.length; i++) { + + final IBindingSet bset = chunk[i]; + + final IV iv = v.get(bset); + + if (log.isDebugEnabled()) { + log.debug("materializing: " + iv); + } + + if (iv != null && iv.isInline()) { + + /* + * This will materialize the BigdataValue and cache + * it on the IV as a side-effect. + */ + iv.asValue(lex); + + if (!iv.hasValue()) { + throw new NotMaterializedException(); + } + + if (log.isDebugEnabled()) { + log.debug("value: " + iv.getValue()); + } + + def[ndef++] = bset; + } + + } + + if (ndef > 0) { + if (ndef == def.length) + sink.add(def); + else + sink.add(Arrays.copyOf(def, ndef)); + } + + } + + sink.flush(); + + return null; + + } finally { + + sink.close(); + + if (log.isDebugEnabled()) { + log.debug("finished inline materialization"); + } + + } + + } + + } + +} Modified: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/AbstractIV.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/AbstractIV.java 2011-05-19 20:03:02 UTC (rev 4528) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/AbstractIV.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -409,6 +409,12 @@ } + final public boolean isResource() { + + return isURI() || isBNode(); + + } + /** * {@inheritDoc} * <p> @@ -663,7 +669,7 @@ * * @return The argument. */ - protected V setValue(final V v) { + public V setValue(final V v) { return (this.cache = v); @@ -685,4 +691,13 @@ } + /** + * Return <code>true</code> if cached or <code>false</code> if not. + */ + final public boolean hasValue() { + + return this.cache != null; + + } + } Modified: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/IV.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/IV.java 2011-05-19 20:03:02 UTC (rev 4528) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/IV.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -86,7 +86,9 @@ */ boolean isLiteral(); - /** Return <code>true</code> iff this is an RDF BlankNode. */ + /** + * Return <code>true</code> iff this is an RDF BlankNode. + */ boolean isBNode(); /** @@ -99,6 +101,11 @@ * is enabled with {@link Options#STATEMENT_IDENTIFIERS}). */ boolean isStatement(); + + /** + * Return <code>true</code> iff this is a URI or a bnode. + */ + boolean isResource(); /* * Data type methods. @@ -226,6 +233,14 @@ V asValue(final LexiconRelation lex) throws UnsupportedOperationException; + /** + * Cache the materialized value. + * + * @param val + * The {@link BigdataValue}. + */ + V setValue(final V val); + /** * Return a pre-materialized RDF {@link BigdataValue} which has been cached * on this {@link IV} by a previous invocation of @@ -244,4 +259,10 @@ */ void dropValue(); + /** + * Returns true if the RDF {@link BigdataValue} has been pre-materialized + * and cached on this {@link IV}. + */ + boolean hasValue(); + } Modified: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/IVUtility.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/IVUtility.java 2011-05-19 20:03:02 UTC (rev 4528) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/IVUtility.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -376,6 +376,15 @@ } /** + * Convenience method. Return true if both operands return true. + */ + public static final boolean canNumericalCompare(final IV iv1, final IV iv2) { + + return canNumericalCompare(iv1) && canNumericalCompare(iv2); + + } + + /** * Encode an RDF value into a key for one of the statement indices. Handles * null {@link IV} references gracefully. * Deleted: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/StrIV.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/StrIV.java 2011-05-19 20:03:02 UTC (rev 4528) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/StrIV.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -1,159 +0,0 @@ -/** - -Copyright (C) SYSTAP, LLC 2006-2010. All rights reserved. - -Contact: - SYSTAP, LLC - 4501 Tower Road - Greensboro, NC 27410 - lic...@bi... - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; version 2 of the License. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -*/ -/* - * Created on Jul 9, 2010 - */ - -package com.bigdata.rdf.internal; - -import com.bigdata.btree.keys.IKeyBuilder; -import com.bigdata.rdf.lexicon.LexiconRelation; -import com.bigdata.rdf.model.BigdataValue; - -/** - * A StrIV is used by the StrBOp to create an alternate "view" of a Literal or - * URI IV, one where the {@link BigdataValue} is transormed into a simple - * literal (no datatype, no language tag) using the URI's toString() or the - * Literal's label. - * - * TODO Mike, I'd advise handling this as a TermId standing in for a Literal - * whose datatype is xsd:string and whose termId is ZERO. That will basically - * look like a Literal which has not been inserted into (or looked up against) - * the database. At a minimum, I would extend {@link AbstractIV}. Bryan - */ -public class StrIV implements IV { - - /** - * - */ - private static final long serialVersionUID = 5020144206004241997L; - - private final IV iv; - - private final BigdataValue strVal; - - public StrIV(final IV iv, final BigdataValue strVal) { - this.iv = iv; - this.strVal = strVal; - } - - public String toString() { - return iv.toString(); - } - - public BigdataValue asValue(final LexiconRelation lex) { - return strVal; - } - - public int compareTo(Object o) { - return iv.compareTo(o); - } - - public byte flags() { - return iv.flags(); - } - - public int byteLength() { - return iv.byteLength(); - } - - public IKeyBuilder encode(IKeyBuilder keyBuilder) { - return iv.encode(keyBuilder); - } - - public VTE getVTE() { - return iv.getVTE(); - } - - public boolean isLiteral() { - return iv.isLiteral(); - } - - public boolean isBNode() { - return iv.isBNode(); - } - - public boolean isURI() { - return iv.isURI(); - } - - public boolean isStatement() { - return iv.isStatement(); - } - - public DTE getDTE() { - return iv.getDTE(); - } - - public boolean isTermId() { - return iv.isTermId(); - } - - public long getTermId() throws UnsupportedOperationException { - return iv.getTermId(); - } - - public boolean isInline() { - return iv.isInline(); - } - - public Object getInlineValue() throws UnsupportedOperationException { - return iv.getInlineValue(); - } - - public boolean isNumeric() { - return iv.isNumeric(); - } - - public boolean isSignedNumeric() { - return iv.isSignedNumeric(); - } - - public boolean isUnsignedNumeric() { - return iv.isUnsignedNumeric(); - } - - public boolean isFixedNumeric() { - return iv.isFixedNumeric(); - } - - public boolean isBigNumeric() { - return iv.isBigNumeric(); - } - - public boolean isFloatingPointNumeric() { - return iv.isFloatingPointNumeric(); - } - - public void dropValue() { - // TODO Auto-generated method stub - - } - - public BigdataValue getValue() throws NotMaterializedException { - // TODO Auto-generated method stub - return null; - } - -} Modified: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/VTE.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/VTE.java 2011-05-19 20:03:02 UTC (rev 4528) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/VTE.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -27,6 +27,11 @@ package com.bigdata.rdf.internal; +import org.openrdf.model.BNode; +import org.openrdf.model.Literal; +import org.openrdf.model.URI; +import org.openrdf.model.Value; + import com.bigdata.rdf.lexicon.TermIdEncoder; /** @@ -126,4 +131,18 @@ } } + /** + * Determine the VTE based on the type of Value - URI, Literal, or BNode. + */ + static public final VTE valueOf(final Value v) { + if (v instanceof URI) + return URI; + if (v instanceof Literal) + return LITERAL; + if (v instanceof BNode) + return BNODE; + + throw new IllegalArgumentException(); + } + } Copied: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/WrappedIV.java (from rev 4427, branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/StrIV.java) =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/WrappedIV.java (rev 0) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/WrappedIV.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -0,0 +1,171 @@ +/** + +Copyright (C) SYSTAP, LLC 2006-2010. All rights reserved. + +Contact: + SYSTAP, LLC + 4501 Tower Road + Greensboro, NC 27410 + lic...@bi... + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; version 2 of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ +/* + * Created on Jul 9, 2010 + */ + +package com.bigdata.rdf.internal; + +import com.bigdata.btree.keys.IKeyBuilder; +import com.bigdata.rdf.lexicon.LexiconRelation; +import com.bigdata.rdf.model.BigdataValue; + +/** + * This is used by certain IVValueExpressions to create an alternate "view" of a + * Literal or URI IV, such as one where the {@link BigdataValue} is transformed + * into a simple literal (no datatype, no language tag) using the URI's + * toString() or the Literal's label. + * + * TODO Mike, I'd advise handling this as a TermId standing in for a Literal + * whose datatype is xsd:string and whose termId is ZERO. That will basically + * look like a Literal which has not been inserted into (or looked up against) + * the database. At a minimum, I would extend {@link AbstractIV}. Bryan + */ +public class WrappedIV implements IV { + + /** + * + */ + private static final long serialVersionUID = 5020144206004241997L; + + private final IV iv; + + private volatile transient BigdataValue altVal; + + public WrappedIV(final IV iv, final BigdataValue altVal) { + this.iv = iv; + this.altVal = altVal; + } + + public String toString() { + return iv.toString(); + } + + public BigdataValue asValue(final LexiconRelation lex) { + return altVal; + } + + public BigdataValue setValue(final BigdataValue altVal) { + return (this.altVal = altVal); + } + + public BigdataValue getValue() throws NotMaterializedException { + return altVal; + } + + public boolean hasValue() { + return altVal != null; + } + + public void dropValue() { + altVal = null; + } + + // delegate everything else + + public int compareTo(Object o) { + return iv.compareTo(o); + } + + public byte flags() { + return iv.flags(); + } + + public int byteLength() { + return iv.byteLength(); + } + + public IKeyBuilder encode(IKeyBuilder keyBuilder) { + return iv.encode(keyBuilder); + } + + public VTE getVTE() { + return iv.getVTE(); + } + + public boolean isLiteral() { + return iv.isLiteral(); + } + + public boolean isBNode() { + return iv.isBNode(); + } + + public boolean isURI() { + return iv.isURI(); + } + + public boolean isStatement() { + return iv.isStatement(); + } + + public boolean isResource() { + return iv.isResource(); + } + + public DTE getDTE() { + return iv.getDTE(); + } + + public boolean isTermId() { + return iv.isTermId(); + } + + public long getTermId() throws UnsupportedOperationException { + return iv.getTermId(); + } + + public boolean isInline() { + return iv.isInline(); + } + + public Object getInlineValue() throws UnsupportedOperationException { + return iv.getInlineValue(); + } + + public boolean isNumeric() { + return iv.isNumeric(); + } + + public boolean isSignedNumeric() { + return iv.isSignedNumeric(); + } + + public boolean isUnsignedNumeric() { + return iv.isUnsignedNumeric(); + } + + public boolean isFixedNumeric() { + return iv.isFixedNumeric(); + } + + public boolean isBigNumeric() { + return iv.isBigNumeric(); + } + + public boolean isFloatingPointNumeric() { + return iv.isFloatingPointNumeric(); + } + +} Modified: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/AndBOp.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/AndBOp.java 2011-05-19 20:03:02 UTC (rev 4528) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/AndBOp.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -43,10 +43,23 @@ */ private static final long serialVersionUID = -1217715173822304819L; + /** + * Each operand of this operator must evaluate to a boolean. If the operand + * is not known to evaluate to a boolean, wrap it with an {@link EBVBOp}. + */ + private static final XSDBooleanIVValueExpression wrap( + final IValueExpression<? extends IV> ve) { + + return ve instanceof XSDBooleanIVValueExpression ? + (XSDBooleanIVValueExpression) ve : + new EBVBOp(ve); + + } + public AndBOp(final IValueExpression<? extends IV> x, final IValueExpression<? extends IV> y) { - this(new BOp[] { x, y }, null/*annocations*/); + this(new BOp[] { wrap(x), wrap(y) }, null/*annocations*/); } @@ -59,7 +72,13 @@ if (args.length != 2 || args[0] == null || args[1] == null) throw new IllegalArgumentException(); + + if (!(args[0] instanceof XSDBooleanIVValueExpression)) + throw new IllegalArgumentException(); + if (!(args[1] instanceof XSDBooleanIVValueExpression)) + throw new IllegalArgumentException(); + } /** @@ -68,6 +87,11 @@ public AndBOp(final AndBOp op) { super(op); } + + @Override + public XSDBooleanIVValueExpression get(final int i) { + return (XSDBooleanIVValueExpression) super.get(i); + } /** * Follows semantics from SPARQL spec - "Testing Values". Modified: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/CompareBOp.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/CompareBOp.java 2011-05-19 20:03:02 UTC (rev 4528) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/CompareBOp.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -24,27 +24,30 @@ package com.bigdata.rdf.internal.constraints; +import java.util.LinkedHashSet; import java.util.Map; +import java.util.Set; import org.apache.log4j.Logger; import org.openrdf.query.algebra.Compare.CompareOp; +import org.openrdf.query.algebra.evaluation.util.QueryEvaluationUtil; import com.bigdata.bop.BOp; import com.bigdata.bop.IBindingSet; import com.bigdata.bop.IValueExpression; +import com.bigdata.bop.IVariable; import com.bigdata.bop.NV; import com.bigdata.bop.PipelineOp; import com.bigdata.rdf.error.SparqlTypeErrorException; import com.bigdata.rdf.internal.IV; import com.bigdata.rdf.internal.IVUtility; -import com.bigdata.rdf.internal.XSDBooleanIV; +import com.bigdata.rdf.model.BigdataValue; /** - * Use inline terms to perform numerical comparison operations. - * - * @see IVUtility#numericalCompare(IV, IV) + * Perform open-world value comparison operations per the SPARQL spec. */ -public class CompareBOp extends XSDBooleanIVValueExpression { +public class CompareBOp extends XSDBooleanIVValueExpression + implements INeedsMaterialization { /** * @@ -83,6 +86,10 @@ throw new IllegalArgumentException(); } + + if (log.isDebugEnabled()) { + log.debug(toString()); + } } @@ -105,41 +112,63 @@ // not yet bound if (left == null || right == null) throw new SparqlTypeErrorException(); + + if (log.isDebugEnabled()) { + log.debug("left: " + left); + log.debug("right: " + right); + + log.debug("left value: " + (left.hasValue() ? left.getValue() : null)); + log.debug("right value: " + (right.hasValue() ? right.getValue() : null)); + } final CompareOp op = op(); - if (left.isTermId() && right.isTermId()) { - if (op == CompareOp.EQ || op == CompareOp.NE) { - return _accept(left.compareTo(right)); - } else { - if (log.isInfoEnabled()) - log.info("cannot compare: " - + left + " " + op + " " + right); - - throw new SparqlTypeErrorException(); - } + if (left.isStatement() || right.isStatement()) { + + throw new SparqlTypeErrorException(); + } - /* - * This code is bad. - */ - if (!IVUtility.canNumericalCompare(left) || - !IVUtility.canNumericalCompare(right)) { - if (op == CompareOp.EQ) { - return false; - } else if (op == CompareOp.NE) { + // handle the special case where we have exact termId equality + // probably would never hit this because of SameTermOp + if (op == CompareOp.EQ && left.isTermId() && right.isTermId()) { + + if (left.getTermId() == right.getTermId()) return true; - } else { - if (log.isInfoEnabled()) - log.info("cannot numerical compare: " - + left + " " + op + " " + right); - - throw new SparqlTypeErrorException(); + + } + + // handle inlines: booleans, numerics, and dateTimes (if inline) + if (IVUtility.canNumericalCompare(left, right)) { + + return _accept(IVUtility.numericalCompare(left, right)); + + } + + final BigdataValue val1 = left.getValue(); + final BigdataValue val2 = right.getValue(); + + try { + + // use the Sesame implementation directly + final boolean accept = QueryEvaluationUtil.compare(val1, val2, op); + + if (log.isDebugEnabled()) { + log.debug(accept); } + + return accept; + + } catch (Exception ex) { + + if (log.isDebugEnabled()) { + log.debug("exception: " + ex); + } + + throw new SparqlTypeErrorException(); + } - return _accept(IVUtility.numericalCompare(left, right)); - } protected boolean _accept(final int compare) { @@ -164,5 +193,27 @@ } } + + private volatile transient Set<IVariable<IV>> terms; + + public Set<IVariable<IV>> getTermsToMaterialize() { + + if (terms == null) { + + terms = new LinkedHashSet<IVariable<IV>>(); + + final IValueExpression<? extends IV> left = get(0); + if (left instanceof IVariable) + terms.add((IVariable<IV>) left); + + final IValueExpression<? extends IV> right = get(1); + if (right instanceof IVariable) + terms.add((IVariable<IV>) right); + + } + + return terms; + + } } Added: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/INeedsMaterialization.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/INeedsMaterialization.java (rev 0) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/INeedsMaterialization.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -0,0 +1,42 @@ +/* + +Copyright (C) SYSTAP, LLC 2006-2011. All rights reserved. + +Contact: + SYSTAP, LLC + 4501 Tower Road + Greensboro, NC 27410 + lic...@bi... + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; version 2 of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ +package com.bigdata.rdf.internal.constraints; + +import java.util.Set; + +import com.bigdata.bop.IVariable; +import com.bigdata.rdf.internal.IV; + + +/** + * Some {@link IVValueExpression} need materialized terms to perform their + * evaluation. Those that do can implement this interface, and specify which + * terms they need materialized. + */ +public interface INeedsMaterialization { + + Set<IVariable<IV>> getTermsToMaterialize(); + +} Modified: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IVValueExpression.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IVValueExpression.java 2011-05-19 20:03:02 UTC (rev 4528) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IVValueExpression.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -55,14 +55,18 @@ /** * Required deep copy constructor. */ - public IVValueExpression(final IVValueExpression op) { + public IVValueExpression(final IVValueExpression<T> op) { super(op); } + /** + * The presumption with IVValueExpression is that its operands are always + * themselves IVs. + */ @Override - public IValueExpression<T> get(final int i) { + public IValueExpression<? extends IV> get(final int i) { try { - return (IValueExpression<T>) super.get(i); + return (IValueExpression<? extends IV>) super.get(i); } catch (ClassCastException ex) { throw new SparqlTypeErrorException(); } Modified: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IsBoundBOp.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IsBoundBOp.java 2011-05-19 20:03:02 UTC (rev 4528) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IsBoundBOp.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -28,10 +28,8 @@ import com.bigdata.bop.BOp; import com.bigdata.bop.IBindingSet; -import com.bigdata.bop.IValueExpression; import com.bigdata.bop.IVariable; import com.bigdata.rdf.internal.IV; -import com.bigdata.rdf.internal.XSDBooleanIV; /** * Imposes the constraint <code>bound(x)</code> for the variable x. @@ -68,9 +66,9 @@ super(op); } - public boolean accept(final IBindingSet s) { + public boolean accept(final IBindingSet bs) { - return get(0).get(s) != null; + return get(0).get(bs) != null; } Modified: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IsInlineBOp.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IsInlineBOp.java 2011-05-19 20:03:02 UTC (rev 4528) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IsInlineBOp.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -26,15 +26,15 @@ import java.util.Map; +import org.apache.log4j.Logger; + import com.bigdata.bop.BOp; import com.bigdata.bop.IBindingSet; -import com.bigdata.bop.IValueExpression; import com.bigdata.bop.IVariable; import com.bigdata.bop.NV; import com.bigdata.bop.PipelineOp; import com.bigdata.rdf.error.SparqlTypeErrorException; import com.bigdata.rdf.internal.IV; -import com.bigdata.rdf.internal.XSDBooleanIV; /** * Imposes the constraint <code>isInline(x)</code>. @@ -45,6 +45,9 @@ * */ private static final long serialVersionUID = 3125106876006900339L; + + private static final transient Logger log = Logger.getLogger(IsInlineBOp.class); + public interface Annotations extends PipelineOp.Annotations { @@ -59,6 +62,12 @@ } + public IsInlineBOp(final IVariable<IV> x) { + + this(x, true); + + } + public IsInlineBOp(final IVariable<IV> x, final boolean inline) { this(new BOp[] { x }, NV.asMap(new NV(Annotations.INLINE, inline))); @@ -75,6 +84,9 @@ if (args.length != 1 || args[0] == null) throw new IllegalArgumentException(); + if (getProperty(Annotations.INLINE) == null) + throw new IllegalArgumentException(); + } /** @@ -90,6 +102,12 @@ (Boolean) getRequiredProperty(Annotations.INLINE); final IV iv = get(0).get(bs); + + if (log.isDebugEnabled()) { + log.debug(iv); + if (iv != null) + log.debug("inline?: " + iv.isInline()); + } // not yet bound if (iv == null) Modified: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IsLiteralBOp.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IsLiteralBOp.java 2011-05-19 20:03:02 UTC (rev 4528) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IsLiteralBOp.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -28,11 +28,9 @@ import com.bigdata.bop.BOp; import com.bigdata.bop.IBindingSet; -import com.bigdata.bop.IValueExpression; import com.bigdata.bop.IVariable; import com.bigdata.rdf.error.SparqlTypeErrorException; import com.bigdata.rdf.internal.IV; -import com.bigdata.rdf.internal.XSDBooleanIV; /** * Imposes the constraint <code>isLiteral(x)</code>. Added: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IsMaterializedBOp.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IsMaterializedBOp.java (rev 0) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IsMaterializedBOp.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -0,0 +1,119 @@ +/* + +Copyright (C) SYSTAP, LLC 2006-2007. All rights reserved. + +Contact: + SYSTAP, LLC + 4501 Tower Road + Greensboro, NC 27410 + lic...@bi... + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; version 2 of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ +package com.bigdata.rdf.internal.constraints; + +import java.util.Map; + +import org.apache.log4j.Logger; + +import com.bigdata.bop.BOp; +import com.bigdata.bop.IBindingSet; +import com.bigdata.bop.IVariable; +import com.bigdata.bop.NV; +import com.bigdata.bop.PipelineOp; +import com.bigdata.rdf.error.SparqlTypeErrorException; +import com.bigdata.rdf.internal.IV; + +/** + * Imposes the constraint <code>isMaterialized(x)</code>. + */ +public class IsMaterializedBOp extends XSDBooleanIVValueExpression { + + /** + * + */ + private static final long serialVersionUID = -7552628930845996572L; + + private static final transient Logger log = Logger.getLogger(IsMaterializedBOp.class); + + + public interface Annotations extends PipelineOp.Annotations { + + /** + * If true, only accept variable bindings for {@link #x} that have a + * materialized RDF {@link BigdataValue}. If false, only accept those + * that don't. + */ + String MATERIALIZED = (IsMaterializedBOp.class.getName() + ".materialized").intern(); + + } + + public IsMaterializedBOp(final IVariable<IV> x) { + + this(x, true); + + } + + public IsMaterializedBOp(final IVariable<IV> x, final boolean materialized) { + + this(new BOp[] { x }, + NV.asMap(new NV(Annotations.MATERIALIZED, materialized))); + + } + + /** + * Required shallow copy constructor. + */ + public IsMaterializedBOp(final BOp[] args, final Map<String, Object> anns) { + + super(args, anns); + + if (args.length != 1 || args[0] == null) + throw new IllegalArgumentException(); + + if (getProperty(Annotations.MATERIALIZED) == null) + throw new IllegalArgumentException(); + + } + + /** + * Required deep copy constructor. + */ + public IsMaterializedBOp(final IsMaterializedBOp op) { + super(op); + } + + public boolean accept(final IBindingSet bs) { + + final boolean materialized = + (Boolean) getRequiredProperty(Annotations.MATERIALIZED); + + final IV iv = get(0).get(bs); + + if (log.isDebugEnabled()) { + log.debug(iv); + if (iv != null) + log.debug("materialized?: " + iv.hasValue()); + } + + // not yet bound + if (iv == null) + throw new SparqlTypeErrorException(); + + return iv.hasValue() == materialized; + + } + +} Modified: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IsURIBOp.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IsURIBOp.java 2011-05-19 20:03:02 UTC (rev 4528) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/IsURIBOp.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -28,11 +28,9 @@ import com.bigdata.bop.BOp; import com.bigdata.bop.IBindingSet; -import com.bigdata.bop.IValueExpression; import com.bigdata.bop.IVariable; import com.bigdata.rdf.error.SparqlTypeErrorException; import com.bigdata.rdf.internal.IV; -import com.bigdata.rdf.internal.XSDBooleanIV; /** * Imposes the constraint <code>isURI(x)</code>. Modified: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/MathBOp.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/MathBOp.java 2011-05-19 20:03:02 UTC (rev 4528) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/MathBOp.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -39,7 +39,7 @@ * operation to be applied to the operands is specified by the * {@link Annotations#OP} annotation. */ -final public class MathBOp extends IVValueExpression<IV> { +final public class MathBOp extends IVValueExpression { /** * @@ -145,11 +145,11 @@ } - public IValueExpression<IV> left() { + public IValueExpression<? extends IV> left() { return get(0); } - public IValueExpression<IV> right() { + public IValueExpression<? extends IV> right() { return get(1); } @@ -180,7 +180,7 @@ } - final public boolean equals(final IValueExpression<IV> o) { + final public boolean equals(final IVValueExpression o) { if(!(o instanceof MathBOp)) { // incomparable types. Modified: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/NotBOp.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/NotBOp.java 2011-05-19 20:03:02 UTC (rev 4528) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/NotBOp.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -29,6 +29,7 @@ import com.bigdata.bop.BOp; import com.bigdata.bop.IBindingSet; import com.bigdata.bop.IValueExpression; +import com.bigdata.rdf.error.SparqlTypeErrorException; import com.bigdata.rdf.internal.IV; import com.bigdata.rdf.internal.XSDBooleanIV; @@ -42,9 +43,22 @@ */ private static final long serialVersionUID = -5701967329003122236L; - public NotBOp(final IValueExpression<? extends IV> x) { + /** + * The operand of this operator must evaluate to a boolean. If the operand + * is not known to evaluate to a boolean, wrap it with an {@link EBVBOp}. + */ + private static final XSDBooleanIVValueExpression wrap( + final IValueExpression<? extends IV> ve) { + + return ve instanceof XSDBooleanIVValueExpression ? + (XSDBooleanIVValueExpression) ve : + new EBVBOp(ve); + + } + + public NotBOp(final IValueExpression<? extends IV> x) { - this(new BOp[] { x }, null/*Annotations*/); + this(new BOp[] { wrap(x) }, null/*annocations*/); } @@ -58,6 +72,9 @@ if (args.length != 1 || args[0] == null) throw new IllegalArgumentException(); + if (!(args[0] instanceof XSDBooleanIVValueExpression)) + throw new IllegalArgumentException(); + } /** @@ -71,6 +88,10 @@ final XSDBooleanIV iv = (XSDBooleanIV) get(0).get(bs); + // not yet bound + if (iv == null) + throw new SparqlTypeErrorException(); + return !iv.booleanValue(); } Modified: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/OrBOp.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/OrBOp.java 2011-05-19 20:03:02 UTC (rev 4528) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/OrBOp.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -43,10 +43,24 @@ */ private static final long serialVersionUID = 610253427197564102L; - public OrBOp(final IValueExpression<? extends IV> x, + /** + * Each operand of this operator must evaluate to a boolean. If the operand + * is not known to evaluate to a boolean, wrap it with an {@link EBVBOp}. + */ + private static final XSDBooleanIVValueExpression wrap( + final IValueExpression<? extends IV> ve) { + + return ve instanceof XSDBooleanIVValueExpression ? + (XSDBooleanIVValueExpression) ve : + new EBVBOp(ve); + + } + + public OrBOp( + final IValueExpression<? extends IV> x, final IValueExpression<? extends IV> y) { - this(new BOp[] { x, y }, null/*annocations*/); + this(new BOp[] { wrap(x), wrap(y) }, null/*annocations*/); } @@ -59,7 +73,13 @@ if (args.length != 2 || args[0] == null || args[1] == null) throw new IllegalArgumentException(); + + if (!(args[0] instanceof XSDBooleanIVValueExpression)) + throw new IllegalArgumentException(); + if (!(args[1] instanceof XSDBooleanIVValueExpression)) + throw new IllegalArgumentException(); + } /** @@ -68,6 +88,11 @@ public OrBOp(final OrBOp op) { super(op); } + + @Override + public IValueExpression<? extends XSDBooleanIV> get(final int i) { + return (IValueExpression<? extends XSDBooleanIV>) super.get(i); + } /** * Follows semantics from SPARQL spec - "Testing Values". Added: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/RegexBOp.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/RegexBOp.java (rev 0) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/RegexBOp.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -0,0 +1,192 @@ +/* + +Copyright (C) SYSTAP, LLC 2006-2007. All rights reserved. + +Contact: + SYSTAP, LLC + 4501 Tower Road + Greensboro, NC 27410 + lic...@bi... + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; version 2 of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ +package com.bigdata.rdf.internal.constraints; + +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; +import java.util.regex.Pattern; + +import org.apache.log4j.Logger; +import org.openrdf.model.Literal; +import org.openrdf.model.Value; +import org.openrdf.query.algebra.evaluation.util.QueryEvaluationUtil; + +import com.bigdata.bop.BOp; +import com.bigdata.bop.IBindingSet; +import com.bigdata.bop.IValueExpression; +import com.bigdata.bop.IVariable; +import com.bigdata.rdf.error.SparqlTypeErrorException; +import com.bigdata.rdf.internal.IV; + +public class RegexBOp extends XSDBooleanIVValueExpression + implements INeedsMaterialization { + + /** + * + */ + private static final long serialVersionUID = 1357420268214930143L; + + private static final transient Logger log = Logger.getLogger(RegexBOp.class); + + + /** + * Construct a regex bop without flags. + */ + public RegexBOp( + final IValueExpression<? extends IV> var, + final IValueExpression<? extends IV> pattern) { + + this(new BOp[] { var, pattern }, null/*annocations*/); + + } + + /** + * Construct a regex bop with flags. + */ + public RegexBOp( + final IValueExpression<? extends IV> var, + final IValueExpression<? extends IV> pattern, + final IValueExpression<? extends IV> flags) { + + this(new BOp[] { var, pattern, flags }, null/*annocations*/); + + } + + /** + * Required shallow copy constructor. + */ + public RegexBOp(final BOp[] args, final Map<String, Object> anns) { + + super(args, anns); + + if (args.length < 2 || args[0] == null || args[1] == null) + throw new IllegalArgumentException(); + + } + + /** + * Required deep copy constructor. + */ + public RegexBOp(final RegexBOp op) { + super(op); + } + + + + private volatile transient Set<IVariable<IV>> terms; + + public Set<IVariable<IV>> getTermsToMaterialize() { + + if (terms == null) { + + terms = new LinkedHashSet<IVariable<IV>>(1); + + if (get(0) instanceof IVariable) + terms.add((IVariable<IV>) get(0)); + + } + + return terms; + + } + + public boolean accept(final IBindingSet bs) { + + final IV var = get(0).get(bs); + final IV pattern = get(1).get(bs); + final IV flags = arity() > 2 ? get(2).get(bs) : null; + + if (log.isDebugEnabled()) { + log.debug("regex var: " + var); + log.debug("regex pattern: " + pattern); + log.debug("regex flags: " + flags); + } + + // not yet bound + if (var == null || pattern == null) + throw new SparqlTypeErrorException(); + + return accept(var.getValue(), pattern.getValue(), + flags != null ? flags.getValue() : null); + + } + + /** + * Lifted directly from Sesame's EvaluationStrategyImpl. + */ + private boolean accept(final Value arg, final Value parg, final Value farg) { + + if (log.isDebugEnabled()) { + log.debug("regex var: " + arg); + log.debug("regex pattern: " + parg); + log.debug("regex flags: " + farg); + } + + if (QueryEvaluationUtil.isSimpleLiteral(arg) && QueryEvaluationUtil.isSimpleLiteral(parg) + && (farg == null || QueryEvaluationUtil.isSimpleLiteral(farg))) + { + String text = ((Literal)arg).getLabel(); + String ptn = ((Literal)parg).getLabel(); + String flags = ""; + if (farg != null) { + flags = ((Literal)farg).getLabel(); + } + // TODO should this Pattern be cached? + int f = 0; + for (char c : flags.toCharArray()) { + switch (c) { + case 's': + f |= Pattern.DOTALL; + break; + case 'm': + f |= Pattern.MULTILINE; + break; + case 'i': + f |= Pattern.CASE_INSENSITIVE; + break; + case 'x': + f |= Pattern.COMMENTS; + break; + case 'd': + f |= Pattern.UNIX_LINES; + break; + case 'u': + f |= Pattern.UNICODE_CASE; + break; + default: + throw new SparqlTypeErrorException(); + } + } + Pattern pattern = Pattern.compile(ptn, f); + boolean result = pattern.matcher(text).find(); + return result; + } + + throw new SparqlTypeErrorException(); + + } + +} Modified: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/SPARQLConstraint.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/SPARQLConstraint.java 2011-05-19 20:03:02 UTC (rev 4528) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/SPARQLConstraint.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -30,7 +30,6 @@ import com.bigdata.bop.BOp; import com.bigdata.bop.IBindingSet; -import com.bigdata.bop.IConstraint; import com.bigdata.bop.IValueExpression; import com.bigdata.rdf.error.SparqlTypeErrorException; import com.bigdata.rdf.internal.IV; @@ -51,19 +50,27 @@ protected static final Logger log = Logger.getLogger(SPARQLConstraint.class); /** - * Convenience method to generate a constraint from a value expression. + * The operand of this operator must evaluate to a boolean. If the operand + * is not known to evaluate to a boolean, wrap it with an {@link EBVBOp}. */ - public static IConstraint wrap(final IValueExpression<? extends IV> ve) { - if (ve instanceof EBVBOp) - return new SPARQLConstraint((EBVBOp) ve); - else - return new SPARQLConstraint(new EBVBOp(ve)); + private static XSDBooleanIVValueExpression wrap( + final IValueExpression<? extends IV> ve) { + + return ve instanceof XSDBooleanIVValueExpression ? + (XSDBooleanIVValueExpression) ve : + new EBVBOp(ve); + } - public SPARQLConstraint(final EBVBOp x) { + /** + * Construct a SPARQL constraint using the specified value expression. + * The value expression will be automatically wrapped inside an + * {@link EBVBOp} if it does not itself evaluate to a boolean. + */ + public SPARQLConstraint(final IValueExpression<? extends IV> x) { - this(new BOp[] { x }, null/*annocations*/); + this(new BOp[] { wrap(x) }, null/*annocations*/); } @@ -72,7 +79,12 @@ */ public SPARQLConstraint(final BOp[] args, final Map<String, Object> anns) { + super(args, anns); + + if (args.length != 1 || args[0] == null) + throw new IllegalArgumentException(); + } /** @@ -83,12 +95,12 @@ } @Override - public EBVBOp get(final int i) { - return (EBVBOp) super.get(i); + public IValueExpression<? extends XSDBooleanIV> get(final int i) { + return (IValueExpression<? extends XSDBooleanIV>) super.get(i); } - public IValueExpression<XSDBooleanIV> getValueExpression() { - return get(0).get(0); + public IValueExpression<? extends XSDBooleanIV> getValueExpression() { + return get(0); } public boolean accept(final IBindingSet bs) { @@ -96,7 +108,9 @@ try { // evaluate the EBV operator - return get(0).get(bs).booleanValue(); + final XSDBooleanIV iv = get(0).get(bs); + + return iv.booleanValue(); } catch (Throwable t) { Modified: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/SameTermBOp.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/SameTermBOp.java 2011-05-19 20:03:02 UTC (rev 4528) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/SameTermBOp.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -31,7 +31,6 @@ import com.bigdata.bop.IValueExpression; import com.bigdata.rdf.error.SparqlTypeErrorException; import com.bigdata.rdf.internal.IV; -import com.bigdata.rdf.internal.XSDBooleanIV; /** * Compare two terms for exact equality. Modified: branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/StrBOp.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/StrBOp.java 2011-05-19 20:03:02 UTC (rev 4528) +++ branches/QUADS_QUERY_BRANCH/bigdata-rdf/src/java/com/bigdata/rdf/internal/constraints/StrBOp.java 2011-05-20 00:12:12 UTC (rev 4529) @@ -24,34 +... [truncated message content] |