[Mulgara-svn] r1040 - trunk/src/jar/query/java/org/mulgara/query
pag at mulgara.org
pag at mulgara.org
Wed Jul 2 21:33:35 UTC 2008
Author: pag
Date: 2008-07-02 14:33:34 -0700 (Wed, 02 Jul 2008)
New Revision: 1040
Added:
trunk/src/jar/query/java/org/mulgara/query/AskQuery.java
trunk/src/jar/query/java/org/mulgara/query/BooleanAnswer.java
trunk/src/jar/query/java/org/mulgara/query/ConstructQuery.java
trunk/src/jar/query/java/org/mulgara/query/GraphAnswer.java
Log:
Extended Query into Construct and AskQueries, with the associated GraphAnswer and BooleanAnswer
Added: trunk/src/jar/query/java/org/mulgara/query/AskQuery.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/query/AskQuery.java (rev 0)
+++ trunk/src/jar/query/java/org/mulgara/query/AskQuery.java 2008-07-02 21:33:34 UTC (rev 1040)
@@ -0,0 +1,60 @@
+/*
+ * The contents of this file are subject to the Open Software License
+ * Version 3.0 (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.opensource.org/licenses/osl-3.0.txt
+ *
+ * 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.
+ */
+
+package org.mulgara.query;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.mulgara.connection.Connection;
+
+/**
+ * A query type to indicate that the result should be boolean. A true result indicates
+ * that the query would return more than 0 rows.
+ *
+ * @created Jun 26, 2008
+ * @author Paul Gearon
+ * @copyright © 2008 <a href="http://www.topazproject.org/">The Topaz Project</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class AskQuery extends Query {
+
+ /** Required serialization ID */
+ private static final long serialVersionUID = -6024259961466362580L;
+
+ /**
+ * Creates an ASK query.
+ * @param variableList The variables in the result to check for.
+ * @param modelExpression The source of the data to query.
+ * @param constraintExpression The WHERE clause to test.
+ */
+ @SuppressWarnings("unchecked")
+ public AskQuery(List<? extends SelectElement> variableList, ModelExpression modelExpression,
+ ConstraintExpression constraintExpression) {
+ super(variableList, modelExpression, constraintExpression,
+ null, // no having
+ (List<Order>)Collections.EMPTY_LIST, // no ordering
+ null, // no limit
+ 0, // zero offset
+ new UnconstrainedAnswer());
+ }
+
+ /**
+ * 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. Closing is optional.
+ */
+ public BooleanAnswer execute(Connection conn) throws QueryException, TuplesException {
+ return new BooleanAnswer(conn.getSession().query(this));
+ }
+
+}
Added: trunk/src/jar/query/java/org/mulgara/query/BooleanAnswer.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/query/BooleanAnswer.java (rev 0)
+++ trunk/src/jar/query/java/org/mulgara/query/BooleanAnswer.java 2008-07-02 21:33:34 UTC (rev 1040)
@@ -0,0 +1,151 @@
+/*
+ * The contents of this file are subject to the Open Software License
+ * Version 3.0 (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.opensource.org/licenses/osl-3.0.txt
+ *
+ * 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.
+ */
+
+package org.mulgara.query;
+
+import java.io.Serializable;
+
+/**
+ * An Answer that represents a simple true/false result.
+ *
+ * @created Jun 26, 2008
+ * @author Paul Gearon
+ * @copyright © 2008 <a href="http://www.topazproject.org/">The Topaz Project</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class BooleanAnswer implements Answer, Serializable {
+
+ /** Required ID for serialization */
+ private static final long serialVersionUID = -4548465246790083233L;
+
+ /** A default variable name. This matches the default names used elsewhere. */
+ private static final String KONSTANT_VAR_NAME = "k0";
+
+ /** The default variable. */
+ private static final Variable KONSTANT_VAR = new Variable(KONSTANT_VAR_NAME);
+
+ /** An array containing the default variable. */
+ private static final Variable[] KONSTANT_VAR_ARR = new Variable[] { KONSTANT_VAR };
+
+ /** Used to simulate a cursored result. */
+ private boolean beforeFirstState = false;
+
+ /** The actual result to be returned, and wrapped by this Answer */
+ private boolean result;
+
+ /**
+ * Constructs a new BooleanAnswer.
+ * @param result The result this answer represents.
+ */
+ public BooleanAnswer(boolean result) {
+ this.result = result;
+ }
+
+ /**
+ * Gets the result this answer represents.
+ * @return The result of this answer.
+ */
+ public boolean getResult() {
+ return result;
+ }
+
+ /**
+ * @see org.mulgara.query.Answer#getObject(int)
+ */
+ public Object getObject(int column) throws TuplesException {
+ if (column == 0) return result;
+ throw new TuplesException("Invalid column: " + column);
+ }
+
+ /**
+ * @see org.mulgara.query.Answer#getObject(java.lang.String)
+ */
+ public Object getObject(String columnName) throws TuplesException {
+ if (KONSTANT_VAR_NAME.equals(columnName)) return result;
+ throw new TuplesException("Unknown variable");
+ }
+
+ /** @see org.mulgara.query.Cursor#beforeFirst() */
+ public void beforeFirst() throws TuplesException {
+ beforeFirstState = true;
+ }
+
+ /** @see org.mulgara.query.Cursor#close() */
+ public void close() throws TuplesException { /* no op */ }
+
+ /**
+ * @see org.mulgara.query.Cursor#getColumnIndex(org.mulgara.query.Variable)
+ */
+ public int getColumnIndex(Variable column) throws TuplesException {
+ if (KONSTANT_VAR.equals(column)) return 0;
+ throw new TuplesException("Unknown variable");
+ }
+
+ /**
+ * @see org.mulgara.query.Cursor#getNumberOfVariables()
+ */
+ public int getNumberOfVariables() {
+ return 1;
+ }
+
+ /**
+ * @see org.mulgara.query.Cursor#getRowCardinality()
+ */
+ public int getRowCardinality() throws TuplesException {
+ return 1;
+ }
+
+ /**
+ * @see org.mulgara.query.Cursor#getRowCount()
+ */
+ public long getRowCount() throws TuplesException {
+ return 1;
+ }
+
+ /**
+ * @see org.mulgara.query.Cursor#getRowUpperBound()
+ */
+ public long getRowUpperBound() throws TuplesException {
+ return 1;
+ }
+
+ /**
+ * @see org.mulgara.query.Cursor#getVariables()
+ */
+ public Variable[] getVariables() {
+ return KONSTANT_VAR_ARR;
+ }
+
+ /**
+ * @see org.mulgara.query.Cursor#isUnconstrained()
+ */
+ public boolean isUnconstrained() throws TuplesException {
+ return false;
+ }
+
+ /**
+ * @see org.mulgara.query.Cursor#next()
+ */
+ public boolean next() throws TuplesException {
+ if (beforeFirstState) {
+ beforeFirstState = false;
+ return true;
+ }
+ return false;
+ }
+
+ /** @see java.lang.Object#clone() */
+ public Object clone() {
+ return new BooleanAnswer(result);
+ }
+
+}
Added: trunk/src/jar/query/java/org/mulgara/query/ConstructQuery.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/query/ConstructQuery.java (rev 0)
+++ trunk/src/jar/query/java/org/mulgara/query/ConstructQuery.java 2008-07-02 21:33:34 UTC (rev 1040)
@@ -0,0 +1,62 @@
+/*
+ * The contents of this file are subject to the Open Software License
+ * Version 3.0 (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.opensource.org/licenses/osl-3.0.txt
+ *
+ * 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.
+ */
+
+package org.mulgara.query;
+
+import java.util.List;
+
+import org.mulgara.connection.Connection;
+
+/**
+ * A query type similar to SELECT, though it may only contain results with multiples of
+ * 3 columns, with the following rules:
+ * First column: contains URIs or BlankNodes
+ * Second column: contains URIs
+ * Third column: contains URIs, BlankNodes, or Literals
+ *
+ * @created Jun 26, 2008
+ * @author Paul Gearon
+ * @copyright © 2008 <a href="http://www.topazproject.org/">The Topaz Project</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class ConstructQuery extends Query {
+
+ /** Required serialization ID */
+ private static final long serialVersionUID = -6024259961466362580L;
+
+ @SuppressWarnings("unchecked")
+ public ConstructQuery(List<? extends SelectElement> variableList, ModelExpression modelExpression,
+ ConstraintExpression constraintExpression,
+ List<Order> orderList, Integer limit, int offset) {
+ super(variableList, modelExpression, constraintExpression,
+ null, // no having
+ orderList,
+ limit,
+ offset,
+ new UnconstrainedAnswer());
+ }
+
+ /**
+ * 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. This must be closed by the calling code.
+ */
+ public GraphAnswer execute(Connection conn) throws QueryException, TuplesException {
+ // pipe all the query types through the one Session method
+ GraphAnswer answer = (GraphAnswer)conn.getSession().query(this);
+ if (answer == null) throw new QueryException("Invalid answer received");
+ // move to the first row
+ answer.beforeFirst();
+ return answer;
+ }
+
+}
Added: trunk/src/jar/query/java/org/mulgara/query/GraphAnswer.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/query/GraphAnswer.java (rev 0)
+++ trunk/src/jar/query/java/org/mulgara/query/GraphAnswer.java 2008-07-02 21:33:34 UTC (rev 1040)
@@ -0,0 +1,206 @@
+/*
+ * The contents of this file are subject to the Open Software License
+ * Version 3.0 (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.opensource.org/licenses/osl-3.0.txt
+ *
+ * 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.
+ */
+
+package org.mulgara.query;
+
+import java.io.Serializable;
+
+import org.jrdf.graph.BlankNode;
+import org.jrdf.graph.Literal;
+
+/**
+ * An Answer that represents a graph.
+ *
+ * @created Jun 30, 2008
+ * @author Paul Gearon
+ * @copyright © 2008 <a href="http://www.topazproject.org/">The Topaz Project</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class GraphAnswer implements Answer, Serializable {
+
+ /** The serialization ID. */
+ private static final long serialVersionUID = -5499236950928116988L;
+
+ /** The variable name for the first column. */
+ private static final String CONSTANT_VAR_SUBJECT = "subject";
+
+ /** The variable name for the second column. */
+ private static final String CONSTANT_VAR_PREDICATE = "predicate";
+
+ /** The variable name for the third column. */
+ private static final String CONSTANT_VAR_OBJECT = "object";
+
+ /** The first column variable. */
+ private static final Variable SUBJECT_VAR = new Variable(CONSTANT_VAR_SUBJECT);
+
+ /** The second column variable. */
+ private static final Variable PREDICATE_VAR = new Variable(CONSTANT_VAR_PREDICATE);
+
+ /** The third column variable. */
+ private static final Variable OBJECT_VAR = new Variable(CONSTANT_VAR_OBJECT);
+
+ /** An array containing the variable. */
+ private static final Variable[] CONSTANT_VAR_ARR = new Variable[] { SUBJECT_VAR, PREDICATE_VAR, OBJECT_VAR };
+
+ /** The raw answer to wrap. */
+ private Answer rawAnswer;
+
+ /**
+ * Constructs a new BooleanAnswer.
+ * @param result The result this answer represents.
+ */
+ public GraphAnswer(Answer rawAnswer) {
+ if (rawAnswer.getNumberOfVariables() != 3) {
+ throw new IllegalArgumentException("Cannot construct a graph with " + rawAnswer.getNumberOfVariables() + " columns.");
+ }
+ this.rawAnswer = rawAnswer;
+ }
+
+ /**
+ * @see org.mulgara.query.Answer#getObject(int)
+ */
+ public Object getObject(int column) throws TuplesException {
+ return rawAnswer.getObject(column);
+ }
+
+ /**
+ * @see org.mulgara.query.Answer#getObject(java.lang.String)
+ */
+ public Object getObject(String columnName) throws TuplesException {
+ // use an unrolled loop
+ if (CONSTANT_VAR_SUBJECT.equals(columnName)) return rawAnswer.getObject(0);
+ if (CONSTANT_VAR_PREDICATE.equals(columnName)) return rawAnswer.getObject(1);
+ if (CONSTANT_VAR_OBJECT.equals(columnName)) return rawAnswer.getObject(2);
+ throw new TuplesException("Unknown variable: " + columnName);
+ }
+
+ /** @see org.mulgara.query.Cursor#beforeFirst() */
+ public void beforeFirst() throws TuplesException {
+ rawAnswer.beforeFirst();
+ }
+
+ /** @see org.mulgara.query.Cursor#close() */
+ public void close() throws TuplesException {
+ rawAnswer.close();
+ }
+
+ /**
+ * @see org.mulgara.query.Cursor#getColumnIndex(org.mulgara.query.Variable)
+ */
+ public int getColumnIndex(Variable column) throws TuplesException {
+ // use an unrolled loop
+ if (SUBJECT_VAR.equals(column)) return 0;
+ if (PREDICATE_VAR.equals(column)) return 1;
+ if (OBJECT_VAR.equals(column)) return 2;
+ throw new TuplesException("Unknown variable: " + column);
+ }
+
+ /**
+ * @see org.mulgara.query.Cursor#getNumberOfVariables()
+ */
+ public int getNumberOfVariables() {
+ return 3;
+ }
+
+ /**
+ * @see org.mulgara.query.Cursor#getRowCardinality()
+ */
+ public int getRowCardinality() throws TuplesException {
+ int rawCardinality = rawAnswer.getRowCardinality();
+ if (rawCardinality == 0) return 0;
+ // get a copy to work with
+ GraphAnswer answerCopy = (GraphAnswer)clone();
+ try {
+ answerCopy.beforeFirst();
+ // test if one row
+ if (!answerCopy.next()) return 0;
+ // test if we know it can't be more than 1, or if there is no second row
+ if (rawCardinality == 1 || !answerCopy.next()) return 1;
+ // Return the raw cardinality
+ return rawCardinality;
+ } finally {
+ answerCopy.close();
+ }
+ }
+
+ /**
+ * @see org.mulgara.query.Cursor#getRowCount()
+ */
+ public long getRowCount() throws TuplesException {
+ // Urk. Doing this the hard way...
+ // get a copy to work with
+ GraphAnswer answerCopy = (GraphAnswer)clone();
+ try {
+ answerCopy.beforeFirst();
+ long result = 0;
+ while (answerCopy.next()) result++;
+ return result;
+ } finally {
+ answerCopy.close();
+ }
+ }
+
+ /**
+ * @see org.mulgara.query.Cursor#getRowUpperBound()
+ */
+ public long getRowUpperBound() throws TuplesException {
+ return rawAnswer.getRowUpperBound();
+ }
+
+ /**
+ * @see org.mulgara.query.Cursor#getVariables()
+ */
+ public Variable[] getVariables() {
+ return CONSTANT_VAR_ARR;
+ }
+
+ /**
+ * Since the returned variables are static, provide them statically as well.
+ */
+ public static Variable[] getGraphVariables() {
+ return CONSTANT_VAR_ARR;
+ }
+
+ /**
+ * @see org.mulgara.query.Cursor#isUnconstrained()
+ */
+ public boolean isUnconstrained() throws TuplesException {
+ return false;
+ }
+
+ /**
+ * @see org.mulgara.query.Cursor#next()
+ */
+ public boolean next() throws TuplesException {
+ boolean nextAvailable;
+ do {
+ nextAvailable = rawAnswer.next();
+ } while (nextAvailable && notGraphable());
+ return nextAvailable;
+ }
+
+ /** @see java.lang.Object#clone() */
+ public Object clone() {
+ return new GraphAnswer((Answer)rawAnswer.clone());
+ }
+
+ /**
+ * Test if the current row is expressible as a graph row.
+ * @return <code>true</code> if the subject-predicate-object have valid node types.
+ * @throws TuplesException The row could not be accessed.
+ */
+ private boolean notGraphable() throws TuplesException {
+ if (rawAnswer.getObject(0) instanceof Literal) return false;
+ Object predicate = rawAnswer.getObject(1);
+ return !(predicate instanceof Literal || predicate instanceof BlankNode);
+ }
+}
More information about the Mulgara-svn
mailing list