[Mulgara-svn] r1475 - trunk/src/jar/query/java/org/mulgara/query
pag at mulgara.org
pag at mulgara.org
Tue Feb 3 05:39:14 UTC 2009
Author: pag
Date: 2009-02-02 21:39:13 -0800 (Mon, 02 Feb 2009)
New Revision: 1475
Modified:
trunk/src/jar/query/java/org/mulgara/query/GraphAnswer.java
Log:
Now handling graphs built from a multiple of 3 elements, and not just 3
Modified: trunk/src/jar/query/java/org/mulgara/query/GraphAnswer.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/query/GraphAnswer.java 2009-02-03 05:38:08 UTC (rev 1474)
+++ trunk/src/jar/query/java/org/mulgara/query/GraphAnswer.java 2009-02-03 05:39:13 UTC (rev 1475)
@@ -54,14 +54,20 @@
/** The raw answer to wrap. */
private Answer rawAnswer;
+ /** The column counter for emulating rows. */
+ private int colOffset = 0;
+
+ /** The number of rows per column. */
+ private final int rowsPerCol;
+
/**
* 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.");
- }
+ int cols = rawAnswer.getNumberOfVariables();
+ if (cols % 3 != 0) throw new IllegalArgumentException("Cannot construct a graph with " + cols + " columns.");
+ rowsPerCol = cols / 3;
this.rawAnswer = rawAnswer;
}
@@ -69,7 +75,7 @@
* @see org.mulgara.query.Answer#getObject(int)
*/
public Object getObject(int column) throws TuplesException {
- return rawAnswer.getObject(column);
+ return rawAnswer.getObject(column + colOffset);
}
/**
@@ -77,15 +83,16 @@
*/
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);
+ if (CONSTANT_VAR_SUBJECT.equals(columnName)) return rawAnswer.getObject(colOffset);
+ if (CONSTANT_VAR_PREDICATE.equals(columnName)) return rawAnswer.getObject(1 + colOffset);
+ if (CONSTANT_VAR_OBJECT.equals(columnName)) return rawAnswer.getObject(2 + colOffset);
throw new TuplesException("Unknown variable: " + columnName);
}
/** @see org.mulgara.query.Cursor#beforeFirst() */
public void beforeFirst() throws TuplesException {
rawAnswer.beforeFirst();
+ colOffset = (rowsPerCol - 1) * 3;
}
/** @see org.mulgara.query.Cursor#close() */
@@ -115,7 +122,7 @@
* @see org.mulgara.query.Cursor#getRowCardinality()
*/
public int getRowCardinality() throws TuplesException {
- int rawCardinality = rawAnswer.getRowCardinality();
+ int rawCardinality = rawAnswer.getRowCardinality() * rowsPerCol;
if (rawCardinality == 0) return 0;
// get a copy to work with
GraphAnswer answerCopy = (GraphAnswer)clone();
@@ -124,7 +131,8 @@
// 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;
+ if (rawCardinality == 1) return 1;
+ if (!answerCopy.next()) return rowsPerCol;
// Return the raw cardinality
return rawCardinality;
} finally {
@@ -143,7 +151,7 @@
answerCopy.beforeFirst();
long result = 0;
while (answerCopy.next()) result++;
- return result;
+ return result * rowsPerCol;
} finally {
answerCopy.close();
}
@@ -153,7 +161,7 @@
* @see org.mulgara.query.Cursor#getRowUpperBound()
*/
public long getRowUpperBound() throws TuplesException {
- return rawAnswer.getRowUpperBound();
+ return rawAnswer.getRowUpperBound() * rowsPerCol;
}
/**
@@ -177,30 +185,45 @@
return false;
}
+
/**
* @see org.mulgara.query.Cursor#next()
*/
public boolean next() throws TuplesException {
boolean nextAvailable;
do {
- nextAvailable = rawAnswer.next();
+ nextAvailable = internalNext();
} while (nextAvailable && !graphable());
return nextAvailable;
}
+
/** @see java.lang.Object#clone() */
public Object clone() {
return new GraphAnswer((Answer)rawAnswer.clone());
}
+
/**
+ * An internal method for moving on to the next row, without testing validity.
+ * @return <code>true</code> if this call has not exhausted the rows.
+ * @throws TuplesException Due to an error in the underlying rawAnswer.
+ */
+ private boolean internalNext() throws TuplesException {
+ if ((colOffset += 3) < (rowsPerCol * 3)) return true;
+ colOffset = 0;
+ return rawAnswer.next();
+ }
+
+
+ /**
* 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 graphable() throws TuplesException {
- if (rawAnswer.getObject(0) instanceof Literal) return false;
- Object predicate = rawAnswer.getObject(1);
+ if (rawAnswer.getObject(colOffset) instanceof Literal) return false;
+ Object predicate = rawAnswer.getObject(1 + colOffset);
return !(predicate instanceof Literal || predicate instanceof BlankNode);
}
}
More information about the Mulgara-svn
mailing list