[Mulgara-svn] r1480 - trunk/src/jar/tuples/java/org/mulgara/store/tuples
andrae at mulgara.org
andrae at mulgara.org
Thu Feb 12 11:45:11 UTC 2009
Author: andrae
Date: 2009-02-12 03:45:10 -0800 (Thu, 12 Feb 2009)
New Revision: 1480
Added:
trunk/src/jar/tuples/java/org/mulgara/store/tuples/ReorderAnnotation.java
Removed:
trunk/src/jar/tuples/java/org/mulgara/store/tuples/ConstrainedNegationTuples.java
trunk/src/jar/tuples/java/org/mulgara/store/tuples/ConstrainedNegationTuplesUnitTest.java
Modified:
trunk/src/jar/tuples/java/org/mulgara/store/tuples/TuplesOperations.java
Log:
Cleaned up TuplesOperations.project() prior to fixing project() ordering performance bug.
Removed left-over krud that used to support exclude().
Took the opportunity to add the ReorderAnnotation class I will require to fix the bug.
Deleted: trunk/src/jar/tuples/java/org/mulgara/store/tuples/ConstrainedNegationTuples.java
===================================================================
--- trunk/src/jar/tuples/java/org/mulgara/store/tuples/ConstrainedNegationTuples.java 2009-02-07 02:21:08 UTC (rev 1479)
+++ trunk/src/jar/tuples/java/org/mulgara/store/tuples/ConstrainedNegationTuples.java 2009-02-12 11:45:10 UTC (rev 1480)
@@ -1,644 +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.
- * getModel() contributed by Netymon Pty Ltd on behalf of
- * The Australian Commonwealth Government under contract 4500507038.
- *
- * [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.store.tuples;
-
-// Third party packages
-import org.apache.log4j.Logger;
-
-// Standard Java packages
-import java.util.*;
-
-// Locally written packages
-import org.mulgara.query.*;
-import org.mulgara.store.nodepool.NodePool;
-import org.mulgara.store.statement.StatementStore;
-import org.mulgara.store.statement.StatementStoreException;
-
-/**
- * Tuples backed by the graph, corresponding the tuples that don't match
- * a particular constraint. This class retains the original constraint so that
- * the graph index it's resolved against can be resolved anew as its variables
- * are bound.
- *
- * @created 2004-08-04
- *
- * @author Andrew Newman
- *
- * @version $Revision: 1.10 $
- *
- * @modified $Date: 2005/02/22 08:17:04 $ by $Author: newmana $
- *
- * @maintenanceAuthor $Author: newmana $
- *
- * @company <a href="mailto:info at PIsoftware.com">Plugged In Software</a>
- *
- * @copyright © 2003 <A href="http://www.PIsoftware.com/">Plugged In
- * Software Pty Ltd</A>
- *
- * @licence <a href="{@docRoot}/../../LICENCE">Mozilla Public License v1.1</a>
- */
-class ConstrainedNegationTuples extends AbstractTuples {
-
- protected static final Logger logger = Logger.getLogger(ConstrainedNegationTuples.class);
-
- /**
- * The constraint these tuples were generated to satisfy.
- */
- protected Constraint constraint;
-
- /**
- * The graph from which these tuples were generated.
- */
- protected StatementStore store;
-
- /**
- * The tuples.
- */
- private Tuples tuples;
-
- /**
- * The long representation of the subject node or NodePool.NONE
- */
- private long subject;
-
- /**
- * The long representation of the predicate node or NodePool.NONE
- */
- private long predicate;
-
- /**
- * The long representation of the object node or NodePool.NONE
- */
- private long object;
-
- /**
- * The long representation of the meta node or NodePool.NONE
- */
- private long metanode;
-
- /**
- * Prefix. The prefix into the tuples to the section to exclude.
- */
- private long[] excludePrefix;
-
- /**
- * The not evaluator for a given tuples.
- */
- private TuplesEvaluator tuplesEvaluator;
-
- /**
- * Whether we've calculated the rows.
- */
- private boolean calculatedRowCount;
-
- /**
- * An array that contains the position in the tuples. Elements 0, 1, 2, 3
- * map to subject, predicate, object and meta and the value in the array is
- * the column position.
- */
- private int[] columnOrder;
-
- /**
- * True when we have skipped over the block in the store.
- */
- private boolean skipped = false;
-
- /**
- * True if all constraints are fixed.
- */
- private boolean allFixedConstraints;
-
- /**
- * True if a single statement given by the constraints exists.
- */
- private boolean singleStatementExists;
-
- /**
- * Construct a tuples with node numbers and local tuples objects.
- *
- * @param newSubject the subject number.
- * @param newPredicate the predicate number.
- * @param newObject the object number.
- * @param newTuples a local tuples representing a graph.
- * @param newColumnOrder the column bindings - element 0 says which column
- * the subject is in, element 1 says which column the predicate is in and
- * so forth.
- */
- ConstrainedNegationTuples(long newSubject, long newPredicate,
- long newObject, Tuples newTuples, int[] newColumnOrder) {
-
- subject = newSubject;
- predicate = newPredicate;
- object = newObject;
-
- allFixedConstraints = (subject != NodePool.NONE) &&
- (predicate != NodePool.NONE) && (object != NodePool.NONE);
-
- tuples = newTuples;
- try {
- if (allFixedConstraints) {
- tuples.beforeFirst(new long[] { subject, predicate, object}, 0);
- singleStatementExists = tuples.next();
- }
- else {
- tuples.beforeFirst();
- }
- }
- catch (TuplesException te) {
- logger.error(te);
- }
- columnOrder = newColumnOrder;
- setTuplesEvaluator();
- setVariables(newTuples.getVariables());
- }
-
- /**
- * Find a graph index that satisfies a constraint.
- *
- * @param newConstraint the constraint to satisfy
- * @param newStore the store to resolve against
- * @throws IllegalArgumentException if <var>constraint</var> or <var>graph
- * </var> is <code>null</code>
- * @throws TuplesException EXCEPTION TO DO
- */
- ConstrainedNegationTuples(Constraint newConstraint, StatementStore newStore)
- throws TuplesException {
-
- if (newStore == null) {
- throw new IllegalArgumentException("Cannot give null statementstore");
- }
-
- constraint = newConstraint;
- store = newStore;
-
- subject = toGraphTuplesIndex(constraint.getElement(0));
- predicate = toGraphTuplesIndex(constraint.getElement(1));
- object = toGraphTuplesIndex(constraint.getElement(2));
- metanode = toGraphTuplesIndex(constraint.getModel());
-
- allFixedConstraints = (subject != NodePool.NONE) &&
- (predicate != NodePool.NONE) && (object != NodePool.NONE);
-
- try {
-
- if (allFixedConstraints) {
- singleStatementExists = store.existsTriples(subject, predicate, object,
- metanode);
- }
-
- // Return the tuples using the s, p, o hints.
- tuples = store.findTuples(subject != NodePool.NONE,
- predicate != NodePool.NONE,
- object != NodePool.NONE,
- metanode != NodePool.NONE);
-
- tuples.beforeFirst();
-
- // Get the column order - this is column index -> tuples index we want
- // tuple index -> column index
- int[] inverseColumnOrder = ((StoreTuples) tuples).getColumnOrder();
-
- columnOrder = new int[4];
-
- // Create column map.
- for (int index = 0; index < inverseColumnOrder.length; index++) {
- columnOrder[inverseColumnOrder[index]] = index;
- }
-
- // Prepopulate variable list.
- List variableList = new ArrayList(Arrays.asList(StatementStore.VARIABLES));
- boolean changedList = false;
-
- // Create correct variable bindings in order.
- for (int index = 0; index < 4; index++) {
- if (constraint.getElement(index) instanceof Variable) {
- Variable var = (Variable) constraint.getElement(index);
- if (!var.equals(Variable.FROM)) {
-
- // Remove it from it's position in the list and re-add it with the
- // correct value.
- changedList = true;
- variableList.remove(columnOrder[index]);
- variableList.add(columnOrder[index], var);
- }
- }
- }
-
- setTuplesEvaluator();
- setVariables(variableList);
- }
- catch (StatementStoreException se) {
- throw new TuplesException("Failed to set-up tuples", se);
- }
- }
-
- /**
- * Create the appropriate tuples evaluator.
- */
- private void setTuplesEvaluator() {
-
- if (((subject != NodePool.NONE) && (predicate == NodePool.NONE) && (object == NodePool.NONE)) ||
- ((subject != NodePool.NONE) && (predicate != NodePool.NONE) && (object == NodePool.NONE)) ||
- ((subject != NodePool.NONE) && (predicate != NodePool.NONE) && (object != NodePool.NONE))) {
- tuplesEvaluator = new SPOTuplesEvaluator();
-
- if ((subject != NodePool.NONE) && (predicate == NodePool.NONE) && (object == NodePool.NONE)) {
- excludePrefix = new long[] { subject };
- }
- else if ((subject != NodePool.NONE) && (predicate != NodePool.NONE) && (object == NodePool.NONE)) {
- excludePrefix = new long[] { subject, predicate };
- }
- else if ((subject != NodePool.NONE) && (predicate != NodePool.NONE) && (object != NodePool.NONE)) {
- excludePrefix = new long[] { subject, predicate, object };
- }
- }
- else if (((subject == NodePool.NONE) && (predicate != NodePool.NONE) && (object == NodePool.NONE)) ||
- ((subject == NodePool.NONE) && (predicate != NodePool.NONE) && (object != NodePool.NONE))) {
- tuplesEvaluator = new POSTuplesEvaluator();
- if ((subject == NodePool.NONE) && (predicate != NodePool.NONE) && (object == NodePool.NONE)) {
- excludePrefix = new long[] { predicate };
- }
- else if ((subject == NodePool.NONE) && (predicate != NodePool.NONE) && (object != NodePool.NONE)) {
- excludePrefix = new long[] { predicate, object };
- }
- }
- else if ((subject == NodePool.NONE) && (predicate == NodePool.NONE) && (object != NodePool.NONE)) {
- tuplesEvaluator = new OSPTuplesEvaluator();
- excludePrefix = new long[] { object };
- }
- else if ((subject != NodePool.NONE) && (predicate == NodePool.NONE) && (object != NodePool.NONE)) {
- tuplesEvaluator = new SOPTuplesEvaluator();
- excludePrefix = new long[] { subject, object };
- }
- else if ((subject == NodePool.NONE) && (predicate == NodePool.NONE) && (object == NodePool.NONE)) {
- tuplesEvaluator = new UnconstrainedTuplesEvaluator();
- excludePrefix = new long[] {};
- }
- }
-
- public long getRowCount() throws TuplesException {
-
- // Only calculate rows once.
- if (!calculatedRowCount) {
-
- // Start with total number of tuples.
- rowCount = tuples.getRowCount();
-
- // A counter of what to subtract from total number of tuples.
- long counter = 0;
-
- // If the exclude prefix is not empty calculate otherwise we've asked
- // for a NOT ($s $p $o) which is 0.
- if (excludePrefix.length > 0) {
-
- Tuples tmpTuples = null;
- try {
-
- // Clone the tuples and start at the exclusion prefix.
- tmpTuples = (Tuples) tuples.clone();
- tmpTuples.beforeFirst(excludePrefix, 0);
-
- // Start at the prefix and count how many tuples there are.
- for (int prefixIndex = 0; prefixIndex < excludePrefix.length;
- prefixIndex++) {
- long columnValue = excludePrefix[prefixIndex];
-
- while (tmpTuples.next() &&
- tmpTuples.getColumnValue(prefixIndex) == columnValue) {
- counter++;
- }
- }
- }
- finally {
- if (tmpTuples != null) {
- tmpTuples.close();
- }
- }
-
- rowCount = rowCount - counter;
- }
- else {
- rowCount = 0;
- }
-
- // Ensure we don't calculate the rows again.
- calculatedRowCount = true;
- }
- return rowCount;
- }
-
- public boolean hasNoDuplicates() throws TuplesException {
- return tuples.hasNoDuplicates();
- }
-
- public List getOperands() {
- return new ArrayList();
- }
-
- public void beforeFirst(long[] prefix, int suffixTruncation) throws TuplesException {
- if (prefix.length > 4) {
- throw new TuplesException("Prefix too long");
- }
-
- // Reset skipped.
- skipped = false;
-
- // Move to the
- tuples.beforeFirst(prefix, suffixTruncation);
- }
-
- public void close() throws TuplesException {
- if (tuples != null) {
- tuples.close();
- }
- }
-
- /**
- * METHOD TO DO
- *
- * @return RETURNED VALUE TO DO
- */
- public Object clone() {
- ConstrainedNegationTuples cloned = (ConstrainedNegationTuples) super.clone();
- cloned.tuples = (Tuples) tuples.clone();
- cloned.setVariables(getVariables());
-
- // Create a new tuples evaluator.
- cloned.setTuplesEvaluator();
- return cloned;
- }
-
-
- public long getRowUpperBound() throws TuplesException {
- return getRowCount();
- }
-
- public int getRowCardinality() throws TuplesException {
- long count = getRowCount();
- if (count > 1) {
- return Cursor.MANY;
- }
- switch ((int) count) {
- case 0:
- return Cursor.ZERO;
- case 1:
- return Cursor.ONE;
- default:
- throw new TuplesException("Illegal row count: " + count);
- }
- }
-
- public long getColumnValue(int column) throws TuplesException {
- return tuples.getColumnValue(column);
- }
-
- public boolean isColumnEverUnbound(int column) throws TuplesException {
- return tuples.isColumnEverUnbound(column);
- }
-
- public boolean isUnconstrained() throws TuplesException {
-
- // If we have a least one variable return the tuples isUnconstrainedValue.
- if (!allFixedConstraints) {
- return tuples.isUnconstrained();
- }
- else {
-
- // If the statement exists we are constrained.
- if (singleStatementExists) {
- return false;
- }
- else {
- return true;
- }
- }
- }
-
- public boolean isMaterialized() {
- return tuples.isMaterialized();
- }
-
- public boolean next() throws TuplesException {
- boolean hasNext = tuplesEvaluator.next();
- return hasNext;
- }
-
- public String toString() {
- return "not " + tuples.toString() + " from constraint " + constraint;
- }
-
- /**
- * Returns the long representation of the constraint element or
- * NodePool.NONE if a variable.
- *
- * @param constraintElement the constraint element to resolve.
- * @throws TuplesException if the constraint element is not supported.
- * @return long the long representation of the constraint element.
- */
- protected static long toGraphTuplesIndex(ConstraintElement constraintElement)
- throws TuplesException {
- if (constraintElement instanceof Variable) {
- return NodePool.NONE;
- }
- if (constraintElement instanceof LocalNode) {
- return ((LocalNode) constraintElement).getValue();
- }
-
- throw new TuplesException("Unsupported constraint element: " +
- constraintElement + " (" + constraintElement.getClass() + ")");
- }
-
- /**
- * A decorator around the next() method on a tuples that skips a set of
- * statements if they are given in the constraint.
- */
- private interface TuplesEvaluator {
-
- /**
- * Returns true if there is another tuples. Calling next may skip a set
- * of tuples based on the constraint.
- *
- * @throws TuplesException if there was a problem accessing the tuples.
- * @return true if there is another tuples.
- */
- public boolean next() throws TuplesException;
- }
-
- /**
- * The evaluator for the S, P, O constrained tuples.
- */
- private class SPOTuplesEvaluator implements TuplesEvaluator {
-
- public boolean next() throws TuplesException {
-
- // Iterate forward one.
- boolean moreTuples = tuples.next();
-
- // If we've skipped over the tuples then just call the normal tuples.next.
- if (!skipped) {
-
- // Assume that the subject is always bound - otherwise wouldn't be used.
- // If the predicate is also constrained iterate through these.
- if (predicate != NodePool.NONE) {
-
- // If the predicate is also constrained iterate through these.
- if (object != NodePool.NONE) {
-
- // If we match then skip over the totally constrained tuple.
- if ((moreTuples) &&
- (tuples.getColumnValue(columnOrder[0]) == subject) &&
- (tuples.getColumnValue(columnOrder[1]) == predicate) &&
- (tuples.getColumnValue(columnOrder[2]) == object)) {
- moreTuples = tuples.next();
- skipped = true;
- }
- }
- else {
-
- // Skip over all the objects for the given subject, predicate.
- while ((moreTuples) &&
- (tuples.getColumnValue(columnOrder[0]) == subject) &&
- (tuples.getColumnValue(columnOrder[1]) == predicate)) {
- moreTuples = tuples.next();
- skipped = true;
- }
- }
- }
- else {
-
- // Skip over all the predicate, objects for the given subject.
- while ((moreTuples) &&
- (tuples.getColumnValue(columnOrder[0]) == subject)) {
- moreTuples = tuples.next();
- skipped = true;
- }
- }
- }
- return moreTuples;
- }
- }
-
- /**
- * The evaluator for the P, O, S constrained tuples.
- */
- private class POSTuplesEvaluator implements TuplesEvaluator {
-
- public boolean next() throws TuplesException {
-
- // Iterate forward one.
- boolean moreTuples = tuples.next();
-
- // If we've skipped over the tuples then just call the normal tuples.next.
- if (!skipped) {
-
- // If the object is also constrained iterate through these.
- if (object != NodePool.NONE) {
-
- // Skip over all the subjects for the given predicate, objects.
- while ((moreTuples) &&
- (tuples.getColumnValue(columnOrder[1]) == predicate) &&
- (tuples.getColumnValue(columnOrder[2]) == object)) {
- moreTuples = tuples.next();
- skipped = true;
- }
- }
- else {
-
- // Skip over all the object, subjects for the given predicate.
- while ((moreTuples) &&
- (tuples.getColumnValue(columnOrder[1]) == predicate)) {
- moreTuples = tuples.next();
- skipped = true;
- }
- }
- }
- return moreTuples;
- }
- }
-
- /**
- * The evaluator for the O, S, P constrained tuples.
- */
- private class OSPTuplesEvaluator implements TuplesEvaluator {
-
- public boolean next() throws TuplesException {
-
- // Iterate forward one.
- boolean moreTuples = tuples.next();
-
- // If we've skipped over the tuples then just call the normal tuples.next.
- if (!skipped) {
-
- // Skip over all the subject, predicates for the given object.
- while ((moreTuples) &&
- (tuples.getColumnValue(columnOrder[2]) == object)) {
- moreTuples = tuples.next();
- skipped = true;
- }
- }
- return moreTuples;
- }
- }
-
- /**
- * The evaluator for the S, O, P constrained tuples.
- */
- private class SOPTuplesEvaluator implements TuplesEvaluator {
-
- public boolean next() throws TuplesException {
-
- // Iterate forward one.
- boolean moreTuples = tuples.next();
-
- // If we've skipped over the tuples then just call the normal tuples.next.
- if (!skipped) {
-
- // Skip over all the subject, predicates for the given object.
- while ((moreTuples) &&
- (tuples.getColumnValue(columnOrder[0]) == subject) &&
- (tuples.getColumnValue(columnOrder[2]) == object)) {
- moreTuples = tuples.next();
- skipped = true;
- }
- }
- return moreTuples;
- }
- }
-
- /**
- * The evaluator when none of the tuples are constrained.
- */
- private class UnconstrainedTuplesEvaluator implements TuplesEvaluator {
-
- public boolean next() throws TuplesException {
- return false;
- }
- }
-}
Deleted: trunk/src/jar/tuples/java/org/mulgara/store/tuples/ConstrainedNegationTuplesUnitTest.java
===================================================================
--- trunk/src/jar/tuples/java/org/mulgara/store/tuples/ConstrainedNegationTuplesUnitTest.java 2009-02-07 02:21:08 UTC (rev 1479)
+++ trunk/src/jar/tuples/java/org/mulgara/store/tuples/ConstrainedNegationTuplesUnitTest.java 2009-02-12 11:45:10 UTC (rev 1480)
@@ -1,945 +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.store.tuples;
-
-import java.util.*;
-
-// JUnit
-import junit.framework.*;
-
-// Log4J
-import org.apache.log4j.Logger;
-
-// Local packages
-import org.mulgara.store.nodepool.NodePool;
-
-/**
- * Test case for {@link org.mulgara.store.tuples.ConstrainedNegationTuples}.
- *
- * @created 2004-06-15
- *
- * @author Andrew Newman
- *
- * @version $Revision: 1.9 $
- *
- * @modified $Date: 2005/01/05 04:59:10 $
- *
- * @maintenanceAuthor $Author: newmana $
- *
- * @company <A href="mailto:info at PIsoftware.com">Plugged In Software</A>
- *
- * @copyright © 2003 <A href="http://www.PIsoftware.com/">Plugged In
- * Software Pty Ltd</A>
- *
- * @licence <a href="{@docRoot}/../../LICENCE">Mozilla Public License v1.1</a>
- */
-public class ConstrainedNegationTuplesUnitTest extends TestCase {
-
- /**
- * Logger.
- */
- private Logger logger =
- Logger.getLogger(ConstrainedNegationTuplesUnitTest.class.getName());
-
- /**
- * Tuples for testing with.
- */
- private LiteralTuples t1, t2, t3, t4;
-
- /**
- * Constrained negation test tuples.
- */
- private ConstrainedNegationTuples tuples;
-
- /**
- * Constructs a new test with the given name.
- *
- * @param name the name of the test
- */
- public ConstrainedNegationTuplesUnitTest(String name) {
- super(name);
- }
-
- /**
- * Hook for test runner to obtain a test suite from.
- *
- * @return The test suite
- */
- public static Test suite() {
-
- TestSuite suite = new TestSuite();
-
- suite.addTest(new ConstrainedNegationTuplesUnitTest("testConstrained"));
- suite.addTest(new ConstrainedNegationTuplesUnitTest("testUnconstrained"));
- suite.addTest(new ConstrainedNegationTuplesUnitTest("testSubjects"));
- suite.addTest(new ConstrainedNegationTuplesUnitTest("testPredicates"));
- suite.addTest(new ConstrainedNegationTuplesUnitTest("testObjects"));
- suite.addTest(new ConstrainedNegationTuplesUnitTest("testSubjectPredicates"));
- suite.addTest(new ConstrainedNegationTuplesUnitTest("testSubjectObjects"));
- suite.addTest(new ConstrainedNegationTuplesUnitTest("testPredicateObjects"));
- suite.addTest(new ConstrainedNegationTuplesUnitTest("testSubjectPredicateObjects"));
-
- return suite;
- }
-
- /**
- * Create test instance.
- *
- * @throws Exception EXCEPTION TO DO
- */
- public void setUp() throws Exception {
-
- String[] var1 = new String[] { "x", "y", "z" };
- t1 = new LiteralTuples(var1);
- long[][] subjectTuples = new long[][] {
- new long[] { 1, 2, 3 },
- new long[] { 1, 2, 4 },
- new long[] { 1, 2, 5 },
- new long[] { 1, 3, 4 },
- new long[] { 2, 2, 5 },
- new long[] { 2, 3, 6 },
- new long[] { 3, 2, 4 },
- new long[] { 4, 2, 1 },
- new long[] { 4, 2, 6 },
- new long[] { 4, 3, 7 },
- new long[] { 4, 4, 8 },
- new long[] { 4, 5, 8 },
- new long[] { 5, 5, 8 },
- new long[] { 6, 1, 2 },
- new long[] { 7, 2, 1 },
- new long[] { 7, 2, 9 },
- new long[] { 7, 4, 8 },
- new long[] { 7, 5, 9 },
- };
-
- for (int i = 0; i < subjectTuples.length; i++) {
- ((LiteralTuples) t1).appendTuple(subjectTuples[i]);
- }
-
- String[] var2 = new String[] { "y", "z", "x" };
- t2 = new LiteralTuples(var2);
- long[][] predicateTuples = new long[][] {
- new long[] { 1, 2, 6 },
- new long[] { 2, 1, 4 },
- new long[] { 2, 1, 7 },
- new long[] { 2, 3, 1 },
- new long[] { 2, 4, 1 },
- new long[] { 2, 4, 3 },
- new long[] { 2, 5, 1 },
- new long[] { 2, 5, 2 },
- new long[] { 2, 6, 4 },
- new long[] { 3, 6, 2 },
- new long[] { 3, 7, 4 },
- new long[] { 4, 8, 4 },
- new long[] { 4, 8, 7 },
- new long[] { 5, 8, 5 },
- new long[] { 5, 9, 7 },
- };
-
- for (int i = 0; i < predicateTuples.length; i++) {
- ((LiteralTuples) t2).appendTuple(predicateTuples[i]);
- }
-
- String[] var3 = new String[] { "z", "x", "y" };
- t3 = new LiteralTuples(var3);
- long[][] objectTuples = new long[][] {
- new long[] { 1, 4, 2 },
- new long[] { 1, 7, 2 },
- new long[] { 2, 6, 1 },
- new long[] { 3, 1, 2 },
- new long[] { 4, 1, 2 },
- new long[] { 4, 3, 2 },
- new long[] { 5, 1, 2 },
- new long[] { 5, 2, 2 },
- new long[] { 6, 2, 3 },
- new long[] { 6, 4, 2 },
- new long[] { 7, 4, 3 },
- new long[] { 8, 4, 4 },
- new long[] { 8, 5, 5 },
- new long[] { 8, 7, 4 },
- new long[] { 9, 7, 5 },
- };
-
- for (int i = 0; i < objectTuples.length; i++) {
- ((LiteralTuples) t3).appendTuple(objectTuples[i]);
- }
-
- String[] var4 = new String[] { "x", "z", "y" };
- t4 = new LiteralTuples(var4);
- long[][] subjectObjectTuples = new long[][] {
- new long[] { 1, 3, 2 },
- new long[] { 1, 4, 2 },
- new long[] { 1, 4, 3 },
- new long[] { 1, 5, 2 },
- new long[] { 2, 5, 2 },
- new long[] { 2, 6, 3 },
- new long[] { 3, 4, 2 },
- new long[] { 4, 1, 2 },
- new long[] { 4, 6, 2 },
- new long[] { 4, 7, 3 },
- new long[] { 4, 8, 4 },
- new long[] { 4, 8, 5 },
- new long[] { 5, 8, 5 },
- new long[] { 6, 2, 1 },
- new long[] { 7, 1, 2 },
- new long[] { 7, 8, 4 },
- new long[] { 7, 9, 2 },
- new long[] { 7, 9, 5 },
- };
-
- for (int i = 0; i < subjectObjectTuples.length; i++) {
- ((LiteralTuples) t4).appendTuple(subjectObjectTuples[i]);
- }
- }
-
- /**
- * Default text runner.
- *
- * @param args The command line arguments
- */
- public static void main(String[] args) {
-
- junit.textui.TestRunner.run(suite());
- }
-
- //
- // Test cases
- //
-
- /**
- * Test a negation where all are constrained but does not exist in the
- * database.
- *
- * @throws Exception if query fails when it should have succeeded
- */
- public void testConstrained() throws Exception {
-
- // Not subject = 1, predicate = 2, object = 3.
- tuples = new ConstrainedNegationTuples(1l, 2l, 3l, t1, new int[] { 0, 1, 2, 3});
-
- // Expected results - false.
- assertFalse("Should be constrained if found", tuples.isUnconstrained());
-
- // Not subject = 1, predicate = 2, object = 7.
- tuples = new ConstrainedNegationTuples(1l, 2l, 7l, t1, new int[] { 0, 1, 2, 3});
-
- // Expected results - TRUE/Unconstrained
- assertTrue("Should be unconstrained if not found", tuples.isUnconstrained());
- }
-
- /**
- * Test a negation based only all unconstrained that is, variables.
- *
- * @throws Exception if query fails when it should have succeeded
- */
- public void testUnconstrained() throws Exception {
-
- // Not subject = 1.
- tuples = new ConstrainedNegationTuples(NodePool.NONE, NodePool.NONE,
- NodePool.NONE, t1, new int[] { 0, 1, 2, 3});
-
- // Expected results
- long[][] expectedTuples = new long[][] {
- };
-
- testTuples(expectedTuples, tuples);
- }
-
- /**
- * Test a negation based only on a given subject.
- *
- * @throws Exception if query fails when it should have succeeded
- */
- public void testSubjects() throws Exception {
-
- // Not subject = 1.
- tuples = new ConstrainedNegationTuples(1l, NodePool.NONE,
- NodePool.NONE, t1, new int[] { 0, 1, 2, 3});
-
- // Expected results
- long[][] expectedTuples = new long[][] {
- new long[] { 2, 2, 5 },
- new long[] { 2, 3, 6 },
- new long[] { 3, 2, 4 },
- new long[] { 4, 2, 1 },
- new long[] { 4, 2, 6 },
- new long[] { 4, 3, 7 },
- new long[] { 4, 4, 8 },
- new long[] { 4, 5, 8 },
- new long[] { 5, 5, 8 },
- new long[] { 6, 1, 2 },
- new long[] { 7, 2, 1 },
- new long[] { 7, 2, 9 },
- new long[] { 7, 4, 8 },
- new long[] { 7, 5, 9 },
- };
-
- testTuples(expectedTuples, tuples);
-
- // Not subject = 4.
- tuples = new ConstrainedNegationTuples(4l, NodePool.NONE,
- NodePool.NONE, t1, new int[] { 0, 1, 2, 3});
-
- // Expected results
- expectedTuples = new long[][] {
- new long[] { 1, 2, 3 },
- new long[] { 1, 2, 4 },
- new long[] { 1, 2, 5 },
- new long[] { 1, 3, 4 },
- new long[] { 2, 2, 5 },
- new long[] { 2, 3, 6 },
- new long[] { 3, 2, 4 },
- new long[] { 5, 5, 8 },
- new long[] { 6, 1, 2 },
- new long[] { 7, 2, 1 },
- new long[] { 7, 2, 9 },
- new long[] { 7, 4, 8 },
- new long[] { 7, 5, 9 },
- };
-
- testTuples(expectedTuples, tuples);
-
- // Not subject = 7.
- tuples = new ConstrainedNegationTuples(7l, NodePool.NONE,
- NodePool.NONE, t1, new int[] { 0, 1, 2, 3});
-
- // Expected results
- expectedTuples = new long[][] {
- new long[] { 1, 2, 3 },
- new long[] { 1, 2, 4 },
- new long[] { 1, 2, 5 },
- new long[] { 1, 3, 4 },
- new long[] { 2, 2, 5 },
- new long[] { 2, 3, 6 },
- new long[] { 3, 2, 4 },
- new long[] { 4, 2, 1 },
- new long[] { 4, 2, 6 },
- new long[] { 4, 3, 7 },
- new long[] { 4, 4, 8 },
- new long[] { 4, 5, 8 },
- new long[] { 5, 5, 8 },
- new long[] { 6, 1, 2 },
- };
-
- testTuples(expectedTuples, tuples);
- }
-
- /**
- * Test a negation based only on a given predicate.
- *
- * @throws Exception if query fails when it should have succeeded
- */
- public void testPredicates() throws Exception {
-
- // Not predicate = 1.
- tuples = new ConstrainedNegationTuples(NodePool.NONE, 1l,
- NodePool.NONE, t2, new int[] { 2, 0, 1, 3 });
-
- // Expected results
- long[][] expectedTuples = new long[][] {
- new long[] { 2, 1, 4 },
- new long[] { 2, 1, 7 },
- new long[] { 2, 3, 1 },
- new long[] { 2, 4, 1 },
- new long[] { 2, 4, 3 },
- new long[] { 2, 5, 1 },
- new long[] { 2, 5, 2 },
- new long[] { 2, 6, 4 },
- new long[] { 3, 6, 2 },
- new long[] { 3, 7, 4 },
- new long[] { 4, 8, 4 },
- new long[] { 4, 8, 7 },
- new long[] { 5, 8, 5 },
- new long[] { 5, 9, 7 },
- };
-
- testTuples(expectedTuples, tuples);
-
- // Not predicate = 2.
- tuples = new ConstrainedNegationTuples(NodePool.NONE, 2l,
- NodePool.NONE, t2, new int[] { 2, 0, 1, 3 });
-
- // Expected results
- expectedTuples = new long[][] {
- new long[] { 1, 2, 6 },
- new long[] { 3, 6, 2 },
- new long[] { 3, 7, 4 },
- new long[] { 4, 8, 4 },
- new long[] { 4, 8, 7 },
- new long[] { 5, 8, 5 },
- new long[] { 5, 9, 7 },
- };
-
- testTuples(expectedTuples, tuples);
-
- // Not predicate = 7.
- tuples = new ConstrainedNegationTuples(NodePool.NONE, 7l,
- NodePool.NONE, t2, new int[] { 2, 0, 1, 3 });
-
- // Expected results
- expectedTuples = new long[][] {
- new long[] { 1, 2, 6 },
- new long[] { 2, 1, 4 },
- new long[] { 2, 1, 7 },
- new long[] { 2, 3, 1 },
- new long[] { 2, 4, 1 },
- new long[] { 2, 4, 3 },
- new long[] { 2, 5, 1 },
- new long[] { 2, 5, 2 },
- new long[] { 2, 6, 4 },
- new long[] { 3, 6, 2 },
- new long[] { 3, 7, 4 },
- new long[] { 4, 8, 4 },
- new long[] { 4, 8, 7 },
- new long[] { 5, 8, 5 },
- new long[] { 5, 9, 7 },
- };
-
- testTuples(expectedTuples, tuples);
- }
-
- /**
- * Test a negation based only on a given object.
- *
- * @throws Exception if query fails when it should have succeeded
- */
- public void testObjects() throws Exception {
-
- // Not object = 3.
- tuples = new ConstrainedNegationTuples(NodePool.NONE,
- NodePool.NONE, 3l, t3, new int[] { 1, 2, 0, 3});
-
- // Expected results
- long[][] expectedTuples = new long[][] {
- new long[] { 1, 4, 2 },
- new long[] { 1, 7, 2 },
- new long[] { 2, 6, 1 },
- new long[] { 4, 1, 2 },
- new long[] { 4, 3, 2 },
- new long[] { 5, 1, 2 },
- new long[] { 5, 2, 2 },
- new long[] { 6, 2, 3 },
- new long[] { 6, 4, 2 },
- new long[] { 7, 4, 3 },
- new long[] { 8, 4, 4 },
- new long[] { 8, 5, 5 },
- new long[] { 8, 7, 4 },
- new long[] { 9, 7, 5 },
- };
-
- testTuples(expectedTuples, tuples);
-
- // Not object = 8.
- tuples = new ConstrainedNegationTuples(NodePool.NONE,
- NodePool.NONE, 8l, t3, new int[] { 1, 2, 0, 3});
-
- // Expected results
- expectedTuples = new long[][] {
- new long[] { 1, 4, 2 },
- new long[] { 1, 7, 2 },
- new long[] { 2, 6, 1 },
- new long[] { 3, 1, 2 },
- new long[] { 4, 1, 2 },
- new long[] { 4, 3, 2 },
- new long[] { 5, 1, 2 },
- new long[] { 5, 2, 2 },
- new long[] { 6, 2, 3 },
- new long[] { 6, 4, 2 },
- new long[] { 7, 4, 3 },
- new long[] { 9, 7, 5 },
- };
-
- testTuples(expectedTuples, tuples);
-
- // Not object = 10.
- tuples = new ConstrainedNegationTuples(NodePool.NONE,
- NodePool.NONE, 10l, t3, new int[] { 1, 2, 0, 3});
-
- // Expected results
- expectedTuples = new long[][] {
- new long[] { 1, 4, 2 },
- new long[] { 1, 7, 2 },
- new long[] { 2, 6, 1 },
- new long[] { 3, 1, 2 },
- new long[] { 4, 1, 2 },
- new long[] { 4, 3, 2 },
- new long[] { 5, 1, 2 },
- new long[] { 5, 2, 2 },
- new long[] { 6, 2, 3 },
- new long[] { 6, 4, 2 },
- new long[] { 7, 4, 3 },
- new long[] { 8, 4, 4 },
- new long[] { 8, 5, 5 },
- new long[] { 8, 7, 4 },
- new long[] { 9, 7, 5 },
- };
-
- testTuples(expectedTuples, tuples);
- }
-
- /**
- * Test a negation based only on a given subject, predicate.
- *
- * @throws Exception if query fails when it should have succeeded
- */
- public void testSubjectPredicates() throws Exception {
-
- // Not subject, predicate = 1, 2.
- tuples = new ConstrainedNegationTuples(1l, 2l,
- NodePool.NONE, t1, new int[] { 0, 1, 2, 3});
-
- // Expected results
- long[][] expectedTuples = new long[][] {
- new long[] { 1, 3, 4 },
- new long[] { 2, 2, 5 },
- new long[] { 2, 3, 6 },
- new long[] { 3, 2, 4 },
- new long[] { 4, 2, 1 },
- new long[] { 4, 2, 6 },
- new long[] { 4, 3, 7 },
- new long[] { 4, 4, 8 },
- new long[] { 4, 5, 8 },
- new long[] { 5, 5, 8 },
- new long[] { 6, 1, 2 },
- new long[] { 7, 2, 1 },
- new long[] { 7, 2, 9 },
- new long[] { 7, 4, 8 },
- new long[] { 7, 5, 9 },
- };
-
- testTuples(expectedTuples, tuples);
-
- // Not subject, predicate = 4, 2.
- tuples = new ConstrainedNegationTuples(4l, 2l,
- NodePool.NONE, t1, new int[] { 0, 1, 2, 3});
-
- // Expected results
- expectedTuples = new long[][] {
- new long[] { 1, 2, 3 },
- new long[] { 1, 2, 4 },
- new long[] { 1, 2, 5 },
- new long[] { 1, 3, 4 },
- new long[] { 2, 2, 5 },
- new long[] { 2, 3, 6 },
- new long[] { 3, 2, 4 },
- new long[] { 4, 3, 7 },
- new long[] { 4, 4, 8 },
- new long[] { 4, 5, 8 },
- new long[] { 5, 5, 8 },
- new long[] { 6, 1, 2 },
- new long[] { 7, 2, 1 },
- new long[] { 7, 2, 9 },
- new long[] { 7, 4, 8 },
- new long[] { 7, 5, 9 },
- };
-
- testTuples(expectedTuples, tuples);
-
- // Not subject, predicate = 7, 6.
- tuples = new ConstrainedNegationTuples(7l, 6l,
- NodePool.NONE, t1, new int[] { 0, 1, 2, 3});
-
- // Expected results
- expectedTuples = new long[][] {
- new long[] { 1, 2, 3 },
- new long[] { 1, 2, 4 },
- new long[] { 1, 2, 5 },
- new long[] { 1, 3, 4 },
- new long[] { 2, 2, 5 },
- new long[] { 2, 3, 6 },
- new long[] { 3, 2, 4 },
- new long[] { 4, 2, 1 },
- new long[] { 4, 2, 6 },
- new long[] { 4, 3, 7 },
- new long[] { 4, 4, 8 },
- new long[] { 4, 5, 8 },
- new long[] { 5, 5, 8 },
- new long[] { 6, 1, 2 },
- new long[] { 7, 2, 1 },
- new long[] { 7, 2, 9 },
- new long[] { 7, 4, 8 },
- new long[] { 7, 5, 9 },
- };
-
- testTuples(expectedTuples, tuples);
- }
-
-
- /**
- * Test a negation based on a given subject, object.
- *
- * @throws Exception if query fails when it should have succeeded
- */
- public void testSubjectObjects() throws Exception {
-
- // Not subject, object = 1, 2.
- tuples = new ConstrainedNegationTuples(1l, NodePool.NONE, 4l, t4,
- new int[] { 0, 2, 1, 3 });
-
- // Expected results
- long[][] expectedTuples = new long[][] {
- new long[] { 1, 3, 2 },
- new long[] { 1, 5, 2 },
- new long[] { 2, 5, 2 },
- new long[] { 2, 6, 3 },
- new long[] { 3, 4, 2 },
- new long[] { 4, 1, 2 },
- new long[] { 4, 6, 2 },
- new long[] { 4, 7, 3 },
- new long[] { 4, 8, 4 },
- new long[] { 4, 8, 5 },
- new long[] { 5, 8, 5 },
- new long[] { 6, 2, 1 },
- new long[] { 7, 1, 2 },
- new long[] { 7, 8, 4 },
- new long[] { 7, 9, 2 },
- new long[] { 7, 9, 5 },
- };
-
- testTuples(expectedTuples, tuples);
-
- // Not predicate, object = 4, 2.
- tuples = new ConstrainedNegationTuples(7l, NodePool.NONE, 9l, t4,
- new int[] { 0, 2, 1, 3 });
-
- // Expected results
- expectedTuples = new long[][] {
- new long[] { 1, 3, 2 },
- new long[] { 1, 4, 2 },
- new long[] { 1, 4, 3 },
- new long[] { 1, 5, 2 },
- new long[] { 2, 5, 2 },
- new long[] { 2, 6, 3 },
- new long[] { 3, 4, 2 },
- new long[] { 4, 1, 2 },
- new long[] { 4, 6, 2 },
- new long[] { 4, 7, 3 },
- new long[] { 4, 8, 4 },
- new long[] { 4, 8, 5 },
- new long[] { 5, 8, 5 },
- new long[] { 6, 2, 1 },
- new long[] { 7, 1, 2 },
- new long[] { 7, 8, 4 },
- };
-
- testTuples(expectedTuples, tuples);
-
- // Not predicate, object = 7, 6.
- tuples = new ConstrainedNegationTuples(4l, NodePool.NONE, 9l, t4,
- new int[] { 0, 2, 1, 3 });
-
- // Expected results
- expectedTuples = new long[][] {
- new long[] { 1, 3, 2 },
- new long[] { 1, 4, 2 },
- new long[] { 1, 4, 3 },
- new long[] { 1, 5, 2 },
- new long[] { 2, 5, 2 },
- new long[] { 2, 6, 3 },
- new long[] { 3, 4, 2 },
- new long[] { 4, 1, 2 },
- new long[] { 4, 6, 2 },
- new long[] { 4, 7, 3 },
- new long[] { 4, 8, 4 },
- new long[] { 4, 8, 5 },
- new long[] { 5, 8, 5 },
- new long[] { 6, 2, 1 },
- new long[] { 7, 1, 2 },
- new long[] { 7, 8, 4 },
- new long[] { 7, 9, 2 },
- new long[] { 7, 9, 5 },
- };
-
- testTuples(expectedTuples, tuples);
- }
-
- /**
- * Test a negation based only on a given predicate, object.
- *
- * @throws Exception if query fails when it should have succeeded
- */
- public void testPredicateObjects() throws Exception {
-
- // Not predicate, object = 1, 2.
- tuples = new ConstrainedNegationTuples(NodePool.NONE, 1l, 2l, t2,
- new int[] { 2, 0, 1, 3 });
-
- // Expected results
- long[][] expectedTuples = new long[][] {
- new long[] { 2, 1, 4 },
- new long[] { 2, 1, 7 },
- new long[] { 2, 3, 1 },
- new long[] { 2, 4, 1 },
- new long[] { 2, 4, 3 },
- new long[] { 2, 5, 1 },
- new long[] { 2, 5, 2 },
- new long[] { 2, 6, 4 },
- new long[] { 3, 6, 2 },
- new long[] { 3, 7, 4 },
- new long[] { 4, 8, 4 },
- new long[] { 4, 8, 7 },
- new long[] { 5, 8, 5 },
- new long[] { 5, 9, 7 },
- };
-
- testTuples(expectedTuples, tuples);
-
- // Not predicate, object = 4, 2.
- tuples = new ConstrainedNegationTuples(NodePool.NONE, 2l, 4l, t2,
- new int[] { 2, 0, 1, 3 });
-
- // Expected results
- expectedTuples = new long[][] {
- new long[] { 1, 2, 6 },
- new long[] { 2, 1, 4 },
- new long[] { 2, 1, 7 },
- new long[] { 2, 3, 1 },
- new long[] { 2, 5, 1 },
- new long[] { 2, 5, 2 },
- new long[] { 2, 6, 4 },
- new long[] { 3, 6, 2 },
- new long[] { 3, 7, 4 },
- new long[] { 4, 8, 4 },
- new long[] { 4, 8, 7 },
- new long[] { 5, 8, 5 },
- new long[] { 5, 9, 7 },
- };
-
- testTuples(expectedTuples, tuples);
-
- // Not predicate, object = 7, 6.
- tuples = new ConstrainedNegationTuples(NodePool.NONE, 7l, 6l, t2,
- new int[] { 2, 0, 1, 3 });
-
- // Expected results
- expectedTuples = new long[][] {
- new long[] { 1, 2, 6 },
- new long[] { 2, 1, 4 },
- new long[] { 2, 1, 7 },
- new long[] { 2, 3, 1 },
- new long[] { 2, 4, 1 },
- new long[] { 2, 4, 3 },
- new long[] { 2, 5, 1 },
- new long[] { 2, 5, 2 },
- new long[] { 2, 6, 4 },
- new long[] { 3, 6, 2 },
- new long[] { 3, 7, 4 },
- new long[] { 4, 8, 4 },
- new long[] { 4, 8, 7 },
- new long[] { 5, 8, 5 },
- new long[] { 5, 9, 7 },
- };
-
- testTuples(expectedTuples, tuples);
- }
-
- /**
- * Test a negation based only on a given subject, predicate, object.
- *
- * @throws Exception if query fails when it should have succeeded
- */
- public void testSubjectPredicateObjects() throws Exception {
-
- // Not subject, predicate, object = 1, 2, 3.
- tuples = new ConstrainedNegationTuples(1l, 2l, 3l, t1,
- new int[] { 0, 1, 2, 3 });
-
- // Expected results
- long[][] expectedTuples = new long[][] {
- new long[] { 1, 2, 4 },
- new long[] { 1, 2, 5 },
- new long[] { 1, 3, 4 },
- new long[] { 2, 2, 5 },
- new long[] { 2, 3, 6 },
- new long[] { 3, 2, 4 },
- new long[] { 4, 2, 1 },
- new long[] { 4, 2, 6 },
- new long[] { 4, 3, 7 },
- new long[] { 4, 4, 8 },
- new long[] { 4, 5, 8 },
- new long[] { 5, 5, 8 },
- new long[] { 6, 1, 2 },
- new long[] { 7, 2, 1 },
- new long[] { 7, 2, 9 },
- new long[] { 7, 4, 8 },
- new long[] { 7, 5, 9 },
- };
-
- testTuples(expectedTuples, tuples);
-
- // Not subject, predicate, object = 1, 2, 4.
- tuples = new ConstrainedNegationTuples(1l, 2l, 4l, t1,
- new int[] { 0, 1, 2, 3 });
-
- // Expected results
- expectedTuples = new long[][] {
- new long[] { 1, 2, 3 },
- new long[] { 1, 2, 5 },
- new long[] { 1, 3, 4 },
- new long[] { 2, 2, 5 },
- new long[] { 2, 3, 6 },
- new long[] { 3, 2, 4 },
- new long[] { 4, 2, 1 },
- new long[] { 4, 2, 6 },
- new long[] { 4, 3, 7 },
- new long[] { 4, 4, 8 },
- new long[] { 4, 5, 8 },
- new long[] { 5, 5, 8 },
- new long[] { 6, 1, 2 },
- new long[] { 7, 2, 1 },
- new long[] { 7, 2, 9 },
- new long[] { 7, 4, 8 },
- new long[] { 7, 5, 9 },
- };
-
- testTuples(expectedTuples, tuples);
-
- // Not subject, predicate, object = 1, 2, 5.
- tuples = new ConstrainedNegationTuples(1l, 2l, 5l, t1,
- new int[] { 0, 1, 2, 3 });
-
- // Expected results
- expectedTuples = new long[][] {
- new long[] { 1, 2, 3 },
- new long[] { 1, 2, 4 },
- new long[] { 1, 3, 4 },
- new long[] { 2, 2, 5 },
- new long[] { 2, 3, 6 },
- new long[] { 3, 2, 4 },
- new long[] { 4, 2, 1 },
- new long[] { 4, 2, 6 },
- new long[] { 4, 3, 7 },
- new long[] { 4, 4, 8 },
- new long[] { 4, 5, 8 },
- new long[] { 5, 5, 8 },
- new long[] { 6, 1, 2 },
- new long[] { 7, 2, 1 },
- new long[] { 7, 2, 9 },
- new long[] { 7, 4, 8 },
- new long[] { 7, 5, 9 },
- };
-
- testTuples(expectedTuples, tuples);
-
- // Not subject, predicate, object = 7, 5, 9.
- tuples = new ConstrainedNegationTuples(7l, 5l, 9l, t1,
- new int[] { 0, 1, 2, 3});
-
- // Expected results
- expectedTuples = new long[][] {
- new long[] { 1, 2, 3 },
- new long[] { 1, 2, 4 },
- new long[] { 1, 2, 5 },
- new long[] { 1, 3, 4 },
- new long[] { 2, 2, 5 },
- new long[] { 2, 3, 6 },
- new long[] { 3, 2, 4 },
- new long[] { 4, 2, 1 },
- new long[] { 4, 2, 6 },
- new long[] { 4, 3, 7 },
- new long[] { 4, 4, 8 },
- new long[] { 4, 5, 8 },
- new long[] { 5, 5, 8 },
- new long[] { 6, 1, 2 },
- new long[] { 7, 2, 1 },
- new long[] { 7, 2, 9 },
- new long[] { 7, 4, 8 },
- };
-
- testTuples(expectedTuples, tuples);
-
- // Not subject, predicate, object = 7, 5, 7.
- tuples = new ConstrainedNegationTuples(7l, 5l, 7l, t1,
- new int[] { 0, 1, 2, 3 });
-
- // Expected results
- expectedTuples = new long[][] {
- new long[] { 1, 2, 3 },
- new long[] { 1, 2, 4 },
- new long[] { 1, 2, 5 },
- new long[] { 1, 3, 4 },
- new long[] { 2, 2, 5 },
- new long[] { 2, 3, 6 },
- new long[] { 3, 2, 4 },
- new long[] { 4, 2, 1 },
- new long[] { 4, 2, 6 },
- new long[] { 4, 3, 7 },
- new long[] { 4, 4, 8 },
- new long[] { 4, 5, 8 },
- new long[] { 5, 5, 8 },
- new long[] { 6, 1, 2 },
- new long[] { 7, 2, 1 },
- new long[] { 7, 2, 9 },
- new long[] { 7, 4, 8 },
- new long[] { 7, 5, 9 },
- };
-
- assertTrue("Should be unconstrained if not found", tuples.isUnconstrained());
- testTuples(expectedTuples, tuples);
- }
-
- /**
- * Test a constraint negation's results.
- *
- * @throws Exception if query fails when it should have succeeded
- */
- public void testTuples(long[][] expectedTuples, Tuples result)
- throws Exception {
-
- assertTrue("Expected: " + expectedTuples.length + ", " +
- result.getRowCount(), expectedTuples.length == result.getRowCount());
-
- if (result.getRowCount() > 0) {
- result.beforeFirst();
- result.next();
-
- int index = 0;
- do {
-
- long tuple[] = new long[] {
- result.getColumnValue(0),
- result.getColumnValue(1),
- result.getColumnValue(2)
- };
-
- assertTrue("Expected tuple result: " + expectedTuples[index][0] + "," +
- expectedTuples[index][1] + "," +
- expectedTuples[index][2] + " but was: " + tuple[0] + "," + tuple[1] +
- "," + tuple[1],
- expectedTuples[index][0] == tuple[0] &&
- expectedTuples[index][1] == tuple[1] &&
- expectedTuples[index][2] == tuple[2]);
- index++;
- result.next();
- }
- while (index < expectedTuples.length);
-
- assertFalse("Should have no extra results", result.next());
- }
- }
-}
Added: trunk/src/jar/tuples/java/org/mulgara/store/tuples/ReorderAnnotation.java
===================================================================
--- trunk/src/jar/tuples/java/org/mulgara/store/tuples/ReorderAnnotation.java (rev 0)
+++ trunk/src/jar/tuples/java/org/mulgara/store/tuples/ReorderAnnotation.java 2009-02-12 11:45:10 UTC (rev 1480)
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2009 Andrae Muys
+ * Licensed under the Open Software Licence 3.0
+ */
+
+package org.mulgara.store.tuples;
+
+// Java packages
+import java.util.List;
+import java.util.Set;
+
+// Local packages
+import org.mulgara.query.Variable;
+
+/**
+ * Allows a partial order to be specified over a given tuples.
+ * The returned tuples variable ordering will comply with the specified partial-order.
+ *
+ * @author <a href="http://netymon.com/people/staff/andrae">Andrae Muys</a>
+ * @copyright ©2009 <a href="mailto:andrae at netymon.com">Andrae Muys</a>
+ */
+
+public class ReorderAnnotation implements Annotation {
+ public ReorderAnnotation() {
+ }
+
+ /**
+ * Returns a new tuples identical to this tuples, but with variables
+ * reordered compliant with the specified partial order.
+ */
+ public Tuples reorder(List<Set<Variable>> partialOrder) {
+ throw new UnsupportedOperationException("reorder unimplemented");
+ }
+}
Modified: trunk/src/jar/tuples/java/org/mulgara/store/tuples/TuplesOperations.java
===================================================================
--- trunk/src/jar/tuples/java/org/mulgara/store/tuples/TuplesOperations.java 2009-02-07 02:21:08 UTC (rev 1479)
+++ trunk/src/jar/tuples/java/org/mulgara/store/tuples/TuplesOperations.java 2009-02-12 11:45:10 UTC (rev 1480)
@@ -727,44 +727,39 @@
try {
boolean noVariables = (variableList == null) || (variableList.size() == 0);
-
if (tuples.isUnconstrained() || (noVariables && tuples.getRowCardinality() != Cursor.ZERO)) {
-
- if (logger.isDebugEnabled()) logger.debug("returning Unconstrained Tuples.");
-
- return TuplesOperations.unconstrained();
+ return unconstrained();
} else if (tuples.getRowCardinality() == Cursor.ZERO) {
return empty();
- // If the tuples is not unconstrained, and there's no variables in the SELECT
- // and the tuples is a ConstrainedNegationTuples return empty/false.
- } else if ((noVariables) && (tuples instanceof ConstrainedNegationTuples)) {
- return empty();
- } else {
- if (logger.isDebugEnabled()) logger.debug("Projecting to " + variableList);
+ }
- // Perform the actual projection
- Tuples oldTuples = tuples;
- tuples = new UnorderedProjection(tuples, variableList);
- assert tuples != oldTuples;
+ if (logger.isDebugEnabled()) logger.debug("Projecting to " + variableList);
- // Test whether creating an unordered projects has removed variables.
- if (tuples.isUnconstrained()) {
- tuples.close();
- return TuplesOperations.unconstrained();
- }
+ // Perform the actual projection
+ Tuples oldTuples = tuples;
+ tuples = new UnorderedProjection(tuples, variableList);
+ assert tuples != oldTuples;
- // Eliminate any duplicates
- oldTuples = tuples;
- tuples = removeDuplicates(tuples);
- assert tuples != oldTuples;
+ // Test whether creating an unordered projects has removed variables.
+ if (tuples.isUnconstrained()) {
+ tuples.close();
+ return TuplesOperations.unconstrained();
+ }
- if (tuples == oldTuples) logger.warn("removeDuplicates does not change the underlying tuples");
- else oldTuples.close();
+ // Eliminate any duplicates
+ oldTuples = tuples;
+ tuples = removeDuplicates(tuples);
+ assert tuples != oldTuples;
- assert tuples.hasNoDuplicates();
-
- return tuples;
+ if (tuples == oldTuples) {
+ logger.warn("removeDuplicates does not change the underlying tuples");
+ } else {
+ oldTuples.close();
}
+
+ assert tuples.hasNoDuplicates();
+
+ return tuples;
} catch (TuplesException e) {
throw new TuplesException("Couldn't perform projection", e);
}
More information about the Mulgara-svn
mailing list