[Mulgara-svn] r377 - branches/nw-interface/src/jar/query/java/org/mulgara/query
pag at mulgara.org
pag at mulgara.org
Fri Aug 24 17:19:04 UTC 2007
Author: pag
Date: 2007-08-24 12:19:04 -0500 (Fri, 24 Aug 2007)
New Revision: 377
Modified:
branches/nw-interface/src/jar/query/java/org/mulgara/query/Query.java
branches/nw-interface/src/jar/query/java/org/mulgara/query/QueryUnitTest.java
Log:
Implemented Query.execute() and refactored for formatting and generics
Modified: branches/nw-interface/src/jar/query/java/org/mulgara/query/Query.java
===================================================================
--- branches/nw-interface/src/jar/query/java/org/mulgara/query/Query.java 2007-08-24 16:07:49 UTC (rev 376)
+++ branches/nw-interface/src/jar/query/java/org/mulgara/query/Query.java 2007-08-24 17:19:04 UTC (rev 377)
@@ -45,12 +45,6 @@
*
* @author <a href="http://staff.pisoftware.com/raboczi">Simon Raboczi</a>
*
- * @version $Revision: 1.8 $
- *
- * @modified $Date: 2005/01/05 04:58:20 $
- *
- * @maintenanceAuthor $Author: newmana $
- *
* @company <A href="mailto:info at PIsoftware.com">Plugged In Software</A>
*
* @copyright © 2001-2003 <A href="http://www.PIsoftware.com/">Plugged In
@@ -65,13 +59,10 @@
* have not occurred with the class.
* NOTE : update this serialVersionUID when a method or a public member is
* deleted.
- * NOTE: Incremented UID to reflect removal of relatedTo.
*/
- static final long serialVersionUID = 7973523792022156621L;
+ private static final long serialVersionUID = 5165341858790210479L;
- /**
- * Logger.
- */
+ /** Logger. */
private static Logger logger = Logger.getLogger(Query.class.getName());
/**
@@ -80,29 +71,21 @@
* that there is no <code>select</code> clause and that no projection will be
* performed.
*/
- private List variableList;
+ private List<Object> variableList;
/**
* Mutable version of the variable list. This isn't exposed via {@link
* #getVariableList} the way {@link #variableList} is.
*/
- private List mutableVariableList;
+ private List<Object> mutableVariableList;
- /**
- * The model expression.
- *
- * It corresponds to the <code>from</code> clause.
- */
+ /** The model expression. It corresponds to the <code>from</code> clause. */
private ModelExpression modelExpression;
- /**
- * The constraint expression. It corresponds to the <code>where</code> clause.
- */
+ /** The constraint expression. It corresponds to the <code>where</code> clause. */
private ConstraintExpression constraintExpression;
- /**
- * The having expression. It corresponds to the <code>having</code> clause.
- */
+ /** The having expression. It corresponds to the <code>having</code> clause. */
private ConstraintHaving havingConstraint;
/**
@@ -110,7 +93,7 @@
* major orderings preceding minor orderings. It's only sensible for this to
* contain orders on variables in the {@link #variableList}.
*/
- private List orderList;
+ private List<Order> orderList;
/**
* The limit on rows in the result. If this is <code>null</code>, it indicates
@@ -118,17 +101,15 @@
*/
private Integer limit;
- /**
- * The offset on rows in the result. This value is never negative.
- */
+ /** The offset on rows in the result. This value is never negative. */
private int offset;
- /**
- * The accumulated solutions. This can be <code>null</code>, indicating no
- * solutions.
- */
+ /** The accumulated solutions. This can be <code>null</code>, indicating no solutions. */
private Answer answer;
+ /** A UI message describing the result of this message. */
+ private String resultMessage = "";
+
//
// Constructors
//
@@ -138,15 +119,16 @@
*
* @param variableList {@link Variable}s or node values to appear as bindings
* in the solution (i.e. columns of the result {@link Answer});
- * <code>null</code> indicates that all columns are to be retained
+ * <code>null</code> indicates that all columns are to be retained.
+ * This is a list of: Variable; ConstantValue; Count; Subquery.
* @param modelExpression an expression defining the model to query, never
* <code>null</code>
* @param constraintExpression an expression defining the constraints to
* satisfy, never <code>null</code>
* @param havingExpression an expression defining the conditions to apply to
* aggregate functions or null if not given.
- * @param orderList sort order column names, currently a list of {@link
- * Variable}s with the order assumed to be ascending in all cases
+ * @param orderList sort order column names. This is a list of {@link Order}s
+ * which is a wrapper around Variable, with an "ascending" flag.
* @param limit the maximum number of rows to return, which must be
* non-negative; <code>null</code> indicates no limit
* @param offset the number of rows to skip from the beginning of the result,
@@ -159,9 +141,9 @@
* <var>constraintExpression</var>, <var>orderList<var> or
* <var>answer</var> are <code>null</code>
*/
- public Query(List variableList, ModelExpression modelExpression,
+ public Query(List<Object> variableList, ModelExpression modelExpression,
ConstraintExpression constraintExpression,
- ConstraintHaving havingExpression, List orderList, Integer limit,
+ ConstraintHaving havingExpression, List<Order> orderList, Integer limit,
int offset, Answer answer) {
// Validate parameters
@@ -178,14 +160,12 @@
} else if (answer == null) {
throw new IllegalArgumentException("Null \"answer\" parameter");
} else if (variableList != null) {
- Set variableSet = new HashSet(constraintExpression.getVariables());
+ Set<Variable> variableSet = new HashSet<Variable>(constraintExpression.getVariables());
variableSet.addAll(Arrays.asList(answer.getVariables()));
- Iterator i = variableList.iterator();
- while (i.hasNext()) {
- Object o = i.next();
+ for (Object o: variableList) {
if (o instanceof Variable) {
- Variable var = (Variable) o;
+ Variable var = (Variable)o;
if (!variableSet.contains(var)) {
logger.warn("Failed to find " + var + " in " + variableSet);
throw new IllegalArgumentException("Failed to constrain all variables: " + var +
@@ -196,20 +176,18 @@
}
// Initialize fields
- this.mutableVariableList =
- (variableList == null) ? null : new ArrayList(variableList);
- this.variableList =
- (variableList == null) ? null
- : Collections.unmodifiableList(mutableVariableList);
+ this.mutableVariableList = (variableList == null) ? null : new ArrayList<Object>(variableList);
+ this.variableList = (variableList == null) ? null : Collections.unmodifiableList(mutableVariableList);
this.modelExpression = modelExpression;
this.constraintExpression = constraintExpression;
this.havingConstraint = havingExpression;
- this.orderList = Collections.unmodifiableList(new ArrayList(orderList));
+ this.orderList = Collections.unmodifiableList(new ArrayList<Order>(orderList));
this.limit = limit;
this.offset = offset;
this.answer = answer;
}
+
/**
* Cloning must always be supported.
*/
@@ -224,13 +202,10 @@
}
// Copy mutable fields by value
- cloned.mutableVariableList =
- (variableList == null) ? null : new ArrayList(variableList);
- cloned.variableList =
- (variableList == null) ? null
- : Collections.unmodifiableList(cloned.mutableVariableList);
+ cloned.mutableVariableList = (variableList == null) ? null : new ArrayList<Object>(variableList);
+ cloned.variableList = (variableList == null) ? null : Collections.unmodifiableList(cloned.mutableVariableList);
cloned.modelExpression = modelExpression; // FIXME: should be cloned
- cloned.answer = (Answer) answer.clone();
+ cloned.answer = (Answer)answer.clone();
// Copy immutable fields by reference
cloned.orderList = orderList;
@@ -247,15 +222,16 @@
/**
* Accessor for the <code>variableList</code> property.
*
- * @return a {@link List} containing one or more {@link Variable}s
+ * @return a {@link List} containing one or more {@link Variable}s, {@link ContantValue}s,
+ * {@link Count}s or {@link Subquery}
*/
- public List getVariableList() {
+ public List<Object> getVariableList() {
return variableList;
}
+
/**
* Accessor for the <code>constraintExpression</code> property.
- *
* @return a {@link ConstraintExpression}
*/
public ConstraintExpression getConstraintExpression() {
@@ -263,8 +239,7 @@
}
/**
- * Accesor for the <code>havingExpression</code> property.
- *
+ * Accessor for the <code>havingExpression</code> property.
* @return a {@link ConstraintExpression} containing only
* {@link ConstraintHaving} or <code>null</code> to indicate an empty
* having clause.
@@ -273,149 +248,112 @@
return havingConstraint;
}
+
/**
* Accessor for the <code>modelExpression</code> property.
- *
- * @return a {@link ModelExpression}, or <code>null</code> to indicate the
- * empty model
+ * @return a {@link ModelExpression}, or <code>null</code> to indicate the empty model
*/
public ModelExpression getModelExpression() {
return modelExpression;
}
+
/**
* Accessor for the <code>orderList</code> property.
- *
- * @return a {@link List} containing one or more {@link Variable}s
+ * @return a {@link List} containing one or more {@link Order}s
+ * (which wrap {@link Variable}s)
*/
- public List getOrderList() {
+ public List<Order> getOrderList() {
return orderList;
}
+
/**
* Accessor for the <code>limit</code> property.
- *
* @return the limit for this query, or <code>null</code> if unlimited
*/
public Integer getLimit() {
return limit;
}
+
/**
* Accessor for the <code>offset</code> property.
- *
* @return the offset for this query, a non-negative integer
*/
public int getOffset() {
return offset;
}
+
/**
* Accessor for the <code>answer</code> property. If the <var>
* constraintExpression</var> property is <code>null</code>, this is the
* answer to the entire query.
- *
- * @return an {@link Answer}, or <code>null</code> to indicate the set of all
- * statements
+ * @return an {@link Answer}, or <code>null</code> to indicate the set of all statements
*/
public Answer getGiven() {
return answer;
}
+
//
// Methods overriding Object
//
/**
* Equality is by value.
- *
- * @param object PARAMETER TO DO
- * @return RETURNED VALUE TO DO
+ * @param object The object to compare to
+ * @return <code>true</code> if object is functionaly equivalent to this object.
*/
public boolean equals(Object object) {
- if (object == this) {
+ if (object == this) return true;
+ if (object == null) return false;
+ if (!(object instanceof Query)) return false;
- return true;
- }
+ Query query = (Query)object;
- if (object == null) {
-
- return false;
- }
-
- if (! (object instanceof Query)) {
-
- return false;
- }
-
- Query query = (Query) object;
-
// Check the variableList field
- if (!variableList.equals(query.variableList)) {
+ if (!variableList.equals(query.variableList)) return false;
- return false;
- }
-
// Check the modelExpression field
- if ( (modelExpression == null) ? (query.modelExpression != null)
- : (!modelExpression.equals(
- query.modelExpression))) {
-
+ if ((modelExpression == null) ?
+ (query.modelExpression != null) :
+ (!modelExpression.equals(query.modelExpression))) {
return false;
}
// Check the constraintExpression field
- if ((constraintExpression == null) ? (query.constraintExpression != null)
- : (!constraintExpression.equals(query.constraintExpression))) {
-
+ if ((constraintExpression == null) ?
+ (query.constraintExpression != null) :
+ (!constraintExpression.equals(query.constraintExpression))) {
return false;
}
- if ((havingConstraint == null) ? (query.havingConstraint != null)
- : (!havingConstraint.equals(query.havingConstraint))) {
-
+ if ((havingConstraint == null) ?
+ (query.havingConstraint != null) :
+ (!havingConstraint.equals(query.havingConstraint))) {
return false;
}
// Check the orderList field
- if ( (orderList == null) ^ (query.orderList == null)) {
+ if ((orderList == null) ^ (query.orderList == null)) return false;
- return false;
- }
+ if ((orderList != null) && !orderList.equals(query.orderList)) return false;
- if ( (orderList != null) && !orderList.equals(query.orderList)) {
-
- return false;
- }
-
// Check the limit field
- if ( (limit == null) ^ (query.limit == null)) {
+ if ((limit == null) ^ (query.limit == null)) return false;
+ if ((limit != null) && !limit.equals(query.limit)) return false;
- return false;
- }
-
- if ( (limit != null) && !limit.equals(query.limit)) {
-
- return false;
- }
-
// Check the offset field
- if (offset != query.offset) {
+ if (offset != query.offset) return false;
- return false;
- }
-
- // Check the answer field
- if (!answer.equals(query.answer)) {
-
- return false;
- }
-
- // All checks passed, so the object is equal
- return true;
+ // Finally, it comes down to the answer field
+ return answer.equals(query.answer);
}
+
/**
* Close this {@link Query}, and the underlying {@link Answer} objects.
*/
@@ -424,34 +362,25 @@
answer = null;
if (mutableVariableList != null) {
- Iterator it = mutableVariableList.iterator();
- while (it.hasNext()) {
- Object v = it.next();
- if (v instanceof AggregateFunction)
- ((AggregateFunction)v).getQuery().close();
+ for (Object v: mutableVariableList) {
+ if (v instanceof AggregateFunction) ((AggregateFunction)v).getQuery().close();
}
}
}
+
/**
* Generate a legible representation of the query.
*
- * @return RETURNED VALUE TO DO
+ * @return A string representing all the elements of a query.
*/
public String toString() {
-
StringBuffer buffer = new StringBuffer();
// SELECT
if (variableList != null) {
-
buffer.append("SELECT");
-
- for (Iterator i = variableList.iterator(); i.hasNext(); ) {
-
- buffer.append(" ").append(i.next());
- }
-
+ for (Object i: variableList) buffer.append(" ").append(i);
buffer.append(" ");
}
@@ -462,46 +391,29 @@
buffer.append(" WHERE ").append(constraintExpression);
// HAVING
- if (havingConstraint != null) {
- buffer.append(" HAVING ").append(havingConstraint);
- }
+ if (havingConstraint != null) buffer.append(" HAVING ").append(havingConstraint);
// ORDER BY
if (!orderList.isEmpty()) {
-
buffer.append(" ORDER BY");
-
- for (Iterator i = orderList.iterator(); i.hasNext(); ) {
-
- buffer.append(" ").append(i.next());
- }
+ for (Order o: orderList) buffer.append(" ").append(o);
}
// LIMIT
- if (limit != null) {
+ if (limit != null) buffer.append(" LIMIT ").append(limit.intValue());
- buffer.append(" LIMIT ").append(limit.intValue());
- }
-
// OFFSET
- if (offset != 0) {
+ if (offset != 0) buffer.append(" OFFSET ").append(offset);
- buffer.append(" OFFSET ").append(offset);
- }
-
// GIVEN
- if (answer != null) {
+ if (answer != null) buffer.append(" GIVEN ").append(answer);
- buffer.append(" GIVEN ").append(answer);
- }
-
return buffer.toString();
}
/**
* Serializes the current object to a stream.
- *
* @param out The stream to write to.
* @throws IOException If an I/O error occurs while writing.
*/
@@ -520,6 +432,11 @@
out.defaultWriteObject();
}
+
+ //
+ // Command interface methods
+ //
+
/**
* Operation can only be run by a server.
* @return <code>false</code> as this is AST for a server.
@@ -528,6 +445,7 @@
return false;
}
+
/**
* Operation is not restricted to a user interface.
* @return <code>false</code> as this operation has no effect on a UI.
@@ -536,6 +454,7 @@
return false;
}
+
/**
* Gets the associated server for a non-local operation.
* @return the server URI, or <code>null</code> if the data should be found locally.
@@ -545,8 +464,31 @@
return dbURIs.isEmpty() ? null : dbURIs.iterator().next();
}
- public Object execute(Connection conn) throws Exception {
- // TODO Auto-generated method stub
- return null;
+
+ /**
+ * Gets a message text relevant to the operation. Useful for the UI.
+ * Consider changing this to a serialization of the result.
+ * @return A text message associated with the result of this operation.
+ */
+ public String getResultMessage() {
+ return resultMessage;
}
+
+
+ /**
+ * Executes this query on a connection.
+ * @param conn The connection to a database session to execute the query against.
+ * @return The answer to this query.
+ */
+ public Object execute(Connection conn) throws QueryException, TuplesException {
+ if (logger.isDebugEnabled()) logger.debug("Executing query " + toString());
+ Answer answer = conn.getSession().query(this);
+ if (answer == null) throw new QueryException("Invalid answer received");
+ resultMessage = "Successfully executed query";
+ if (logger.isDebugEnabled()) logger.debug(resultMessage);
+ // move to the first row
+ answer.beforeFirst();
+ return answer;
+ }
+
}
Modified: branches/nw-interface/src/jar/query/java/org/mulgara/query/QueryUnitTest.java
===================================================================
--- branches/nw-interface/src/jar/query/java/org/mulgara/query/QueryUnitTest.java 2007-08-24 16:07:49 UTC (rev 376)
+++ branches/nw-interface/src/jar/query/java/org/mulgara/query/QueryUnitTest.java 2007-08-24 17:19:04 UTC (rev 377)
@@ -35,7 +35,7 @@
import java.util.*;
// Log4J
-import org.apache.log4j.Category;
+// import org.apache.log4j.Category;
// Locally written packages.
import org.mulgara.query.rdf.LiteralImpl;
@@ -66,10 +66,8 @@
*/
private Query query;
- /**
- * Logger.
- */
- private Category logger = Category.getInstance(QueryUnitTest.class.getName());
+ // /** Logger. */
+ // private Category logger = Category.getInstance(QueryUnitTest.class.getName());
/**
* Constructs a new answer test with the given name.
@@ -111,17 +109,17 @@
*
* @throws Exception EXCEPTION TO DO
*/
+ @SuppressWarnings("unchecked")
public void setUp() throws Exception {
query = new Query(
- Arrays.asList(new Variable[] {
- new Variable("x")}), // variable list
+ Arrays.asList(new Object[] {new Variable("x")}), // variable list
new ModelResource(new URI("x:m")), // model expression
new ConstraintImpl(new Variable("x"), // constraint expression
new URIReferenceImpl(new URI("x:p")),
new LiteralImpl("o")),
null, // no having
- Collections.EMPTY_LIST, // no ordering
+ (List<Order>)Collections.EMPTY_LIST, // no ordering
null, // no limit
0, // zero offset
new UnconstrainedAnswer());
@@ -160,18 +158,18 @@
*
* @throws Exception if query fails when it should have succeeded
*/
+ @SuppressWarnings("unchecked")
public void test2Equals() throws Exception {
// Compose test instances
Query query2 = new Query(
- Arrays.asList(new Variable[] {
- new Variable("x")}), // variable list
+ Arrays.asList(new Object[] {new Variable("x")}), // variable list
new ModelResource(new URI("x:m")), // model expression
new ConstraintImpl(new Variable("x"), // constraint expression
new URIReferenceImpl(new URI("x:p")),
new LiteralImpl("o")),
null, // no having
- Collections.EMPTY_LIST, // no ordering
+ (List<Order>)Collections.EMPTY_LIST, // no ordering
null, // no limit
0, // zero offset
new UnconstrainedAnswer());
More information about the Mulgara-svn
mailing list