[Mulgara-svn] r431 - trunk/src/jar/resolver/java/org/mulgara/resolver
andrae at mulgara.org
andrae at mulgara.org
Thu Sep 13 05:09:29 UTC 2007
Author: andrae
Date: 2007-09-13 00:09:28 -0500 (Thu, 13 Sep 2007)
New Revision: 431
Added:
trunk/src/jar/resolver/java/org/mulgara/resolver/DefaultConstraintHandlers.java
Removed:
trunk/src/jar/resolver/java/org/mulgara/resolver/LocalQuery.java
Modified:
trunk/src/jar/resolver/java/org/mulgara/resolver/ConstraintOperations.java
trunk/src/jar/resolver/java/org/mulgara/resolver/DatabaseOperationContext.java
trunk/src/jar/resolver/java/org/mulgara/resolver/LocalQueryResolver.java
Log:
svn merge -r 422:424 ../branches/mgr-69/
For some reason the previous merge missed revisions 423 and 424 - which delete
LocalQuery and complete the refactorings associated with mgr-69.
Modified: trunk/src/jar/resolver/java/org/mulgara/resolver/ConstraintOperations.java
===================================================================
--- trunk/src/jar/resolver/java/org/mulgara/resolver/ConstraintOperations.java 2007-09-12 05:57:17 UTC (rev 430)
+++ trunk/src/jar/resolver/java/org/mulgara/resolver/ConstraintOperations.java 2007-09-13 05:09:28 UTC (rev 431)
@@ -80,6 +80,10 @@
static Map constraintLocalizations = new HashMap();
static Map constraintModelRewrites = new HashMap();
+ static {
+ DefaultConstraintHandlers.initializeHandlers();
+ }
+
static void addConstraintResolutionHandlers(NVPair[] resolutionHandlers) throws RuntimeException {
addToMap(resolutionHandlers, constraintResolutionHandlers,
ConstraintExpression.class, ConstraintResolutionHandler.class);
Modified: trunk/src/jar/resolver/java/org/mulgara/resolver/DatabaseOperationContext.java
===================================================================
--- trunk/src/jar/resolver/java/org/mulgara/resolver/DatabaseOperationContext.java 2007-09-12 05:57:17 UTC (rev 430)
+++ trunk/src/jar/resolver/java/org/mulgara/resolver/DatabaseOperationContext.java 2007-09-13 05:09:28 UTC (rev 431)
@@ -780,7 +780,7 @@
}
try {
query = transform(query);
- LocalQuery lq = new LocalQuery(systemResolver, this);
+ LocalQueryResolver lq = new LocalQueryResolver(this, systemResolver);
Tuples result = lq.resolveE(query);
query.close();
@@ -832,7 +832,7 @@
query = transform(query);
- LocalQuery localQuery = new LocalQuery(systemResolver, this);
+ LocalQueryResolver localQuery = new LocalQueryResolver(this, systemResolver);
// Complete the numerical phase of resolution
Tuples tuples = localQuery.resolveE(query);
Copied: trunk/src/jar/resolver/java/org/mulgara/resolver/DefaultConstraintHandlers.java (from rev 424, branches/mgr-69/src/jar/resolver/java/org/mulgara/resolver/DefaultConstraintHandlers.java)
Deleted: trunk/src/jar/resolver/java/org/mulgara/resolver/LocalQuery.java
===================================================================
--- trunk/src/jar/resolver/java/org/mulgara/resolver/LocalQuery.java 2007-09-12 05:57:17 UTC (rev 430)
+++ trunk/src/jar/resolver/java/org/mulgara/resolver/LocalQuery.java 2007-09-13 05:09:28 UTC (rev 431)
@@ -1,250 +0,0 @@
-/*
- * The contents of this file are subject to the Mozilla Public License
- * Version 1.1 (the "License"); you may not use this file except in
- * compliance with the License. You may obtain a copy of the License at
- * http://www.mozilla.org/MPL/
- *
- * Software distributed under the License is distributed on an "AS IS"
- * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
- * the License for the specific language governing rights and limitations
- * under the License.
- *
- * The Original Code is the Kowari Metadata Store.
- *
- * The Initial Developer of the Original Code is Plugged In Software Pty
- * Ltd (http://www.pisoftware.com, mailto:info at pisoftware.com). Portions
- * created by Plugged In Software Pty Ltd are Copyright (C) 2001,2002
- * Plugged In Software Pty Ltd. All Rights Reserved.
- *
- * Contributor(s): N/A.
- *
- * [NOTE: The text of this Exhibit A may differ slightly from the text
- * of the notices in the Source Code files of the Original Code. You
- * should use the text of this Exhibit A rather than the text found in the
- * Original Code Source Code for Your Modifications.]
- *
- */
-
-package org.mulgara.resolver;
-
-// Java 2 standard packages
-import java.util.*;
-
-// Third party packages
-import org.apache.log4j.Logger;
-import org.jrdf.graph.Node;
-import org.jrdf.graph.URIReference;
-
-// Local packages
-import org.mulgara.query.*;
-import org.mulgara.query.rdf.BlankNodeImpl;
-import org.mulgara.query.rdf.LiteralImpl;
-import org.mulgara.query.rdf.URIReferenceImpl;
-import org.mulgara.resolver.spi.GlobalizeException;
-import org.mulgara.resolver.spi.LocalizeException;
-import org.mulgara.resolver.spi.LocalizedTuples;
-import org.mulgara.resolver.spi.QueryEvaluationContext;
-import org.mulgara.resolver.spi.ResolverSession;
-import org.mulgara.resolver.spi.SymbolicTransformation;
-import org.mulgara.store.nodepool.NodePool;
-import org.mulgara.store.tuples.RestrictPredicateFactory;
-import org.mulgara.store.tuples.Tuples;
-import org.mulgara.store.tuples.TuplesOperations;
-
-/**
- * Localized version of a global {@link Query}.
- *
- * As well as providing coordinate transformation from global to local
- * coordinates, this adds methods to partially resolve the query.
- *
- * @created 2004-05-06
- * @author <a href="http://www.pisoftware.com/raboczi">Simon Raboczi</a>
- * @version $Revision: 1.13 $
- * @modified $Date: 2005/06/09 09:26:02 $
- * @maintenanceAuthor $Author: raboczi $
- * @company <a href="mailto:info at PIsoftware.com">Plugged In Software</a>
- * @copyright ©2004 <a href="http://www.tucanatech.com/">Tucana
- * Technology, Inc</a>
- * @licence <a href="{@docRoot}/../../LICENCE">Mozilla Public License v1.1</a>
- */
-class LocalQuery implements Cloneable
-{
- /** Logger. */
- private static final Logger logger =
- Logger.getLogger(LocalQuery.class.getName());
-
- /** The current localisation/globalisation session. */
- private final ResolverSession resolverSession;
-
- private LocalQueryResolver context;
-
- /**
- * Construct a database.
- *
- * @param query the query to localize
- * @param resolverSession the database session to localize the
- * <var>query</var> against
- * @throws IllegalArgumentException if <var>query</var> or
- * <var>resolverSession</var> are <code>null</code>
- * @throws LocalizeException if the <var>query</var> can't be localized
- */
- LocalQuery(ResolverSession resolverSession, DatabaseOperationContext context)
- throws LocalizeException, TuplesException
- {
- // Validate "resolverSession" parameter
- if (resolverSession == null) {
- throw new IllegalArgumentException("Null \"resolverSession\" parameter");
- }
-
- // Initialize fields
- this.context = new LocalQueryResolver(context, resolverSession);
- this.resolverSession = resolverSession;
- if (logger.isDebugEnabled()) {
- logger.debug("Constructed local query");
- }
- }
-
- /**
- * @return the solution to this query
- * @throws QueryException if resolution can't be obtained
- */
- Tuples resolveE(Query query) throws QueryException
- {
- if (query == null) {
- throw new IllegalArgumentException("Query null in LocalQuery::resolveE");
- }
-
- try {
- if (logger.isDebugEnabled()) {
- logger.debug("Resolving query " + query);
- }
-
- if (logger.isDebugEnabled()) {
- logger.debug("Stacktrace: ", new Throwable());
- }
-
- Tuples result = ConstraintOperations.resolveConstraintExpression(context,
- query.getModelExpression(), query.getConstraintExpression());
-
- if (logger.isDebugEnabled()) {
- logger.debug("Tuples result = " + TuplesOperations.formatTuplesTree(result));
- }
-
- result = projectSelectClause(query, result);
- result = appendAggregates(query, result);
- result = applyHaving(query, result);
- result = orderResult(query, result);
- result = offsetResult(query, result);
- result = limitResult(query, result);
-
- return result;
- } catch (TuplesException et) {
- throw new QueryException("Failed to resolve query", et);
- }
- }
-
-
- private Tuples projectSelectClause(Query query, Tuples result) throws TuplesException
- {
- if (result.getRowCardinality() > Cursor.ZERO) {
- Tuples tmp = result;
- try {
- List variables = new ArrayList();
-
- /*
- * Note that this code need not concern itself with the order of the select-list,
- * only the contents. The mapping is handled by the subsequent Answer object,
- * and only becomes important if the row-order is important and is therefore
- * deferred to order-by resolution.
- */
- Variable[] vars = result.getVariables();
- for (int i = 0; i < vars.length; i++) {
- if (query.getVariableList().contains(vars[i])) {
- variables.add(vars[i]);
- }
- }
-
- result = TuplesOperations.project(result, variables);
- } finally {
- tmp.close();
- }
- }
-
- return result;
- }
-
-
- private Tuples appendAggregates(Query query, Tuples result) throws TuplesException
- {
- if (result.getRowCardinality() != Tuples.ZERO) {
- Tuples tmp = result;
- result = new AppendAggregateTuples(resolverSession, context, result,
- filterSubqueries(query.getVariableList()));
- tmp.close();
- }
-
- return result;
- }
-
- private List filterSubqueries(List select) {
- List result = new ArrayList();
- for (Object o : select) {
- if (!(o instanceof Subquery)) {
- result.add(o);
- }
- }
-
- return result;
- }
-
-
- private Tuples applyHaving(Query query, Tuples result) throws TuplesException {
- ConstraintHaving having = query.getHavingExpression();
- Tuples tmp = result;
- if (having != null) {
- result = TuplesOperations.restrict(
- result, RestrictPredicateFactory.getPredicate(having, resolverSession));
- tmp.close();
- }
-
- return result;
- }
-
-
- private Tuples orderResult(Query query, Tuples result) throws TuplesException, QueryException {
- List orderList = query.getOrderList();
- if (orderList.size() > 0 && result.getRowCardinality() > Cursor.ONE) {
- Tuples tmp = result;
- result = TuplesOperations.sort(result,
- new OrderByRowComparator(result, orderList, resolverSession));
- tmp.close();
- }
-
- return result;
- }
-
- private Tuples offsetResult(Query query, Tuples result) throws TuplesException
- {
- int offset = query.getOffset();
- if (offset > 0) {
- Tuples tmp = result;
- result = TuplesOperations.offset(result, offset);
- tmp.close();
- }
-
- return result;
- }
-
-
- private Tuples limitResult(Query query, Tuples result) throws TuplesException
- {
- Integer limit = query.getLimit();
- if (limit != null) {
- Tuples tmp = result;
- result = TuplesOperations.limit(result, limit.intValue());
- tmp.close();
- }
-
- return result;
- }
-}
Modified: trunk/src/jar/resolver/java/org/mulgara/resolver/LocalQueryResolver.java
===================================================================
--- trunk/src/jar/resolver/java/org/mulgara/resolver/LocalQueryResolver.java 2007-09-12 05:57:17 UTC (rev 430)
+++ trunk/src/jar/resolver/java/org/mulgara/resolver/LocalQueryResolver.java 2007-09-13 05:09:28 UTC (rev 431)
@@ -97,278 +97,19 @@
private ResolverSession resolverSession;
- //
// Constructor
- //
LocalQueryResolver(DatabaseOperationContext operationContext, ResolverSession resolverSession) {
if (operationContext == null) {
throw new IllegalArgumentException("Null 'operationContext' parameter");
+ } else if (resolverSession == null) {
+ throw new IllegalArgumentException("Null 'resolverSession' parameter");
}
- // Initialize fields
this.operationContext = operationContext;
this.resolverSession = resolverSession;
}
- static {
- ConstraintOperations.addModelResolutionHandlers(new NVPair[]
- {
- new NVPair(ModelUnion.class, new ModelResolutionHandler() {
- public Tuples resolve(QueryEvaluationContext context, ModelExpression modelExpr,
- Constraint constraint) throws Exception {
- Tuples lhs = ConstraintOperations.
- resolveModelExpression(context, ((ModelOperation)modelExpr).getLHS(), constraint);
- Tuples rhs = ConstraintOperations.
- resolveModelExpression(context, ((ModelOperation)modelExpr).getRHS(), constraint);
- try {
- return TuplesOperations.append(lhs, rhs);
- } finally {
- lhs.close();
- rhs.close();
- }
- }
- }),
- new NVPair(ModelIntersection.class, new ModelResolutionHandler() {
- public Tuples resolve(QueryEvaluationContext context, ModelExpression modelExpr,
- Constraint constraint) throws Exception {
- Tuples lhs = ConstraintOperations.
- resolveModelExpression(context, ((ModelOperation)modelExpr).getLHS(), constraint);
- Tuples rhs = ConstraintOperations.
- resolveModelExpression(context, ((ModelOperation)modelExpr).getRHS(), constraint);
- try {
- return TuplesOperations.join(lhs, rhs);
- } finally {
- lhs.close();
- rhs.close();
- }
- }
- }),
- new NVPair(ModelResource.class, new ModelResolutionHandler() {
- public Tuples resolve(QueryEvaluationContext context, ModelExpression modelExpr,
- Constraint constraint) throws Exception {
- return context.resolve((ModelResource)modelExpr, (Constraint)constraint);
- }
- })
- });
- }
-
-
- static {
- ConstraintOperations.addConstraintResolutionHandlers(new NVPair[]
- {
- new NVPair(ConstraintConjunction.class, new ConstraintResolutionHandler() {
- public Tuples resolve(QueryEvaluationContext context, ModelExpression modelExpr, ConstraintExpression constraintExpr) throws Exception {
- List l =
- context.resolveConstraintOperation(modelExpr, (ConstraintOperation)constraintExpr);
- try {
- return TuplesOperations.join(l);
- } finally {
- Iterator i = l.iterator();
- while (i.hasNext()) {
- ((Tuples)i.next()).close();
- }
- }
- }
- }),
- new NVPair(ConstraintDisjunction.class, new ConstraintResolutionHandler() {
- public Tuples resolve(QueryEvaluationContext context, ModelExpression modelExpr, ConstraintExpression constraintExpr) throws Exception {
- List l =
- context.resolveConstraintOperation(modelExpr, (ConstraintOperation)constraintExpr);
- try {
- return TuplesOperations.append(l);
- } finally {
- Iterator i = l.iterator();
- while (i.hasNext()) {
- ((Tuples)i.next()).close();
- }
- }
- }
- }),
- new NVPair(ConstraintDifference.class, new ConstraintResolutionHandler() {
- public Tuples resolve(QueryEvaluationContext context, ModelExpression modelExpr, ConstraintExpression constraintExpr) throws Exception {
- List args = context.resolveConstraintOperation(modelExpr, (ConstraintOperation)constraintExpr);
- assert args.size() == 2;
- try {
- return TuplesOperations.subtract((Tuples)args.get(0), (Tuples)args.get(1));
- } finally {
- ((Tuples)args.get(0)).close();
- ((Tuples)args.get(1)).close();
- }
- }
- }),
- new NVPair(ConstraintIs.class, new ConstraintResolutionHandler() {
- public Tuples resolve(QueryEvaluationContext context, ModelExpression modelExpr, ConstraintExpression constraintExpr) throws Exception {
- ConstraintIs constraint = (ConstraintIs)constraintExpr;
- return TuplesOperations.assign((Variable)context.localize(constraint.getVariable()),
- ((LocalNode)context.localize(constraint.getValueNode())).getValue());
- }
- }),
- new NVPair(ConstraintImpl.class, new ConstraintResolutionHandler() {
- public Tuples resolve(QueryEvaluationContext context, ModelExpression modelExpr, ConstraintExpression constraintExpr) throws Exception {
- ConstraintElement constraintElem =
- ((ConstraintImpl) constraintExpr).getModel();
- assert constraintElem != null;
- if (constraintElem.equals(Variable.FROM)) {
- return ConstraintOperations.resolveModelExpression(context, modelExpr, (Constraint)constraintExpr);
- }
- else if (constraintElem instanceof URIReference) {
- return ConstraintOperations.resolveModelExpression(context, new ModelResource(((URIReference)constraintElem).getURI()), (Constraint)constraintExpr);
- }
- else if (constraintElem instanceof LocalNode) {
- return context.resolve(null, (ConstraintImpl)constraintExpr);
- }
- else if (constraintElem instanceof Variable) {
- return context.resolve(null, (ConstraintImpl)constraintExpr);
- }
- else {
- throw new QueryException("Specified model not a URIReference/Variable: " + constraintElem +" is a " + constraintElem.getClass().getName() );
- }
- }
- }),
- new NVPair(ConstraintNegation.class, new ConstraintResolutionHandler() {
- public Tuples resolve(QueryEvaluationContext context, ModelExpression modelExpr, ConstraintExpression constraintExpr) throws Exception {
- if (((ConstraintNegation)constraintExpr).getModel().equals(Variable.FROM)) {
- return ConstraintOperations.resolveModelExpression(context, modelExpr, (Constraint)constraintExpr);
- } else {
- ConstraintElement constraintElem = ((ConstraintNegation)constraintExpr).getElement(3);
- if (constraintElem instanceof URIReference) {
- return ConstraintOperations.resolveModelExpression(context, new ModelResource(((URIReference)constraintElem).getURI()), (Constraint)constraintExpr);
- } else {
- throw new QueryException("Specified model not a URIReference: " + constraintElem +" is a " + constraintElem.getClass().getName() );
- }
- }
- }
- }),
- new NVPair(WalkConstraint.class, new ConstraintResolutionHandler() {
- public Tuples resolve(QueryEvaluationContext context, ModelExpression modelExpr, ConstraintExpression constraintExpr) throws Exception {
- return WalkFunction.walk(context, (WalkConstraint)constraintExpr, modelExpr, context.getResolverSession());
- }
- }),
- new NVPair(SingleTransitiveConstraint.class, new ConstraintResolutionHandler() {
- public Tuples resolve(QueryEvaluationContext context, ModelExpression modelExpr, ConstraintExpression constraintExpr) throws Exception {
- SingleTransitiveConstraint constraint = (SingleTransitiveConstraint)constraintExpr;
- if (constraint.isAnchored()) {
- return DirectTransitiveFunction.infer(context, constraint, modelExpr, context.getResolverSession());
- } else {
- return ExhaustiveTransitiveFunction.infer(context, constraint, modelExpr, context.getResolverSession());
- }
- }
- }),
- new NVPair(TransitiveConstraint.class, new ConstraintResolutionHandler() {
- public Tuples resolve(QueryEvaluationContext context, ModelExpression modelExpr, ConstraintExpression constraintExpr) throws Exception {
- return ExhaustiveTransitiveFunction.infer(context, (TransitiveConstraint)constraintExpr, modelExpr, context.getResolverSession());
- }
- }),
- });
- }
-
- static {
- ConstraintOperations.addConstraintBindingHandlers(new NVPair[]
- {
- new NVPair(ConstraintTrue.class, new ConstraintBindingHandler() {
- public ConstraintExpression bindVariables(Map bindings, ConstraintExpression constraintExpr) throws Exception {
- return constraintExpr;
- }
- }),
- new NVPair(ConstraintFalse.class, new ConstraintBindingHandler() {
- public ConstraintExpression bindVariables(Map bindings, ConstraintExpression constraintExpr) throws Exception {
- return constraintExpr;
- }
- }),
- new NVPair(ConstraintImpl.class, new ConstraintBindingHandler() {
- public ConstraintExpression bindVariables(Map bindings, ConstraintExpression constraintExpr) throws Exception {
- return ConstraintOperations.replace(bindings, (Constraint)constraintExpr);
- }
- }),
- new NVPair(ConstraintIs.class, new ConstraintBindingHandler() {
- public ConstraintExpression bindVariables(Map bindings, ConstraintExpression constraintExpr) throws Exception {
- return ConstraintOperations.replace(bindings, (Constraint)constraintExpr);
- }
- }),
- new NVPair(SingleTransitiveConstraint.class, new ConstraintBindingHandler() {
- public ConstraintExpression bindVariables(Map bindings, ConstraintExpression constraintExpr) throws Exception {
- return new SingleTransitiveConstraint(ConstraintOperations.replace(bindings, (Constraint)constraintExpr));
- }
- }),
- new NVPair(TransitiveConstraint.class, new ConstraintBindingHandler() {
- public ConstraintExpression bindVariables(Map bindings, ConstraintExpression constraintExpr) throws Exception {
- TransitiveConstraint tc = (TransitiveConstraint)constraintExpr;
- return new TransitiveConstraint(ConstraintOperations.replace(bindings, tc.getAnchoredConstraint()),
- ConstraintOperations.replace(bindings, tc.getUnanchoredConstraint()));
- }
- }),
- new NVPair(WalkConstraint.class, new ConstraintBindingHandler() {
- public ConstraintExpression bindVariables(Map bindings, ConstraintExpression constraintExpr) throws Exception {
- WalkConstraint wc = (WalkConstraint)constraintExpr;
- return new WalkConstraint(ConstraintOperations.replace(bindings, wc.getAnchoredConstraint()),
- ConstraintOperations.replace(bindings, wc.getUnanchoredConstraint()));
- }
- }),
- new NVPair(ConstraintNegation.class, new ConstraintBindingHandler() {
- public ConstraintExpression bindVariables(Map bindings, ConstraintExpression constraintExpr) throws Exception {
- return new ConstraintNegation(ConstraintOperations.replace(bindings, (Constraint)constraintExpr));
- }
- }),
- new NVPair(ConstraintConjunction.class, new ConstraintBindingHandler() {
- public ConstraintExpression bindVariables(Map bindings, ConstraintExpression constraintExpr) throws Exception {
- return new ConstraintConjunction(ConstraintOperations.replaceOperationArgs(bindings, (ConstraintOperation)constraintExpr));
- }
- }),
- new NVPair(ConstraintDisjunction.class, new ConstraintBindingHandler() {
- public ConstraintExpression bindVariables(Map bindings, ConstraintExpression constraintExpr) throws Exception {
- return new ConstraintDisjunction(ConstraintOperations.replaceOperationArgs(bindings, (ConstraintOperation)constraintExpr));
- }
- }),
- new NVPair(ConstraintDifference.class, new ConstraintBindingHandler() {
- public ConstraintExpression bindVariables(Map bindings, ConstraintExpression constraintExpr) throws Exception {
- List args = ConstraintOperations.replaceOperationArgs(bindings, (ConstraintOperation)constraintExpr);
- return new ConstraintDifference((ConstraintExpression)args.get(0), (ConstraintExpression)args.get(1));
- }
- }),
- });
- }
-
-
- static {
- ConstraintOperations.addConstraintModelRewrites(new NVPair[]
- {
- new NVPair(ConstraintImpl.class, new ConstraintModelRewrite() {
- public Constraint rewrite(ConstraintElement newModel, Constraint constraint) throws Exception {
- return new ConstraintImpl(constraint.getElement(0), constraint.getElement(1), constraint.getElement(2), newModel);
- }
- }),
- new NVPair(ConstraintNegation.class, new ConstraintModelRewrite() {
- public Constraint rewrite(ConstraintElement newModel, Constraint constraint) throws Exception {
- return new ConstraintNegation(new ConstraintImpl(constraint.getElement(0), constraint.getElement(1), constraint.getElement(2), newModel));
- }
- }),
- });
- }
-
- static {
- ConstraintOperations.addConstraintLocalizations(new NVPair[]
- {
- new NVPair(ConstraintImpl.class, new ConstraintLocalization() {
- public Constraint localize(QueryEvaluationContext context, Constraint constraint) throws Exception {
- return new ConstraintImpl(context.localize(constraint.getElement(0)),
- context.localize(constraint.getElement(1)),
- context.localize(constraint.getElement(2)),
- context.localize(constraint.getElement(3)));
- }
- }),
- new NVPair(ConstraintNegation.class, new ConstraintLocalization() {
- public Constraint localize(QueryEvaluationContext context, Constraint constraint) throws Exception {
- return new ConstraintNegation(new ConstraintImpl(
- context.localize(constraint.getElement(0)),
- context.localize(constraint.getElement(1)),
- context.localize(constraint.getElement(2)),
- context.localize(constraint.getElement(3))));
- }
- }),
- });
- }
-
public List resolveConstraintOperation(ModelExpression modelExpr,
ConstraintOperation constraintOper)
throws QueryException
@@ -512,4 +253,148 @@
return new ConstraintConjunction(args);
}
+
+ /**
+ * @return the solution to this query
+ * @throws QueryException if resolution can't be obtained
+ */
+ Tuples resolveE(Query query) throws QueryException
+ {
+ if (query == null) {
+ throw new IllegalArgumentException("Query null in LocalQuery::resolveE");
+ }
+
+ try {
+ if (logger.isDebugEnabled()) {
+ logger.debug("Resolving query " + query);
+ }
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("Stacktrace: ", new Throwable());
+ }
+
+ Tuples result = ConstraintOperations.resolveConstraintExpression(this,
+ query.getModelExpression(), query.getConstraintExpression());
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("Tuples result = " + TuplesOperations.formatTuplesTree(result));
+ }
+
+ result = projectSelectClause(query, result);
+ result = appendAggregates(query, result);
+ result = applyHaving(query, result);
+ result = orderResult(query, result);
+ result = offsetResult(query, result);
+ result = limitResult(query, result);
+
+ return result;
+ } catch (TuplesException et) {
+ throw new QueryException("Failed to resolve query", et);
+ }
+ }
+
+
+ private Tuples projectSelectClause(Query query, Tuples result) throws TuplesException
+ {
+ if (result.getRowCardinality() > Cursor.ZERO) {
+ Tuples tmp = result;
+ try {
+ List variables = new ArrayList();
+
+ /*
+ * Note that this code need not concern itself with the order of the select-list,
+ * only the contents. The mapping is handled by the subsequent Answer object,
+ * and only becomes important if the row-order is important and is therefore
+ * deferred to order-by resolution.
+ */
+ Variable[] vars = result.getVariables();
+ for (int i = 0; i < vars.length; i++) {
+ if (query.getVariableList().contains(vars[i])) {
+ variables.add(vars[i]);
+ }
+ }
+
+ result = TuplesOperations.project(result, variables);
+ } finally {
+ tmp.close();
+ }
+ }
+
+ return result;
+ }
+
+
+ private Tuples appendAggregates(Query query, Tuples result) throws TuplesException
+ {
+ if (result.getRowCardinality() != Tuples.ZERO) {
+ Tuples tmp = result;
+ result = new AppendAggregateTuples(resolverSession, this, result,
+ filterSubqueries(query.getVariableList()));
+ tmp.close();
+ }
+
+ return result;
+ }
+
+ private List filterSubqueries(List select) {
+ List result = new ArrayList();
+ for (Object o : select) {
+ if (!(o instanceof Subquery)) {
+ result.add(o);
+ }
+ }
+
+ return result;
+ }
+
+
+ private Tuples applyHaving(Query query, Tuples result) throws TuplesException {
+ ConstraintHaving having = query.getHavingExpression();
+ Tuples tmp = result;
+ if (having != null) {
+ result = TuplesOperations.restrict(
+ result, RestrictPredicateFactory.getPredicate(having, resolverSession));
+ tmp.close();
+ }
+
+ return result;
+ }
+
+
+ private Tuples orderResult(Query query, Tuples result) throws TuplesException, QueryException {
+ List orderList = query.getOrderList();
+ if (orderList.size() > 0 && result.getRowCardinality() > Cursor.ONE) {
+ Tuples tmp = result;
+ result = TuplesOperations.sort(result,
+ new OrderByRowComparator(result, orderList, resolverSession));
+ tmp.close();
+ }
+
+ return result;
+ }
+
+ private Tuples offsetResult(Query query, Tuples result) throws TuplesException
+ {
+ int offset = query.getOffset();
+ if (offset > 0) {
+ Tuples tmp = result;
+ result = TuplesOperations.offset(result, offset);
+ tmp.close();
+ }
+
+ return result;
+ }
+
+
+ private Tuples limitResult(Query query, Tuples result) throws TuplesException
+ {
+ Integer limit = query.getLimit();
+ if (limit != null) {
+ Tuples tmp = result;
+ result = TuplesOperations.limit(result, limit.intValue());
+ tmp.close();
+ }
+
+ return result;
+ }
}
More information about the Mulgara-svn
mailing list