From 4e42cc9375d3d9b86a4ab1ed94f7d9f573a5a12f Mon Sep 17 00:00:00 2001 From: Michael Grove Date: Mon, 20 Feb 2012 11:53:23 -0500 Subject: [PATCH] fixing a bug w/ how we're doing the setting of limit & offset directly in a query model --- .../openrdf/query/SesameQueryUtils.java | 90 +++++++++++++++++-- 1 file changed, 82 insertions(+), 8 deletions(-) diff --git a/core/src/com/clarkparsia/openrdf/query/SesameQueryUtils.java b/core/src/com/clarkparsia/openrdf/query/SesameQueryUtils.java index c9ff9b0..29c5ff8 100644 --- a/core/src/com/clarkparsia/openrdf/query/SesameQueryUtils.java +++ b/core/src/com/clarkparsia/openrdf/query/SesameQueryUtils.java @@ -59,6 +59,9 @@ import org.openrdf.query.algebra.ProjectionElem; import org.openrdf.query.algebra.Projection; import org.openrdf.query.algebra.MultiProjection; +import org.openrdf.query.algebra.Reduced; +import org.openrdf.query.algebra.QueryModelVisitor; +import org.openrdf.query.algebra.QueryModelNode; import org.openrdf.query.MalformedQueryException; import org.openrdf.query.GraphQueryResult; import org.openrdf.query.QueryEvaluationException; @@ -293,7 +296,7 @@ public static String escape(String theString) { */ public static void setLimit(final ParsedQuery theQuery, final int theLimit) { try { - SliceMutator aLimitSetter = new SliceMutator(theLimit); + SliceMutator aLimitSetter = SliceMutator.changeLimit(theLimit); theQuery.getTupleExpr().visit(aLimitSetter); if (!aLimitSetter.limitWasSet()) { @@ -317,14 +320,13 @@ public static void setLimit(final ParsedQuery theQuery, final int theLimit) { */ public static void setOffset(final ParsedQuery theQuery, final int theOffset) { try { - SliceMutator aLimitSetter = new SliceMutator(theOffset); + SliceMutator aLimitSetter = SliceMutator.changeOffset(theOffset); theQuery.getTupleExpr().visit(aLimitSetter); if (!aLimitSetter.offsetWasSet()) { - Slice aSlice = new Slice(); + Slice aSlice = new Slice(theQuery.getTupleExpr()); aSlice.setOffset(theOffset); - aSlice.setArg(theQuery.getTupleExpr()); theQuery.setTupleExpr(aSlice); } @@ -334,6 +336,18 @@ public static void setOffset(final ParsedQuery theQuery, final int theOffset) { } } +// public static void setDistinct(final ParsedQuery theQuery, final boolean theDistinct) throws Exception { +// } +// +// public static void setReduced(final ParsedQuery theQuery, final boolean theReduced) throws Exception { +// if (!theReduced) { +// theQuery.getTupleExpr().visit(new RemoveUnaryVisitor(Reduced.class)); +// } +// else if (!contains(theQuery, Reduced.class)) { +// +// } +// } + /** * Close the iteration, ignoring any thrown exception and instead just logging it. * @param theResults the iteration to close @@ -350,6 +364,52 @@ public static void closeQuietly(final CloseableIteration } } + private static class RemoveUnaryVisitor extends QueryModelVisitorBase { + private final Class mClass; + + public RemoveUnaryVisitor(final Class theClass) { + mClass = theClass; + } + + @Override + protected void meetUnaryTupleOperator(final UnaryTupleOperator theUnaryTupleOperator) throws Exception { + if (mClass.equals(theUnaryTupleOperator.getClass())) { + theUnaryTupleOperator.replaceWith(theUnaryTupleOperator.getArg()); + } + else { + super.meetUnaryTupleOperator(theUnaryTupleOperator); + } + } + } + + private static class ContainsVisitor extends QueryModelVisitorBase { + private boolean mContains = false; + + private final Class mClass; + + private ContainsVisitor(final Class theClass) { + mClass = theClass; + } + + public boolean isContains() { + return mContains; + } + + /** + * @inheritDoc + */ + @Override + protected void meetNode(final QueryModelNode theQueryModelNode) throws Exception { + if (mClass.equals(theQueryModelNode.getClass())) { + mContains = true; + } + else { + super.meetNode(theQueryModelNode); + } + } + } + + /** * Implementation of a {@link org.openrdf.query.algebra.QueryModelVisitor} which will set the limit or offset of a query * object to the provided value. If there is no slice operator specified, {@link #limitWasSet} and {@link #offsetWasSet} will return false. @@ -368,21 +428,35 @@ private static class SliceMutator extends QueryModelVisitorBase { /** * The new limit for the query */ - private int mNewLimit; + private final int mNewLimit; /** * The new offset for the query */ - private int mNewOffset; + private final int mNewOffset; /** * Create a new SetLimit object - * @param theNewLimit the new limit to use for the query + * @param theNewLimit the new limit to use for the query, or -1 to not set + * @param theNewOffset the new offset to use for the query, or -1 to not set */ - private SliceMutator(final int theNewLimit) { + private SliceMutator(final int theNewLimit, final int theNewOffset) { mNewLimit = theNewLimit; + mNewOffset = theNewOffset; } + static SliceMutator changeLimit(final int theNewLimit) { + return new SliceMutator(theNewLimit, -1); + } + + static SliceMutator changeOffset(final int theNewOffset) { + return new SliceMutator(-1, theNewOffset); + } + + static SliceMutator changeLimitAndOffset(final int theNewLimit, final int theNewOffset) { + return new SliceMutator(theNewLimit, theNewOffset); + } + /** * Resets the state of this visitor so it can be re-used. */