You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rya.apache.org by mi...@apache.org on 2016/02/07 19:26:04 UTC

[01/16] incubator-rya git commit: RYA-32 Improve how metadata and values are written to Accumulo PCJ tables

Repository: incubator-rya
Updated Branches:
  refs/heads/develop 4b7bd4f4e -> 6cd8aeb71


http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/VarConstExternalProcessorTest.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/VarConstExternalProcessorTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/VarConstExternalProcessorTest.java
deleted file mode 100644
index 6449486..0000000
--- a/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/VarConstExternalProcessorTest.java
+++ /dev/null
@@ -1,490 +0,0 @@
-package mvm.rya.indexing.external.tupleSet;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-import static org.junit.Assert.*;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Set;
-
-import mvm.rya.indexing.external.ExternalProcessor;
-import mvm.rya.indexing.external.tupleSet.ExternalProcessorTest.ExternalTupleVstor;
-
-import org.junit.Assert;
-import org.junit.Test;
-import org.openrdf.query.algebra.Projection;
-import org.openrdf.query.algebra.QueryModelNode;
-import org.openrdf.query.algebra.StatementPattern;
-import org.openrdf.query.algebra.TupleExpr;
-import org.openrdf.query.algebra.helpers.StatementPatternCollector;
-import org.openrdf.query.parser.ParsedQuery;
-import org.openrdf.query.parser.sparql.SPARQLParser;
-
-import com.google.common.collect.Sets;
-
-public class VarConstExternalProcessorTest {
-
-    
-    
-    
-    String q15 = ""//
-            + "SELECT ?a ?b ?c ?d ?e ?f ?q " //
-            + "{" //
-            + " GRAPH ?x { " //
-            + "  ?a a ?b ."//
-            + "  ?b <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-            + "  ?d <uri:talksTo> ?e . "//
-            + "  FILTER ( ?e < ?f && (?a > ?b || ?c = ?d) ). " //
-            + "  FILTER(bound(?f) && sameTerm(?a,?b)&&bound(?q)). " //
-            + "  ?b a ?q ."//
-            + "     }"//
-            + "}";//
-    
-    
-    
-    
-    String q17 = ""//
-            + "SELECT ?j ?k ?l ?m ?n ?o " //
-            + "{" //
-            + " GRAPH ?z { " //
-            + "  ?l a ?m. " //
-            + "  ?n a ?o. " //
-            + "  ?j <uri:talksTo> ?k . "//
-            + "  FILTER ( ?k < ?l && (?m > ?n || ?o = ?j) ). " //
-            + "     }"//
-            + "}";//
-    
-    String q18 = ""//
-            + "SELECT ?r ?s ?t ?u " //
-            + "{" //
-            + " GRAPH ?q { " //
-            + "  FILTER(bound(?r) && sameTerm(?s,?t)&&bound(?u)). " //
-            + "  ?t a ?u ."//
-            + "  ?s a ?r ."//
-            + "     }"//
-            + "}";//
-    
-    
-    
-    String q19 = ""//
-            + "SELECT ?a ?c ?d ?f ?q " //
-            + "{" //
-            + " GRAPH ?x { " //
-            + "  ?f a ?a ."//
-            + " \"3\" a ?c . "//
-            + "  ?d <uri:talksTo> \"5\" . "//
-            + "  FILTER ( \"5\" < ?f && (?a > \"3\" || ?c = ?d) ). " //
-            + "  FILTER(bound(?f) && sameTerm(?a,\"3\") && bound(?q)). " //
-            + "  \"3\" a ?q ."//
-            + "  ?a a ?f ."//
-            + "     }"//
-            + "}";//
-    
-   
-    
-    
-    
-    
-    String q21 = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
-            + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
-            + "SELECT ?feature ?point " //
-            + "{" //
-            + "  ?feature a geo:Feature . "//
-            + "  ?feature geo:hasGeometry ?point . "//
-            + "  ?point a geo:Point . "//
-            + "  ?point geo:asWKT \"wkt\" . "//
-            + "  FILTER(geof:sfWithin(\"wkt\", \"Polygon\")) " //
-            + "}";//
-    
-    
-     String q22 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
-             + "SELECT ?person " //
-             + "{" //
-             + "  ?person a <http://example.org/ontology/Person> . "//
-             + "  ?person <http://www.w3.org/2000/01/rdf-schema#label> \"sally\" . "//
-             + "  ?person <http://www.w3.org/2000/01/rdf-schema#label> \"john\" . "//
-             + "  FILTER(fts:text(\"sally\", \"bob\")) . " //
-             + "  FILTER(fts:text(\"john\", \"harry\"))  " //
-             + "  ?person <uri:hasName> \"bob\". "//
-             + "  ?person <uri:hasName> \"harry\". "//
-             + "}";//
-     
-     
-     String q23 = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
-                + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
-                + "SELECT ?a ?b ?c ?d " //
-                + "{" //
-                + "  ?a a geo:Feature . "//
-                + "  ?b a geo:Point . "//
-                + "  ?b geo:asWKT ?c . "//
-                + "  FILTER(geof:sfWithin(?c, ?d)) " //
-                + "}";//
-     
-     
-     String q24 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
-             + "SELECT ?f ?g ?h" //
-             + "{" //
-             + "  ?f <http://www.w3.org/2000/01/rdf-schema#label> ?g . "//
-             + "  FILTER(fts:text(?g,?h)).  " //
-             + " ?f <uri:hasName> ?h. " //
-             + "}";//
-     
-
-     String q25 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
-             + "SELECT ?person ?point" //
-             + "{" //
-             + "  ?person <http://www.w3.org/2000/01/rdf-schema#label> \"label\" . "//
-             + "  FILTER(fts:text(\"label\", \"bob\")) . " //
-             + "  ?person <uri:hasName> \"bob\" . " //
-             + "  ?person a ?point. " //
-             + "  \"bob\" a <http://example.org/ontology/Person> . "//
-             + "  ?person <http://www.w3.org/2000/01/rdf-schema#commentmatch> \"comment\" . "//
-             + "  FILTER((?person > ?point) || (?person = \"comment\")). "
-             + "  FILTER(fts:text(\"comment\", \"bob\"))  " //
-             + "}";//
-     
-     
-     String q26 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
-             + "SELECT ?a ?b ?c ?d " //
-             + "{" //
-             + "  ?a a ?c. " //
-             + "  ?d a <http://example.org/ontology/Person> . "//
-             + "  ?a <http://www.w3.org/2000/01/rdf-schema#commentmatch> ?b . "//
-             + "  FILTER((?a > ?c) || (?a = ?b)). "
-             + "  FILTER(fts:text(?b, ?d)) . " //
-             + "}";//
-     
-     
-     
-     String q27 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
-             + "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
-             + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
-             + "SELECT ?person ?feature ?point " //
-             + "{" //
-             + "  ?person <http://www.w3.org/2000/01/rdf-schema#label> \"label\" . "//
-             + "  FILTER(fts:text(\"label\", \"bob\")) . " //
-             + "  ?person <uri:hasName> \"bob\" . " //
-             + "  ?person a ?point. " //
-             + "  \"bob\" a <http://example.org/ontology/Person> . "//
-             + "  ?person <http://www.w3.org/2000/01/rdf-schema#commentmatch> \"comment\" . "//
-             + "  FILTER((?person > ?point) || (?person = \"comment\")). "
-             + "  FILTER(fts:text(\"comment\", \"bob\"))  " //
-             + "  ?feature a geo:Feature . "//
-             + "  ?point a geo:Point . "//
-             + "  ?point geo:asWKT \"wkt\" . "//
-             + "  FILTER(geof:sfWithin(\"wkt\", \"Polygon\")) " //
-             + "}";//
-     
-     
-    
-    
-     String q28 = ""//
-             + "SELECT ?m ?n " //
-             + "{" //
-             + "  FILTER(?m IN (1,2,3) && ?n NOT IN(5,6,7)). " //
-             + "  ?n <http://www.w3.org/2000/01/rdf-schema#label> ?m. "//
-             + "}";//
-    
-    
-    
-    
-    
-    
-    
-    @Test
-    public void testContextFilterFourIndex() throws Exception {
-
-        SPARQLParser parser1 = new SPARQLParser();
-        SPARQLParser parser3 = new SPARQLParser();
-        SPARQLParser parser4 = new SPARQLParser();
-
-        ParsedQuery pq1 = parser1.parseQuery(q19, null);
-        ParsedQuery pq3 = parser3.parseQuery(q17, null);
-        ParsedQuery pq4 = parser4.parseQuery(q18, null);
-   
-
-        System.out.println("Query is " + pq1.getTupleExpr());
-        System.out.println("Indexes are " + pq3.getTupleExpr()+ " , " +pq4.getTupleExpr());
-        
- 
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(new Projection(pq3.getTupleExpr()));
-        SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(new Projection(pq4.getTupleExpr()));
-        
-
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-       
-        list.add(extTup3);
-        list.add(extTup2);
-     
-
-        ExternalProcessor processor = new ExternalProcessor(list);
-        
-        TupleExpr tup = processor.process(pq1.getTupleExpr());
-
-        System.out.println("Processed query is " + tup);
-          
-        Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector.process(pq1.getTupleExpr()));
-        
-        ExternalTupleVstor eTup = new ExternalTupleVstor();
-        tup.visit(eTup);
-        Set<QueryModelNode> eTupSet =  eTup.getExtTup();
-        
-        Assert.assertEquals(2, eTupSet.size());
-        
-        Set<StatementPattern> set = Sets.newHashSet();
-        
-        for (QueryModelNode s : eTupSet) {
-            Set<StatementPattern> tempSet = Sets.newHashSet(StatementPatternCollector.process(((ExternalTupleSet) s)
-                    .getTupleExpr()));
-            set.addAll(tempSet);
-
-        }
-        
-        
-        Assert.assertTrue(qSet.containsAll(set));
-    }
-    
-    
-    
-    
-    @Test
-    public void testGeoIndexFunction() throws Exception {
-
-        SPARQLParser parser1 = new SPARQLParser();
-        SPARQLParser parser2 = new SPARQLParser();
-
-        ParsedQuery pq1 = parser1.parseQuery(q21, null);
-        ParsedQuery pq2 = parser2.parseQuery(q23, null);
-
-        System.out.println("Query is " + pq1.getTupleExpr());
-        System.out.println("Index is " + pq2.getTupleExpr());
-
-        
-        SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-        
-
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-        list.add(extTup);
-
-        
-        ExternalProcessor processor = new ExternalProcessor(list);
-        
-        TupleExpr tup = processor.process(pq1.getTupleExpr());
-
-        System.out.println("Processed query is " + tup);
-        
-        Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector.process(pq1.getTupleExpr()));
-        
-        
-        ExternalTupleVstor eTup = new ExternalTupleVstor();
-        tup.visit(eTup);
-        Set<QueryModelNode> eTupSet =  eTup.getExtTup();
-        
-        Set<StatementPattern> set = Sets.newHashSet();
-        
-        Assert.assertEquals(1, eTupSet.size());
-        
-        for (QueryModelNode s : eTupSet) {
-            Set<StatementPattern> tempSet = Sets.newHashSet(StatementPatternCollector.process(((ExternalTupleSet) s)
-                    .getTupleExpr()));
-            set.addAll(tempSet);
-
-        }
-        
-        
-        
-        Assert.assertTrue(qSet.containsAll(set));
-
-    }
-    
-    
-    
-    @Test
-    public void testFreeTestIndexFunction() throws Exception {
-
-        SPARQLParser parser1 = new SPARQLParser();
-        SPARQLParser parser2 = new SPARQLParser();
-
-        ParsedQuery pq1 = parser1.parseQuery(q22, null);
-        ParsedQuery pq2 = parser2.parseQuery(q24, null);
-
-        System.out.println("Query is " + pq1.getTupleExpr());
-        System.out.println("Index is " + pq2.getTupleExpr());
-
-        
-        SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-        
-
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-        list.add(extTup);
-
-        ExternalProcessor processor = new ExternalProcessor(list);
-        
-        TupleExpr tup = processor.process(pq1.getTupleExpr());
-
-        System.out.println("Processed query is " + tup);
-        
-        Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector.process(pq1.getTupleExpr()));
-        
-        
-        ExternalTupleVstor eTup = new ExternalTupleVstor();
-        tup.visit(eTup);
-        Set<QueryModelNode> eTupSet =  eTup.getExtTup();
-        
-        Set<StatementPattern> set = Sets.newHashSet();
-        
-        Assert.assertEquals(2, eTupSet.size());
-        
-        for (QueryModelNode s : eTupSet) {
-            Set<StatementPattern> tempSet = Sets.newHashSet(StatementPatternCollector.process(((ExternalTupleSet) s)
-                    .getTupleExpr()));
-            set.addAll(tempSet);
-
-        }
-        
-        
-        Assert.assertTrue(qSet.containsAll(set));
-
-    }
-    
-    
-    @Test
-    public void testThreeIndexGeoFreeCompareFilterMix() throws Exception {
-
-        SPARQLParser parser1 = new SPARQLParser();
-        SPARQLParser parser2 = new SPARQLParser();
-        SPARQLParser parser3 = new SPARQLParser();
-
-        ParsedQuery pq1 = parser1.parseQuery(q25, null);
-        ParsedQuery pq2 = parser2.parseQuery(q24, null);
-        ParsedQuery pq3 = parser3.parseQuery(q26, null);
-
-        System.out.println("Query is " + pq1.getTupleExpr());
-        System.out.println("Indexes are " + pq2.getTupleExpr() + " and " + pq3.getTupleExpr());
-
-        
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(new Projection(pq3.getTupleExpr()));
-
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-        list.add(extTup1);
-        list.add(extTup2);
-
-        
-        ExternalProcessor processor = new ExternalProcessor(list);
-        
-        TupleExpr tup = processor.process(pq1.getTupleExpr());
-
-        System.out.println("Processed query is " + tup);
-        
-        Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector.process(pq1.getTupleExpr()));
-        
-        ExternalTupleVstor eTup = new ExternalTupleVstor();
-        tup.visit(eTup);
-        Set<QueryModelNode> eTupSet =  eTup.getExtTup();
-        Set<StatementPattern> set = Sets.newHashSet();
-        
-        Assert.assertEquals(2, eTupSet.size());
-        
-        for (QueryModelNode s : eTupSet) {
-            Set<StatementPattern> tempSet = Sets.newHashSet(StatementPatternCollector.process(((ExternalTupleSet) s)
-                    .getTupleExpr()));
-            set.addAll(tempSet);
-
-        }
-        
-        
-        Assert.assertTrue(qSet.containsAll(set));
-
-    }
-    
-    
-    
-    
-    
-    @Test
-    public void testFourIndexGeoFreeCompareFilterMix() throws Exception {
-
-        SPARQLParser parser1 = new SPARQLParser();
-        SPARQLParser parser2 = new SPARQLParser();
-        SPARQLParser parser3 = new SPARQLParser();
-        SPARQLParser parser4 = new SPARQLParser();
-      
-
-        ParsedQuery pq1 = parser1.parseQuery(q27, null);
-        ParsedQuery pq2 = parser2.parseQuery(q23, null);
-        ParsedQuery pq3 = parser3.parseQuery(q26, null);
-        ParsedQuery pq4 = parser4.parseQuery(q24, null);
-        
-        System.out.println("Query is " + pq1.getTupleExpr());
-        System.out.println("Indexes are " + pq2.getTupleExpr() + " , " + pq3.getTupleExpr() + " , " + pq4.getTupleExpr());
-
-        
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(new Projection(pq3.getTupleExpr()));
-        SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(new Projection(pq4.getTupleExpr()));
-
-
-
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-
-        list.add(extTup1);
-        list.add(extTup2);
-        list.add(extTup3);
-
-        
-        ExternalProcessor processor = new ExternalProcessor(list);
-        
-        TupleExpr tup = processor.process(pq1.getTupleExpr());
-
-        System.out.println("Processed query is " + tup);
-        
-        Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector.process(pq1.getTupleExpr()));
-        
-        ExternalTupleVstor eTup = new ExternalTupleVstor();
-        tup.visit(eTup);
-        Set<QueryModelNode> eTupSet =  eTup.getExtTup();
-        Set<StatementPattern> set = Sets.newHashSet();
-        
-        Assert.assertEquals(3, eTupSet.size());
-        
-        for (QueryModelNode s : eTupSet) {
-            Set<StatementPattern> tempSet = Sets.newHashSet(StatementPatternCollector.process(((ExternalTupleSet) s)
-                    .getTupleExpr()));
-            set.addAll(tempSet);
-
-        }
-        
-        
-        Assert.assertTrue(qSet.containsAll(set));
-
-
-
-    }
-    
-    
-    
-    
-    
-    
-    
-}

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexingExample/src/main/java/RyaDirectExample.java
----------------------------------------------------------------------
diff --git a/extras/indexingExample/src/main/java/RyaDirectExample.java b/extras/indexingExample/src/main/java/RyaDirectExample.java
index 0d2df3f..db1ffe3 100644
--- a/extras/indexingExample/src/main/java/RyaDirectExample.java
+++ b/extras/indexingExample/src/main/java/RyaDirectExample.java
@@ -6,9 +6,9 @@
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -17,13 +17,21 @@
  * under the License.
  */
 
-
 import java.util.List;
 
+import mvm.rya.accumulo.AccumuloRdfConfiguration;
+import mvm.rya.api.RdfCloudTripleStoreConfiguration;
+import mvm.rya.api.persist.RyaDAOException;
+import mvm.rya.indexing.RyaSailFactory;
+import mvm.rya.indexing.accumulo.ConfigUtils;
+import mvm.rya.indexing.accumulo.geo.GeoConstants;
+import mvm.rya.indexing.external.tupleSet.PcjTables;
+import mvm.rya.indexing.external.tupleSet.PcjTables.PcjException;
+import mvm.rya.indexing.external.tupleSet.PcjTables.PcjVarOrderFactory;
+
 import org.apache.accumulo.core.client.AccumuloException;
 import org.apache.accumulo.core.client.AccumuloSecurityException;
 import org.apache.accumulo.core.client.Connector;
-import org.apache.accumulo.core.client.MutationsRejectedException;
 import org.apache.accumulo.core.client.TableExistsException;
 import org.apache.accumulo.core.client.TableNotFoundException;
 import org.apache.accumulo.core.client.mock.MockInstance;
@@ -51,751 +59,768 @@ import org.openrdf.repository.RepositoryException;
 import org.openrdf.repository.sail.SailRepository;
 import org.openrdf.repository.sail.SailRepositoryConnection;
 import org.openrdf.sail.Sail;
-import org.openrdf.sail.SailException;
 
-import mvm.rya.accumulo.AccumuloRdfConfiguration;
-import mvm.rya.api.RdfCloudTripleStoreConfiguration;
-import mvm.rya.indexing.RyaSailFactory;
-import mvm.rya.indexing.accumulo.ConfigUtils;
-import mvm.rya.indexing.accumulo.geo.GeoConstants;
-import mvm.rya.indexing.external.tupleSet.AccumuloIndexSet;
+import com.google.common.base.Optional;
 
 public class RyaDirectExample {
-    private static final Logger log = Logger.getLogger(RyaDirectExample.class);
-
-    //
-    // Connection configuration parameters
-    //
-
-    private static final boolean USE_MOCK_INSTANCE = true;
-    private static final boolean PRINT_QUERIES = true;
-    private static final String INSTANCE = "instance";
-    private static final String RYA_TABLE_PREFIX = "x_test_triplestore_";
-    private static final String AUTHS = "";
-
-
-
-    public static void main(String[] args) throws Exception {
-        Configuration conf = getConf();
-        conf.setBoolean(ConfigUtils.DISPLAY_QUERY_PLAN, PRINT_QUERIES);
-
-        log.info("Creating the tables as root.");
-//        createTables(addRootConf(conf), conf);
-
-        SailRepository repository = null;
-        SailRepositoryConnection conn = null;
-
-        try {
-            log.info("Connecting to Indexing Sail Repository.");
-
-            Sail extSail = RyaSailFactory.getInstance(conf);
-            repository = new SailRepository(extSail);
-            repository.initialize();
-            conn = repository.getConnection();
-
-            createPCJ(conn);
-
-            long start = System.currentTimeMillis();
-            log.info("Running SPARQL Example: Add and Delete");
-            testAddAndDelete(conn);
-            log.info("Running SAIL/SPARQL Example: PCJ Search");
-            testPCJSearch(conn);
-            log.info("Running SAIL/SPARQL Example: Add and Temporal Search");
-            testAddAndTemporalSearchWithPCJ(conn);
-            log.info("Running SAIL/SPARQL Example: Add and Free Text Search with PCJ");
-            testAddAndFreeTextSearchWithPCJ(conn);
-            log.info("Running SPARQL Example: Add Point and Geo Search with PCJ");
-            testAddPointAndWithinSearchWithPCJ(conn);
-            log.info("Running SPARQL Example: Temporal, Freetext, and Geo Search");
-            testTemporalFreeGeoSearch(conn);
-            log.info("Running SPARQL Example: Geo, Freetext, and PCJ Search");
-            testGeoFreetextWithPCJSearch(conn);
-            log.info("Running SPARQL Example: Delete Temporal Data");
-            testDeleteTemporalData(conn);
-            log.info("Running SPARQL Example: Delete Free Text Data");
-            testDeleteFreeTextData(conn);
-            log.info("Running SPARQL Example: Delete Geo Data");
-            testDeleteGeoData(conn);
-
-            log.info("TIME: " + (System.currentTimeMillis() - start) / 1000.);
-        } finally {
-            log.info("Shutting down");
-            closeQuietly(conn);
-            closeQuietly(repository);
-        }
-    }
-
-    private static void closeQuietly(SailRepository repository) {
-        if (repository != null) {
-            try {
-                repository.shutDown();
-            } catch (RepositoryException e) {
-                // quietly absorb this exception
-            }
-        }
-    }
-
-    private static void closeQuietly(SailRepositoryConnection conn) {
-        if (conn != null) {
-            try {
-                conn.close();
-            } catch (RepositoryException e) {
-                // quietly absorb this exception
-            }
-        }
-    }
-
-    private static Configuration getConf() {
-
-        AccumuloRdfConfiguration conf = new AccumuloRdfConfiguration();
-
-        conf.setBoolean(ConfigUtils.USE_MOCK_INSTANCE, USE_MOCK_INSTANCE);
-        conf.set(ConfigUtils.USE_PCJ, "true");
-        conf.set(ConfigUtils.USE_GEO, "true");
-        conf.set(ConfigUtils.USE_FREETEXT, "true");
-        conf.set(ConfigUtils.USE_TEMPORAL, "true");
-        conf.set(RdfCloudTripleStoreConfiguration.CONF_TBL_PREFIX, RYA_TABLE_PREFIX);
-        conf.set(ConfigUtils.CLOUDBASE_USER, "root");
-        conf.set(ConfigUtils.CLOUDBASE_PASSWORD, "");
-        conf.set(ConfigUtils.CLOUDBASE_INSTANCE, INSTANCE);
-        conf.setInt(ConfigUtils.NUM_PARTITIONS, 3);
-        conf.set(ConfigUtils.CLOUDBASE_AUTHS, AUTHS);
-
-        // only geo index statements with geo:asWKT predicates
-        conf.set(ConfigUtils.GEO_PREDICATES_LIST, GeoConstants.GEO_AS_WKT.stringValue());
-        return conf;
-    }
-
-    public static void testAddAndDelete(SailRepositoryConnection conn) throws MalformedQueryException,
-            RepositoryException, UpdateExecutionException, QueryEvaluationException, TupleQueryResultHandlerException,
-            AccumuloException, AccumuloSecurityException, TableNotFoundException {
-
-        // Add data
-        String query = "INSERT DATA\n"//
-                + "{ GRAPH <http://updated/test> {\n"//
-                + "  <http://acme.com/people/Mike> " //
-                + "       <http://acme.com/actions/likes> \"A new book\" ;\n"//
-                + "       <http://acme.com/actions/likes> \"Avocados\" .\n" + "} }";
-
-        log.info("Performing Query");
-
-        Update update = conn.prepareUpdate(QueryLanguage.SPARQL, query);
-        update.execute();
-
-        query = "select ?p ?o { GRAPH <http://updated/test> {<http://acme.com/people/Mike> ?p ?o . }}";
-        CountingResultHandler resultHandler = new CountingResultHandler();
-        TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
-        tupleQuery.evaluate(resultHandler);
-        log.info("Result count : " + resultHandler.getCount());
-
-        Validate.isTrue(resultHandler.getCount() == 2);
-        resultHandler.resetCount();
-
-        // Delete Data
-        query = "DELETE DATA\n" //
-                + "{ GRAPH <http://updated/test> {\n"
-                + "  <http://acme.com/people/Mike> <http://acme.com/actions/likes> \"A new book\" ;\n"
-                + "   <http://acme.com/actions/likes> \"Avocados\" .\n" + "}}";
-
-        update = conn.prepareUpdate(QueryLanguage.SPARQL, query);
-        update.execute();
-
-        query = "select ?p ?o { GRAPH <http://updated/test> {<http://acme.com/people/Mike> ?p ?o . }}";
-        tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
-        tupleQuery.evaluate(resultHandler);
-        log.info("Result count : " + resultHandler.getCount());
-
-        Validate.isTrue(resultHandler.getCount() == 0);
-    }
-
-
-    private static void testPCJSearch(SailRepositoryConnection conn) throws Exception {
-
-        String queryString;
-        TupleQuery tupleQuery;
-        CountingResultHandler tupleHandler;
-
-     // ///////////// search for bob
-        queryString = "SELECT ?e ?c ?l ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "}";//
-
-        tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
-        tupleHandler = new CountingResultHandler();
-        tupleQuery.evaluate(tupleHandler);
-        log.info("Result count : " + tupleHandler.getCount());
-        Validate.isTrue(tupleHandler.getCount() == 1);
-
-     // ///////////// search for bob
-        queryString = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
-                + "SELECT ?e ?c ?l ?o " //
-                + "{" //
-                + "  ?c a ?e . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "}";//
-
-        tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
-        tupleHandler = new CountingResultHandler();
-        tupleQuery.evaluate(tupleHandler);
-        log.info("Result count : " + tupleHandler.getCount());
-        Validate.isTrue(tupleHandler.getCount() == 2);
-
-    }
-
-
-
-
-    private static void testAddAndTemporalSearchWithPCJ(SailRepositoryConnection conn) throws Exception {
-
-        // create some resources and literals to make statements out of
-
-        String sparqlInsert = "PREFIX time: <http://www.w3.org/2006/time#>\n"
-                + "INSERT DATA {\n" //
-                + "_:eventz       a       time:Instant ;\n"
-                + "     time:inXSDDateTime '2001-01-01T01:01:01-08:00' ;\n" //  one second
-                + "     time:inXSDDateTime '2001-01-01T04:01:02.000-05:00'^^<http://www.w3.org/2001/XMLSchema#dateTime> ;\n" //   2 seconds
-                + "     time:inXSDDateTime \"2001-01-01T01:01:03-08:00\" ;\n" //   3 seconds
-                + "     time:inXSDDateTime '2001-01-01T01:01:04-08:00' ;\n" //   4 seconds
-                + "     time:inXSDDateTime '2001-01-01T09:01:05Z' ;\n"
-                + "     time:inXSDDateTime '2006-01-01' ;\n"
-                + "     time:inXSDDateTime '2007-01-01' ;\n"
-                + "     time:inXSDDateTime '2008-01-01' ; .\n"
-                + "}";
-
-        Update update = conn.prepareUpdate(QueryLanguage.SPARQL, sparqlInsert);
-        update.execute();
-
-
-        // Find all stored dates.
-        String queryString = "PREFIX time: <http://www.w3.org/2006/time#> \n"//
-                + "PREFIX tempo: <tag:rya-rdf.org,2015:temporal#> \n"//
-                + "SELECT ?event ?time \n" //
-                + "WHERE { \n"
-                + "  ?event time:inXSDDateTime ?time . \n"//
-                + "  FILTER(tempo:after(?time, '2001-01-01T01:01:03-08:00') ) \n"// after 3 seconds
-                + "}";//
-
-
-
-        TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
-        CountingResultHandler tupleHandler = new CountingResultHandler();
-        tupleQuery.evaluate(tupleHandler);
-        log.info("Result count : " + tupleHandler.getCount());
-        Validate.isTrue(tupleHandler.getCount() == 5);
-
-        // Find all stored dates.
-        queryString = "PREFIX time: <http://www.w3.org/2006/time#> \n"//
-                + "PREFIX tempo: <tag:rya-rdf.org,2015:temporal#> \n"//
-                + "SELECT ?event ?time \n" //
-                + "WHERE { \n"
-                + "  ?event time:inXSDDateTime ?time . \n"//
-                + "  ?event a  time:Instant . \n"//
-                + "  FILTER(tempo:after(?time, '2001-01-01T01:01:03-08:00') ) \n"// after 3 seconds
-                + "}";//
-
-
-
-        tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
-        tupleHandler = new CountingResultHandler();
-        tupleQuery.evaluate(tupleHandler);
-        log.info("Result count : " + tupleHandler.getCount());
-        Validate.isTrue(tupleHandler.getCount() == 5);
-
-
-        // Find all stored dates.
-        queryString = "PREFIX time: <http://www.w3.org/2006/time#> \n"//
-                + "PREFIX tempo: <tag:rya-rdf.org,2015:temporal#> \n"//
-                + "SELECT ?event ?time ?e ?c ?l ?o \n" //
-                + "WHERE { \n"
-                + "  ?e a ?c . \n"//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . \n"//
-                + "  ?e <uri:talksTo> ?o . \n"//
-                + "  ?event a  time:Instant . \n"//
-                + "  ?event time:inXSDDateTime ?time . \n"//
-                + "  FILTER(tempo:after(?time, '2001-01-01T01:01:03-08:00') ) \n"// after 3 seconds
-                + "}";//
-
-        tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
-        tupleHandler = new CountingResultHandler();
-        tupleQuery.evaluate(tupleHandler);
-        log.info("Result count : " + tupleHandler.getCount());
-        Validate.isTrue(tupleHandler.getCount() == 5);
-    }
-
-
-    private static void testAddAndFreeTextSearchWithPCJ(SailRepositoryConnection conn) throws Exception {
-        // add data to the repository using the SailRepository add methods
-        ValueFactory f = conn.getValueFactory();
-        URI person = f.createURI("http://example.org/ontology/Person");
-
-        String uuid;
-
-        uuid = "urn:people:alice";
-        conn.add(f.createURI(uuid), RDF.TYPE, person);
-        conn.add(f.createURI(uuid), RDFS.LABEL, f.createLiteral("Alice Palace Hose", f.createURI("xsd:string")));
-
-        uuid = "urn:people:bobss";
-        conn.add(f.createURI(uuid), RDF.TYPE, person);
-        conn.add(f.createURI(uuid), RDFS.LABEL, f.createLiteral("Bob Snob Hose", "en"));
-
-        String queryString;
-        TupleQuery tupleQuery;
-        CountingResultHandler tupleHandler;
-
-        // ///////////// search for alice
-        queryString = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
-                + "SELECT ?person ?match ?e ?c ?l ?o " //
-                + "{" //
-                + "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . "//
-                + "  FILTER(fts:text(?match, \"pal*\")) " //
-                + "}";//
-        tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
-        tupleHandler = new CountingResultHandler();
-        tupleQuery.evaluate(tupleHandler);
-        log.info("Result count : " + tupleHandler.getCount());
-        Validate.isTrue(tupleHandler.getCount() == 1);
-
-
-        // ///////////// search for alice and bob
-        queryString = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
-                + "SELECT ?person ?match " //
-                + "{" //
-                + "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . "//
-                  + "  ?person a <http://example.org/ontology/Person> . "//
-                + "  FILTER(fts:text(?match, \"(alice | bob) *SE\")) " //
-                + "}";//
-        tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
-        tupleHandler = new CountingResultHandler();
-        tupleQuery.evaluate(tupleHandler);
-        log.info("Result count : " + tupleHandler.getCount());
-        Validate.isTrue(tupleHandler.getCount() == 2);
-
-     // ///////////// search for alice and bob
-        queryString = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
-                + "SELECT ?person ?match " //
-                + "{" //
-                + "  ?person a <http://example.org/ontology/Person> . "//
-                + "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . "//
-                + "  FILTER(fts:text(?match, \"(alice | bob) *SE\")) " //
-                + "  FILTER(fts:text(?match, \"pal*\")) " //
-                + "}";//
-        tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
-        tupleHandler = new CountingResultHandler();
-        tupleQuery.evaluate(tupleHandler);
-        log.info("Result count : " + tupleHandler.getCount());
-        Validate.isTrue(tupleHandler.getCount() == 1);
-
-
-        // ///////////// search for bob
-        queryString = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
-                + "SELECT ?person ?match ?e ?c ?l ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?person a <http://example.org/ontology/Person> . "//
-                + "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . "//
-                + "  FILTER(fts:text(?match, \"!alice & hose\")) " //
-                + "}";//
-
-        tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
-        tupleHandler = new CountingResultHandler();
-        tupleQuery.evaluate(tupleHandler);
-        log.info("Result count : " + tupleHandler.getCount());
-        Validate.isTrue(tupleHandler.getCount() == 1);
-    }
-
-
-
-    private static void testAddPointAndWithinSearchWithPCJ(SailRepositoryConnection conn) throws Exception {
-
-        String update = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
-                + "INSERT DATA { " //
-                + "  <urn:feature> a geo:Feature ; " //
-                + "    geo:hasGeometry [ " //
-                + "      a geo:Point ; " //
-                + "      geo:asWKT \"Point(-77.03524 38.889468)\"^^geo:wktLiteral "//
-                + "    ] . " //
-                + "}";
-
-        Update u = conn.prepareUpdate(QueryLanguage.SPARQL, update);
-        u.execute();
-
-        String queryString;
-        TupleQuery tupleQuery;
-        CountingResultHandler tupleHandler;
-
-        // point outside search ring
-        queryString = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
-                + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
-                + "SELECT ?feature ?point ?wkt " //
-                + "{" //
-                + "  ?feature a geo:Feature . "//
-                + "  ?feature geo:hasGeometry ?point . "//
-                + "  ?point a geo:Point . "//
-                + "  ?point geo:asWKT ?wkt . "//
-                + "  FILTER(geof:sfWithin(?wkt, \"POLYGON((-77 39, -76 39, -76 38, -77 38, -77 39))\"^^geo:wktLiteral)) " //
-                + "}";//
-        tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
-        tupleHandler = new CountingResultHandler();
-        tupleQuery.evaluate(tupleHandler);
-        log.info("Result count : " + tupleHandler.getCount());
-        Validate.isTrue(tupleHandler.getCount() == 0);
-
-
-        // point inside search ring
-        queryString = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
-                + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
-                + "SELECT ?feature ?point ?wkt ?e ?l ?o" //
-                + "{" //
-                + "  ?feature a ?e . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?feature a geo:Feature . "//
-                + "  ?feature geo:hasGeometry ?point . "//
-                + "  ?point a geo:Point . "//
-                + "  ?point geo:asWKT ?wkt . "//
-                + "  FILTER(geof:sfWithin(?wkt, \"POLYGON((-78 39, -77 39, -77 38, -78 38, -78 39))\"^^geo:wktLiteral)) " //
-                + "}";//
-
-        tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
-        tupleHandler = new CountingResultHandler();
-        tupleQuery.evaluate(tupleHandler);
-        log.info("Result count : " + tupleHandler.getCount());
-        Validate.isTrue(tupleHandler.getCount() == 1);
-
-
-        // point inside search ring with Pre-Computed Join
-        queryString = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
-                + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
-                + "SELECT ?feature ?point ?wkt ?e ?l ?o" //
-                + "{" //
-                + "  ?feature a ?e . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?feature a geo:Feature . "//
-                + "  ?feature geo:hasGeometry ?point . "//
-                + "  ?point a geo:Point . "//
-                + "  ?point geo:asWKT ?wkt . "//
-                + "  FILTER(geof:sfWithin(?wkt, \"POLYGON((-78 39, -77 39, -77 38, -78 38, -78 39))\"^^geo:wktLiteral)) " //
-                + "}";//
-
-        tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
-        tupleHandler = new CountingResultHandler();
-        tupleQuery.evaluate(tupleHandler);
-        log.info("Result count : " + tupleHandler.getCount());
-        Validate.isTrue(tupleHandler.getCount() >= 1); // may see points from during previous runs
-
-        // point outside search ring with PCJ
-        queryString = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
-                + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
-                + "SELECT ?feature ?point ?wkt ?e ?l ?o " //
-                + "{" //
-                + "  ?feature a ?e . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?feature a geo:Feature . "//
-                + "  ?feature geo:hasGeometry ?point . "//
-                + "  ?point a geo:Point . "//
-                + "  ?point geo:asWKT ?wkt . "//
-                + "  FILTER(geof:sfWithin(?wkt, \"POLYGON((-77 39, -76 39, -76 38, -77 38, -77 39))\"^^geo:wktLiteral)) " //
-                + "}";//
-        tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
-        tupleHandler = new CountingResultHandler();
-        tupleQuery.evaluate(tupleHandler);
-        log.info("Result count : " + tupleHandler.getCount());
-        Validate.isTrue(tupleHandler.getCount() == 0);
-
-        // point inside search ring with different Pre-Computed Join
-        queryString = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
-                + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
-                + "SELECT ?feature ?point ?wkt ?e ?c ?l ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?feature a geo:Feature . "//
-                + "  ?feature geo:hasGeometry ?point . "//
-                + "  ?point a geo:Point . "//
-                + "  ?point geo:asWKT ?wkt . "//
-                + "  FILTER(geof:sfWithin(?wkt, \"POLYGON((-78 39, -77 39, -77 38, -78 38, -78 39))\"^^geo:wktLiteral)) " //
-                + "}";//
-        tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
-        tupleHandler = new CountingResultHandler();
-        tupleQuery.evaluate(tupleHandler);
-        log.info("Result count : " + tupleHandler.getCount());
-        Validate.isTrue(tupleHandler.getCount() == 1);
-    }
-
-
-    private static void testTemporalFreeGeoSearch(SailRepositoryConnection conn) throws MalformedQueryException,
-    RepositoryException, UpdateExecutionException, TupleQueryResultHandlerException, QueryEvaluationException {
-
-
-        String queryString;
-        TupleQuery tupleQuery;
-        CountingResultHandler tupleHandler;
-
-        // ring containing point
-        queryString = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
-                + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
-                + "PREFIX time: <http://www.w3.org/2006/time#> "//
-                + "PREFIX tempo: <tag:rya-rdf.org,2015:temporal#> "//
-                + "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
-                + "SELECT ?feature ?point ?wkt ?event ?time ?person ?match" //
-                + "{" //
-                + "  ?event a  time:Instant . \n"//
-                + "  ?event time:inXSDDateTime ?time . \n"//
-                + "  FILTER(tempo:after(?time, '2001-01-01T01:01:03-08:00') ) \n"// after 3 seconds
-                + "  ?feature a geo:Feature . "//
-                + "  ?feature geo:hasGeometry ?point . "//
-                + "  ?point a geo:Point . "//
-                + "  ?point geo:asWKT ?wkt . "//
-                + "  FILTER(geof:sfWithin(?wkt, \"POLYGON((-78 39, -77 39, -77 38, -78 38, -78 39))\"^^geo:wktLiteral)). " //
-                + "  ?person a <http://example.org/ontology/Person> . "//
-                + "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . "//
-                + "  FILTER(fts:text(?match, \"pal*\")) " //
-                + "}";//
-
-
-
-        tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
-
-        tupleHandler = new CountingResultHandler();
-        tupleQuery.evaluate(tupleHandler);
-        log.info("Result count : " + tupleHandler.getCount());
-        Validate.isTrue(tupleHandler.getCount() == 5);
-
-    }
-
-
-
-    private static void testGeoFreetextWithPCJSearch(SailRepositoryConnection conn) throws MalformedQueryException,
-    RepositoryException, TupleQueryResultHandlerException, QueryEvaluationException {
-     // ring outside point
-        String queryString = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
-                + "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
-                + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
-                + "SELECT ?feature ?point ?wkt ?e ?c ?l ?o ?person ?match " //
-                + "{" //
-                + "  ?person a <http://example.org/ontology/Person> . "//
-                + "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . "//
-                + "  FILTER(fts:text(?match, \"!alice & hose\")) " //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?feature a geo:Feature . "//
-                + "  ?feature geo:hasGeometry ?point . "//
-                + "  ?point a geo:Point . "//
-                + "  ?point geo:asWKT ?wkt . "//
-                + "  FILTER(geof:sfWithin(?wkt, \"POLYGON((-78 39, -77 39, -77 38, -78 38, -78 39))\"^^geo:wktLiteral)) " //
-                + "}";//
-        TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
-        CountingResultHandler tupleHandler = new CountingResultHandler();
-        tupleQuery.evaluate(tupleHandler);
-        log.info("Result count : " + tupleHandler.getCount());
-        Validate.isTrue(tupleHandler.getCount() == 1);
-    }
-
-
-    private static void testDeleteTemporalData(SailRepositoryConnection conn) throws Exception {
-        // Delete all stored dates
-        String sparqlDelete = "PREFIX time: <http://www.w3.org/2006/time#>\n"
-                + "PREFIX tempo: <tag:rya-rdf.org,2015:temporal#> \n"//
-                + "DELETE {\n" //
-                + "  ?event time:inXSDDateTime ?time . \n"
-                + "}\n"
-                + "WHERE { \n"
-                + "  ?event time:inXSDDateTime ?time . \n"//
-                + "}";//
-
-        Update deleteUpdate = conn.prepareUpdate(QueryLanguage.SPARQL, sparqlDelete);
-        deleteUpdate.execute();
-
-
-        // Find all stored dates.
-        String queryString = "PREFIX time: <http://www.w3.org/2006/time#> \n"//
-                + "PREFIX tempo: <tag:rya-rdf.org,2015:temporal#> \n"//
-                + "SELECT ?event ?time \n" //
-                + "WHERE { \n"
-                + "  ?event time:inXSDDateTime ?time . \n"//
-                + "  FILTER(tempo:after(?time, '2001-01-01T01:01:03-08:00') ) \n"// after 3 seconds
-                + "}";//
-
-
-        CountingResultHandler tupleHandler = new CountingResultHandler();
-        TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
-        tupleQuery.evaluate(tupleHandler);
-        log.info("Result count : " + tupleHandler.getCount());
-        Validate.isTrue(tupleHandler.getCount() == 0);
-    }
-
-
-    private static void testDeleteFreeTextData(SailRepositoryConnection conn) throws Exception {
-        // Delete data from the repository using the SailRepository remove methods
-        ValueFactory f = conn.getValueFactory();
-        URI person = f.createURI("http://example.org/ontology/Person");
-
-        String uuid;
-
-        uuid = "urn:people:alice";
-        conn.remove(f.createURI(uuid), RDF.TYPE, person);
-        conn.remove(f.createURI(uuid), RDFS.LABEL, f.createLiteral("Alice Palace Hose", f.createURI("xsd:string")));
-
-        uuid = "urn:people:bobss";
-        conn.remove(f.createURI(uuid), RDF.TYPE, person);
-        conn.remove(f.createURI(uuid), RDFS.LABEL, f.createLiteral("Bob Snob Hose", "en"));
-
-        conn.remove(person, RDFS.LABEL, f.createLiteral("label", "en"));
-
-        String queryString;
-        TupleQuery tupleQuery;
-        CountingResultHandler tupleHandler;
-
-        // Find all
-        queryString = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
-                + "SELECT ?person ?match " //
-                + "{" //
-                + "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . "//
-                + "  ?person a <http://example.org/ontology/Person> . "//
-                + "}";//
-        tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
-        tupleHandler = new CountingResultHandler();
-        tupleQuery.evaluate(tupleHandler);
-        log.info("Result count : " + tupleHandler.getCount());
-        Validate.isTrue(tupleHandler.getCount() == 0);
-    }
-
-
-    private static void testDeleteGeoData(SailRepositoryConnection conn) throws Exception {
-        // Delete all stored points
-        String sparqlDelete = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
-                + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
-                + "DELETE {\n" //
-                + "  ?feature a geo:Feature . "//
-                + "  ?feature geo:hasGeometry ?point . "//
-                + "  ?point a geo:Point . "//
-                + "  ?point geo:asWKT ?wkt . "//
-                + "}\n"
-                + "WHERE { \n"
-                + "  ?feature a geo:Feature . "//
-                + "  ?feature geo:hasGeometry ?point . "//
-                + "  ?point a geo:Point . "//
-                + "  ?point geo:asWKT ?wkt . "//
-                + "}";//
-
-        Update deleteUpdate = conn.prepareUpdate(QueryLanguage.SPARQL, sparqlDelete);
-        deleteUpdate.execute();
-
-        String queryString;
-        TupleQuery tupleQuery;
-        CountingResultHandler tupleHandler;
-
-        // Find all stored points
-        queryString = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
-                + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
-                + "SELECT ?feature ?point ?wkt " //
-                + "{" //
-                + "  ?feature a geo:Feature . "//
-                + "  ?feature geo:hasGeometry ?point . "//
-                + "  ?point a geo:Point . "//
-                + "  ?point geo:asWKT ?wkt . "//
-                + "}";//
-        tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
-        tupleHandler = new CountingResultHandler();
-        tupleQuery.evaluate(tupleHandler);
-        log.info("Result count : " + tupleHandler.getCount());
-        Validate.isTrue(tupleHandler.getCount() == 0);
-    }
-
-
-    private static void createPCJ(SailRepositoryConnection conn)
-            throws RepositoryException, AccumuloException, AccumuloSecurityException, TableExistsException {
-
-        String queryString1 = ""//
-                + "SELECT ?e ?c ?l ?o " //
-                + "{" //
-                + "  ?c a ?e . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "}";//
-
-        String queryString2 = ""//
-                + "SELECT ?e ?c ?l ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "}";//
-
-
-        URI obj,subclass,talksTo;
-        URI person = new URIImpl("urn:people:alice");
-        URI feature = new URIImpl("urn:feature");
-        URI sub = new URIImpl("uri:entity");
-        subclass = new URIImpl("uri:class");
-        obj = new URIImpl("uri:obj");
-        talksTo = new URIImpl("uri:talksTo");
-
-        conn.add(person, RDF.TYPE, sub);
-        conn.add(feature, RDF.TYPE, sub);
-        conn.add(sub, RDF.TYPE, subclass);
-        conn.add(sub, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(sub, talksTo, obj);
-
-        AccumuloIndexSet ais1 = null;
-        AccumuloIndexSet ais2 = null;
-        String tablename1 = RYA_TABLE_PREFIX + "INDEX_1";
-        String tablename2 = RYA_TABLE_PREFIX + "INDEX_2";
-
-        Connector accCon = new MockInstance(INSTANCE).getConnector("root", new PasswordToken("".getBytes()));
-        accCon.tableOperations().create(tablename1);
-        accCon.tableOperations().create(tablename2);
-
-        try {
-            ais1 = new AccumuloIndexSet(queryString1, conn, accCon, tablename1);
-            ais2 = new AccumuloIndexSet(queryString2, conn, accCon, tablename2);
-        } catch (MalformedQueryException | SailException | QueryEvaluationException | MutationsRejectedException | TableNotFoundException e) {
-            log.error("Error creating Accumulo Index", e);
-        }
-    }
-
-
-    private static class CountingResultHandler implements TupleQueryResultHandler {
-        private int count = 0;
-
-        public int getCount() {
-            return count;
-        }
-
-        public void resetCount() {
-            this.count = 0;
-        }
-
-        @Override
-        public void startQueryResult(List<String> arg0) throws TupleQueryResultHandlerException {
-        }
-
-        @Override
-        public void handleSolution(BindingSet arg0) throws TupleQueryResultHandlerException {
-            count++;
-            System.out.println(arg0);
-        }
-
-        @Override
-        public void endQueryResult() throws TupleQueryResultHandlerException {
-        }
-
-        @Override
-        public void handleBoolean(boolean arg0) throws QueryResultHandlerException {
-        }
-
-        @Override
-        public void handleLinks(List<String> arg0) throws QueryResultHandlerException {
-        }
-    }
+	private static final Logger log = Logger.getLogger(RyaDirectExample.class);
+
+	//
+	// Connection configuration parameters
+	//
+
+	private static final boolean USE_MOCK_INSTANCE = true;
+	private static final boolean PRINT_QUERIES = true;
+	private static final String INSTANCE = "instance";
+	private static final String RYA_TABLE_PREFIX = "x_test_triplestore_";
+	private static final String AUTHS = "";
+
+	public static void main(String[] args) throws Exception {
+		final Configuration conf = getConf();
+		conf.setBoolean(ConfigUtils.DISPLAY_QUERY_PLAN, PRINT_QUERIES);
+
+		log.info("Creating the tables as root.");
+		// createTables(addRootConf(conf), conf);
+
+		SailRepository repository = null;
+		SailRepositoryConnection conn = null;
+
+		try {
+			log.info("Connecting to Indexing Sail Repository.");
+
+			final Sail extSail = RyaSailFactory.getInstance(conf);
+			repository = new SailRepository(extSail);
+			repository.initialize();
+			conn = repository.getConnection();
+
+			createPCJ(conf);
+
+			final long start = System.currentTimeMillis();
+			log.info("Running SPARQL Example: Add and Delete");
+			testAddAndDelete(conn);
+			log.info("Running SAIL/SPARQL Example: PCJ Search");
+			testPCJSearch(conn);
+			log.info("Running SAIL/SPARQL Example: Add and Temporal Search");
+			testAddAndTemporalSearchWithPCJ(conn);
+			log.info("Running SAIL/SPARQL Example: Add and Free Text Search with PCJ");
+			testAddAndFreeTextSearchWithPCJ(conn);
+			log.info("Running SPARQL Example: Add Point and Geo Search with PCJ");
+			testAddPointAndWithinSearchWithPCJ(conn);
+			log.info("Running SPARQL Example: Temporal, Freetext, and Geo Search");
+			testTemporalFreeGeoSearch(conn);
+			log.info("Running SPARQL Example: Geo, Freetext, and PCJ Search");
+			testGeoFreetextWithPCJSearch(conn);
+			log.info("Running SPARQL Example: Delete Temporal Data");
+			testDeleteTemporalData(conn);
+			log.info("Running SPARQL Example: Delete Free Text Data");
+			testDeleteFreeTextData(conn);
+			log.info("Running SPARQL Example: Delete Geo Data");
+			testDeleteGeoData(conn);
+
+			log.info("TIME: " + (System.currentTimeMillis() - start) / 1000.);
+		} finally {
+			log.info("Shutting down");
+			closeQuietly(conn);
+			closeQuietly(repository);
+		}
+	}
+
+	private static void closeQuietly(SailRepository repository) {
+		if (repository != null) {
+			try {
+				repository.shutDown();
+			} catch (final RepositoryException e) {
+				// quietly absorb this exception
+			}
+		}
+	}
+
+	private static void closeQuietly(SailRepositoryConnection conn) {
+		if (conn != null) {
+			try {
+				conn.close();
+			} catch (final RepositoryException e) {
+				// quietly absorb this exception
+			}
+		}
+	}
+
+	private static Configuration getConf() {
+
+		final AccumuloRdfConfiguration conf = new AccumuloRdfConfiguration();
+
+		conf.setBoolean(ConfigUtils.USE_MOCK_INSTANCE, USE_MOCK_INSTANCE);
+		conf.set(ConfigUtils.USE_PCJ, "true");
+		conf.set(ConfigUtils.USE_GEO, "true");
+		conf.set(ConfigUtils.USE_FREETEXT, "true");
+		conf.set(ConfigUtils.USE_TEMPORAL, "true");
+		conf.set(RdfCloudTripleStoreConfiguration.CONF_TBL_PREFIX,
+				RYA_TABLE_PREFIX);
+		conf.set(ConfigUtils.CLOUDBASE_USER, "root");
+		conf.set(ConfigUtils.CLOUDBASE_PASSWORD, "");
+		conf.set(ConfigUtils.CLOUDBASE_INSTANCE, INSTANCE);
+		conf.setInt(ConfigUtils.NUM_PARTITIONS, 3);
+		conf.set(ConfigUtils.CLOUDBASE_AUTHS, AUTHS);
+
+		// only geo index statements with geo:asWKT predicates
+		conf.set(ConfigUtils.GEO_PREDICATES_LIST,
+				GeoConstants.GEO_AS_WKT.stringValue());
+		return conf;
+	}
+
+	public static void testAddAndDelete(SailRepositoryConnection conn)
+			throws MalformedQueryException, RepositoryException,
+			UpdateExecutionException, QueryEvaluationException,
+			TupleQueryResultHandlerException, AccumuloException,
+			AccumuloSecurityException, TableNotFoundException {
+
+		// Add data
+		String query = "INSERT DATA\n"//
+				+ "{ GRAPH <http://updated/test> {\n"//
+				+ "  <http://acme.com/people/Mike> " //
+				+ "       <http://acme.com/actions/likes> \"A new book\" ;\n"//
+				+ "       <http://acme.com/actions/likes> \"Avocados\" .\n"
+				+ "} }";
+
+		log.info("Performing Query");
+
+		Update update = conn.prepareUpdate(QueryLanguage.SPARQL, query);
+		update.execute();
+
+		query = "select ?p ?o { GRAPH <http://updated/test> {<http://acme.com/people/Mike> ?p ?o . }}";
+		final CountingResultHandler resultHandler = new CountingResultHandler();
+		TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL,
+				query);
+		tupleQuery.evaluate(resultHandler);
+		log.info("Result count : " + resultHandler.getCount());
+
+		Validate.isTrue(resultHandler.getCount() == 2);
+		resultHandler.resetCount();
+
+		// Delete Data
+		query = "DELETE DATA\n" //
+				+ "{ GRAPH <http://updated/test> {\n"
+				+ "  <http://acme.com/people/Mike> <http://acme.com/actions/likes> \"A new book\" ;\n"
+				+ "   <http://acme.com/actions/likes> \"Avocados\" .\n" + "}}";
+
+		update = conn.prepareUpdate(QueryLanguage.SPARQL, query);
+		update.execute();
+
+		query = "select ?p ?o { GRAPH <http://updated/test> {<http://acme.com/people/Mike> ?p ?o . }}";
+		tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
+		tupleQuery.evaluate(resultHandler);
+		log.info("Result count : " + resultHandler.getCount());
+
+		Validate.isTrue(resultHandler.getCount() == 0);
+	}
+
+	private static void testPCJSearch(SailRepositoryConnection conn)
+			throws Exception {
+
+		String queryString;
+		TupleQuery tupleQuery;
+		CountingResultHandler tupleHandler;
+
+		// ///////////// search for bob
+		queryString = "SELECT ?e ?c ?l ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "}";//
+
+		tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
+		tupleHandler = new CountingResultHandler();
+		tupleQuery.evaluate(tupleHandler);
+		log.info("Result count : " + tupleHandler.getCount());
+		Validate.isTrue(tupleHandler.getCount() == 1);
+
+		// ///////////// search for bob
+		queryString = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
+				+ "SELECT ?e ?c ?l ?o " //
+				+ "{" //
+				+ "  ?c a ?e . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "}";//
+
+		tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
+		tupleHandler = new CountingResultHandler();
+		tupleQuery.evaluate(tupleHandler);
+		log.info("Result count : " + tupleHandler.getCount());
+		Validate.isTrue(tupleHandler.getCount() == 2);
+
+	}
+
+	private static void testAddAndTemporalSearchWithPCJ(
+			SailRepositoryConnection conn) throws Exception {
+
+		// create some resources and literals to make statements out of
+
+		final String sparqlInsert = "PREFIX time: <http://www.w3.org/2006/time#>\n"
+				+ "INSERT DATA {\n" //
+				+ "_:eventz       a       time:Instant ;\n"
+				+ "     time:inXSDDateTime '2001-01-01T01:01:01-08:00' ;\n" // one
+																			// second
+				+ "     time:inXSDDateTime '2001-01-01T04:01:02.000-05:00'^^<http://www.w3.org/2001/XMLSchema#dateTime> ;\n" // 2
+																																// seconds
+				+ "     time:inXSDDateTime \"2001-01-01T01:01:03-08:00\" ;\n" // 3
+																				// seconds
+				+ "     time:inXSDDateTime '2001-01-01T01:01:04-08:00' ;\n" // 4
+																			// seconds
+				+ "     time:inXSDDateTime '2001-01-01T09:01:05Z' ;\n"
+				+ "     time:inXSDDateTime '2006-01-01' ;\n"
+				+ "     time:inXSDDateTime '2007-01-01' ;\n"
+				+ "     time:inXSDDateTime '2008-01-01' ; .\n" + "}";
+
+		final Update update = conn.prepareUpdate(QueryLanguage.SPARQL, sparqlInsert);
+		update.execute();
+
+		// Find all stored dates.
+		String queryString = "PREFIX time: <http://www.w3.org/2006/time#> \n"//
+				+ "PREFIX tempo: <tag:rya-rdf.org,2015:temporal#> \n"//
+				+ "SELECT ?event ?time \n" //
+				+ "WHERE { \n"
+				+ "  ?event time:inXSDDateTime ?time . \n"//
+				+ "  FILTER(tempo:after(?time, '2001-01-01T01:01:03-08:00') ) \n"// after
+																					// 3
+																					// seconds
+				+ "}";//
+
+		TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL,
+				queryString);
+		CountingResultHandler tupleHandler = new CountingResultHandler();
+		tupleQuery.evaluate(tupleHandler);
+		log.info("Result count : " + tupleHandler.getCount());
+		Validate.isTrue(tupleHandler.getCount() == 5);
+
+		// Find all stored dates.
+		queryString = "PREFIX time: <http://www.w3.org/2006/time#> \n"//
+				+ "PREFIX tempo: <tag:rya-rdf.org,2015:temporal#> \n"//
+				+ "SELECT ?event ?time \n" //
+				+ "WHERE { \n"
+				+ "  ?event time:inXSDDateTime ?time . \n"//
+				+ "  ?event a  time:Instant . \n"//
+				+ "  FILTER(tempo:after(?time, '2001-01-01T01:01:03-08:00') ) \n"// after
+																					// 3
+																					// seconds
+				+ "}";//
+
+		tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
+		tupleHandler = new CountingResultHandler();
+		tupleQuery.evaluate(tupleHandler);
+		log.info("Result count : " + tupleHandler.getCount());
+		Validate.isTrue(tupleHandler.getCount() == 5);
+
+		// Find all stored dates.
+		queryString = "PREFIX time: <http://www.w3.org/2006/time#> \n"//
+				+ "PREFIX tempo: <tag:rya-rdf.org,2015:temporal#> \n"//
+				+ "SELECT ?event ?time ?e ?c ?l ?o \n" //
+				+ "WHERE { \n"
+				+ "  ?e a ?c . \n"//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . \n"//
+				+ "  ?e <uri:talksTo> ?o . \n"//
+				+ "  ?event a  time:Instant . \n"//
+				+ "  ?event time:inXSDDateTime ?time . \n"//
+				+ "  FILTER(tempo:after(?time, '2001-01-01T01:01:03-08:00') ) \n"// after
+																					// 3
+																					// seconds
+				+ "}";//
+
+		tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
+		tupleHandler = new CountingResultHandler();
+		tupleQuery.evaluate(tupleHandler);
+		log.info("Result count : " + tupleHandler.getCount());
+		Validate.isTrue(tupleHandler.getCount() == 5);
+	}
+
+	private static void testAddAndFreeTextSearchWithPCJ(
+			SailRepositoryConnection conn) throws Exception {
+		// add data to the repository using the SailRepository add methods
+		final ValueFactory f = conn.getValueFactory();
+		final URI person = f.createURI("http://example.org/ontology/Person");
+
+		String uuid;
+
+		uuid = "urn:people:alice";
+		conn.add(f.createURI(uuid), RDF.TYPE, person);
+		conn.add(f.createURI(uuid), RDFS.LABEL,
+				f.createLiteral("Alice Palace Hose", f.createURI("xsd:string")));
+
+		uuid = "urn:people:bobss";
+		conn.add(f.createURI(uuid), RDF.TYPE, person);
+		conn.add(f.createURI(uuid), RDFS.LABEL,
+				f.createLiteral("Bob Snob Hose", "en"));
+
+		String queryString;
+		TupleQuery tupleQuery;
+		CountingResultHandler tupleHandler;
+
+		// ///////////// search for alice
+		queryString = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
+				+ "SELECT ?person ?match ?e ?c ?l ?o " //
+				+ "{" //
+				+ "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . "//
+				+ "  FILTER(fts:text(?match, \"pal*\")) " //
+				+ "}";//
+		tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
+		tupleHandler = new CountingResultHandler();
+		tupleQuery.evaluate(tupleHandler);
+		log.info("Result count : " + tupleHandler.getCount());
+		Validate.isTrue(tupleHandler.getCount() == 1);
+
+		// ///////////// search for alice and bob
+		queryString = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
+				+ "SELECT ?person ?match " //
+				+ "{" //
+				+ "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . "//
+				+ "  ?person a <http://example.org/ontology/Person> . "//
+				+ "  FILTER(fts:text(?match, \"(alice | bob) *SE\")) " //
+				+ "}";//
+		tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
+		tupleHandler = new CountingResultHandler();
+		tupleQuery.evaluate(tupleHandler);
+		log.info("Result count : " + tupleHandler.getCount());
+		Validate.isTrue(tupleHandler.getCount() == 2);
+
+		// ///////////// search for alice and bob
+		queryString = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
+				+ "SELECT ?person ?match " //
+				+ "{" //
+				+ "  ?person a <http://example.org/ontology/Person> . "//
+				+ "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . "//
+				+ "  FILTER(fts:text(?match, \"(alice | bob) *SE\")) " //
+				+ "  FILTER(fts:text(?match, \"pal*\")) " //
+				+ "}";//
+		tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
+		tupleHandler = new CountingResultHandler();
+		tupleQuery.evaluate(tupleHandler);
+		log.info("Result count : " + tupleHandler.getCount());
+		Validate.isTrue(tupleHandler.getCount() == 1);
+
+		// ///////////// search for bob
+		queryString = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
+				+ "SELECT ?person ?match ?e ?c ?l ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?person a <http://example.org/ontology/Person> . "//
+				+ "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . "//
+				+ "  FILTER(fts:text(?match, \"!alice & hose\")) " //
+				+ "}";//
+
+		tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
+		tupleHandler = new CountingResultHandler();
+		tupleQuery.evaluate(tupleHandler);
+		log.info("Result count : " + tupleHandler.getCount());
+		Validate.isTrue(tupleHandler.getCount() == 1);
+	}
+
+	private static void testAddPointAndWithinSearchWithPCJ(
+			SailRepositoryConnection conn) throws Exception {
+
+		final String update = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
+				+ "INSERT DATA { " //
+				+ "  <urn:feature> a geo:Feature ; " //
+				+ "    geo:hasGeometry [ " //
+				+ "      a geo:Point ; " //
+				+ "      geo:asWKT \"Point(-77.03524 38.889468)\"^^geo:wktLiteral "//
+				+ "    ] . " //
+				+ "}";
+
+		final Update u = conn.prepareUpdate(QueryLanguage.SPARQL, update);
+		u.execute();
+
+		String queryString;
+		TupleQuery tupleQuery;
+		CountingResultHandler tupleHandler;
+
+		// point outside search ring
+		queryString = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
+				+ "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
+				+ "SELECT ?feature ?point ?wkt " //
+				+ "{" //
+				+ "  ?feature a geo:Feature . "//
+				+ "  ?feature geo:hasGeometry ?point . "//
+				+ "  ?point a geo:Point . "//
+				+ "  ?point geo:asWKT ?wkt . "//
+				+ "  FILTER(geof:sfWithin(?wkt, \"POLYGON((-77 39, -76 39, -76 38, -77 38, -77 39))\"^^geo:wktLiteral)) " //
+				+ "}";//
+		tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
+		tupleHandler = new CountingResultHandler();
+		tupleQuery.evaluate(tupleHandler);
+		log.info("Result count : " + tupleHandler.getCount());
+		Validate.isTrue(tupleHandler.getCount() == 0);
+
+		// point inside search ring
+		queryString = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
+				+ "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
+				+ "SELECT ?feature ?point ?wkt ?e ?l ?o" //
+				+ "{" //
+				+ "  ?feature a ?e . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?feature a geo:Feature . "//
+				+ "  ?feature geo:hasGeometry ?point . "//
+				+ "  ?point a geo:Point . "//
+				+ "  ?point geo:asWKT ?wkt . "//
+				+ "  FILTER(geof:sfWithin(?wkt, \"POLYGON((-78 39, -77 39, -77 38, -78 38, -78 39))\"^^geo:wktLiteral)) " //
+				+ "}";//
+
+		tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
+		tupleHandler = new CountingResultHandler();
+		tupleQuery.evaluate(tupleHandler);
+		log.info("Result count : " + tupleHandler.getCount());
+		Validate.isTrue(tupleHandler.getCount() == 1);
+
+		// point inside search ring with Pre-Computed Join
+		queryString = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
+				+ "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
+				+ "SELECT ?feature ?point ?wkt ?e ?l ?o" //
+				+ "{" //
+				+ "  ?feature a ?e . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?feature a geo:Feature . "//
+				+ "  ?feature geo:hasGeometry ?point . "//
+				+ "  ?point a geo:Point . "//
+				+ "  ?point geo:asWKT ?wkt . "//
+				+ "  FILTER(geof:sfWithin(?wkt, \"POLYGON((-78 39, -77 39, -77 38, -78 38, -78 39))\"^^geo:wktLiteral)) " //
+				+ "}";//
+
+		tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
+		tupleHandler = new CountingResultHandler();
+		tupleQuery.evaluate(tupleHandler);
+		log.info("Result count : " + tupleHandler.getCount());
+		Validate.isTrue(tupleHandler.getCount() >= 1); // may see points from
+														// during previous runs
+
+		// point outside search ring with PCJ
+		queryString = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
+				+ "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
+				+ "SELECT ?feature ?point ?wkt ?e ?l ?o " //
+				+ "{" //
+				+ "  ?feature a ?e . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?feature a geo:Feature . "//
+				+ "  ?feature geo:hasGeometry ?point . "//
+				+ "  ?point a geo:Point . "//
+				+ "  ?point geo:asWKT ?wkt . "//
+				+ "  FILTER(geof:sfWithin(?wkt, \"POLYGON((-77 39, -76 39, -76 38, -77 38, -77 39))\"^^geo:wktLiteral)) " //
+				+ "}";//
+		tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
+		tupleHandler = new CountingResultHandler();
+		tupleQuery.evaluate(tupleHandler);
+		log.info("Result count : " + tupleHandler.getCount());
+		Validate.isTrue(tupleHandler.getCount() == 0);
+
+		// point inside search ring with different Pre-Computed Join
+		queryString = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
+				+ "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
+				+ "SELECT ?feature ?point ?wkt ?e ?c ?l ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?feature a geo:Feature . "//
+				+ "  ?feature geo:hasGeometry ?point . "//
+				+ "  ?point a geo:Point . "//
+				+ "  ?point geo:asWKT ?wkt . "//
+				+ "  FILTER(geof:sfWithin(?wkt, \"POLYGON((-78 39, -77 39, -77 38, -78 38, -78 39))\"^^geo:wktLiteral)) " //
+				+ "}";//
+		tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
+		tupleHandler = new CountingResultHandler();
+		tupleQuery.evaluate(tupleHandler);
+		log.info("Result count : " + tupleHandler.getCount());
+		Validate.isTrue(tupleHandler.getCount() == 1);
+	}
+
+	private static void testTemporalFreeGeoSearch(SailRepositoryConnection conn)
+			throws MalformedQueryException, RepositoryException,
+			UpdateExecutionException, TupleQueryResultHandlerException,
+			QueryEvaluationException {
+
+		String queryString;
+		TupleQuery tupleQuery;
+		CountingResultHandler tupleHandler;
+
+		// ring containing point
+		queryString = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
+				+ "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
+				+ "PREFIX time: <http://www.w3.org/2006/time#> "//
+				+ "PREFIX tempo: <tag:rya-rdf.org,2015:temporal#> "//
+				+ "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
+				+ "SELECT ?feature ?point ?wkt ?event ?time ?person ?match" //
+				+ "{" //
+				+ "  ?event a  time:Instant . \n"//
+				+ "  ?event time:inXSDDateTime ?time . \n"//
+				+ "  FILTER(tempo:after(?time, '2001-01-01T01:01:03-08:00') ) \n"// after
+																					// 3
+																					// seconds
+				+ "  ?feature a geo:Feature . "//
+				+ "  ?feature geo:hasGeometry ?point . "//
+				+ "  ?point a geo:Point . "//
+				+ "  ?point geo:asWKT ?wkt . "//
+				+ "  FILTER(geof:sfWithin(?wkt, \"POLYGON((-78 39, -77 39, -77 38, -78 38, -78 39))\"^^geo:wktLiteral)). " //
+				+ "  ?person a <http://example.org/ontology/Person> . "//
+				+ "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . "//
+				+ "  FILTER(fts:text(?match, \"pal*\")) " //
+				+ "}";//
+
+		tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
+
+		tupleHandler = new CountingResultHandler();
+		tupleQuery.evaluate(tupleHandler);
+		log.info("Result count : " + tupleHandler.getCount());
+		Validate.isTrue(tupleHandler.getCount() == 5);
+
+	}
+
+	private static void testGeoFreetextWithPCJSearch(
+			SailRepositoryConnection conn) throws MalformedQueryException,
+			RepositoryException, TupleQueryResultHandlerException,
+			QueryEvaluationException {
+		// ring outside point
+		final String queryString = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
+				+ "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
+				+ "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
+				+ "SELECT ?feature ?point ?wkt ?e ?c ?l ?o ?person ?match " //
+				+ "{" //
+				+ "  ?person a <http://example.org/ontology/Person> . "//
+				+ "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . "//
+				+ "  FILTER(fts:text(?match, \"!alice & hose\")) " //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?feature a geo:Feature . "//
+				+ "  ?feature geo:hasGeometry ?point . "//
+				+ "  ?point a geo:Point . "//
+				+ "  ?point geo:asWKT ?wkt . "//
+				+ "  FILTER(geof:sfWithin(?wkt, \"POLYGON((-78 39, -77 39, -77 38, -78 38, -78 39))\"^^geo:wktLiteral)) " //
+				+ "}";//
+		final TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL,
+				queryString);
+		final CountingResultHandler tupleHandler = new CountingResultHandler();
+		tupleQuery.evaluate(tupleHandler);
+		log.info("Result count : " + tupleHandler.getCount());
+		Validate.isTrue(tupleHandler.getCount() == 1);
+	}
+
+	private static void testDeleteTemporalData(SailRepositoryConnection conn)
+			throws Exception {
+		// Delete all stored dates
+		final String sparqlDelete = "PREFIX time: <http://www.w3.org/2006/time#>\n"
+				+ "PREFIX tempo: <tag:rya-rdf.org,2015:temporal#> \n"//
+				+ "DELETE {\n" //
+				+ "  ?event time:inXSDDateTime ?time . \n" + "}\n"
+				+ "WHERE { \n" + "  ?event time:inXSDDateTime ?time . \n"//
+				+ "}";//
+
+		final Update deleteUpdate = conn.prepareUpdate(QueryLanguage.SPARQL,
+				sparqlDelete);
+		deleteUpdate.execute();
+
+		// Find all stored dates.
+		final String queryString = "PREFIX time: <http://www.w3.org/2006/time#> \n"//
+				+ "PREFIX tempo: <tag:rya-rdf.org,2015:temporal#> \n"//
+				+ "SELECT ?event ?time \n" //
+				+ "WHERE { \n"
+				+ "  ?event time:inXSDDateTime ?time . \n"//
+				+ "  FILTER(tempo:after(?time, '2001-01-01T01:01:03-08:00') ) \n"// after
+																					// 3
+																					// seconds
+				+ "}";//
+
+		final CountingResultHandler tupleHandler = new CountingResultHandler();
+		final TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL,
+				queryString);
+		tupleQuery.evaluate(tupleHandler);
+		log.info("Result count : " + tupleHandler.getCount());
+		Validate.isTrue(tupleHandler.getCount() == 0);
+	}
+
+	private static void testDeleteFreeTextData(SailRepositoryConnection conn)
+			throws Exception {
+		// Delete data from the repository using the SailRepository remove
+		// methods
+		final ValueFactory f = conn.getValueFactory();
+		final URI person = f.createURI("http://example.org/ontology/Person");
+
+		String uuid;
+
+		uuid = "urn:people:alice";
+		conn.remove(f.createURI(uuid), RDF.TYPE, person);
+		conn.remove(f.createURI(uuid), RDFS.LABEL,
+				f.createLiteral("Alice Palace Hose", f.createURI("xsd:string")));
+
+		uuid = "urn:people:bobss";
+		conn.remove(f.createURI(uuid), RDF.TYPE, person);
+		conn.remove(f.createURI(uuid), RDFS.LABEL,
+				f.createLiteral("Bob Snob Hose", "en"));
+
+		conn.remove(person, RDFS.LABEL, f.createLiteral("label", "en"));
+
+		String queryString;
+		TupleQuery tupleQuery;
+		CountingResultHandler tupleHandler;
+
+		// Find all
+		queryString = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
+				+ "SELECT ?person ?match " //
+				+ "{" //
+				+ "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?match . "//
+				+ "  ?person a <http://example.org/ontology/Person> . "//
+				+ "}";//
+		tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
+		tupleHandler = new CountingResultHandler();
+		tupleQuery.evaluate(tupleHandler);
+		log.info("Result count : " + tupleHandler.getCount());
+		Validate.isTrue(tupleHandler.getCount() == 0);
+	}
+
+	private static void testDeleteGeoData(SailRepositoryConnection conn)
+			throws Exception {
+		// Delete all stored points
+		final String sparqlDelete = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
+				+ "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
+				+ "DELETE {\n" //
+				+ "  ?feature a geo:Feature . "//
+				+ "  ?feature geo:hasGeometry ?point . "//
+				+ "  ?point a geo:Point . "//
+				+ "  ?point geo:asWKT ?wkt . "//
+				+ "}\n" + "WHERE { \n" + "  ?feature a geo:Feature . "//
+				+ "  ?feature geo:hasGeometry ?point . "//
+				+ "  ?point a geo:Point . "//
+				+ "  ?point geo:asWKT ?wkt . "//
+				+ "}";//
+
+		final Update deleteUpdate = conn.prepareUpdate(QueryLanguage.SPARQL,
+				sparqlDelete);
+		deleteUpdate.execute();
+
+		String queryString;
+		TupleQuery tupleQuery;
+		CountingResultHandler tupleHandler;
+
+		// Find all stored points
+		queryString = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
+				+ "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
+				+ "SELECT ?feature ?point ?wkt " //
+				+ "{" //
+				+ "  ?feature a geo:Feature . "//
+				+ "  ?feature geo:hasGeometry ?point . "//
+				+ "  ?point a geo:Point . "//
+				+ "  ?point geo:asWKT ?wkt . "//
+				+ "}";//
+		tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
+		tupleHandler = new CountingResultHandler();
+		tupleQuery.evaluate(tupleHandler);
+		log.info("Result count : " + tupleHandler.getCount());
+		Validate.isTrue(tupleHandler.getCount() == 0);
+	}
+
+	private static void createPCJ(Configuration conf)
+			throws RepositoryException, AccumuloException,
+			AccumuloSecurityException, TableExistsException, PcjException {
+
+
+		final Configuration config = new AccumuloRdfConfiguration(conf);
+		config.set(ConfigUtils.USE_PCJ, "false");
+		Sail extSail = null;
+		try {
+			extSail = RyaSailFactory.getInstance(config);
+		} catch (final RyaDAOException e) {
+			e.printStackTrace();
+		}
+		final SailRepository repository = new SailRepository(extSail);
+		repository.initialize();
+		final SailRepositoryConnection conn = repository.getConnection();
+
+
+		final String queryString1 = ""//
+				+ "SELECT ?e ?c ?l ?o " //
+				+ "{" //
+				+ "  ?c a ?e . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "}";//
+
+		final String queryString2 = ""//
+				+ "SELECT ?e ?c ?l ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "}";//
+
+		URI obj, subclass, talksTo;
+		final URI person = new URIImpl("urn:people:alice");
+		final URI feature = new URIImpl("urn:feature");
+		final URI sub = new URIImpl("uri:entity");
+		subclass = new URIImpl("uri:class");
+		obj = new URIImpl("uri:obj");
+		talksTo = new URIImpl("uri:talksTo");
+
+		conn.add(person, RDF.TYPE, sub);
+		conn.add(feature, RDF.TYPE, sub);
+		conn.add(sub, RDF.TYPE, subclass);
+		conn.add(sub, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(sub, talksTo, obj);
+
+		final String tablename1 = RYA_TABLE_PREFIX + "INDEX_1";
+		final String tablename2 = RYA_TABLE_PREFIX + "INDEX_2";
+
+		final Connector accCon = new MockInstance(INSTANCE).getConnector("root",
+				new PasswordToken("".getBytes()));
+
+		new PcjTables().createAndPopulatePcj(conn, accCon, tablename1,
+				queryString1, new String[] { "e", "c", "l", "o" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		new PcjTables().createAndPopulatePcj(conn, accCon, tablename2,
+				queryString2, new String[] { "e", "c", "l", "o" },
+				Optional.<PcjVarOrderFactory> absent());
+
+	}
+
+	private static class CountingResultHandler implements
+			TupleQueryResultHandler {
+		private int count = 0;
+
+		public int getCount() {
+			return count;
+		}
+
+		public void resetCount() {
+			this.count = 0;
+		}
+
+		@Override
+		public void startQueryResult(List<String> arg0)
+				throws TupleQueryResultHandlerException {
+		}
+
+		@Override
+		public void handleSolution(BindingSet arg0)
+				throws TupleQueryResultHandlerException {
+			count++;
+			System.out.println(arg0);
+		}
+
+		@Override
+		public void endQueryResult() throws TupleQueryResultHandlerException {
+		}
+
+		@Override
+		public void handleBoolean(boolean arg0)
+				throws QueryResultHandlerException {
+		}
+
+		@Override
+		public void handleLinks(List<String> arg0)
+				throws QueryResultHandlerException {
+		}
+	}
 }


[03/16] incubator-rya git commit: RYA-32 Improve how metadata and values are written to Accumulo PCJ tables

Posted by mi...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/AccumuloIndexSetTest.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/AccumuloIndexSetTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/AccumuloIndexSetTest.java
new file mode 100644
index 0000000..dca641c
--- /dev/null
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/AccumuloIndexSetTest.java
@@ -0,0 +1,678 @@
+package mvm.rya.indexing.external.tupleSet;
+
+import info.aduna.iteration.CloseableIteration;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import mvm.rya.accumulo.AccumuloRdfConfiguration;
+import mvm.rya.api.RdfCloudTripleStoreConfiguration;
+import mvm.rya.api.persist.RyaDAOException;
+import mvm.rya.api.resolver.RyaTypeResolverException;
+import mvm.rya.indexing.RyaSailFactory;
+import mvm.rya.indexing.accumulo.ConfigUtils;
+import mvm.rya.indexing.external.PcjIntegrationTestingUtil;
+import mvm.rya.indexing.external.QueryVariableNormalizer;
+import mvm.rya.indexing.external.tupleSet.PcjTables.PcjException;
+import mvm.rya.indexing.external.tupleSet.PcjTables.PcjTableNameFactory;
+import mvm.rya.indexing.external.tupleSet.PcjTables.PcjVarOrderFactory;
+import mvm.rya.rdftriplestore.RyaSailRepository;
+
+import org.apache.accumulo.core.client.AccumuloException;
+import org.apache.accumulo.core.client.AccumuloSecurityException;
+import org.apache.accumulo.core.client.Connector;
+import org.apache.accumulo.core.client.MutationsRejectedException;
+import org.apache.accumulo.core.client.TableNotFoundException;
+import org.apache.accumulo.core.client.admin.TableOperations;
+import org.apache.hadoop.conf.Configuration;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.openrdf.model.Statement;
+import org.openrdf.model.impl.LiteralImpl;
+import org.openrdf.model.impl.NumericLiteralImpl;
+import org.openrdf.model.impl.StatementImpl;
+import org.openrdf.model.impl.URIImpl;
+import org.openrdf.model.vocabulary.XMLSchema;
+import org.openrdf.query.BindingSet;
+import org.openrdf.query.MalformedQueryException;
+import org.openrdf.query.QueryEvaluationException;
+import org.openrdf.query.algebra.Projection;
+import org.openrdf.query.algebra.evaluation.QueryBindingSet;
+import org.openrdf.query.parser.ParsedQuery;
+import org.openrdf.query.parser.sparql.SPARQLParser;
+import org.openrdf.repository.RepositoryConnection;
+import org.openrdf.repository.RepositoryException;
+import org.openrdf.sail.SailException;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+
+public class AccumuloIndexSetTest {
+
+	 protected static Connector accumuloConn = null;
+	 protected RyaSailRepository ryaRepo = null;
+	 protected RepositoryConnection ryaConn = null;
+	 protected Configuration conf = getConf();
+	 protected String prefix = "rya_";
+
+	@Before
+	public void init() throws AccumuloException, AccumuloSecurityException, RyaDAOException, RepositoryException, TableNotFoundException {
+		accumuloConn = ConfigUtils.getConnector(conf);
+		final TableOperations ops = accumuloConn.tableOperations();
+		if(ops.exists(prefix+"INDEX_"+ "testPcj")) {
+			ops.delete(prefix+"INDEX_"+ "testPcj");
+		}
+		ryaRepo = new RyaSailRepository(RyaSailFactory.getInstance(conf));
+		ryaRepo.initialize();
+		ryaConn = ryaRepo.getConnection();
+	}
+
+
+	/**
+     * TODO doc
+     * @throws MutationsRejectedException
+     * @throws QueryEvaluationException
+     * @throws SailException
+     * @throws MalformedQueryException
+     */
+    @Test
+    public void accumuloIndexSetTestWithEmptyBindingSet() throws RepositoryException, PcjException, TableNotFoundException,
+    RyaTypeResolverException, MalformedQueryException, SailException, QueryEvaluationException, MutationsRejectedException {
+        // Load some Triples into Rya.
+        final Set<Statement> triples = new HashSet<>();
+        triples.add( new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://hasAge"), new NumericLiteralImpl(14, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://hasAge"), new NumericLiteralImpl(16, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://hasAge"), new NumericLiteralImpl(12, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://hasAge"), new NumericLiteralImpl(43, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+
+        for(final Statement triple : triples) {
+            ryaConn.add(triple);
+        }
+
+        // Create a PCJ table will include those triples in its results.
+        final String sparql =
+                "SELECT ?name ?age " +
+                "{" +
+                  "FILTER(?age < 30) ." +
+                  "?name <http://hasAge> ?age." +
+                  "?name <http://playsSport> \"Soccer\" " +
+                "}";
+
+        final String pcjTableName = new PcjTableNameFactory().makeTableName(prefix, "testPcj");
+        // Create and populate the PCJ table.
+        PcjIntegrationTestingUtil.createAndPopulatePcj(ryaConn, accumuloConn, pcjTableName, sparql, new String[]{"name", "age"}, Optional.<PcjVarOrderFactory>absent());
+
+        final AccumuloIndexSet ais = new AccumuloIndexSet(accumuloConn, pcjTableName);
+
+        final CloseableIteration<BindingSet, QueryEvaluationException> results = ais.evaluate(new QueryBindingSet());
+        final Set<BindingSet> fetchedResults = new HashSet<BindingSet>();
+        while(results.hasNext()) {
+        	fetchedResults.add(results.next());
+        }
+        // Ensure the expected results match those that were stored.
+        final QueryBindingSet alice = new QueryBindingSet();
+        alice.addBinding("name", new URIImpl("http://Alice"));
+        alice.addBinding("age", new NumericLiteralImpl(14, XMLSchema.INTEGER));
+
+        final QueryBindingSet bob = new QueryBindingSet();
+        bob.addBinding("name", new URIImpl("http://Bob"));
+        bob.addBinding("age", new NumericLiteralImpl(16, XMLSchema.INTEGER));
+
+        final QueryBindingSet charlie = new QueryBindingSet();
+        charlie.addBinding("name", new URIImpl("http://Charlie"));
+        charlie.addBinding("age", new NumericLiteralImpl(12, XMLSchema.INTEGER));
+
+        final Set<BindingSet> expectedResults = Sets.<BindingSet>newHashSet(alice, bob, charlie);
+        Assert.assertEquals(expectedResults, fetchedResults);
+    }
+
+
+	/**
+     * TODO doc
+     * @throws MutationsRejectedException
+     * @throws QueryEvaluationException
+     * @throws SailException
+     * @throws MalformedQueryException
+     */
+    @Test
+    public void accumuloIndexSetTestWithBindingSet() throws RepositoryException, PcjException, TableNotFoundException,
+    RyaTypeResolverException, MalformedQueryException, SailException, QueryEvaluationException, MutationsRejectedException {
+        // Load some Triples into Rya.
+        final Set<Statement> triples = new HashSet<>();
+        triples.add( new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://hasAge"), new NumericLiteralImpl(14, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://hasAge"), new NumericLiteralImpl(16, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://hasAge"), new NumericLiteralImpl(12, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://hasAge"), new NumericLiteralImpl(43, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+
+        for(final Statement triple : triples) {
+            ryaConn.add(triple);
+        }
+
+        // Create a PCJ table will include those triples in its results.
+        final String sparql =
+                "SELECT ?name ?age " +
+                "{" +
+                  "FILTER(?age < 30) ." +
+                  "?name <http://hasAge> ?age." +
+                  "?name <http://playsSport> \"Soccer\" " +
+                "}";
+
+        final String pcjTableName = new PcjTableNameFactory().makeTableName(prefix, "testPcj");
+
+        // Create and populate the PCJ table.
+        PcjIntegrationTestingUtil.createAndPopulatePcj(ryaConn, accumuloConn, pcjTableName, sparql, new String[]{"name", "age"}, Optional.<PcjVarOrderFactory>absent());
+
+        final AccumuloIndexSet ais = new AccumuloIndexSet(accumuloConn, pcjTableName);
+
+        final QueryBindingSet bs = new QueryBindingSet();
+        bs.addBinding("name",new URIImpl("http://Alice"));
+        bs.addBinding("location",new URIImpl("http://Virginia"));
+
+        final CloseableIteration<BindingSet, QueryEvaluationException> results = ais.evaluate(bs);
+
+        bs.addBinding("age",new NumericLiteralImpl(14, XMLSchema.INTEGER));
+        Assert.assertEquals(bs, results.next());
+
+    }
+
+
+    @Test
+    public void accumuloIndexSetTestWithTwoBindingSets() throws RepositoryException, PcjException, TableNotFoundException,
+    RyaTypeResolverException, MalformedQueryException, SailException, QueryEvaluationException, MutationsRejectedException {
+        // Load some Triples into Rya.
+        final Set<Statement> triples = new HashSet<>();
+        triples.add( new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://hasAge"), new NumericLiteralImpl(14, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://hasAge"), new NumericLiteralImpl(16, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://hasAge"), new NumericLiteralImpl(12, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://hasAge"), new NumericLiteralImpl(43, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+
+        for(final Statement triple : triples) {
+            ryaConn.add(triple);
+        }
+
+        // Create a PCJ table will include those triples in its results.
+        final String sparql =
+                "SELECT ?name ?age " +
+                "{" +
+                  "FILTER(?age < 30) ." +
+                  "?name <http://hasAge> ?age." +
+                  "?name <http://playsSport> \"Soccer\" " +
+                "}";
+
+        final String pcjTableName = new PcjTableNameFactory().makeTableName(prefix, "testPcj");
+
+        // Create and populate the PCJ table.
+        PcjIntegrationTestingUtil.createAndPopulatePcj(ryaConn, accumuloConn, pcjTableName, sparql, new String[]{"name", "age"}, Optional.<PcjVarOrderFactory>absent());
+
+        final AccumuloIndexSet ais = new AccumuloIndexSet(accumuloConn, pcjTableName);
+
+        final QueryBindingSet bs = new QueryBindingSet();
+        bs.addBinding("birthDate",new LiteralImpl("1983-03-17",new URIImpl("http://www.w3.org/2001/XMLSchema#date")));
+        bs.addBinding("name",new URIImpl("http://Alice"));
+
+        final QueryBindingSet bs2 = new QueryBindingSet();
+        bs2.addBinding("birthDate",new LiteralImpl("1983-04-18",new URIImpl("http://www.w3.org/2001/XMLSchema#date")));
+        bs2.addBinding("name",new URIImpl("http://Bob"));
+
+        final Set<BindingSet> bSets = Sets.<BindingSet>newHashSet(bs,bs2);
+
+        final CloseableIteration<BindingSet, QueryEvaluationException> results = ais.evaluate(bSets);
+
+        final QueryBindingSet alice = new QueryBindingSet();
+        alice.addBinding("name", new URIImpl("http://Alice"));
+        alice.addBinding("age", new NumericLiteralImpl(14, XMLSchema.INTEGER));
+        alice.addBinding("birthDate", new LiteralImpl("1983-03-17",new URIImpl("http://www.w3.org/2001/XMLSchema#date")));
+
+        final QueryBindingSet bob = new QueryBindingSet();
+        bob.addBinding("name", new URIImpl("http://Bob"));
+        bob.addBinding("age", new NumericLiteralImpl(16, XMLSchema.INTEGER));
+        bob.addBinding("birthDate", new LiteralImpl("1983-04-18",new URIImpl("http://www.w3.org/2001/XMLSchema#date")));
+
+
+        final Set<BindingSet> fetchedResults = new HashSet<>();
+        while(results.hasNext()) {
+        	final BindingSet next = results.next();
+        	System.out.println(next);
+        	fetchedResults.add(next);
+        }
+
+        Assert.assertEquals(Sets.<BindingSet>newHashSet(alice,bob), fetchedResults);
+    }
+
+
+
+    @Test
+    public void accumuloIndexSetTestWithNoBindingSet() throws RepositoryException, PcjException, TableNotFoundException,
+    RyaTypeResolverException, MalformedQueryException, SailException, QueryEvaluationException, MutationsRejectedException {
+        // Load some Triples into Rya.
+        final Set<Statement> triples = new HashSet<>();
+        triples.add( new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://hasAge"), new NumericLiteralImpl(14, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://hasAge"), new NumericLiteralImpl(16, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://hasAge"), new NumericLiteralImpl(12, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://hasAge"), new NumericLiteralImpl(43, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+
+        for(final Statement triple : triples) {
+            ryaConn.add(triple);
+        }
+
+        // Create a PCJ table will include those triples in its results.
+        final String sparql =
+                "SELECT ?name ?age " +
+                "{" +
+                  "FILTER(?age < 30) ." +
+                  "?name <http://hasAge> ?age." +
+                  "?name <http://playsSport> \"Soccer\" " +
+                "}";
+
+        final String pcjTableName = new PcjTableNameFactory().makeTableName(prefix, "testPcj");
+
+        // Create and populate the PCJ table.
+        PcjIntegrationTestingUtil.createAndPopulatePcj(ryaConn, accumuloConn, pcjTableName, sparql, new String[]{"name", "age"}, Optional.<PcjVarOrderFactory>absent());
+
+        final AccumuloIndexSet ais = new AccumuloIndexSet(accumuloConn, pcjTableName);
+
+        final CloseableIteration<BindingSet, QueryEvaluationException> results = ais.evaluate(new HashSet<BindingSet>());
+
+        Assert.assertEquals(false, results.hasNext());
+
+    }
+
+
+    @Test
+    public void accumuloIndexSetTestWithDirectProductBindingSet() throws RepositoryException, PcjException, TableNotFoundException,
+    RyaTypeResolverException, MalformedQueryException, SailException, QueryEvaluationException, MutationsRejectedException {
+        // Load some Triples into Rya.
+        final Set<Statement> triples = new HashSet<>();
+        triples.add( new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://hasAge"), new NumericLiteralImpl(14, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://hasAge"), new NumericLiteralImpl(16, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://hasAge"), new NumericLiteralImpl(12, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://hasAge"), new NumericLiteralImpl(43, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+
+        for(final Statement triple : triples) {
+            ryaConn.add(triple);
+        }
+
+        // Create a PCJ table will include those triples in its results.
+        final String sparql =
+                "SELECT ?name ?age " +
+                "{" +
+                  "FILTER(?age < 30) ." +
+                  "?name <http://hasAge> ?age." +
+                  "?name <http://playsSport> \"Soccer\" " +
+                "}";
+
+        final String pcjTableName = new PcjTableNameFactory().makeTableName(prefix, "testPcj");
+
+        // Create and populate the PCJ table.
+        PcjIntegrationTestingUtil.createAndPopulatePcj(ryaConn, accumuloConn, pcjTableName, sparql, new String[]{"name", "age"}, Optional.<PcjVarOrderFactory>absent());
+
+        final AccumuloIndexSet ais = new AccumuloIndexSet(accumuloConn, pcjTableName);
+
+        final QueryBindingSet bs = new QueryBindingSet();
+        bs.addBinding("birthDate",new LiteralImpl("1983-03-17",new URIImpl("http://www.w3.org/2001/XMLSchema#date")));
+        bs.addBinding("location",new URIImpl("http://Virginia"));
+
+        final CloseableIteration<BindingSet, QueryEvaluationException> results = ais.evaluate(bs);
+
+        final QueryBindingSet alice = new QueryBindingSet();
+        alice.addBinding("name", new URIImpl("http://Alice"));
+        alice.addBinding("age", new NumericLiteralImpl(14, XMLSchema.INTEGER));
+        alice.addAll(bs);
+
+        final QueryBindingSet bob = new QueryBindingSet();
+        bob.addBinding("name", new URIImpl("http://Bob"));
+        bob.addBinding("age", new NumericLiteralImpl(16, XMLSchema.INTEGER));
+        bob.addAll(bs);
+
+        final QueryBindingSet charlie = new QueryBindingSet();
+        charlie.addBinding("name", new URIImpl("http://Charlie"));
+        charlie.addBinding("age", new NumericLiteralImpl(12, XMLSchema.INTEGER));
+        charlie.addAll(bs);
+
+        final Set<BindingSet> fetchedResults = new HashSet<>();
+        while(results.hasNext()) {
+        	fetchedResults.add(results.next());
+        }
+        Assert.assertEquals(3,fetchedResults.size());
+        Assert.assertEquals(Sets.<BindingSet>newHashSet(alice,bob,charlie), fetchedResults);
+    }
+
+    @Test
+    public void accumuloIndexSetTestWithTwoDirectProductBindingSet() throws RepositoryException, PcjException, TableNotFoundException,
+    RyaTypeResolverException, MalformedQueryException, SailException, QueryEvaluationException, MutationsRejectedException {
+        // Load some Triples into Rya.
+        final Set<Statement> triples = new HashSet<>();
+        triples.add( new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://hasAge"), new NumericLiteralImpl(14, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://hasAge"), new NumericLiteralImpl(16, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://hasAge"), new NumericLiteralImpl(12, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://hasAge"), new NumericLiteralImpl(43, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+
+        for(final Statement triple : triples) {
+            ryaConn.add(triple);
+        }
+
+        // Create a PCJ table will include those triples in its results.
+        final String sparql =
+                "SELECT ?name ?age " +
+                "{" +
+                  "FILTER(?age < 30) ." +
+                  "?name <http://hasAge> ?age." +
+                  "?name <http://playsSport> \"Soccer\" " +
+                "}";
+
+        final String pcjTableName = new PcjTableNameFactory().makeTableName(prefix, "testPcj");
+
+        // Create and populate the PCJ table.
+        PcjIntegrationTestingUtil.createAndPopulatePcj(ryaConn, accumuloConn, pcjTableName, sparql, new String[]{"name", "age"}, Optional.<PcjVarOrderFactory>absent());
+
+        final AccumuloIndexSet ais = new AccumuloIndexSet(accumuloConn, pcjTableName);
+
+        final QueryBindingSet bs = new QueryBindingSet();
+        bs.addBinding("birthDate",new LiteralImpl("1983-03-17",new URIImpl("http://www.w3.org/2001/XMLSchema#date")));
+        bs.addBinding("location",new URIImpl("http://Virginia"));
+
+        final QueryBindingSet bs2 = new QueryBindingSet();
+        bs2.addBinding("birthDate",new LiteralImpl("1983-04-18",new URIImpl("http://www.w3.org/2001/XMLSchema#date")));
+        bs2.addBinding("location",new URIImpl("http://Georgia"));
+
+        final Set<BindingSet> bSets = Sets.<BindingSet>newHashSet(bs,bs2);
+
+        final CloseableIteration<BindingSet, QueryEvaluationException> results = ais.evaluate(bSets);
+
+        final QueryBindingSet alice1 = new QueryBindingSet();
+        alice1.addBinding("name", new URIImpl("http://Alice"));
+        alice1.addBinding("age", new NumericLiteralImpl(14, XMLSchema.INTEGER));
+        alice1.addAll(bs);
+
+        final QueryBindingSet bob1 = new QueryBindingSet();
+        bob1.addBinding("name", new URIImpl("http://Bob"));
+        bob1.addBinding("age", new NumericLiteralImpl(16, XMLSchema.INTEGER));
+        bob1.addAll(bs);
+
+        final QueryBindingSet charlie1 = new QueryBindingSet();
+        charlie1.addBinding("name", new URIImpl("http://Charlie"));
+        charlie1.addBinding("age", new NumericLiteralImpl(12, XMLSchema.INTEGER));
+        charlie1.addAll(bs);
+
+        final QueryBindingSet alice2 = new QueryBindingSet();
+        alice2.addBinding("name", new URIImpl("http://Alice"));
+        alice2.addBinding("age", new NumericLiteralImpl(14, XMLSchema.INTEGER));
+        alice2.addAll(bs2);
+
+        final QueryBindingSet bob2 = new QueryBindingSet();
+        bob2.addBinding("name", new URIImpl("http://Bob"));
+        bob2.addBinding("age", new NumericLiteralImpl(16, XMLSchema.INTEGER));
+        bob2.addAll(bs2);
+
+        final QueryBindingSet charlie2 = new QueryBindingSet();
+        charlie2.addBinding("name", new URIImpl("http://Charlie"));
+        charlie2.addBinding("age", new NumericLiteralImpl(12, XMLSchema.INTEGER));
+        charlie2.addAll(bs2);
+
+        final Set<BindingSet> fetchedResults = new HashSet<>();
+        while(results.hasNext()) {
+        	final BindingSet next = results.next();
+        	System.out.println(next);
+        	fetchedResults.add(next);
+        }
+
+        Assert.assertEquals(Sets.<BindingSet>newHashSet(alice1,bob1,charlie1,alice2,bob2,charlie2), fetchedResults);
+    }
+
+
+
+    @Test
+    public void accumuloIndexSetTestWithTwoDirectProductBindingSetsWithMapping() throws RepositoryException, PcjException, TableNotFoundException,
+    RyaTypeResolverException, MalformedQueryException, SailException, QueryEvaluationException, MutationsRejectedException {
+        // Load some Triples into Rya.
+        final Set<Statement> triples = new HashSet<>();
+        triples.add( new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://hasAge"), new NumericLiteralImpl(14, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://hasAge"), new NumericLiteralImpl(16, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://hasAge"), new NumericLiteralImpl(12, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://hasAge"), new NumericLiteralImpl(43, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+
+        for(final Statement triple : triples) {
+            ryaConn.add(triple);
+        }
+
+        // Create a PCJ table will include those triples in its results.
+        final String sparql =
+                "SELECT ?name ?age " +
+                "{" +
+                  "FILTER(?age < 30) ." +
+                  "?name <http://hasAge> ?age." +
+                  "?name <http://playsSport> \"Soccer\" " +
+                "}";
+
+        final String pcjTableName = new PcjTableNameFactory().makeTableName(prefix, "testPcj");
+
+        // Create and populate the PCJ table.
+        PcjIntegrationTestingUtil.createAndPopulatePcj(ryaConn, accumuloConn, pcjTableName, sparql, new String[]{"name", "age"}, Optional.<PcjVarOrderFactory>absent());
+
+        final String sparql2 =
+                "SELECT ?x ?y " +
+                "{" +
+                  "FILTER(?y < 30) ." +
+                  "?x <http://hasAge> ?y." +
+                  "?x <http://playsSport> \"Soccer\" " +
+                "}";
+
+        final SPARQLParser p = new SPARQLParser();
+        final ParsedQuery pq = p.parseQuery(sparql2, null);
+
+        final Map<String,String> map = new HashMap<>();
+        map.put("x", "name");
+        map.put("y", "age");
+        final AccumuloIndexSet ais = new AccumuloIndexSet(accumuloConn, pcjTableName);
+        ais.setProjectionExpr((Projection) pq.getTupleExpr());
+        ais.setTableVarMap(map);
+        ais.setSupportedVariableOrderMap(Lists.<String>newArrayList("x;y","y;x"));
+
+        final QueryBindingSet bs = new QueryBindingSet();
+        bs.addBinding("birthDate",new LiteralImpl("1983-03-17",new URIImpl("http://www.w3.org/2001/XMLSchema#date")));
+        bs.addBinding("x",new URIImpl("http://Alice"));
+
+        final QueryBindingSet bs2 = new QueryBindingSet();
+        bs2.addBinding("birthDate",new LiteralImpl("1983-04-18",new URIImpl("http://www.w3.org/2001/XMLSchema#date")));
+        bs2.addBinding("x",new URIImpl("http://Bob"));
+
+        final Set<BindingSet> bSets = Sets.<BindingSet>newHashSet(bs,bs2);
+
+        final CloseableIteration<BindingSet, QueryEvaluationException> results = ais.evaluate(bSets);
+
+        final QueryBindingSet alice = new QueryBindingSet();
+        alice.addBinding("x", new URIImpl("http://Alice"));
+        alice.addBinding("y", new NumericLiteralImpl(14, XMLSchema.INTEGER));
+        alice.addBinding("birthDate", new LiteralImpl("1983-03-17",new URIImpl("http://www.w3.org/2001/XMLSchema#date")));
+
+        final QueryBindingSet bob = new QueryBindingSet();
+        bob.addBinding("x", new URIImpl("http://Bob"));
+        bob.addBinding("y", new NumericLiteralImpl(16, XMLSchema.INTEGER));
+        bob.addBinding("birthDate", new LiteralImpl("1983-04-18",new URIImpl("http://www.w3.org/2001/XMLSchema#date")));
+
+
+        final Set<BindingSet> fetchedResults = new HashSet<>();
+        while(results.hasNext()) {
+        	final BindingSet next = results.next();
+        	System.out.println(next);
+        	fetchedResults.add(next);
+        }
+
+        Assert.assertEquals(Sets.<BindingSet>newHashSet(alice,bob), fetchedResults);
+    }
+
+
+
+    @Test
+    public void accumuloIndexSetTestWithTwoDirectProductBindingSetsWithConstantMapping() throws Exception {
+        // Load some Triples into Rya.
+        final Set<Statement> triples = new HashSet<>();
+        triples.add( new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://hasAge"), new NumericLiteralImpl(14, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://hasAge"), new NumericLiteralImpl(16, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://hasAge"), new NumericLiteralImpl(12, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://hasAge"), new NumericLiteralImpl(43, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+
+        for(final Statement triple : triples) {
+            ryaConn.add(triple);
+        }
+
+        // Create a PCJ table will include those triples in its results.
+        final String sparql =
+                "SELECT ?name ?age " +
+                "{" +
+                  "?name <http://hasAge> ?age." +
+                  "?name <http://playsSport> \"Soccer\" " +
+                "}";
+
+        final String pcjTableName = new PcjTableNameFactory().makeTableName(prefix, "testPcj");
+
+        // Create and populate the PCJ table.
+        PcjIntegrationTestingUtil.createAndPopulatePcj(ryaConn, accumuloConn, pcjTableName, sparql, new String[]{"name", "age"}, Optional.<PcjVarOrderFactory>absent());
+
+        final String sparql2 =
+                "SELECT ?x " +
+                "{" +
+                  "?x <http://hasAge> 16 ." +
+                  "?x <http://playsSport> \"Soccer\" " +
+                "}";
+
+        final SPARQLParser p = new SPARQLParser();
+        final ParsedQuery pq1 = p.parseQuery(sparql, null);
+        final ParsedQuery pq2 = p.parseQuery(sparql2, null);
+
+        final AccumuloIndexSet ais = new AccumuloIndexSet(accumuloConn, pcjTableName);
+        ais.setProjectionExpr((Projection) QueryVariableNormalizer.getNormalizedIndex(pq2.getTupleExpr(), pq1.getTupleExpr()).get(0));
+
+        final QueryBindingSet bs = new QueryBindingSet();
+        bs.addBinding("birthDate",new LiteralImpl("1983-03-17",new URIImpl("http://www.w3.org/2001/XMLSchema#date")));
+        bs.addBinding("x",new URIImpl("http://Alice"));
+
+        final QueryBindingSet bs2 = new QueryBindingSet();
+        bs2.addBinding("birthDate",new LiteralImpl("1983-04-18",new URIImpl("http://www.w3.org/2001/XMLSchema#date")));
+        bs2.addBinding("x",new URIImpl("http://Bob"));
+
+        final Set<BindingSet> bSets = Sets.<BindingSet>newHashSet(bs,bs2);
+
+        final CloseableIteration<BindingSet, QueryEvaluationException> results = ais.evaluate(bSets);
+
+        final Set<BindingSet> fetchedResults = new HashSet<>();
+        while(results.hasNext()) {
+        	final BindingSet next = results.next();
+        	fetchedResults.add(next);
+        }
+
+        Assert.assertEquals(Sets.<BindingSet>newHashSet(bs2), fetchedResults);
+    }
+
+
+
+    @Test
+    public void accumuloIndexSetTestAttemptJoinAccrossTypes() throws Exception {
+        // Load some Triples into Rya.
+        final Set<Statement> triples = new HashSet<>();
+        triples.add( new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://hasAge"), new NumericLiteralImpl(14, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://hasAge"), new NumericLiteralImpl(16, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+
+        for(final Statement triple : triples) {
+            ryaConn.add(triple);
+        }
+
+        // Create a PCJ table will include those triples in its results.
+        final String sparql =
+                "SELECT ?name ?age " +
+                "{" +
+                  "?name <http://hasAge> ?age." +
+                  "?name <http://playsSport> \"Soccer\" " +
+                "}";
+
+        final String pcjTableName = new PcjTableNameFactory().makeTableName(prefix, "testPcj");
+
+        // Create and populate the PCJ table.
+        PcjIntegrationTestingUtil.createAndPopulatePcj(ryaConn, accumuloConn, pcjTableName, sparql, new String[]{"name", "age"}, Optional.<PcjVarOrderFactory>absent());
+        AccumuloIndexSet ais = new AccumuloIndexSet(accumuloConn,pcjTableName);
+
+        final QueryBindingSet bs1 = new QueryBindingSet();
+        bs1.addBinding("age",new LiteralImpl("16"));
+        final QueryBindingSet bs2 = new QueryBindingSet();
+        bs2.addBinding("age",new NumericLiteralImpl(14, XMLSchema.INTEGER));
+
+        final Set<BindingSet> bSets = Sets.<BindingSet>newHashSet(bs1,bs2);
+
+        final CloseableIteration<BindingSet, QueryEvaluationException> results = ais.evaluate(bSets);
+
+        final Set<BindingSet> fetchedResults = new HashSet<>();
+        while(results.hasNext()) {
+        	final BindingSet next = results.next();
+        	fetchedResults.add(next);
+        }
+
+        bs2.addBinding("name", new URIImpl("http://Alice"));
+        Assert.assertEquals(Sets.<BindingSet>newHashSet(bs2), fetchedResults);
+    }
+
+
+
+
+
+
+
+    @After
+    public void close() throws RepositoryException {
+    	ryaConn.close();
+    	ryaRepo.shutDown();
+    }
+
+
+    private static Configuration getConf() {
+        final AccumuloRdfConfiguration conf = new AccumuloRdfConfiguration();
+        conf.setBoolean(ConfigUtils.USE_MOCK_INSTANCE, true);
+        conf.set(RdfCloudTripleStoreConfiguration.CONF_TBL_PREFIX, "rya_");
+        conf.set(ConfigUtils.CLOUDBASE_USER, "root");
+        conf.set(ConfigUtils.CLOUDBASE_PASSWORD, "");
+        conf.set(ConfigUtils.CLOUDBASE_INSTANCE, "instance");
+        conf.set(ConfigUtils.CLOUDBASE_AUTHS, "");
+        return conf;
+    }
+
+
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/AccumuloPcjSerialzerTest.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/AccumuloPcjSerialzerTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/AccumuloPcjSerialzerTest.java
new file mode 100644
index 0000000..9fb5126
--- /dev/null
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/AccumuloPcjSerialzerTest.java
@@ -0,0 +1,114 @@
+package mvm.rya.indexing.external.tupleSet;
+
+import mvm.rya.api.resolver.RyaTypeResolverException;
+
+import org.junit.Test;
+import org.openrdf.model.impl.LiteralImpl;
+import org.openrdf.model.impl.URIImpl;
+import org.openrdf.query.BindingSet;
+import org.openrdf.query.algebra.evaluation.QueryBindingSet;
+
+import com.vividsolutions.jts.util.Assert;
+
+public class AccumuloPcjSerialzerTest {
+
+	@Test
+	public void basicShortUriBsTest() {
+		final QueryBindingSet bs = new QueryBindingSet();
+		bs.addBinding("X",new URIImpl("http://uri1"));
+		bs.addBinding("Y",new URIImpl("http://uri2"));
+		final String[] varOrder = new String[]{"X","Y"};
+		try {
+			final byte[] byteVal = AccumuloPcjSerializer.serialize(bs, varOrder);
+			final BindingSet newBs = AccumuloPcjSerializer.deSerialize(byteVal, varOrder);
+			Assert.equals(bs, newBs);
+		} catch (final RyaTypeResolverException e) {
+			e.printStackTrace();
+		}
+	}
+
+	@Test
+	public void basicLongUriBsTest() {
+		final QueryBindingSet bs = new QueryBindingSet();
+		bs.addBinding("X",new URIImpl("http://uri1"));
+		bs.addBinding("Y",new URIImpl("http://uri2"));
+		bs.addBinding("Z",new URIImpl("http://uri3"));
+		bs.addBinding("A",new URIImpl("http://uri4"));
+		bs.addBinding("B",new URIImpl("http://uri5"));
+		final String[] varOrder = new String[]{"X","Y","Z","A","B"};
+		try {
+			final byte[] byteVal = AccumuloPcjSerializer.serialize(bs, varOrder);
+			final BindingSet newBs = AccumuloPcjSerializer.deSerialize(byteVal, varOrder);
+			Assert.equals(bs, newBs);
+		} catch (final RyaTypeResolverException e) {
+			e.printStackTrace();
+		}
+	}
+
+	@Test
+	public void basicShortStringLiteralBsTest() {
+		final QueryBindingSet bs = new QueryBindingSet();
+		bs.addBinding("X",new LiteralImpl("literal1"));
+		bs.addBinding("Y",new LiteralImpl("literal2"));
+		final String[] varOrder = new String[]{"X","Y"};
+		try {
+			final byte[] byteVal = AccumuloPcjSerializer.serialize(bs, varOrder);
+			final BindingSet newBs = AccumuloPcjSerializer.deSerialize(byteVal, varOrder);
+			Assert.equals(bs, newBs);
+		} catch (final RyaTypeResolverException e) {
+			e.printStackTrace();
+		}
+	}
+
+	@Test
+	public void basicShortMixLiteralBsTest() {
+		final QueryBindingSet bs = new QueryBindingSet();
+		bs.addBinding("X",new LiteralImpl("literal1"));
+		bs.addBinding("Y",new LiteralImpl("5", new URIImpl("http://www.w3.org/2001/XMLSchema#integer")));
+		final String[] varOrder = new String[]{"X","Y"};
+		try {
+			final byte[] byteVal = AccumuloPcjSerializer.serialize(bs, varOrder);
+			final BindingSet newBs = AccumuloPcjSerializer.deSerialize(byteVal, varOrder);
+			Assert.equals(bs, newBs);
+		} catch (final RyaTypeResolverException e) {
+			e.printStackTrace();
+		}
+	}
+
+	@Test
+	public void basicLongMixLiteralBsTest() {
+		final QueryBindingSet bs = new QueryBindingSet();
+		bs.addBinding("X",new LiteralImpl("literal1"));
+		bs.addBinding("Y",new LiteralImpl("5", new URIImpl("http://www.w3.org/2001/XMLSchema#integer")));
+		bs.addBinding("Z",new LiteralImpl("5.0", new URIImpl("http://www.w3.org/2001/XMLSchema#double")));
+		bs.addBinding("W",new LiteralImpl("1000", new URIImpl("http://www.w3.org/2001/XMLSchema#long")));
+		final String[] varOrder = new String[]{"W","X","Y","Z"};
+		try {
+			final byte[] byteVal = AccumuloPcjSerializer.serialize(bs, varOrder);
+			final BindingSet newBs = AccumuloPcjSerializer.deSerialize(byteVal, varOrder);
+			Assert.equals(bs, newBs);
+		} catch (final RyaTypeResolverException e) {
+			e.printStackTrace();
+		}
+	}
+
+	@Test
+	public void basicMixUriLiteralBsTest() {
+		final QueryBindingSet bs = new QueryBindingSet();
+		bs.addBinding("X",new LiteralImpl("literal1"));
+		bs.addBinding("Y",new LiteralImpl("5", new URIImpl("http://www.w3.org/2001/XMLSchema#integer")));
+		bs.addBinding("Z",new LiteralImpl("5.0", new URIImpl("http://www.w3.org/2001/XMLSchema#double")));
+		bs.addBinding("W",new LiteralImpl("1000", new URIImpl("http://www.w3.org/2001/XMLSchema#long")));
+		bs.addBinding("A",new URIImpl("http://uri1"));
+		bs.addBinding("B",new URIImpl("http://uri2"));
+		bs.addBinding("C",new URIImpl("http://uri3"));
+		final String[] varOrder = new String[]{"A","W","X","Y","Z","B","C"};
+		try {
+			final byte[] byteVal = AccumuloPcjSerializer.serialize(bs, varOrder);
+			final BindingSet newBs = AccumuloPcjSerializer.deSerialize(byteVal, varOrder);
+			Assert.equals(bs, newBs);
+		} catch (final RyaTypeResolverException e) {
+			e.printStackTrace();
+		}
+	}
+}



[15/16] incubator-rya git commit: RYA-32 Improve how metadata and values are written to Accumulo PCJ tables

Posted by mi...@apache.org.
RYA-32 Improve how metadata and values are written to Accumulo PCJ tables


Project: http://git-wip-us.apache.org/repos/asf/incubator-rya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-rya/commit/c12f58f4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-rya/tree/c12f58f4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-rya/diff/c12f58f4

Branch: refs/heads/develop
Commit: c12f58f46be0aed3c88eff7504d4bfd4e4f295e9
Parents: 4b7bd4f
Author: Caleb Meier <me...@gmail.com>
Authored: Fri Jan 29 11:40:59 2016 -0500
Committer: Caleb Meier <me...@gmail.com>
Committed: Fri Jan 29 11:40:59 2016 -0500

----------------------------------------------------------------------
 extras/indexing/pom.xml                         |    7 +
 .../accumulo/precompQuery/AccumuloPcjQuery.java |  352 ++
 .../AccumuloPrecompQueryIndexer.java            |  326 --
 .../GeneralizedExternalProcessor.java           |  156 +-
 .../IndexPlanValidator/IndexListPruner.java     |   12 +-
 .../IndexedExecutionPlanGenerator.java          |  109 +-
 .../ValidIndexCombinationGenerator.java         | 1054 ++---
 .../VarConstantIndexListPruner.java             |   61 +-
 .../main/java/mvm/rya/indexing/PcjQuery.java    |   40 +
 .../mvm/rya/indexing/PrecompQueryIndexer.java   |   63 -
 .../accumulo/entity/EntityOptimizer.java        |   48 +-
 .../accumulo/entity/EntityTupleSet.java         |   73 +-
 .../rya/indexing/accumulo/entity/StarQuery.java |  342 +-
 .../rya/indexing/accumulo/geo/GeoTupleSet.java  |   53 +-
 .../indexing/external/ExternalIndexMain.java    |  219 -
 .../indexing/external/ExternalProcessor.java    |  726 ---
 .../mvm/rya/indexing/external/ExternalSail.java |   86 -
 .../indexing/external/ExternalSailExample.java  |  124 -
 .../indexing/external/PrecompJoinOptimizer.java | 1474 +++---
 .../external/tupleSet/AccumuloIndexSet.java     |  775 ++--
 .../tupleSet/AccumuloPcjSerializer.java         |  103 +
 .../external/tupleSet/ExternalTupleSet.java     |  241 +-
 .../indexing/external/tupleSet/PcjTables.java   |  800 ++++
 .../tupleSet/SimpleExternalTupleSet.java        |  118 +-
 .../GeneralizedExternalProcessorTest.java       |  418 +-
 .../IndexPlanValidatorTest.java                 | 1856 +++-----
 .../IndexedExecutionPlanGeneratorTest.java      |  717 ++-
 .../ThreshholdPlanSelectorTest.java             | 1465 +++---
 .../TupleExecutionPlanGeneratorTest.java        |  138 +-
 .../IndexPlanValidator/TupleReArrangerTest.java |   83 +-
 .../ValidIndexCombinationGeneratorTest.java     | 1083 ++---
 .../VarConstantIndexListPrunerTest.java         |  547 ++-
 .../external/AccumuloConstantIndexSetTest.java  |  831 ----
 .../AccumuloConstantPcjIntegrationTest.java     |  410 ++
 .../indexing/external/AccumuloIndexSetTest.java | 4330 ------------------
 .../external/AccumuloIndexSetTest2.java         |  803 ----
 .../external/AccumuloPcjIntegrationTest.java    | 1426 ++++++
 .../external/PcjIntegrationTestingUtil.java     |  385 ++
 .../PrecompJoinOptimizerIntegrationTest.java    |  939 ++--
 .../external/PrecompJoinOptimizerTest.java      |  361 +-
 .../external/PrecompJoinOptimizerTest2.java     | 1130 +++++
 .../PrecompJoinOptimizerVarToConstTest.java     |  430 ++
 .../external/tupleSet/AccumuloIndexSetTest.java |  678 +++
 .../tupleSet/AccumuloPcjSerialzerTest.java      |  114 +
 .../tupleSet/ExternalProcessorTest.java         | 1654 -------
 .../tupleSet/PcjTablesIntegrationTests.java     |  413 ++
 .../external/tupleSet/PcjTablesTests.java       |   65 +
 .../tupleSet/VarConstExternalProcessorTest.java |  490 --
 .../src/main/java/RyaDirectExample.java         | 1521 +++---
 49 files changed, 12568 insertions(+), 17081 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/pom.xml
----------------------------------------------------------------------
diff --git a/extras/indexing/pom.xml b/extras/indexing/pom.xml
index f484916..3bfa0ce 100644
--- a/extras/indexing/pom.xml
+++ b/extras/indexing/pom.xml
@@ -81,6 +81,13 @@ under the License.
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
+        
+        <dependency>
+            <groupId>org.apache.accumulo</groupId>
+            <artifactId>accumulo-minicluster</artifactId>
+            <version>${accumulo.version}</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
     <build>
         <plugins>

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/accumulo/precompQuery/AccumuloPcjQuery.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/accumulo/precompQuery/AccumuloPcjQuery.java b/extras/indexing/src/main/java/mvm/rya/accumulo/precompQuery/AccumuloPcjQuery.java
new file mode 100644
index 0000000..08c0f78
--- /dev/null
+++ b/extras/indexing/src/main/java/mvm/rya/accumulo/precompQuery/AccumuloPcjQuery.java
@@ -0,0 +1,352 @@
+package mvm.rya.accumulo.precompQuery;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ *
+ */
+import info.aduna.iteration.CloseableIteration;
+import info.aduna.iteration.Iteration;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.NoSuchElementException;
+import java.util.Set;
+
+import mvm.rya.api.resolver.RyaTypeResolverException;
+import mvm.rya.indexing.PcjQuery;
+import mvm.rya.indexing.external.tupleSet.AccumuloIndexSet;
+import mvm.rya.indexing.external.tupleSet.AccumuloPcjSerializer;
+import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
+
+import org.apache.accumulo.core.client.BatchScanner;
+import org.apache.accumulo.core.client.Connector;
+import org.apache.accumulo.core.client.TableNotFoundException;
+import org.apache.accumulo.core.data.Key;
+import org.apache.accumulo.core.data.Range;
+import org.apache.accumulo.core.data.Value;
+import org.apache.accumulo.core.security.Authorizations;
+import org.apache.hadoop.io.Text;
+import org.openrdf.query.BindingSet;
+import org.openrdf.query.QueryEvaluationException;
+import org.openrdf.query.algebra.evaluation.QueryBindingSet;
+
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+
+/**
+ * This class encapsulates how pre-computed join tables are used during query
+ * evaluation. The method
+ * {@link AccumuloPcjQuery#queryPrecompJoin(List, String, Map, Map, Collection)
+ * is used by {@link AccumuloIndexSet#evaluate(BindingSet)} to evaluate the
+ * {@link AccumuloIndexSet#getTupleExpr()} associated with the Accumulo
+ * pre-computed join table. Given the {@link BindingSet} constraints, it uses
+ * the variables common to the BindingSet constraints and the pre-computed join
+ * table TupleExpr to build a {@Range} prefix to scan the pre-computed
+ * join table to obtain results for the constrained sub-query.
+ *
+ */
+public class AccumuloPcjQuery implements PcjQuery {
+	private final Connector accCon;
+	private final String tableName;
+
+	public AccumuloPcjQuery(Connector accCon, String tableName) {
+		this.accCon = accCon;
+		this.tableName = tableName;
+	}
+
+	/**
+	 * @param commonVars - variables common to bsConstraints and table in terms of query variables
+	 * @param localityGroup - the column family to scan in terms of table variables
+	 * @param valMap - Literal type associated with constant constraints
+	 * @param varMap - map query variables to pre-computed join table variables
+	 * @param bsConstraints - binding set constraints
+	 * @return {@link Iteration} over result BindingSets
+	 */
+	@Override
+	public CloseableIteration<BindingSet, QueryEvaluationException> queryPrecompJoin(
+			List<String> commonVars, String localityGroup,
+			Map<String, org.openrdf.model.Value> valMap,
+			Map<String, String> varMap, Collection<BindingSet> bsConstraints)
+			throws QueryEvaluationException, TableNotFoundException {
+
+		final Iterator<Entry<Key, Value>> accIter;
+		final Map<String, org.openrdf.model.Value> constValMap = valMap;
+		final HashMultimap<Range, BindingSet> map = HashMultimap.create();
+
+		final List<BindingSet> extProdList = Lists.newArrayList();
+		final List<String> prefixVars = commonVars;
+		final BatchScanner bs = accCon.createBatchScanner(tableName,
+				new Authorizations(), 10);
+		final Set<Range> ranges = Sets.newHashSet();
+		final Map<String, String> tableVarMap = varMap;
+		final boolean bsContainsPrefixVar = bindingsContainsPrefixVar(
+				bsConstraints, prefixVars);
+
+		bs.fetchColumnFamily(new Text(localityGroup));
+		// process bindingSet and constant constraints
+		for (final BindingSet bSet : bsConstraints) {
+			// bindings sets and PCJ have common vars
+			if (bsContainsPrefixVar) {
+				byte[] rangePrefix = null;
+				final QueryBindingSet rangeBs = new QueryBindingSet();
+				for (final String var : prefixVars) {
+					if (var.startsWith(ExternalTupleSet.CONST_PREFIX)) {
+						rangeBs.addBinding(var, constValMap.get(var));
+					} else {
+						rangeBs.addBinding(var, bSet.getBinding(var).getValue());
+					}
+				}
+				try {
+					rangePrefix = AccumuloPcjSerializer.serialize(rangeBs,
+							commonVars.toArray(new String[commonVars.size()]));
+				} catch (final RyaTypeResolverException e) {
+					e.printStackTrace();
+				}
+				final Range r = Range.prefix(new Text(rangePrefix));
+				map.put(r, bSet);
+				ranges.add(r);
+				// non-empty binding sets and no common vars with no constant
+				// constraints
+			} else if (bSet.size() > 0 && prefixVars.size() == 0) {
+				extProdList.add(bSet);
+			}
+		}
+
+		// constant constraints and no bindingSet constraints
+		// add range of entire table if no constant constraints and
+		// bsConstraints consists of single, empty set (occurs when AIS is
+		// first node evaluated in query)
+		if (ranges.isEmpty()) {
+			// constant constraints
+			if (prefixVars.size() > 0) {
+				byte[] rangePrefix = null;
+				final QueryBindingSet rangeBs = new QueryBindingSet();
+				for (final String var : prefixVars) {
+					if (var.startsWith(ExternalTupleSet.CONST_PREFIX)) {
+						rangeBs.addBinding(var, constValMap.get(var));
+					}
+				}
+				try {
+					rangePrefix = AccumuloPcjSerializer.serialize(rangeBs,
+							commonVars.toArray(new String[commonVars.size()]));
+				} catch (final RyaTypeResolverException e) {
+					e.printStackTrace();
+				}
+				final Range r = Range.prefix(new Text(rangePrefix));
+				ranges.add(r);
+			}
+			// no constant or bindingSet constraints
+			else {
+				ranges.add(new Range("", true, "~", false));
+			}
+		}
+
+		if (ranges.size() == 0) {
+			accIter = null;
+		} else {
+			bs.setRanges(ranges);
+			accIter = bs.iterator();
+		}
+		return new CloseableIteration<BindingSet, QueryEvaluationException>() {
+			@Override
+			public void remove() throws QueryEvaluationException {
+				throw new UnsupportedOperationException();
+			}
+
+			private Iterator<BindingSet> inputSet = null;
+			private QueryBindingSet currentSolutionBs = null;
+			private boolean hasNextCalled = false;
+			private boolean isEmpty = false;
+
+			@Override
+			public BindingSet next() throws QueryEvaluationException {
+				final QueryBindingSet bs = new QueryBindingSet();
+				if (hasNextCalled) {
+					hasNextCalled = false;
+					if (inputSet != null) {
+						bs.addAll(inputSet.next());
+					}
+					bs.addAll(currentSolutionBs);
+				} else if (isEmpty) {
+					throw new NoSuchElementException();
+				} else {
+					if (this.hasNext()) {
+						hasNextCalled = false;
+						if (inputSet != null) {
+							bs.addAll(inputSet.next());
+						}
+						bs.addAll(currentSolutionBs);
+					} else {
+						throw new NoSuchElementException();
+					}
+				}
+				return bs;
+			}
+
+			@Override
+			public boolean hasNext() throws QueryEvaluationException {
+				if (accIter == null) {
+					isEmpty = true;
+					return false;
+				}
+				if (!hasNextCalled && !isEmpty) {
+					while (accIter.hasNext() || inputSet != null
+							&& inputSet.hasNext()) {
+						if (inputSet != null && inputSet.hasNext()) {
+							hasNextCalled = true;
+							return true;
+						}
+						final Key k = accIter.next().getKey();
+						// get bindings from scan without values associated with
+						// constant constraints
+						BindingSet bs;
+						try {
+							bs = getBindingSetWithoutConstants(k, tableVarMap);
+						} catch (final RyaTypeResolverException e) {
+							throw new QueryEvaluationException(e);
+						}
+						currentSolutionBs = new QueryBindingSet();
+						currentSolutionBs.addAll(bs);
+
+						// check to see if additional bindingSet constraints
+						// exist in map
+						if (map.size() > 0) {
+							// get prefix range to retrieve remainder of
+							// bindingSet from map
+							byte[] rangePrefix;
+							try {
+								rangePrefix = getPrefixByte(bs, constValMap,
+										prefixVars);
+							} catch (final RyaTypeResolverException e) {
+								throw new QueryEvaluationException(e);
+							}
+							final Range r = Range.prefix(new Text(rangePrefix));
+							inputSet = map.get(r).iterator();
+							if (!inputSet.hasNext()) {
+								continue;
+							} else {
+								hasNextCalled = true;
+								return true;
+							}
+							// check to see if binding set constraints exist,
+							// but no common vars
+						} else if (extProdList.size() > 0) {
+							inputSet = extProdList.iterator();
+							hasNextCalled = true;
+							return true;
+						}
+						// no bindingsSet constraints--only constant constraints
+						// or none
+						else {
+							hasNextCalled = true;
+							return true;
+						}
+					}
+					isEmpty = true;
+					return false;
+				} else if (isEmpty) {
+					return false;
+				} else {
+					return true;
+				}
+			}
+
+			@Override
+			public void close() throws QueryEvaluationException {
+				bs.close();
+			}
+		};
+	}
+
+	/**
+	 *
+	 * @param bindingSets - binding set constraints
+	 * @param prefixVars - common prefix variables to table and any constant constraints
+	 * @return true if there are variables common to binding sets and table and false
+	 * if prefixVars only consists of constant constraints
+	 */
+	private boolean bindingsContainsPrefixVar(
+			Collection<BindingSet> bindingSets, List<String> prefixVars) {
+		final Iterator<BindingSet> iter = bindingSets.iterator();
+		if (iter.hasNext()) {
+			final BindingSet tempBindingSet = iter.next();
+			final Set<String> bindings = tempBindingSet.getBindingNames();
+			for (final String var : prefixVars) {
+				if (bindings.contains(var)) {
+					return true;
+				}
+			}
+		}
+		return false;
+	}
+
+	/**
+	 *
+	 * @param bs - binding set from which byte is extracted
+	 * @param valMap - map which associated Value type to constant constraint
+	 * @param prefixVars - prefix of variables common to binding sets and table and constant constraints
+	 * @return - bytes associated with values in bs that are associated with prefixVars
+	 * @throws RyaTypeResolverException
+	 */
+	private static byte[] getPrefixByte(BindingSet bs,
+			Map<String, org.openrdf.model.Value> valMap, List<String> prefixVars)
+			throws RyaTypeResolverException {
+		final QueryBindingSet bSet = new QueryBindingSet();
+		for (final String var : prefixVars) {
+			if (var.startsWith(ExternalTupleSet.CONST_PREFIX)) {
+				bSet.addBinding(var, valMap.get(var));
+			} else if (bs.getBindingNames().size() > 0
+					&& bs.getBinding(var) != null) {
+				bSet.addBinding(var, bs.getBinding(var).getValue());
+			}
+		}
+		return AccumuloPcjSerializer.serialize(bSet,
+				prefixVars.toArray(new String[prefixVars.size()]));
+	}
+
+	/**
+	 *
+	 * @param key - Accumulo key obtained from scan
+	 * @param tableVarMap - map that associated query variables and table variables
+	 * @return - BindingSet without values associated with constant constraints
+	 * @throws RyaTypeResolverException
+	 */
+	private static BindingSet getBindingSetWithoutConstants(Key key,
+			Map<String, String> tableVarMap) throws RyaTypeResolverException {
+		final byte[] row = key.getRow().getBytes();
+		final String[] varOrder = key.getColumnFamily().toString()
+				.split(ExternalTupleSet.VAR_ORDER_DELIM);
+		final QueryBindingSet temp = new QueryBindingSet(
+				AccumuloPcjSerializer.deSerialize(row, varOrder));
+		final QueryBindingSet bs = new QueryBindingSet();
+		for (final String var : temp.getBindingNames()) {
+			if (!tableVarMap.get(var).startsWith(ExternalTupleSet.CONST_PREFIX)) {
+				bs.addBinding(tableVarMap.get(var), temp.getValue(var));
+			}
+		}
+		return bs;
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/accumulo/precompQuery/AccumuloPrecompQueryIndexer.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/accumulo/precompQuery/AccumuloPrecompQueryIndexer.java b/extras/indexing/src/main/java/mvm/rya/accumulo/precompQuery/AccumuloPrecompQueryIndexer.java
deleted file mode 100644
index 86cb73e..0000000
--- a/extras/indexing/src/main/java/mvm/rya/accumulo/precompQuery/AccumuloPrecompQueryIndexer.java
+++ /dev/null
@@ -1,326 +0,0 @@
-package mvm.rya.accumulo.precompQuery;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-import info.aduna.iteration.CloseableIteration;
-
-import java.io.IOException;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.NoSuchElementException;
-import java.util.Map.Entry;
-import java.util.Set;
-
-import org.apache.accumulo.core.client.BatchScanner;
-import org.apache.accumulo.core.client.Connector;
-import org.apache.accumulo.core.client.TableNotFoundException;
-import org.apache.accumulo.core.data.Key;
-import org.apache.accumulo.core.data.Range;
-import org.apache.accumulo.core.data.Value;
-import org.apache.accumulo.core.security.Authorizations;
-import org.apache.hadoop.io.Text;
-import org.openrdf.query.Binding;
-import org.openrdf.query.BindingSet;
-import org.openrdf.query.QueryEvaluationException;
-import org.openrdf.query.algebra.evaluation.QueryBindingSet;
-
-import com.google.common.collect.HashMultimap;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Sets;
-
-import mvm.rya.indexing.PrecompQueryIndexer;
-import mvm.rya.indexing.external.tupleSet.AccumuloIndexSet.AccValueFactory;
-
-public class AccumuloPrecompQueryIndexer implements PrecompQueryIndexer {
-
-    
-    private Connector accCon;
-    private String tableName;
-    private Map<String, AccValueFactory> bindings;
-  
-    
-    
-    public AccumuloPrecompQueryIndexer(Connector accCon, String tableName) {
-        this.accCon = accCon;
-        this.tableName = tableName;
-    }
-    
-    
-    @Override
-    public void storeBindingSet(BindingSet bs) throws IOException {
-        // TODO Auto-generated method stub
-
-    }
-
-    @Override
-    public void storeBindingSets(Collection<BindingSet> bindingSets) throws IOException, IllegalArgumentException {
-        // TODO Auto-generated method stub
-
-    }
-
-    @Override
-    public CloseableIteration<BindingSet, QueryEvaluationException> queryPrecompJoin(List<String> varOrder,
-            String localityGroup, Map<String, AccValueFactory> bindings, Map<String, org.openrdf.model.Value> valMap, Collection<BindingSet> bsConstraints) 
-                    throws QueryEvaluationException, TableNotFoundException {
-        
-        
-        final int prefixLength = Integer.parseInt(varOrder.remove(varOrder.size()-1));
-        final Iterator<Entry<Key,Value>> accIter;
-        final HashMultimap<Range,BindingSet> map = HashMultimap.create();
-        final List<BindingSet> extProdList = Lists.newArrayList();
-        final Map<String, AccValueFactory> bindingMap = bindings;
-        final List<String> order = varOrder;
-        final BatchScanner bs = accCon.createBatchScanner(tableName, new Authorizations(), 10);
-        final Set<Range> ranges = Sets.newHashSet();
-        
-        
-        
-        bs.fetchColumnFamily(new Text(localityGroup));
-        
-        //process bindingSet and constant constraints
-        for (BindingSet bSet : bsConstraints) {
-            StringBuffer rangePrefix = new StringBuffer();
-            int i = 0;
-
-            for (String b : order) {
-
-                if (i >= prefixLength) {
-                    break;
-                }
-
-                if (b.startsWith("-const-")) {
-                    String val = bindings.get(b).create(valMap.get(b));
-                    rangePrefix.append(val);
-                    rangePrefix.append("\u0000");
-                } else {
-
-                    Binding v = bSet.getBinding(b);
-                    if (v == null) {
-                        throw new IllegalStateException("Binding set can't have null value!");
-                    }
-                    String val = bindings.get(b).create(bSet.getValue(b));
-                    rangePrefix.append(val);
-                    rangePrefix.append("\u0000");
-
-                }
-
-                i++;
-
-            }
-            if (rangePrefix.length() > 0) {
-                String prefixWithOutNull = rangePrefix.deleteCharAt(rangePrefix.length() - 1).toString();
-                String prefixWithNull = prefixWithOutNull + "\u0001";
-                Range r = new Range(new Key(prefixWithOutNull), true, new Key(prefixWithNull), false);
-                map.put(r, bSet);
-                ranges.add(r);
-            } else if (bSet.size() > 0) {
-                extProdList.add(bSet);
-            }
-        }
-        
-        //constant constraints and no bindingSet constraints
-        //add range of entire table if no constant constraints and
-        //bsConstraints consists of single, empty set (occurs when AIS is
-        //first node evaluated in query)
-        if (ranges.isEmpty() && bsConstraints.size() > 0) {
-
-            if (prefixLength > 0) {
-                StringBuffer rangePrefix = new StringBuffer();
-
-                int i = 0;
-                for (String b : order) {
-                    if (i >= prefixLength) {
-                        break;
-                    }
-                    if (b.startsWith("-const-")) {
-                        String val = bindings.get(b).create(valMap.get(b));
-                        rangePrefix.append(val);
-                        rangePrefix.append("\u0000");
-                    } 
-                    i++;
-                }
-
-                String prefixWithOutNull = rangePrefix.deleteCharAt(rangePrefix.length() - 1).toString();
-                String prefixWithNull = prefixWithOutNull + "\u0001";
-                Range r = new Range(new Key(prefixWithOutNull), true, new Key(prefixWithNull), false);
-                ranges.add(r);
-
-            } else { // no constant or bindingSet constraints
-                ranges.add(new Range("", true, "~", false));
-            }
-        }
-        
-        if (ranges.size() == 0) {
-            accIter = null;
-        } else {
-            bs.setRanges(ranges);
-            accIter = bs.iterator();
-        }
-
-   
-        return new CloseableIteration<BindingSet, QueryEvaluationException>() {
-
-            @Override
-            public void remove() throws QueryEvaluationException {
-                throw new UnsupportedOperationException();
-            }
-
-            private Iterator<BindingSet> inputSet = null;
-            private QueryBindingSet currentSolutionBs = null;
-            private boolean hasNextCalled = false;
-            private boolean isEmpty = false;
-           
-
-            
-            @Override
-            public BindingSet next() throws QueryEvaluationException {
-                QueryBindingSet bs = new QueryBindingSet();
-
-                if (hasNextCalled) {
-                    hasNextCalled = false;
-                    if (inputSet != null) {
-                        bs.addAll(inputSet.next());
-                    }
-                    bs.addAll(currentSolutionBs);
-                } else if (isEmpty) {
-                    throw new NoSuchElementException();
-                } else {
-                    if (this.hasNext()) {
-                        hasNextCalled = false;
-                        if (inputSet != null) {
-                            bs.addAll(inputSet.next());
-                        }
-                        bs.addAll(currentSolutionBs);
-                    } else {
-                        throw new NoSuchElementException();
-                    }
-                }
-
-                return bs;
-            }
-
-            @Override
-            public boolean hasNext() throws QueryEvaluationException {
-
-                if(accIter == null ) {
-                    isEmpty = true;
-                    return false;
-                }
-                
-                if (!hasNextCalled && !isEmpty) {
-                    while (accIter.hasNext() || (inputSet != null && inputSet.hasNext())) {
-
-                        if(inputSet != null && inputSet.hasNext()) {
-                            hasNextCalled = true;
-                            return true;
-                        }
-                        
-                        
-                        Key k = accIter.next().getKey();
-                        final String[] s = k.getRow().toString().split("\u0000");
-                       
-                        StringBuilder rangePrefix = new StringBuilder();
-                        // TODO Assuming that order specifies order of variables
-                        // commmon to
-                        // bindingSet passed in and variables in index table
-                        // --size is equal to
-                        
-                        for (int i = 0; i < prefixLength; i++) {
-                            rangePrefix.append(s[i]);
-                            rangePrefix.append("\u0000");
-                        }
-
-                        // TODO I need to remember what the type was!
-                        currentSolutionBs = new QueryBindingSet();
-                        int i = 0;
-                        for (String b : order) {
-                            if (b.startsWith("-const")) {
-                                i++;
-                            } else {
-                                final String v = s[i];
-                                currentSolutionBs.addBinding(b, bindingMap.get(b).create(v));
-                                i++;
-                            }
-
-                        }
-                        //check to see if bindingSet constraints exist
-                        if (map.size() > 0) {
-                            String prefixWithOutNull = rangePrefix.deleteCharAt(rangePrefix.length() - 1).toString();
-                            String prefixWithNull = prefixWithOutNull + "\u0001";
-                            Range r = new Range(new Key(prefixWithOutNull), true, new Key(prefixWithNull), false);
-                            inputSet = map.get(r).iterator();
-                            if (!inputSet.hasNext()) {
-                                continue;
-                            } else {
-                                hasNextCalled = true;
-                                return true;
-                            } // check to see if binding set constraints exist, but no common vars
-                        } else if (extProdList.size() > 0) {
-                            inputSet = extProdList.iterator();
-                            hasNextCalled = true;
-                            return true;
-                        }else {  //no bindingsSet constraints--only constant constraints or none
-                            hasNextCalled = true;
-                            return true;
-                        }
-                    }
-
-                    isEmpty = true;
-                    return false;
-
-                } else if (isEmpty) {
-                    return false;
-                } else {
-                    return true;
-                }
-
-            }
-
-            @Override
-            public void close() throws QueryEvaluationException {
-                bs.close();
-            }
-
-        };
-    }
-
-    
-    
-    @Override
-    public void flush() throws IOException {
-        // TODO Auto-generated method stub
-
-    }
-
-    @Override
-    public void close() throws IOException {
-        // TODO Auto-generated method stub
-
-    }
-    
-    
-    
-    
-    
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/GeneralizedExternalProcessor.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/GeneralizedExternalProcessor.java b/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/GeneralizedExternalProcessor.java
index 27a0d15..58e84d9 100644
--- a/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/GeneralizedExternalProcessor.java
+++ b/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/GeneralizedExternalProcessor.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.IndexPlanValidator;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -53,29 +53,26 @@ public class GeneralizedExternalProcessor {
 
     /**
      * Iterates through list of normalized indexes and replaces all subtrees of query which match index with index.
-     * 
+     *
      * @param query
      * @return TupleExpr
      */
     public static TupleExpr process(TupleExpr query, List<ExternalTupleSet> indexSet) {
-        
+
         boolean indexPlaced = false;
         TupleExpr rtn = query.clone();
-                
-        
-        //TODO optimization: turn on when testing done
         QueryNodeCount qnc = new QueryNodeCount();
         rtn.visit(qnc);
-        
+
         if(qnc.getNodeCount()/2 < indexSet.size()) {
             return null;
         }
 
-        
+
         //move BindingSetAssignment Nodes out of the way
         organizeBSAs(rtn);
-        
-        
+
+
         // test to see if query contains no other nodes
         // than filter, join, projection, and statement pattern and
         // test whether query contains duplicate StatementPatterns and filters
@@ -105,26 +102,18 @@ public class GeneralizedExternalProcessor {
 
             }
             if(indexPlaced) {
-//                if(indexSet.size() == 3) {
-//                    System.out.println("IndexSet is " + indexSet);
-//                    System.out.println("Tuple is " + rtn);
-//                }
                 return rtn;
             } else {
-//                if(indexSet.size() == 3) {
-//                    System.out.println("IndexSet is " + indexSet);
-//                }
-//               
                 return null;
             }
-            
+
         } else {
             throw new IllegalArgumentException("Invalid Query.");
         }
     }
-    
-    
-    
+
+
+
 
 
     // determines whether query is valid, which requires that a
@@ -139,12 +128,12 @@ public class GeneralizedExternalProcessor {
 
         Set<String> spVars = getVarNames(getQNodes("sp", node));
 
-        if (vqv.isValid() && (spVars.size() > 0)) {
+        if (vqv.isValid() && spVars.size() > 0) {
 
             FilterCollector fvis = new FilterCollector();
             node.visit(fvis);
             List<QueryModelNode> fList = fvis.getFilters();
-            return (fList.size() == Sets.newHashSet(fList).size() && getVarNames(fList).size() <= spVars.size());
+            return fList.size() == Sets.newHashSet(fList).size() && getVarNames(fList).size() <= spVars.size();
 
         } else {
             return false;
@@ -211,16 +200,17 @@ public class GeneralizedExternalProcessor {
 
         }
 
-        public void meet(Projection node) {
+        @Override
+		public void meet(Projection node) {
             // moves external tuples above statement patterns before attempting
             // to bubble down index statement patterns found in query tree
-            
+
             organizeExtTuples(node);
-            
             super.meet(node);
         }
 
-        public void meet(Join node) {
+        @Override
+		public void meet(Join node) {
             // if right node contained in index, move it to bottom of query tree
             if (sSet.contains(node.getRightArg())) {
 
@@ -228,7 +218,6 @@ public class GeneralizedExternalProcessor {
                 Set<QueryModelNode> compSet = Sets.difference(eSet, sSet);
 
                 if (eSet.containsAll(sSet)) {
-
                     QNodeExchanger qne = new QNodeExchanger(node.getRightArg(), compSet);
                     node.visit(qne);
                     node.replaceChildNode(node.getRightArg(), qne.getReplaced());
@@ -293,7 +282,8 @@ public class GeneralizedExternalProcessor {
             return toBeReplaced;
         }
 
-        public void meet(Join node) {
+        @Override
+		public void meet(Join node) {
 
             if (compSet.contains(node.getRightArg())) {
                 this.toBeReplaced = node.getRightArg();
@@ -317,7 +307,6 @@ public class GeneralizedExternalProcessor {
     // this method is that
     // SPBubbleDownVisitor has been called to position index StatementPatterns
     // within query tree.
-    //TODO this visitor assumes that all filters are positioned at top of query tree
     //could lead to problems if filter optimizer called before external processor
     private static class FilterBubbleDownVisitor extends QueryModelVisitorBase<RuntimeException> {
 
@@ -335,7 +324,8 @@ public class GeneralizedExternalProcessor {
             return filterPlaced;
         }
 
-        public void meet(Join node) {
+        @Override
+		public void meet(Join node) {
 
             if (!compSet.contains(node.getRightArg())) {
                 // looks for placed to position filter node. if right node is
@@ -343,13 +333,13 @@ public class GeneralizedExternalProcessor {
                 // and left node is statement pattern node contained in index or
                 // is a join, place
                 // filter above join.
-                if (node.getLeftArg() instanceof Join || !(compSet.contains(node.getLeftArg()))) {
-                    
+                if (node.getLeftArg() instanceof Join || !compSet.contains(node.getLeftArg())) {
+
                     QueryModelNode pNode = node.getParentNode();
                     ((Filter) filter).setArg(node);
                     pNode.replaceChildNode(node, filter);
                     filterPlaced = true;
-                    
+
                     return;
                 } // otherwise place filter below join and above right arg
                 else {
@@ -359,12 +349,12 @@ public class GeneralizedExternalProcessor {
                     return;
 
                 }
-            } else if ((node.getLeftArg() instanceof StatementPattern) && !compSet.contains(node.getLeftArg())) {
-                
+            } else if (node.getLeftArg() instanceof StatementPattern && !compSet.contains(node.getLeftArg())) {
+
                 ((Filter) filter).setArg(node.getLeftArg());
                 node.replaceChildNode(node.getLeftArg(), filter);
                 filterPlaced = true;
-                
+
                 return;
             } else {
                 super.meet(node);
@@ -380,8 +370,9 @@ public class GeneralizedExternalProcessor {
 
         for (QueryModelNode s : nodes) {
             tempVars = VarCollector.process(s);
-            for (String t : tempVars)
-                nodeVarNames.add(t);
+            for (String t : tempVars) {
+				nodeVarNames.add(t);
+			}
         }
         return nodeVarNames;
 
@@ -403,7 +394,8 @@ public class GeneralizedExternalProcessor {
 
         }
 
-        public void meet(Filter node) {
+        @Override
+		public void meet(Filter node) {
 
             Set<QueryModelNode> eSet = getQNodes(node);
             Set<QueryModelNode> compSet = Sets.difference(eSet, sSet);
@@ -415,7 +407,7 @@ public class GeneralizedExternalProcessor {
             // and index (assuming that SPBubbleDownVisitor has already been
             // called)
             if (sSet.contains(node.getCondition()) && !bubbledFilters.contains(node.getCondition())) {
-                FilterBubbleDownVisitor fbdv = new FilterBubbleDownVisitor((Filter) node.clone(), compSet);
+                FilterBubbleDownVisitor fbdv = new FilterBubbleDownVisitor(node.clone(), compSet);
                 node.visit(fbdv);
                 bubbledFilters.add(node.getCondition());
                 // checks if filter correctly placed, and if it has been,
@@ -425,7 +417,7 @@ public class GeneralizedExternalProcessor {
                     QueryModelNode pNode = node.getParentNode();
                     TupleExpr cNode = node.getArg();
                     pNode.replaceChildNode(node, cNode);
-                   
+
 
                     super.meetNode(pNode);
                 }
@@ -446,37 +438,35 @@ public class GeneralizedExternalProcessor {
     // to position the StatementPatterns and Filters.
     private static class SubsetEqualsVisitor extends QueryModelVisitorBase<RuntimeException> {
 
-        private TupleExpr query;
         private TupleExpr tuple;
         private QueryModelNode indexQNode;
         private ExternalTupleSet set;
         private Set<QueryModelNode> sSet = Sets.newHashSet();
-        private TupleExpr temp;
         private boolean indexPlaced = false;
-        
+
 
         public SubsetEqualsVisitor(ExternalTupleSet index, TupleExpr query) {
-            this.query = query;
             this.tuple = index.getTupleExpr();
             this.set = index;
             indexQNode = ((Projection) tuple).getArg();
             sSet = getQNodes(indexQNode);
 
         }
-        
+
         public boolean indexPlaced() {
             return indexPlaced;
         }
-        
 
-        public void meet(Join node) {
+
+        @Override
+		public void meet(Join node) {
 
             Set<QueryModelNode> eSet = getQNodes(node);
 
             if (eSet.containsAll(sSet) && !(node.getRightArg() instanceof BindingSetAssignment)) {
 
 //                System.out.println("Eset is " + eSet + " and sSet is " + sSet);
-                
+
                 if (eSet.equals(sSet)) {
                     node.replaceWith(set);
                     indexPlaced = true;
@@ -506,9 +496,9 @@ public class GeneralizedExternalProcessor {
             }
 
         }
-      //TODO might need to include BindingSetAssignment Condition here
       //to account for index consisting of only filter and BindingSetAssignment nodes
-        public void meet(Filter node) {
+        @Override
+		public void meet(Filter node) {
 
             Set<QueryModelNode> eSet = getQNodes(node);
 
@@ -523,9 +513,10 @@ public class GeneralizedExternalProcessor {
                 }
             }
         }
-        
-        
-        public void meet(StatementPattern node) {
+
+
+        @Override
+		public void meet(StatementPattern node) {
             return;
         }
     }
@@ -541,24 +532,27 @@ public class GeneralizedExternalProcessor {
             return isValid;
         }
 
-        public void meet(Projection node) {
+        @Override
+		public void meet(Projection node) {
             node.getArg().visit(this);
         }
 
-        public void meet(Filter node) {
+        @Override
+		public void meet(Filter node) {
             node.getArg().visit(this);
         }
-        
-      
-       
-        
-       
-        public void meetNode(QueryModelNode node) {
 
-            if (!((node instanceof Join) || (node instanceof StatementPattern) || (node instanceof BindingSetAssignment) || (node instanceof Var))) {
+
+
+
+
+        @Override
+		public void meetNode(QueryModelNode node) {
+
+            if (!(node instanceof Join || node instanceof StatementPattern || node instanceof BindingSetAssignment || node instanceof Var)) {
                 isValid = false;
                 return;
-           
+
             } else{
                 super.meetNode(node);
             }
@@ -575,21 +569,22 @@ public class GeneralizedExternalProcessor {
             this.extTuples = extTuples;
         }
 
-        public void meet(Join queryNode) {
+        @Override
+		public void meet(Join queryNode) {
 
             // if query tree contains external tuples and they are not
             // positioned above statement pattern node
             // reposition
             if (this.extTuples.size() > 0 && !(queryNode.getRightArg() instanceof ExternalTupleSet)
                     && !(queryNode.getRightArg() instanceof BindingSetAssignment)) {
-                
+
                 if (queryNode.getLeftArg() instanceof ExternalTupleSet) {
                     QueryModelNode temp = queryNode.getLeftArg();
                     queryNode.setLeftArg(queryNode.getRightArg());
                     queryNode.setRightArg((TupleExpr)temp);
                 } else {
 
-                    QNodeExchanger qnev = new QNodeExchanger((QueryModelNode) queryNode.getRightArg(), this.extTuples);
+                    QNodeExchanger qnev = new QNodeExchanger(queryNode.getRightArg(), this.extTuples);
                     queryNode.visit(qnev);
                     queryNode.replaceChildNode(queryNode.getRightArg(), qnev.getReplaced());
                     super.meet(queryNode);
@@ -657,13 +652,14 @@ public class GeneralizedExternalProcessor {
             this.bsas = bsas;
         }
 
-        public void meet(Join queryNode) {
+        @Override
+		public void meet(Join queryNode) {
 
             // if query tree contains external tuples and they are not
             // positioned above statement pattern node
             // reposition
             if (this.bsas.size() > 0 && !(queryNode.getRightArg() instanceof BindingSetAssignment)) {
-                QNodeExchanger qnev = new QNodeExchanger((QueryModelNode) queryNode.getRightArg(), bsas);
+                QNodeExchanger qnev = new QNodeExchanger(queryNode.getRightArg(), bsas);
                 queryNode.visit(qnev);
                 queryNode.replaceChildNode(queryNode.getRightArg(), qnev.getReplaced());
                 super.meet(queryNode);
@@ -674,8 +670,8 @@ public class GeneralizedExternalProcessor {
         }
 
     }
-        
-        
+
+
     public static class BindingSetAssignmentCollector extends QueryModelVisitorBase<RuntimeException> {
 
         private Set<QueryModelNode> bindingSetList = Sets.newHashSet();
@@ -685,7 +681,7 @@ public class GeneralizedExternalProcessor {
         }
 
         public boolean containsBSAs() {
-            return (bindingSetList.size() > 0);
+            return bindingSetList.size() > 0;
         }
 
         @Override
@@ -695,9 +691,9 @@ public class GeneralizedExternalProcessor {
         }
 
     }
-    
-    
-    
+
+
+
     public static class QueryNodeCount extends QueryModelVisitorBase<RuntimeException> {
 
         private int nodeCount;
@@ -725,6 +721,6 @@ public class GeneralizedExternalProcessor {
 
     }
 
-   
-   
+
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/IndexListPruner.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/IndexListPruner.java b/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/IndexListPruner.java
index fa1dc13..8fbcbe0 100644
--- a/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/IndexListPruner.java
+++ b/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/IndexListPruner.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.IndexPlanValidator;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -22,14 +22,10 @@ package mvm.rya.indexing.IndexPlanValidator;
 
 import java.util.List;
 
-
-
-import java.util.Set;
-
 import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
 
 public interface IndexListPruner {
 
-    public Set<ExternalTupleSet> getRelevantIndices(List<ExternalTupleSet> indexList);
-        
+    public List<ExternalTupleSet> getRelevantIndices(List<ExternalTupleSet> indexList);
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/IndexedExecutionPlanGenerator.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/IndexedExecutionPlanGenerator.java b/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/IndexedExecutionPlanGenerator.java
index acf3f6a..a0fca34 100644
--- a/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/IndexedExecutionPlanGenerator.java
+++ b/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/IndexedExecutionPlanGenerator.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.IndexPlanValidator;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -22,9 +22,7 @@ package mvm.rya.indexing.IndexPlanValidator;
 
 import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
 import java.util.NoSuchElementException;
-import java.util.Set;
 
 import mvm.rya.indexing.external.QueryVariableNormalizer;
 import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
@@ -32,38 +30,29 @@ import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
 import org.openrdf.query.algebra.Projection;
 import org.openrdf.query.algebra.TupleExpr;
 
-import com.google.common.collect.BiMap;
-import com.google.common.collect.HashBiMap;
 import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
 
 public class IndexedExecutionPlanGenerator implements ExternalIndexMatcher {
 
     private final TupleExpr query;
-    private List<ExternalTupleSet> normalizedIndexList;
-    
+    private final List<ExternalTupleSet> normalizedIndexList;
+
     public IndexedExecutionPlanGenerator(TupleExpr query, List<ExternalTupleSet> indexList) {
         this.query = query;
-        VarConstantIndexListPruner vci = new VarConstantIndexListPruner(query);
+        final VarConstantIndexListPruner vci = new VarConstantIndexListPruner(query);
         normalizedIndexList = getNormalizedIndices(vci.getRelevantIndices(indexList));
     }
-    
+
     public List<ExternalTupleSet> getNormalizedIndices() {
         return normalizedIndexList;
     }
-    
-  
-    
-    
+
     @Override
     public Iterator<TupleExpr> getIndexedTuples() {
-        
-        ValidIndexCombinationGenerator vic = new ValidIndexCombinationGenerator(query);
-        final Iterator<List<ExternalTupleSet>> iter = vic.getValidIndexCombos(normalizedIndexList);
 
+        final ValidIndexCombinationGenerator vic = new ValidIndexCombinationGenerator(query);
+        final Iterator<List<ExternalTupleSet>> iter = vic.getValidIndexCombos(normalizedIndexList);
         return new Iterator<TupleExpr>() {
-
             private TupleExpr next = null;
             private boolean hasNextCalled = false;
             private boolean isEmpty = false;
@@ -73,7 +62,7 @@ public class IndexedExecutionPlanGenerator implements ExternalIndexMatcher {
 
                 if (!hasNextCalled && !isEmpty) {
                     while (iter.hasNext()) {
-                        TupleExpr temp = GeneralizedExternalProcessor.process(query, iter.next());
+                        final TupleExpr temp = GeneralizedExternalProcessor.process(query, iter.next());
                         if (temp != null) {
                             next = temp;
                             hasNextCalled = true;
@@ -104,104 +93,36 @@ public class IndexedExecutionPlanGenerator implements ExternalIndexMatcher {
                     } else {
                         throw new NoSuchElementException();
                     }
-
                 }
-
             }
 
             @Override
             public void remove() {
-
                 throw new UnsupportedOperationException("Cannot delete from iterator!");
-
             }
-
         };
     }
 
-    
-    private List<ExternalTupleSet> getNormalizedIndices(Set<ExternalTupleSet> indexSet) {
+    private List<ExternalTupleSet> getNormalizedIndices(List<ExternalTupleSet> indexSet) {
 
         ExternalTupleSet tempIndex;
-        List<ExternalTupleSet> normalizedIndexSet = Lists.newArrayList();
-
-        for (ExternalTupleSet e : indexSet) {
+        final List<ExternalTupleSet> normalizedIndexSet = Lists.newArrayList();
 
+        for (final ExternalTupleSet e : indexSet) {
             List<TupleExpr> tupList = null;
             try {
                 tupList = QueryVariableNormalizer.getNormalizedIndex(query, e.getTupleExpr());
-            } catch (Exception e1) {
+            } catch (final Exception e1) {
                 // TODO Auto-generated catch block
                 e1.printStackTrace();
             }
 
-            for (TupleExpr te : tupList) {
-
+            for (final TupleExpr te : tupList) {
                 tempIndex = (ExternalTupleSet) e.clone();
-                setTableMap(te, tempIndex);
-                setSupportedVarOrderMap(tempIndex);
                 tempIndex.setProjectionExpr((Projection) te);
                 normalizedIndexSet.add(tempIndex);
-
             }
-
         }
-
         return normalizedIndexSet;
     }
-
-    private void setTableMap(TupleExpr tupleMatch, ExternalTupleSet index) {
-
-        List<String> replacementVars = Lists.newArrayList(tupleMatch.getBindingNames());
-        List<String> tableVars = Lists.newArrayList(index.getTupleExpr().getBindingNames());
-
-        Map<String, String> tableMap = Maps.newHashMap();
-
-        for (int i = 0; i < tableVars.size(); i++) {
-            tableMap.put(replacementVars.get(i), tableVars.get(i));
-        }
-        // System.out.println("Table map is " + tableMap);
-        index.setTableVarMap(tableMap);
-
-    }
-    
-    
-    private void setSupportedVarOrderMap(ExternalTupleSet index) {
-
-        Map<String, Set<String>> supportedVarOrders = Maps.newHashMap();
-        BiMap<String, String> biMap = HashBiMap.create(index.getTableVarMap()).inverse();
-        Map<String, Set<String>> oldSupportedVarOrders = index.getSupportedVariableOrderMap();
-
-        Set<String> temp = null;
-        Set<String> keys = oldSupportedVarOrders.keySet();
-
-        for (String s : keys) {
-            temp = oldSupportedVarOrders.get(s);
-            Set<String> newSet = Sets.newHashSet();
-
-            for (String t : temp) {
-                newSet.add(biMap.get(t));
-            }
-            
-            String[] tempStrings = s.split("\u0000");
-            String v = "";
-            for(String u: tempStrings) {
-                if(v.length() == 0){
-                    v = v + biMap.get(u);
-                } else {
-                    v = v + "\u0000" + biMap.get(u);
-                }
-            }
-
-            supportedVarOrders.put(v, newSet);
-
-        }
-
-        index.setSupportedVariableOrderMap(supportedVarOrders);
-
-    }
-    
-    
-    
-
 }

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/ValidIndexCombinationGenerator.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/ValidIndexCombinationGenerator.java b/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/ValidIndexCombinationGenerator.java
index b3c3fcd..483457f 100644
--- a/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/ValidIndexCombinationGenerator.java
+++ b/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/ValidIndexCombinationGenerator.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.IndexPlanValidator;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -19,7 +19,6 @@ package mvm.rya.indexing.IndexPlanValidator;
  * under the License.
  */
 
-
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
@@ -27,645 +26,436 @@ import java.util.NoSuchElementException;
 import java.util.Set;
 
 import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
-import mvm.rya.indexing.external.tupleSet.SimpleExternalTupleSet;
 
-import org.openrdf.query.MalformedQueryException;
 import org.openrdf.query.algebra.Filter;
-import org.openrdf.query.algebra.Projection;
 import org.openrdf.query.algebra.QueryModelNode;
 import org.openrdf.query.algebra.StatementPattern;
 import org.openrdf.query.algebra.TupleExpr;
 import org.openrdf.query.algebra.helpers.QueryModelVisitorBase;
-import org.openrdf.query.parser.ParsedQuery;
-import org.openrdf.query.parser.sparql.SPARQLParser;
 
 import com.google.common.base.Joiner;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
 
 public class ValidIndexCombinationGenerator {
-    
-    
-    private TupleExpr query;
-    private Set<String> invalidCombos = Sets.newTreeSet();
-    private Set<QueryModelNode> spFilterSet;
-    
-    
-    public ValidIndexCombinationGenerator(TupleExpr query) {
-        this.query = query;
-        SpFilterCollector sfc = new SpFilterCollector();
-        query.visit(sfc);
-        spFilterSet = sfc.getSpFilterSet();
-    }
-    
-    
-    
-    
-    public Iterator<List<ExternalTupleSet>> getValidIndexCombos(List<ExternalTupleSet> indexSet) {
-
-        Collections.shuffle(indexSet);
-        final List<ExternalTupleSet> list = indexSet;
-        final Iterator<List<Integer>> iter = getValidCombos(list);
-
-        return new Iterator<List<ExternalTupleSet>>() {
-
-            private List<ExternalTupleSet> next = null;
-            private List<Integer> nextCombo = null;
-            private boolean hasNextCalled = false;
-            private boolean isEmpty = false;
-
-            @Override
-            public boolean hasNext() {
-
-                if (!hasNextCalled && !isEmpty) {
-                    if (!iter.hasNext()) {
-                        isEmpty = true;
-                        return false;
-                    } else {
-                        nextCombo = iter.next();
-                        List<ExternalTupleSet> indexCombo = Lists.newArrayList();
-                        for (Integer i : nextCombo) {
-                            indexCombo.add(list.get(i));
-                        }
-                        next = indexCombo;
-                        hasNextCalled = true;
-                        return true;
-
-                    }
-
-                } else if (isEmpty) {
-                    return false;
-                } else {
-                    return true;
-                }
-            }
-
-            @Override
-            public List<ExternalTupleSet> next() {
-
-                if (hasNextCalled) {
-                    hasNextCalled = false;
-                    return next;
-                } else if(isEmpty) {
-                    throw new NoSuchElementException();
-                }else {
-                    if (this.hasNext()) {
-                        hasNextCalled = false;
-                        return next;
-                    } else {
-                        throw new NoSuchElementException();
-                    }
-                }
-            }
-
-            @Override
-            public void remove() {
-
-                throw new UnsupportedOperationException("Cannot delete from iterator!");
-
-            }
-
-        };
-
-    }
-    
-    
-    
-    private Iterator<List<Integer>> getValidCombos(List<ExternalTupleSet> indexList) {
-        
-        
-        final List<ExternalTupleSet> list = indexList;
-        final int indexSize = list.size();
-        final Iterator<List<Integer>> iter = getCombos(indexSize);
-        
-        
-        return new Iterator<List<Integer>>() {
-
-            private List<Integer> next = null;
-            private boolean hasNextCalled = false;
-            private boolean isEmpty = false;
-
-            @Override
-            public boolean hasNext() {
-                if (!hasNextCalled && !isEmpty) {
-
-                    while (iter.hasNext()) {
-                        List<Integer> tempNext = iter.next();
-                        if (isValid(tempNext, list)) {
-                            next = tempNext;
-                            hasNextCalled = true;
-                            return true;
-                        }
-
-                    }
-
-                    isEmpty = true;
-                    return false;
-
-                } else if (isEmpty) {
-                    return false;
-                } else {
-                    return true;
-                }
-            }
-
-            @Override
-            public List<Integer> next() {
-
-                if (hasNextCalled) {
-                    hasNextCalled = false;
-                    return next;
-                } else if (isEmpty) {
-                    throw new NoSuchElementException();
-                } else {
-                    if (this.hasNext()) {
-                        hasNextCalled = false;
-                        return next;
-                    } else {
-                        throw new NoSuchElementException();
-                    }
-
-                }
-
-            }
-
-            @Override
-            public void remove() {
-
-                throw new UnsupportedOperationException("Cannot delete from iterator!");
-
-            }
-
-        };
-    }
-    
-    
-    
-    
-    
-    
-    private Iterator<List<Integer>> getCombos(int indexListSize) {
-
-        final int indexSize = indexListSize;
-        final int maxSubListSize = spFilterSet.size() / 2;
-
-        return new Iterator<List<Integer>>() {
-
-            private List<Integer> next = null;
-            private boolean hasNextCalled = false;
-            private boolean isEmpty = false;
-            private int subListSize = Math.min(maxSubListSize, indexSize) + 1;
-            Iterator<List<Integer>> subList = null;
-
-            @Override
-            public boolean hasNext() {
-
-                if (!hasNextCalled && !isEmpty) {
-                    if (subList != null && subList.hasNext()) {
-                        next = subList.next();
-                        hasNextCalled = true;
-                        return true;
-                    } else {
-                        subListSize--;
-                        if (subListSize == 0) {
-                            isEmpty = true;
-                            return false;
-                        }
-                        subList = getCombos(subListSize, indexSize);
-                        if (subList == null) {
-                            throw new IllegalStateException("Combos cannot be null!");
-                        }
-                        next = subList.next();
-                        hasNextCalled = true;
-                        return true;
-
-                    }
-                } else if (isEmpty) {
-                    return false;
-                } else {
-                    return true;
-                }
-            }
-
-            @Override
-            public List<Integer> next() {
-
-                if (hasNextCalled) {
-                    hasNextCalled = false;
-                    return next;
-                } else if (isEmpty) {
-                    throw new NoSuchElementException();
-                } else {
-                    if (this.hasNext()) {
-                        hasNextCalled = false;
-                        return next;
-                    } else {
-                        throw new NoSuchElementException();
-                    }
-
-                }
-
-            }
-
-            @Override
-            public void remove() {
-                throw new UnsupportedOperationException("Cannot delete from iterator!");
-            }
-
-        };
-
-    }
-    
-    
-    
-    private Iterator<List<Integer>> getCombos(int subListSize, int indexListSize) {
-        
-        if(subListSize > indexListSize) {
-            throw new IllegalArgumentException("Sublist size must be less than or equal to list size!");
-        }
-        
-        final int subSize = subListSize;
-        final int indexSize = indexListSize;
-        
-        return new Iterator<List<Integer>>() {
-
-            private List<Integer> next = null;
-            private List<Integer> tempList = Lists.newArrayList();
-            private boolean calledHasNext = false;
-            private boolean isEmpty = false;
-            
-            @Override
-            public boolean hasNext() {
-
-                if (!calledHasNext && !isEmpty) {
-                    if (next == null) {
-                        for (int i = 0; i < subSize; i++) {
-                            tempList.add(i);
-                        }
-                        next = tempList;
-                        calledHasNext = true;
-                        return true;
-                    } else {
-                        next = getNext(next, indexSize - 1);
-                        if (next == null) {
-                            isEmpty = true;
-                            return false;
-                        } else {
-                            calledHasNext = true;
-                            return true;
-                        }
-
-                    }
-                } else if(isEmpty) {  
-                    return false;
-                } else {
-                    return true;
-                }
-
-            }
-
-            @Override
-            public List<Integer> next() {
-
-                if (calledHasNext) {
-                    calledHasNext = false;
-                    return next;
-                } else if (isEmpty) {
-                    throw new NoSuchElementException();
-                } else {
-                    if (this.hasNext()) {
-                        calledHasNext = false;
-                        return next;
-                    } else {
-                        throw new NoSuchElementException();
-                    }
-                }
-            }
-            @Override
-            public void remove() {
-                throw new UnsupportedOperationException();
-                
-            }
-            
-            
-            
-        };
-    }
-    
-
-    
-    
-    
-    
-    private List<Integer> getNext(List<Integer> prev, int maxInt) {
-        
-        List<Integer> returnList = Lists.newArrayList();
-        int size = prev.size();
-        int incrementPos = -1;
-        int incrementVal = 0;
-        
-        for(int i = 0; i < size; i++) {
-            if(prev.get(size-(i+1)) != maxInt - i) {
-                incrementPos = size - (i+1);
-                break;
-            }
-        }
-        
-        if (incrementPos == -1) {
-            return null;
-        } else {
-
-            incrementVal = prev.get(incrementPos);
-            for (int i = 0; i < incrementPos; i++) {
-                returnList.add(prev.get(i));
-            }
-
-            for (int j = incrementPos; j < size; j++) {
-                returnList.add(++incrementVal);
-            }
-
-            return returnList;
-        }
-    }
-    
-    
-    
-    
-    private boolean isValid(List<Integer> combo, List<ExternalTupleSet> indexList) {
-        
-        String s1 = Joiner.on("\u0000").join(combo).trim();
-        
-        if(invalidCombos.contains(s1)) {
-            return false;
-        } else {
-            int valid = indicesDisjoint(combo, indexList);
-            
-            if (valid >= 0) {
-                String s2 = "";
-                for (int i = 0; i < valid + 1; i++) {
-                    if (s2.length() == 0) {
-                        s2 = s2 + combo.get(i);
-                    } else {
-                        s2 = s2 + "\u0000" + combo.get(i);
-                    }
-                }
-                invalidCombos.add(s2);
-
-                for (int i = valid + 1; i < combo.size(); i++) {
-                    s2 = s2 + "\u0000" + combo.get(i);
-                    invalidCombos.add(s2);
-                }
-
-                return false;
-            } else {
-                return true;
-            }
-        }
-        
-        
-    }
-    
-    
-    
-    private int indicesDisjoint(List<Integer> combo, List<ExternalTupleSet> indexList) {
-        
-        Set<QueryModelNode> indexNodes = Sets.newHashSet();
-        Set<QueryModelNode> tempNodes;
-        TupleExpr temp;
-        
-        
-        int j = 0;
-        for(Integer i: combo) {
-            temp = indexList.get(i).getTupleExpr();
-            SpFilterCollector spf = new SpFilterCollector();
-            temp.visit(spf);
-            tempNodes = spf.getSpFilterSet();
-            if(Sets.intersection(indexNodes, tempNodes).size() == 0) {
-                indexNodes = Sets.union(indexNodes, tempNodes);
-                if(indexNodes.size() > spFilterSet.size()) {
-                    return j;
-                }
-            } else {
-                return j;
-            }
-            j++;
-        }
-        
-        return -1;
-    }
-    
-    
-    
-    
-    public static void main(String[] args) {
-        
-        
-        String q1 = ""//
-                + "SELECT ?f ?m ?d " //
-                + "{" //
-                + "  ?f a ?m ."//
-                + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-                + "  ?d <uri:talksTo> ?f . "//
-                + "  ?f <uri:hangOutWith> ?m ." //
-                + "  ?m <uri:hangOutWith> ?d ." //
-                + "  ?f <uri:associatesWith> ?m ." //
-                + "  ?m <uri:associatesWith> ?d ." //
-                + "}";//
-        
-        
-        String q2 = ""//
-                + "SELECT ?t ?s ?u " //
-                + "{" //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "  ?u <uri:talksTo> ?s . "//
-                + "}";//
-        
-        
-        String q3 = ""//
-                + "SELECT ?s ?t ?u " //
-                + "{" //
-                + "  ?s <uri:hangOutWith> ?t ." //
-                + "  ?t <uri:hangOutWith> ?u ." //
-                + "}";//
-        
-        String q4 = ""//
-                + "SELECT ?s ?t ?u " //
-                + "{" //
-                + "  ?s <uri:associatesWith> ?t ." //
-                + "  ?t <uri:associatesWith> ?u ." //
-                + "}";//
-        
-        
-        String q5 = ""//
-                + "SELECT ?t ?s ?u " //
-                + "{" //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "  ?u <uri:talksTo> ?s . "//
-                + "  ?s <uri:hangOutWith> ?t ." //
-                + "  ?t <uri:hangOutWith> ?u ." //
-                + "}";//
-        
-        String q6 = ""//
-                + "SELECT ?s ?t ?u " //
-                + "{" //
-                + "  ?s <uri:associatesWith> ?t ." //
-                + "  ?t <uri:associatesWith> ?u ." //
-                + "  ?s <uri:hangOutWith> ?t ." //
-                + "  ?t <uri:hangOutWith> ?u ." //
-                + "}";//
-        
-        
-        String q7 = ""//
-                + "SELECT ?s ?t ?u " //
-                + "{" //
-                + "  ?s <uri:associatesWith> ?t ." //
-                + "  ?t <uri:associatesWith> ?u ." //
-                + "  ?t <uri:hangOutWith> ?u ." //
-                + "}";//
-        
-        
-        
-        String q8 = ""//
-                + "SELECT ?t ?s ?u " //
-                + "{" //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "  ?u <uri:talksTo> ?s . "//
-                + "  ?s <uri:associatesWith> ?t ." //
-                + "}";//
-        
-        
-        String q9 = ""//
-                + "SELECT ?t ?s ?u " //
-                + "{" //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "}";//
-        
-        
-        
-        
-        
-        
-        
-        
-
-        SPARQLParser parser = new SPARQLParser();
-        ParsedQuery pq1 = null;
-        ParsedQuery pq2 = null;
-        ParsedQuery pq3 = null;
-        ParsedQuery pq4 = null;
-        ParsedQuery pq5 = null;
-        ParsedQuery pq6 = null;
-        ParsedQuery pq7 = null;
-        ParsedQuery pq8 = null;
-        ParsedQuery pq9 = null;
-        
-        SimpleExternalTupleSet extTup1 = null;
-        SimpleExternalTupleSet extTup2 = null;
-        SimpleExternalTupleSet extTup3 = null;
-        SimpleExternalTupleSet extTup4 = null;
-        SimpleExternalTupleSet extTup5 = null;
-        SimpleExternalTupleSet extTup6 = null;
-        SimpleExternalTupleSet extTup7 = null;
-        SimpleExternalTupleSet extTup8 = null;
-        
-        
-        
-        
-        
-        try {
-            pq1 = parser.parseQuery(q1, null);
-            pq2 = parser.parseQuery(q2, null);
-            pq3 = parser.parseQuery(q3, null);
-            pq4 = parser.parseQuery(q4, null);
-            pq5 = parser.parseQuery(q5, null);
-            pq6 = parser.parseQuery(q6, null);
-            pq7 = parser.parseQuery(q7, null);
-            pq8 = parser.parseQuery(q8, null);
-            pq9 = parser.parseQuery(q9, null);
-           
-
-            extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-            extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
-            extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
-            extTup4 = new SimpleExternalTupleSet((Projection) pq5.getTupleExpr());
-            extTup5 = new SimpleExternalTupleSet((Projection) pq6.getTupleExpr());
-            extTup6 = new SimpleExternalTupleSet((Projection) pq7.getTupleExpr());
-            extTup7 = new SimpleExternalTupleSet((Projection) pq8.getTupleExpr());
-            extTup8 = new SimpleExternalTupleSet((Projection) pq9.getTupleExpr());
-            
-          
-        } catch (MalformedQueryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-        
-        List<ExternalTupleSet> indexList = Lists.newArrayList();
-        indexList.add(extTup1);
-        indexList.add(extTup2);
-        indexList.add(extTup3);
-        indexList.add(extTup4);
-        indexList.add(extTup5);
-        indexList.add(extTup6);
-        indexList.add(extTup7);
-        indexList.add(extTup8);
-        
-        
-        ValidIndexCombinationGenerator vic = new ValidIndexCombinationGenerator(pq1.getTupleExpr());
-        Iterator<List<ExternalTupleSet>> combos = vic.getValidIndexCombos(indexList);
-        int size = 0;
-        while(combos.hasNext()) {
-            combos.hasNext();
-            size++;
-            List<ExternalTupleSet> eSet = combos.next();
-            System.out.println("********************************************");
-            for(ExternalTupleSet e: eSet) {
-                System.out.println(e.getTupleExpr());
-            }
-            System.out.println("********************************************");
-        }
-        
-        System.out.println("size is " + size + " has next " + combos.hasNext());
-    }
-    
-    
-    
-    
-    
-    private static class SpFilterCollector extends QueryModelVisitorBase<RuntimeException> {
-
-        private Set<QueryModelNode> spFilterSet = Sets.newHashSet();
-
-        
-        public int getNodeNumber() {
-            return spFilterSet.size();
-        }
-        
-        
-        public Set<QueryModelNode> getSpFilterSet() {
-            return spFilterSet;
-        }
-        
-        
-        @Override
-        public void meet(StatementPattern node) {
-            
-            spFilterSet.add(node);
-            return;
-            
-        }
-        
-        
-        @Override
-        public void meet(Filter node) {
-            
-            spFilterSet.add(node.getCondition());
-            node.getArg().visit(this);
-        }  
-        
-
-    }
+
+	private TupleExpr query;
+	private Set<String> invalidCombos = Sets.newTreeSet();
+	private Set<QueryModelNode> spFilterSet;
+
+	public ValidIndexCombinationGenerator(TupleExpr query) {
+		this.query = query;
+		SpFilterCollector sfc = new SpFilterCollector();
+		query.visit(sfc);
+		spFilterSet = sfc.getSpFilterSet();
+	}
+
+	public Iterator<List<ExternalTupleSet>> getValidIndexCombos(
+			List<ExternalTupleSet> indexSet) {
+
+		Collections.shuffle(indexSet);
+		final List<ExternalTupleSet> list = indexSet;
+		final Iterator<List<Integer>> iter = getValidCombos(list);
+
+		return new Iterator<List<ExternalTupleSet>>() {
+
+			private List<ExternalTupleSet> next = null;
+			private List<Integer> nextCombo = null;
+			private boolean hasNextCalled = false;
+			private boolean isEmpty = false;
+
+			@Override
+			public boolean hasNext() {
+
+				if (!hasNextCalled && !isEmpty) {
+					if (!iter.hasNext()) {
+						isEmpty = true;
+						return false;
+					} else {
+						nextCombo = iter.next();
+						List<ExternalTupleSet> indexCombo = Lists
+								.newArrayList();
+						for (Integer i : nextCombo) {
+							indexCombo.add(list.get(i));
+						}
+						next = indexCombo;
+						hasNextCalled = true;
+						return true;
+
+					}
+
+				} else if (isEmpty) {
+					return false;
+				} else {
+					return true;
+				}
+			}
+
+			@Override
+			public List<ExternalTupleSet> next() {
+
+				if (hasNextCalled) {
+					hasNextCalled = false;
+					return next;
+				} else if (isEmpty) {
+					throw new NoSuchElementException();
+				} else {
+					if (this.hasNext()) {
+						hasNextCalled = false;
+						return next;
+					} else {
+						throw new NoSuchElementException();
+					}
+				}
+			}
+
+			@Override
+			public void remove() {
+
+				throw new UnsupportedOperationException(
+						"Cannot delete from iterator!");
+
+			}
+
+		};
+
+	}
+
+	private Iterator<List<Integer>> getValidCombos(
+			List<ExternalTupleSet> indexList) {
+
+		final List<ExternalTupleSet> list = indexList;
+		final int indexSize = list.size();
+		final Iterator<List<Integer>> iter = getCombos(indexSize);
+
+		return new Iterator<List<Integer>>() {
+
+			private List<Integer> next = null;
+			private boolean hasNextCalled = false;
+			private boolean isEmpty = false;
+
+			@Override
+			public boolean hasNext() {
+				if (!hasNextCalled && !isEmpty) {
+
+					while (iter.hasNext()) {
+						List<Integer> tempNext = iter.next();
+						if (isValid(tempNext, list)) {
+							next = tempNext;
+							hasNextCalled = true;
+							return true;
+						}
+
+					}
+
+					isEmpty = true;
+					return false;
+
+				} else if (isEmpty) {
+					return false;
+				} else {
+					return true;
+				}
+			}
+
+			@Override
+			public List<Integer> next() {
+
+				if (hasNextCalled) {
+					hasNextCalled = false;
+					return next;
+				} else if (isEmpty) {
+					throw new NoSuchElementException();
+				} else {
+					if (this.hasNext()) {
+						hasNextCalled = false;
+						return next;
+					} else {
+						throw new NoSuchElementException();
+					}
+
+				}
+
+			}
+
+			@Override
+			public void remove() {
+
+				throw new UnsupportedOperationException(
+						"Cannot delete from iterator!");
+
+			}
+
+		};
+	}
+
+	private Iterator<List<Integer>> getCombos(int indexListSize) {
+
+		final int indexSize = indexListSize;
+		final int maxSubListSize = spFilterSet.size() / 2;
+
+		return new Iterator<List<Integer>>() {
+
+			private List<Integer> next = null;
+			private boolean hasNextCalled = false;
+			private boolean isEmpty = false;
+			private int subListSize = Math.min(maxSubListSize, indexSize) + 1;
+			Iterator<List<Integer>> subList = null;
+
+			@Override
+			public boolean hasNext() {
+
+				if (!hasNextCalled && !isEmpty) {
+					if (subList != null && subList.hasNext()) {
+						next = subList.next();
+						hasNextCalled = true;
+						return true;
+					} else {
+						subListSize--;
+						if (subListSize == 0) {
+							isEmpty = true;
+							return false;
+						}
+						subList = getCombos(subListSize, indexSize);
+						if (subList == null) {
+							throw new IllegalStateException(
+									"Combos cannot be null!");
+						}
+						next = subList.next();
+						hasNextCalled = true;
+						return true;
+
+					}
+				} else if (isEmpty) {
+					return false;
+				} else {
+					return true;
+				}
+			}
+
+			@Override
+			public List<Integer> next() {
+
+				if (hasNextCalled) {
+					hasNextCalled = false;
+					return next;
+				} else if (isEmpty) {
+					throw new NoSuchElementException();
+				} else {
+					if (this.hasNext()) {
+						hasNextCalled = false;
+						return next;
+					} else {
+						throw new NoSuchElementException();
+					}
+
+				}
+
+			}
+
+			@Override
+			public void remove() {
+				throw new UnsupportedOperationException(
+						"Cannot delete from iterator!");
+			}
+
+		};
+
+	}
+
+	private Iterator<List<Integer>> getCombos(int subListSize, int indexListSize) {
+
+		if (subListSize > indexListSize) {
+			throw new IllegalArgumentException(
+					"Sublist size must be less than or equal to list size!");
+		}
+
+		final int subSize = subListSize;
+		final int indexSize = indexListSize;
+
+		return new Iterator<List<Integer>>() {
+
+			private List<Integer> next = null;
+			private List<Integer> tempList = Lists.newArrayList();
+			private boolean calledHasNext = false;
+			private boolean isEmpty = false;
+
+			@Override
+			public boolean hasNext() {
+
+				if (!calledHasNext && !isEmpty) {
+					if (next == null) {
+						for (int i = 0; i < subSize; i++) {
+							tempList.add(i);
+						}
+						next = tempList;
+						calledHasNext = true;
+						return true;
+					} else {
+						next = getNext(next, indexSize - 1);
+						if (next == null) {
+							isEmpty = true;
+							return false;
+						} else {
+							calledHasNext = true;
+							return true;
+						}
+
+					}
+				} else if (isEmpty) {
+					return false;
+				} else {
+					return true;
+				}
+
+			}
+
+			@Override
+			public List<Integer> next() {
+
+				if (calledHasNext) {
+					calledHasNext = false;
+					return next;
+				} else if (isEmpty) {
+					throw new NoSuchElementException();
+				} else {
+					if (this.hasNext()) {
+						calledHasNext = false;
+						return next;
+					} else {
+						throw new NoSuchElementException();
+					}
+				}
+			}
+
+			@Override
+			public void remove() {
+				throw new UnsupportedOperationException();
+
+			}
+
+		};
+	}
+
+	private List<Integer> getNext(List<Integer> prev, int maxInt) {
+
+		List<Integer> returnList = Lists.newArrayList();
+		int size = prev.size();
+		int incrementPos = -1;
+		int incrementVal = 0;
+
+		for (int i = 0; i < size; i++) {
+			if (prev.get(size - (i + 1)) != maxInt - i) {
+				incrementPos = size - (i + 1);
+				break;
+			}
+		}
+
+		if (incrementPos == -1) {
+			return null;
+		} else {
+
+			incrementVal = prev.get(incrementPos);
+			for (int i = 0; i < incrementPos; i++) {
+				returnList.add(prev.get(i));
+			}
+
+			for (int j = incrementPos; j < size; j++) {
+				returnList.add(++incrementVal);
+			}
+
+			return returnList;
+		}
+	}
+
+	private boolean isValid(List<Integer> combo,
+			List<ExternalTupleSet> indexList) {
+
+		String s1 = Joiner.on("\u0000").join(combo).trim();
+
+		if (invalidCombos.contains(s1)) {
+			return false;
+		} else {
+			int valid = indicesDisjoint(combo, indexList);
+
+			if (valid >= 0) {
+				String s2 = "";
+				for (int i = 0; i < valid + 1; i++) {
+					if (s2.length() == 0) {
+						s2 = s2 + combo.get(i);
+					} else {
+						s2 = s2 + "\u0000" + combo.get(i);
+					}
+				}
+				invalidCombos.add(s2);
+
+				for (int i = valid + 1; i < combo.size(); i++) {
+					s2 = s2 + "\u0000" + combo.get(i);
+					invalidCombos.add(s2);
+				}
+
+				return false;
+			} else {
+				return true;
+			}
+		}
+
+	}
+
+	private int indicesDisjoint(List<Integer> combo,
+			List<ExternalTupleSet> indexList) {
+
+		Set<QueryModelNode> indexNodes = Sets.newHashSet();
+		Set<QueryModelNode> tempNodes;
+		TupleExpr temp;
+
+		int j = 0;
+		for (Integer i : combo) {
+			temp = indexList.get(i).getTupleExpr();
+			SpFilterCollector spf = new SpFilterCollector();
+			temp.visit(spf);
+			tempNodes = spf.getSpFilterSet();
+			if (Sets.intersection(indexNodes, tempNodes).size() == 0) {
+				indexNodes = Sets.union(indexNodes, tempNodes);
+				if (indexNodes.size() > spFilterSet.size()) {
+					return j;
+				}
+			} else {
+				return j;
+			}
+			j++;
+		}
+
+		return -1;
+	}
+
+	private static class SpFilterCollector extends
+			QueryModelVisitorBase<RuntimeException> {
+
+		private Set<QueryModelNode> spFilterSet = Sets.newHashSet();
+
+		public int getNodeNumber() {
+			return spFilterSet.size();
+		}
+
+		public Set<QueryModelNode> getSpFilterSet() {
+			return spFilterSet;
+		}
+
+		@Override
+		public void meet(StatementPattern node) {
+
+			spFilterSet.add(node);
+			return;
+
+		}
+
+		@Override
+		public void meet(Filter node) {
+
+			spFilterSet.add(node.getCondition());
+			node.getArg().visit(this);
+		}
+
+	}
 }


[02/16] incubator-rya git commit: RYA-32 Improve how metadata and values are written to Accumulo PCJ tables

Posted by mi...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/ExternalProcessorTest.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/ExternalProcessorTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/ExternalProcessorTest.java
deleted file mode 100644
index bac9871..0000000
--- a/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/ExternalProcessorTest.java
+++ /dev/null
@@ -1,1654 +0,0 @@
-package mvm.rya.indexing.external.tupleSet;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-
-import mvm.rya.indexing.external.ExternalProcessor;
-import mvm.rya.indexing.external.ExternalProcessor.BindingSetAssignmentCollector;
-import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
-import mvm.rya.indexing.external.tupleSet.SimpleExternalTupleSet;
-
-import org.junit.Test;
-
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
-import org.junit.Assert;
-import org.openrdf.query.algebra.Projection;
-import org.openrdf.query.algebra.QueryModelNode;
-import org.openrdf.query.algebra.StatementPattern;
-import org.openrdf.query.algebra.TupleExpr;
-import org.openrdf.query.algebra.helpers.QueryModelVisitorBase;
-import org.openrdf.query.algebra.helpers.StatementPatternCollector;
-import org.openrdf.query.parser.ParsedQuery;
-import org.openrdf.query.parser.sparql.SPARQLParser;
-
-import com.google.common.collect.Sets;
-
-
-
-
-
-
-public class ExternalProcessorTest {
-	
-	
-	
-	
-	private String queryString = ""//
-			+ "SELECT ?e ?c ?l ?o " //
-			+ "{" //
-			+ "  ?e a ?c . "//
-			+ "  ?c a ?l . "//
-			+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-			+ "  ?e <uri:talksTo> ?o  "//
-			+ "}";//
-
-	private String indexSparqlString = ""//
-			+ "SELECT ?x ?y ?z " //
-			+ "{" //
-			+ "  ?x <http://www.w3.org/2000/01/rdf-schema#label> ?z. "//
-			+ "  ?x a ?y . "//
-			+ "  ?y a ?z  "//
-			+ "}";//
-	
-	
-	private String q1 = ""//
-			+ "SELECT ?e ?l ?c " //
-			+ "{" //
-			+ "  ?e a ?c . "//
-			+ "  ?c <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-			+ "  ?l <uri:talksTo> ?e . "//
-			+ "}";//
-
-	private String q2 = ""//
-			+ "SELECT ?a ?t ?v  " //
-			+ "{" //
-			+ "  ?a a ?t . "//
-			+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?v . "//
-			+ "  ?v <uri:talksTo> ?a . "//
-			+ "}";//
-	
-	
-	
-	private String q5 = ""//
-			+ "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r " //
-			+ "{" //
-			+ "  ?f a ?m ."//
-			+ "  ?e a ?l ."//
-			+ "  ?n a ?o ."//
-			+ "  ?a a ?h ."//
-			+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-			+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-			+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
-			+ "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
-			+ "  ?d <uri:talksTo> ?f . "//
-			+ "  ?c <uri:talksTo> ?e . "//
-			+ "  ?p <uri:talksTo> ?n . "//
-			+ "  ?r <uri:talksTo> ?a . "//
-			+ "}";//
-	
-	
-	
-	private String q7 = ""//
-			+ "SELECT ?s ?t ?u " //
-			+ "{" //
-			+ "  ?s a ?t ."//
-			+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-			+ "  ?u <uri:talksTo> ?s . "//
-			+ "}";//
-	
-	
-	private String q8 = ""//
-			+ "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r " //
-			+ "{" //
-			+ "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
-			+ "  ?f a ?m ."//
-			+ "  ?p <uri:talksTo> ?n . "//
-			+ "  ?e a ?l ."//
-			+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
-			+ "  ?d <uri:talksTo> ?f . "//
-			+ "  ?c <uri:talksTo> ?e . "//
-			+ "  ?n a ?o ."//
-			+ "  ?a a ?h ."//
-			+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-			+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-			+ "  ?r <uri:talksTo> ?a . "//
-			+ "}";//
-	
-	
-	
-	
-	private String q11 = ""//
-			+ "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r ?x ?y ?w ?t ?duck ?chicken ?pig ?rabbit " //
-			+ "{" //
-			+ "  ?w a ?t ."//
-			+ "  ?x a ?y ."//
-			+ "  ?duck a ?chicken ."//
-			+ "  ?pig a ?rabbit ."//
-			+ "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
-			+ "  ?f a ?m ."//
-			+ "  ?p <uri:talksTo> ?n . "//
-			+ "  ?e a ?l ."//
-			+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
-			+ "  ?d <uri:talksTo> ?f . "//
-			+ "  ?c <uri:talksTo> ?e . "//
-			+ "  ?n a ?o ."//
-			+ "  ?a a ?h ."//
-			+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-			+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-			+ "  ?r <uri:talksTo> ?a . "//
-			+ "}";//
-	
-	
-	private String q12 = ""//
-			+ "SELECT ?b ?p ?dog ?cat " //
-			+ "{" //
-			+ "  ?b a ?p ."//
-			+ "  ?dog a ?cat. "//
-			+ "}";//
-	
-	
-	
-	private String q13 = ""//
-			+ "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r ?x ?y ?w ?t ?duck ?chicken ?pig ?rabbit ?dick ?jane ?betty " //
-			+ "{" //
-			+ "  ?w a ?t ."//
-			+ "  ?x a ?y ."//
-			+ "  ?duck a ?chicken ."//
-			+ "  ?pig a ?rabbit ."//
-			+ "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
-			+ "  ?f a ?m ."//
-			+ "  ?p <uri:talksTo> ?n . "//
-			+ "  ?e a ?l ."//
-			+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
-			+ "  ?d <uri:talksTo> ?f . "//
-			+ "  ?c <uri:talksTo> ?e . "//
-			+ "  ?n a ?o ."//
-			+ "  ?a a ?h ."//
-			+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-			+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-			+ "  ?r <uri:talksTo> ?a . "//
-			+ "  ?dick <uri:talksTo> ?jane . "//
-			+ "  ?jane <uri:talksTo> ?betty . "//
-			+ "}";//
-	
-	
-	private String q14 = ""//
-			+ "SELECT ?harry ?susan ?mary " //
-			+ "{" //
-			+ "  ?harry <uri:talksTo> ?susan . "//
-			+ "  ?susan <uri:talksTo> ?mary . "//
-			+ "}";//
-
-
-	
-	String q15 = ""//
-			+ "SELECT ?a ?b ?c ?d ?e ?f ?q " //
-			+ "{" //
-			+ " GRAPH ?x { " //
-			+ "  ?a a ?b ."//
-			+ "  ?b <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-			+ "  ?d <uri:talksTo> ?e . "//
-			+ "  FILTER ( ?e < ?f && (?a > ?b || ?c = ?d) ). " //
-			+ "  FILTER(bound(?f) && sameTerm(?a,?b)&&bound(?q)). " //
-			+ "  ?b a ?q ."//
-			+ "		}"//
-			+ "}";//
-	
-	
-	String q16 = ""//
-			+ "SELECT ?g ?h ?i " //
-			+ "{" //
-			+ " GRAPH ?y { " //
-			+ "  ?g a ?h ."//
-			+ "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?i ."//
-			+ "		}"//
-			+ "}";//
-	
-	String q17 = ""//
-			+ "SELECT ?j ?k ?l ?m ?n ?o " //
-			+ "{" //
-			+ " GRAPH ?z { " //
-			+ "  ?j <uri:talksTo> ?k . "//
-			+ "  FILTER ( ?k < ?l && (?m > ?n || ?o = ?j) ). " //
-			+ "		}"//
-			+ "}";//
-	
-	String q18 = ""//
-			+ "SELECT ?r ?s ?t ?u " //
-			+ "{" //
-			+ " GRAPH ?q { " //
-			+ "  FILTER(bound(?r) && sameTerm(?s,?t)&&bound(?u)). " //
-			+ "  ?t a ?u ."//
-			+ "		}"//
-			+ "}";//
-	
-	
-	
-	String q19 = ""//
-			+ "SELECT ?a ?b ?c ?d ?e ?f ?q ?g ?h " //
-			+ "{" //
-			+ " GRAPH ?x { " //
-			+ "  ?a a ?b ."//
-			+ "  ?b <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-			+ "  ?d <uri:talksTo> ?e . "//
-			+ "  FILTER ( ?e < ?f && (?a > ?b || ?c = ?d) ). " //
-			+ "  FILTER(bound(?f) && sameTerm(?a,?b)&&bound(?q)). " //
-			+ "  FILTER(?g IN (1,2,3) && ?h NOT IN(5,6,7)). " //
-			+ "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?g. "//
-			+ "  ?b a ?q ."//
-			+ "		}"//
-			+ "}";//
-	
-	
-	String q20 = ""//
-			+ "SELECT ?m ?n " //
-			+ "{" //
-			+ " GRAPH ?q { " //
-			+ "  FILTER(?m IN (1,2,3) && ?n NOT IN(5,6,7)). " //
-			+ "  ?n <http://www.w3.org/2000/01/rdf-schema#label> ?m. "//
-			+ "		}"//
-			+ "}";//
-	
-	
-	String q21 = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
-			+ "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
-			+ "SELECT ?feature ?point ?wkt " //
-			+ "{" //
-			+ "  ?feature a geo:Feature . "//
-			+ "  ?feature geo:hasGeometry ?point . "//
-			+ "  ?point a geo:Point . "//
-			+ "  ?point geo:asWKT ?wkt . "//
-			+ "  FILTER(geof:sfWithin(?wkt, \"Polygon\")) " //
-			+ "}";//
-	
-	
-	 String q22 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
-             + "SELECT ?person ?commentmatch ?labelmatch" //
-             + "{" //
-             + "  ?person a <http://example.org/ontology/Person> . "//
-             + "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?labelmatch . "//
-             + "  ?person <http://www.w3.org/2000/01/rdf-schema#comment> ?commentmatch . "//
-             + "  FILTER(fts:text(?labelmatch, \"bob\")) . " //
-             + "  FILTER(fts:text(?commentmatch, \"bob\"))  " //
-             + "}";//
-	 
-	 
-	 String q23 = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
-				+ "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
-				+ "SELECT ?a ?b ?c " //
-				+ "{" //
-				+ "  ?a a geo:Feature . "//
-				+ "  ?b a geo:Point . "//
-				+ "  ?b geo:asWKT ?c . "//
-				+ "  FILTER(geof:sfWithin(?c, \"Polygon\")) " //
-				+ "}";//
-	 
-	 
-	 String q24 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
-             + "SELECT ?f ?g " //
-             + "{" //
-             + "  ?f <http://www.w3.org/2000/01/rdf-schema#comment> ?g . "//
-             + "  FILTER(fts:text(?g, \"bob\"))  " //
-             + "}";//
-	 
-
-	 String q25 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
-             + "SELECT ?person ?commentmatch ?labelmatch ?point" //
-             + "{" //
-             + "  ?person a ?point. " //
-             + "  ?person a <http://example.org/ontology/Person> . "//
-             + "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?labelmatch . "//
-             + "  ?person <http://www.w3.org/2000/01/rdf-schema#comment> ?commentmatch . "//
-             + "  FILTER((?person > ?point) || (?person = ?labelmatch)). "
-             + "  FILTER(fts:text(?labelmatch, \"bob\")) . " //
-             + "  FILTER(fts:text(?commentmatch, \"bob\"))  " //
-             + "}";//
-	 
-	 
-	 String q26 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
-             + "SELECT ?a ?b ?c  " //
-             + "{" //
-             + "  ?a a ?c. " //
-             + "  ?a a <http://example.org/ontology/Person> . "//
-             + "  ?a <http://www.w3.org/2000/01/rdf-schema#label> ?b . "//
-             + "  FILTER((?a > ?c) || (?a = ?b)). "
-             + "  FILTER(fts:text(?b, \"bob\")) . " //
-             + "}";//
-	 
-	 
-	 
-	 String q27 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
-			 + "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
-			 + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
-             + "SELECT ?person ?commentmatch ?labelmatch ?other ?feature ?point ?wkt ?g ?h" //
-             + "{" //
-             + "  ?person a <http://example.org/ontology/Person> . "//
-             + "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?labelmatch . "//
-             + "  ?person <http://www.w3.org/2000/01/rdf-schema#comment> ?commentmatch . "//
-             + "  FILTER((?person > ?other) || (?person = ?labelmatch)). "
-             + "  ?person a ?other. "//
-             + "  FILTER(fts:text(?labelmatch, \"bob\")) . " //
-             + "  FILTER(fts:text(?commentmatch, \"bob\"))  " //
-             + " ?feature a geo:Feature . "//
-		     + "  ?point a geo:Point . "//
-			 + "  ?point geo:asWKT ?wkt . "//
-			 + "  FILTER(geof:sfWithin(?wkt, \"Polygon\")) " //
-			 + "  FILTER(?g IN (1,2,3) && ?h NOT IN(5,6,7)). " //
-			 + "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?g. "//
-             + "}";//
-	 
-	 
-	 String q28 = ""//
-				+ "SELECT ?m ?n " //
-				+ "{" //
-				+ "  FILTER(?m IN (1,2,3) && ?n NOT IN(5,6,7)). " //
-				+ "  ?n <http://www.w3.org/2000/01/rdf-schema#label> ?m. "//
-				+ "}";//
-	 
-	 
-	 String q29 = ""//
-				+ "SELECT ?m ?n ?o" //
-				+ "{" //
-				+ "  FILTER(?m IN (1,2,3) && ?n NOT IN(5,6,7)). " //
-				+ "  ?n <http://www.w3.org/2000/01/rdf-schema#label> ?m. "//
-				+ "  ?m a ?o." //
-				+ "  FILTER(ISNUMERIC(?o))."
-				+ "}";//
-	 
-	 String q30 = ""//
-				+ "SELECT ?pig ?dog ?owl" //
-				+ "{" //
-				+ "  FILTER(?pig IN (1,2,3) && ?dog NOT IN(5,6,7)). " //
-				+ "  ?dog <http://www.w3.org/2000/01/rdf-schema#label> ?pig. "//
-				+ "  ?pig a ?owl. " //
-				+ "  FILTER(ISNUMERIC(?owl))."
-				+ "}";//
-	 
-	 
-	 String q31 = ""//
-	            + "SELECT ?q ?r ?s " //
-	            + "{" //
-	            + "  {?q a ?r} UNION {?r a ?s} ."//
-	            + "  ?r a ?s ."//
-	            + "}";// 
-	 
-	
-	 
-	 String q33 = ""//
-             + "SELECT ?q ?r ?s ?t " //
-             + "{" //
-             + "  OPTIONAL {?q a ?r} ."//
-             + "  ?s a ?t ."//
-             + "}";// 
-	 
-	 
-	 String q34 = ""//
-             + "SELECT ?q ?r  " //
-             + "{" //
-             + "  FILTER(?q > ?r) ."//
-             + "  ?q a ?r ."//
-             + "}";// 
-	 
-	 
-	 String q35 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
-             + "SELECT ?s ?t ?u ?v ?w ?x ?y ?z " //
-             + "{" //
-             + "  FILTER(?s > ?t)."//
-             + "  ?s a ?t ."//
-             + "  FILTER(?u > ?v)."//
-             + "  ?u a ?v ."// 
-             + "  ?w <http://www.w3.org/2000/01/rdf-schema#label> ?x ."//
-             + "  FILTER(fts:text(?x, \"bob\")) . " //
-             + "  ?y <http://www.w3.org/2000/01/rdf-schema#label> ?z ."//
-             + "  FILTER(fts:text(?z, \"bob\")) . " //
-             + "}";// 
-	 
-	 
-	String q36 =  "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
-	        + "SELECT ?dog ?cat  " //
-            + "{" //
-	        + "  ?dog <http://www.w3.org/2000/01/rdf-schema#label> ?cat ."//
-            + "  FILTER(fts:text(?cat, \"bob\")) . " //
-            + "}";// 
-	
-	 
-    String q37 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
-            + "SELECT ?s ?t " //
-            + "{" //
-            + "  FILTER(?s > ?t)."//
-            + "  ?s a ?t ."//
-            + "  FILTER(?s > ?t)."//
-            + "  ?s a ?t ."// 
-             + "  FILTER(?s > ?t)."//
-            + "  ?s a ?t ."// 
-            + "}";// 
-            
-	
-	
-    String q38 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
-            + "SELECT ?s ?t " //
-            + "{" //
-            + "  FILTER(?s > ?t)."//
-            + "  ?s a ?t ."// 
-            + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?s ."//
-            + "  FILTER(?s > ?t)."//
-            + "}";// 
-    
-    
-    
-    String q39 = "PREFIX fts: <http://rdf.useekm.com/fts#> "//
-            + "SELECT ?s ?t " //
-            + "{" //
-            + " VALUES(?s) { (<ub:poodle>)(<ub:pitbull>)} ." //
-            + " ?t <ub:peesOn> <ub:rug> ." //
-            + " ?t <http://www.w3.org/2000/01/rdf-schema#label> ?s ."//
-            + "}";//
-
-    String q40 = "PREFIX fts: <http://rdf.useekm.com/fts#> "//
-            + "SELECT ?u ?v " //
-            + "{" //
-            + " ?v <ub:peesOn> <ub:rug> ." //
-            + " ?v <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-            + "}";//
-
-    String q41 = "PREFIX fts: <http://rdf.useekm.com/fts#> "//
-            + "SELECT ?s ?t ?w ?x" //
-            + "{" //
-            + " FILTER(?s > ?t)."//
-            + " VALUES(?s) { (<ub:poodle>)(<ub:pitbull>)} ." //
-            + " VALUES(?w) { (<ub:persian>) (<ub:siamese>) } ." //
-            + " ?t <ub:peesOn> <ub:rug> ." //
-            + " ?t <http://www.w3.org/2000/01/rdf-schema#label> ?s ."//
-            + " ?w <ub:peesOn> <ub:rug> ." //
-            + " ?w <http://www.w3.org/2000/01/rdf-schema#label> ?x ."//
-            + "}";//
-
-    String q42 = "PREFIX fts: <http://rdf.useekm.com/fts#> "//
-            + "SELECT ?u ?v " //
-            + "{" //
-            + " FILTER(?u > ?v)."//
-            + " ?v <ub:peesOn> <ub:rug> ." //
-            + " ?v <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-            + "}";//
-
-    String q43 = "PREFIX fts: <http://rdf.useekm.com/fts#> "//
-            + "SELECT ?a ?b " //
-            + "{" //
-            + " ?b <ub:peesOn> <ub:rug> ." //
-            + " ?b <http://www.w3.org/2000/01/rdf-schema#label> ?a ."//
-            + "}";//
-    
-	
-
-	
-	@Test
-	public void testVarRelableIndexSmaller() throws Exception {
-
-		SPARQLParser parser1 = new SPARQLParser();
-		SPARQLParser parser2 = new SPARQLParser();
-
-		ParsedQuery pq1 = parser1.parseQuery(queryString, null);
-		ParsedQuery pq2 = parser2.parseQuery(indexSparqlString, null);
-
-		System.out.println("Query is " + pq1.getTupleExpr());
-		System.out.println("Index is " + pq2.getTupleExpr());
-
-		
-		SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-		
-
-		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-		list.add(extTup);
-
-		
-		ExternalProcessor processor = new ExternalProcessor(list);
-		
-		TupleExpr tup = processor.process(pq1.getTupleExpr());
-
-		System.out.println("Processed query is " + tup);
-		
-		
-		ExternalTupleVstor visitor = new ExternalTupleVstor();
-		tup.visit(visitor);
-		
-		StatementPatternCollector spc = new StatementPatternCollector();
-		pq1.getTupleExpr().visit(spc);
-		Set<StatementPattern> qSet = Sets.newHashSet(spc.getStatementPatterns());
-		
-		
-		ExternalTupleVstor eTup = new ExternalTupleVstor();
-		tup.visit(eTup);
-		Set<QueryModelNode> eTupSet =  eTup.getExtTup();
-		Set<StatementPattern> set = Sets.newHashSet();
-		for(QueryModelNode s: eTupSet) {
-			StatementPatternCollector spc1 = new StatementPatternCollector();
-			((ExternalTupleSet) s).getTupleExpr().visit(spc1);
-			Set<StatementPattern> tempSet = Sets.newHashSet(spc1.getStatementPatterns());
-			for(StatementPattern t: tempSet) {
-				set.add(t);
-			}
-			
-		}
-		
-		
-
-		
-		Assert.assertTrue(qSet.containsAll(set) && set.size() != 0);
-
-	}
-	
-	
-	
-	@Test
-	public void testVarRelableIndexSameSize() throws Exception {
-
-		SPARQLParser parser1 = new SPARQLParser();
-		SPARQLParser parser2 = new SPARQLParser();
-
-		ParsedQuery pq1 = parser1.parseQuery(q1, null);
-		ParsedQuery pq2 = parser2.parseQuery(q2, null);
-
-		System.out.println("Query is " + pq1.getTupleExpr());
-		System.out.println("Index is " + pq2.getTupleExpr());
-
-		
-		SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-		
-
-		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-		list.add(extTup);
-
-		
-		ExternalProcessor processor = new ExternalProcessor(list);
-		
-		TupleExpr tup = processor.process(pq1.getTupleExpr());
-
-		System.out.println("Processed query is " + tup);
-		
-		
-		ExternalTupleVstor visitor = new ExternalTupleVstor();
-		tup.visit(visitor);
-		
-		StatementPatternCollector spc = new StatementPatternCollector();
-		pq1.getTupleExpr().visit(spc);
-		Set<StatementPattern> qSet = Sets.newHashSet(spc.getStatementPatterns());
-		
-		
-		ExternalTupleVstor eTup = new ExternalTupleVstor();
-		tup.visit(eTup);
-		Set<QueryModelNode> eTupSet =  eTup.getExtTup();
-		Set<StatementPattern> set = Sets.newHashSet();
-		for(QueryModelNode s: eTupSet) {
-			StatementPatternCollector spc1 = new StatementPatternCollector();
-			((ExternalTupleSet) s).getTupleExpr().visit(spc1);
-			Set<StatementPattern> tempSet = Sets.newHashSet(spc1.getStatementPatterns());
-			for(StatementPattern t: tempSet) {
-				set.add(t);
-			}
-			
-		}
-		
-		
-		
-		Assert.assertTrue(set.equals(qSet));
-
-
-	}
-	
-	
-	
-	
-	
-	@Test
-	public void testTwoIndexLargeQuery() throws Exception {
-
-		SPARQLParser parser1 = new SPARQLParser();
-		SPARQLParser parser2 = new SPARQLParser();
-		SPARQLParser parser3 = new SPARQLParser();
-
-		ParsedQuery pq1 = parser1.parseQuery(q11, null);
-		ParsedQuery pq2 = parser2.parseQuery(q7, null);
-		ParsedQuery pq3 = parser3.parseQuery(q12, null);
-
-		System.out.println("Query is " + pq1.getTupleExpr());
-		System.out.println("Indexes are " + pq2.getTupleExpr() + " and " + pq3.getTupleExpr());
-
-		
-		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(new Projection(pq3.getTupleExpr()));
-
-		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-		list.add(extTup1);
-		list.add(extTup2);
-
-		
-		ExternalProcessor processor = new ExternalProcessor(list);
-		
-		TupleExpr tup = processor.process(pq1.getTupleExpr());
-
-		System.out.println("Processed query is " + tup);
-		
-		
-		
-		ExternalTupleVstor visitor = new ExternalTupleVstor();
-		tup.visit(visitor);
-		
-		StatementPatternCollector spc = new StatementPatternCollector();
-		pq1.getTupleExpr().visit(spc);
-		Set<StatementPattern> qSet = Sets.newHashSet(spc.getStatementPatterns());
-		
-		
-		ExternalTupleVstor eTup = new ExternalTupleVstor();
-		tup.visit(eTup);
-		Set<QueryModelNode> eTupSet =  eTup.getExtTup();
-		Set<StatementPattern> set = Sets.newHashSet();
-		for(QueryModelNode s: eTupSet) {
-			StatementPatternCollector spc1 = new StatementPatternCollector();
-			((ExternalTupleSet) s).getTupleExpr().visit(spc1);
-			Set<StatementPattern> tempSet = Sets.newHashSet(spc1.getStatementPatterns());
-			for(StatementPattern t: tempSet) {
-				set.add(t);
-			}
-			
-		}
-	
-		
-		Assert.assertTrue(set.equals(qSet));
-
-
-	}
-	
-	
-	
-	@Test
-	public void testThreeIndexLargeQuery() throws Exception {
-
-		SPARQLParser parser1 = new SPARQLParser();
-		SPARQLParser parser2 = new SPARQLParser();
-		SPARQLParser parser3 = new SPARQLParser();
-		SPARQLParser parser4 = new SPARQLParser();
-
-		ParsedQuery pq1 = parser1.parseQuery(q13, null);
-		ParsedQuery pq2 = parser2.parseQuery(q5, null);
-		ParsedQuery pq3 = parser3.parseQuery(q12, null);
-		ParsedQuery pq4 = parser4.parseQuery(q14, null);
-
-		System.out.println("Query is " + pq1.getTupleExpr());
-		System.out.println("Indexes are " + pq2.getTupleExpr()+ " , " + pq3.getTupleExpr()+ " , " +pq4.getTupleExpr());
-		
-		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(new Projection(pq3.getTupleExpr()));
-		SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(new Projection(pq4.getTupleExpr()));
-
-		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-		list.add(extTup1);
-		list.add(extTup2);
-		list.add(extTup3);
-		
-		ExternalProcessor processor = new ExternalProcessor(list);
-		
-		TupleExpr tup = processor.process(pq1.getTupleExpr());
-
-		System.out.println("Processed query is " + tup);
-		
-		
-		
-		ExternalTupleVstor visitor = new ExternalTupleVstor();
-		tup.visit(visitor);
-		
-		StatementPatternCollector spc = new StatementPatternCollector();
-		pq1.getTupleExpr().visit(spc);
-		Set<StatementPattern> qSet = Sets.newHashSet(spc.getStatementPatterns());
-		
-		
-		ExternalTupleVstor eTup = new ExternalTupleVstor();
-		tup.visit(eTup);
-		Set<QueryModelNode> eTupSet =  eTup.getExtTup();
-		Set<StatementPattern> set = Sets.newHashSet();
-		for(QueryModelNode s: eTupSet) {
-			StatementPatternCollector spc1 = new StatementPatternCollector();
-			((ExternalTupleSet) s).getTupleExpr().visit(spc1);
-			Set<StatementPattern> tempSet = Sets.newHashSet(spc1.getStatementPatterns());
-			for(StatementPattern t: tempSet) {
-				set.add(t);
-			}
-			
-		}
-		
-		
-		Assert.assertTrue(set.equals(qSet));
-
-	}
-	
-	
-	
-	
-	
-	
-	
-	
-	@Test
-	public void testSingleIndexLargeQuery() throws Exception {
-
-		SPARQLParser parser1 = new SPARQLParser();
-		SPARQLParser parser2 = new SPARQLParser();
-
-		ParsedQuery pq1 = parser1.parseQuery(q8, null);
-		ParsedQuery pq2 = parser2.parseQuery(q7, null);
-
-		System.out.println("Query is " + pq1.getTupleExpr());
-		System.out.println("Index is " + pq2.getTupleExpr());
-
-		
-		SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-		
-
-		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-		list.add(extTup);
-
-		
-		ExternalProcessor processor = new ExternalProcessor(list);
-		
-		TupleExpr tup = processor.process(pq1.getTupleExpr());
-
-		System.out.println("Processed query is " + tup);
-		
-		
-		ExternalTupleVstor visitor = new ExternalTupleVstor();
-		tup.visit(visitor);
-		
-		StatementPatternCollector spc = new StatementPatternCollector();
-		pq1.getTupleExpr().visit(spc);
-		Set<StatementPattern> qSet = Sets.newHashSet(spc.getStatementPatterns());
-		
-		
-		ExternalTupleVstor eTup = new ExternalTupleVstor();
-		tup.visit(eTup);
-		Set<QueryModelNode> eTupSet =  eTup.getExtTup();
-		Set<StatementPattern> set = Sets.newHashSet();
-		for(QueryModelNode s: eTupSet) {
-			StatementPatternCollector spc1 = new StatementPatternCollector();
-			((ExternalTupleSet) s).getTupleExpr().visit(spc1);
-			Set<StatementPattern> tempSet = Sets.newHashSet(spc1.getStatementPatterns());
-			for(StatementPattern t: tempSet) {
-				set.add(t);
-			}
-			
-		}
-		
-		
-		
-		Assert.assertTrue(set.equals(qSet));
-
-	}
-	
-	
-
-	
-	
-
-	@Test
-	public void testContextFilter() throws Exception {
-
-		SPARQLParser parser1 = new SPARQLParser();
-		SPARQLParser parser2 = new SPARQLParser();
-		SPARQLParser parser3 = new SPARQLParser();
-		SPARQLParser parser4 = new SPARQLParser();
-
-		ParsedQuery pq1 = parser1.parseQuery(q15, null);
-		ParsedQuery pq2 = parser2.parseQuery(q16, null);
-		ParsedQuery pq3 = parser3.parseQuery(q17, null);
-		ParsedQuery pq4 = parser4.parseQuery(q18, null);
-
-		System.out.println("Query is " + pq1.getTupleExpr());
-		System.out.println("Indexes are " + pq2.getTupleExpr()+ " , " + pq3.getTupleExpr()+ " , " +pq4.getTupleExpr());
-		
-		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(new Projection(pq3.getTupleExpr()));
-		SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(new Projection(pq4.getTupleExpr()));
-
-		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-		list.add(extTup1);
-		list.add(extTup2);
-		list.add(extTup3);
-		
-		
-		
-		ExternalProcessor processor = new ExternalProcessor(list);
-		
-		TupleExpr tup = processor.process(pq1.getTupleExpr());
-
-		System.out.println("Processed query is " + tup);
-		
-		ExternalTupleVstor visitor = new ExternalTupleVstor();
-		tup.visit(visitor);
-		
-		StatementPatternCollector spc = new StatementPatternCollector();
-		pq1.getTupleExpr().visit(spc);
-		Set<StatementPattern> qSet = Sets.newHashSet(spc.getStatementPatterns());
-		
-		
-		ExternalTupleVstor eTup = new ExternalTupleVstor();
-		tup.visit(eTup);
-		Set<QueryModelNode> eTupSet =  eTup.getExtTup();
-		Set<StatementPattern> set = Sets.newHashSet();
-		for(QueryModelNode s: eTupSet) {
-			StatementPatternCollector spc1 = new StatementPatternCollector();
-			((ExternalTupleSet) s).getTupleExpr().visit(spc1);
-			Set<StatementPattern> tempSet = Sets.newHashSet(spc1.getStatementPatterns());
-			for(StatementPattern t: tempSet) {
-				set.add(t);
-			}
-			
-		}
-		
-		
-		Assert.assertTrue(qSet.containsAll(set) && eTupSet.size() == 1);
-	}
-	
-	
-	
-	
-
-	@Test
-	public void testContextFilterFourIndex() throws Exception {
-
-		SPARQLParser parser1 = new SPARQLParser();
-		SPARQLParser parser2 = new SPARQLParser();
-		SPARQLParser parser3 = new SPARQLParser();
-		SPARQLParser parser4 = new SPARQLParser();
-		SPARQLParser parser5 = new SPARQLParser();
-
-		ParsedQuery pq1 = parser1.parseQuery(q19, null);
-		ParsedQuery pq2 = parser2.parseQuery(q16, null);
-		ParsedQuery pq3 = parser3.parseQuery(q17, null);
-		ParsedQuery pq4 = parser4.parseQuery(q18, null);
-		ParsedQuery pq5 = parser5.parseQuery(q20, null);
-
-		System.out.println("Query is " + pq1.getTupleExpr());
-		System.out.println("Indexes are " + pq2.getTupleExpr()+ " , " + pq3.getTupleExpr()+ " , " +pq4.getTupleExpr()+ " , " +pq5.getTupleExpr());
-		
-		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(new Projection(pq3.getTupleExpr()));
-		SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(new Projection(pq4.getTupleExpr()));
-		SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet(new Projection(pq5.getTupleExpr()));
-		
-		
-
-		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-		list.add(extTup1);
-		list.add(extTup2);
-		list.add(extTup3);
-		list.add(extTup4);
-
-		ExternalProcessor processor = new ExternalProcessor(list);
-		
-		TupleExpr tup = processor.process(pq1.getTupleExpr());
-
-		System.out.println("Processed query is " + tup);
-		
-		ExternalTupleVstor visitor = new ExternalTupleVstor();
-		tup.visit(visitor);
-		
-		StatementPatternCollector spc = new StatementPatternCollector();
-		pq1.getTupleExpr().visit(spc);
-		Set<StatementPattern> qSet = Sets.newHashSet(spc.getStatementPatterns());
-		
-		
-		ExternalTupleVstor eTup = new ExternalTupleVstor();
-		tup.visit(eTup);
-		Set<QueryModelNode> eTupSet =  eTup.getExtTup();
-		Set<StatementPattern> set = Sets.newHashSet();
-		for(QueryModelNode s: eTupSet) {
-			StatementPatternCollector spc1 = new StatementPatternCollector();
-			((ExternalTupleSet) s).getTupleExpr().visit(spc1);
-			Set<StatementPattern> tempSet = Sets.newHashSet(spc1.getStatementPatterns());
-			for(StatementPattern t: tempSet) {
-				set.add(t);
-			}
-			
-		}
-		
-		
-		Assert.assertTrue(qSet.containsAll(set) && eTupSet.size() == 2);
-	}
-	
-	
-	
-	
-	@Test
-	public void testGeoIndexFunction() throws Exception {
-
-		SPARQLParser parser1 = new SPARQLParser();
-		SPARQLParser parser2 = new SPARQLParser();
-
-		ParsedQuery pq1 = parser1.parseQuery(q21, null);
-		ParsedQuery pq2 = parser2.parseQuery(q23, null);
-
-		System.out.println("Query is " + pq1.getTupleExpr());
-		System.out.println("Index is " + pq2.getTupleExpr());
-
-		
-		SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-		
-
-		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-		list.add(extTup);
-
-		
-		ExternalProcessor processor = new ExternalProcessor(list);
-		
-		TupleExpr tup = processor.process(pq1.getTupleExpr());
-
-		System.out.println("Processed query is " + tup);
-		
-		
-		ExternalTupleVstor visitor = new ExternalTupleVstor();
-		tup.visit(visitor);
-		
-		StatementPatternCollector spc = new StatementPatternCollector();
-		pq1.getTupleExpr().visit(spc);
-		Set<StatementPattern> qSet = Sets.newHashSet(spc.getStatementPatterns());
-		
-		
-		ExternalTupleVstor eTup = new ExternalTupleVstor();
-		tup.visit(eTup);
-		Set<QueryModelNode> eTupSet =  eTup.getExtTup();
-		Set<StatementPattern> set = Sets.newHashSet();
-		for(QueryModelNode s: eTupSet) {
-			StatementPatternCollector spc1 = new StatementPatternCollector();
-			((ExternalTupleSet) s).getTupleExpr().visit(spc1);
-			Set<StatementPattern> tempSet = Sets.newHashSet(spc1.getStatementPatterns());
-			for(StatementPattern t: tempSet) {
-				set.add(t);
-			}
-			
-		}
-		
-		
-		
-		Assert.assertTrue(qSet.containsAll(set) && set.size() != 0);
-
-	}
-	
-	
-	
-	@Test
-	public void testFreeTestIndexFunction() throws Exception {
-
-		SPARQLParser parser1 = new SPARQLParser();
-		SPARQLParser parser2 = new SPARQLParser();
-
-		ParsedQuery pq1 = parser1.parseQuery(q22, null);
-		ParsedQuery pq2 = parser2.parseQuery(q24, null);
-
-		System.out.println("Query is " + pq1.getTupleExpr());
-		System.out.println("Index is " + pq2.getTupleExpr());
-
-		
-		SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-		
-
-		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-		list.add(extTup);
-
-		ExternalProcessor processor = new ExternalProcessor(list);
-		
-		TupleExpr tup = processor.process(pq1.getTupleExpr());
-
-		System.out.println("Processed query is " + tup);
-		
-		
-		ExternalTupleVstor visitor = new ExternalTupleVstor();
-		tup.visit(visitor);
-		
-		StatementPatternCollector spc = new StatementPatternCollector();
-		pq1.getTupleExpr().visit(spc);
-		Set<StatementPattern> qSet = Sets.newHashSet(spc.getStatementPatterns());
-		
-		
-		ExternalTupleVstor eTup = new ExternalTupleVstor();
-		tup.visit(eTup);
-		Set<QueryModelNode> eTupSet =  eTup.getExtTup();
-		Set<StatementPattern> set = Sets.newHashSet();
-		for(QueryModelNode s: eTupSet) {
-			StatementPatternCollector spc1 = new StatementPatternCollector();
-			((ExternalTupleSet) s).getTupleExpr().visit(spc1);
-			Set<StatementPattern> tempSet = Sets.newHashSet(spc1.getStatementPatterns());
-			for(StatementPattern t: tempSet) {
-				set.add(t);
-			}
-			
-		}
-		
-		
-		
-		Assert.assertTrue(qSet.containsAll(set) && set.size() != 0);
-
-	}
-	
-	
-	@Test
-	public void testThreeIndexGeoFreeCompareFilterMix() throws Exception {
-
-		SPARQLParser parser1 = new SPARQLParser();
-		SPARQLParser parser2 = new SPARQLParser();
-		SPARQLParser parser3 = new SPARQLParser();
-
-		ParsedQuery pq1 = parser1.parseQuery(q25, null);
-		ParsedQuery pq2 = parser2.parseQuery(q24, null);
-		ParsedQuery pq3 = parser3.parseQuery(q26, null);
-
-		System.out.println("Query is " + pq1.getTupleExpr());
-		System.out.println("Indexes are " + pq2.getTupleExpr() + " and " + pq3.getTupleExpr());
-
-		
-		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(new Projection(pq3.getTupleExpr()));
-
-		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-		list.add(extTup1);
-		list.add(extTup2);
-
-		
-		ExternalProcessor processor = new ExternalProcessor(list);
-		
-		TupleExpr tup = processor.process(pq1.getTupleExpr());
-
-		System.out.println("Processed query is " + tup);
-		
-		
-		
-		ExternalTupleVstor visitor = new ExternalTupleVstor();
-		tup.visit(visitor);
-		
-		StatementPatternCollector spc = new StatementPatternCollector();
-		pq1.getTupleExpr().visit(spc);
-		Set<StatementPattern> qSet = Sets.newHashSet(spc.getStatementPatterns());
-		
-		
-		ExternalTupleVstor eTup = new ExternalTupleVstor();
-		tup.visit(eTup);
-		Set<QueryModelNode> eTupSet =  eTup.getExtTup();
-		Set<StatementPattern> set = Sets.newHashSet();
-		for(QueryModelNode s: eTupSet) {
-			StatementPatternCollector spc1 = new StatementPatternCollector();
-			((ExternalTupleSet) s).getTupleExpr().visit(spc1);
-			Set<StatementPattern> tempSet = Sets.newHashSet(spc1.getStatementPatterns());
-			for(StatementPattern t: tempSet) {
-				set.add(t);
-			}
-			
-		}
-	
-		
-		Assert.assertTrue(set.equals(qSet) && eTupSet.size() == 2);
-
-
-	}
-	
-	
-	
-	
-	
-	@Test
-	public void testFourIndexGeoFreeCompareFilterMix() throws Exception {
-
-		SPARQLParser parser1 = new SPARQLParser();
-		SPARQLParser parser2 = new SPARQLParser();
-		SPARQLParser parser3 = new SPARQLParser();
-		SPARQLParser parser4 = new SPARQLParser();
-		SPARQLParser parser5 = new SPARQLParser();
-
-		ParsedQuery pq1 = parser1.parseQuery(q27, null);
-		ParsedQuery pq2 = parser2.parseQuery(q23, null);
-		ParsedQuery pq3 = parser3.parseQuery(q26, null);
-		ParsedQuery pq4 = parser4.parseQuery(q24, null);
-		ParsedQuery pq5 = parser5.parseQuery(q28, null);
-		
-		System.out.println("Query is " + pq1.getTupleExpr());
-		System.out.println("Indexes are " + pq2.getTupleExpr() + " , " + pq3.getTupleExpr() + " , " + pq4.getTupleExpr()+ " and " + pq5.getTupleExpr());
-
-		
-		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(new Projection(pq3.getTupleExpr()));
-		SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(new Projection(pq4.getTupleExpr()));
-		SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet(new Projection(pq5.getTupleExpr()));
-
-
-		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-		list.add(extTup4);
-		list.add(extTup1);
-		list.add(extTup2);
-		list.add(extTup3);
-
-		
-		ExternalProcessor processor = new ExternalProcessor(list);
-		
-		TupleExpr tup = processor.process(pq1.getTupleExpr());
-
-		System.out.println("Processed query is " + tup);
-		
-		
-		
-		ExternalTupleVstor visitor = new ExternalTupleVstor();
-		tup.visit(visitor);
-		
-		StatementPatternCollector spc = new StatementPatternCollector();
-		pq1.getTupleExpr().visit(spc);
-		Set<StatementPattern> qSet = Sets.newHashSet(spc.getStatementPatterns());
-		
-		
-		ExternalTupleVstor eTup = new ExternalTupleVstor();
-		tup.visit(eTup);
-		Set<QueryModelNode> eTupSet =  eTup.getExtTup();
-		Assert.assertTrue(eTupSet.size() == 4);
-		Set<StatementPattern> set = Sets.newHashSet();
-		for(QueryModelNode s: eTupSet) {
-			StatementPatternCollector spc1 = new StatementPatternCollector();
-			((ExternalTupleSet) s).getTupleExpr().visit(spc1);
-			Set<StatementPattern> tempSet = Sets.newHashSet(spc1.getStatementPatterns());
-			for(StatementPattern t: tempSet) {
-				set.add(t);
-			}
-			
-		}
-	
-		
-		Assert.assertTrue(set.equals(qSet));
-
-
-
-	}
-	
-	
-	
-	
-	
-	@Test
-	public void testThreeIndexGeoFreeCompareFilterMix2() throws Exception {
-
-		SPARQLParser parser1 = new SPARQLParser();
-		SPARQLParser parser2 = new SPARQLParser();
-		SPARQLParser parser3 = new SPARQLParser();
-		SPARQLParser parser4 = new SPARQLParser();
-		
-
-		ParsedQuery pq1 = parser1.parseQuery(q27, null);
-		ParsedQuery pq2 = parser2.parseQuery(q23, null);
-		ParsedQuery pq3 = parser3.parseQuery(q26, null);
-		ParsedQuery pq4 = parser4.parseQuery(q28, null);
-		
-		
-		System.out.println("Query is " + pq1.getTupleExpr());
-		System.out.println("Indexes are " + pq2.getTupleExpr() + " , " + pq3.getTupleExpr() + " , " + pq4.getTupleExpr());
-
-		
-		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(new Projection(pq3.getTupleExpr()));
-		SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(new Projection(pq4.getTupleExpr()));
-		
-
-
-		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-
-		list.add(extTup1);
-		list.add(extTup3);
-		list.add(extTup2);
-		
-
-		
-		ExternalProcessor processor = new ExternalProcessor(list);
-		
-		TupleExpr tup = processor.process(pq1.getTupleExpr());
-
-		System.out.println("Processed query is " + tup);
-		
-		
-		
-		ExternalTupleVstor visitor = new ExternalTupleVstor();
-		tup.visit(visitor);
-		
-		StatementPatternCollector spc = new StatementPatternCollector();
-		pq1.getTupleExpr().visit(spc);
-		Set<StatementPattern> qSet = Sets.newHashSet(spc.getStatementPatterns());
-		
-		
-		ExternalTupleVstor eTup = new ExternalTupleVstor();
-		tup.visit(eTup);
-		Set<QueryModelNode> eTupSet =  eTup.getExtTup();
-		Assert.assertTrue(eTupSet.size() == 3);
-		Set<StatementPattern> set = Sets.newHashSet();
-		for(QueryModelNode s: eTupSet) {
-			StatementPatternCollector spc1 = new StatementPatternCollector();
-			((ExternalTupleSet) s).getTupleExpr().visit(spc1);
-			Set<StatementPattern> tempSet = Sets.newHashSet(spc1.getStatementPatterns());
-			for(StatementPattern t: tempSet) {
-				set.add(t);
-			}
-			
-		}
-	
-		Assert.assertTrue(qSet.containsAll(set));
-	
-
-	}
-	
-	
-	
-	
-	
-	
-	
-	@Test
-	public void testISNUMERIC() throws Exception {
-
-		SPARQLParser parser1 = new SPARQLParser();
-		SPARQLParser parser2 = new SPARQLParser();
-
-		ParsedQuery pq1 = parser1.parseQuery(q29, null);
-		ParsedQuery pq2 = parser2.parseQuery(q30, null);
-
-		System.out.println("Query is " + pq1.getTupleExpr());
-		System.out.println("Index is " + pq2.getTupleExpr());
-
-		
-		SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-		
-
-		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-		list.add(extTup);
-
-		
-		ExternalProcessor processor = new ExternalProcessor(list);
-		
-		TupleExpr tup = processor.process(pq1.getTupleExpr());
-
-		System.out.println("Processed query is " + tup);
-		
-		
-		ExternalTupleVstor visitor = new ExternalTupleVstor();
-		tup.visit(visitor);
-		
-		StatementPatternCollector spc = new StatementPatternCollector();
-		pq1.getTupleExpr().visit(spc);
-		Set<StatementPattern> qSet = Sets.newHashSet(spc.getStatementPatterns());
-		
-		
-		ExternalTupleVstor eTup = new ExternalTupleVstor();
-		tup.visit(eTup);
-		Set<QueryModelNode> eTupSet =  eTup.getExtTup();
-		Set<StatementPattern> set = Sets.newHashSet();
-		for(QueryModelNode s: eTupSet) {
-			StatementPatternCollector spc1 = new StatementPatternCollector();
-			((ExternalTupleSet) s).getTupleExpr().visit(spc1);
-			Set<StatementPattern> tempSet = Sets.newHashSet(spc1.getStatementPatterns());
-			for(StatementPattern t: tempSet) {
-				set.add(t);
-			}
-			
-		}
-		
-		Assert.assertTrue(set.equals(qSet) && eTupSet.size() == 1);
-		
-
-	}
-	
-	
-	@Test
-    public void testInvalidQueryUnion() throws Exception {
-
-        SPARQLParser parser1 = new SPARQLParser();
-        SPARQLParser parser2 = new SPARQLParser();
-
-        ParsedQuery pq1 = parser1.parseQuery(q31, null);
-        ParsedQuery pq2 = parser2.parseQuery(q31, null);
-
-        System.out.println("Query is " + pq1.getTupleExpr());
-        System.out.println("Index is " + pq2.getTupleExpr());
-
-        
-        SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-        
-
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-        list.add(extTup);
-        
-        boolean thrown = false;
-
-        try {
-            ExternalProcessor processor = new ExternalProcessor(list);
-            processor.process(pq1.getTupleExpr());
-        } catch (IllegalArgumentException e) {
-            System.out.println(e);
-            thrown = true;
-        }
-
-        Assert.assertTrue(thrown);
-
-    }
-	
-	
-
-    
-	
-	@Test
-    public void testInvalidQueryOptional() throws Exception {
-
-        SPARQLParser parser1 = new SPARQLParser();
-        SPARQLParser parser2 = new SPARQLParser();
-
-        ParsedQuery pq1 = parser1.parseQuery(q33, null);
-        ParsedQuery pq2 = parser2.parseQuery(q33, null);
-
-        System.out.println("Query is " + pq1.getTupleExpr());
-        System.out.println("Index is " + pq2.getTupleExpr());
-
-        
-        SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-        
-
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-        list.add(extTup);
-        
-        boolean thrown = false;
-
-        try {
-            ExternalProcessor processor = new ExternalProcessor(list);
-            processor.process(pq1.getTupleExpr());
-        } catch (IllegalArgumentException e) {
-            System.out.println(e);
-            thrown = true;
-        }
-
-        Assert.assertTrue(thrown);
-
-    }
-    
-	
-	
-	
-	@Test
-    public void testTwoRepeatedIndex() throws Exception {
-
-        SPARQLParser parser1 = new SPARQLParser();
-        SPARQLParser parser2 = new SPARQLParser();
-        SPARQLParser parser3 = new SPARQLParser();
-
-        ParsedQuery pq1 = parser1.parseQuery(q35, null);
-        ParsedQuery pq2 = parser2.parseQuery(q34, null);
-        ParsedQuery pq3 = parser3.parseQuery(q36, null);
-
-        System.out.println("Query is " + pq1.getTupleExpr());
-        System.out.println("Indexes are " + pq2.getTupleExpr() + " and " + pq3.getTupleExpr());
-
-        
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(new Projection(pq3.getTupleExpr()));
-
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-        list.add(extTup1);
-        list.add(extTup2);
-
-        
-        ExternalProcessor processor = new ExternalProcessor(list);
-        
-        TupleExpr tup = processor.process(pq1.getTupleExpr());
-
-        System.out.println("Processed query is " + tup);
-        
-        
-        
-        ExternalTupleVstor visitor = new ExternalTupleVstor();
-        tup.visit(visitor);
-        
-        StatementPatternCollector spc = new StatementPatternCollector();
-        pq1.getTupleExpr().visit(spc);
-        Set<StatementPattern> qSet = Sets.newHashSet(spc.getStatementPatterns());
-        
-        
-        ExternalTupleVstor eTup = new ExternalTupleVstor();
-        tup.visit(eTup);
-        Set<QueryModelNode> eTupSet =  eTup.getExtTup();
-        Set<StatementPattern> set = Sets.newHashSet();
-        for(QueryModelNode s: eTupSet) {
-            StatementPatternCollector spc1 = new StatementPatternCollector();
-            ((ExternalTupleSet) s).getTupleExpr().visit(spc1);
-            Set<StatementPattern> tempSet = Sets.newHashSet(spc1.getStatementPatterns());
-            for(StatementPattern t: tempSet) {
-                set.add(t);
-            }
-            
-        }
-    
-        
-        Assert.assertTrue(set.equals(qSet) && eTupSet.size()==4);
-
-
-    }
-    
-	
-	
-	@Test
-    public void testRepeatedStatementPatternQuery() throws Exception {
-
-        SPARQLParser parser1 = new SPARQLParser();
-        SPARQLParser parser2 = new SPARQLParser();
-
-        ParsedQuery pq1 = parser1.parseQuery(q37, null);
-        ParsedQuery pq2 = parser2.parseQuery(q34, null);
-
-        System.out.println("Query is " + pq1.getTupleExpr());
-        System.out.println("Index is " + pq2.getTupleExpr());
-
-        
-        SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-        
-
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-        list.add(extTup);
-        
-        boolean thrown = false;
-
-        try {
-            ExternalProcessor processor = new ExternalProcessor(list);
-            processor.process(pq1.getTupleExpr());
-        } catch (IllegalArgumentException e) {
-            System.out.println(e);
-            thrown = true;
-        }
-
-        Assert.assertTrue(thrown);
-    }
-	
-	
-	
-	
-
- 
-    
-    
-	@Test
-    public void testRepeatedFilterQuery() throws Exception {
-
-        SPARQLParser parser1 = new SPARQLParser();
-        SPARQLParser parser2 = new SPARQLParser();
-
-        ParsedQuery pq1 = parser1.parseQuery(q38, null);
-        ParsedQuery pq2 = parser2.parseQuery(q38, null);
-
-        System.out.println("Query is " + pq1.getTupleExpr());
-        System.out.println("Index is " + pq2.getTupleExpr());
-
-        
-        SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-        
-
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-        list.add(extTup);
-        
-        boolean thrown = false;
-
-        try {
-            ExternalProcessor processor = new ExternalProcessor(list);
-            processor.process(pq1.getTupleExpr());
-        } catch (IllegalArgumentException e) {
-            System.out.println(e);
-            thrown = true;
-        }
-
-        Assert.assertTrue(thrown);
-    }
-	
-	
-
-	
-    @Test
-    public void testBindingSetAssignment1() throws Exception {
-
-        SPARQLParser parser1 = new SPARQLParser();
-        SPARQLParser parser2 = new SPARQLParser();
-
-        ParsedQuery pq1 = parser1.parseQuery(q39, null);
-        ParsedQuery pq2 = parser2.parseQuery(q40, null);
-
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-
-        list.add(extTup1);
-
-        ExternalProcessor processor = new ExternalProcessor(list);
-
-        TupleExpr tup = processor.process(pq1.getTupleExpr());
-
-        ExternalTupleVstor visitor = new ExternalTupleVstor();
-        tup.visit(visitor);
-
-        StatementPatternCollector spc = new StatementPatternCollector();
-        pq1.getTupleExpr().visit(spc);
-        Set<StatementPattern> qSet = Sets.newHashSet(spc.getStatementPatterns());
-
-        ExternalTupleVstor eTup = new ExternalTupleVstor();
-        tup.visit(eTup);
-        Set<QueryModelNode> eTupSet = eTup.getExtTup();
-        Set<StatementPattern> set = Sets.newHashSet();
-        for (QueryModelNode s : eTupSet) {
-            StatementPatternCollector spc1 = new StatementPatternCollector();
-            ((ExternalTupleSet) s).getTupleExpr().visit(spc1);
-            Set<StatementPattern> tempSet = Sets.newHashSet(spc1.getStatementPatterns());
-            for (StatementPattern t : tempSet) {
-                set.add(t);
-            }
-
-            Assert.assertTrue(set.equals(qSet) && eTupSet.size() == 1);
-
-            BindingSetAssignmentCollector bsac1 = new BindingSetAssignmentCollector();
-            BindingSetAssignmentCollector bsac2 = new BindingSetAssignmentCollector();
-            pq1.getTupleExpr().visit(bsac1);
-            tup.visit(bsac2);
-
-            Assert.assertTrue(bsac1.getBindingSetAssignments().equals(bsac2.getBindingSetAssignments()));
-
-        }
-    }
-    
-    
-    @Test
-    public void testBindingSetAssignment2() throws Exception {
-
-        SPARQLParser parser1 = new SPARQLParser();
-        SPARQLParser parser2 = new SPARQLParser();
-        SPARQLParser parser3 = new SPARQLParser();
-
-        ParsedQuery pq1 = parser1.parseQuery(q41, null);
-        ParsedQuery pq2 = parser2.parseQuery(q42, null);
-        ParsedQuery pq3 = parser2.parseQuery(q43, null);
-
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(new Projection(pq3.getTupleExpr()));
-
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-        list.add(extTup1);
-        list.add(extTup2);
-
-        ExternalProcessor processor = new ExternalProcessor(list);
-        TupleExpr tup = processor.process(pq1.getTupleExpr());
-        System.out.println("Processed query is " + tup);
-
-        ExternalTupleVstor visitor = new ExternalTupleVstor();
-        tup.visit(visitor);
-
-        StatementPatternCollector spc = new StatementPatternCollector();
-        pq1.getTupleExpr().visit(spc);
-        Set<StatementPattern> qSet = Sets.newHashSet(spc.getStatementPatterns());
-        ExternalTupleVstor eTup = new ExternalTupleVstor();
-        tup.visit(eTup);
-        Set<QueryModelNode> eTupSet = eTup.getExtTup();
-        Set<StatementPattern> set = Sets.newHashSet();
-        for (QueryModelNode s : eTupSet) {
-            StatementPatternCollector spc1 = new StatementPatternCollector();
-            ((ExternalTupleSet) s).getTupleExpr().visit(spc1);
-            Set<StatementPattern> tempSet = Sets.newHashSet(spc1.getStatementPatterns());
-            for (StatementPattern t : tempSet) {
-                set.add(t);
-            }
-        }
-
-        Assert.assertTrue(set.equals(qSet) && eTupSet.size() == 2);
-
-        BindingSetAssignmentCollector bsac1 = new BindingSetAssignmentCollector();
-        BindingSetAssignmentCollector bsac2 = new BindingSetAssignmentCollector();
-        pq1.getTupleExpr().visit(bsac1);
-        tup.visit(bsac2);
-
-        Assert.assertTrue(bsac1.getBindingSetAssignments().equals(bsac2.getBindingSetAssignments()));
-
-    }
-    
-    
-	
-    public static class ExternalTupleVstor extends QueryModelVisitorBase<RuntimeException> {
-
-        private Set<QueryModelNode> eSet = new HashSet<QueryModelNode>();
-
-        @Override
-        public void meetNode(QueryModelNode node) throws RuntimeException {
-            if (node instanceof ExternalTupleSet) {
-                eSet.add(node);
-            }
-            super.meetNode(node);
-        }
-
-        public Set<QueryModelNode> getExtTup() {
-            return eSet;
-        }
-
-    }
-	
-	
-	
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/PcjTablesIntegrationTests.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/PcjTablesIntegrationTests.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/PcjTablesIntegrationTests.java
new file mode 100644
index 0000000..b8b6a57
--- /dev/null
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/PcjTablesIntegrationTests.java
@@ -0,0 +1,413 @@
+package mvm.rya.indexing.external.tupleSet;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.junit.Assert.assertEquals;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Map.Entry;
+import java.util.Set;
+
+import mvm.rya.accumulo.AccumuloRdfConfiguration;
+import mvm.rya.accumulo.AccumuloRyaDAO;
+import mvm.rya.api.RdfCloudTripleStoreConfiguration;
+import mvm.rya.api.resolver.RyaTypeResolverException;
+import mvm.rya.indexing.accumulo.ConfigUtils;
+import mvm.rya.indexing.external.tupleSet.PcjTables.PcjException;
+import mvm.rya.indexing.external.tupleSet.PcjTables.PcjMetadata;
+import mvm.rya.indexing.external.tupleSet.PcjTables.PcjTableNameFactory;
+import mvm.rya.indexing.external.tupleSet.PcjTables.PcjVarOrderFactory;
+import mvm.rya.indexing.external.tupleSet.PcjTables.ShiftVarOrderFactory;
+import mvm.rya.indexing.external.tupleSet.PcjTables.VariableOrder;
+import mvm.rya.rdftriplestore.RdfCloudTripleStore;
+import mvm.rya.rdftriplestore.RyaSailRepository;
+
+import org.apache.accumulo.core.client.AccumuloException;
+import org.apache.accumulo.core.client.AccumuloSecurityException;
+import org.apache.accumulo.core.client.Connector;
+import org.apache.accumulo.core.client.Instance;
+import org.apache.accumulo.core.client.Scanner;
+import org.apache.accumulo.core.client.TableNotFoundException;
+import org.apache.accumulo.core.client.ZooKeeperInstance;
+import org.apache.accumulo.core.client.security.tokens.PasswordToken;
+import org.apache.accumulo.core.data.Key;
+import org.apache.accumulo.core.data.Value;
+import org.apache.accumulo.core.security.Authorizations;
+import org.apache.accumulo.minicluster.MiniAccumuloCluster;
+import org.apache.hadoop.io.Text;
+import org.apache.log4j.Logger;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.openrdf.model.Statement;
+import org.openrdf.model.impl.LiteralImpl;
+import org.openrdf.model.impl.NumericLiteralImpl;
+import org.openrdf.model.impl.StatementImpl;
+import org.openrdf.model.impl.URIImpl;
+import org.openrdf.model.vocabulary.XMLSchema;
+import org.openrdf.query.BindingSet;
+import org.openrdf.query.impl.MapBindingSet;
+import org.openrdf.repository.RepositoryConnection;
+import org.openrdf.repository.RepositoryException;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Sets;
+import com.google.common.io.Files;
+
+/**
+ * Performs integration test using {@link MiniAccumuloCluster} to ensure the
+ * functions of {@link PcjTables} work within a cluster setting.
+ */
+public class PcjTablesIntegrationTests {
+    private static final Logger log = Logger.getLogger(PcjTablesIntegrationTests.class);
+
+    protected static final String RYA_TABLE_PREFIX = "demo_";
+
+    // Rya data store and connections.
+    protected MiniAccumuloCluster accumulo = null;
+    protected static Connector accumuloConn = null;
+    protected RyaSailRepository ryaRepo = null;
+    protected RepositoryConnection ryaConn = null;
+
+    @Before
+    public void setupMiniResources() throws IOException, InterruptedException, AccumuloException, AccumuloSecurityException, RepositoryException {
+        // Initialize the Mini Accumulo that will be used to store Triples and get a connection to it.
+        accumulo = startMiniAccumulo();
+
+        // Setup the Rya library to use the Mini Accumulo.
+        ryaRepo = setupRya(accumulo);
+        ryaConn = ryaRepo.getConnection();
+    }
+
+    /**
+     * Ensure that when a new PCJ table is created, it is initialized with the
+     * correct metadata values.
+     * <p>
+     * The method being tested is {@link PcjTables#createPcjTable(Connector, String, Set, String)}
+     */
+    @Test
+    public void createPcjTable() throws PcjException {
+        final String sparql =
+                "SELECT ?name ?age " +
+                "{" +
+                  "FILTER(?age < 30) ." +
+                  "?name <http://hasAge> ?age." +
+                  "?name <http://playsSport> \"Soccer\" " +
+                "}";
+
+        // Create a PCJ table in the Mini Accumulo.
+        final String pcjTableName = new PcjTableNameFactory().makeTableName(RYA_TABLE_PREFIX, "testPcj");
+        Set<VariableOrder> varOrders = new ShiftVarOrderFactory().makeVarOrders(new VariableOrder("name;age"));
+        PcjTables pcjs = new PcjTables();
+        pcjs.createPcjTable(accumuloConn, pcjTableName, varOrders, sparql);
+
+        // Fetch the PcjMetadata and ensure it has the correct values.
+        final PcjMetadata pcjMetadata = pcjs.getPcjMetadata(accumuloConn, pcjTableName);
+
+        // Ensure the metadata matches the expected value.
+        final PcjMetadata expected = new PcjMetadata(sparql, 0L, varOrders);
+        assertEquals(expected, pcjMetadata);
+    }
+
+    /**
+     * Ensure when results have been written to the PCJ table that they are in Accumulo.
+     * <p>
+     * The method being tested is {@link PcjTables#addResults(Connector, String, java.util.Collection)}
+     */
+    @Test
+    public void addResults() throws PcjException, TableNotFoundException, RyaTypeResolverException {
+        final String sparql =
+                "SELECT ?name ?age " +
+                "{" +
+                  "FILTER(?age < 30) ." +
+                  "?name <http://hasAge> ?age." +
+                  "?name <http://playsSport> \"Soccer\" " +
+                "}";
+
+        // Create a PCJ table in the Mini Accumulo.
+        final String pcjTableName = new PcjTableNameFactory().makeTableName(RYA_TABLE_PREFIX, "testPcj");
+        Set<VariableOrder> varOrders = new ShiftVarOrderFactory().makeVarOrders(new VariableOrder("name;age"));
+        PcjTables pcjs = new PcjTables();
+        pcjs.createPcjTable(accumuloConn, pcjTableName, varOrders, sparql);
+
+        // Add a few results to the PCJ table.
+        MapBindingSet alice = new MapBindingSet();
+        alice.addBinding("name", new URIImpl("http://Alice"));
+        alice.addBinding("age", new NumericLiteralImpl(14, XMLSchema.INTEGER));
+
+        MapBindingSet bob = new MapBindingSet();
+        bob.addBinding("name", new URIImpl("http://Bob"));
+        bob.addBinding("age", new NumericLiteralImpl(16, XMLSchema.INTEGER));
+
+        MapBindingSet charlie = new MapBindingSet();
+        charlie.addBinding("name", new URIImpl("http://Charlie"));
+        charlie.addBinding("age", new NumericLiteralImpl(12, XMLSchema.INTEGER));
+
+        Set<BindingSet> results = Sets.<BindingSet>newHashSet(alice, bob, charlie);
+        pcjs.addResults(accumuloConn, pcjTableName, results);
+
+        // Make sure the cardinality was updated.
+        PcjMetadata metadata = pcjs.getPcjMetadata(accumuloConn, pcjTableName);
+        assertEquals(3, metadata.getCardinality());
+
+        // Scan Accumulo for the stored results.
+        Multimap<String, BindingSet> fetchedResults = loadPcjResults(accumuloConn, pcjTableName);
+
+        // Ensure the expected results match those that were stored.
+        Multimap<String, BindingSet> expectedResults = HashMultimap.create();
+        expectedResults.putAll("name;age", results);
+        expectedResults.putAll("age;name", results);
+        assertEquals(expectedResults, fetchedResults);
+    }
+
+    /**
+     * Ensure when results are already stored in Rya, that we are able to populate
+     * the PCJ table for a new SPARQL query using those results.
+     * <p>
+     * The method being tested is: {@link PcjTables#populatePcj(Connector, String, RepositoryConnection, String)}
+     */
+    @Test
+    public void populatePcj() throws RepositoryException, PcjException, TableNotFoundException, RyaTypeResolverException {
+        // Load some Triples into Rya.
+        Set<Statement> triples = new HashSet<>();
+        triples.add( new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://hasAge"), new NumericLiteralImpl(14, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://hasAge"), new NumericLiteralImpl(16, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://hasAge"), new NumericLiteralImpl(12, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://hasAge"), new NumericLiteralImpl(43, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+
+        for(Statement triple : triples) {
+            ryaConn.add(triple);
+        }
+
+        // Create a PCJ table that will include those triples in its results.
+        final String sparql =
+                "SELECT ?name ?age " +
+                "{" +
+                  "FILTER(?age < 30) ." +
+                  "?name <http://hasAge> ?age." +
+                  "?name <http://playsSport> \"Soccer\" " +
+                "}";
+
+        final String pcjTableName = new PcjTableNameFactory().makeTableName(RYA_TABLE_PREFIX, "testPcj");
+        Set<VariableOrder> varOrders = new ShiftVarOrderFactory().makeVarOrders(new VariableOrder("name;age"));
+        PcjTables pcjs = new PcjTables();
+        pcjs.createPcjTable(accumuloConn, pcjTableName, varOrders, sparql);
+
+        // Populate the PCJ table using a Rya connection.
+        pcjs.populatePcj(accumuloConn, pcjTableName, ryaConn);
+
+        // Scan Accumulo for the stored results.
+        Multimap<String, BindingSet> fetchedResults = loadPcjResults(accumuloConn, pcjTableName);
+
+        // Make sure the cardinality was updated.
+        PcjMetadata metadata = pcjs.getPcjMetadata(accumuloConn, pcjTableName);
+        assertEquals(3, metadata.getCardinality());
+
+        // Ensure the expected results match those that were stored.
+        MapBindingSet alice = new MapBindingSet();
+        alice.addBinding("name", new URIImpl("http://Alice"));
+        alice.addBinding("age", new NumericLiteralImpl(14, XMLSchema.INTEGER));
+
+        MapBindingSet bob = new MapBindingSet();
+        bob.addBinding("name", new URIImpl("http://Bob"));
+        bob.addBinding("age", new NumericLiteralImpl(16, XMLSchema.INTEGER));
+
+        MapBindingSet charlie = new MapBindingSet();
+        charlie.addBinding("name", new URIImpl("http://Charlie"));
+        charlie.addBinding("age", new NumericLiteralImpl(12, XMLSchema.INTEGER));
+
+        Set<BindingSet> results = Sets.<BindingSet>newHashSet(alice, bob, charlie);
+
+        Multimap<String, BindingSet> expectedResults = HashMultimap.create();
+        expectedResults.putAll("name;age", results);
+        expectedResults.putAll("age;name", results);
+        assertEquals(expectedResults, fetchedResults);
+    }
+
+    /**
+     * Ensure the method that creates a new PCJ table, scans Rya for matches, and
+     * stores them in the PCJ table works.
+     * <p>
+     * The method being tested is: {@link PcjTables#createAndPopulatePcj(RepositoryConnection, Connector, String, String, String[], Optional)}
+     */
+    @Test
+    public void createAndPopulatePcj() throws RepositoryException, PcjException, TableNotFoundException, RyaTypeResolverException {
+        // Load some Triples into Rya.
+        Set<Statement> triples = new HashSet<>();
+        triples.add( new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://hasAge"), new NumericLiteralImpl(14, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://hasAge"), new NumericLiteralImpl(16, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://hasAge"), new NumericLiteralImpl(12, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+        triples.add( new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://hasAge"), new NumericLiteralImpl(43, XMLSchema.INTEGER)) );
+        triples.add( new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")) );
+
+        for(Statement triple : triples) {
+            ryaConn.add(triple);
+        }
+
+        // Create a PCJ table that will include those triples in its results.
+        final String sparql =
+                "SELECT ?name ?age " +
+                "{" +
+                  "FILTER(?age < 30) ." +
+                  "?name <http://hasAge> ?age." +
+                  "?name <http://playsSport> \"Soccer\" " +
+                "}";
+
+        final String pcjTableName = new PcjTableNameFactory().makeTableName(RYA_TABLE_PREFIX, "testPcj");
+
+        // Create and populate the PCJ table.
+        PcjTables pcjs = new PcjTables();
+        pcjs.createAndPopulatePcj(ryaConn, accumuloConn, pcjTableName, sparql, new String[]{"name", "age"}, Optional.<PcjVarOrderFactory>absent());
+
+        // Make sure the cardinality was updated.
+        PcjMetadata metadata = pcjs.getPcjMetadata(accumuloConn, pcjTableName);
+        assertEquals(3, metadata.getCardinality());
+
+        // Scan Accumulo for the stored results.
+        Multimap<String, BindingSet> fetchedResults = loadPcjResults(accumuloConn, pcjTableName);
+
+        // Ensure the expected results match those that were stored.
+        MapBindingSet alice = new MapBindingSet();
+        alice.addBinding("name", new URIImpl("http://Alice"));
+        alice.addBinding("age", new NumericLiteralImpl(14, XMLSchema.INTEGER));
+
+        MapBindingSet bob = new MapBindingSet();
+        bob.addBinding("name", new URIImpl("http://Bob"));
+        bob.addBinding("age", new NumericLiteralImpl(16, XMLSchema.INTEGER));
+
+        MapBindingSet charlie = new MapBindingSet();
+        charlie.addBinding("name", new URIImpl("http://Charlie"));
+        charlie.addBinding("age", new NumericLiteralImpl(12, XMLSchema.INTEGER));
+
+        Set<BindingSet> results = Sets.<BindingSet>newHashSet(alice, bob, charlie);
+
+        Multimap<String, BindingSet> expectedResults = HashMultimap.create();
+        expectedResults.putAll("name;age", results);
+        expectedResults.putAll("age;name", results);
+
+        assertEquals(expectedResults, fetchedResults);
+    }
+
+    /**
+     * Scan accumulo for the results that are stored in a PCJ table. The
+     * multimap stores a set of deserialized binding sets that were in the PCJ
+     * table for every variable order that is found in the PCJ metadata.
+     */
+    private static Multimap<String, BindingSet> loadPcjResults(Connector accumuloConn, String pcjTableName) throws PcjException, TableNotFoundException, RyaTypeResolverException {
+        Multimap<String, BindingSet> fetchedResults = HashMultimap.create();
+
+        // Get the variable orders the data was written to.
+        PcjTables pcjs = new PcjTables();
+        PcjMetadata pcjMetadata = pcjs.getPcjMetadata(accumuloConn, pcjTableName);
+
+        // Scan Accumulo for the stored results.
+        for(VariableOrder varOrder : pcjMetadata.getVarOrders()) {
+            Scanner scanner = accumuloConn.createScanner(pcjTableName, new Authorizations());
+            scanner.fetchColumnFamily( new Text(varOrder.toString()) );
+
+            for(Entry<Key, Value> entry : scanner) {
+                byte[] serializedResult = entry.getKey().getRow().getBytes();
+                BindingSet result = AccumuloPcjSerializer.deSerialize(serializedResult, varOrder.toArray());
+                fetchedResults.put(varOrder.toString(), result);
+            }
+        }
+
+        return fetchedResults;
+    }
+
+    @After
+    public void shutdownMiniResources() {
+        if(ryaConn != null) {
+            try {
+                log.info("Shutting down Rya Connection.");
+                ryaConn.close();
+                log.info("Rya Connection shut down.");
+            } catch(final Exception e) {
+                log.error("Could not shut down the Rya Connection.", e);
+            }
+        }
+
+        if(ryaRepo != null) {
+            try {
+                log.info("Shutting down Rya Repo.");
+                ryaRepo.shutDown();
+                log.info("Rya Repo shut down.");
+            } catch(final Exception e) {
+                log.error("Could not shut down the Rya Repo.", e);
+            }
+        }
+
+        if(accumulo != null) {
+            try {
+                log.info("Shutting down the Mini Accumulo being used as a Rya store.");
+                accumulo.stop();
+                log.info("Mini Accumulo being used as a Rya store shut down.");
+            } catch(final Exception e) {
+                log.error("Could not shut down the Mini Accumulo.", e);
+            }
+        }
+    }
+
+    /**
+     * Setup a Mini Accumulo cluster that uses a temporary directory to store its data.
+     *
+     * @return A Mini Accumulo cluster.
+     */
+    private static MiniAccumuloCluster startMiniAccumulo() throws IOException, InterruptedException, AccumuloException, AccumuloSecurityException {
+        final File miniDataDir = Files.createTempDir();
+
+        // Setup and start the Mini Accumulo.
+        final MiniAccumuloCluster accumulo = new MiniAccumuloCluster(miniDataDir, "password");
+        accumulo.start();
+
+        // Store a connector to the Mini Accumulo.
+        final Instance instance = new ZooKeeperInstance(accumulo.getInstanceName(), accumulo.getZooKeepers());
+        accumuloConn = instance.getConnector("root", new PasswordToken("password"));
+
+        return accumulo;
+    }
+
+    /**
+     * Format a Mini Accumulo to be a Rya repository.
+     *
+     * @param accumulo - The Mini Accumulo cluster Rya will sit on top of. (not null)
+     * @return The Rya repository sitting on top of the Mini Accumulo.
+     */
+    private static RyaSailRepository setupRya(final MiniAccumuloCluster accumulo) throws AccumuloException, AccumuloSecurityException, RepositoryException {
+        checkNotNull(accumulo);
+
+        // Setup the Rya Repository that will be used to create Repository Connections.
+        final RdfCloudTripleStore ryaStore = new RdfCloudTripleStore();
+        final AccumuloRyaDAO crdfdao = new AccumuloRyaDAO();
+        crdfdao.setConnector(accumuloConn);
+
+        // Setup Rya configuration values.
+        final AccumuloRdfConfiguration conf = new AccumuloRdfConfiguration();
+        conf.setTablePrefix("demo_");
+        conf.setDisplayQueryPlan(true);
+
+        conf.setBoolean(ConfigUtils.USE_MOCK_INSTANCE, true);
+        conf.set(RdfCloudTripleStoreConfiguration.CONF_TBL_PREFIX, RYA_TABLE_PREFIX);
+        conf.set(ConfigUtils.CLOUDBASE_USER, "root");
+        conf.set(ConfigUtils.CLOUDBASE_PASSWORD, "password");
+        conf.set(ConfigUtils.CLOUDBASE_INSTANCE, accumulo.getInstanceName());
+
+        crdfdao.setConf(conf);
+        ryaStore.setRyaDAO(crdfdao);
+
+        final RyaSailRepository ryaRepo = new RyaSailRepository(ryaStore);
+        ryaRepo.initialize();
+
+        return ryaRepo;
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/PcjTablesTests.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/PcjTablesTests.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/PcjTablesTests.java
new file mode 100644
index 0000000..0a8ebc8
--- /dev/null
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/PcjTablesTests.java
@@ -0,0 +1,65 @@
+package mvm.rya.indexing.external.tupleSet;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Set;
+
+import org.junit.Test;
+
+import com.google.common.collect.Sets;
+
+import mvm.rya.indexing.external.tupleSet.PcjTables.PcjMetadata;
+import mvm.rya.indexing.external.tupleSet.PcjTables.ShiftVarOrderFactory;
+import mvm.rya.indexing.external.tupleSet.PcjTables.VariableOrder;
+
+/**
+ * Tests the classes and methods of {@link PcjTables}.
+ */
+public class PcjTablesTests {
+
+    @Test
+    public void variableOrder_hashCode() {
+        assertEquals(new VariableOrder("a", "b", "C").hashCode(), new VariableOrder("a", "b", "C").hashCode());
+    }
+
+    @Test
+    public void variableOrder_equals() {
+        assertEquals(new VariableOrder("a", "b", "C"), new VariableOrder("a", "b", "C"));
+    }
+
+    @Test
+    public void variableOrder_fromString() {
+        assertEquals(new VariableOrder("a", "b", "c"), new VariableOrder("a;b;c"));
+    }
+
+    @Test
+    public void variableORder_toString() {
+        assertEquals("a;b;c", new VariableOrder("a", "b", "c").toString());
+    }
+
+    @Test
+    public void pcjMetadata_hashCode() {
+        PcjMetadata meta1 = new PcjMetadata("A SPARQL string.", 5, Sets.newHashSet(new VariableOrder("a", "b", "c"), new VariableOrder("d", "e", "f")));
+        PcjMetadata meta2 = new PcjMetadata("A SPARQL string.", 5, Sets.newHashSet(new VariableOrder("a", "b", "c"), new VariableOrder("d", "e", "f")));
+        assertEquals(meta1.hashCode(), meta2.hashCode());
+    }
+
+    @Test
+    public void pcjMetadata_equals() {
+        PcjMetadata meta1 = new PcjMetadata("A SPARQL string.", 5, Sets.newHashSet(new VariableOrder("a", "b", "c"), new VariableOrder("d", "e", "f")));
+        PcjMetadata meta2 = new PcjMetadata("A SPARQL string.", 5, Sets.newHashSet(new VariableOrder("a", "b", "c"), new VariableOrder("d", "e", "f")));
+        assertEquals(meta1, meta2);
+    }
+
+    @Test
+    public void shiftVarOrdersFactory() {
+        Set<VariableOrder> expected = Sets.newHashSet(
+                new VariableOrder("a;b;c"),
+                new VariableOrder("b;c;a"),
+                new VariableOrder("c;a;b"));
+
+        Set<VariableOrder> varOrders = new ShiftVarOrderFactory().makeVarOrders(new VariableOrder("a;b;c"));
+        assertEquals(expected, varOrders);
+    }
+
+}
\ No newline at end of file


[05/16] incubator-rya git commit: RYA-32 Improve how metadata and values are written to Accumulo PCJ tables

Posted by mi...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/external/PcjIntegrationTestingUtil.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/PcjIntegrationTestingUtil.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/PcjIntegrationTestingUtil.java
new file mode 100644
index 0000000..d76695c
--- /dev/null
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/external/PcjIntegrationTestingUtil.java
@@ -0,0 +1,385 @@
+package mvm.rya.indexing.external;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import mvm.rya.accumulo.AccumuloRdfConfiguration;
+import mvm.rya.api.persist.RyaDAOException;
+import mvm.rya.api.resolver.RyaTypeResolverException;
+import mvm.rya.indexing.RyaSailFactory;
+import mvm.rya.indexing.accumulo.ConfigUtils;
+import mvm.rya.indexing.external.tupleSet.AccumuloPcjSerializer;
+import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
+import mvm.rya.indexing.external.tupleSet.PcjTables;
+import mvm.rya.indexing.external.tupleSet.PcjTables.PcjException;
+import mvm.rya.indexing.external.tupleSet.PcjTables.PcjMetadata;
+import mvm.rya.indexing.external.tupleSet.PcjTables.PcjVarOrderFactory;
+import mvm.rya.indexing.external.tupleSet.PcjTables.ShiftVarOrderFactory;
+import mvm.rya.indexing.external.tupleSet.PcjTables.VariableOrder;
+
+import org.apache.accumulo.core.client.AccumuloException;
+import org.apache.accumulo.core.client.AccumuloSecurityException;
+import org.apache.accumulo.core.client.BatchWriter;
+import org.apache.accumulo.core.client.BatchWriterConfig;
+import org.apache.accumulo.core.client.Connector;
+import org.apache.accumulo.core.client.MutationsRejectedException;
+import org.apache.accumulo.core.client.TableNotFoundException;
+import org.apache.accumulo.core.client.admin.TableOperations;
+import org.apache.accumulo.core.data.Mutation;
+import org.openrdf.query.BindingSet;
+import org.openrdf.query.MalformedQueryException;
+import org.openrdf.query.QueryEvaluationException;
+import org.openrdf.query.QueryLanguage;
+import org.openrdf.query.TupleQuery;
+import org.openrdf.query.TupleQueryResult;
+import org.openrdf.query.algebra.BindingSetAssignment;
+import org.openrdf.query.algebra.QueryModelNode;
+import org.openrdf.query.algebra.TupleExpr;
+import org.openrdf.query.algebra.helpers.QueryModelVisitorBase;
+import org.openrdf.repository.RepositoryConnection;
+import org.openrdf.repository.RepositoryException;
+import org.openrdf.repository.sail.SailRepository;
+import org.openrdf.repository.sail.SailRepositoryConnection;
+import org.openrdf.sail.Sail;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.Sets;
+
+public class PcjIntegrationTestingUtil {
+
+	public static Set<QueryModelNode> getTupleSets(TupleExpr te) {
+		final ExternalTupleVisitor etv = new ExternalTupleVisitor();
+		te.visit(etv);
+		return etv.getExtTup();
+	}
+
+	public static void deleteCoreRyaTables(Connector accCon, String prefix)
+			throws AccumuloException, AccumuloSecurityException,
+			TableNotFoundException {
+		final TableOperations ops = accCon.tableOperations();
+		if (ops.exists(prefix + "spo")) {
+			ops.delete(prefix + "spo");
+		}
+		if (ops.exists(prefix + "po")) {
+			ops.delete(prefix + "po");
+		}
+		if (ops.exists(prefix + "osp")) {
+			ops.delete(prefix + "osp");
+		}
+	}
+
+	public static SailRepository getPcjRepo(String tablePrefix, String instance)
+			throws AccumuloException, AccumuloSecurityException,
+			RyaDAOException, RepositoryException {
+
+		final AccumuloRdfConfiguration pcjConf = new AccumuloRdfConfiguration();
+		pcjConf.set(ConfigUtils.USE_PCJ, "true");
+		pcjConf.set(ConfigUtils.USE_MOCK_INSTANCE, "true");
+		pcjConf.set(ConfigUtils.CLOUDBASE_INSTANCE, instance);
+		pcjConf.setTablePrefix(tablePrefix);
+
+		final Sail pcjSail = RyaSailFactory.getInstance(pcjConf);
+		final SailRepository pcjRepo = new SailRepository(pcjSail);
+		pcjRepo.initialize();
+		return pcjRepo;
+	}
+
+	public static SailRepository getNonPcjRepo(String tablePrefix,
+			String instance) throws AccumuloException,
+			AccumuloSecurityException, RyaDAOException, RepositoryException {
+
+		final AccumuloRdfConfiguration nonPcjConf = new AccumuloRdfConfiguration();
+		nonPcjConf.set(ConfigUtils.USE_MOCK_INSTANCE, "true");
+		nonPcjConf.set(ConfigUtils.CLOUDBASE_INSTANCE, instance);
+		nonPcjConf.setTablePrefix(tablePrefix);
+
+		final Sail nonPcjSail = RyaSailFactory.getInstance(nonPcjConf);
+		final SailRepository nonPcjRepo = new SailRepository(nonPcjSail);
+		nonPcjRepo.initialize();
+		return nonPcjRepo;
+	}
+
+	public static void closeAndShutdown(SailRepositoryConnection connection,
+			SailRepository repo) throws RepositoryException {
+		connection.close();
+		repo.shutDown();
+	}
+
+	public static void deleteIndexTables(Connector accCon, int tableNum,
+			String prefix) throws AccumuloException, AccumuloSecurityException,
+			TableNotFoundException {
+		final TableOperations ops = accCon.tableOperations();
+		final String tablename = prefix + "INDEX_";
+		for (int i = 1; i < tableNum + 1; i++) {
+			if (ops.exists(tablename + i)) {
+				ops.delete(tablename + i);
+			}
+		}
+	}
+
+	public static class BindingSetAssignmentCollector extends
+			QueryModelVisitorBase<RuntimeException> {
+
+		private final Set<QueryModelNode> bindingSetList = Sets.newHashSet();
+
+		public Set<QueryModelNode> getBindingSetAssignments() {
+			return bindingSetList;
+		}
+
+		public boolean containsBSAs() {
+			return bindingSetList.size() > 0;
+		}
+
+		@Override
+		public void meet(BindingSetAssignment node) {
+			bindingSetList.add(node);
+			super.meet(node);
+		}
+
+	}
+
+	public static class ExternalTupleVisitor extends
+			QueryModelVisitorBase<RuntimeException> {
+
+		private final Set<QueryModelNode> eSet = new HashSet<>();
+
+		@Override
+		public void meetNode(QueryModelNode node) throws RuntimeException {
+			if (node instanceof ExternalTupleSet) {
+				eSet.add(node);
+			}
+			super.meetNode(node);
+		}
+
+		public Set<QueryModelNode> getExtTup() {
+			return eSet;
+		}
+
+	}
+
+
+
+
+
+
+//****************************Creation and Population of PcjTables***********************************
+
+
+
+
+
+	  /**
+     * Creates a new PCJ Table in Accumulo and populates it by scanning an
+     * instance of Rya for historic matches.
+     * <p>
+     * If any portion of this operation fails along the way, the partially
+     * create PCJ table will be left in Accumulo.
+     *
+     * @param ryaConn - Connects to the Rya that will be scanned. (not null)
+     * @param accumuloConn - Connects to the accumulo that hosts the PCJ results. (not null)
+     * @param pcjTableName - The name of the PCJ table that will be created. (not null)
+     * @param sparql - The SPARQL query whose results will be loaded into the table. (not null)
+     * @param resultVariables - The variables that are included in the query's resulting binding sets. (not null)
+     * @param pcjVarOrderFactory - An optional factory that indicates the various variable orders
+     *   the results will be stored in. If one is not provided, then {@link ShiftVarOrderFactory}
+     *   is used by default. (not null)
+     * @throws PcjException The PCJ table could not be create or the values from
+     *   Rya were not able to be loaded into it.
+     */
+    public static void createAndPopulatePcj(
+            final RepositoryConnection ryaConn,
+            final Connector accumuloConn,
+            final String pcjTableName,
+            final String sparql,
+            final String[] resultVariables,
+            final Optional<PcjVarOrderFactory> pcjVarOrderFactory) throws PcjException {
+        checkNotNull(ryaConn);
+        checkNotNull(accumuloConn);
+        checkNotNull(pcjTableName);
+        checkNotNull(sparql);
+        checkNotNull(resultVariables);
+        checkNotNull(pcjVarOrderFactory);
+
+        PcjTables pcj = new PcjTables();
+        // Create the PCJ's variable orders.
+        PcjVarOrderFactory varOrderFactory = pcjVarOrderFactory.or(new ShiftVarOrderFactory());
+        Set<VariableOrder> varOrders = varOrderFactory.makeVarOrders( new VariableOrder(resultVariables) );
+
+        // Create the PCJ table in Accumulo.
+        pcj.createPcjTable(accumuloConn, pcjTableName, varOrders, sparql);
+
+        // Load historic matches from Rya into the PCJ table.
+        populatePcj(accumuloConn, pcjTableName, ryaConn);
+    }
+
+
+	/**
+	 * Scan Rya for results that solve the PCJ's query and store them in the PCJ
+	 * table.
+	 * <p>
+	 * This method assumes the PCJ table has already been created.
+	 *
+	 * @param accumuloConn
+	 *            - A connection to the Accumulo that hosts the PCJ table. (not
+	 *            null)
+	 * @param pcjTableName
+	 *            - The name of the PCJ table that will receive the results.
+	 *            (not null)
+	 * @param ryaConn
+	 *            - A connection to the Rya store that will be queried to find
+	 *            results. (not null)
+	 * @throws PcjException
+	 *             If results could not be written to the PCJ table, the PCJ
+	 *             table does not exist, or the query that is being execute was
+	 *             malformed.
+	 */
+	public static void populatePcj(final Connector accumuloConn,
+			final String pcjTableName, final RepositoryConnection ryaConn)
+			throws PcjException {
+		checkNotNull(accumuloConn);
+		checkNotNull(pcjTableName);
+		checkNotNull(ryaConn);
+
+		try {
+			// Fetch the query that needs to be executed from the PCJ table.
+			PcjMetadata pcjMetadata = new PcjTables().getPcjMetadata(
+					accumuloConn, pcjTableName);
+			String sparql = pcjMetadata.getSparql();
+
+			// Query Rya for results to the SPARQL query.
+			TupleQuery query = ryaConn.prepareTupleQuery(QueryLanguage.SPARQL,
+					sparql);
+			TupleQueryResult results = query.evaluate();
+
+			// Load batches of 1000 of them at a time into the PCJ table
+			Set<BindingSet> batch = new HashSet<>(1000);
+			while (results.hasNext()) {
+				batch.add(results.next());
+
+				if (batch.size() == 1000) {
+					addResults(accumuloConn, pcjTableName, batch);
+					batch.clear();
+				}
+			}
+
+			if (!batch.isEmpty()) {
+				addResults(accumuloConn, pcjTableName, batch);
+			}
+
+		} catch (RepositoryException | MalformedQueryException
+				| QueryEvaluationException e) {
+			throw new PcjException(
+					"Could not populate a PCJ table with Rya results for the table named: "
+							+ pcjTableName, e);
+		}
+	}
+
+	public static void addResults(final Connector accumuloConn,
+			final String pcjTableName, final Collection<BindingSet> results)
+			throws PcjException {
+		checkNotNull(accumuloConn);
+		checkNotNull(pcjTableName);
+		checkNotNull(results);
+
+		// Write a result to each of the variable orders that are in the table.
+		writeResults(accumuloConn, pcjTableName, results);
+	}
+
+	/**
+	 * Add a collection of results to a specific PCJ table.
+	 *
+	 * @param accumuloConn
+	 *            - A connection to the Accumulo that hosts the PCJ table. (not
+	 *            null)
+	 * @param pcjTableName
+	 *            - The name of the PCJ table that will receive the results.
+	 *            (not null)
+	 * @param results
+	 *            - Binding sets that will be written to the PCJ table. (not
+	 *            null)
+	 * @throws PcjException
+	 *             The provided PCJ table doesn't exist, is missing the PCJ
+	 *             metadata, or the result could not be written to it.
+	 */
+	private static void writeResults(final Connector accumuloConn,
+			final String pcjTableName, final Collection<BindingSet> results)
+			throws PcjException {
+		checkNotNull(accumuloConn);
+		checkNotNull(pcjTableName);
+		checkNotNull(results);
+
+		// Fetch the variable orders from the PCJ table.
+		PcjMetadata metadata = new PcjTables().getPcjMetadata(accumuloConn,
+				pcjTableName);
+
+		// Write each result formatted using each of the variable orders.
+		BatchWriter writer = null;
+		try {
+			writer = accumuloConn.createBatchWriter(pcjTableName,
+					new BatchWriterConfig());
+			for (BindingSet result : results) {
+				Set<Mutation> addResultMutations = makeWriteResultMutations(
+						metadata.getVarOrders(), result);
+				writer.addMutations(addResultMutations);
+			}
+		} catch (TableNotFoundException | MutationsRejectedException e) {
+			throw new PcjException(
+					"Could not add results to the PCJ table named: "
+							+ pcjTableName, e);
+		} finally {
+			if (writer != null) {
+				try {
+					writer.close();
+				} catch (MutationsRejectedException e) {
+					throw new PcjException(
+							"Could not add results to a PCJ table because some of the mutations were rejected.",
+							e);
+				}
+			}
+		}
+	}
+
+	/**
+	 * Create the {@link Mutations} required to write a new {@link BindingSet}
+	 * to a PCJ table for each {@link VariableOrder} that is provided.
+	 *
+	 * @param varOrders
+	 *            - The variables orders the result will be written to. (not
+	 *            null)
+	 * @param result
+	 *            - A new PCJ result. (not null)
+	 * @return Mutation that will write the result to a PCJ table.
+	 * @throws PcjException
+	 *             The binding set could not be encoded.
+	 */
+	private static Set<Mutation> makeWriteResultMutations(
+			final Set<VariableOrder> varOrders, final BindingSet result)
+			throws PcjException {
+		checkNotNull(varOrders);
+		checkNotNull(result);
+
+		Set<Mutation> mutations = new HashSet<>();
+
+		for (final VariableOrder varOrder : varOrders) {
+			try {
+				// Serialize the result to the variable order.
+				byte[] serializedResult = AccumuloPcjSerializer.serialize(
+						result, varOrder.toArray());
+
+				// Row ID = binding set values, Column Family = variable order
+				// of the binding set.
+				Mutation addResult = new Mutation(serializedResult);
+				addResult.put(varOrder.toString(), "", "");
+				mutations.add(addResult);
+			} catch (RyaTypeResolverException e) {
+				throw new PcjException("Could not serialize a result.", e);
+			}
+		}
+
+		return mutations;
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerIntegrationTest.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerIntegrationTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerIntegrationTest.java
index f7f3cbf..4eaa279 100644
--- a/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerIntegrationTest.java
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerIntegrationTest.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.external;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -19,31 +19,21 @@ package mvm.rya.indexing.external;
  * under the License.
  */
 
-
 import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
 
-import junit.framework.Assert;
-import mvm.rya.accumulo.AccumuloRdfConfiguration;
 import mvm.rya.api.persist.RyaDAOException;
-import mvm.rya.indexing.RyaSailFactory;
-import mvm.rya.indexing.accumulo.ConfigUtils;
-import mvm.rya.indexing.external.tupleSet.AccumuloIndexSet;
+import mvm.rya.indexing.external.tupleSet.PcjTables.PcjException;
+import mvm.rya.indexing.external.tupleSet.PcjTables.PcjVarOrderFactory;
 
 import org.apache.accumulo.core.client.AccumuloException;
 import org.apache.accumulo.core.client.AccumuloSecurityException;
 import org.apache.accumulo.core.client.Connector;
-import org.apache.accumulo.core.client.Scanner;
 import org.apache.accumulo.core.client.TableExistsException;
 import org.apache.accumulo.core.client.TableNotFoundException;
 import org.apache.accumulo.core.client.mock.MockInstance;
 import org.apache.accumulo.core.client.security.tokens.PasswordToken;
-import org.apache.accumulo.core.data.Key;
-import org.apache.accumulo.core.data.Value;
-import org.apache.accumulo.core.security.Authorizations;
-import org.apache.hadoop.conf.Configuration;
 import org.junit.After;
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.openrdf.model.URI;
@@ -61,490 +51,445 @@ import org.openrdf.query.TupleQueryResultHandlerException;
 import org.openrdf.repository.RepositoryException;
 import org.openrdf.repository.sail.SailRepository;
 import org.openrdf.repository.sail.SailRepositoryConnection;
-import org.openrdf.sail.Sail;
 import org.openrdf.sail.SailException;
-import org.openrdf.sail.memory.MemoryStore;
+
+import com.google.common.base.Optional;
 
 public class PrecompJoinOptimizerIntegrationTest {
 
-    private SailRepositoryConnection conn;
-    private SailRepository repo;
-    private Connector accCon;
-    String tablePrefix = "table_";
-    AccumuloRdfConfiguration conf;
-    URI sub, sub2, obj,obj2,subclass, subclass2, talksTo;
-   
-    
-    
-    
-    @Before
-    public void init() throws RepositoryException, TupleQueryResultHandlerException, QueryEvaluationException, MalformedQueryException, 
-    AccumuloException, AccumuloSecurityException, TableExistsException, RyaDAOException {
-
-        conf = new AccumuloRdfConfiguration();
-        conf.set(ConfigUtils.USE_PCJ, "true");
-        conf.set(ConfigUtils.USE_MOCK_INSTANCE,"true");
-        conf.set(ConfigUtils.CLOUDBASE_INSTANCE, "instance");
-        conf.setTablePrefix(tablePrefix);
-        
-        Sail sail = RyaSailFactory.getInstance(conf);
-        repo = new SailRepository(sail);
-        repo.initialize();
-        conn = repo.getConnection();
-
-        sub = new URIImpl("uri:entity");
-        subclass = new URIImpl("uri:class");
-        obj = new URIImpl("uri:obj");
-        talksTo = new URIImpl("uri:talksTo");
-
-        conn.add(sub, RDF.TYPE, subclass);
-        conn.add(sub, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(sub, talksTo, obj);
-
-        sub2 = new URIImpl("uri:entity2");
-        subclass2 = new URIImpl("uri:class2");
-        obj2 = new URIImpl("uri:obj2");
-
-        conn.add(sub2, RDF.TYPE, subclass2);
-        conn.add(sub2, RDFS.LABEL, new LiteralImpl("label2"));
-        conn.add(sub2, talksTo, obj2);
-
-        accCon = new MockInstance("instance").getConnector("root",new PasswordToken("".getBytes()));
-
-    }
-    
-    
-   @After
-   public void close() throws RepositoryException, AccumuloException, AccumuloSecurityException, TableNotFoundException {
-       
-       conf = null;
-       conn.close();
-       accCon.tableOperations().delete(tablePrefix + "spo");
-       accCon.tableOperations().delete(tablePrefix + "po");
-       accCon.tableOperations().delete(tablePrefix + "osp");
-   }
-    
-    
-    
-    @Test
-    public void testEvaluateSingeIndex() throws TupleQueryResultHandlerException, QueryEvaluationException,
-    MalformedQueryException, RepositoryException, AccumuloException, 
-    AccumuloSecurityException, TableExistsException, RyaDAOException, SailException, TableNotFoundException {
-
-        if (accCon.tableOperations().exists(tablePrefix + "INDEX1")) {
-            accCon.tableOperations().delete(tablePrefix + "INDEX1");
-        }
-        accCon.tableOperations().create(tablePrefix + "INDEX1");
-        
-        String indexSparqlString = ""//
-                + "SELECT ?e ?l ?c " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-      
-        AccumuloIndexSet ais = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablePrefix + "INDEX1");
-         
-       
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "}";//
-
-        CountingResultHandler crh = new CountingResultHandler();       
-        conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh);
-        
-//        Scanner scan = accCon.createScanner(tablePrefix + "spo", new Authorizations("U"));
-//        
-//        for(Entry<Key,Value> e: scan) {
-//            System.out.println(e.getKey().getRow());
-//        }
-        
-        Assert.assertEquals(2, crh.getCount());
-        
-         
-    }
-    
-    
-    
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexTwoVarOrder1() throws AccumuloException, AccumuloSecurityException, 
-    TableExistsException, RepositoryException, MalformedQueryException, SailException, QueryEvaluationException, 
-    TableNotFoundException, TupleQueryResultHandlerException, RyaDAOException {
-        
-        if (accCon.tableOperations().exists(tablePrefix + "INDEX1")) {
-            accCon.tableOperations().delete(tablePrefix + "INDEX1");
-        }
-
-        if (accCon.tableOperations().exists(tablePrefix + "INDEX2")) {
-            accCon.tableOperations().delete(tablePrefix + "INDEX2");
-        }
-
-        accCon.tableOperations().create(tablePrefix + "INDEX1");
-        accCon.tableOperations().create(tablePrefix + "INDEX2");
-        
-        
-        
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-
- 
-        String indexSparqlString = ""//
-                + "SELECT ?e ?l ?c " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        String indexSparqlString2 = ""//
-                + "SELECT ?e ?o ?l " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablePrefix + "INDEX1");
-        AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, tablePrefix + "INDEX2");
-
-        CountingResultHandler crh = new CountingResultHandler();
-        conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh);
-
-        Assert.assertEquals(2, crh.getCount());
-     
-
-        
-        
-    }
-    
-    
-    @Test
-    public void testEvaluateSingeFilterIndex() throws TupleQueryResultHandlerException, QueryEvaluationException,
-    MalformedQueryException, RepositoryException, AccumuloException, 
-    AccumuloSecurityException, TableExistsException, RyaDAOException, SailException, TableNotFoundException {
-
-        if (accCon.tableOperations().exists(tablePrefix + "INDEX1")) {
-            accCon.tableOperations().delete(tablePrefix + "INDEX1");
-        }
-        accCon.tableOperations().create(tablePrefix + "INDEX1");
-        
-        String indexSparqlString = ""//
-                + "SELECT ?e ?l ?c " //
-                + "{" //
-                + "  Filter(?e = <uri:entity>) " //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-      
-        AccumuloIndexSet ais = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablePrefix + "INDEX1");
-         
-       
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o " //
-                + "{" //
-                + "   Filter(?e = <uri:entity>) " //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "}";//
-
-        CountingResultHandler crh = new CountingResultHandler();       
-        conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh);
-        
-        Assert.assertEquals(1, crh.getCount());
-        
-         
-    }
-    
-    
-    
-    
-    @Test
-    public void testEvaluateSingeFilterWithUnion() throws TupleQueryResultHandlerException, QueryEvaluationException,
-    MalformedQueryException, RepositoryException, AccumuloException, 
-    AccumuloSecurityException, TableExistsException, RyaDAOException, SailException, TableNotFoundException {
-
-        if (accCon.tableOperations().exists(tablePrefix + "INDEX2")) {
-            accCon.tableOperations().delete(tablePrefix + "INDEX2");
-        }
-        accCon.tableOperations().create(tablePrefix + "INDEX2");
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?e ?l ?c " //
-                + "{" //
-                + "  Filter(?l = \"label2\") " //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-      
-        AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, tablePrefix + "INDEX2");
-         
-       
-        String queryString = ""//
-                + "SELECT ?e ?c ?o ?m ?l" //
-                + "{" //
-                + "   Filter(?l = \"label2\") " //
-                + "  ?e <uri:talksTo> ?o . "//
-                + " { ?e a ?c .  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?m  }"//
-                + " UNION { ?e a ?c .  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l  }"//
-                + "}";//
-
-        CountingResultHandler crh = new CountingResultHandler();       
-        conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh);
-        
-        Assert.assertEquals(1, crh.getCount());
-        
-         
-    }
-    
-    
-    
-    @Test
-    public void testEvaluateSingeFilterWithLeftJoin() throws TupleQueryResultHandlerException, QueryEvaluationException,
-    MalformedQueryException, RepositoryException, AccumuloException, 
-    AccumuloSecurityException, TableExistsException, RyaDAOException, SailException, TableNotFoundException {
-
-        if (accCon.tableOperations().exists(tablePrefix + "INDEX1")) {
-            accCon.tableOperations().delete(tablePrefix + "INDEX1");
-        }
-        accCon.tableOperations().create(tablePrefix + "INDEX1");
-        
-        String indexSparqlString1 = ""//
-                + "SELECT ?e ?l ?c " //
-                + "{" //
-                + "  Filter(?l = \"label3\") " //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-   
-        URI sub3 = new URIImpl("uri:entity3");
-        URI subclass3 = new URIImpl("uri:class3");
-        conn.add(sub3, RDF.TYPE, subclass3);
-        conn.add(sub3,RDFS.LABEL, new LiteralImpl("label3"));
-        AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString1, conn, accCon, tablePrefix + "INDEX1");
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?o ?m ?l" //
-                + "{" //
-                + "  Filter(?l = \"label3\") " //
-                + "  ?e a ?c . " //  
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . " //
-                + "  OPTIONAL { ?e <uri:talksTo> ?o . ?e <http://www.w3.org/2000/01/rdf-schema#label> ?m }"//
-                + "}";//
-
-        CountingResultHandler crh = new CountingResultHandler();       
-        conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh);
-        
-        Assert.assertEquals(1, crh.getCount());
-        
-         
-    }
-    
-    
-    
-    
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexUnionFilter() throws AccumuloException, AccumuloSecurityException, 
-    TableExistsException, RepositoryException, MalformedQueryException, SailException, QueryEvaluationException, 
-    TableNotFoundException, TupleQueryResultHandlerException, RyaDAOException {
-        
-        if (accCon.tableOperations().exists(tablePrefix + "INDEX1")) {
-            accCon.tableOperations().delete(tablePrefix + "INDEX1");
-        }
-
-        if (accCon.tableOperations().exists(tablePrefix + "INDEX2")) {
-            accCon.tableOperations().delete(tablePrefix + "INDEX2");
-        }
-
-        accCon.tableOperations().create(tablePrefix + "INDEX1");
-        accCon.tableOperations().create(tablePrefix + "INDEX2");
-           
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        conn.add(sub, RDF.TYPE, obj);
-        conn.add(sub2, RDF.TYPE, obj2);
-     
-     
-        String indexSparqlString = ""//
-                + "SELECT ?e ?l ?o " //
-                + "{" //
-                + "   Filter(?l = \"label2\") " //
-                + "  ?e a ?o . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        String indexSparqlString2 = ""//
-                + "SELECT ?e ?l ?o " //
-                + "{" //
-                + "   Filter(?l = \"label2\") " //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        String queryString = ""//
-                + "SELECT ?c ?e ?l ?o " //
-                + "{" //
-                + "   Filter(?l = \"label2\") " //
-                + "  ?e a ?c . "//
-                + " { ?e a ?o .  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l  }"//
-                + " UNION { ?e <uri:talksTo> ?o .  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l  }"//
-                + "}";//
-
-        AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablePrefix + "INDEX1");
-        AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, tablePrefix + "INDEX2");
-
-        CountingResultHandler crh = new CountingResultHandler();
-        conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh);
-          
-        Assert.assertEquals(6, crh.getCount());
-     
-
-     
-    }
-    
-    
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexLeftJoinUnionFilter() throws AccumuloException, AccumuloSecurityException, 
-    TableExistsException, RepositoryException, MalformedQueryException, SailException, QueryEvaluationException, 
-    TableNotFoundException, TupleQueryResultHandlerException, RyaDAOException {
-        
-        if (accCon.tableOperations().exists(tablePrefix + "INDEX1")) {
-            accCon.tableOperations().delete(tablePrefix + "INDEX1");
-        }
-
-        if (accCon.tableOperations().exists(tablePrefix + "INDEX2")) {
-            accCon.tableOperations().delete(tablePrefix + "INDEX2");
-        }
-
-        accCon.tableOperations().create(tablePrefix + "INDEX1");
-        accCon.tableOperations().create(tablePrefix + "INDEX2");
-           
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        conn.add(sub, RDF.TYPE, obj);
-        conn.add(sub2, RDF.TYPE, obj2);
-        
-        URI livesIn = new URIImpl("uri:livesIn");
-        URI city = new URIImpl("uri:city");
-        URI city2 = new URIImpl("uri:city2");
-        URI city3 = new URIImpl("uri:city3");
-        conn.add(sub,livesIn,city);
-        conn.add(sub2,livesIn,city2);
-        conn.add(sub2,livesIn,city3);
-        conn.add(sub,livesIn,city3);
-       
-     
-        String indexSparqlString = ""//
-                + "SELECT ?e ?l ?o " //
-                + "{" //
-                + "  ?e a ?o . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        String indexSparqlString2 = ""//
-                + "SELECT ?e ?l ?o " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        String queryString = ""//
-                + "SELECT ?c ?e ?l ?o " //
-                + "{" //
-                + " Filter(?c = <uri:city3>) " //
-                + " ?e <uri:livesIn> ?c . "//
-                + " OPTIONAL{{ ?e a ?o .  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l  }"//
-                + " UNION { ?e <uri:talksTo> ?o .  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l  }}"//
-                + "}";//
-
-        AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablePrefix + "INDEX1");
-        AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, tablePrefix + "INDEX2");
-
-        CountingResultHandler crh = new CountingResultHandler();
-        conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh);
-
-//        Scanner scan = accCon.createScanner(tablePrefix + "spo", new Authorizations("U"));
-//        
-//        for(Entry<Key,Value> e: scan) {
-//            System.out.println(e.getKey().getRow());
-//        }
-        
-        Assert.assertEquals(6, crh.getCount());
-     
-
-     
-    }
-    
-    
-    
-    
-    public static class CountingResultHandler implements TupleQueryResultHandler {
-        private int count = 0;
-
-        public int getCount() {
-            return count;
-        }
-
-        public void resetCount() {
-            this.count = 0;
-        }
-
-        @Override
-        public void startQueryResult(List<String> arg0) throws TupleQueryResultHandlerException {
-        }
-
-
-        @Override
-        public void handleSolution(BindingSet arg0) throws TupleQueryResultHandlerException {
-            System.out.println(arg0);
-            count++;
-            System.out.println("Count is " + count);
-        }
-
-        @Override
-        public void endQueryResult() throws TupleQueryResultHandlerException {
-        }
-
-        @Override
-        public void handleBoolean(boolean arg0) throws QueryResultHandlerException {
-            // TODO Auto-generated method stub
-
-        }
-
-        @Override
-        public void handleLinks(List<String> arg0) throws QueryResultHandlerException {
-            // TODO Auto-generated method stub
-
-        }
-    }
-    
-    
-    
-    
-    
+	private SailRepositoryConnection conn, pcjConn;
+	private SailRepository repo, pcjRepo;
+	private Connector accCon;
+	String tablePrefix = "table_";
+	URI sub, sub2, obj, obj2, subclass, subclass2, talksTo;
+
+	@Before
+	public void init() throws RepositoryException,
+			TupleQueryResultHandlerException, QueryEvaluationException,
+			MalformedQueryException, AccumuloException,
+			AccumuloSecurityException, TableExistsException, RyaDAOException,
+			TableNotFoundException {
+
+		repo = PcjIntegrationTestingUtil.getNonPcjRepo(tablePrefix, "instance");
+		conn = repo.getConnection();
+
+		pcjRepo = PcjIntegrationTestingUtil.getPcjRepo(tablePrefix, "instance");
+		pcjConn = pcjRepo.getConnection();
+
+		sub = new URIImpl("uri:entity");
+		subclass = new URIImpl("uri:class");
+		obj = new URIImpl("uri:obj");
+		talksTo = new URIImpl("uri:talksTo");
+
+		conn.add(sub, RDF.TYPE, subclass);
+		conn.add(sub, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(sub, talksTo, obj);
+
+		sub2 = new URIImpl("uri:entity2");
+		subclass2 = new URIImpl("uri:class2");
+		obj2 = new URIImpl("uri:obj2");
+
+		conn.add(sub2, RDF.TYPE, subclass2);
+		conn.add(sub2, RDFS.LABEL, new LiteralImpl("label2"));
+		conn.add(sub2, talksTo, obj2);
+
+		accCon = new MockInstance("instance").getConnector("root",
+				new PasswordToken(""));
+
+	}
+
+	@After
+	public void close() throws RepositoryException, AccumuloException,
+			AccumuloSecurityException, TableNotFoundException {
+
+		PcjIntegrationTestingUtil.closeAndShutdown(conn, repo);
+		PcjIntegrationTestingUtil.closeAndShutdown(pcjConn, pcjRepo);
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, tablePrefix);
+		PcjIntegrationTestingUtil.deleteIndexTables(accCon, 2, tablePrefix);
+
+	}
+
+	@Test
+	public void testEvaluateSingeIndex()
+			throws TupleQueryResultHandlerException, QueryEvaluationException,
+			MalformedQueryException, RepositoryException, AccumuloException,
+			AccumuloSecurityException, TableExistsException, RyaDAOException,
+			SailException, TableNotFoundException, PcjException {
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?e ?l ?c " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablePrefix
+				+ "INDEX_1", indexSparqlString, new String[] { "e", "l", "c" },
+				Optional.<PcjVarOrderFactory> absent());
+		final String queryString = ""//
+				+ "SELECT ?e ?c ?l ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "}";//
+
+		final CountingResultHandler crh = new CountingResultHandler();
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, tablePrefix);
+		PcjIntegrationTestingUtil.closeAndShutdown(conn, repo);
+		repo = PcjIntegrationTestingUtil.getPcjRepo(tablePrefix, "instance");
+		conn = repo.getConnection();
+		conn.add(sub, talksTo, obj);
+		conn.add(sub2, talksTo, obj2);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh);
+
+		Assert.assertEquals(2, crh.getCount());
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexTwoVarOrder1() throws AccumuloException,
+			AccumuloSecurityException, TableExistsException,
+			RepositoryException, MalformedQueryException, SailException,
+			QueryEvaluationException, TableNotFoundException,
+			TupleQueryResultHandlerException, RyaDAOException, PcjException {
+
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?e ?l ?c " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?e ?o ?l " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final String queryString = ""//
+				+ "SELECT ?e ?c ?l ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablePrefix
+				+ "INDEX_1", indexSparqlString, new String[] { "e", "l", "c" },
+				Optional.<PcjVarOrderFactory> absent());
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablePrefix
+				+ "INDEX_2", indexSparqlString2, new String[] { "e", "l", "o" },
+				Optional.<PcjVarOrderFactory> absent());
+		final CountingResultHandler crh = new CountingResultHandler();
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, tablePrefix);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(
+				crh);
+
+		Assert.assertEquals(2, crh.getCount());
+
+	}
+
+	@Test
+	public void testEvaluateSingeFilterIndex()
+			throws TupleQueryResultHandlerException, QueryEvaluationException,
+			MalformedQueryException, RepositoryException, AccumuloException,
+			AccumuloSecurityException, TableExistsException, RyaDAOException,
+			SailException, TableNotFoundException, PcjException {
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?e ?l ?c " //
+				+ "{" //
+				+ "  Filter(?e = <uri:entity>) " //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablePrefix
+				+ "INDEX_1", indexSparqlString, new String[] { "e", "l", "c" },
+				Optional.<PcjVarOrderFactory> absent());
+		final String queryString = ""//
+				+ "SELECT ?e ?c ?l ?o " //
+				+ "{" //
+				+ "   Filter(?e = <uri:entity>) " //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "}";//
+
+		final CountingResultHandler crh = new CountingResultHandler();
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, tablePrefix);
+		PcjIntegrationTestingUtil.closeAndShutdown(conn, repo);
+		repo = PcjIntegrationTestingUtil.getPcjRepo(tablePrefix, "instance");
+		conn = repo.getConnection();
+		conn.add(sub, talksTo, obj);
+		conn.add(sub2, talksTo, obj2);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(
+				crh);
+
+		Assert.assertEquals(1, crh.getCount());
+
+	}
+
+	@Test
+	public void testEvaluateSingeFilterWithUnion()
+			throws TupleQueryResultHandlerException, QueryEvaluationException,
+			MalformedQueryException, RepositoryException, AccumuloException,
+			AccumuloSecurityException, TableExistsException, RyaDAOException,
+			SailException, TableNotFoundException, PcjException {
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?e ?l ?c " //
+				+ "{" //
+				+ "  Filter(?l = \"label2\") " //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablePrefix
+				+ "INDEX_2", indexSparqlString2, new String[] { "e", "l", "c" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		final String queryString = ""//
+				+ "SELECT ?e ?c ?o ?m ?l" //
+				+ "{" //
+				+ "   Filter(?l = \"label2\") " //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ " { ?e a ?c .  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?m  }"//
+				+ " UNION { ?e a ?c .  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l  }"//
+				+ "}";//
+
+		final CountingResultHandler crh = new CountingResultHandler();
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, tablePrefix);
+		PcjIntegrationTestingUtil.closeAndShutdown(conn, repo);
+		repo = PcjIntegrationTestingUtil.getPcjRepo(tablePrefix, "instance");
+		conn = repo.getConnection();
+		conn.add(sub, talksTo, obj);
+		conn.add(sub2, talksTo, obj2);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(
+				crh);
+
+		Assert.assertEquals(1, crh.getCount());
+
+	}
+
+	@Test
+	public void testEvaluateSingeFilterWithLeftJoin()
+			throws TupleQueryResultHandlerException, QueryEvaluationException,
+			MalformedQueryException, RepositoryException, AccumuloException,
+			AccumuloSecurityException, TableExistsException, RyaDAOException,
+			SailException, TableNotFoundException, PcjException {
+
+		final String indexSparqlString1 = ""//
+				+ "SELECT ?e ?l ?c " //
+				+ "{" //
+				+ "  Filter(?l = \"label3\") " //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final URI sub3 = new URIImpl("uri:entity3");
+		final URI subclass3 = new URIImpl("uri:class3");
+		conn.add(sub3, RDF.TYPE, subclass3);
+		conn.add(sub3, RDFS.LABEL, new LiteralImpl("label3"));
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablePrefix
+				+ "INDEX_1", indexSparqlString1, new String[] { "e", "l", "c" },
+				Optional.<PcjVarOrderFactory> absent());
+		final String queryString = ""//
+				+ "SELECT ?e ?c ?o ?m ?l" //
+				+ "{" //
+				+ "  Filter(?l = \"label3\") " //
+				+ "  ?e a ?c . " //
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . " //
+				+ "  OPTIONAL { ?e <uri:talksTo> ?o . ?e <http://www.w3.org/2000/01/rdf-schema#label> ?m }"//
+				+ "}";//
+
+		final CountingResultHandler crh = new CountingResultHandler();
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, tablePrefix);
+		PcjIntegrationTestingUtil.closeAndShutdown(conn, repo);
+		repo = PcjIntegrationTestingUtil.getPcjRepo(tablePrefix, "instance");
+		conn = repo.getConnection();
+		conn.add(sub, talksTo, obj);
+		conn.add(sub, RDFS.LABEL, new LiteralImpl("label"));
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(
+				crh);
+
+		Assert.assertEquals(1, crh.getCount());
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexUnionFilter() throws AccumuloException,
+			AccumuloSecurityException, TableExistsException,
+			RepositoryException, MalformedQueryException, SailException,
+			QueryEvaluationException, TableNotFoundException,
+			TupleQueryResultHandlerException, RyaDAOException, PcjException {
+
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+		conn.add(sub, RDF.TYPE, obj);
+		conn.add(sub2, RDF.TYPE, obj2);
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?e ?l ?o " //
+				+ "{" //
+				+ "   Filter(?l = \"label2\") " //
+				+ "  ?e a ?o . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?e ?l ?o " //
+				+ "{" //
+				+ "   Filter(?l = \"label2\") " //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final String queryString = ""//
+				+ "SELECT ?c ?e ?l ?o " //
+				+ "{" //
+				+ "   Filter(?l = \"label2\") " //
+				+ "  ?e a ?c . "//
+				+ " { ?e a ?o .  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l  }"//
+				+ " UNION { ?e <uri:talksTo> ?o .  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l  }"//
+				+ "}";//
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablePrefix
+				+ "INDEX_1", indexSparqlString, new String[] { "e", "l", "o" },
+				Optional.<PcjVarOrderFactory> absent());
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablePrefix
+				+ "INDEX_2", indexSparqlString2, new String[] { "e", "l", "o" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, tablePrefix);
+		PcjIntegrationTestingUtil.closeAndShutdown(conn, repo);
+		repo = PcjIntegrationTestingUtil.getPcjRepo(tablePrefix, "instance");
+		conn = repo.getConnection();
+		conn.add(sub2, RDF.TYPE, subclass2);
+		conn.add(sub2, RDF.TYPE, obj2);
+		final CountingResultHandler crh = new CountingResultHandler();
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(
+				crh);
+
+		Assert.assertEquals(6, crh.getCount());
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexLeftJoinUnionFilter()
+			throws AccumuloException, AccumuloSecurityException,
+			TableExistsException, RepositoryException, MalformedQueryException,
+			SailException, QueryEvaluationException, TableNotFoundException,
+			TupleQueryResultHandlerException, RyaDAOException, PcjException {
+
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+		conn.add(sub, RDF.TYPE, obj);
+		conn.add(sub2, RDF.TYPE, obj2);
+
+		final URI livesIn = new URIImpl("uri:livesIn");
+		final URI city = new URIImpl("uri:city");
+		final URI city2 = new URIImpl("uri:city2");
+		final URI city3 = new URIImpl("uri:city3");
+		conn.add(sub, livesIn, city);
+		conn.add(sub2, livesIn, city2);
+		conn.add(sub2, livesIn, city3);
+		conn.add(sub, livesIn, city3);
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?e ?l ?o " //
+				+ "{" //
+				+ "  ?e a ?o . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?e ?l ?o " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final String queryString = ""//
+				+ "SELECT ?c ?e ?l ?o " //
+				+ "{" //
+				+ " Filter(?c = <uri:city3>) " //
+				+ " ?e <uri:livesIn> ?c . "//
+				+ " OPTIONAL{{ ?e a ?o .  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l  }"//
+				+ " UNION { ?e <uri:talksTo> ?o .  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l  }}"//
+				+ "}";//
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablePrefix
+				+ "INDEX_1", indexSparqlString, new String[] { "e", "l", "o" },
+				Optional.<PcjVarOrderFactory> absent());
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablePrefix
+				+ "INDEX_2", indexSparqlString2, new String[] { "e", "l", "o" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, tablePrefix);
+		PcjIntegrationTestingUtil.closeAndShutdown(conn, repo);
+		repo = PcjIntegrationTestingUtil.getPcjRepo(tablePrefix, "instance");
+		conn = repo.getConnection();
+		conn.add(sub2, livesIn, city3);
+		conn.add(sub, livesIn, city3);
+
+		final CountingResultHandler crh = new CountingResultHandler();
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(
+				crh);
+
+		Assert.assertEquals(6, crh.getCount());
+
+	}
+
+	public static class CountingResultHandler implements
+			TupleQueryResultHandler {
+		private int count = 0;
+
+		public int getCount() {
+			return count;
+		}
+
+		public void resetCount() {
+			this.count = 0;
+		}
+
+		@Override
+		public void startQueryResult(List<String> arg0)
+				throws TupleQueryResultHandlerException {
+		}
+
+		@Override
+		public void handleSolution(BindingSet arg0)
+				throws TupleQueryResultHandlerException {
+			System.out.println(arg0);
+			count++;
+			System.out.println("Count is " + count);
+		}
+
+		@Override
+		public void endQueryResult() throws TupleQueryResultHandlerException {
+		}
+
+		@Override
+		public void handleBoolean(boolean arg0)
+				throws QueryResultHandlerException {
+			// TODO Auto-generated method stub
+
+		}
+
+		@Override
+		public void handleLinks(List<String> arg0)
+				throws QueryResultHandlerException {
+			// TODO Auto-generated method stub
+
+		}
+	}
+
 }
-    
-    
-    
-    

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerTest.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerTest.java
index 396224f..6e8c721 100644
--- a/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerTest.java
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerTest.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.external;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -23,10 +23,10 @@ package mvm.rya.indexing.external;
 import java.util.ArrayList;
 import java.util.List;
 
-import junit.framework.Assert;
 import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
 import mvm.rya.indexing.external.tupleSet.SimpleExternalTupleSet;
 
+import org.junit.Assert;
 import org.junit.Test;
 import org.openrdf.query.algebra.Projection;
 import org.openrdf.query.algebra.QueryModelNode;
@@ -41,34 +41,34 @@ import com.beust.jcommander.internal.Lists;
 
 public class PrecompJoinOptimizerTest {
 
-    private String q7 = ""//
+    private final String q7 = ""//
             + "SELECT ?s ?t ?u " //
             + "{" //
             + "  ?s a ?t ."//
             + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
             + "  ?u <uri:talksTo> ?s . "//
             + "}";//
-    
-    private String q8 = ""//
+
+    private final String q8 = ""//
             + "SELECT ?e ?l ?c " //
             + "{" //
             + "  ?e a ?l ."//
             + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
             + "  ?c <uri:talksTo> ?e . "//
             + "}";//
-    
-    private String q9 = ""//
+
+    private final String q9 = ""//
             + "SELECT ?f ?m ?d " //
             + "{" //
             + "  ?f a ?m ."//
             + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
             + "  ?d <uri:talksTo> ?f . "//
             + "}";//
-    
-    
-    
-    
-    private String q15 = ""//
+
+
+
+
+    private final String q15 = ""//
             + "SELECT ?f ?m ?d ?e ?l ?c " //
             + "{" //
             + "  ?f a ?m ."//
@@ -78,8 +78,8 @@ public class PrecompJoinOptimizerTest {
             + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
             + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
             + "}";//
-    
-    private String q16 = ""//
+
+    private final String q16 = ""//
             + "SELECT ?f ?m ?d ?e ?l " //
             + "{" //
             + "  ?d <uri:talksTo> ?f . "//
@@ -87,28 +87,28 @@ public class PrecompJoinOptimizerTest {
             + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
             + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
             + "}";//
-    
-    private String q17 = ""//
+
+    private final String q17 = ""//
             + "SELECT ?chicken ?dog ?cat  " //
             + "{" //
             + "  ?chicken <uri:talksTo> ?dog . "//
             + "  ?cat <http://www.w3.org/2000/01/rdf-schema#label> ?chicken ."//
             + "}";//
-    
-    private String q18 = ""//
+
+    private final String q18 = ""//
             + "SELECT ?dog ?chicken " //
             + "{" //
             + "  ?chicken <uri:talksTo> ?dog . "//
             + "}";//
-    
-    private String q19 = ""//
+
+    private final String q19 = ""//
             + "SELECT ?cat ?chicken " //
             + "{" //
             + "  ?cat <http://www.w3.org/2000/01/rdf-schema#label> ?chicken ."//
             + "}";//
-    
-    
-    private String q20 = ""//
+
+
+    private final String q20 = ""//
             + "SELECT ?f ?m ?d ?e ?l ?c " //
             + "{" //
             + "  ?f a ?m ."//
@@ -119,68 +119,65 @@ public class PrecompJoinOptimizerTest {
             + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
             + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
             + "}";//
-    
-    
-    
-    private String q21 = ""//
+
+
+
+    private final String q21 = ""//
             + "SELECT ?u ?s ?t " //
             + "{" //
             + "  ?s a ?t ."//
             + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
             + "  ?u <uri:talksTo> ?s . "//
             + "}";//
-    
-    
-    
+
+
+
     @Test
     public void testSingleIndex() throws Exception {
 
-        SPARQLParser parser = new SPARQLParser();
-        
+        final SPARQLParser parser = new SPARQLParser();
+
 
-        ParsedQuery pq1 = parser.parseQuery(q15, null);
-        ParsedQuery pq2 = parser.parseQuery(q7, null);
-        ParsedQuery pq3 = parser.parseQuery(q8, null);
-        ParsedQuery pq4 = parser.parseQuery(q9, null);
-       
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
-        SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
+        final ParsedQuery pq1 = parser.parseQuery(q15, null);
+        final ParsedQuery pq2 = parser.parseQuery(q7, null);
+        final ParsedQuery pq3 = parser.parseQuery(q8, null);
+        final ParsedQuery pq4 = parser.parseQuery(q9, null);
 
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+        final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
+        final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
+        final SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
+
+        final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
 
         list.add(extTup1);
-        
-        List<QueryModelNode> optTupNodes = Lists.newArrayList();
+
+        final List<QueryModelNode> optTupNodes = Lists.newArrayList();
         optTupNodes.add(extTup2);
         optTupNodes.add(extTup3);
 
-        PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, true);
-        TupleExpr te = pq1.getTupleExpr();
+        final PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, true);
+        final TupleExpr te = pq1.getTupleExpr();
         pcj.optimize(te, null, null);
-        
-        NodeCollector nc = new NodeCollector();
+
+        final NodeCollector nc = new NodeCollector();
         te.visit(nc);
-        
-        List<QueryModelNode> qNodes = nc.getNodes();
-             
-        
+
+        final List<QueryModelNode> qNodes = nc.getNodes();
+
+
         Assert.assertEquals(qNodes.size(), optTupNodes.size());
-        for(QueryModelNode node: qNodes) {
+        for(final QueryModelNode node: qNodes) {
             Assert.assertTrue(optTupNodes.contains(node));
         }
-               
+
 
     }
-    
-    
-    
-    
-    
+
+
     @Test
     public void testSingleIndex2() throws Exception {
 
-        String q1 = ""//
+        final String q1 = ""//
                 + "SELECT ?f ?m ?d ?e ?l ?c " //
                 + "{" //
                 + "  ?f a ?m ."//
@@ -192,7 +189,7 @@ public class PrecompJoinOptimizerTest {
                 + "  ?m <uri:talksTo> ?e . "//
                 + "}";//
 
-        String q2 = ""//
+        final String q2 = ""//
                 + "SELECT ?u ?s ?t " //
                 + "{" //
                 + "  ?s a ?t ."//
@@ -200,7 +197,7 @@ public class PrecompJoinOptimizerTest {
                 + "  ?u <uri:talksTo> ?s . "//
                 + "}";//
 
-        String q3 = ""//
+        final String q3 = ""//
                 + "SELECT ?e ?c ?l " //
                 + "{" //
                 + "  ?c a ?l ."//
@@ -208,7 +205,7 @@ public class PrecompJoinOptimizerTest {
                 + "  ?e <uri:talksTo> ?c . "//
                 + "}";//
 
-        String q4 = ""//
+        final String q4 = ""//
                 + "SELECT ?d ?f ?m " //
                 + "{" //
                 + "  ?f a ?m ."//
@@ -216,54 +213,54 @@ public class PrecompJoinOptimizerTest {
                 + "  ?d <uri:talksTo> ?f . "//
                 + "}";//
 
-        SPARQLParser parser = new SPARQLParser();
+        final SPARQLParser parser = new SPARQLParser();
 
-        ParsedQuery pq1 = parser.parseQuery(q1, null);
-        ParsedQuery pq2 = parser.parseQuery(q2, null);
-        ParsedQuery pq3 = parser.parseQuery(q3, null);
-        ParsedQuery pq4 = parser.parseQuery(q4, null);
+        final ParsedQuery pq1 = parser.parseQuery(q1, null);
+        final ParsedQuery pq2 = parser.parseQuery(q2, null);
+        final ParsedQuery pq3 = parser.parseQuery(q3, null);
+        final ParsedQuery pq4 = parser.parseQuery(q4, null);
 
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
-        SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
+        final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
+        final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
+        final SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
 
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+        final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
 
         list.add(extTup1);
 
-        List<StatementPattern> spList = StatementPatternCollector.process(pq1.getTupleExpr());
-        List<QueryModelNode> optTupNodes = Lists.newArrayList();
+        final List<StatementPattern> spList = StatementPatternCollector.process(pq1.getTupleExpr());
+        final List<QueryModelNode> optTupNodes = Lists.newArrayList();
         optTupNodes.add(extTup3);
         optTupNodes.add(spList.get(6));
         optTupNodes.add(extTup2);
 
-        PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, true);
-        TupleExpr te = pq1.getTupleExpr();
+        final PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, true);
+        final TupleExpr te = pq1.getTupleExpr();
         pcj.optimize(te, null, null);
 
-        NodeCollector nc = new NodeCollector();
+        final NodeCollector nc = new NodeCollector();
         te.visit(nc);
 
         //System.out.println("Optimal plan is " + optimalTup);
 
-        List<QueryModelNode> qNodes = nc.getNodes();
+        final List<QueryModelNode> qNodes = nc.getNodes();
         //System.out.println("Returned list is " + qNodes + " and comp list is " + optTupNodes);
 
         Assert.assertTrue(qNodes.equals(optTupNodes));
 
     }
-    
-    
-    
-    
-    
-    
-    
-    
+
+
+
+
+
+
+
+
     @Test
     public void testTwoIndex() throws Exception {
 
-        String q1 = ""//
+        final String q1 = ""//
                 + "SELECT ?f ?m ?d ?h ?i " //
                 + "{" //
                 + "  ?f a ?m ."//
@@ -275,7 +272,7 @@ public class PrecompJoinOptimizerTest {
                 + "  ?i <uri:associatesWith> ?h ." //
                 + "}";//
 
-        String q2 = ""//
+        final String q2 = ""//
                 + "SELECT ?t ?s ?u " //
                 + "{" //
                 + "  ?s a ?t ."//
@@ -283,21 +280,21 @@ public class PrecompJoinOptimizerTest {
                 + "  ?u <uri:talksTo> ?s . "//
                 + "}";//
 
-        String q3 = ""//
+        final String q3 = ""//
                 + "SELECT ?s ?t ?u " //
                 + "{" //
                 + "  ?s <uri:hangOutWith> ?t ." //
                 + "  ?t <uri:hangOutWith> ?u ." //
                 + "}";//
 
-        String q4 = ""//
+        final String q4 = ""//
                 + "SELECT ?s ?t ?u " //
                 + "{" //
                 + "  ?s <uri:associatesWith> ?t ." //
                 + "  ?t <uri:associatesWith> ?u ." //
                 + "}";//
 
-        String q5 = ""//
+        final String q5 = ""//
                 + "SELECT ?m ?f ?d " //
                 + "{" //
                 + "  ?f a ?m ."//
@@ -305,73 +302,73 @@ public class PrecompJoinOptimizerTest {
                 + "  ?d <uri:talksTo> ?f . "//
                 + "}";//
 
-        String q6 = ""//
+        final String q6 = ""//
                 + "SELECT ?d ?f ?h " //
                 + "{" //
                 + "  ?d <uri:hangOutWith> ?f ." //
                 + "  ?f <uri:hangOutWith> ?h ." //
                 + "}";//
 
-        String q7 = ""//
+        final String q7 = ""//
                 + "SELECT ?f ?i ?h " //
                 + "{" //
                 + "  ?f <uri:associatesWith> ?i ." //
                 + "  ?i <uri:associatesWith> ?h ." //
                 + "}";//
 
-        SPARQLParser parser = new SPARQLParser();
+        final SPARQLParser parser = new SPARQLParser();
 
-        ParsedQuery pq1 = parser.parseQuery(q1, null);
-        ParsedQuery pq2 = parser.parseQuery(q2, null);
-        ParsedQuery pq3 = parser.parseQuery(q3, null);
-        ParsedQuery pq4 = parser.parseQuery(q4, null);
-        ParsedQuery pq5 = parser.parseQuery(q5, null);
-        ParsedQuery pq6 = parser.parseQuery(q6, null);
-        ParsedQuery pq7 = parser.parseQuery(q7, null);
+        final ParsedQuery pq1 = parser.parseQuery(q1, null);
+        final ParsedQuery pq2 = parser.parseQuery(q2, null);
+        final ParsedQuery pq3 = parser.parseQuery(q3, null);
+        final ParsedQuery pq4 = parser.parseQuery(q4, null);
+        final ParsedQuery pq5 = parser.parseQuery(q5, null);
+        final ParsedQuery pq6 = parser.parseQuery(q6, null);
+        final ParsedQuery pq7 = parser.parseQuery(q7, null);
 
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
-        SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
-        SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet((Projection) pq5.getTupleExpr());
-        SimpleExternalTupleSet extTup5 = new SimpleExternalTupleSet((Projection) pq6.getTupleExpr());
-        SimpleExternalTupleSet extTup6 = new SimpleExternalTupleSet((Projection) pq7.getTupleExpr());
+        final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
+        final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
+        final SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
+        final SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet((Projection) pq5.getTupleExpr());
+        final SimpleExternalTupleSet extTup5 = new SimpleExternalTupleSet((Projection) pq6.getTupleExpr());
+        final SimpleExternalTupleSet extTup6 = new SimpleExternalTupleSet((Projection) pq7.getTupleExpr());
 
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+        final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
 
         list.add(extTup2);
         list.add(extTup1);
         list.add(extTup3);
 
-        List<QueryModelNode> optTupNodes = Lists.newArrayList();
+        final List<QueryModelNode> optTupNodes = Lists.newArrayList();
         optTupNodes.add(extTup4);
         optTupNodes.add(extTup6);
         optTupNodes.add(extTup5);
 
-        PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, true);
-        TupleExpr te = pq1.getTupleExpr();
+        final PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, true);
+        final TupleExpr te = pq1.getTupleExpr();
         pcj.optimize(te, null, null);
 
         System.out.println(te);
 
-        NodeCollector nc = new NodeCollector();
+        final NodeCollector nc = new NodeCollector();
         te.visit(nc);
 
-        List<QueryModelNode> qNodes = nc.getNodes();
+        final List<QueryModelNode> qNodes = nc.getNodes();
 
         Assert.assertTrue(qNodes.equals(optTupNodes));
 
     }
-    
-     
 
-    
-    
-    
+
+
+
+
+
     @Test
     public void twoIndexFilterTest() {
-        
-        
-        String q1 = ""//
+
+
+        final String q1 = ""//
                 + "SELECT ?f ?m ?d ?e ?l ?c " //
                 + "{" //
                 + "  Filter(?f > \"5\")." //
@@ -383,59 +380,59 @@ public class PrecompJoinOptimizerTest {
                 + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
                 + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
                 + "}";//
-        
-        
-        String q2 = ""//
+
+
+        final String q2 = ""//
                 + "SELECT ?s ?t ?u " //
                 + "{" //
                 + "  ?s a ?t ."//
                 + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
                 + "  ?u <uri:talksTo> ?s . "//
                 + "}";//
-        
-        
-        String q3 = ""//
+
+
+        final String q3 = ""//
                 + "SELECT ?s ?t ?u " //
                 + "{" //
-                + " Filter(?s > \"5\") ."// 
+                + " Filter(?s > \"5\") ."//
                 + "  ?s a ?t ."//
                 + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
                 + "  ?u <uri:talksTo> ?s . "//
                 + "}";//
-        
-        
-        
-        String q4 = ""//
+
+
+
+        final String q4 = ""//
                 + "SELECT ?f ?m ?d " //
                 + "{" //
-                + " Filter(?f > \"5\") ."// 
+                + " Filter(?f > \"5\") ."//
                 + "  ?f a ?m ."//
                 + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
                 + "  ?d <uri:talksTo> ?f . "//
                 + "}";//
-        
-        
-        String q5 = ""//
+
+
+        final String q5 = ""//
                 + "SELECT ?e ?l ?c " //
                 + "{" //
-                + " Filter(?e > \"5\") ."// 
+                + " Filter(?e > \"5\") ."//
                 + "  ?e a ?l ."//
                 + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
                 + "  ?c <uri:talksTo> ?e . "//
                 + "}";//
-        
-        
-        
-        
-        SPARQLParser parser = new SPARQLParser();
+
+
+
+
+        final SPARQLParser parser = new SPARQLParser();
 
         ParsedQuery pq1 = null;
         ParsedQuery pq2 = null;
         ParsedQuery pq3 = null;
         ParsedQuery pq4 = null;
         ParsedQuery pq5 = null;
-        
-       
+
+
 
         try {
             pq1 = parser.parseQuery(q1, null);
@@ -443,53 +440,53 @@ public class PrecompJoinOptimizerTest {
             pq3 = parser.parseQuery(q3, null);
             pq4 = parser.parseQuery(q4, null);
             pq5 = parser.parseQuery(q5, null);
-            
-           
-        } catch (Exception e) {
+
+
+        } catch (final Exception e) {
             e.printStackTrace();
         }
 
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
-        SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
-        SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet((Projection) pq5.getTupleExpr());
-     
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+        final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
+        final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
+        final SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
+        final SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet((Projection) pq5.getTupleExpr());
+
+        final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
 
         list.add(extTup2);
         list.add(extTup1);
-        
-        List<ExternalTupleSet> list2 = new ArrayList<ExternalTupleSet>();
+
+        final List<ExternalTupleSet> list2 = new ArrayList<ExternalTupleSet>();
 
         list2.add(extTup3);
         list2.add(extTup4);
-      
-        PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, true);
-        TupleExpr te = pq1.getTupleExpr();
+
+        final PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, true);
+        final TupleExpr te = pq1.getTupleExpr();
         pcj.optimize(te, null, null);
 
         System.out.println(te);
-        
-        NodeCollector nc = new NodeCollector();
+
+        final NodeCollector nc = new NodeCollector();
         te.visit(nc);
 
         Assert.assertEquals(nc.getNodes().size(), list2.size());
 
-        for(QueryModelNode e: nc.getNodes()) {
-            Assert.assertTrue(list2.contains((ExternalTupleSet)e));
+        for(final QueryModelNode e: nc.getNodes()) {
+            Assert.assertTrue(list2.contains(e));
         }
-        
-        
-        
+
+
+
     }
-    
-
-    
-    
-    
-    
- 
-    
+
+
+
+
+
+
+
+
     public static class NodeCollector extends QueryModelVisitorBase<RuntimeException> {
 
         List<QueryModelNode> qNodes = Lists.newArrayList();
@@ -498,24 +495,24 @@ public class PrecompJoinOptimizerTest {
         public List<QueryModelNode> getNodes() {
             return qNodes;
         }
-        
-       
+
+
 
         @Override
         public void meetNode(QueryModelNode node) {
             if(node instanceof StatementPattern || node instanceof ExternalTupleSet) {
                 qNodes.add(node);
-            } 
+            }
             super.meetNode(node);
-            
+
         }
 
-     
+
     }
-    
-    
-    
-    
+
+
+
+
 
 
 }


[11/16] incubator-rya git commit: RYA-32 Improve how metadata and values are written to Accumulo PCJ tables

Posted by mi...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/IndexPlanValidatorTest.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/IndexPlanValidatorTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/IndexPlanValidatorTest.java
index eea5b95..2826318 100644
--- a/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/IndexPlanValidatorTest.java
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/IndexPlanValidatorTest.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.IndexPlanValidator;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -19,22 +19,18 @@ package mvm.rya.indexing.IndexPlanValidator;
  * under the License.
  */
 
-
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Set;
 
-import junit.framework.Assert;
-import mvm.rya.indexing.IndexPlanValidator.ThreshholdPlanSelectorTest.NodeCollector;
-import mvm.rya.indexing.external.ExternalProcessor;
+import mvm.rya.indexing.external.PrecompJoinOptimizer;
 import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
 import mvm.rya.indexing.external.tupleSet.SimpleExternalTupleSet;
 
+import org.junit.Assert;
 import org.junit.Test;
 import org.openrdf.query.MalformedQueryException;
 import org.openrdf.query.algebra.Projection;
-import org.openrdf.query.algebra.QueryModelNode;
 import org.openrdf.query.algebra.TupleExpr;
 import org.openrdf.query.parser.ParsedQuery;
 import org.openrdf.query.parser.sparql.SPARQLParser;
@@ -43,1106 +39,746 @@ import com.google.common.collect.Lists;
 
 public class IndexPlanValidatorTest {
 
-    
-    @Test
-    public void testEvaluateTwoIndexTwoVarOrder1() {
-        
-        System.out.println("********************Test number 1***************************");
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?c ?e ?l  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?e ?o ?l " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        
-       
-        SPARQLParser sp = new SPARQLParser();
-        ParsedQuery index1 = null;
-        ParsedQuery index2 = null;
-        try {
-            index1 = sp.parseQuery(indexSparqlString, null);
-            index2 = sp.parseQuery(indexSparqlString2, null);
-        } catch (MalformedQueryException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        }
-        
-          
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        
-        SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet((Projection)index1.getTupleExpr());
-        SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet((Projection)index2.getTupleExpr());;
-                
-        index.add(ais1);
-        index.add(ais2);
-        
-        ParsedQuery pq = null;
-
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-        TupleExpr tup = processor.process(pq.getTupleExpr());
-
-        System.out.println("TupleExpr is " + tup);
-        
-        IndexPlanValidator ipv = new IndexPlanValidator(false);
-        Assert.assertEquals(false, ipv.isValid(tup));
-        
-    }
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexTwoVarOrder2() {
-        
-        System.out.println("********************Test number 2***************************");
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?e ?l ?c  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?e ?o ?l " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        
-       
-        SPARQLParser sp = new SPARQLParser();
-        ParsedQuery index1 = null;
-        ParsedQuery index2 = null;
-        try {
-            index1 = sp.parseQuery(indexSparqlString, null);
-            index2 = sp.parseQuery(indexSparqlString2, null);
-        } catch (MalformedQueryException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        }
-        
-          
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        
-        SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet((Projection)index1.getTupleExpr());
-        SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet((Projection)index2.getTupleExpr());;
-                
-        index.add(ais1);
-        index.add(ais2);
-        
-        ParsedQuery pq = null;
-
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-        TupleExpr tup = processor.process(pq.getTupleExpr());
-
-        System.out.println("TupleExpr is " + tup);
-        
-        IndexPlanValidator ipv = new IndexPlanValidator(false);
-        Assert.assertEquals(true, ipv.isValid(tup));
-        
-    }
-    
-    
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexTwoVarOrder3() {
-      
-        
-        System.out.println("********************Test number 3***************************");
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?l ?e ?c  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?e ?o ?l " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        
-       
-        SPARQLParser sp = new SPARQLParser();
-        ParsedQuery index1 = null;
-        ParsedQuery index2 = null;
-        try {
-            index1 = sp.parseQuery(indexSparqlString, null);
-            index2 = sp.parseQuery(indexSparqlString2, null);
-        } catch (MalformedQueryException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        }
-        
-          
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        
-        SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet((Projection)index1.getTupleExpr());
-        SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet((Projection)index2.getTupleExpr());;
-                
-        index.add(ais1);
-        index.add(ais2);
-        
-        ParsedQuery pq = null;
-
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-        TupleExpr tup = processor.process(pq.getTupleExpr());
-
-        System.out.println("TupleExpr is " + tup);
-        
-        IndexPlanValidator ipv = new IndexPlanValidator(false);
-        Assert.assertEquals(true, ipv.isValid(tup));
-        
-    }
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexTwoVarOrder4() {
-        
-        
-        System.out.println("********************Test number 4***************************");
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?e ?c ?l  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?e ?o ?l " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        
-       
-        SPARQLParser sp = new SPARQLParser();
-        ParsedQuery index1 = null;
-        ParsedQuery index2 = null;
-        try {
-            index1 = sp.parseQuery(indexSparqlString, null);
-            index2 = sp.parseQuery(indexSparqlString2, null);
-        } catch (MalformedQueryException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        }
-        
-          
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        
-        SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet((Projection)index1.getTupleExpr());
-        SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet((Projection)index2.getTupleExpr());;
-                
-        index.add(ais1);
-        index.add(ais2);
-        
-        ParsedQuery pq = null;
-
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-        TupleExpr tup = processor.process(pq.getTupleExpr());
-
-        System.out.println("TupleExpr is " + tup);
-        
-        IndexPlanValidator ipv = new IndexPlanValidator(false);
-        Assert.assertEquals(false, ipv.isValid(tup));
-        
-    }
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexTwoVarOrder5() {
-        
-        System.out.println("********************Test number 5***************************");
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?e ?l ?c  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?l ?o ?e " //
-                + "{" //
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l ."//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        
-       
-        SPARQLParser sp = new SPARQLParser();
-        ParsedQuery index1 = null;
-        ParsedQuery index2 = null;
-        try {
-            index1 = sp.parseQuery(indexSparqlString, null);
-            index2 = sp.parseQuery(indexSparqlString2, null);
-        } catch (MalformedQueryException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        }
-        
-        
-          
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        
-        SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet((Projection)index1.getTupleExpr());
-        SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet((Projection)index2.getTupleExpr());
-        
-        System.out.println("Supported variable orders are " + ais1.getSupportedVariableOrders() + ", " + ais2.getSupportedVariableOrders());
-                
-        index.add(ais2);
-        index.add(ais1);
-        
-        
-        ParsedQuery pq = null;
-
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-       
-        System.out.println("query assured binding names are " + pq.getTupleExpr().getAssuredBindingNames());
-        
-        ExternalProcessor processor = new ExternalProcessor(index);
-        TupleExpr tup = processor.process(pq.getTupleExpr());
-
-        System.out.println("TupleExpr is " + tup);
-        
-        IndexPlanValidator ipv = new IndexPlanValidator(false);
-        Assert.assertEquals(false, ipv.isValid(tup));
-        
-    }
-    
-    
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexTwoVarOrder6() {
-        
-        
-        System.out.println("********************Test number 6***************************");
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?e ?l ?c  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?l ?e ?o " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        
-       
-        SPARQLParser sp = new SPARQLParser();
-        ParsedQuery index1 = null;
-        ParsedQuery index2 = null;
-        try {
-            index1 = sp.parseQuery(indexSparqlString, null);
-            index2 = sp.parseQuery(indexSparqlString2, null);
-        } catch (MalformedQueryException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        }
-        
-        
-      
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        
-        SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet((Projection)index1.getTupleExpr());
-        SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet((Projection)index2.getTupleExpr());;
-                
-        index.add(ais2);
-        index.add(ais1);
-        
-        
-        ParsedQuery pq = null;
-
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-        TupleExpr tup = processor.process(pq.getTupleExpr());
-
-        System.out.println("TupleExpr is " + tup);
-        
-        IndexPlanValidator ipv = new IndexPlanValidator(false);
-        Assert.assertEquals(true, ipv.isValid(tup));
-        
-    }
-    
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexCrossProduct1() {
-        
-        System.out.println("********************Test number 7***************************");
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?e ?l ?c  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?e ?l ?o " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o ?f ?g " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?f <uri:talksTo> ?g . " //
-                + "}";//
-
-        
-       
-        SPARQLParser sp = new SPARQLParser();
-        ParsedQuery index1 = null;
-        ParsedQuery index2 = null;
-        try {
-            index1 = sp.parseQuery(indexSparqlString, null);
-            index2 = sp.parseQuery(indexSparqlString2, null);
-        } catch (MalformedQueryException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        }
-        
-       
-          
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        
-        SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet((Projection)index1.getTupleExpr());
-        SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet((Projection)index2.getTupleExpr());;
-                
-        index.add(ais2);
-        index.add(ais1);
-        
-        
-        ParsedQuery pq = null;
-
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-        TupleExpr tup = processor.process(pq.getTupleExpr());
-
-        System.out.println("TupleExpr is " + tup);
-        
-        IndexPlanValidator ipv = new IndexPlanValidator(true);
-        Assert.assertEquals(false, ipv.isValid(tup));
-        
-    }
-    
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexCrossProduct2() {
-        
-        System.out.println("********************Test number 8***************************");
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?e ?l ?c  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?e ?l ?o " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o ?f ?g " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?f <uri:talksTo> ?g . " //
-                + "}";//
-
-        
-       
-        SPARQLParser sp = new SPARQLParser();
-        ParsedQuery index1 = null;
-        ParsedQuery index2 = null;
-        try {
-            index1 = sp.parseQuery(indexSparqlString, null);
-            index2 = sp.parseQuery(indexSparqlString2, null);
-        } catch (MalformedQueryException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        }
-        
-          
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        
-        SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet((Projection)index1.getTupleExpr());
-        SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet((Projection)index2.getTupleExpr());;
-                
-        
-        index.add(ais1);
-        index.add(ais2);
-        
-        
-        ParsedQuery pq = null;
-
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-        TupleExpr tup = processor.process(pq.getTupleExpr());
-
-        System.out.println("TupleExpr is " + tup);
-        
-        IndexPlanValidator ipv = new IndexPlanValidator(true);
-        Assert.assertEquals(false, ipv.isValid(tup));
-        
-    }
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexCrossProduct3() {
-        
-        System.out.println("********************Test number 9***************************");
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?e ?l ?c  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?e ?l ?o " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o ?f ?g " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?f <uri:talksTo> ?g . " //
-                + "}";//
-
-        
-       
-        SPARQLParser sp = new SPARQLParser();
-        ParsedQuery index1 = null;
-        ParsedQuery index2 = null;
-        try {
-            index1 = sp.parseQuery(indexSparqlString, null);
-            index2 = sp.parseQuery(indexSparqlString2, null);
-        } catch (MalformedQueryException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        }
-        
-          
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        
-        SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet((Projection)index1.getTupleExpr());
-        SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet((Projection)index2.getTupleExpr());;
-                
-        
-        index.add(ais1);
-        index.add(ais2);
-        
-        
-        ParsedQuery pq = null;
-
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-        TupleExpr tup = processor.process(pq.getTupleExpr());
-
-        System.out.println("TupleExpr is " + tup);
-        
-        IndexPlanValidator ipv = new IndexPlanValidator(false);
-        Assert.assertEquals(true, ipv.isValid(tup));
-        
-    }
-    
-    
-    
-    
-    
-    
-   
-    
-    @Test
-    public void testEvaluateTwoIndexDiffVars() {
-        
-        System.out.println("********************Test number 10***************************");
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?chicken ?dog ?pig  " //
-                + "{" //
-                + "  ?dog a ?chicken . "//
-                + "  ?dog <http://www.w3.org/2000/01/rdf-schema#label> ?pig "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?fish ?ant ?turkey " //
-                + "{" //
-                + "  ?fish <uri:talksTo> ?turkey . "//
-                + "  ?turkey <http://www.w3.org/2000/01/rdf-schema#label> ?ant "//
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o ?f ?g " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?f <uri:talksTo> ?g . " //
-                + "}";//
-
-        
-       
-        SPARQLParser sp = new SPARQLParser();
-        ParsedQuery index1 = null;
-        ParsedQuery index2 = null;
-        try {
-            index1 = sp.parseQuery(indexSparqlString, null);
-            index2 = sp.parseQuery(indexSparqlString2, null);
-        } catch (MalformedQueryException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        }
-        
-          
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        
-        SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet((Projection)index1.getTupleExpr());
-        SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet((Projection)index2.getTupleExpr());;
-                
-        
-        index.add(ais1);
-        index.add(ais2);
-        
-        
-        ParsedQuery pq = null;
-
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-        TupleExpr tup = processor.process(pq.getTupleExpr());
-
-        System.out.println("TupleExpr is " + tup);
-        
-        IndexPlanValidator ipv = new IndexPlanValidator(false);
-        Assert.assertEquals(false, ipv.isValid(tup));
-        
-    }
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexDiffVars2() {
-        
-        System.out.println("********************Test number 11***************************");
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?dog ?pig ?chicken  " //
-                + "{" //
-                + "  ?dog a ?chicken . "//
-                + "  ?dog <http://www.w3.org/2000/01/rdf-schema#label> ?pig "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?fish ?ant ?turkey " //
-                + "{" //
-                + "  ?fish <uri:talksTo> ?turkey . "//
-                + "  ?turkey <http://www.w3.org/2000/01/rdf-schema#label> ?ant "//
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o ?f ?g " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?f <uri:talksTo> ?g . " //
-                + "}";//
-
-        
-       
-        SPARQLParser sp = new SPARQLParser();
-        ParsedQuery index1 = null;
-        ParsedQuery index2 = null;
-        try {
-            index1 = sp.parseQuery(indexSparqlString, null);
-            index2 = sp.parseQuery(indexSparqlString2, null);
-        } catch (MalformedQueryException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        }
-        
-          
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        
-        SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet((Projection)index1.getTupleExpr());
-        SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet((Projection)index2.getTupleExpr());;
-                
-        
-        index.add(ais1);
-        index.add(ais2);
-        
-        
-        ParsedQuery pq = null;
-
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-        TupleExpr tup = processor.process(pq.getTupleExpr());
-
-        System.out.println("TupleExpr is " + tup);
-        
-        IndexPlanValidator ipv = new IndexPlanValidator(false);
-        Assert.assertEquals(true, ipv.isValid(tup));
-        
-    }
-    
-    
-    @Test
-    public void testEvaluateTwoIndexDiffVars3() {
-        
-        System.out.println("********************Test number 11***************************");
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?pig ?dog ?chicken  " //
-                + "{" //
-                + "  ?dog a ?chicken . "//
-                + "  ?dog <http://www.w3.org/2000/01/rdf-schema#label> ?pig "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?fish ?ant ?turkey " //
-                + "{" //
-                + "  ?fish <uri:talksTo> ?turkey . "//
-                + "  ?turkey <http://www.w3.org/2000/01/rdf-schema#label> ?ant "//
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o ?f ?g " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?f <uri:talksTo> ?g . " //
-                + "}";//
-
-        
-       
-        SPARQLParser sp = new SPARQLParser();
-        ParsedQuery index1 = null;
-        ParsedQuery index2 = null;
-        try {
-            index1 = sp.parseQuery(indexSparqlString, null);
-            index2 = sp.parseQuery(indexSparqlString2, null);
-        } catch (MalformedQueryException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        }
-        
-          
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        
-        SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet((Projection)index1.getTupleExpr());
-        SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet((Projection)index2.getTupleExpr());;
-                
-        
-        index.add(ais1);
-        index.add(ais2);
-        
-        
-        ParsedQuery pq = null;
-
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-        TupleExpr tup = processor.process(pq.getTupleExpr());
-
-        System.out.println("TupleExpr is " + tup);
-        
-        IndexPlanValidator ipv = new IndexPlanValidator(false);
-        Assert.assertEquals(true, ipv.isValid(tup));
-        
-    }
-    
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexDiffVarsDirProd() {
-        
-        System.out.println("********************Test number 12***************************");
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?pig ?dog ?chicken  " //
-                + "{" //
-                + "  ?dog a ?chicken . "//
-                + "  ?dog <http://www.w3.org/2000/01/rdf-schema#label> ?pig "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?fish ?ant ?turkey " //
-                + "{" //
-                + "  ?fish <uri:talksTo> ?turkey . "//
-                + "  ?turkey <http://www.w3.org/2000/01/rdf-schema#label> ?ant "//
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o ?f ?g " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?f <uri:talksTo> ?g . " //
-                + "}";//
-
-        
-       
-        SPARQLParser sp = new SPARQLParser();
-        ParsedQuery index1 = null;
-        ParsedQuery index2 = null;
-        try {
-            index1 = sp.parseQuery(indexSparqlString, null);
-            index2 = sp.parseQuery(indexSparqlString2, null);
-        } catch (MalformedQueryException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        }
-        
-          
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        
-        SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet((Projection)index1.getTupleExpr());
-        SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet((Projection)index2.getTupleExpr());;
-                
-        
-        index.add(ais1);
-        index.add(ais2);
-        
-        
-        ParsedQuery pq = null;
-
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-        TupleExpr tup = processor.process(pq.getTupleExpr());
-
-        System.out.println("TupleExpr is " + tup);
-        
-        IndexPlanValidator ipv = new IndexPlanValidator(true);
-        Assert.assertEquals(false, ipv.isValid(tup));
-        
-    }
-    
-    
-    
-    @Test
-    public void testValidTupleIterator() throws Exception {
-        
-        System.out.println("********************Test number 13***************************");
-        
-        String q1 = ""//
-                + "SELECT ?f ?m ?d ?h ?i " //
-                + "{" //
-                + "  ?f a ?m ."//
-                + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-                + "  ?d <uri:talksTo> ?f . "//
-                + "  ?d <uri:hangOutWith> ?f ." //
-                + "  ?f <uri:hangOutWith> ?h ." //
-                + "  ?f <uri:associatesWith> ?i ." //
-                + "  ?i <uri:associatesWith> ?h ." //
-                + "}";//
-
-        String q2 = ""//
-                + "SELECT ?t ?s ?u " //
-                + "{" //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "  ?u <uri:talksTo> ?s . "//
-                + "}";//
-        
-        
-        String q3 = ""//
-                + "SELECT ?s ?t ?u " //
-                + "{" //
-                + "  ?s <uri:hangOutWith> ?t ." //
-                + "  ?t <uri:hangOutWith> ?u ." //
-                + "}";//
-        
-        String q4 = ""//
-                + "SELECT ?s ?t ?u " //
-                + "{" //
-                + "  ?s <uri:associatesWith> ?t ." //
-                + "  ?t <uri:associatesWith> ?u ." //
-                + "}";//
-        
-      
-        
-        
-        
-
-        SPARQLParser parser = new SPARQLParser();
-
-        ParsedQuery pq1 = parser.parseQuery(q1, null);
-        ParsedQuery pq2 = parser.parseQuery(q2, null);
-        ParsedQuery pq3 = parser.parseQuery(q3, null);
-        ParsedQuery pq4 = parser.parseQuery(q4, null);
-     
-
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
-        SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
-        
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-
-        list.add(extTup2);
-        list.add(extTup1);
-        list.add(extTup3);
-
-        IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(pq1.getTupleExpr(), list);
-
-        Iterator<TupleExpr> plans = (new TupleExecutionPlanGenerator()).getPlans(iep.getIndexedTuples());
-        IndexPlanValidator ipv = new IndexPlanValidator(true);
-        Iterator<TupleExpr> validPlans = ipv.getValidTuples(plans);
-        
-        int size = 0;
-        
-        while(validPlans.hasNext()) {
-            Assert.assertTrue(validPlans.hasNext());
-            validPlans.next();
-            size++;
-        }
-        
-        Assert.assertTrue(!validPlans.hasNext());
-        Assert.assertEquals(732, size);
-
-       
-
-    }
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
+	@Test
+	public void testEvaluateTwoIndexTwoVarOrder1()
+			throws MalformedQueryException {
+
+		System.out
+				.println("********************Test number 1***************************");
+
+		String indexSparqlString = ""//
+				+ "SELECT ?c ?e ?l  " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		String indexSparqlString2 = ""//
+				+ "SELECT ?e ?o ?l " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		String queryString = ""//
+				+ "SELECT ?e ?c ?l ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		SPARQLParser sp = new SPARQLParser();
+		ParsedQuery index1 = sp.parseQuery(indexSparqlString, null);
+		ParsedQuery index2 = sp.parseQuery(indexSparqlString2, null);
+		List<ExternalTupleSet> index = Lists.newArrayList();
+
+		SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet(
+				(Projection) index1.getTupleExpr());
+		SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet(
+				(Projection) index2.getTupleExpr());
+
+		index.add(ais1);
+		index.add(ais2);
+
+		ParsedQuery pq = sp.parseQuery(queryString, null);
+		TupleExpr tup = pq.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(index, false);
+		pcj.optimize(tup, null, null);
+
+		System.out.println("TupleExpr is " + tup);
+
+		IndexPlanValidator ipv = new IndexPlanValidator(false);
+		Assert.assertEquals(false, ipv.isValid(tup));
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexTwoVarOrder2()
+			throws MalformedQueryException {
+
+		System.out
+				.println("********************Test number 2***************************");
+
+		String indexSparqlString = ""//
+				+ "SELECT ?e ?l ?c  " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		String indexSparqlString2 = ""//
+				+ "SELECT ?e ?o ?l " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		String queryString = ""//
+				+ "SELECT ?e ?c ?l ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		SPARQLParser sp = new SPARQLParser();
+		ParsedQuery index1 = sp.parseQuery(indexSparqlString, null);
+		ParsedQuery index2 = sp.parseQuery(indexSparqlString2, null);
+
+		List<ExternalTupleSet> index = Lists.newArrayList();
+
+		SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet(
+				(Projection) index1.getTupleExpr());
+		SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet(
+				(Projection) index2.getTupleExpr());
+
+		index.add(ais1);
+		index.add(ais2);
+
+		ParsedQuery pq = sp.parseQuery(queryString, null);
+		TupleExpr tup = pq.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(index, false);
+		pcj.optimize(tup, null, null);
+
+		System.out.println("TupleExpr is " + tup);
+
+		IndexPlanValidator ipv = new IndexPlanValidator(false);
+		Assert.assertEquals(true, ipv.isValid(tup));
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexTwoVarOrder3()
+			throws MalformedQueryException {
+
+		System.out
+				.println("********************Test number 3***************************");
+
+		String indexSparqlString = ""//
+				+ "SELECT ?l ?e ?c  " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		String indexSparqlString2 = ""//
+				+ "SELECT ?e ?o ?l " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		String queryString = ""//
+				+ "SELECT ?e ?c ?l ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		SPARQLParser sp = new SPARQLParser();
+		ParsedQuery index1 = sp.parseQuery(indexSparqlString, null);
+		ParsedQuery index2 = sp.parseQuery(indexSparqlString2, null);
+
+		List<ExternalTupleSet> index = Lists.newArrayList();
+
+		SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet(
+				(Projection) index1.getTupleExpr());
+		SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet(
+				(Projection) index2.getTupleExpr());
+
+		index.add(ais1);
+		index.add(ais2);
+
+		ParsedQuery pq = sp.parseQuery(queryString, null);
+		TupleExpr tup = pq.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(index, false);
+		pcj.optimize(tup, null, null);
+
+		System.out.println("TupleExpr is " + tup);
+
+		IndexPlanValidator ipv = new IndexPlanValidator(false);
+		Assert.assertEquals(true, ipv.isValid(tup));
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexTwoVarOrder4()
+			throws MalformedQueryException {
+
+		System.out
+				.println("********************Test number 4***************************");
+
+		String indexSparqlString = ""//
+				+ "SELECT ?e ?c ?l  " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		String indexSparqlString2 = ""//
+				+ "SELECT ?e ?o ?l " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		String queryString = ""//
+				+ "SELECT ?e ?c ?l ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		SPARQLParser sp = new SPARQLParser();
+		ParsedQuery index1 = sp.parseQuery(indexSparqlString, null);
+		ParsedQuery index2 = sp.parseQuery(indexSparqlString2, null);
+
+		List<ExternalTupleSet> index = Lists.newArrayList();
+
+		SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet(
+				(Projection) index1.getTupleExpr());
+		SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet(
+				(Projection) index2.getTupleExpr());
+
+		index.add(ais1);
+		index.add(ais2);
+
+		ParsedQuery pq = sp.parseQuery(queryString, null);
+		TupleExpr tup = pq.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(index, false);
+		pcj.optimize(tup, null, null);
+
+		System.out.println("TupleExpr is " + tup);
+
+		IndexPlanValidator ipv = new IndexPlanValidator(false);
+		Assert.assertEquals(false, ipv.isValid(tup));
+
+	}
+
+
+	@Test
+	public void testEvaluateTwoIndexTwoVarOrder6()
+			throws MalformedQueryException {
+
+		System.out
+				.println("********************Test number 6***************************");
+
+		String indexSparqlString = ""//
+				+ "SELECT ?e ?l ?c  " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		String indexSparqlString2 = ""//
+				+ "SELECT ?l ?e ?o " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		String queryString = ""//
+				+ "SELECT ?e ?c ?l ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		SPARQLParser sp = new SPARQLParser();
+		ParsedQuery index1 = sp.parseQuery(indexSparqlString, null);
+		ParsedQuery index2 = sp.parseQuery(indexSparqlString2, null);
+
+		List<ExternalTupleSet> index = Lists.newArrayList();
+
+		SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet(
+				(Projection) index1.getTupleExpr());
+		SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet(
+				(Projection) index2.getTupleExpr());
+
+		index.add(ais2);
+		index.add(ais1);
+
+		ParsedQuery pq = sp.parseQuery(queryString, null);
+		TupleExpr tup = pq.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(index, false);
+		pcj.optimize(tup, null, null);
+
+		System.out.println("TupleExpr is " + tup);
+
+		IndexPlanValidator ipv = new IndexPlanValidator(false);
+		Assert.assertEquals(true, ipv.isValid(tup));
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexCrossProduct1()
+			throws MalformedQueryException {
+
+		System.out
+				.println("********************Test number 7***************************");
+
+		String indexSparqlString = ""//
+				+ "SELECT ?e ?l ?c  " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		String indexSparqlString2 = ""//
+				+ "SELECT ?e ?l ?o " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		String queryString = ""//
+				+ "SELECT ?e ?c ?l ?o ?f ?g " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?f <uri:talksTo> ?g . " //
+				+ "}";//
+
+		SPARQLParser sp = new SPARQLParser();
+		ParsedQuery index1 = sp.parseQuery(indexSparqlString, null);
+		ParsedQuery index2 = sp.parseQuery(indexSparqlString2, null);
+		List<ExternalTupleSet> index = Lists.newArrayList();
+
+		SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet(
+				(Projection) index1.getTupleExpr());
+		SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet(
+				(Projection) index2.getTupleExpr());
+
+		index.add(ais2);
+		index.add(ais1);
+
+		ParsedQuery pq = sp.parseQuery(queryString, null);
+		TupleExpr tup = pq.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(index, false);
+		pcj.optimize(tup, null, null);
+
+		IndexPlanValidator ipv = new IndexPlanValidator(true);
+		Assert.assertEquals(false, ipv.isValid(tup));
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexCrossProduct2()
+			throws MalformedQueryException {
+
+		System.out
+				.println("********************Test number 8***************************");
+
+		String indexSparqlString = ""//
+				+ "SELECT ?e ?l ?c  " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		String indexSparqlString2 = ""//
+				+ "SELECT ?e ?l ?o " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		String queryString = ""//
+				+ "SELECT ?e ?c ?l ?o ?f ?g " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?f <uri:talksTo> ?g . " //
+				+ "}";//
+
+		SPARQLParser sp = new SPARQLParser();
+		ParsedQuery index1 = sp.parseQuery(indexSparqlString, null);
+		ParsedQuery index2 = sp.parseQuery(indexSparqlString2, null);
+
+		List<ExternalTupleSet> index = Lists.newArrayList();
+
+		SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet(
+				(Projection) index1.getTupleExpr());
+		SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet(
+				(Projection) index2.getTupleExpr());
+
+		index.add(ais1);
+		index.add(ais2);
+
+		ParsedQuery pq = sp.parseQuery(queryString, null);
+		TupleExpr tup = pq.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(index, false);
+		pcj.optimize(tup, null, null);
+
+		IndexPlanValidator ipv = new IndexPlanValidator(true);
+		Assert.assertEquals(false, ipv.isValid(tup));
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexCrossProduct3()
+			throws MalformedQueryException {
+
+		System.out
+				.println("********************Test number 9***************************");
+
+		String indexSparqlString = ""//
+				+ "SELECT ?e ?l ?c  " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		String indexSparqlString2 = ""//
+				+ "SELECT ?e ?l ?o " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		String queryString = ""//
+				+ "SELECT ?e ?c ?l ?o ?f ?g " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?f <uri:talksTo> ?g . " //
+				+ "}";//
+
+		SPARQLParser sp = new SPARQLParser();
+		ParsedQuery index1 = sp.parseQuery(indexSparqlString, null);
+		ParsedQuery index2 = sp.parseQuery(indexSparqlString2, null);
+
+		List<ExternalTupleSet> index = Lists.newArrayList();
+
+		SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet(
+				(Projection) index1.getTupleExpr());
+		SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet(
+				(Projection) index2.getTupleExpr());
+
+		index.add(ais1);
+		index.add(ais2);
+
+		ParsedQuery pq = sp.parseQuery(queryString, null);
+		TupleExpr tup = pq.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(index, false);
+		pcj.optimize(tup, null, null);
+
+		IndexPlanValidator ipv = new IndexPlanValidator(false);
+		Assert.assertEquals(true, ipv.isValid(tup));
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexDiffVars() throws MalformedQueryException {
+
+		System.out
+				.println("********************Test number 10***************************");
+
+		String indexSparqlString = ""//
+				+ "SELECT ?chicken ?dog ?pig  " //
+				+ "{" //
+				+ "  ?dog a ?chicken . "//
+				+ "  ?dog <http://www.w3.org/2000/01/rdf-schema#label> ?pig "//
+				+ "}";//
+
+		String indexSparqlString2 = ""//
+				+ "SELECT ?fish ?ant ?turkey " //
+				+ "{" //
+				+ "  ?fish <uri:talksTo> ?turkey . "//
+				+ "  ?turkey <http://www.w3.org/2000/01/rdf-schema#label> ?ant "//
+				+ "}";//
+
+		String queryString = ""//
+				+ "SELECT ?e ?c ?l ?o ?f ?g " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?f <uri:talksTo> ?g . " //
+				+ "}";//
+
+		SPARQLParser sp = new SPARQLParser();
+		ParsedQuery index1 = sp.parseQuery(indexSparqlString, null);
+		ParsedQuery index2 = sp.parseQuery(indexSparqlString2, null);
+
+		List<ExternalTupleSet> index = Lists.newArrayList();
+
+		SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet(
+				(Projection) index1.getTupleExpr());
+		SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet(
+				(Projection) index2.getTupleExpr());
+
+		index.add(ais1);
+		index.add(ais2);
+
+		ParsedQuery pq = sp.parseQuery(queryString, null);
+		TupleExpr tup = pq.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(index, false);
+		pcj.optimize(tup, null, null);
+
+		IndexPlanValidator ipv = new IndexPlanValidator(false);
+		Assert.assertEquals(false, ipv.isValid(tup));
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexDiffVars2() throws MalformedQueryException {
+
+		System.out
+				.println("********************Test number 11***************************");
+
+		String indexSparqlString = ""//
+				+ "SELECT ?dog ?pig ?chicken  " //
+				+ "{" //
+				+ "  ?dog a ?chicken . "//
+				+ "  ?dog <http://www.w3.org/2000/01/rdf-schema#label> ?pig "//
+				+ "}";//
+
+		String indexSparqlString2 = ""//
+				+ "SELECT ?fish ?ant ?turkey " //
+				+ "{" //
+				+ "  ?fish <uri:talksTo> ?turkey . "//
+				+ "  ?turkey <http://www.w3.org/2000/01/rdf-schema#label> ?ant "//
+				+ "}";//
+
+		String queryString = ""//
+				+ "SELECT ?e ?c ?l ?o ?f ?g " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?f <uri:talksTo> ?g . " //
+				+ "}";//
+
+		SPARQLParser sp = new SPARQLParser();
+		ParsedQuery index1 = sp.parseQuery(indexSparqlString, null);
+		ParsedQuery index2 = sp.parseQuery(indexSparqlString2, null);
+
+		List<ExternalTupleSet> index = Lists.newArrayList();
+
+		SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet(
+				(Projection) index1.getTupleExpr());
+		SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet(
+				(Projection) index2.getTupleExpr());
+
+		index.add(ais1);
+		index.add(ais2);
+
+		ParsedQuery pq = sp.parseQuery(queryString, null);
+		TupleExpr tup = pq.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(index, false);
+		pcj.optimize(tup, null, null);
+
+		IndexPlanValidator ipv = new IndexPlanValidator(false);
+		Assert.assertEquals(true, ipv.isValid(tup));
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexDiffVars3() throws MalformedQueryException {
+
+		System.out
+				.println("********************Test number 11***************************");
+
+		String indexSparqlString = ""//
+				+ "SELECT ?pig ?dog ?chicken  " //
+				+ "{" //
+				+ "  ?dog a ?chicken . "//
+				+ "  ?dog <http://www.w3.org/2000/01/rdf-schema#label> ?pig "//
+				+ "}";//
+
+		String indexSparqlString2 = ""//
+				+ "SELECT ?fish ?ant ?turkey " //
+				+ "{" //
+				+ "  ?fish <uri:talksTo> ?turkey . "//
+				+ "  ?turkey <http://www.w3.org/2000/01/rdf-schema#label> ?ant "//
+				+ "}";//
+
+		String queryString = ""//
+				+ "SELECT ?e ?c ?l ?o ?f ?g " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?f <uri:talksTo> ?g . " //
+				+ "}";//
+
+		SPARQLParser sp = new SPARQLParser();
+		ParsedQuery index1 = sp.parseQuery(indexSparqlString, null);
+		ParsedQuery index2 = sp.parseQuery(indexSparqlString2, null);
+
+		List<ExternalTupleSet> index = Lists.newArrayList();
+
+		SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet(
+				(Projection) index1.getTupleExpr());
+		SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet(
+				(Projection) index2.getTupleExpr());
+
+		index.add(ais1);
+		index.add(ais2);
+
+		ParsedQuery pq = sp.parseQuery(queryString, null);
+		TupleExpr tup = pq.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(index, false);
+		pcj.optimize(tup, null, null);
+
+		IndexPlanValidator ipv = new IndexPlanValidator(false);
+		Assert.assertEquals(true, ipv.isValid(tup));
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexDiffVarsDirProd()
+			throws MalformedQueryException {
+
+		System.out
+				.println("********************Test number 12***************************");
+
+		String indexSparqlString = ""//
+				+ "SELECT ?pig ?dog ?chicken  " //
+				+ "{" //
+				+ "  ?dog a ?chicken . "//
+				+ "  ?dog <http://www.w3.org/2000/01/rdf-schema#label> ?pig "//
+				+ "}";//
+
+		String indexSparqlString2 = ""//
+				+ "SELECT ?fish ?ant ?turkey " //
+				+ "{" //
+				+ "  ?fish <uri:talksTo> ?turkey . "//
+				+ "  ?turkey <http://www.w3.org/2000/01/rdf-schema#label> ?ant "//
+				+ "}";//
+
+		String queryString = ""//
+				+ "SELECT ?e ?c ?l ?o ?f ?g " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?f <uri:talksTo> ?g . " //
+				+ "}";//
+
+		SPARQLParser sp = new SPARQLParser();
+		ParsedQuery index1 = sp.parseQuery(indexSparqlString, null);
+		ParsedQuery index2 = sp.parseQuery(indexSparqlString2, null);
+
+		List<ExternalTupleSet> index = Lists.newArrayList();
+
+		SimpleExternalTupleSet ais1 = new SimpleExternalTupleSet(
+				(Projection) index1.getTupleExpr());
+		SimpleExternalTupleSet ais2 = new SimpleExternalTupleSet(
+				(Projection) index2.getTupleExpr());
+
+		index.add(ais1);
+		index.add(ais2);
+
+		ParsedQuery pq = sp.parseQuery(queryString, null);
+		TupleExpr tup = pq.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(index, false);
+		pcj.optimize(tup, null, null);
+
+		IndexPlanValidator ipv = new IndexPlanValidator(true);
+		Assert.assertEquals(false, ipv.isValid(tup));
+
+	}
+
+	@Test
+	public void testValidTupleIterator() throws Exception {
+
+		System.out
+				.println("********************Test number 13***************************");
+
+		String q1 = ""//
+				+ "SELECT ?f ?m ?d ?h ?i " //
+				+ "{" //
+				+ "  ?f a ?m ."//
+				+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+				+ "  ?d <uri:talksTo> ?f . "//
+				+ "  ?d <uri:hangOutWith> ?f ." //
+				+ "  ?f <uri:hangOutWith> ?h ." //
+				+ "  ?f <uri:associatesWith> ?i ." //
+				+ "  ?i <uri:associatesWith> ?h ." //
+				+ "}";//
+
+		String q2 = ""//
+				+ "SELECT ?t ?s ?u " //
+				+ "{" //
+				+ "  ?s a ?t ."//
+				+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+				+ "  ?u <uri:talksTo> ?s . "//
+				+ "}";//
+
+		String q3 = ""//
+				+ "SELECT ?s ?t ?u " //
+				+ "{" //
+				+ "  ?s <uri:hangOutWith> ?t ." //
+				+ "  ?t <uri:hangOutWith> ?u ." //
+				+ "}";//
+
+		String q4 = ""//
+				+ "SELECT ?s ?t ?u " //
+				+ "{" //
+				+ "  ?s <uri:associatesWith> ?t ." //
+				+ "  ?t <uri:associatesWith> ?u ." //
+				+ "}";//
+
+		SPARQLParser parser = new SPARQLParser();
+
+		ParsedQuery pq1 = parser.parseQuery(q1, null);
+		ParsedQuery pq2 = parser.parseQuery(q2, null);
+		ParsedQuery pq3 = parser.parseQuery(q3, null);
+		ParsedQuery pq4 = parser.parseQuery(q4, null);
+
+		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				(Projection) pq3.getTupleExpr());
+		SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(
+				(Projection) pq4.getTupleExpr());
+
+		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+
+		list.add(extTup2);
+		list.add(extTup1);
+		list.add(extTup3);
+
+		IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
+				pq1.getTupleExpr(), list);
+
+		Iterator<TupleExpr> plans = new TupleExecutionPlanGenerator()
+				.getPlans(iep.getIndexedTuples());
+		IndexPlanValidator ipv = new IndexPlanValidator(true);
+		Iterator<TupleExpr> validPlans = ipv.getValidTuples(plans);
+
+		int size = 0;
+
+		while (validPlans.hasNext()) {
+			Assert.assertTrue(validPlans.hasNext());
+			validPlans.next();
+			size++;
+		}
+
+		Assert.assertTrue(!validPlans.hasNext());
+		Assert.assertEquals(732, size);
+
+	}
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/IndexedExecutionPlanGeneratorTest.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/IndexedExecutionPlanGeneratorTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/IndexedExecutionPlanGeneratorTest.java
index 79a6656..296a010 100644
--- a/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/IndexedExecutionPlanGeneratorTest.java
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/IndexedExecutionPlanGeneratorTest.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.IndexPlanValidator;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -19,405 +19,350 @@ package mvm.rya.indexing.IndexPlanValidator;
  * under the License.
  */
 
-
-
-
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.NoSuchElementException;
-import java.util.Set;
-import junit.framework.Assert;
-import mvm.rya.indexing.external.ExternalProcessor;
+
 import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
 import mvm.rya.indexing.external.tupleSet.SimpleExternalTupleSet;
 
+import org.junit.Assert;
 import org.junit.Test;
 import org.openrdf.query.algebra.Projection;
 import org.openrdf.query.algebra.TupleExpr;
 import org.openrdf.query.parser.ParsedQuery;
 import org.openrdf.query.parser.sparql.SPARQLParser;
 
-import com.google.common.collect.Lists;
-
 public class IndexedExecutionPlanGeneratorTest {
 
-    private String q7 = ""//
-            + "SELECT ?s ?t ?u " //
-            + "{" //
-            + "  ?s a ?t ."//
-            + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-            + "  ?u <uri:talksTo> ?s . "//
-            + "}";//
-     
-    
-    
-    private String q12 = ""//
-            + "SELECT ?b ?p ?dog ?cat " //
-            + "{" //
-            + "  ?b a ?p ."//
-            + "  ?dog a ?cat. "//
-            + "}";//
-    
-    private String q15 = ""//
-            + "SELECT ?f ?m ?d ?e ?l ?c " //
-            + "{" //
-            + "  ?f a ?m ."//
-            + "  ?e a ?l ."//
-            + "  ?d <uri:talksTo> ?f . "//
-            + "  ?c <uri:talksTo> ?e . "//
-            + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-            + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-            + "}";//
-    
-    private String q16 = ""//
-            + "SELECT ?f ?m ?d ?e ?l ?c " //
-            + "{" //
-            + "  ?l <uri:talksTo> ?c . "//
-            + "  ?d <uri:talksTo> ?f . "//
-            + "  ?c <uri:talksTo> ?e . "//
-            + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-            + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-            + "}";//
-    
-    private String q17 = ""//
-            + "SELECT ?dog ?cat ?chicken " //
-            + "{" //
-            + "  ?chicken <uri:talksTo> ?dog . "//
-            + "  ?cat <http://www.w3.org/2000/01/rdf-schema#label> ?chicken ."//
-            + "}";//
-    
-    private String q18 = ""//
-            + "SELECT ?cat ?chicken ?pig ?duck " //
-            + "{" //
-            + "  ?cat <uri:talksTo> ?chicken. "//
-            + "  ?pig <uri:talksTo> ?duck . "//
-            + "}";//
-    
-   
-    
-    private String q19 = ""//
-            + "SELECT ?f ?m ?d ?e ?l ?c " //
-            + "{" //
-            + "  ?f <uri:talksTo> ?m . "//
-            + "  ?d <uri:talksTo> ?e . "//
-            + "  ?l <uri:talksTo> ?c . "//
-            + "}";//
-    
-    private String q20 = ""//
-            + "SELECT ?f ?m " //
-            + "{" //
-            + "  ?f <uri:talksTo> ?m . "//
-            + "}";//
-    
-    
-    private String q21 = ""//
-            + "SELECT ?s ?t ?u " //
-            + "{" //
-            + " Filter(?s > 3). " //
-            + "  ?s a ?t ."//
-            + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-            + "  ?u <uri:talksTo> ?s . "//
-            + "}";//
-     
-    
-    
-    private String q22 = ""//
-            + "SELECT ?f ?m ?d ?e ?l ?c " //
-            + "{" //
-            + " Filter(?f > 3) ."//
-            + " Filter(?e > 3) ."//
-            + "  ?e a ?f ." //
-            + "  ?f a ?m ."//
-            + "  ?e a ?l ."//
-            + "  ?d <uri:talksTo> ?f . "//
-            + "  ?c <uri:talksTo> ?e . "//
-            + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-            + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-            + "}";//
-    
-    
-    private String q23 = ""//
-            + "SELECT ?h ?i ?j " //
-            + "{" //
-            + " Filter(?h > 3) ."//
-            + " Filter(?i > 3) ."//
-            + "  ?h a ?i ." //
-            + "  ?h a ?j ."//
-            + "}";//
-    
-    
-    
-    
-    
-    
-    @Test
-    public void testTwoIndexLargeQuery() throws Exception {
-
-        SPARQLParser parser = new SPARQLParser();
-
-        ParsedQuery pq1 = parser.parseQuery(q15, null);
-        ParsedQuery pq2 = parser.parseQuery(q7, null);
-        ParsedQuery pq3 = parser.parseQuery(q12, null);
-
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
-
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-
-        list.add(extTup2);
-        list.add(extTup1);
-
-        IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(pq1.getTupleExpr(), list);
-        List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
-        Assert.assertEquals(4, indexSet.size());
-
-        Iterator<TupleExpr> processedTups = iep.getIndexedTuples();
-
-        int size = 0;
-
-        while (processedTups.hasNext()) {
-            Assert.assertTrue(processedTups.hasNext());
-            processedTups.next();
-            size++;
-        }
-        
-        Assert.assertTrue(!processedTups.hasNext());
-
-        Assert.assertEquals(5, size);
-
-    }
-    
-    
-    
-    
-    
-    @Test
-    public void testThreeSingleNodeIndex() throws Exception {
-
-        SPARQLParser parser = new SPARQLParser();
-
-        ParsedQuery pq1 = parser.parseQuery(q19, null);
-        ParsedQuery pq2 = parser.parseQuery(q20, null);
-
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-
-        list.add(extTup1);
-
-        IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(pq1.getTupleExpr(), list);
-        List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
-        Assert.assertEquals(3, indexSet.size());
-
-        Iterator<TupleExpr> processedTups = iep.getIndexedTuples();
-
-        int size = 0;
-
-        while(processedTups.hasNext()) {
-            Assert.assertTrue(processedTups.hasNext());
-            processedTups.next();
-            size++;
-        }
-        Assert.assertTrue(!processedTups.hasNext());
-
-        Assert.assertEquals(3, size);
-
-    }
-    
-    
-    
-    @Test
-    public void testThreeIndexQuery() throws Exception {
-
-        SPARQLParser parser = new SPARQLParser();
-        
-
-        ParsedQuery pq1 = parser.parseQuery(q16, null);
-        ParsedQuery pq2 = parser.parseQuery(q17, null);
-        ParsedQuery pq3 = parser.parseQuery(q18, null);
-
-        
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
-      
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-       
-        list.add(extTup2);
-        list.add(extTup1);
-        
-
-        IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(pq1.getTupleExpr(),list);
-        List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
-        Assert.assertEquals(6, indexSet.size());
-
-        Iterator<TupleExpr> processedTups = iep.getIndexedTuples();
-        
-        int size = 0;
-        
-        while(processedTups.hasNext()) {
-            Assert.assertTrue(processedTups.hasNext());
-            processedTups.next();
-            size++;
-        }
-        
-        Assert.assertTrue(!processedTups.hasNext());
-        Assert.assertEquals(9, size);
-        
-
-    }
-    
-    
-    
-    
-    @Test
-    public void testThrowsException1() throws Exception {
-
-        SPARQLParser parser = new SPARQLParser();
-        
-
-        ParsedQuery pq1 = parser.parseQuery(q16, null);
-        ParsedQuery pq2 = parser.parseQuery(q17, null);
-        ParsedQuery pq3 = parser.parseQuery(q18, null);
-
-        
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
-      
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-       
-        list.add(extTup2);
-        list.add(extTup1);
-        
-
-        IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(pq1.getTupleExpr(),list);
-        List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
-        Assert.assertEquals(6, indexSet.size());
-
-        Iterator<TupleExpr> processedTups = iep.getIndexedTuples();
-        
-     
-        boolean exceptionThrown = false;
-        
-        try{
-            processedTups.remove();
-        } catch(UnsupportedOperationException e) {
-            exceptionThrown = true;
-        }
-        
-        Assert.assertTrue(exceptionThrown);
-        
-        
-    }
-    
-    
-    @Test
-    public void testThrowsException2() throws Exception {
-
-        SPARQLParser parser = new SPARQLParser();
-
-        ParsedQuery pq1 = parser.parseQuery(q19, null);
-        ParsedQuery pq2 = parser.parseQuery(q20, null);
-
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-
-        list.add(extTup1);
-
-        IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(pq1.getTupleExpr(), list);
-        List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
-        Assert.assertEquals(3, indexSet.size());
-
-        Iterator<TupleExpr> processedTups = iep.getIndexedTuples();
-
-        int size = 0;
-
-       processedTups.next();
-       processedTups.next();
-       processedTups.next();
-
-        boolean exceptionThrown = false;
-        try {
-            processedTups.next();
-        } catch (NoSuchElementException e) {
-            exceptionThrown = true;
-        }
-
-        Assert.assertTrue(exceptionThrown);
-
-    }
-
-    
-    
-    
-    
-    @Test
-    public void testThreeIndexQueryFilter() throws Exception {
-
-        SPARQLParser parser = new SPARQLParser();
-        
-
-        ParsedQuery pq1 = parser.parseQuery(q22, null);
-        ParsedQuery pq2 = parser.parseQuery(q7, null);
-        ParsedQuery pq3 = parser.parseQuery(q21, null);
-        ParsedQuery pq4 = parser.parseQuery(q23, null);
-        
-        
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
-        SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
-      
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-       
-        list.add(extTup2);
-        list.add(extTup1);
-        list.add(extTup3);
-        
-
-        IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(pq1.getTupleExpr(),list);
-        List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
-        Assert.assertEquals(5, indexSet.size());
-
-        Iterator<TupleExpr> processedTups = iep.getIndexedTuples();
-        
-        
-        int size = 0;
-        
-        while(processedTups.hasNext()) {
-            Assert.assertTrue(processedTups.hasNext());
-            TupleExpr te = processedTups.next();
-            System.out.println(te);
-            size++;
-        }
-        
-        Assert.assertTrue(!processedTups.hasNext());
-        Assert.assertEquals(10, size);
-        
-
-    }
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
+	private String q7 = ""//
+			+ "SELECT ?s ?t ?u " //
+			+ "{" //
+			+ "  ?s a ?t ."//
+			+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+			+ "  ?u <uri:talksTo> ?s . "//
+			+ "}";//
+
+	private String q12 = ""//
+			+ "SELECT ?b ?p ?dog ?cat " //
+			+ "{" //
+			+ "  ?b a ?p ."//
+			+ "  ?dog a ?cat. "//
+			+ "}";//
+
+	private String q15 = ""//
+			+ "SELECT ?f ?m ?d ?e ?l ?c " //
+			+ "{" //
+			+ "  ?f a ?m ."//
+			+ "  ?e a ?l ."//
+			+ "  ?d <uri:talksTo> ?f . "//
+			+ "  ?c <uri:talksTo> ?e . "//
+			+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+			+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+			+ "}";//
+
+	private String q16 = ""//
+			+ "SELECT ?f ?m ?d ?e ?l ?c " //
+			+ "{" //
+			+ "  ?l <uri:talksTo> ?c . "//
+			+ "  ?d <uri:talksTo> ?f . "//
+			+ "  ?c <uri:talksTo> ?e . "//
+			+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+			+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+			+ "}";//
+
+	private String q17 = ""//
+			+ "SELECT ?dog ?cat ?chicken " //
+			+ "{" //
+			+ "  ?chicken <uri:talksTo> ?dog . "//
+			+ "  ?cat <http://www.w3.org/2000/01/rdf-schema#label> ?chicken ."//
+			+ "}";//
+
+	private String q18 = ""//
+			+ "SELECT ?cat ?chicken ?pig ?duck " //
+			+ "{" //
+			+ "  ?cat <uri:talksTo> ?chicken. "//
+			+ "  ?pig <uri:talksTo> ?duck . "//
+			+ "}";//
+
+	private String q19 = ""//
+			+ "SELECT ?f ?m ?d ?e ?l ?c " //
+			+ "{" //
+			+ "  ?f <uri:talksTo> ?m . "//
+			+ "  ?d <uri:talksTo> ?e . "//
+			+ "  ?l <uri:talksTo> ?c . "//
+			+ "}";//
+
+	private String q20 = ""//
+			+ "SELECT ?f ?m " //
+			+ "{" //
+			+ "  ?f <uri:talksTo> ?m . "//
+			+ "}";//
+
+	private String q21 = ""//
+			+ "SELECT ?s ?t ?u " //
+			+ "{" //
+			+ " Filter(?s > 3). " //
+			+ "  ?s a ?t ."//
+			+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+			+ "  ?u <uri:talksTo> ?s . "//
+			+ "}";//
+
+	private String q22 = ""//
+			+ "SELECT ?f ?m ?d ?e ?l ?c " //
+			+ "{" //
+			+ " Filter(?f > 3) ."//
+			+ " Filter(?e > 3) ."//
+			+ "  ?e a ?f ." //
+			+ "  ?f a ?m ."//
+			+ "  ?e a ?l ."//
+			+ "  ?d <uri:talksTo> ?f . "//
+			+ "  ?c <uri:talksTo> ?e . "//
+			+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+			+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+			+ "}";//
+
+	private String q23 = ""//
+			+ "SELECT ?h ?i ?j " //
+			+ "{" //
+			+ " Filter(?h > 3) ."//
+			+ " Filter(?i > 3) ."//
+			+ "  ?h a ?i ." //
+			+ "  ?h a ?j ."//
+			+ "}";//
+
+	@Test
+	public void testTwoIndexLargeQuery() throws Exception {
+
+		SPARQLParser parser = new SPARQLParser();
+
+		ParsedQuery pq1 = parser.parseQuery(q15, null);
+		ParsedQuery pq2 = parser.parseQuery(q7, null);
+		ParsedQuery pq3 = parser.parseQuery(q12, null);
+
+		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				(Projection) pq3.getTupleExpr());
+
+		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+
+		list.add(extTup2);
+		list.add(extTup1);
+
+		IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
+				pq1.getTupleExpr(), list);
+		List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
+		Assert.assertEquals(4, indexSet.size());
+
+		Iterator<TupleExpr> processedTups = iep.getIndexedTuples();
+
+		int size = 0;
+
+		while (processedTups.hasNext()) {
+			Assert.assertTrue(processedTups.hasNext());
+			processedTups.next();
+			size++;
+		}
+
+		Assert.assertTrue(!processedTups.hasNext());
+
+		Assert.assertEquals(5, size);
+
+	}
+
+	@Test
+	public void testThreeSingleNodeIndex() throws Exception {
+
+		SPARQLParser parser = new SPARQLParser();
+
+		ParsedQuery pq1 = parser.parseQuery(q19, null);
+		ParsedQuery pq2 = parser.parseQuery(q20, null);
+
+		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+
+		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+
+		list.add(extTup1);
+
+		IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
+				pq1.getTupleExpr(), list);
+		List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
+		Assert.assertEquals(3, indexSet.size());
+
+		Iterator<TupleExpr> processedTups = iep.getIndexedTuples();
+
+		int size = 0;
+
+		while (processedTups.hasNext()) {
+			Assert.assertTrue(processedTups.hasNext());
+			processedTups.next();
+			size++;
+		}
+		Assert.assertTrue(!processedTups.hasNext());
+
+		Assert.assertEquals(3, size);
+
+	}
+
+	@Test
+	public void testThreeIndexQuery() throws Exception {
+
+		SPARQLParser parser = new SPARQLParser();
+
+		ParsedQuery pq1 = parser.parseQuery(q16, null);
+		ParsedQuery pq2 = parser.parseQuery(q17, null);
+		ParsedQuery pq3 = parser.parseQuery(q18, null);
+
+		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				(Projection) pq3.getTupleExpr());
+
+		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+
+		list.add(extTup2);
+		list.add(extTup1);
+
+		IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
+				pq1.getTupleExpr(), list);
+		List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
+		Assert.assertEquals(6, indexSet.size());
+
+		Iterator<TupleExpr> processedTups = iep.getIndexedTuples();
+
+		int size = 0;
+
+		while (processedTups.hasNext()) {
+			Assert.assertTrue(processedTups.hasNext());
+			processedTups.next();
+			size++;
+		}
+
+		Assert.assertTrue(!processedTups.hasNext());
+		Assert.assertEquals(9, size);
+
+	}
+
+	@Test
+	public void testThrowsException1() throws Exception {
+
+		SPARQLParser parser = new SPARQLParser();
+
+		ParsedQuery pq1 = parser.parseQuery(q16, null);
+		ParsedQuery pq2 = parser.parseQuery(q17, null);
+		ParsedQuery pq3 = parser.parseQuery(q18, null);
+
+		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				(Projection) pq3.getTupleExpr());
+
+		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+
+		list.add(extTup2);
+		list.add(extTup1);
+
+		IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
+				pq1.getTupleExpr(), list);
+		List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
+		Assert.assertEquals(6, indexSet.size());
+
+		Iterator<TupleExpr> processedTups = iep.getIndexedTuples();
+
+		boolean exceptionThrown = false;
+
+		try {
+			processedTups.remove();
+		} catch (UnsupportedOperationException e) {
+			exceptionThrown = true;
+		}
+
+		Assert.assertTrue(exceptionThrown);
+
+	}
+
+	@Test
+	public void testThrowsException2() throws Exception {
+
+		SPARQLParser parser = new SPARQLParser();
+
+		ParsedQuery pq1 = parser.parseQuery(q19, null);
+		ParsedQuery pq2 = parser.parseQuery(q20, null);
+
+		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+
+		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+
+		list.add(extTup1);
+
+		IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
+				pq1.getTupleExpr(), list);
+		List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
+		Assert.assertEquals(3, indexSet.size());
+
+		Iterator<TupleExpr> processedTups = iep.getIndexedTuples();
+
+		processedTups.next();
+		processedTups.next();
+		processedTups.next();
+
+		boolean exceptionThrown = false;
+		try {
+			processedTups.next();
+		} catch (NoSuchElementException e) {
+			exceptionThrown = true;
+		}
+
+		Assert.assertTrue(exceptionThrown);
+
+	}
+
+	@Test
+	public void testThreeIndexQueryFilter() throws Exception {
+
+		SPARQLParser parser = new SPARQLParser();
+
+		ParsedQuery pq1 = parser.parseQuery(q22, null);
+		ParsedQuery pq2 = parser.parseQuery(q7, null);
+		ParsedQuery pq3 = parser.parseQuery(q21, null);
+		ParsedQuery pq4 = parser.parseQuery(q23, null);
+
+		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				(Projection) pq3.getTupleExpr());
+		SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(
+				(Projection) pq4.getTupleExpr());
+
+		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+
+		list.add(extTup2);
+		list.add(extTup1);
+		list.add(extTup3);
+
+		IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
+				pq1.getTupleExpr(), list);
+		List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
+		Assert.assertEquals(5, indexSet.size());
+
+		Iterator<TupleExpr> processedTups = iep.getIndexedTuples();
+
+		int size = 0;
+
+		while (processedTups.hasNext()) {
+			Assert.assertTrue(processedTups.hasNext());
+			TupleExpr te = processedTups.next();
+			System.out.println(te);
+			size++;
+		}
+
+		Assert.assertTrue(!processedTups.hasNext());
+		Assert.assertEquals(10, size);
 
+	}
 
 }


[06/16] incubator-rya git commit: RYA-32 Improve how metadata and values are written to Accumulo PCJ tables

Posted by mi...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloIndexSetTest2.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloIndexSetTest2.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloIndexSetTest2.java
deleted file mode 100644
index f93f58a..0000000
--- a/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloIndexSetTest2.java
+++ /dev/null
@@ -1,803 +0,0 @@
-package mvm.rya.indexing.external;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-import java.util.Arrays;
-import java.util.List;
-
-import junit.framework.Assert;
-import mvm.rya.accumulo.AccumuloRdfConfiguration;
-import mvm.rya.api.persist.RyaDAOException;
-import mvm.rya.indexing.RyaSailFactory;
-import mvm.rya.indexing.accumulo.ConfigUtils;
-import mvm.rya.indexing.external.tupleSet.AccumuloIndexSet;
-
-import org.apache.accumulo.core.client.AccumuloException;
-import org.apache.accumulo.core.client.AccumuloSecurityException;
-import org.apache.accumulo.core.client.Connector;
-import org.apache.accumulo.core.client.MutationsRejectedException;
-import org.apache.accumulo.core.client.TableExistsException;
-import org.apache.accumulo.core.client.TableNotFoundException;
-import org.apache.accumulo.core.client.mock.MockInstance;
-import org.apache.accumulo.core.client.security.tokens.PasswordToken;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.openrdf.model.URI;
-import org.openrdf.model.impl.LiteralImpl;
-import org.openrdf.model.impl.URIImpl;
-import org.openrdf.model.vocabulary.RDF;
-import org.openrdf.model.vocabulary.RDFS;
-import org.openrdf.query.BindingSet;
-import org.openrdf.query.MalformedQueryException;
-import org.openrdf.query.QueryEvaluationException;
-import org.openrdf.query.QueryLanguage;
-import org.openrdf.query.QueryResultHandlerException;
-import org.openrdf.query.TupleQueryResultHandler;
-import org.openrdf.query.TupleQueryResultHandlerException;
-import org.openrdf.repository.RepositoryException;
-import org.openrdf.repository.sail.SailRepository;
-import org.openrdf.repository.sail.SailRepositoryConnection;
-import org.openrdf.sail.Sail;
-import org.openrdf.sail.SailException;
-
-public class AccumuloIndexSetTest2 {
-
-    private SailRepositoryConnection conn;
-    private SailRepository repo;
-    private Connector accCon;
-    String tablePrefix = "table_";
-    AccumuloRdfConfiguration conf;
-    URI sub, sub2, obj, obj2, subclass, subclass2, talksTo;
-
-    @Before
-    public void init() throws RepositoryException, TupleQueryResultHandlerException, QueryEvaluationException,
-            MalformedQueryException, AccumuloException, AccumuloSecurityException, TableExistsException,
-            RyaDAOException {
-
-        conf = new AccumuloRdfConfiguration();
-        conf.set(ConfigUtils.USE_PCJ, "true");
-        conf.set(ConfigUtils.USE_MOCK_INSTANCE, "true");
-        conf.set(ConfigUtils.CLOUDBASE_INSTANCE, "instance");
-        conf.setTablePrefix(tablePrefix);
-        conf.setPcjTables(Arrays.asList("table1", "table2"));
-
-        Sail sail = RyaSailFactory.getInstance(conf);
-        repo = new SailRepository(sail);
-        repo.initialize();
-        conn = repo.getConnection();
-
-        sub = new URIImpl("uri:entity");
-        subclass = new URIImpl("uri:class");
-        obj = new URIImpl("uri:obj");
-        talksTo = new URIImpl("uri:talksTo");
-
-        conn.add(sub, RDF.TYPE, subclass);
-        conn.add(sub, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(sub, talksTo, obj);
-
-        sub2 = new URIImpl("uri:entity2");
-        subclass2 = new URIImpl("uri:class2");
-        obj2 = new URIImpl("uri:obj2");
-
-        conn.add(sub2, RDF.TYPE, subclass2);
-        conn.add(sub2, RDFS.LABEL, new LiteralImpl("label2"));
-        conn.add(sub2, talksTo, obj2);
-
-        accCon = new MockInstance("instance").getConnector("root", new PasswordToken("".getBytes()));
-        accCon.tableOperations().create("table1");
-        accCon.tableOperations().create("table2");
-
-    }
-
-    @After
-    public void close() throws RepositoryException, AccumuloException, AccumuloSecurityException,
-            TableNotFoundException {
-
-        conf = null;
-        conn.close();
-        accCon.tableOperations().delete(tablePrefix + "spo");
-        accCon.tableOperations().delete(tablePrefix + "po");
-        accCon.tableOperations().delete(tablePrefix + "osp");
-
-        if (accCon.tableOperations().exists("table1")) {
-            accCon.tableOperations().delete("table1");
-        }
-
-        if (accCon.tableOperations().exists("table2")) {
-            accCon.tableOperations().delete("table2");
-        }
-
-    }
-
-    @Test
-    public void testEvaluateTwoIndexTwoVarOrder2() throws RepositoryException, MalformedQueryException, SailException,
-            QueryEvaluationException, MutationsRejectedException, TableNotFoundException,
-            TupleQueryResultHandlerException {
-
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-
-        String indexSparqlString = ""//
-                + "SELECT ?c ?e ?l  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        String indexSparqlString2 = ""//
-                + "SELECT ?e ?o ?l " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1");
-        AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-
-        CountingResultHandler crh = new CountingResultHandler();
-
-        conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh);
-
-        Assert.assertEquals(2, crh.getCount());
-
-    }
-
-    @Test
-    public void testEvaluateTwoIndexTwoVarInvalidOrder() throws RepositoryException, MalformedQueryException,
-            SailException, QueryEvaluationException, MutationsRejectedException, TableNotFoundException,
-            TupleQueryResultHandlerException {
-
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-
-        String indexSparqlString = ""//
-                + "SELECT ?e ?c ?l  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        String indexSparqlString2 = ""//
-                + "SELECT ?e ?o ?l " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1");
-        AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-
-        CountingResultHandler crh = new CountingResultHandler();
-
-        conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh);
-
-        Assert.assertEquals(2, crh.getCount());
-
-    }
-
-    @Test
-    public void testEvaluateTwoIndexThreeVarOrder1() throws MalformedQueryException, SailException,
-            QueryEvaluationException, MutationsRejectedException, TableNotFoundException, RepositoryException,
-            TupleQueryResultHandlerException {
-
-        URI superclass = new URIImpl("uri:superclass");
-        URI superclass2 = new URIImpl("uri:superclass2");
-
-        conn.add(subclass, RDF.TYPE, superclass);
-        conn.add(subclass2, RDF.TYPE, superclass2);
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-
-        String indexSparqlString = ""//
-                + "SELECT ?c ?e ?l  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        String indexSparqlString2 = ""//
-                + "SELECT ?e ?c ?l ?f ?o" //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?f ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1");
-        AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-
-        CountingResultHandler crh = new CountingResultHandler();
-
-        conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh);
-
-        Assert.assertEquals(2, crh.getCount());
-
-    }
-
-    // @Test
-    public void testEvaluateTwoIndexThreeVarsDiffLabel() throws RepositoryException, MalformedQueryException,
-            SailException, QueryEvaluationException, MutationsRejectedException, TableNotFoundException,
-            TupleQueryResultHandlerException {
-
-        URI superclass = new URIImpl("uri:superclass");
-        URI superclass2 = new URIImpl("uri:superclass2");
-        conn.add(subclass, RDF.TYPE, superclass);
-        conn.add(subclass2, RDF.TYPE, superclass2);
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?dog ?pig ?owl  " //
-                + "{" //
-                + "  ?pig a ?dog . "//
-                + "  ?pig <http://www.w3.org/2000/01/rdf-schema#label> ?owl "//
-                + "}";//
-
-        String indexSparqlString2 = ""//
-                + "SELECT ?e ?c ?l ?f ?o" //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?f ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1");
-        AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-
-        CountingResultHandler crh = new CountingResultHandler();
-
-        conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh);
-
-        Assert.assertEquals(2, crh.getCount());
-
-    }
-
-    @Test
-    public void testEvaluateTwoIndexThreeVarOrder2() throws RepositoryException, MalformedQueryException,
-            SailException, QueryEvaluationException, MutationsRejectedException, TableNotFoundException,
-            TupleQueryResultHandlerException {
-
-        URI superclass = new URIImpl("uri:superclass");
-        URI superclass2 = new URIImpl("uri:superclass2");
-
-        conn.add(subclass, RDF.TYPE, superclass);
-        conn.add(subclass2, RDF.TYPE, superclass2);
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?c ?e ?l  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        String indexSparqlString2 = ""//
-                + "SELECT ?o ?f ?e ?c ?l  " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?f ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1");
-        AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-
-        CountingResultHandler crh = new CountingResultHandler();
-
-        conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh);
-
-        Assert.assertEquals(2, crh.getCount());
-
-    }
-
-    @Test
-    public void testEvaluateTwoIndexThreeVarOrder3ThreeBindingSet() throws TupleQueryResultHandlerException,
-            QueryEvaluationException, MalformedQueryException, RepositoryException, SailException,
-            MutationsRejectedException, TableNotFoundException {
-
-        URI sub3 = new URIImpl("uri:entity3");
-        URI subclass3 = new URIImpl("uri:class3");
-        URI obj3 = new URIImpl("uri:obj3");
-
-        URI superclass = new URIImpl("uri:superclass");
-        URI superclass2 = new URIImpl("uri:superclass2");
-        URI superclass3 = new URIImpl("uri:superclass3");
-
-        conn.add(sub3, RDF.TYPE, subclass3);
-        conn.add(sub3, RDFS.LABEL, new LiteralImpl("label3"));
-        conn.add(sub3, talksTo, obj3);
-
-        conn.add(subclass, RDF.TYPE, superclass);
-        conn.add(subclass2, RDF.TYPE, superclass2);
-        conn.add(subclass3, RDF.TYPE, superclass3);
-
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        conn.add(obj3, RDFS.LABEL, new LiteralImpl("label3"));
-
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-
-        String indexSparqlString = ""//
-                + "SELECT ?c ?e ?l  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        String indexSparqlString2 = ""//
-                + "SELECT ?o ?f ?l ?e ?c  " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?f ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1");
-        AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-
-        CountingResultHandler crh = new CountingResultHandler();
-
-        conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh);
-
-        Assert.assertEquals(3, crh.getCount());
-
-    }
-
-    @Test
-    public void testEvaluateTwoIndexThreeVarOrder5ThreeBindingSet() throws MalformedQueryException, SailException,
-            QueryEvaluationException, MutationsRejectedException, TableNotFoundException, RepositoryException,
-            TupleQueryResultHandlerException {
-
-        URI sub3 = new URIImpl("uri:entity3");
-        URI subclass3 = new URIImpl("uri:class3");
-        URI obj3 = new URIImpl("uri:obj3");
-
-        URI superclass = new URIImpl("uri:superclass");
-        URI superclass2 = new URIImpl("uri:superclass2");
-        URI superclass3 = new URIImpl("uri:superclass3");
-
-        conn.add(sub3, RDF.TYPE, subclass3);
-        conn.add(sub3, RDFS.LABEL, new LiteralImpl("label3"));
-        conn.add(sub3, talksTo, obj3);
-
-        conn.add(subclass, RDF.TYPE, superclass);
-        conn.add(subclass2, RDF.TYPE, superclass2);
-        conn.add(subclass3, RDF.TYPE, superclass3);
-
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        conn.add(obj3, RDFS.LABEL, new LiteralImpl("label3"));
-
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?c ?e ?l  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        String indexSparqlString2 = ""//
-                + "SELECT ?o ?f ?e ?l ?c  " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?f ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1");
-        AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-
-        CountingResultHandler crh = new CountingResultHandler();
-
-        conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh);
-
-        Assert.assertEquals(3, crh.getCount());
-
-    }
-
-    @Test
-    public void testEvaluateTwoIndexThreeVarOrder4ThreeBindingSet() throws MalformedQueryException, SailException,
-            QueryEvaluationException, MutationsRejectedException, TableNotFoundException, RepositoryException,
-            TupleQueryResultHandlerException {
-
-        URI sub3 = new URIImpl("uri:entity3");
-        URI subclass3 = new URIImpl("uri:class3");
-        URI obj3 = new URIImpl("uri:obj3");
-
-        URI superclass = new URIImpl("uri:superclass");
-        URI superclass2 = new URIImpl("uri:superclass2");
-        URI superclass3 = new URIImpl("uri:superclass3");
-
-        conn.add(sub3, RDF.TYPE, subclass3);
-        conn.add(sub3, RDFS.LABEL, new LiteralImpl("label3"));
-        conn.add(sub3, talksTo, obj3);
-
-        conn.add(subclass, RDF.TYPE, superclass);
-        conn.add(subclass2, RDF.TYPE, superclass2);
-        conn.add(subclass3, RDF.TYPE, superclass3);
-
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        conn.add(obj3, RDFS.LABEL, new LiteralImpl("label3"));
-
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?c ?e ?l  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        String indexSparqlString2 = ""//
-                + "SELECT ?o ?f ?c ?e ?l  " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?f ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1");
-        AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-
-        CountingResultHandler crh = new CountingResultHandler();
-
-        conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh);
-
-        Assert.assertEquals(3, crh.getCount());
-
-    }
-
-    @Test
-    public void testEvaluateTwoIndexThreeVarOrder6ThreeBindingSet() throws MalformedQueryException, SailException,
-            QueryEvaluationException, MutationsRejectedException, TableNotFoundException, RepositoryException,
-            TupleQueryResultHandlerException {
-
-        URI sub3 = new URIImpl("uri:entity3");
-        URI subclass3 = new URIImpl("uri:class3");
-        URI obj3 = new URIImpl("uri:obj3");
-
-        URI superclass = new URIImpl("uri:superclass");
-        URI superclass2 = new URIImpl("uri:superclass2");
-        URI superclass3 = new URIImpl("uri:superclass3");
-
-        conn.add(sub3, RDF.TYPE, subclass3);
-        conn.add(sub3, RDFS.LABEL, new LiteralImpl("label3"));
-        conn.add(sub3, talksTo, obj3);
-
-        conn.add(subclass, RDF.TYPE, superclass);
-        conn.add(subclass2, RDF.TYPE, superclass2);
-        conn.add(subclass3, RDF.TYPE, superclass3);
-
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        conn.add(obj3, RDFS.LABEL, new LiteralImpl("label3"));
-
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-
-        String indexSparqlString = ""//
-                + "SELECT ?c ?e ?l  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        String indexSparqlString2 = ""//
-                + "SELECT ?c ?l ?e ?o ?f " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?f ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1");
-        AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-
-        CountingResultHandler crh = new CountingResultHandler();
-
-        conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh);
-
-        Assert.assertEquals(3, crh.getCount());
-
-    }
-
-    @Test
-    public void testEvaluateTwoIndexThreeVarOrder7ThreeBindingSet() throws MalformedQueryException, SailException,
-            QueryEvaluationException, MutationsRejectedException, TableNotFoundException, RepositoryException,
-            TupleQueryResultHandlerException {
-
-        URI sub3 = new URIImpl("uri:entity3");
-        URI subclass3 = new URIImpl("uri:class3");
-        URI obj3 = new URIImpl("uri:obj3");
-
-        URI superclass = new URIImpl("uri:superclass");
-        URI superclass2 = new URIImpl("uri:superclass2");
-        URI superclass3 = new URIImpl("uri:superclass3");
-
-        conn.add(sub3, RDF.TYPE, subclass3);
-        conn.add(sub3, RDFS.LABEL, new LiteralImpl("label3"));
-        conn.add(sub3, talksTo, obj3);
-
-        conn.add(subclass, RDF.TYPE, superclass);
-        conn.add(subclass2, RDF.TYPE, superclass2);
-        conn.add(subclass3, RDF.TYPE, superclass3);
-
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        conn.add(obj3, RDFS.LABEL, new LiteralImpl("label3"));
-
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-
-        String indexSparqlString = ""//
-                + "SELECT ?c ?e ?l  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        String indexSparqlString2 = ""//
-                + "SELECT ?o ?l ?c ?e ?f " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?f ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1");
-        AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-
-        CountingResultHandler crh = new CountingResultHandler();
-
-        conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh);
-
-        Assert.assertEquals(3, crh.getCount());
-
-    }
-
-    @Test
-    public void testEvaluateOneIndex() throws RepositoryException, MalformedQueryException, SailException,
-            QueryEvaluationException, MutationsRejectedException, TableNotFoundException,
-            TupleQueryResultHandlerException {
-
-        String indexSparqlString = ""//
-                + "SELECT ?dog ?pig ?duck  " //
-                + "{" //
-                + "  ?pig a ?dog . "//
-                + "  ?pig <http://www.w3.org/2000/01/rdf-schema#label> ?duck "//
-                + "}";//
-
-        AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1");
-
-        CountingResultHandler crh = new CountingResultHandler();
-
-        conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString).evaluate(crh);
-
-        Assert.assertEquals(2, crh.getCount());
-
-    }
-
-    @Test
-    public void testEvaluateTwoIndexThreeVarOrder3() throws RepositoryException, MalformedQueryException,
-            SailException, QueryEvaluationException, MutationsRejectedException, TableNotFoundException,
-            TupleQueryResultHandlerException {
-
-        URI superclass = new URIImpl("uri:superclass");
-        URI superclass2 = new URIImpl("uri:superclass2");
-
-        conn.add(subclass, RDF.TYPE, superclass);
-        conn.add(subclass2, RDF.TYPE, superclass2);
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-
-        conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-
-        String indexSparqlString = ""//
-                + "SELECT ?dog ?pig ?duck  " //
-                + "{" //
-                + "  ?pig a ?dog . "//
-                + "  ?pig <http://www.w3.org/2000/01/rdf-schema#label> ?duck "//
-                + "}";//
-
-        String indexSparqlString2 = ""//
-                + "SELECT ?o ?f ?e ?c ?l  " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?f ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        AccumuloIndexSet ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, "table1");
-        AccumuloIndexSet ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-
-        CountingResultHandler crh = new CountingResultHandler();
-
-        conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh);
-
-        Assert.assertEquals(2, crh.getCount());
-
-    }
-
-    public static class CountingResultHandler implements TupleQueryResultHandler {
-        private int count = 0;
-
-        public int getCount() {
-            return count;
-        }
-
-        public void resetCount() {
-            this.count = 0;
-        }
-
-        @Override
-        public void startQueryResult(List<String> arg0) throws TupleQueryResultHandlerException {
-        }
-
-        @Override
-        public void handleSolution(BindingSet arg0) throws TupleQueryResultHandlerException {
-            count++;
-        }
-
-        @Override
-        public void endQueryResult() throws TupleQueryResultHandlerException {
-        }
-
-        @Override
-        public void handleBoolean(boolean arg0) throws QueryResultHandlerException {
-            // TODO Auto-generated method stub
-
-        }
-
-        @Override
-        public void handleLinks(List<String> arg0) throws QueryResultHandlerException {
-            // TODO Auto-generated method stub
-
-        }
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloPcjIntegrationTest.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloPcjIntegrationTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloPcjIntegrationTest.java
new file mode 100644
index 0000000..a18be2f
--- /dev/null
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloPcjIntegrationTest.java
@@ -0,0 +1,1426 @@
+package mvm.rya.indexing.external;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+
+import mvm.rya.api.persist.RyaDAOException;
+import mvm.rya.indexing.IndexPlanValidator.IndexPlanValidator;
+import mvm.rya.indexing.external.tupleSet.AccumuloIndexSet;
+import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
+import mvm.rya.indexing.external.tupleSet.PcjTables.PcjException;
+import mvm.rya.indexing.external.tupleSet.PcjTables.PcjVarOrderFactory;
+
+import org.apache.accumulo.core.client.AccumuloException;
+import org.apache.accumulo.core.client.AccumuloSecurityException;
+import org.apache.accumulo.core.client.Connector;
+import org.apache.accumulo.core.client.MutationsRejectedException;
+import org.apache.accumulo.core.client.TableExistsException;
+import org.apache.accumulo.core.client.TableNotFoundException;
+import org.apache.accumulo.core.client.admin.TableOperations;
+import org.apache.accumulo.core.client.mock.MockInstance;
+import org.apache.accumulo.core.client.security.tokens.PasswordToken;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.openrdf.model.URI;
+import org.openrdf.model.impl.LiteralImpl;
+import org.openrdf.model.impl.URIImpl;
+import org.openrdf.model.vocabulary.RDF;
+import org.openrdf.model.vocabulary.RDFS;
+import org.openrdf.query.BindingSet;
+import org.openrdf.query.MalformedQueryException;
+import org.openrdf.query.QueryEvaluationException;
+import org.openrdf.query.QueryLanguage;
+import org.openrdf.query.QueryResultHandlerException;
+import org.openrdf.query.TupleQueryResultHandler;
+import org.openrdf.query.TupleQueryResultHandlerException;
+import org.openrdf.query.algebra.TupleExpr;
+import org.openrdf.query.parser.ParsedQuery;
+import org.openrdf.query.parser.sparql.SPARQLParser;
+import org.openrdf.repository.RepositoryException;
+import org.openrdf.repository.sail.SailRepository;
+import org.openrdf.repository.sail.SailRepositoryConnection;
+import org.openrdf.sail.SailException;
+
+import com.beust.jcommander.internal.Sets;
+import com.google.common.base.Optional;
+import com.google.common.collect.Lists;
+
+public class AccumuloPcjIntegrationTest {
+
+	private SailRepositoryConnection conn, pcjConn;
+	private SailRepository repo, pcjRepo;
+	private Connector accCon;
+	private final String prefix = "table_";
+	private final String tablename = "table_INDEX_";
+	private URI obj, obj2, subclass, subclass2, talksTo;
+
+	@Before
+	public void init() throws RepositoryException,
+			TupleQueryResultHandlerException, QueryEvaluationException,
+			MalformedQueryException, AccumuloException,
+			AccumuloSecurityException, TableExistsException, RyaDAOException,
+			TableNotFoundException {
+
+		repo = PcjIntegrationTestingUtil.getNonPcjRepo(prefix, "instance");
+		conn = repo.getConnection();
+
+		pcjRepo = PcjIntegrationTestingUtil.getPcjRepo(prefix, "instance");
+		pcjConn = pcjRepo.getConnection();
+
+		final URI sub = new URIImpl("uri:entity");
+		subclass = new URIImpl("uri:class");
+		obj = new URIImpl("uri:obj");
+		talksTo = new URIImpl("uri:talksTo");
+
+		conn.add(sub, RDF.TYPE, subclass);
+		conn.add(sub, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(sub, talksTo, obj);
+
+		final URI sub2 = new URIImpl("uri:entity2");
+		subclass2 = new URIImpl("uri:class2");
+		obj2 = new URIImpl("uri:obj2");
+
+		conn.add(sub2, RDF.TYPE, subclass2);
+		conn.add(sub2, RDFS.LABEL, new LiteralImpl("label2"));
+		conn.add(sub2, talksTo, obj2);
+
+		accCon = new MockInstance("instance").getConnector("root", new PasswordToken(""));
+
+
+	}
+
+	@After
+	public void close() throws RepositoryException, AccumuloException,
+			AccumuloSecurityException, TableNotFoundException {
+
+		PcjIntegrationTestingUtil.closeAndShutdown(conn, repo);
+		PcjIntegrationTestingUtil.closeAndShutdown(pcjConn, pcjRepo);
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, prefix);
+		PcjIntegrationTestingUtil.deleteIndexTables(accCon, 3, prefix);
+
+	}
+
+	@Test
+	public void testEvaluateSingleIndex()
+			throws TupleQueryResultHandlerException, QueryEvaluationException,
+			MalformedQueryException, RepositoryException, PcjException,
+			SailException, MutationsRejectedException, TableNotFoundException {
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?e ?l ?c " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 1,
+				indexSparqlString, new String[] { "e", "l", "c" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		final String queryString = ""//
+				+ "SELECT ?e ?c ?l ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "}";//
+
+		final CountingResultHandler crh1 = new CountingResultHandler();
+		final CountingResultHandler crh2 = new CountingResultHandler();
+
+		conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString)
+				.evaluate(crh1);
+
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(
+				crh2);
+
+		Assert.assertEquals(crh1.getCount(), crh2.getCount());
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexTwoVarOrder1()
+			throws TupleQueryResultHandlerException, QueryEvaluationException,
+			MalformedQueryException, RepositoryException, AccumuloException,
+			AccumuloSecurityException, TableExistsException, PcjException,
+			SailException, TableNotFoundException {
+
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?e ?l ?c " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?e ?o ?l " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final String queryString = ""//
+				+ "SELECT ?e ?c ?l ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final CountingResultHandler crh1 = new CountingResultHandler();
+		final CountingResultHandler crh2 = new CountingResultHandler();
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 1,
+				indexSparqlString, new String[] { "e", "l", "c" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 2,
+				indexSparqlString2, new String[] { "e", "o", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString)
+				.evaluate(crh1);
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, prefix);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(
+				crh2);
+
+		Assert.assertEquals(2, crh1.getCount());
+		Assert.assertEquals(2, crh2.getCount());
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexTwoVarOrder2() throws PcjException,
+			RepositoryException, TupleQueryResultHandlerException,
+			QueryEvaluationException, MalformedQueryException,
+			AccumuloException, AccumuloSecurityException, TableExistsException,
+			SailException, TableNotFoundException {
+
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?c ?e ?l  " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?e ?o ?l " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final String queryString = ""//
+				+ "SELECT ?e ?c ?l ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final CountingResultHandler crh1 = new CountingResultHandler();
+		final CountingResultHandler crh2 = new CountingResultHandler();
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 1,
+				indexSparqlString, new String[] { "c", "e", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 2,
+				indexSparqlString2, new String[] { "e", "o", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString)
+				.evaluate(crh1);
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, prefix);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(
+				crh2);
+
+		Assert.assertEquals(2, crh1.getCount());
+		Assert.assertEquals(2, crh2.getCount());
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexTwoVarInvalidOrder() throws PcjException,
+			RepositoryException, AccumuloException, AccumuloSecurityException,
+			TableExistsException, TupleQueryResultHandlerException,
+			QueryEvaluationException, MalformedQueryException, SailException,
+			TableNotFoundException {
+
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?e ?c ?l  " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?e ?o ?l " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final String queryString = ""//
+				+ "SELECT ?e ?c ?l ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final CountingResultHandler crh1 = new CountingResultHandler();
+		final CountingResultHandler crh2 = new CountingResultHandler();
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 1,
+				indexSparqlString, new String[] { "e", "c", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 2,
+				indexSparqlString2, new String[] { "e", "o", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString)
+				.evaluate(crh1);
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, prefix);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(
+				crh2);
+
+		Assert.assertEquals(crh1.getCount(), crh2.getCount());
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexThreeVarOrder1()
+			throws TupleQueryResultHandlerException, QueryEvaluationException,
+			MalformedQueryException, RepositoryException, AccumuloException,
+			AccumuloSecurityException, TableExistsException,
+			TableNotFoundException, PcjException, SailException {
+
+		final TableOperations ops = accCon.tableOperations();
+		final Set<String> tables = ops.tableIdMap().keySet();
+		final Collection<String> vals = ops.tableIdMap().values();
+		System.out.println("Tables: " + tables + "and values " + vals);
+
+		final URI superclass = new URIImpl("uri:superclass");
+		final URI superclass2 = new URIImpl("uri:superclass2");
+
+		conn.add(subclass, RDF.TYPE, superclass);
+		conn.add(subclass2, RDF.TYPE, superclass2);
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?c ?e ?l  " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?e ?c ?l ?f ?o" //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final String queryString = ""//
+				+ "SELECT ?e ?c ?l ?f ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final CountingResultHandler crh1 = new CountingResultHandler();
+		final CountingResultHandler crh2 = new CountingResultHandler();
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 1,
+				indexSparqlString, new String[] { "c", "e", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 2,
+				indexSparqlString2, new String[] { "e", "c", "l", "f", "o" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString)
+				.evaluate(crh1);
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, prefix);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString)
+				.evaluate(crh2);
+
+		Assert.assertEquals(2, crh1.getCount());
+		Assert.assertEquals(2, crh2.getCount());
+	}
+
+	 @Test
+	public void testEvaluateTwoIndexThreeVarsDiffLabel() throws PcjException,
+			RepositoryException, AccumuloException, AccumuloSecurityException,
+			TableNotFoundException, TableExistsException,
+			MalformedQueryException, SailException, QueryEvaluationException,
+			TupleQueryResultHandlerException {
+
+		final URI superclass = new URIImpl("uri:superclass");
+		final URI superclass2 = new URIImpl("uri:superclass2");
+
+		conn.add(subclass, RDF.TYPE, superclass);
+		conn.add(subclass2, RDF.TYPE, superclass2);
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?dog ?pig ?owl  " //
+				+ "{" //
+				+ "  ?pig a ?dog . "//
+				+ "  ?pig <http://www.w3.org/2000/01/rdf-schema#label> ?owl "//
+				+ "}";//
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?e ?c ?l ?f ?o" //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final String queryString = ""//
+				+ "SELECT ?e ?c ?l ?f ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final CountingResultHandler crh2 = new CountingResultHandler();
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 1,
+				indexSparqlString, new String[] { "dog", "pig", "owl" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 2,
+				indexSparqlString2, new String[] { "e", "c", "l", "f", "o" },
+				Optional.<PcjVarOrderFactory> absent());
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, prefix);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(
+				crh2);
+
+		Assert.assertEquals(2, crh2.getCount());
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexThreeVarOrder2() throws PcjException,
+			RepositoryException, AccumuloException, AccumuloSecurityException,
+			TableExistsException, TableNotFoundException,
+			TupleQueryResultHandlerException, QueryEvaluationException,
+			MalformedQueryException, SailException {
+
+		final URI superclass = new URIImpl("uri:superclass");
+		final URI superclass2 = new URIImpl("uri:superclass2");
+
+		conn.add(subclass, RDF.TYPE, superclass);
+		conn.add(subclass2, RDF.TYPE, superclass2);
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+		accCon.tableOperations().create("table2");
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?c ?e ?l  " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?o ?f ?e ?c ?l  " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final String queryString = ""//
+				+ "SELECT ?e ?c ?l ?f ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final CountingResultHandler crh2 = new CountingResultHandler();
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 1,
+				indexSparqlString, new String[] { "c", "e", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 2,
+				indexSparqlString2, new String[] { "o", "f", "e", "c", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, prefix);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(
+				crh2);
+
+		Assert.assertEquals(2, crh2.getCount());
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexThreeVarOrder3ThreeBindingSet()
+			throws TupleQueryResultHandlerException, QueryEvaluationException,
+			MalformedQueryException, RepositoryException, AccumuloException,
+			AccumuloSecurityException, TableNotFoundException,
+			TableExistsException, PcjException, SailException {
+
+		final URI sub3 = new URIImpl("uri:entity3");
+		final URI subclass3 = new URIImpl("uri:class3");
+		final URI obj3 = new URIImpl("uri:obj3");
+		final URI superclass = new URIImpl("uri:superclass");
+		final URI superclass2 = new URIImpl("uri:superclass2");
+		final URI superclass3 = new URIImpl("uri:superclass3");
+
+		conn.add(sub3, RDF.TYPE, subclass3);
+		conn.add(sub3, RDFS.LABEL, new LiteralImpl("label3"));
+		conn.add(sub3, talksTo, obj3);
+		conn.add(subclass, RDF.TYPE, superclass);
+		conn.add(subclass2, RDF.TYPE, superclass2);
+		conn.add(subclass3, RDF.TYPE, superclass3);
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+		conn.add(obj3, RDFS.LABEL, new LiteralImpl("label3"));
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+		final String indexSparqlString = ""//
+				+ "SELECT ?c ?e ?l  " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?o ?f ?l ?e ?c  " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final String queryString = ""//
+				+ "SELECT ?e ?c ?l ?f ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final CountingResultHandler crh1 = new CountingResultHandler();
+		final CountingResultHandler crh2 = new CountingResultHandler();
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 1,
+				indexSparqlString, new String[] { "c", "e", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 2,
+				indexSparqlString2, new String[] { "o", "f", "l", "e", "c" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString)
+				.evaluate(crh1);
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, prefix);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(
+				crh2);
+		Assert.assertEquals(3, crh1.getCount());
+		Assert.assertEquals(3, crh2.getCount());
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexThreeVarOrder5ThreeBindingSet()
+			throws TupleQueryResultHandlerException, QueryEvaluationException,
+			MalformedQueryException, RepositoryException, AccumuloException,
+			AccumuloSecurityException, TableNotFoundException,
+			TableExistsException, PcjException, SailException {
+
+		final URI sub3 = new URIImpl("uri:entity3");
+		final URI subclass3 = new URIImpl("uri:class3");
+		final URI obj3 = new URIImpl("uri:obj3");
+
+		final URI superclass = new URIImpl("uri:superclass");
+		final URI superclass2 = new URIImpl("uri:superclass2");
+		final URI superclass3 = new URIImpl("uri:superclass3");
+
+		conn.add(sub3, RDF.TYPE, subclass3);
+		conn.add(sub3, RDFS.LABEL, new LiteralImpl("label3"));
+		conn.add(sub3, talksTo, obj3);
+		conn.add(subclass, RDF.TYPE, superclass);
+		conn.add(subclass2, RDF.TYPE, superclass2);
+		conn.add(subclass3, RDF.TYPE, superclass3);
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+		conn.add(obj3, RDFS.LABEL, new LiteralImpl("label3"));
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+		final String indexSparqlString = ""//
+				+ "SELECT ?c ?e ?l  " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?o ?f ?e ?l ?c  " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final String queryString = ""//
+				+ "SELECT ?e ?c ?l ?f ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final CountingResultHandler crh1 = new CountingResultHandler();
+		final CountingResultHandler crh2 = new CountingResultHandler();
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 1,
+				indexSparqlString, new String[] { "c", "e", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 2,
+				indexSparqlString2, new String[] { "o", "f", "e", "l", "c" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString)
+				.evaluate(crh1);
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, prefix);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(
+				crh2);
+
+		Assert.assertEquals(3, crh1.getCount());
+		Assert.assertEquals(3, crh2.getCount());
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexThreeVarOrder4ThreeBindingSet()
+			throws PcjException, RepositoryException, AccumuloException,
+			AccumuloSecurityException, TableExistsException,
+			TableNotFoundException, TupleQueryResultHandlerException,
+			QueryEvaluationException, MalformedQueryException, SailException {
+
+		final URI sub3 = new URIImpl("uri:entity3");
+		final URI subclass3 = new URIImpl("uri:class3");
+		final URI obj3 = new URIImpl("uri:obj3");
+
+		final URI superclass = new URIImpl("uri:superclass");
+		final URI superclass2 = new URIImpl("uri:superclass2");
+		final URI superclass3 = new URIImpl("uri:superclass3");
+
+		conn.add(sub3, RDF.TYPE, subclass3);
+		conn.add(sub3, RDFS.LABEL, new LiteralImpl("label3"));
+		conn.add(sub3, talksTo, obj3);
+		conn.add(subclass, RDF.TYPE, superclass);
+		conn.add(subclass2, RDF.TYPE, superclass2);
+		conn.add(subclass3, RDF.TYPE, superclass3);
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+		conn.add(obj3, RDFS.LABEL, new LiteralImpl("label3"));
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?c ?e ?l  " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?o ?f ?c ?e ?l  " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final String queryString = ""//
+				+ "SELECT ?e ?c ?l ?f ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final CountingResultHandler crh1 = new CountingResultHandler();
+		final CountingResultHandler crh2 = new CountingResultHandler();
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 1,
+				indexSparqlString, new String[] { "c", "e", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 2,
+				indexSparqlString2, new String[] { "o", "f", "c", "e", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString)
+				.evaluate(crh1);
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, prefix);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(
+				crh2);
+
+		Assert.assertEquals(3, crh1.getCount());
+		Assert.assertEquals(3, crh2.getCount());
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexThreeVarOrder6ThreeBindingSet()
+			throws MalformedQueryException, RepositoryException,
+			AccumuloException, AccumuloSecurityException, TableExistsException,
+			TableNotFoundException, PcjException,
+			TupleQueryResultHandlerException, QueryEvaluationException,
+			SailException {
+
+		final URI sub3 = new URIImpl("uri:entity3");
+		final URI subclass3 = new URIImpl("uri:class3");
+		final URI obj3 = new URIImpl("uri:obj3");
+
+		final URI superclass = new URIImpl("uri:superclass");
+		final URI superclass2 = new URIImpl("uri:superclass2");
+		final URI superclass3 = new URIImpl("uri:superclass3");
+
+		conn.add(sub3, RDF.TYPE, subclass3);
+		conn.add(sub3, RDFS.LABEL, new LiteralImpl("label3"));
+		conn.add(sub3, talksTo, obj3);
+		conn.add(subclass, RDF.TYPE, superclass);
+		conn.add(subclass2, RDF.TYPE, superclass2);
+		conn.add(subclass3, RDF.TYPE, superclass3);
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+		conn.add(obj3, RDFS.LABEL, new LiteralImpl("label3"));
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+		final String indexSparqlString = ""//
+				+ "SELECT ?c ?e ?l  " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?c ?l ?e ?o ?f " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final String queryString = ""//
+				+ "SELECT ?e ?c ?l ?f ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final CountingResultHandler crh1 = new CountingResultHandler();
+		final CountingResultHandler crh2 = new CountingResultHandler();
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 1,
+				indexSparqlString, new String[] { "c", "e", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 2,
+				indexSparqlString2, new String[] { "c", "l", "e", "o", "f" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString)
+				.evaluate(crh1);
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, prefix);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(
+				crh2);
+
+		Assert.assertEquals(3, crh1.getCount());
+		Assert.assertEquals(3, crh2.getCount());
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexThreeVarOrder7ThreeBindingSet()
+			throws PcjException, RepositoryException, AccumuloException,
+			AccumuloSecurityException, TableExistsException,
+			TableNotFoundException, TupleQueryResultHandlerException,
+			QueryEvaluationException, MalformedQueryException, SailException {
+
+		final URI sub3 = new URIImpl("uri:entity3");
+		final URI subclass3 = new URIImpl("uri:class3");
+		final URI obj3 = new URIImpl("uri:obj3");
+
+		final URI superclass = new URIImpl("uri:superclass");
+		final URI superclass2 = new URIImpl("uri:superclass2");
+		final URI superclass3 = new URIImpl("uri:superclass3");
+
+		conn.add(sub3, RDF.TYPE, subclass3);
+		conn.add(sub3, RDFS.LABEL, new LiteralImpl("label3"));
+		conn.add(sub3, talksTo, obj3);
+		conn.add(subclass, RDF.TYPE, superclass);
+		conn.add(subclass2, RDF.TYPE, superclass2);
+		conn.add(subclass3, RDF.TYPE, superclass3);
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+		conn.add(obj3, RDFS.LABEL, new LiteralImpl("label3"));
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+		final String indexSparqlString = ""//
+				+ "SELECT ?c ?e ?l  " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?o ?l ?c ?e ?f " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final String queryString = ""//
+				+ "SELECT ?e ?c ?l ?f ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final CountingResultHandler crh1 = new CountingResultHandler();
+		final CountingResultHandler crh2 = new CountingResultHandler();
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 1,
+				indexSparqlString, new String[] { "c", "e", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 2,
+				indexSparqlString2, new String[] { "o", "l", "c", "e", "f" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString)
+				.evaluate(crh1);
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, prefix);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(
+				crh2);
+		Assert.assertEquals(3, crh1.getCount());
+		Assert.assertEquals(3, crh2.getCount());
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexThreeVarInvalidOrder1()
+			throws TupleQueryResultHandlerException, QueryEvaluationException,
+			MalformedQueryException, RepositoryException, AccumuloException,
+			AccumuloSecurityException, TableExistsException,
+			TableNotFoundException, PcjException, SailException {
+
+		final URI superclass = new URIImpl("uri:superclass");
+		final URI superclass2 = new URIImpl("uri:superclass2");
+
+		conn.add(subclass, RDF.TYPE, superclass);
+		conn.add(subclass2, RDF.TYPE, superclass2);
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?c ?e ?l  " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
+				+ "}";//
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?e ?o ?f ?c ?l  " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final String queryString = ""//
+				+ "SELECT ?e ?c ?l ?f ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final CountingResultHandler crh1 = new CountingResultHandler();
+		final CountingResultHandler crh2 = new CountingResultHandler();
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 1,
+				indexSparqlString, new String[] { "c", "e", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 2,
+				indexSparqlString2, new String[] { "e", "o", "f", "c", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString)
+				.evaluate(crh1);
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, prefix);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(
+				crh2);
+		Assert.assertEquals(crh1.getCount(), crh2.getCount());
+
+	}
+
+
+	@Test
+	public void testEvaluateOneIndex() throws PcjException,
+			RepositoryException, AccumuloException, AccumuloSecurityException,
+			TableExistsException, TableNotFoundException,
+			MalformedQueryException, SailException, QueryEvaluationException,
+			TupleQueryResultHandlerException {
+
+		final URI superclass = new URIImpl("uri:superclass");
+		final URI superclass2 = new URIImpl("uri:superclass2");
+
+		conn.add(subclass, RDF.TYPE, superclass);
+		conn.add(subclass2, RDF.TYPE, superclass2);
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?dog ?pig ?duck  " //
+				+ "{" //
+				+ "  ?pig a ?dog . "//
+				+ "  ?pig <http://www.w3.org/2000/01/rdf-schema#label> ?duck "//
+				+ "}";//
+
+		final CountingResultHandler crh1 = new CountingResultHandler();
+		final CountingResultHandler crh2 = new CountingResultHandler();
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 1,
+				indexSparqlString, new String[] { "dog", "pig", "duck" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString)
+				.evaluate(crh1);
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, prefix);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString)
+				.evaluate(crh2);
+
+		Assert.assertEquals(crh1.count, crh2.count);
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexThreeVarOrder3() throws PcjException,
+			RepositoryException, AccumuloException, AccumuloSecurityException,
+			TableNotFoundException, TableExistsException,
+			TupleQueryResultHandlerException, QueryEvaluationException,
+			MalformedQueryException, SailException {
+
+		final URI superclass = new URIImpl("uri:superclass");
+		final URI superclass2 = new URIImpl("uri:superclass2");
+
+		conn.add(subclass, RDF.TYPE, superclass);
+		conn.add(subclass2, RDF.TYPE, superclass2);
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?dog ?pig ?duck  " //
+				+ "{" //
+				+ "  ?pig a ?dog . "//
+				+ "  ?pig <http://www.w3.org/2000/01/rdf-schema#label> ?duck "//
+				+ "}";//
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?o ?f ?e ?c ?l  " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final String queryString = ""//
+				+ "SELECT ?e ?c ?l ?f ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final CountingResultHandler crh1 = new CountingResultHandler();
+		final CountingResultHandler crh2 = new CountingResultHandler();
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 1,
+				indexSparqlString, new String[] { "dog", "pig", "duck" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 2,
+				indexSparqlString2, new String[] { "o", "f", "e", "c", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString)
+				.evaluate(crh1);
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, prefix);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(
+				crh2);
+		Assert.assertEquals(crh1.getCount(), crh2.getCount());
+
+	}
+
+	@Test
+	public void testSupportedVarOrders1() throws PcjException,
+			RepositoryException, AccumuloException, AccumuloSecurityException,
+			TableNotFoundException, TableExistsException,
+			MalformedQueryException, SailException, QueryEvaluationException {
+
+		final URI superclass = new URIImpl("uri:superclass");
+		final URI superclass2 = new URIImpl("uri:superclass2");
+
+		conn.add(subclass, RDF.TYPE, superclass);
+		conn.add(subclass2, RDF.TYPE, superclass2);
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?dog ?pig ?duck  " //
+				+ "{" //
+				+ "  ?pig a ?dog . "//
+				+ "  ?pig <http://www.w3.org/2000/01/rdf-schema#label> ?duck "//
+				+ "}";//
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?o ?f ?e ?c ?l  " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 1,
+				indexSparqlString, new String[] { "dog", "pig", "duck" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		final AccumuloIndexSet ais1 = new AccumuloIndexSet(accCon,
+				tablename + 1);
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 2,
+				indexSparqlString2, new String[] { "o", "f", "e", "c", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		final AccumuloIndexSet ais2 = new AccumuloIndexSet(accCon,
+				tablename + 2);
+
+		final Set<String> ais1Set1 = Sets.newHashSet();
+		ais1Set1.add("dog");
+
+		Assert.assertTrue(ais1.supportsBindingSet(ais1Set1));
+		ais1Set1.add("duck");
+
+		Assert.assertTrue(ais1.supportsBindingSet(ais1Set1));
+
+		ais1Set1.add("chicken");
+
+		Assert.assertTrue(ais1.supportsBindingSet(ais1Set1));
+
+		final Set<String> ais2Set1 = Sets.newHashSet();
+		ais2Set1.add("f");
+
+		Assert.assertTrue(ais2.supportsBindingSet(ais2Set1));
+		ais2Set1.add("e");
+
+		Assert.assertTrue(ais2.supportsBindingSet(ais2Set1));
+
+		ais2Set1.add("o");
+
+		Assert.assertTrue(ais2.supportsBindingSet(ais2Set1));
+
+		ais2Set1.add("l");
+
+		Assert.assertTrue(ais2.supportsBindingSet(ais2Set1));
+
+		final Set<String> ais2Set2 = Sets.newHashSet();
+		ais2Set2.add("f");
+
+		Assert.assertTrue(ais2.supportsBindingSet(ais2Set2));
+
+		ais2Set2.add("o");
+
+		Assert.assertTrue(ais2.supportsBindingSet(ais2Set2));
+
+		ais2Set2.add("c");
+
+		Assert.assertTrue(!ais2.supportsBindingSet(ais2Set2));
+
+		final Set<String> ais2Set3 = Sets.newHashSet();
+		ais2Set3.add("c");
+
+		Assert.assertTrue(ais2.supportsBindingSet(ais2Set3));
+
+		ais2Set3.add("e");
+
+		Assert.assertTrue(ais2.supportsBindingSet(ais2Set3));
+
+		ais2Set3.add("l");
+
+		Assert.assertTrue(ais2.supportsBindingSet(ais2Set3));
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexThreeVarOrder() throws PcjException,
+			RepositoryException, AccumuloException, AccumuloSecurityException,
+			TableNotFoundException, TableExistsException,
+			TupleQueryResultHandlerException, QueryEvaluationException,
+			MalformedQueryException, SailException {
+
+		final URI superclass = new URIImpl("uri:superclass");
+		final URI superclass2 = new URIImpl("uri:superclass2");
+
+		conn.add(subclass, RDF.TYPE, superclass);
+		conn.add(subclass2, RDF.TYPE, superclass2);
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?dog ?pig ?duck  " //
+				+ "{" //
+				+ "  ?pig a ?dog . "//
+				+ "  ?pig <http://www.w3.org/2000/01/rdf-schema#label> ?duck "//
+				+ "}";//
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?o ?f ?e ?c ?l  " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final String queryString = ""//
+				+ "SELECT ?e ?c ?l ?f ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final CountingResultHandler crh1 = new CountingResultHandler();
+		final CountingResultHandler crh2 = new CountingResultHandler();
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename+1,
+				indexSparqlString, new String[] { "dog", "pig", "duck" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename+2,
+				indexSparqlString2, new String[] { "o", "f", "e", "c", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString)
+				.evaluate(crh1);
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, prefix);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(
+				crh2);
+		Assert.assertEquals(crh1.getCount(), crh2.getCount());
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexValidate() throws PcjException,
+			RepositoryException, AccumuloException, AccumuloSecurityException,
+			TableExistsException, TableNotFoundException,
+			TupleQueryResultHandlerException, QueryEvaluationException,
+			MalformedQueryException, SailException {
+
+		final URI superclass = new URIImpl("uri:superclass");
+		final URI superclass2 = new URIImpl("uri:superclass2");
+
+		conn.add(subclass, RDF.TYPE, superclass);
+		conn.add(subclass2, RDF.TYPE, superclass2);
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?dog ?pig ?duck  " //
+				+ "{" //
+				+ "  ?pig a ?dog . "//
+				+ "  ?pig <http://www.w3.org/2000/01/rdf-schema#label> ?duck "//
+				+ "}";//
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?o ?f ?e ?c ?l  " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final String queryString = ""//
+				+ "SELECT ?e ?c ?l ?f ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename+1,
+				indexSparqlString, new String[] { "dog", "pig", "duck" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		final AccumuloIndexSet ais1 = new AccumuloIndexSet(accCon, tablename+1);
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename+2,
+				indexSparqlString2, new String[] { "o", "f", "e", "c", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		final AccumuloIndexSet ais2 = new AccumuloIndexSet(accCon, tablename+2);
+
+		final List<ExternalTupleSet> index = new ArrayList<>();
+		index.add(ais1);
+		index.add(ais2);
+
+		ParsedQuery pq = null;
+		final SPARQLParser sp = new SPARQLParser();
+		pq = sp.parseQuery(queryString, null);
+		final List<TupleExpr> teList = Lists.newArrayList();
+		final TupleExpr te = pq.getTupleExpr();
+
+		final PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(index, false);
+        pcj.optimize(te, null, null);
+		teList.add(te);
+
+		final IndexPlanValidator ipv = new IndexPlanValidator(false);
+
+		Assert.assertTrue(ipv.isValid(te));
+
+	}
+
+	@Test
+	public void testEvaluateThreeIndexValidate() throws PcjException,
+			RepositoryException, AccumuloException, AccumuloSecurityException,
+			TableExistsException, TableNotFoundException,
+			MalformedQueryException, SailException, QueryEvaluationException,
+			TupleQueryResultHandlerException {
+
+		final URI superclass = new URIImpl("uri:superclass");
+		final URI superclass2 = new URIImpl("uri:superclass2");
+
+		final URI sub = new URIImpl("uri:entity");
+		subclass = new URIImpl("uri:class");
+		obj = new URIImpl("uri:obj");
+		talksTo = new URIImpl("uri:talksTo");
+
+		final URI howlsAt = new URIImpl("uri:howlsAt");
+		final URI subType = new URIImpl("uri:subType");
+		final URI superSuperclass = new URIImpl("uri:super_superclass");
+
+		conn.add(subclass, RDF.TYPE, superclass);
+		conn.add(subclass2, RDF.TYPE, superclass2);
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+		conn.add(sub, howlsAt, superclass);
+		conn.add(superclass, subType, superSuperclass);
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?dog ?pig ?duck  " //
+				+ "{" //
+				+ "  ?pig a ?dog . "//
+				+ "  ?pig <http://www.w3.org/2000/01/rdf-schema#label> ?duck "//
+				+ "}";//
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?o ?f ?e ?c ?l  " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final String indexSparqlString3 = ""//
+				+ "SELECT ?wolf ?sheep ?chicken  " //
+				+ "{" //
+				+ "  ?wolf <uri:howlsAt> ?sheep . "//
+				+ "  ?sheep <uri:subType> ?chicken. "//
+				+ "}";//
+
+		final String queryString = ""//
+				+ "SELECT ?e ?c ?l ?f ?o " //
+				+ "{" //
+				+ "  ?e a ?c . "//
+				+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "  ?e <uri:howlsAt> ?f. "//
+				+ "  ?f <uri:subType> ?o. "//
+				+ "}";//
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename+1,
+				indexSparqlString, new String[] { "dog", "pig", "duck" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		final AccumuloIndexSet ais1 = new AccumuloIndexSet(accCon, tablename+1);
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename+2,
+				indexSparqlString2, new String[] { "o", "f", "e", "c", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		final AccumuloIndexSet ais2 = new AccumuloIndexSet(accCon, tablename+2);
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename+3,
+				indexSparqlString3,
+				new String[] { "wolf", "sheep", "chicken" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		final AccumuloIndexSet ais3 = new AccumuloIndexSet(accCon, tablename+3);
+
+		final List<ExternalTupleSet> index = new ArrayList<>();
+		index.add(ais1);
+		index.add(ais3);
+		index.add(ais2);
+
+		ParsedQuery pq = null;
+		final SPARQLParser sp = new SPARQLParser();
+		pq = sp.parseQuery(queryString, null);
+		final List<TupleExpr> teList = Lists.newArrayList();
+		final TupleExpr te = pq.getTupleExpr();
+
+		final PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(index, false);
+        pcj.optimize(te, null, null);
+
+		teList.add(te);
+
+		final IndexPlanValidator ipv = new IndexPlanValidator(false);
+
+		Assert.assertTrue(ipv.isValid(te));
+
+	}
+
+	public static class CountingResultHandler implements
+			TupleQueryResultHandler {
+		private int count = 0;
+
+		public int getCount() {
+			return count;
+		}
+
+		public void resetCount() {
+			this.count = 0;
+		}
+
+		@Override
+		public void startQueryResult(List<String> arg0)
+				throws TupleQueryResultHandlerException {
+		}
+
+		@Override
+		public void handleSolution(BindingSet arg0)
+				throws TupleQueryResultHandlerException {
+			count++;
+			System.out.println(arg0);
+		}
+
+		@Override
+		public void endQueryResult() throws TupleQueryResultHandlerException {
+		}
+
+		@Override
+		public void handleBoolean(boolean arg0)
+				throws QueryResultHandlerException {
+
+		}
+
+		@Override
+		public void handleLinks(List<String> arg0)
+				throws QueryResultHandlerException {
+
+		}
+	}
+
+}


[10/16] incubator-rya git commit: RYA-32 Improve how metadata and values are written to Accumulo PCJ tables

Posted by mi...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/ThreshholdPlanSelectorTest.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/ThreshholdPlanSelectorTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/ThreshholdPlanSelectorTest.java
index f8da365..3bc895e 100644
--- a/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/ThreshholdPlanSelectorTest.java
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/ThreshholdPlanSelectorTest.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.IndexPlanValidator;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -19,19 +19,16 @@ package mvm.rya.indexing.IndexPlanValidator;
  * under the License.
  */
 
-
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Set;
 
-import junit.framework.Assert;
-import mvm.rya.indexing.external.ExternalProcessor;
+import mvm.rya.indexing.external.PrecompJoinOptimizer;
 import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
 import mvm.rya.indexing.external.tupleSet.SimpleExternalTupleSet;
 
+import org.junit.Assert;
 import org.junit.Test;
-import org.openrdf.query.algebra.Filter;
 import org.openrdf.query.algebra.Projection;
 import org.openrdf.query.algebra.QueryModelNode;
 import org.openrdf.query.algebra.StatementPattern;
@@ -42,797 +39,675 @@ import org.openrdf.query.parser.ParsedQuery;
 import org.openrdf.query.parser.sparql.SPARQLParser;
 
 import com.beust.jcommander.internal.Lists;
-import com.google.common.collect.Sets;
 
 public class ThreshholdPlanSelectorTest {
 
-    private String q7 = ""//
-            + "SELECT ?s ?t ?u " //
-            + "{" //
-            + "  ?s a ?t ."//
-            + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-            + "  ?u <uri:talksTo> ?s . "//
-            + "}";//
-    
-    private String q8 = ""//
-            + "SELECT ?e ?l ?c " //
-            + "{" //
-            + "  ?e a ?l ."//
-            + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-            + "  ?c <uri:talksTo> ?e . "//
-            + "}";//
-    
-    private String q9 = ""//
-            + "SELECT ?f ?m ?d " //
-            + "{" //
-            + "  ?f a ?m ."//
-            + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-            + "  ?d <uri:talksTo> ?f . "//
-            + "}";//
-    
-    
-    
-    
-    private String q15 = ""//
-            + "SELECT ?f ?m ?d ?e ?l ?c " //
-            + "{" //
-            + "  ?f a ?m ."//
-            + "  ?e a ?l ."//
-            + "  ?d <uri:talksTo> ?f . "//
-            + "  ?c <uri:talksTo> ?e . "//
-            + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-            + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-            + "}";//
-    
-    private String q16 = ""//
-            + "SELECT ?f ?m ?d ?e ?l " //
-            + "{" //
-            + "  ?d <uri:talksTo> ?f . "//
-            + "  ?d <uri:talksTo> ?e . "//
-            + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-            + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-            + "}";//
-    
-    private String q17 = ""//
-            + "SELECT ?chicken ?dog ?cat  " //
-            + "{" //
-            + "  ?chicken <uri:talksTo> ?dog . "//
-            + "  ?cat <http://www.w3.org/2000/01/rdf-schema#label> ?chicken ."//
-            + "}";//
-    
-    private String q18 = ""//
-            + "SELECT ?dog ?chicken " //
-            + "{" //
-            + "  ?chicken <uri:talksTo> ?dog . "//
-            + "}";//
-    
-    private String q19 = ""//
-            + "SELECT ?cat ?chicken " //
-            + "{" //
-            + "  ?cat <http://www.w3.org/2000/01/rdf-schema#label> ?chicken ."//
-            + "}";//
-    
-    
-    private String q20 = ""//
-            + "SELECT ?f ?m ?d ?e ?l ?c " //
-            + "{" //
-            + "  ?f a ?m ."//
-            + "  ?e a ?l ."//
-            + "  ?d <uri:talksTo> ?f . "//
-            + "  ?c <uri:talksTo> ?e . "//
-            + "  ?m <uri:talksTo> ?e . "//
-            + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-            + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-            + "}";//
-    
-    
-    
-    private String q21 = ""//
-            + "SELECT ?u ?s ?t " //
-            + "{" //
-            + "  ?s a ?t ."//
-            + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-            + "  ?u <uri:talksTo> ?s . "//
-            + "}";//
-    
-    
-    
-    @Test
-    public void testSingleIndex() throws Exception {
-
-        SPARQLParser parser = new SPARQLParser();
-        
-
-        ParsedQuery pq1 = parser.parseQuery(q15, null);
-        ParsedQuery pq2 = parser.parseQuery(q7, null);
-        ParsedQuery pq3 = parser.parseQuery(q8, null);
-        ParsedQuery pq4 = parser.parseQuery(q9, null);
-        //ParsedQuery pq3 = parser.parseQuery(q12, null);
-       
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
-        SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
-
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-
-        list.add(extTup1);
-        
-        List<QueryModelNode> optTupNodes = Lists.newArrayList();
-        optTupNodes.add(extTup2);
-        optTupNodes.add(extTup3);
-
-        IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(pq1.getTupleExpr(), list);
-
-        Iterator<TupleExpr> plans = (new TupleExecutionPlanGenerator()).getPlans(iep.getIndexedTuples());
-        
-        IndexPlanValidator ipv = new IndexPlanValidator(false);
-        
-        Iterator<TupleExpr> validPlans = ipv.getValidTuples(plans);
-        
-        ThreshholdPlanSelector tps = new ThreshholdPlanSelector(pq1.getTupleExpr());
-        
-        TupleExpr optimalTup = tps.getThreshholdQueryPlan(validPlans, .1, 1, 0, 0);
-        
-        NodeCollector nc = new NodeCollector();
-        optimalTup.visit(nc);
-        
-        List<QueryModelNode> qNodes = nc.getNodes();
-             
-        
-        Assert.assertEquals(qNodes.size(), optTupNodes.size());
-        for(QueryModelNode node: qNodes) {
-            Assert.assertTrue(optTupNodes.contains(node));
-        }
-               
-
-    }
-    
-    
-    
-    
-    
-    @Test
-    public void testSingleIndex2() throws Exception {
-
-        String q1 = ""//
-                + "SELECT ?f ?m ?d ?e ?l ?c " //
-                + "{" //
-                + "  ?f a ?m ."//
-                + "  ?c a ?l ."//
-                + "  ?d <uri:talksTo> ?f . "//
-                + "  ?e <uri:talksTo> ?c . "//
-                + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-                + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?e ."//
-                + "  ?m <uri:talksTo> ?e . "//
-                + "}";//
-
-        String q2 = ""//
-                + "SELECT ?u ?s ?t " //
-                + "{" //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "  ?u <uri:talksTo> ?s . "//
-                + "}";//
-
-        String q3 = ""//
-                + "SELECT ?e ?c ?l " //
-                + "{" //
-                + "  ?c a ?l ."//
-                + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?e ."//
-                + "  ?e <uri:talksTo> ?c . "//
-                + "}";//
-
-        String q4 = ""//
-                + "SELECT ?d ?f ?m " //
-                + "{" //
-                + "  ?f a ?m ."//
-                + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-                + "  ?d <uri:talksTo> ?f . "//
-                + "}";//
-
-        SPARQLParser parser = new SPARQLParser();
-
-        ParsedQuery pq1 = parser.parseQuery(q1, null);
-        ParsedQuery pq2 = parser.parseQuery(q2, null);
-        ParsedQuery pq3 = parser.parseQuery(q3, null);
-        ParsedQuery pq4 = parser.parseQuery(q4, null);
-
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
-        SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
-
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-
-        list.add(extTup1);
-
-        List<StatementPattern> spList = StatementPatternCollector.process(pq1.getTupleExpr());
-        List<QueryModelNode> optTupNodes = Lists.newArrayList();
-        optTupNodes.add(extTup3);
-        optTupNodes.add(spList.get(6));
-        optTupNodes.add(extTup2);
-
-        IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(pq1.getTupleExpr(), list);
-
-        Iterator<TupleExpr> plans = (new TupleExecutionPlanGenerator()).getPlans(iep.getIndexedTuples());
-
-        //System.out.println("Size is " + plans.size());
-        // System.out.println("Plans are " + plans);
-
-        IndexPlanValidator ipv = new IndexPlanValidator(true);
-        Iterator<TupleExpr> validPlans = ipv.getValidTuples(plans);
-
-        //System.out.println("Valid plan size is " + validPlans.size());
-        // System.out.println("Valid plans are " + validPlans);
-
-        ThreshholdPlanSelector tps = new ThreshholdPlanSelector(pq1.getTupleExpr());
-
-        TupleExpr optimalTup = tps.getThreshholdQueryPlan(validPlans, .4, .7, .1, .2);
-
-        NodeCollector nc = new NodeCollector();
-        optimalTup.visit(nc);
-
-        //System.out.println("Optimal plan is " + optimalTup);
-
-        List<QueryModelNode> qNodes = nc.getNodes();
-        //System.out.println("Returned list is " + qNodes + " and comp list is " + optTupNodes);
-
-        Assert.assertTrue(qNodes.equals(optTupNodes));
-
-    }
-    
-    
-    
-    
-    
-    
-    
-    
-    @Test
-    public void testTwoIndex() throws Exception {
-        
-        String q1 = ""//
-                + "SELECT ?f ?m ?d ?h ?i " //
-                + "{" //
-                + "  ?f a ?m ."//
-                + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-                + "  ?d <uri:talksTo> ?f . "//
-                + "  ?d <uri:hangOutWith> ?f ." //
-                + "  ?f <uri:hangOutWith> ?h ." //
-                + "  ?f <uri:associatesWith> ?i ." //
-                + "  ?i <uri:associatesWith> ?h ." //
-                + "}";//
-
-        String q2 = ""//
-                + "SELECT ?t ?s ?u " //
-                + "{" //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "  ?u <uri:talksTo> ?s . "//
-                + "}";//
-        
-        
-        String q3 = ""//
-                + "SELECT ?s ?t ?u " //
-                + "{" //
-                + "  ?s <uri:hangOutWith> ?t ." //
-                + "  ?t <uri:hangOutWith> ?u ." //
-                + "}";//
-        
-        String q4 = ""//
-                + "SELECT ?s ?t ?u " //
-                + "{" //
-                + "  ?s <uri:associatesWith> ?t ." //
-                + "  ?t <uri:associatesWith> ?u ." //
-                + "}";//
-        
-        
-        
-        String q5 = ""//
-                + "SELECT ?m ?f ?d " //
-                + "{" //
-                + "  ?f a ?m ."//
-                + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-                + "  ?d <uri:talksTo> ?f . "//
-                + "}";//
-        
-        
-        String q6 = ""//
-                + "SELECT ?d ?f ?h " //
-                + "{" //
-                + "  ?d <uri:hangOutWith> ?f ." //
-                + "  ?f <uri:hangOutWith> ?h ." //
-                + "}";//
-        
-        String q7 = ""//
-                + "SELECT ?f ?i ?h " //
-                + "{" //
-                + "  ?f <uri:associatesWith> ?i ." //
-                + "  ?i <uri:associatesWith> ?h ." //
-                + "}";//
-        
-        
-       
-        
-        
-
-        SPARQLParser parser = new SPARQLParser();
-
-        ParsedQuery pq1 = parser.parseQuery(q1, null);
-        ParsedQuery pq2 = parser.parseQuery(q2, null);
-        ParsedQuery pq3 = parser.parseQuery(q3, null);
-        ParsedQuery pq4 = parser.parseQuery(q4, null);
-        ParsedQuery pq5 = parser.parseQuery(q5, null);
-        ParsedQuery pq6 = parser.parseQuery(q6, null);
-        ParsedQuery pq7 = parser.parseQuery(q7, null);
-
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
-        SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
-        SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet((Projection) pq5.getTupleExpr());
-        SimpleExternalTupleSet extTup5 = new SimpleExternalTupleSet((Projection) pq6.getTupleExpr());
-        SimpleExternalTupleSet extTup6 = new SimpleExternalTupleSet((Projection) pq7.getTupleExpr());
-      
-
-        
-
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-
-        list.add(extTup2);
-        list.add(extTup1);
-        list.add(extTup3);
-
-        List<QueryModelNode> optTupNodes = Lists.newArrayList();
-        optTupNodes.add(extTup4);
-        optTupNodes.add(extTup6);
-        optTupNodes.add(extTup5);
-
-        IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(pq1.getTupleExpr(), list);
-
-        Iterator<TupleExpr> plans = (new TupleExecutionPlanGenerator()).getPlans(iep.getIndexedTuples());
-        IndexPlanValidator ipv = new IndexPlanValidator(true);
-        Iterator<TupleExpr> validPlans = ipv.getValidTuples(plans);
-
-        ThreshholdPlanSelector tps = new ThreshholdPlanSelector(pq1.getTupleExpr());
-        TupleExpr optimalTup = tps.getThreshholdQueryPlan(validPlans, .2, .6, .4, 0);
-        
-        NodeCollector nc = new NodeCollector();
-        optimalTup.visit(nc);
-
-        List<QueryModelNode> qNodes = nc.getNodes();
-        
-        Assert.assertTrue(qNodes.equals(optTupNodes));
-
-    }
-    
-     
-    
-    
-    @Test
-    public void largeQueryFourtyIndexTest() {
-        
-        
-        String q1 = ""//
-                + "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r " //
-                + "{" //
-                + "  ?f a ?m ."//
-                + "  ?e a ?l ."//
-                + "  ?n a ?o ."//
-                + "  ?a a ?h ."//
-                + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-                + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
-                + "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
-                + "  ?d <uri:talksTo> ?f . "//
-                + "  ?c <uri:talksTo> ?e . "//
-                + "  ?p <uri:talksTo> ?n . "//
-                + "  ?r <uri:talksTo> ?a . "//
-                + "}";//
-        
-        
-        String q2 = ""//
-                + "SELECT ?s ?t ?u " //
-                + "{" //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "  ?u <uri:talksTo> ?s . "//
-                + "}";//
-        
-        
-        
-        String q3 = ""//
-                + "SELECT  ?s ?t ?u ?d ?f ?g " //
-                + "{" //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "  ?u <uri:talksTo> ?s . "//
-                + "  ?d a ?f ."//
-                + "  ?f <http://www.w3.org/2000/01/rdf-schema#label> ?g ."//
-                + "  ?g <uri:talksTo> ?d . "//
-                + "}";//
-        
-        
-        
-        String q4 = ""//
-                + "SELECT  ?s ?t ?u ?d ?f ?g ?a ?b ?c" //
-                + "{" //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "  ?u <uri:talksTo> ?s . "//
-                + "  ?d a ?f ."//
-                + "  ?f <http://www.w3.org/2000/01/rdf-schema#label> ?g ."//
-                + "  ?g <uri:talksTo> ?d . "//
-                + "  ?a a ?b ."//
-                + "  ?b <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-                + "  ?c <uri:talksTo> ?a . "//
-                + "}";//
-        
-        
-        String q5 = ""//
-                + "SELECT  ?f ?m ?d ?a ?h ?r " //
-                + "{" //
-                + "  ?f a ?m ."//
-                + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-                + "  ?d <uri:talksTo> ?f . "//
-                + "  ?a a ?h ."//
-                + "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
-                + "  ?r <uri:talksTo> ?a . "//
-                + "}";//
-        
-        String q6 = ""//
-                + "SELECT ?e ?l ?c " //
-                + "{" //
-                + "  ?e a ?l ."//
-                + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-                + "  ?c <uri:talksTo> ?e . "//
-                + "}";//
-        
-        String q7 = ""//
-                + "SELECT ?n ?o ?p " //
-                + "{" //
-                + "  ?n a ?o ."//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
-                + "  ?p <uri:talksTo> ?n . "//
-                + "}";//
-        
-        
-        
-        
-        
-        SPARQLParser parser = new SPARQLParser();
-
-        ParsedQuery pq1 = null;
-        ParsedQuery pq2 = null;
-        ParsedQuery pq3 = null;
-        ParsedQuery pq4 = null;
-        ParsedQuery pq5 = null;
-        ParsedQuery pq6 = null;
-        ParsedQuery pq7 = null;
-       
-
-        try {
-            pq1 = parser.parseQuery(q1, null);
-            pq2 = parser.parseQuery(q2, null);
-            pq3 = parser.parseQuery(q3, null);
-            pq4 = parser.parseQuery(q4, null);
-            pq5 = parser.parseQuery(q5, null);
-            pq6 = parser.parseQuery(q6, null);
-            pq7 = parser.parseQuery(q7, null);
-           
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
-        SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
-        
-        SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet((Projection) pq5.getTupleExpr());
-        SimpleExternalTupleSet extTup5 = new SimpleExternalTupleSet((Projection) pq6.getTupleExpr());
-        SimpleExternalTupleSet extTup6 = new SimpleExternalTupleSet((Projection) pq7.getTupleExpr());
-     
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-
-        list.add(extTup2);
-        list.add(extTup1);
-        list.add(extTup3);
-        
-        List<ExternalTupleSet> list2 = new ArrayList<ExternalTupleSet>();
-
-        list2.add(extTup4);
-        list2.add(extTup5);
-        list2.add(extTup6);
-
-        IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(pq1.getTupleExpr(), list);
-       
-        Iterator<TupleExpr> plans = (new TupleExecutionPlanGenerator()).getPlans(iep.getIndexedTuples());
-        IndexPlanValidator ipv = new IndexPlanValidator(false);
-        Iterator<TupleExpr> validPlans = ipv.getValidTuples(plans);
-
-        ThreshholdPlanSelector tps = new ThreshholdPlanSelector(pq1.getTupleExpr());
-        TupleExpr optimalTup = tps.getThreshholdQueryPlan(validPlans, .4, .8, .1, .1);
-        
-        NodeCollector nc = new NodeCollector();
-        optimalTup.visit(nc);
-        
-        
-        
-    }
-    
-    
-    
-    
-    
-    
-    
-    
-    @Test
-    public void twoIndexFilterTest() {
-        
-        
-        String q1 = ""//
-                + "SELECT ?f ?m ?d ?e ?l ?c " //
-                + "{" //
-                + "  Filter(?f > \"5\")." //
-                + "  Filter(?e > \"5\")." //
-                + "  ?f a ?m ."//
-                + "  ?e a ?l ."//
-                + "  ?d <uri:talksTo> ?f . "//
-                + "  ?c <uri:talksTo> ?e . "//
-                + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-                + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-                + "}";//
-        
-        
-        String q2 = ""//
-                + "SELECT ?s ?t ?u " //
-                + "{" //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "  ?u <uri:talksTo> ?s . "//
-                + "}";//
-        
-        
-        String q3 = ""//
-                + "SELECT ?s ?t ?u " //
-                + "{" //
-                + " Filter(?s > \"5\") ."// 
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "  ?u <uri:talksTo> ?s . "//
-                + "}";//
-        
-        
-        
-        String q4 = ""//
-                + "SELECT ?f ?m ?d " //
-                + "{" //
-                + " Filter(?f > \"5\") ."// 
-                + "  ?f a ?m ."//
-                + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-                + "  ?d <uri:talksTo> ?f . "//
-                + "}";//
-        
-        
-        String q5 = ""//
-                + "SELECT ?e ?l ?c " //
-                + "{" //
-                + " Filter(?e > \"5\") ."// 
-                + "  ?e a ?l ."//
-                + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-                + "  ?c <uri:talksTo> ?e . "//
-                + "}";//
-        
-        
-        
-        
-        SPARQLParser parser = new SPARQLParser();
-
-        ParsedQuery pq1 = null;
-        ParsedQuery pq2 = null;
-        ParsedQuery pq3 = null;
-        ParsedQuery pq4 = null;
-        ParsedQuery pq5 = null;
-        
-       
-
-        try {
-            pq1 = parser.parseQuery(q1, null);
-            pq2 = parser.parseQuery(q2, null);
-            pq3 = parser.parseQuery(q3, null);
-            pq4 = parser.parseQuery(q4, null);
-            pq5 = parser.parseQuery(q5, null);
-            
-           
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
-        SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
-        SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet((Projection) pq5.getTupleExpr());
-     
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-
-        list.add(extTup2);
-        list.add(extTup1);
-        
-        List<ExternalTupleSet> list2 = new ArrayList<ExternalTupleSet>();
-
-        list2.add(extTup3);
-        list2.add(extTup4);
-      
-        IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(pq1.getTupleExpr(), list);
-       
-        Iterator<TupleExpr> plans = (new TupleExecutionPlanGenerator()).getPlans(iep.getIndexedTuples());
-        IndexPlanValidator ipv = new IndexPlanValidator(false);
-        Iterator<TupleExpr> validPlans = ipv.getValidTuples(plans);
-
-        ThreshholdPlanSelector tps = new ThreshholdPlanSelector(pq1.getTupleExpr());
-        TupleExpr optimalTup = tps.getThreshholdQueryPlan(validPlans, .4, .8, .1, .1);
-        
-        NodeCollector nc = new NodeCollector();
-        optimalTup.visit(nc);
-        
-        Assert.assertEquals(nc.getNodes().size(), list2.size());
-
-        for(QueryModelNode e: nc.getNodes()) {
-            Assert.assertTrue(list2.contains((ExternalTupleSet)e));
-        }
-        
-        
-        
-    }
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    @Test
-    public void testCost1() throws Exception {
-        
-        String q1 = ""//
-                + "SELECT ?f ?m ?d ?h ?i " //
-                + "{" //
-                + "  ?f a ?m ."//
-                + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-                + "  ?d <uri:talksTo> ?f . "//
-                + "  ?d <uri:hangOutWith> ?f ." //
-                + "  ?f <uri:hangOutWith> ?h ." //
-                + "  ?f <uri:associatesWith> ?i ." //
-                + "  ?i <uri:associatesWith> ?h ." //
-                + "}";//
-
-       
-        SPARQLParser parser = new SPARQLParser();
-
-        ParsedQuery pq1 = parser.parseQuery(q1, null);
-
-        ThreshholdPlanSelector tps = new ThreshholdPlanSelector(pq1.getTupleExpr());
-        double cost = tps.getCost(pq1.getTupleExpr(), .6, .4, 0);
-        Assert.assertEquals(.7,cost);
-
-    }
-    
-    
-    
-    
-    @Test
-    public void testCost2() throws Exception {
-        
-        String q1 = ""//
-                + "SELECT ?f ?m ?d ?e ?l ?c " //
-                + "{" //
-                + "  ?f a ?m ."//
-                + "  ?e a ?l ."//
-                + "  ?d <uri:talksTo> ?f . "//
-                + "  ?c <uri:talksTo> ?e . "//
-                + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-                + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-                + "}";//
-
-       
-        SPARQLParser parser = new SPARQLParser();
-
-        ParsedQuery pq1 = parser.parseQuery(q1, null);
-
-        ThreshholdPlanSelector tps = new ThreshholdPlanSelector(pq1.getTupleExpr());
-        double cost = tps.getCost(pq1.getTupleExpr(), .4, .3, .3);
-        Assert.assertEquals(.58,cost, .000000001);
-
-    }
-    
-    
-    
-    
-    
-    @Test
-    public void testCost3() throws Exception {
-        
-        String q1 = ""//
-                + "SELECT ?f ?m ?d ?e ?l ?c " //
-                + "{" //
-                + "  Filter(?f > \"5\")." //
-                + "  Filter(?e > \"6\")." //
-                + "  ?f a ?m ."//
-                + "  ?e a ?l ."//
-                + "  ?d <uri:talksTo> ?f . "//
-                + "  ?c <uri:talksTo> ?e . "//
-                + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-                + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-                + "}";//
-        
-        
-        String q2 = ""//
-                + "SELECT ?s ?t ?u " //
-                + "{" //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "  ?u <uri:talksTo> ?s . "//
-                + "}";//
-
-       
-        SPARQLParser parser = new SPARQLParser();
-
-        ParsedQuery pq1 = parser.parseQuery(q1, null);
-        ParsedQuery pq2 = parser.parseQuery(q2, null);
-        
-        SimpleExternalTupleSet sep = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-        List<ExternalTupleSet> eList = Lists.newArrayList();
-        
-        eList.add(sep);
-        ExternalProcessor ep = new ExternalProcessor(eList);
-        
-        TupleExpr te = pq1.getTupleExpr();
-        te = ep.process(te);
-        
-       
-
-        ThreshholdPlanSelector tps = new ThreshholdPlanSelector(pq1.getTupleExpr());
-        double cost = tps.getCost(te, .4, .3, .3);
-        Assert.assertEquals(.575,cost);
-        
-
-    }
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    public static class NodeCollector extends QueryModelVisitorBase<RuntimeException> {
-
-        List<QueryModelNode> qNodes = Lists.newArrayList();
-
-
-        public List<QueryModelNode> getNodes() {
-            return qNodes;
-        }
-        
-       
-
-        @Override
-        public void meetNode(QueryModelNode node) {
-            if(node instanceof StatementPattern || node instanceof ExternalTupleSet) {
-                qNodes.add(node);
-            } 
-            super.meetNode(node);
-            
-        }
-
-     
-    }
-    
-    
-    
-    
+	private String q7 = ""//
+			+ "SELECT ?s ?t ?u " //
+			+ "{" //
+			+ "  ?s a ?t ."//
+			+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+			+ "  ?u <uri:talksTo> ?s . "//
+			+ "}";//
+
+	private String q8 = ""//
+			+ "SELECT ?e ?l ?c " //
+			+ "{" //
+			+ "  ?e a ?l ."//
+			+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+			+ "  ?c <uri:talksTo> ?e . "//
+			+ "}";//
+
+	private String q9 = ""//
+			+ "SELECT ?f ?m ?d " //
+			+ "{" //
+			+ "  ?f a ?m ."//
+			+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+			+ "  ?d <uri:talksTo> ?f . "//
+			+ "}";//
+
+	private String q15 = ""//
+			+ "SELECT ?f ?m ?d ?e ?l ?c " //
+			+ "{" //
+			+ "  ?f a ?m ."//
+			+ "  ?e a ?l ."//
+			+ "  ?d <uri:talksTo> ?f . "//
+			+ "  ?c <uri:talksTo> ?e . "//
+			+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+			+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+			+ "}";//
+
+	@Test
+	public void testSingleIndex() throws Exception {
+
+		SPARQLParser parser = new SPARQLParser();
+
+		ParsedQuery pq1 = parser.parseQuery(q15, null);
+		ParsedQuery pq2 = parser.parseQuery(q7, null);
+		ParsedQuery pq3 = parser.parseQuery(q8, null);
+		ParsedQuery pq4 = parser.parseQuery(q9, null);
+
+		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				(Projection) pq3.getTupleExpr());
+		SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(
+				(Projection) pq4.getTupleExpr());
+
+		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+
+		list.add(extTup1);
+
+		List<QueryModelNode> optTupNodes = Lists.newArrayList();
+		optTupNodes.add(extTup2);
+		optTupNodes.add(extTup3);
+
+		IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
+				pq1.getTupleExpr(), list);
+
+		Iterator<TupleExpr> plans = new TupleExecutionPlanGenerator()
+				.getPlans(iep.getIndexedTuples());
+
+		IndexPlanValidator ipv = new IndexPlanValidator(false);
+
+		Iterator<TupleExpr> validPlans = ipv.getValidTuples(plans);
+
+		ThreshholdPlanSelector tps = new ThreshholdPlanSelector(
+				pq1.getTupleExpr());
+
+		TupleExpr optimalTup = tps.getThreshholdQueryPlan(validPlans, .1, 1, 0,
+				0);
+
+		NodeCollector nc = new NodeCollector();
+		optimalTup.visit(nc);
+
+		List<QueryModelNode> qNodes = nc.getNodes();
+
+		Assert.assertEquals(qNodes.size(), optTupNodes.size());
+		for (QueryModelNode node : qNodes) {
+			Assert.assertTrue(optTupNodes.contains(node));
+		}
+
+	}
+
+	@Test
+	public void testSingleIndex2() throws Exception {
+
+		String q1 = ""//
+				+ "SELECT ?f ?m ?d ?e ?l ?c " //
+				+ "{" //
+				+ "  ?f a ?m ."//
+				+ "  ?c a ?l ."//
+				+ "  ?d <uri:talksTo> ?f . "//
+				+ "  ?e <uri:talksTo> ?c . "//
+				+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+				+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?e ."//
+				+ "  ?m <uri:talksTo> ?e . "//
+				+ "}";//
+
+		String q2 = ""//
+				+ "SELECT ?u ?s ?t " //
+				+ "{" //
+				+ "  ?s a ?t ."//
+				+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+				+ "  ?u <uri:talksTo> ?s . "//
+				+ "}";//
+
+		String q3 = ""//
+				+ "SELECT ?e ?c ?l " //
+				+ "{" //
+				+ "  ?c a ?l ."//
+				+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?e ."//
+				+ "  ?e <uri:talksTo> ?c . "//
+				+ "}";//
+
+		String q4 = ""//
+				+ "SELECT ?d ?f ?m " //
+				+ "{" //
+				+ "  ?f a ?m ."//
+				+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+				+ "  ?d <uri:talksTo> ?f . "//
+				+ "}";//
+
+		SPARQLParser parser = new SPARQLParser();
+
+		ParsedQuery pq1 = parser.parseQuery(q1, null);
+		ParsedQuery pq2 = parser.parseQuery(q2, null);
+		ParsedQuery pq3 = parser.parseQuery(q3, null);
+		ParsedQuery pq4 = parser.parseQuery(q4, null);
+
+		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				(Projection) pq3.getTupleExpr());
+		SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(
+				(Projection) pq4.getTupleExpr());
+
+		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+
+		list.add(extTup1);
+
+		List<StatementPattern> spList = StatementPatternCollector.process(pq1
+				.getTupleExpr());
+		List<QueryModelNode> optTupNodes = Lists.newArrayList();
+		optTupNodes.add(extTup3);
+		optTupNodes.add(spList.get(6));
+		optTupNodes.add(extTup2);
+
+		IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
+				pq1.getTupleExpr(), list);
+
+		Iterator<TupleExpr> plans = new TupleExecutionPlanGenerator()
+				.getPlans(iep.getIndexedTuples());
+
+		IndexPlanValidator ipv = new IndexPlanValidator(true);
+		Iterator<TupleExpr> validPlans = ipv.getValidTuples(plans);
+
+		ThreshholdPlanSelector tps = new ThreshholdPlanSelector(
+				pq1.getTupleExpr());
+
+		TupleExpr optimalTup = tps.getThreshholdQueryPlan(validPlans, .4, .7,
+				.1, .2);
+
+		NodeCollector nc = new NodeCollector();
+		optimalTup.visit(nc);
+
+		List<QueryModelNode> qNodes = nc.getNodes();
+
+		Assert.assertTrue(qNodes.equals(optTupNodes));
+
+	}
+
+	@Test
+	public void testTwoIndex() throws Exception {
+
+		String q1 = ""//
+				+ "SELECT ?f ?m ?d ?h ?i " //
+				+ "{" //
+				+ "  ?f a ?m ."//
+				+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+				+ "  ?d <uri:talksTo> ?f . "//
+				+ "  ?d <uri:hangOutWith> ?f ." //
+				+ "  ?f <uri:hangOutWith> ?h ." //
+				+ "  ?f <uri:associatesWith> ?i ." //
+				+ "  ?i <uri:associatesWith> ?h ." //
+				+ "}";//
+
+		String q2 = ""//
+				+ "SELECT ?t ?s ?u " //
+				+ "{" //
+				+ "  ?s a ?t ."//
+				+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+				+ "  ?u <uri:talksTo> ?s . "//
+				+ "}";//
+
+		String q3 = ""//
+				+ "SELECT ?s ?t ?u " //
+				+ "{" //
+				+ "  ?s <uri:hangOutWith> ?t ." //
+				+ "  ?t <uri:hangOutWith> ?u ." //
+				+ "}";//
+
+		String q4 = ""//
+				+ "SELECT ?s ?t ?u " //
+				+ "{" //
+				+ "  ?s <uri:associatesWith> ?t ." //
+				+ "  ?t <uri:associatesWith> ?u ." //
+				+ "}";//
+
+		String q5 = ""//
+				+ "SELECT ?m ?f ?d " //
+				+ "{" //
+				+ "  ?f a ?m ."//
+				+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+				+ "  ?d <uri:talksTo> ?f . "//
+				+ "}";//
+
+		String q6 = ""//
+				+ "SELECT ?d ?f ?h " //
+				+ "{" //
+				+ "  ?d <uri:hangOutWith> ?f ." //
+				+ "  ?f <uri:hangOutWith> ?h ." //
+				+ "}";//
+
+		String q7 = ""//
+				+ "SELECT ?f ?i ?h " //
+				+ "{" //
+				+ "  ?f <uri:associatesWith> ?i ." //
+				+ "  ?i <uri:associatesWith> ?h ." //
+				+ "}";//
+
+		SPARQLParser parser = new SPARQLParser();
+
+		ParsedQuery pq1 = parser.parseQuery(q1, null);
+		ParsedQuery pq2 = parser.parseQuery(q2, null);
+		ParsedQuery pq3 = parser.parseQuery(q3, null);
+		ParsedQuery pq4 = parser.parseQuery(q4, null);
+		ParsedQuery pq5 = parser.parseQuery(q5, null);
+		ParsedQuery pq6 = parser.parseQuery(q6, null);
+		ParsedQuery pq7 = parser.parseQuery(q7, null);
+
+		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				(Projection) pq3.getTupleExpr());
+		SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(
+				(Projection) pq4.getTupleExpr());
+		SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet(
+				(Projection) pq5.getTupleExpr());
+		SimpleExternalTupleSet extTup5 = new SimpleExternalTupleSet(
+				(Projection) pq6.getTupleExpr());
+		SimpleExternalTupleSet extTup6 = new SimpleExternalTupleSet(
+				(Projection) pq7.getTupleExpr());
+
+		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+
+		list.add(extTup2);
+		list.add(extTup1);
+		list.add(extTup3);
+
+		List<QueryModelNode> optTupNodes = Lists.newArrayList();
+		optTupNodes.add(extTup4);
+		optTupNodes.add(extTup6);
+		optTupNodes.add(extTup5);
+
+		IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
+				pq1.getTupleExpr(), list);
+
+		Iterator<TupleExpr> plans = new TupleExecutionPlanGenerator()
+				.getPlans(iep.getIndexedTuples());
+		IndexPlanValidator ipv = new IndexPlanValidator(true);
+		Iterator<TupleExpr> validPlans = ipv.getValidTuples(plans);
+
+		ThreshholdPlanSelector tps = new ThreshholdPlanSelector(
+				pq1.getTupleExpr());
+		TupleExpr optimalTup = tps.getThreshholdQueryPlan(validPlans, .2, .6,
+				.4, 0);
+
+		NodeCollector nc = new NodeCollector();
+		optimalTup.visit(nc);
+
+		List<QueryModelNode> qNodes = nc.getNodes();
+
+		Assert.assertTrue(qNodes.equals(optTupNodes));
+
+	}
+
+	@Test
+	public void largeQueryFourtyIndexTest() {
+
+		String q1 = ""//
+				+ "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r " //
+				+ "{" //
+				+ "  ?f a ?m ."//
+				+ "  ?e a ?l ."//
+				+ "  ?n a ?o ."//
+				+ "  ?a a ?h ."//
+				+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+				+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
+				+ "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
+				+ "  ?d <uri:talksTo> ?f . "//
+				+ "  ?c <uri:talksTo> ?e . "//
+				+ "  ?p <uri:talksTo> ?n . "//
+				+ "  ?r <uri:talksTo> ?a . "//
+				+ "}";//
+
+		String q2 = ""//
+				+ "SELECT ?s ?t ?u " //
+				+ "{" //
+				+ "  ?s a ?t ."//
+				+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+				+ "  ?u <uri:talksTo> ?s . "//
+				+ "}";//
+
+		String q3 = ""//
+				+ "SELECT  ?s ?t ?u ?d ?f ?g " //
+				+ "{" //
+				+ "  ?s a ?t ."//
+				+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+				+ "  ?u <uri:talksTo> ?s . "//
+				+ "  ?d a ?f ."//
+				+ "  ?f <http://www.w3.org/2000/01/rdf-schema#label> ?g ."//
+				+ "  ?g <uri:talksTo> ?d . "//
+				+ "}";//
+
+		String q4 = ""//
+				+ "SELECT  ?s ?t ?u ?d ?f ?g ?a ?b ?c" //
+				+ "{" //
+				+ "  ?s a ?t ."//
+				+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+				+ "  ?u <uri:talksTo> ?s . "//
+				+ "  ?d a ?f ."//
+				+ "  ?f <http://www.w3.org/2000/01/rdf-schema#label> ?g ."//
+				+ "  ?g <uri:talksTo> ?d . "//
+				+ "  ?a a ?b ."//
+				+ "  ?b <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+				+ "  ?c <uri:talksTo> ?a . "//
+				+ "}";//
+
+		String q5 = ""//
+				+ "SELECT  ?f ?m ?d ?a ?h ?r " //
+				+ "{" //
+				+ "  ?f a ?m ."//
+				+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+				+ "  ?d <uri:talksTo> ?f . "//
+				+ "  ?a a ?h ."//
+				+ "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
+				+ "  ?r <uri:talksTo> ?a . "//
+				+ "}";//
+
+		String q6 = ""//
+				+ "SELECT ?e ?l ?c " //
+				+ "{" //
+				+ "  ?e a ?l ."//
+				+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+				+ "  ?c <uri:talksTo> ?e . "//
+				+ "}";//
+
+		String q7 = ""//
+				+ "SELECT ?n ?o ?p " //
+				+ "{" //
+				+ "  ?n a ?o ."//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
+				+ "  ?p <uri:talksTo> ?n . "//
+				+ "}";//
+
+		SPARQLParser parser = new SPARQLParser();
+
+		ParsedQuery pq1 = null;
+		ParsedQuery pq2 = null;
+		ParsedQuery pq3 = null;
+		ParsedQuery pq4 = null;
+		ParsedQuery pq5 = null;
+		ParsedQuery pq6 = null;
+		ParsedQuery pq7 = null;
+
+		try {
+			pq1 = parser.parseQuery(q1, null);
+			pq2 = parser.parseQuery(q2, null);
+			pq3 = parser.parseQuery(q3, null);
+			pq4 = parser.parseQuery(q4, null);
+			pq5 = parser.parseQuery(q5, null);
+			pq6 = parser.parseQuery(q6, null);
+			pq7 = parser.parseQuery(q7, null);
+
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+
+		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				(Projection) pq3.getTupleExpr());
+		SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(
+				(Projection) pq4.getTupleExpr());
+
+		SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet(
+				(Projection) pq5.getTupleExpr());
+		SimpleExternalTupleSet extTup5 = new SimpleExternalTupleSet(
+				(Projection) pq6.getTupleExpr());
+		SimpleExternalTupleSet extTup6 = new SimpleExternalTupleSet(
+				(Projection) pq7.getTupleExpr());
+
+		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+
+		list.add(extTup2);
+		list.add(extTup1);
+		list.add(extTup3);
+
+		List<ExternalTupleSet> list2 = new ArrayList<ExternalTupleSet>();
+
+		list2.add(extTup4);
+		list2.add(extTup5);
+		list2.add(extTup6);
+
+		IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
+				pq1.getTupleExpr(), list);
+
+		Iterator<TupleExpr> plans = new TupleExecutionPlanGenerator()
+				.getPlans(iep.getIndexedTuples());
+		IndexPlanValidator ipv = new IndexPlanValidator(false);
+		Iterator<TupleExpr> validPlans = ipv.getValidTuples(plans);
+
+		ThreshholdPlanSelector tps = new ThreshholdPlanSelector(
+				pq1.getTupleExpr());
+		TupleExpr optimalTup = tps.getThreshholdQueryPlan(validPlans, .4, .8,
+				.1, .1);
+
+		NodeCollector nc = new NodeCollector();
+		optimalTup.visit(nc);
+
+	}
+
+	@Test
+	public void twoIndexFilterTest() {
+
+		String q1 = ""//
+				+ "SELECT ?f ?m ?d ?e ?l ?c " //
+				+ "{" //
+				+ "  Filter(?f > \"5\")." //
+				+ "  Filter(?e > \"5\")." //
+				+ "  ?f a ?m ."//
+				+ "  ?e a ?l ."//
+				+ "  ?d <uri:talksTo> ?f . "//
+				+ "  ?c <uri:talksTo> ?e . "//
+				+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+				+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+				+ "}";//
+
+		String q2 = ""//
+				+ "SELECT ?s ?t ?u " //
+				+ "{" //
+				+ "  ?s a ?t ."//
+				+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+				+ "  ?u <uri:talksTo> ?s . "//
+				+ "}";//
+
+		String q3 = ""//
+				+ "SELECT ?s ?t ?u " //
+				+ "{" //
+				+ " Filter(?s > \"5\") ."//
+				+ "  ?s a ?t ."//
+				+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+				+ "  ?u <uri:talksTo> ?s . "//
+				+ "}";//
+
+		String q4 = ""//
+				+ "SELECT ?f ?m ?d " //
+				+ "{" //
+				+ " Filter(?f > \"5\") ."//
+				+ "  ?f a ?m ."//
+				+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+				+ "  ?d <uri:talksTo> ?f . "//
+				+ "}";//
+
+		String q5 = ""//
+				+ "SELECT ?e ?l ?c " //
+				+ "{" //
+				+ " Filter(?e > \"5\") ."//
+				+ "  ?e a ?l ."//
+				+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+				+ "  ?c <uri:talksTo> ?e . "//
+				+ "}";//
+
+		SPARQLParser parser = new SPARQLParser();
+
+		ParsedQuery pq1 = null;
+		ParsedQuery pq2 = null;
+		ParsedQuery pq3 = null;
+		ParsedQuery pq4 = null;
+		ParsedQuery pq5 = null;
+
+		try {
+			pq1 = parser.parseQuery(q1, null);
+			pq2 = parser.parseQuery(q2, null);
+			pq3 = parser.parseQuery(q3, null);
+			pq4 = parser.parseQuery(q4, null);
+			pq5 = parser.parseQuery(q5, null);
+
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+
+		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				(Projection) pq3.getTupleExpr());
+		SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(
+				(Projection) pq4.getTupleExpr());
+		SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet(
+				(Projection) pq5.getTupleExpr());
+
+		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+
+		list.add(extTup2);
+		list.add(extTup1);
+
+		List<ExternalTupleSet> list2 = new ArrayList<ExternalTupleSet>();
+
+		list2.add(extTup3);
+		list2.add(extTup4);
+
+		IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
+				pq1.getTupleExpr(), list);
+
+		Iterator<TupleExpr> plans = new TupleExecutionPlanGenerator()
+				.getPlans(iep.getIndexedTuples());
+		IndexPlanValidator ipv = new IndexPlanValidator(false);
+		Iterator<TupleExpr> validPlans = ipv.getValidTuples(plans);
+
+		ThreshholdPlanSelector tps = new ThreshholdPlanSelector(
+				pq1.getTupleExpr());
+		TupleExpr optimalTup = tps.getThreshholdQueryPlan(validPlans, .4, .8,
+				.1, .1);
+
+		NodeCollector nc = new NodeCollector();
+		optimalTup.visit(nc);
+
+		Assert.assertEquals(nc.getNodes().size(), list2.size());
+
+		for (QueryModelNode e : nc.getNodes()) {
+			Assert.assertTrue(list2.contains(e));
+		}
+
+	}
+
+	@Test
+	public void testCost1() throws Exception {
+
+		String q1 = ""//
+				+ "SELECT ?f ?m ?d ?h ?i " //
+				+ "{" //
+				+ "  ?f a ?m ."//
+				+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+				+ "  ?d <uri:talksTo> ?f . "//
+				+ "  ?d <uri:hangOutWith> ?f ." //
+				+ "  ?f <uri:hangOutWith> ?h ." //
+				+ "  ?f <uri:associatesWith> ?i ." //
+				+ "  ?i <uri:associatesWith> ?h ." //
+				+ "}";//
+
+		SPARQLParser parser = new SPARQLParser();
+
+		ParsedQuery pq1 = parser.parseQuery(q1, null);
+
+		ThreshholdPlanSelector tps = new ThreshholdPlanSelector(
+				pq1.getTupleExpr());
+		double cost = tps.getCost(pq1.getTupleExpr(), .6, .4, 0);
+		Assert.assertEquals(.7, cost, .01);
+
+	}
+
+	@Test
+	public void testCost2() throws Exception {
+
+		String q1 = ""//
+				+ "SELECT ?f ?m ?d ?e ?l ?c " //
+				+ "{" //
+				+ "  ?f a ?m ."//
+				+ "  ?e a ?l ."//
+				+ "  ?d <uri:talksTo> ?f . "//
+				+ "  ?c <uri:talksTo> ?e . "//
+				+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+				+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+				+ "}";//
+
+		SPARQLParser parser = new SPARQLParser();
+
+		ParsedQuery pq1 = parser.parseQuery(q1, null);
+
+		ThreshholdPlanSelector tps = new ThreshholdPlanSelector(
+				pq1.getTupleExpr());
+		double cost = tps.getCost(pq1.getTupleExpr(), .4, .3, .3);
+		Assert.assertEquals(.58, cost, .000000001);
+
+	}
+
+	@Test
+	public void testCost3() throws Exception {
+
+		String q1 = ""//
+				+ "SELECT ?f ?m ?d ?e ?l ?c " //
+				+ "{" //
+				+ "  Filter(?f > \"5\")." //
+				+ "  Filter(?e > \"6\")." //
+				+ "  ?f a ?m ."//
+				+ "  ?e a ?l ."//
+				+ "  ?d <uri:talksTo> ?f . "//
+				+ "  ?c <uri:talksTo> ?e . "//
+				+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+				+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+				+ "}";//
+
+		String q2 = ""//
+				+ "SELECT ?s ?t ?u " //
+				+ "{" //
+				+ "  ?s a ?t ."//
+				+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+				+ "  ?u <uri:talksTo> ?s . "//
+				+ "}";//
+
+		SPARQLParser parser = new SPARQLParser();
+
+		ParsedQuery pq1 = parser.parseQuery(q1, null);
+		ParsedQuery pq2 = parser.parseQuery(q2, null);
+
+		SimpleExternalTupleSet sep = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+		List<ExternalTupleSet> eList = Lists.newArrayList();
+		eList.add(sep);
+
+		final TupleExpr te = pq1.getTupleExpr().clone();
+		final PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(eList, false);
+        pcj.optimize(te, null, null);
+
+		ThreshholdPlanSelector tps = new ThreshholdPlanSelector(
+				pq1.getTupleExpr());
+		double cost = tps.getCost(te, .4, .3, .3);
+		Assert.assertEquals(.575, cost, .0001);
+
+	}
+
+	public static class NodeCollector extends
+			QueryModelVisitorBase<RuntimeException> {
+
+		List<QueryModelNode> qNodes = Lists.newArrayList();
+
+		public List<QueryModelNode> getNodes() {
+			return qNodes;
+		}
+
+		@Override
+		public void meetNode(QueryModelNode node) {
+			if (node instanceof StatementPattern
+					|| node instanceof ExternalTupleSet) {
+				qNodes.add(node);
+			}
+			super.meetNode(node);
+
+		}
 
+	}
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/TupleExecutionPlanGeneratorTest.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/TupleExecutionPlanGeneratorTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/TupleExecutionPlanGeneratorTest.java
index ffb7b2d..f0be8cb 100644
--- a/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/TupleExecutionPlanGeneratorTest.java
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/TupleExecutionPlanGeneratorTest.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.IndexPlanValidator;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -20,14 +20,12 @@ package mvm.rya.indexing.IndexPlanValidator;
  */
 
 
-import static org.junit.Assert.*;
 
 import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
-import junit.framework.Assert;
-
+import org.junit.Assert;
 import org.junit.Test;
 import org.openrdf.query.MalformedQueryException;
 import org.openrdf.query.algebra.TupleExpr;
@@ -39,8 +37,8 @@ import com.beust.jcommander.internal.Sets;
 
 public class TupleExecutionPlanGeneratorTest {
 
-    
-    
+
+
     private String q1 = ""//
             + "SELECT ?s ?t ?u " //
             + "{" //
@@ -48,8 +46,8 @@ public class TupleExecutionPlanGeneratorTest {
             + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
             + "  ?u <uri:talksTo> ?s . "//
             + "}";//
-    
-    
+
+
     private String q2 = ""//
             + "SELECT ?s ?t ?u " //
             + "{" //
@@ -57,8 +55,8 @@ public class TupleExecutionPlanGeneratorTest {
             + "  ?s a ?t ."//
             + "  ?u <uri:talksTo> ?s . "//
             + "}";//
-    
-    
+
+
     private String q3 = ""//
             + "SELECT ?s ?t ?u " //
             + "{" //
@@ -66,8 +64,8 @@ public class TupleExecutionPlanGeneratorTest {
             + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
             + "  ?s a ?t ."//
             + "}";//
-    
-    
+
+
     private String q4 = ""//
             + "SELECT ?s ?t ?u " //
             + "{" //
@@ -75,8 +73,8 @@ public class TupleExecutionPlanGeneratorTest {
             + "  ?u <uri:talksTo> ?s . "//
              + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
             + "}";//
-    
-    
+
+
     private String q5 = ""//
             + "SELECT ?s ?t ?u " //
             + "{" //
@@ -84,8 +82,8 @@ public class TupleExecutionPlanGeneratorTest {
             + "  ?u <uri:talksTo> ?s . "//
             + "  ?s a ?t ."//
             + "}";//
-    
-    
+
+
     private String q6 = ""//
             + "SELECT ?s ?t ?u " //
             + "{" //
@@ -93,30 +91,30 @@ public class TupleExecutionPlanGeneratorTest {
             + "  ?s a ?t ."//
             + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
             + "}";//
-    
-    
-    
-    
+
+
+
+
     private String q7 = ""//
             + "SELECT ?s ?t ?u " //
             + "{" //
             + "  ?s a ?t ."//
             + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
             + "}";//
-    
-    
+
+
     private String q8 = ""//
             + "SELECT ?s ?t ?u " //
             + "{" //
             + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
             + "  ?s a ?t ."//
             + "}";//
-    
-    
-    
-    
-    
-    
+
+
+
+
+
+
     private String q9 = ""//
             + "SELECT ?s ?t ?u " //
             + "{" //
@@ -126,8 +124,8 @@ public class TupleExecutionPlanGeneratorTest {
             + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
             + "  ?u <uri:talksTo> ?s . "//
             + "}";//
-    
-    
+
+
     private String q10 = ""//
             + "SELECT ?s ?t ?u " //
             + "{" //
@@ -137,8 +135,8 @@ public class TupleExecutionPlanGeneratorTest {
             + "  ?s a ?t ."//
             + "  ?u <uri:talksTo> ?s . "//
             + "}";//
-    
-    
+
+
     private String q11 = ""//
             + "SELECT ?s ?t ?u " //
             + "{" //
@@ -148,8 +146,8 @@ public class TupleExecutionPlanGeneratorTest {
             + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
             + "  ?s a ?t ."//
             + "}";//
-    
-    
+
+
     private String q12 = ""//
             + "SELECT ?s ?t ?u " //
             + "{" //
@@ -159,8 +157,8 @@ public class TupleExecutionPlanGeneratorTest {
             + "  ?u <uri:talksTo> ?s . "//
             + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
             + "}";//
-    
-    
+
+
     private String q13 = ""//
             + "SELECT ?s ?t ?u " //
             + "{" //
@@ -170,8 +168,8 @@ public class TupleExecutionPlanGeneratorTest {
             + "  ?u <uri:talksTo> ?s . "//
             + "  ?s a ?t ."//
             + "}";//
-    
-    
+
+
     private String q14 = ""//
             + "SELECT ?s ?t ?u " //
             + "{" //
@@ -181,8 +179,8 @@ public class TupleExecutionPlanGeneratorTest {
             + "  ?s a ?t ."//
             + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
             + "}";//
-    
-    
+
+
     private String q15 = ""//
             + "SELECT ?s ?t ?u " //
             + "{" //
@@ -192,10 +190,10 @@ public class TupleExecutionPlanGeneratorTest {
             + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
             + "  ?u <uri:talksTo> ?s . "//
             + "}";//
-    
-    
-    
-    
+
+
+
+
     @Test
     public void testTwoNodeOrder() {
 
@@ -217,11 +215,11 @@ public class TupleExecutionPlanGeneratorTest {
 
         TupleExecutionPlanGenerator tep = new TupleExecutionPlanGenerator();
         Iterator<TupleExpr> processedTups = tep.getPlans(tupSet.iterator());
-        
+
         List<TupleExpr> processedTupList = Lists.newArrayList();
-        
+
         int size = 0;
-        
+
         while(processedTups.hasNext()) {
             Assert.assertTrue(processedTups.hasNext());
             processedTupList.add(processedTups.next());
@@ -234,11 +232,11 @@ public class TupleExecutionPlanGeneratorTest {
         Assert.assertTrue(processedTupList.get(1).equals(pq1.getTupleExpr()));
 
     }
-    
-    
-    
-    
-    
+
+
+
+
+
     @Test
     public void testThreeNodeOrder() {
 
@@ -270,9 +268,9 @@ public class TupleExecutionPlanGeneratorTest {
         Iterator<TupleExpr> processedTups= tep.getPlans(tupSet.iterator());
 
         List<TupleExpr> processedTupList = Lists.newArrayList();
-        
+
         int size = 0;
-        
+
         while(processedTups.hasNext()) {
             Assert.assertTrue(processedTups.hasNext());
             processedTupList.add(processedTups.next());
@@ -290,8 +288,8 @@ public class TupleExecutionPlanGeneratorTest {
         Assert.assertTrue(processedTupList.get(3).equals(pq6.getTupleExpr()));
 
     }
-    
-    
+
+
 
     @Test
     public void testThreeNodeOrderFilter() {
@@ -318,7 +316,7 @@ public class TupleExecutionPlanGeneratorTest {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
-        
+
         System.out.println(pq1.getTupleExpr());
 
         Set<TupleExpr> tupSet = Sets.newHashSet();
@@ -328,11 +326,11 @@ public class TupleExecutionPlanGeneratorTest {
         Iterator<TupleExpr> processedTups= tep.getPlans(tupSet.iterator());
 
         List<TupleExpr> processedTupList = Lists.newArrayList();
-        
+
         int size = 0;
-        
+
         while(processedTups.hasNext()) {
-            
+
             Assert.assertTrue(processedTups.hasNext());
             TupleExpr te = processedTups.next();
             processedTupList.add(te);
@@ -351,14 +349,14 @@ public class TupleExecutionPlanGeneratorTest {
         Assert.assertTrue(processedTupList.get(3).equals(pq6.getTupleExpr()));
 
     }
-    
-    
-
-    
-    
-    
-    
-    
-    
+
+
+
+
+
+
+
+
+
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/TupleReArrangerTest.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/TupleReArrangerTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/TupleReArrangerTest.java
index 96466cb..aaa4819 100644
--- a/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/TupleReArrangerTest.java
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/TupleReArrangerTest.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.IndexPlanValidator;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -22,8 +22,7 @@ package mvm.rya.indexing.IndexPlanValidator;
 
 import java.util.List;
 
-import junit.framework.Assert;
-
+import org.junit.Assert;
 import org.junit.Test;
 import org.openrdf.query.MalformedQueryException;
 import org.openrdf.query.algebra.TupleExpr;
@@ -35,27 +34,27 @@ public class TupleReArrangerTest {
 
     @Test
     public void tupleReArrangeTest1() throws MalformedQueryException {
-        
+
         String queryString = ""//
                 + "SELECT ?a ?b ?c ?d ?e" //
                 + "{" //
                 + "{ ?a a ?b .  ?a <http://www.w3.org/2000/01/rdf-schema#label> ?c  }"//
                 + " UNION { ?a <uri:talksTo> ?d .  ?a <http://www.w3.org/2000/01/rdf-schema#label> ?e  }"//
                 + "}";//
-        
+
         SPARQLParser sp = new SPARQLParser();
         ParsedQuery pq = sp.parseQuery(queryString, null);
         List<TupleExpr> tuples = TupleReArranger.getTupleReOrderings(pq.getTupleExpr());
-        
+
         Assert.assertEquals(4, tuples.size());
-        
+
     }
-    
-    
-    
+
+
+
     @Test
     public void tupleReArrangeTest2() throws MalformedQueryException {
-        
+
         String queryString = ""//
                 + "SELECT ?a ?b ?c ?d ?e ?x ?y" //
                 + "{" //
@@ -64,23 +63,23 @@ public class TupleReArrangerTest {
                 + "{ ?a a ?b .  ?a <http://www.w3.org/2000/01/rdf-schema#label> ?c  }"//
                 + " UNION { ?a <uri:talksTo> ?d .  ?a <http://www.w3.org/2000/01/rdf-schema#label> ?e  }"//
                 + "}";//
-        
+
         SPARQLParser sp = new SPARQLParser();
         ParsedQuery pq = sp.parseQuery(queryString, null);
         List<TupleExpr> tuples = TupleReArranger.getTupleReOrderings(pq.getTupleExpr());
-        
-        
+
+
         Assert.assertEquals(24, tuples.size());
-        
+
     }
-    
-    
-    
-    
-    
+
+
+
+
+
     @Test
     public void tupleReArrangeTest3() throws MalformedQueryException {
-        
+
         String queryString = ""//
                 + "SELECT ?a ?b ?c ?d ?e ?x ?y" //
                 + "{" //
@@ -91,24 +90,24 @@ public class TupleReArrangerTest {
                 + "{ ?a a ?b .  ?a <http://www.w3.org/2000/01/rdf-schema#label> ?c  }"//
                 + " UNION { ?a <uri:talksTo> ?d .  ?a <http://www.w3.org/2000/01/rdf-schema#label> ?e  }"//
                 + "}";//
-        
+
         SPARQLParser sp = new SPARQLParser();
         ParsedQuery pq = sp.parseQuery(queryString, null);
         List<TupleExpr> tuples = TupleReArranger.getTupleReOrderings(pq.getTupleExpr());
-        
+
         Assert.assertEquals(24, tuples.size());
-        
+
     }
-    
-    
-    
-    
-    
-    
-    
+
+
+
+
+
+
+
     @Test
     public void tupleReArrangeTest4() throws MalformedQueryException {
-        
+
         String queryString = ""//
                 + "SELECT ?a ?b ?c ?d ?e ?x ?y" //
                 + "{" //
@@ -120,22 +119,22 @@ public class TupleReArrangerTest {
                 + "{ ?a a ?b .  ?a <http://www.w3.org/2000/01/rdf-schema#label> ?c  }"//
                 + " UNION { ?a <uri:talksTo> ?d .  ?a <http://www.w3.org/2000/01/rdf-schema#label> ?e  }"//
                 + "}";//
-        
+
         SPARQLParser sp = new SPARQLParser();
         ParsedQuery pq = sp.parseQuery(queryString, null);
         TupleExpr te = pq.getTupleExpr();
-        (new FilterOptimizer()).optimize(te, null, null);
+        new FilterOptimizer().optimize(te, null, null);
         System.out.println(te);
         List<TupleExpr> tuples = TupleReArranger.getTupleReOrderings(te);
         System.out.println(tuples);
-        
+
         Assert.assertEquals(24, tuples.size());
-        
+
     }
-    
-    
-    
-    
-    
+
+
+
+
+
 
 }


[12/16] incubator-rya git commit: RYA-32 Improve how metadata and values are written to Accumulo PCJ tables

Posted by mi...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/AccumuloPcjSerializer.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/AccumuloPcjSerializer.java b/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/AccumuloPcjSerializer.java
new file mode 100644
index 0000000..5aefc40
--- /dev/null
+++ b/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/AccumuloPcjSerializer.java
@@ -0,0 +1,103 @@
+package mvm.rya.indexing.external.tupleSet;
+
+import static mvm.rya.api.RdfCloudTripleStoreConstants.DELIM_BYTE;
+import static mvm.rya.api.RdfCloudTripleStoreConstants.DELIM_BYTES;
+import static mvm.rya.api.RdfCloudTripleStoreConstants.TYPE_DELIM_BYTE;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import mvm.rya.api.domain.RyaType;
+import mvm.rya.api.resolver.RdfToRyaConversions;
+import mvm.rya.api.resolver.RyaContext;
+import mvm.rya.api.resolver.RyaToRdfConversions;
+import mvm.rya.api.resolver.RyaTypeResolverException;
+
+import org.openrdf.model.Value;
+import org.openrdf.query.BindingSet;
+import org.openrdf.query.algebra.evaluation.QueryBindingSet;
+
+import com.google.common.base.Preconditions;
+import com.google.common.primitives.Bytes;
+
+/**
+ * AccumuloPcjSerializer provides two methods, serialize and deserialize, which
+ * are used for writing BindingSets to PCJ tables and reading serialized byte
+ * representations of BindingSets from PCJ tables.
+ *
+ */
+public class AccumuloPcjSerializer {
+
+	/**
+	 *
+	 * @param bs {@link BindingSet} to be serialized
+	 * @param varOrder order in which binding values should be written to byte array
+	 * @return byte array containing serialized values written in order indicated by varOrder
+	 * @throws RyaTypeResolverException
+	 */
+	public static byte[] serialize(BindingSet bs, String[] varOrder) throws RyaTypeResolverException {
+		byte[] byteArray = null;
+		int i = 0;
+		Preconditions.checkNotNull(bs);
+		Preconditions.checkNotNull(varOrder);
+		Preconditions.checkArgument(bs.size() == varOrder.length);
+		for(final String varName: varOrder) {
+			final RyaType rt = RdfToRyaConversions.convertValue(bs.getBinding(varName).getValue());
+			final byte[][] serializedVal = RyaContext.getInstance().serializeType(rt);
+			if(i == 0) {
+				byteArray = Bytes.concat(serializedVal[0], serializedVal[1], DELIM_BYTES);
+			} else {
+				byteArray = Bytes.concat(byteArray, serializedVal[0], serializedVal[1], DELIM_BYTES);
+			}
+			i++;
+		}
+
+		return byteArray;
+	}
+
+	/**
+	 *
+	 * @param row byte rowId (read from Accumulo {@link Key})
+	 * @param varOrder indicates the order in which values are written in row
+	 * @return {@link BindingSet} formed from serialized values in row and variables in varOrder
+	 * @throws RyaTypeResolverException
+	 */
+	public static BindingSet deSerialize(byte[] row, String[] varOrder) throws RyaTypeResolverException {
+		Preconditions.checkNotNull(row);
+		Preconditions.checkNotNull(varOrder);
+		final int lastIndex = Bytes.lastIndexOf(row, DELIM_BYTE);
+		Preconditions.checkArgument(lastIndex >= 0);
+		final List<byte[]> byteList = getByteValues(Arrays.copyOf(row, lastIndex), new ArrayList<byte[]>());
+		final QueryBindingSet bs = new QueryBindingSet();
+		Preconditions.checkArgument(byteList.size() == varOrder.length);
+		for(int i = 0; i < byteList.size(); i++) {
+			bs.addBinding(varOrder[i], getValue(byteList.get(i)));
+		}
+		return bs;
+	}
+
+	private static List<byte[]> getByteValues(byte[] row, List<byte[]> byteList) {
+		 final int firstIndex = Bytes.indexOf(row, DELIM_BYTE);
+		 if(firstIndex < 0) {
+			 byteList.add(row);
+			 return byteList;
+		 } else {
+			 byteList.add(Arrays.copyOf(row, firstIndex));
+			 getByteValues(Arrays.copyOfRange(row, firstIndex+1, row.length), byteList);
+		 }
+
+		 return byteList;
+	}
+
+	private static Value getValue(byte[] byteVal) throws RyaTypeResolverException {
+
+		 final int typeIndex = Bytes.indexOf(byteVal, TYPE_DELIM_BYTE);
+		 Preconditions.checkArgument(typeIndex >= 0);
+		 final byte[] data = Arrays.copyOf(byteVal, typeIndex);
+		 final byte[] type = Arrays.copyOfRange(byteVal, typeIndex, byteVal.length);
+		 final RyaType rt = RyaContext.getInstance().deserialize(Bytes.concat(data,type));
+		 return RyaToRdfConversions.convertValue(rt);
+
+	}
+}

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/ExternalTupleSet.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/ExternalTupleSet.java b/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/ExternalTupleSet.java
index 0e2096d..ddf691d 100644
--- a/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/ExternalTupleSet.java
+++ b/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/ExternalTupleSet.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.external.tupleSet;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -23,37 +23,51 @@ package mvm.rya.indexing.external.tupleSet;
 
 import info.aduna.iteration.CloseableIteration;
 
+import java.util.Collection;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import org.openrdf.query.BindingSet;
 import org.openrdf.query.QueryEvaluationException;
 import org.openrdf.query.algebra.Projection;
-import org.openrdf.query.algebra.Var;
+import org.openrdf.query.algebra.TupleExpr;
 import org.openrdf.query.algebra.evaluation.impl.ExternalSet;
-import org.openrdf.query.algebra.helpers.QueryModelVisitorBase;
 
 import com.beust.jcommander.internal.Sets;
 import com.google.common.base.Joiner;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.BiMap;
+import com.google.common.collect.HashBiMap;
+import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 
 /**
- * Abstract class for an External Tuple Set.  This Tuple 
+ * This is an abstract class of delegating the evaluation of part
+ * of a SPARQL query to an external source.  The {@link TupleExpr} returned by {@link ExternalTupleSet#getTupleExpr()}
+ * represents the SPARQL string that this node evaluates, and table returned by {@link ExternalTupleSet#getTableVarMap()}
+ * maps the variables of TupleExpr to the variables stored in the external store (which may be different).  The map
+ * returned by {@link ExternalTupleSet#getSupportedVariableOrderMap()} provides a map of all the variable orders in which
+ * data is written to the supporting, and is useful for determining which {@link BindingSet} can be passed into
+ * {@link ExternalTupleSet#evaluate(BindingSet)}.
+ *
  */
 public abstract class ExternalTupleSet extends ExternalSet {
 
-    private Projection tupleExpr;
-    private Map<String, String> tableVarMap = Maps.newHashMap();
-    private Map<String, Set<String>> supportedVarOrders = Maps.newHashMap();
+	public static final String VAR_ORDER_DELIM = ";";
+	public static final String CONST_PREFIX = "-const-";
+	private Projection tupleExpr;
+    private Map<String, String> tableVarMap = Maps.newHashMap();  //maps vars in tupleExpr to var in stored binding sets
+    private Map<String, Set<String>> supportedVarOrders = Maps.newHashMap(); //indicates supported var orders
 
-    
     public ExternalTupleSet() {
-    	
     }
-    
+
     public ExternalTupleSet(Projection tupleExpr) {
+    	Preconditions.checkNotNull(tupleExpr);
         this.tupleExpr = tupleExpr;
+        updateTableVarMap(tupleExpr, tupleExpr);
     }
 
     @Override
@@ -79,135 +93,178 @@ public abstract class ExternalTupleSet extends ExternalSet {
     }
 
     public void setProjectionExpr(Projection tupleExpr) {
+    	Preconditions.checkNotNull(tupleExpr);
+    	if(this.tupleExpr == null) {
+    		updateTableVarMap(tupleExpr, tupleExpr);
+    	} else {
+    		updateTableVarMap(tupleExpr, this.tupleExpr);
+    	}
         this.tupleExpr = tupleExpr;
+		if (supportedVarOrders.size() != 0) {
+			updateSupportedVarOrderMap();
+		}
     }
-    
-    
+
     public void setTableVarMap(Map<String,String> vars) {
+    	Preconditions.checkNotNull(vars);
         this.tableVarMap = vars;
     }
-    
-    
+
     public Map<String, String> getTableVarMap() {
         return this.tableVarMap;
     }
-    
-    
+
     public void setSupportedVariableOrderMap(Map<String, Set<String>> varOrders) {
+    	Preconditions.checkNotNull(varOrders);
         this.supportedVarOrders = varOrders;
     }
-    
-    
+
+    public void setSupportedVariableOrderMap(List<String> varOrders) {
+    	Preconditions.checkNotNull(varOrders);
+    	this.supportedVarOrders = createSupportedVarOrderMap(varOrders);
+    }
+
     public Map<String, Set<String>> getSupportedVariableOrderMap() {
         return supportedVarOrders;
     }
-    
-
-    public void updateTupleExp(final Map<Var, Var> oldToNewBindings) {
-        tupleExpr.visit(new QueryModelVisitorBase<RuntimeException>() {
-            @Override
-            public void meet(Var var) {
-                if (oldToNewBindings.containsKey(var)) {
-                    var.replaceWith(oldToNewBindings.get(var));
-                }
-            }
-        });
-    }
 
     @Override
     public ExternalSet clone() {
-        ExternalTupleSet clone = (ExternalTupleSet) super.clone();
+        final ExternalTupleSet clone = (ExternalTupleSet) super.clone();
         clone.tupleExpr = this.tupleExpr.clone();
         clone.tableVarMap = Maps.newHashMap();
-        for(String s: this.tableVarMap.keySet()) {
+        for(final String s: this.tableVarMap.keySet()) {
             clone.tableVarMap.put(s,this.tableVarMap.get(s));
         }
         clone.supportedVarOrders = Maps.newHashMap();
-        for(String s: this.supportedVarOrders.keySet()) {
+        for(final String s: this.supportedVarOrders.keySet()) {
             clone.supportedVarOrders.put(s,this.supportedVarOrders.get(s));
         }
         return clone;
     }
-    
-    
-    public Map<String, Set<String>> getSupportedVariableOrders() {
-        
-        if (supportedVarOrders.size() != 0) {
-            return supportedVarOrders;
-        } else {
 
-            Set<String> varSet = Sets.newHashSet();
-            String t = "";
+	public Map<String, Set<String>> getSupportedVariableOrders() {
+		return supportedVarOrders;
+	}
 
-            for (String s : tupleExpr.getAssuredBindingNames()) {
-                if (t.length() == 0) {
-                    t = s;
-                } else {
-                    t = t + "\u0000" + s;
-                }
+	public boolean supportsBindingSet(Set<String> bindingNames) {
+		final Collection<Set<String>> values = supportedVarOrders.values();
+		final Set<String> bNames = Sets.newHashSet();
+		final Set<String> bNamesWithConstants = Sets.newHashSet();
 
-                varSet.add(s);
-                supportedVarOrders.put(t, new HashSet<String>(varSet));
+		for (final String s : this.getTupleExpr().getAssuredBindingNames()) {
+			if (bindingNames.contains(s)) {
+				bNames.add(s);
+				bNamesWithConstants.add(s);
+			} else if(s.startsWith(CONST_PREFIX)) {
+				bNamesWithConstants.add(s);
+			}
+		}
+		return values.contains(bNames) || values.contains(bNamesWithConstants);
+	}
 
-            }
+	/**
+	 * @param tupleMatch
+	 *            - project expression - call after setting {@link tupleExpr} to
+	 *            map new variables to old -- the variables in the binding list
+	 *            of the new tupleExpr (tupleMatch) must map to the
+	 *            corresponding variables in the binding list of the old
+	 *            tupleExpr
+	 */
+	private void updateTableVarMap(TupleExpr newTuple, TupleExpr oldTuple) {
 
-            return supportedVarOrders;
-        }
-    }
-    
-    
-    
-    
-    public boolean supportsBindingSet(Set<String> bindingNames) {
-
-        Map<String, Set<String>> varOrderMap = getSupportedVariableOrders();
-        String bNames = "";
-
-        for (String s : tupleExpr.getAssuredBindingNames()) {
-            if (bindingNames.contains(s)) {
-                if(bNames.length() == 0) {
-                    bNames = s;
-                } else {
-                    bNames = bNames + "\u0000"+ s;
-                }
-            }
-        }
+		final List<String> replacementVars = Lists.newArrayList(newTuple
+				.getBindingNames());
+		final List<String> tableVars = Lists.newArrayList(oldTuple
+				.getBindingNames());
+
+		final Map<String, String> tableMap = Maps.newHashMap();
+
+		for (int i = 0; i < tableVars.size(); i++) {
+			tableMap.put(replacementVars.get(i), tableVars.get(i));
+		}
+		this.setTableVarMap(tableMap);
+	}
+
+	/**
+	 * call after setting {@link tableVarMap} to update map of supported
+	 * variables in terms of variables in new tupleExpr
+	 */
+	private void updateSupportedVarOrderMap() {
+
+		Preconditions.checkArgument(supportedVarOrders.size() != 0);;
+		final Map<String, Set<String>> newSupportedOrders = Maps.newHashMap();
+		final BiMap<String, String> biMap = HashBiMap.create(tableVarMap)
+				.inverse();
+		Set<String> temp = null;
+		final Set<String> keys = supportedVarOrders.keySet();
+
+		for (final String s : keys) {
+			temp = supportedVarOrders.get(s);
+			final Set<String> newSet = Sets.newHashSet();
+
+			for (final String t : temp) {
+				newSet.add(biMap.get(t));
+			}
+
+			final String[] tempStrings = s.split(VAR_ORDER_DELIM);
+			String v = "";
+			for (final String u : tempStrings) {
+				if (v.length() == 0) {
+					v = v + biMap.get(u);
+				} else {
+					v = v + VAR_ORDER_DELIM + biMap.get(u);
+				}
+			}
+			newSupportedOrders.put(v, newSet);
+		}
+		supportedVarOrders = newSupportedOrders;
+	}
+
+	 /**
+    *
+    * @param orders
+    * @return - map with all possible orders in which results are written to the table
+    */
+   private Map<String, Set<String>> createSupportedVarOrderMap(List<String> orders) {
+	   final Map<String, Set<String>> supportedVars = Maps.newHashMap();
+
+       for (final String t : orders) {
+           final String[] tempOrder = t.split(VAR_ORDER_DELIM);
+           final Set<String> varSet = Sets.newHashSet();
+           String u = "";
+           for (final String s : tempOrder) {
+               if(u.length() == 0) {
+                   u = s;
+               } else{
+                   u = u+ VAR_ORDER_DELIM + s;
+               }
+               varSet.add(s);
+               supportedVars.put(u, new HashSet<String>(varSet));
+           }
+       }
+       return supportedVars;
+   }
 
-        return varOrderMap.containsKey(bNames);
-    }
-        
-        
-    
     @Override
     public boolean equals(Object other) {
-
         if (!(other instanceof ExternalTupleSet)) {
             return false;
         } else {
-
-            ExternalTupleSet arg = (ExternalTupleSet) other;
+            final ExternalTupleSet arg = (ExternalTupleSet) other;
             if (this.getTupleExpr().equals(arg.getTupleExpr())) {
                 return true;
             } else {
                 return false;
             }
-
         }
-
     }
-    
-    
+
     @Override
     public int hashCode() {
         int result = 17;
         result = 31*result + tupleExpr.hashCode();
-        
         return result;
     }
-    
-    
- 
-    
-    
-    
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/PcjTables.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/PcjTables.java b/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/PcjTables.java
new file mode 100644
index 0000000..e422cba
--- /dev/null
+++ b/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/PcjTables.java
@@ -0,0 +1,800 @@
+package mvm.rya.indexing.external.tupleSet;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.Set;
+
+import javax.annotation.ParametersAreNonnullByDefault;
+import javax.annotation.concurrent.Immutable;
+
+import mvm.rya.api.resolver.RyaTypeResolverException;
+
+import org.apache.accumulo.core.client.AccumuloException;
+import org.apache.accumulo.core.client.AccumuloSecurityException;
+import org.apache.accumulo.core.client.BatchWriter;
+import org.apache.accumulo.core.client.BatchWriterConfig;
+import org.apache.accumulo.core.client.ConditionalWriter;
+import org.apache.accumulo.core.client.ConditionalWriterConfig;
+import org.apache.accumulo.core.client.Connector;
+import org.apache.accumulo.core.client.MutationsRejectedException;
+import org.apache.accumulo.core.client.Scanner;
+import org.apache.accumulo.core.client.TableExistsException;
+import org.apache.accumulo.core.client.TableNotFoundException;
+import org.apache.accumulo.core.client.admin.TableOperations;
+import org.apache.accumulo.core.client.lexicoder.ListLexicoder;
+import org.apache.accumulo.core.client.lexicoder.LongLexicoder;
+import org.apache.accumulo.core.client.lexicoder.StringLexicoder;
+import org.apache.accumulo.core.data.Condition;
+import org.apache.accumulo.core.data.ConditionalMutation;
+import org.apache.accumulo.core.data.Key;
+import org.apache.accumulo.core.data.Mutation;
+import org.apache.accumulo.core.data.Value;
+import org.apache.accumulo.core.security.Authorizations;
+import org.apache.commons.lang3.builder.EqualsBuilder;
+import org.apache.hadoop.io.Text;
+import org.apache.log4j.Logger;
+import org.openrdf.query.BindingSet;
+import org.openrdf.query.MalformedQueryException;
+import org.openrdf.query.QueryEvaluationException;
+import org.openrdf.query.QueryLanguage;
+import org.openrdf.query.TupleQuery;
+import org.openrdf.query.TupleQueryResult;
+import org.openrdf.repository.RepositoryConnection;
+import org.openrdf.repository.RepositoryException;
+
+import com.google.common.base.Joiner;
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Lists;
+
+/**
+ * Functions that create and maintain the PCJ tables that are used by Rya.
+ */
+@ParametersAreNonnullByDefault
+public class PcjTables {
+    private static final Logger log = Logger.getLogger(PcjTables.class);
+
+    /**
+     * The Row ID of all {@link PcjMetadata} entries that are stored in Accumulo.
+     */
+    private static final Text PCJ_METADATA_ROW_ID = new Text("pcjMetadata");
+
+    /**
+     * The Column Family for all PCJ metadata entries.
+     */
+    private static final Text PCJ_METADATA_FAMILY = new Text("metadata");
+
+    /**
+     * The Column Qualifier for the SPARQL query a PCJ is built from.
+     */
+    private static final Text PCJ_METADATA_SPARQL_QUERY = new Text("sparql");
+
+    /**
+     * The Column Qualifier for the cardinality of a PCJ.
+     */
+    private static final Text PCJ_METADATA_CARDINALITY = new Text("cardinality");
+
+    /**
+     * The Column Qualifier for the various variable orders a PCJ's results are written to.
+     */
+    private static final Text PCJ_METADATA_VARIABLE_ORDERS = new Text("variableOrders");
+
+    // Lexicoders used to read/write PcjMetadata to/from Accumulo.
+    private static final LongLexicoder longLexicoder = new LongLexicoder();
+    private static final StringLexicoder stringLexicoder = new StringLexicoder();
+    private static final ListLexicoder<String> listLexicoder = new ListLexicoder<>(stringLexicoder);
+
+    /**
+     * An ordered list of {@link BindingSet} variable names. These are used to
+     * specify the order {@link Binding}s within the set are serialized to Accumulo.
+     * This order effects which rows a prefix scan will hit.
+     */
+    @Immutable
+    @ParametersAreNonnullByDefault
+    public static final class VariableOrder implements Iterable<String> {
+
+        public static final String VAR_ORDER_DELIM = ";";
+
+        private final ImmutableList<String> variableOrder;
+
+        /**
+         * Constructs an instance of {@link VariableOrder}.
+         *
+         * @param varOrder - An ordered array of Binding Set variables. (not null)
+         */
+        public VariableOrder(String... varOrder) {
+            checkNotNull(varOrder);
+            this.variableOrder = ImmutableList.copyOf(varOrder);
+        }
+
+        /**
+         * Constructs an instance of {@link VariableOrder}.
+         *
+         * @param varOrderString - The String representation of a VariableOrder. (not null)
+         */
+        public VariableOrder(String varOrderString) {
+            checkNotNull(varOrderString);
+            this.variableOrder = ImmutableList.copyOf( varOrderString.split(VAR_ORDER_DELIM) );
+        }
+
+        /**
+         * @return And ordered list of Binding Set variables.
+         */
+        public ImmutableList<String> getVariableOrders() {
+            return variableOrder;
+        }
+
+        /**
+         * @return The variable order as an ordered array of Strings. This array is mutable.
+         */
+        public String[] toArray() {
+            String[] array = new String[ variableOrder.size() ];
+            return variableOrder.toArray( array );
+        }
+
+        @Override
+        public String toString() {
+            return Joiner.on(VAR_ORDER_DELIM).join(variableOrder);
+        }
+
+        @Override
+        public int hashCode() {
+            return variableOrder.hashCode();
+        }
+
+        @Override
+        public boolean equals(Object o) {
+            if(this == o) {
+                return true;
+            } else if(o instanceof VariableOrder) {
+                VariableOrder varOrder = (VariableOrder) o;
+                return variableOrder.equals( varOrder.variableOrder );
+            }
+            return false;
+        }
+
+        @Override
+        public Iterator<String> iterator() {
+            return variableOrder.iterator();
+        }
+    }
+
+    /**
+     * Metadata that is stored in a PCJ table about the results that are stored within it.
+     */
+    @Immutable
+    @ParametersAreNonnullByDefault
+    public static final class PcjMetadata {
+        private final String sparql;
+        private final long cardinality;
+        private final ImmutableSet<VariableOrder> varOrders;
+
+        /**
+         * Constructs an instance of {@link PcjMetadata}.
+         *
+         * @param sparql - The SPARQL query this PCJ solves. (not null)
+         * @param cardinality  - The number of results the PCJ has. (>= 0)
+         * @param varOrders - Strings that describe each of the variable orders
+         *   the results are stored in. (not null)
+         */
+        public PcjMetadata(final String sparql, final long cardinality, final Collection<VariableOrder> varOrders) {
+            this.sparql = checkNotNull(sparql);
+            this.varOrders = ImmutableSet.copyOf( checkNotNull(varOrders) );
+
+            checkArgument(cardinality >= 0, "Cardinality of a PCJ must be >= 0. Was: " + cardinality);
+            this.cardinality = cardinality;
+        }
+
+        /**
+         * @return The SPARQL query this PCJ solves.
+         */
+        public String getSparql() {
+            return sparql;
+        }
+
+        /**
+         * @return The number of results the PCJ has.
+         */
+        public long getCardinality() {
+            return cardinality;
+        }
+
+        /**
+         * @return Strings that describe each of the variable orders the results are stored in.
+         */
+        public ImmutableSet<VariableOrder> getVarOrders() {
+            return varOrders;
+        }
+
+        /**
+         * Updates the cardinality of a {@link PcjMetadata} by a {@code delta}.
+         *
+         * @param metadata - The PCJ metadata to update. (not null)
+         * @param delta - How much the cardinality of the PCJ has changed.
+         * @return A new instance of {@link PcjMetadata} with the new cardinality.
+         */
+        public static PcjMetadata updateCardinality(final PcjMetadata metadata, final int delta) {
+            checkNotNull(metadata);
+            return new PcjMetadata(metadata.sparql, metadata.cardinality + delta, metadata.varOrders);
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hash(sparql, cardinality, varOrders);
+        }
+
+        @Override
+        public boolean equals(final Object o) {
+            if(this == o) {
+                return true;
+            } else if(o instanceof PcjMetadata) {
+                final PcjMetadata metadata = (PcjMetadata) o;
+                return new EqualsBuilder()
+                        .append(sparql, metadata.sparql)
+                        .append(cardinality, metadata.cardinality)
+                        .append(varOrders, metadata.varOrders)
+                        .build();
+            }
+            return false;
+        }
+    }
+
+    /**
+     * Creates Accumulo table names that may be recognized by Rya as a table that
+     * holds the results of a Precomputed Join.
+     */
+    public static class PcjTableNameFactory {
+
+        /**
+         * Creates an Accumulo table names that may be recognized by Rya as a table
+         * that holds the results of a Precomputed Join.
+         * </p>
+         * An Accumulo cluster may host more than one Rya instance. To ensure each
+         * Rya instance's RDF Triples are segregated from each other, they are stored
+         * within different Accumulo tables. This is accomplished by prepending a
+         * {@code tablePrefix} to every table that is owned by a Rya instance. Each
+         * PCJ table is owned by a specific Rya instance, so it too must be prepended
+         * with the instance's {@code tablePrefix}.
+         * </p>
+         * When Rya scans for PCJ tables that it may use when creating execution plans,
+         * it looks for any table in Accumulo that has a name starting with its
+         * {@code tablePrefix} immediately followed by "INDEX". Anything following
+         * that portion of the table name is just a unique identifier for the SPARQL
+         * query that is being precomputed. Here's an example of what a table name
+         * may look like:
+         * <pre>
+         *     demo_INDEX_QUERY:c8f5367c-1660-4210-a7cb-681ed004d2d9
+         * </pre>
+         * The "demo_INDEX" portion indicates this table is a PCJ table for the "demo_"
+         * instance of Rya. The "_QUERY:c8f5367c-1660-4210-a7cb-681ed004d2d9" portion
+         * could be anything at all that uniquely identifies the query that is being updated.
+         *
+         * @param tablePrefix - The Rya instance's table prefix. (not null)
+         * @param uniqueId - The unique portion of the Rya PCJ table name. (not null)
+         * @return A Rya PCJ table name build using the provided values.
+         */
+        public String makeTableName(final String tablePrefix, final String uniqueId) {
+            return tablePrefix + "INDEX_" + uniqueId;
+        }
+    }
+
+    /**
+     * Create alternative variable orders for a SPARQL query based on
+     * the original ordering of its results.
+     */
+    public static interface PcjVarOrderFactory {
+
+        /**
+         * Create alternative variable orders for a SPARQL query based on
+         * the original ordering of its results.
+         *
+         * @param varOrder - The initial variable order of a SPARQL query. (not null)
+         * @return A set of alternative variable orders for the original.
+         */
+        public Set<VariableOrder> makeVarOrders(VariableOrder varOrder);
+    }
+
+    /**
+     * Shifts the variables to the left so that each variable will appear at
+     * the head of the varOrder once.
+     */
+    @ParametersAreNonnullByDefault
+    public static class ShiftVarOrderFactory implements PcjVarOrderFactory {
+        @Override
+        public Set<VariableOrder> makeVarOrders(final VariableOrder varOrder) {
+            Set<VariableOrder> varOrders = new HashSet<>();
+
+            final List<String> cyclicBuff = Lists.newArrayList( varOrder.getVariableOrders() );
+            final String[] varOrderBuff = new String[ cyclicBuff.size() ];
+
+            for(int shift = 0; shift < cyclicBuff.size(); shift++) {
+                // Build a variable order.
+                for(int i = 0; i < cyclicBuff.size(); i++) {
+                    varOrderBuff[i] = cyclicBuff.get(i);
+                }
+                varOrders.add( new VariableOrder(varOrderBuff) );
+
+                // Shift the order the variables will appear in the cyclic buffer.
+                cyclicBuff.add( cyclicBuff.remove(0) );
+            }
+
+            return varOrders;
+        }
+    }
+
+    /**
+     * Indicates one of the {@link PcjTables} functions has failed to perform its task.
+     */
+    public static class PcjException extends Exception {
+        private static final long serialVersionUID = 1L;
+
+        /**
+         * Constructs an instance of {@link PcjException}.
+         *
+         * @param message - Describes why the exception is being thrown.
+         */
+        public PcjException(String message) {
+            super(message);
+        }
+
+        /**
+         * Constructs an instance of {@link PcjException}.
+         *
+         * @param message - Describes why the exception is being thrown.
+         * @param cause - The exception that caused this one to be thrown.
+         */
+        public PcjException(String message, Throwable cause) {
+            super(message, cause);
+        }
+    }
+
+    /**
+     * Create a new PCJ table within an Accumulo instance for a SPARQL query.
+     * For example, calling the function like this:
+     * <pre>
+     * PcjTables.createPcjTable(
+     *     accumuloConn,
+     *
+     *     "foo_INDEX_query1234",
+     *
+     *     Sets.newHashSet(
+     *         new VariableOrder("city;worker;customer"),
+     *         new VariableOrder("worker;customer;city") ,
+     *         new VariableOrder("customer;city;worker")),
+     *
+     *     "SELECT ?customer ?worker ?city { " +
+     *            "?customer &lt;http://talksTo> ?worker. " +
+     *            "?worker &lt;http://livesIn> ?city. " +
+     *            "?worker &lt;http://worksAt> &lt;http://Home>. " +
+     *     "}");
+     * </pre>
+     * </p>
+     * Will result in an Accumulo table named "foo_INDEX_query1234" with the following entries:
+     * <table border="1" style="width:100%">
+     *   <tr> <th>Row ID</td>  <th>Column</td>  <th>Value</td> </tr>
+     *   <tr> <td>pcjMetadata</td> <td>metadata:sparql</td> <td> ... UTF-8 bytes encoding the query string ... </td> </tr>
+     *   <tr> <td>pcjMetadata</td> <td>metadata:cardinality</td> <td> The query's cardinality </td> </tr>
+     *   <tr> <td>pcjMetadata</td> <td>metadata:variableOrders</td> <td> The variable orders the results are written to </td> </tr>
+     * </table>
+     *
+     * @param accumuloConn - A connection to the Accumulo that hosts the PCJ table. (not null)
+     * @param pcjTableName - The name of the table that will be created. (not null)
+     * @param varOrders - The variable orders the results within the table will be written to. (not null)
+     * @param sparql - The query this table's results solves. (not null)
+     * @throws PcjException Could not create a new PCJ table either because Accumulo
+     *   would not let us create it or the PCJ metadata was not able to be written to it.
+     */
+    public void createPcjTable(
+            final Connector accumuloConn,
+            final String pcjTableName,
+            final Set<VariableOrder> varOrders,
+            final String sparql) throws PcjException {
+        checkNotNull(accumuloConn);
+        checkNotNull(pcjTableName);
+        checkNotNull(varOrders);
+        checkNotNull(sparql);
+
+        final TableOperations tableOps = accumuloConn.tableOperations();
+        if(!tableOps.exists(pcjTableName)) {
+            try {
+                // Create the new table in Accumulo.
+                tableOps.create(pcjTableName);
+
+                // Write the PCJ Metadata to the newly created table.
+                final PcjMetadata pcjMetadata = new PcjMetadata(sparql, 0L, varOrders);
+                final List<Mutation> mutations = makeWriteMetadataMutations(pcjMetadata);
+
+                final BatchWriter writer = accumuloConn.createBatchWriter(pcjTableName, new BatchWriterConfig());
+                writer.addMutations(mutations);
+                writer.close();
+            } catch (final TableExistsException e) {
+                log.warn("Something else just created the Rya PCJ export table named '" + pcjTableName
+                        + "'. This is unexpected, but we will continue as normal.");
+            } catch (AccumuloException | AccumuloSecurityException | TableNotFoundException e) {
+                throw new PcjException("Could not create a new PCJ named: " + pcjTableName, e);
+            }
+        }
+    }
+
+    /**
+     * Create the {@link Mutation}s required to write a {@link PCJMetadata} object
+     * to an Accumulo table.
+     *
+     * @param metadata - The metadata to write. (not null)
+     * @return An ordered list of mutations that write the metadata to an Accumulo table.
+     */
+    private static List<Mutation> makeWriteMetadataMutations(final PcjMetadata metadata) {
+        checkNotNull(metadata);
+
+        final List<Mutation> mutations = new LinkedList<>();
+
+        // SPARQL Query
+        Mutation mutation = new Mutation(PCJ_METADATA_ROW_ID);
+        Value query = new Value( stringLexicoder.encode(metadata.getSparql()) );
+        mutation.put(PCJ_METADATA_FAMILY, PCJ_METADATA_SPARQL_QUERY, query);
+        mutations.add(mutation);
+
+        // Cardinality
+        mutation = new Mutation(PCJ_METADATA_ROW_ID);
+        Value cardinality = new Value( longLexicoder.encode(new Long(metadata.getCardinality())) );
+        mutation.put(PCJ_METADATA_FAMILY, PCJ_METADATA_CARDINALITY, cardinality);
+        mutations.add(mutation);
+
+        //  Variable Orders
+        List<String> varOrderStrings = new ArrayList<>();
+        for(VariableOrder varOrder : metadata.getVarOrders()) {
+            varOrderStrings.add( varOrder.toString() );
+        }
+
+        mutation = new Mutation(PCJ_METADATA_ROW_ID);
+        Value variableOrders = new Value( listLexicoder.encode(varOrderStrings) );
+        mutation.put(PCJ_METADATA_FAMILY, PCJ_METADATA_VARIABLE_ORDERS, variableOrders);
+        mutations.add(mutation);
+
+        return mutations;
+    }
+
+    /**
+     * Fetch the {@link PCJMetadata} from an Accumulo table.
+     * <p>
+     * This method assumes the PCJ table has already been created.
+     *
+     * @param accumuloConn - A connection to the Accumulo that hosts the PCJ table. (not null)
+     * @param pcjTableName - The name of the table that will be search. (not null)
+     * @return The PCJ Metadata that has been stolred in the in the PCJ Table.
+     * @throws PcjException The PCJ Table does not exist.
+     */
+    public PcjMetadata getPcjMetadata(
+            final Connector accumuloConn,
+            final String pcjTableName) throws PcjException {
+        checkNotNull(accumuloConn);
+        checkNotNull(pcjTableName);
+
+        try {
+            // Create an Accumulo scanner that iterates through the metadata entries.
+            Scanner scanner = accumuloConn.createScanner(pcjTableName, new Authorizations());
+            final Iterator<Entry<Key, Value>> entries = scanner.iterator();
+
+            // No metadata has been stored in the table yet.
+            if(!entries.hasNext()) {
+                throw new PcjException("Could not find any PCJ metadata in the table named: " + pcjTableName);
+            }
+
+            // Fetch the metadata from the entries. Assuming they all have the same cardinality and sparql query.
+            String sparql = null;
+            Long cardinality = null;
+            final Set<VariableOrder> varOrders = new HashSet<>();
+
+            while(entries.hasNext()) {
+                final Entry<Key, Value> entry = entries.next();
+                Text columnQualifier = entry.getKey().getColumnQualifier();
+                byte[] value = entry.getValue().get();
+
+                if(columnQualifier.equals(PCJ_METADATA_SPARQL_QUERY)) {
+                    sparql = stringLexicoder.decode(value);
+                } else if(columnQualifier.equals(PCJ_METADATA_CARDINALITY)) {
+                    cardinality = longLexicoder.decode(value);
+                } else if(columnQualifier.equals(PCJ_METADATA_VARIABLE_ORDERS)) {
+                    for(String varOrderStr : listLexicoder.decode(value)) {
+                        varOrders.add( new VariableOrder(varOrderStr) );
+                    }
+                }
+            }
+
+            return new PcjMetadata(sparql, cardinality, varOrders);
+
+        } catch (TableNotFoundException e) {
+            throw new PcjException("Could not add results to a PCJ because the PCJ table does not exist.", e);
+        }
+    }
+
+    /**
+     * Add a collection of results to a PCJ table. The table's cardinality will
+     * be updated to include the new results.
+     * <p>
+     * This method assumes the PCJ table has already been created.
+     *
+     * @param accumuloConn - A connection to the Accumulo that hosts the PCJ table. (not null)
+     * @param pcjTableName - The name of the PCJ table that will receive the results. (not null)
+     * @param results - Binding sets that will be written to the PCJ table. (not null)
+     * @throws PcjException The provided PCJ table doesn't exist, is missing the
+     *   PCJ metadata, or the result could not be written to it.
+     */
+    public void addResults(
+            final Connector accumuloConn,
+            final String pcjTableName,
+            final Collection<BindingSet> results) throws PcjException {
+        checkNotNull(accumuloConn);
+        checkNotNull(pcjTableName);
+        checkNotNull(results);
+
+        // Write a result to each of the variable orders that are in the table.
+        writeResults(accumuloConn, pcjTableName, results);
+
+        // Increment the cardinality of the query by the number of new results.
+        updateCardinality(accumuloConn, pcjTableName, results.size());
+    }
+
+    /**
+     * Add a collection of results to a specific PCJ table.
+     *
+     * @param accumuloConn - A connection to the Accumulo that hosts the PCJ table. (not null)
+     * @param pcjTableName - The name of the PCJ table that will receive the results. (not null)
+     * @param results - Binding sets that will be written to the PCJ table. (not null)
+     * @throws PcjException The provided PCJ table doesn't exist, is missing the
+     *   PCJ metadata, or the result could not be written to it.
+     */
+    private void writeResults(
+            final Connector accumuloConn,
+            final String pcjTableName,
+            final Collection<BindingSet> results) throws PcjException {
+        checkNotNull(accumuloConn);
+        checkNotNull(pcjTableName);
+        checkNotNull(results);
+
+        // Fetch the variable orders from the PCJ table.
+        PcjMetadata metadata = getPcjMetadata(accumuloConn, pcjTableName);
+
+        // Write each result formatted using each of the variable orders.
+        BatchWriter writer = null;
+        try {
+            writer = accumuloConn.createBatchWriter(pcjTableName, new BatchWriterConfig());
+            for(BindingSet result : results) {
+                Set<Mutation> addResultMutations = makeWriteResultMutations(metadata.getVarOrders(), result);
+                writer.addMutations( addResultMutations );
+            }
+        } catch (TableNotFoundException | MutationsRejectedException e) {
+            throw new PcjException("Could not add results to the PCJ table named: " + pcjTableName, e);
+        } finally {
+            if(writer != null) {
+                try {
+                    writer.close();
+                } catch (MutationsRejectedException e) {
+                    throw new PcjException("Could not add results to a PCJ table because some of the mutations were rejected.", e);
+                }
+            }
+        }
+    }
+
+    /**
+     * Create the {@link Mutations} required to write a new {@link BindingSet}
+     * to a PCJ table for each {@link VariableOrder} that is provided.
+     *
+     * @param varOrders - The variables orders the result will be written to. (not null)
+     * @param result - A new PCJ result. (not null)
+     * @return Mutation that will write the result to a PCJ table.
+     * @throws PcjException The binding set could not be encoded.
+     */
+    private static Set<Mutation> makeWriteResultMutations(
+            final Set<VariableOrder> varOrders,
+            final BindingSet result) throws PcjException {
+        checkNotNull(varOrders);
+        checkNotNull(result);
+
+        Set<Mutation> mutations = new HashSet<>();
+
+        for(final VariableOrder varOrder : varOrders) {
+            try {
+                // Serialize the result to the variable order.
+                byte[] serializedResult = AccumuloPcjSerializer.serialize(result, varOrder.toArray());
+
+                // Row ID = binding set values, Column Family = variable order of the binding set.
+                Mutation addResult = new Mutation(serializedResult);
+                addResult.put(varOrder.toString(), "", "");
+                mutations.add(addResult);
+            } catch(RyaTypeResolverException e) {
+                throw new PcjException("Could not serialize a result.", e);
+            }
+        }
+
+        return mutations;
+    }
+
+    /**
+     * Update the cardinality of a PCJ by a {@code delta}.
+     *
+     * @param accumuloConn - A connection to the Accumulo that hosts the PCJ table. (not null)
+     * @param pcjTableName - The name of the PCJ table that will have its cardinality updated. (not null)
+     * @param delta - How much the cardinality will change.
+     * @throws PcjException The cardinality could not be updated.
+     */
+    private void updateCardinality(
+            final Connector accumuloConn,
+            final String pcjTableName,
+            final long delta) throws PcjException {
+        checkNotNull(accumuloConn);
+        checkNotNull(pcjTableName);
+
+        ConditionalWriter conditionalWriter = null;
+        try {
+            conditionalWriter = accumuloConn.createConditionalWriter(pcjTableName, new ConditionalWriterConfig());
+
+            boolean updated = false;
+            while(!updated) {
+                // Write the conditional update request to Accumulo.
+                long cardinality = getPcjMetadata(accumuloConn, pcjTableName).getCardinality();
+                ConditionalMutation mutation = makeUpdateCardinalityMutation(cardinality, delta);
+                ConditionalWriter.Result result = conditionalWriter.write(mutation);
+
+                // Interpret the result.
+                switch(result.getStatus()) {
+                    case ACCEPTED:
+                        updated = true;
+                        break;
+                    case REJECTED:
+                        break;
+                    case UNKNOWN:
+                        // We do not know if the mutation succeeded. At best, we can hope the metadata hasn't been updated
+                        // since we originally fetched it and try again. Otherwise, continue forwards as if it worked. It's
+                        // okay if this number is slightly off.
+                        long newCardinality = getPcjMetadata(accumuloConn, pcjTableName).getCardinality();
+                        if(newCardinality != cardinality) {
+                            updated = true;
+                        }
+                        break;
+                    case VIOLATED:
+                        throw new PcjException("The cardinality could not be updated because the commit violated a table constraint.");
+                    case INVISIBLE_VISIBILITY:
+                        throw new PcjException("The condition contains a visibility the updater can not satisfy.");
+                }
+            }
+        } catch (AccumuloException | AccumuloSecurityException | TableNotFoundException e) {
+            throw new PcjException("Could not update the cardinality value of the PCJ Table named: " + pcjTableName, e);
+        } finally {
+            if(conditionalWriter != null) {
+                conditionalWriter.close();
+            }
+        }
+    }
+
+    /**
+     * Creates a {@link ConditionalMutation} that only updates the cardinality
+     * of the PCJ table if the old value has not changed by the time this mutation
+     * is committed to Accumulo.
+     *
+     * @param current - The current cardinality value.
+     * @param delta - How much the cardinality will change.
+     * @return The mutation that will perform the conditional update.
+     */
+    private static ConditionalMutation makeUpdateCardinalityMutation(long current, long delta) {
+        // Try to update the cardinality by the delta.
+        ConditionalMutation mutation = new ConditionalMutation(PCJ_METADATA_ROW_ID);
+        Condition lastCardinalityStillCurrent = new Condition(
+                PCJ_METADATA_FAMILY,
+                PCJ_METADATA_CARDINALITY);
+
+        // Require the old cardinality to be the value we just read.
+        byte[] currentCardinalityBytes = longLexicoder.encode( current );
+        lastCardinalityStillCurrent.setValue( currentCardinalityBytes );
+        mutation.addCondition(lastCardinalityStillCurrent);
+
+        // If that is the case, then update to the new value.
+        Value newCardinality = new Value( longLexicoder.encode(current + delta) );
+        mutation.put(PCJ_METADATA_FAMILY, PCJ_METADATA_CARDINALITY, newCardinality);
+        return mutation;
+    }
+
+    /**
+     * Scan Rya for results that solve the PCJ's query and store them in the PCJ table.
+     * <p>
+     * This method assumes the PCJ table has already been created.
+     *
+     * @param accumuloConn - A connection to the Accumulo that hosts the PCJ table. (not null)
+     * @param pcjTableName - The name of the PCJ table that will receive the results. (not null)
+     * @param ryaConn - A connection to the Rya store that will be queried to find results. (not null)
+     * @throws PcjException If results could not be written to the PCJ table,
+     *   the PCJ table does not exist, or the query that is being execute
+     *   was malformed.
+     */
+    public void populatePcj(
+            final Connector accumuloConn,
+            final String pcjTableName,
+            final RepositoryConnection ryaConn) throws PcjException {
+        checkNotNull(accumuloConn);
+        checkNotNull(pcjTableName);
+        checkNotNull(ryaConn);
+
+        try {
+            // Fetch the query that needs to be executed from the PCJ table.
+            PcjMetadata pcjMetadata = getPcjMetadata(accumuloConn, pcjTableName);
+            String sparql = pcjMetadata.getSparql();
+
+            // Query Rya for results to the SPARQL query.
+            TupleQuery query = ryaConn.prepareTupleQuery(QueryLanguage.SPARQL, sparql);
+            TupleQueryResult results = query.evaluate();
+
+            // Load batches of 1000 of them at a time into the PCJ table
+            Set<BindingSet> batch = new HashSet<>(1000);
+            while(results.hasNext()) {
+                batch.add( results.next() );
+
+                if(batch.size() == 1000) {
+                    addResults(accumuloConn, pcjTableName, batch);
+                    batch.clear();
+                }
+            }
+
+            if(!batch.isEmpty()) {
+                addResults(accumuloConn, pcjTableName, batch);
+            }
+
+        } catch (RepositoryException | MalformedQueryException | QueryEvaluationException e) {
+            throw new PcjException("Could not populate a PCJ table with Rya results for the table named: " + pcjTableName, e);
+        }
+    }
+
+    private static final PcjVarOrderFactory DEFAULT_VAR_ORDER_FACTORY = new ShiftVarOrderFactory();
+
+    /**
+     * Creates a new PCJ Table in Accumulo and populates it by scanning an
+     * instance of Rya for historic matches.
+     * <p>
+     * If any portion of this operation fails along the way, the partially
+     * create PCJ table will be left in Accumulo.
+     *
+     * @param ryaConn - Connects to the Rya that will be scanned. (not null)
+     * @param accumuloConn - Connects to the accumulo that hosts the PCJ results. (not null)
+     * @param pcjTableName - The name of the PCJ table that will be created. (not null)
+     * @param sparql - The SPARQL query whose results will be loaded into the table. (not null)
+     * @param resultVariables - The variables that are included in the query's resulting binding sets. (not null)
+     * @param pcjVarOrderFactory - An optional factory that indicates the various variable orders
+     *   the results will be stored in. If one is not provided, then {@link ShiftVarOrderFactory}
+     *   is used by default. (not null)
+     * @throws PcjException The PCJ table could not be create or the values from
+     *   Rya were not able to be loaded into it.
+     */
+    public void createAndPopulatePcj(
+            final RepositoryConnection ryaConn,
+            final Connector accumuloConn,
+            final String pcjTableName,
+            final String sparql,
+            final String[] resultVariables,
+            final Optional<PcjVarOrderFactory> pcjVarOrderFactory) throws PcjException {
+        checkNotNull(ryaConn);
+        checkNotNull(accumuloConn);
+        checkNotNull(pcjTableName);
+        checkNotNull(sparql);
+        checkNotNull(resultVariables);
+        checkNotNull(pcjVarOrderFactory);
+
+        // Create the PCJ's variable orders.
+        PcjVarOrderFactory varOrderFactory = pcjVarOrderFactory.or(DEFAULT_VAR_ORDER_FACTORY);
+        Set<VariableOrder> varOrders = varOrderFactory.makeVarOrders( new VariableOrder(resultVariables) );
+
+        // Create the PCJ table in Accumulo.
+        createPcjTable(accumuloConn, pcjTableName, varOrders, sparql);
+
+        // Load historic matches from Rya into the PCJ table.
+        populatePcj(accumuloConn, pcjTableName, ryaConn);
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/SimpleExternalTupleSet.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/SimpleExternalTupleSet.java b/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/SimpleExternalTupleSet.java
index 44925ca..ca97014 100644
--- a/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/SimpleExternalTupleSet.java
+++ b/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/SimpleExternalTupleSet.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.external.tupleSet;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -19,9 +19,14 @@ package mvm.rya.indexing.external.tupleSet;
  * under the License.
  */
 
+import info.aduna.iteration.CloseableIteration;
 
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
 
-import info.aduna.iteration.CloseableIteration;
+import mvm.rya.indexing.external.PrecompJoinOptimizer;
 
 import org.openrdf.query.BindingSet;
 import org.openrdf.query.QueryEvaluationException;
@@ -29,60 +34,79 @@ import org.openrdf.query.algebra.Projection;
 import org.openrdf.query.algebra.QueryModelVisitor;
 
 import com.google.common.base.Joiner;
+import com.google.common.collect.Sets;
 
+/**
+ *  This a testing class to create mock pre-computed join nodes in order to
+ *  test the {@link PrecompJoinOptimizer} for query planning.
+ *
+ */
 
+public class SimpleExternalTupleSet extends ExternalTupleSet {
 
+	public SimpleExternalTupleSet(Projection tuple) {
+		this.setProjectionExpr(tuple);
+		setSupportedVarOrders();
+	}
 
+	private void setSupportedVarOrders() {
 
+		final Set<String> varSet = Sets.newHashSet();
+		final Map<String, Set<String>> supportedVarOrders = new HashMap<>();
+		String t = "";
 
-public class SimpleExternalTupleSet extends ExternalTupleSet {
+		for (final String s : this.getTupleExpr().getAssuredBindingNames()) {
+			if (t.length() == 0) {
+				t = s;
+			} else {
+				t = t + VAR_ORDER_DELIM + s;
+			}
 
- 
-	
-    public SimpleExternalTupleSet(Projection tuple) {
-		super();
-		this.setProjectionExpr(tuple);
-		
+			varSet.add(s);
+			supportedVarOrders.put(t, new HashSet<String>(varSet));
+
+		}
+		this.setSupportedVariableOrderMap(supportedVarOrders);
 	}
-	
+
 	@Override
 	public <X extends Exception> void visit(QueryModelVisitor<X> visitor)
-			throws X
-		{
-			visitor.meetOther(this);
+			throws X {
+		visitor.meetOther(this);
+	}
+
+	@Override
+	public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(
+			BindingSet bindings) throws QueryEvaluationException {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public String getSignature() {
+		return "(SimpleExternalTupleSet) "
+				+ Joiner.on(", ")
+						.join(this.getTupleExpr().getProjectionElemList()
+								.getElements()).replaceAll("\\s+", " ");
+
+	}
+
+	@Override
+	public boolean equals(Object other) {
+
+		if (!(other instanceof SimpleExternalTupleSet)) {
+			return false;
+		} else {
+
+			final SimpleExternalTupleSet arg = (SimpleExternalTupleSet) other;
+			if (this.getTupleExpr().equals(arg.getTupleExpr())) {
+				return true;
+			} else {
+				return false;
+			}
+
 		}
-	
-    @Override
-    public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(BindingSet bindings)
-            throws QueryEvaluationException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public String getSignature() {
-        return "(SimpleExternalTupleSet) "
-                + Joiner.on(", ").join(this.getTupleExpr().getProjectionElemList().getElements()).replaceAll("\\s+", " ");
-
-    }
-    
-    @Override
-    public boolean equals(Object other) {
-
-        if (!(other instanceof SimpleExternalTupleSet)) {
-            return false;
-        } else {
-
-            SimpleExternalTupleSet arg = (SimpleExternalTupleSet) other;
-            if (this.getTupleExpr().equals(arg.getTupleExpr())) {
-                return true;
-            } else {
-                return false;
-            }
-
-        }
-
-    }
-    
+
+	}
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/GeneralizedExternalProcessorTest.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/GeneralizedExternalProcessorTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/GeneralizedExternalProcessorTest.java
index bfea0bd..deeaa63 100644
--- a/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/GeneralizedExternalProcessorTest.java
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/GeneralizedExternalProcessorTest.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.IndexPlanValidator;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -19,18 +19,14 @@ package mvm.rya.indexing.IndexPlanValidator;
  * under the License.
  */
 
-
-import static org.junit.Assert.*;
-
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
 
-import junit.framework.Assert;
-import mvm.rya.indexing.external.ExternalProcessor;
 import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
 import mvm.rya.indexing.external.tupleSet.SimpleExternalTupleSet;
 
+import org.junit.Assert;
 import org.junit.Test;
 import org.openrdf.query.algebra.Projection;
 import org.openrdf.query.algebra.TupleExpr;
@@ -42,284 +38,136 @@ import com.google.common.collect.Sets;
 
 public class GeneralizedExternalProcessorTest {
 
-    private String q7 = ""//
-            + "SELECT ?s ?t ?u " //
-            + "{" //
-            + "  ?s a ?t ."//
-            + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-            + "  ?u <uri:talksTo> ?s . "//
-            + "}";//
-    
-    
-    private String q8 = ""//
-            + "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r " //
-            + "{" //
-            + "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
-            + "  ?f a ?m ."//
-            + "  ?p <uri:talksTo> ?n . "//
-            + "  ?e a ?l ."//
-            + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
-            + "  ?d <uri:talksTo> ?f . "//
-            + "  ?c <uri:talksTo> ?e . "//
-            + "  ?n a ?o ."//
-            + "  ?a a ?h ."//
-            + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-            + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-            + "  ?r <uri:talksTo> ?a . "//
-            + "}";//
-    
-    
-    
-    
-    private String q11 = ""//
-            + "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r ?x ?y ?w ?t ?duck ?chicken ?pig ?rabbit " //
-            + "{" //
-            + "  ?w a ?t ."//
-            + "  ?x a ?y ."//
-            + "  ?duck a ?chicken ."//
-            + "  ?pig a ?rabbit ."//
-            + "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
-            + "  ?f a ?m ."//
-            + "  ?p <uri:talksTo> ?n . "//
-            + "  ?e a ?l ."//
-            + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
-            + "  ?d <uri:talksTo> ?f . "//
-            + "  ?c <uri:talksTo> ?e . "//
-            + "  ?n a ?o ."//
-            + "  ?a a ?h ."//
-            + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-            + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-            + "  ?r <uri:talksTo> ?a . "//
-            + "}";//
-    
-    
-    private String q12 = ""//
-            + "SELECT ?b ?p ?dog ?cat " //
-            + "{" //
-            + "  ?b a ?p ."//
-            + "  ?dog a ?cat. "//
-            + "}";//
-    
-    
-    
-    private String q13 = ""//
-            + "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r ?x ?y ?w ?t ?duck ?chicken ?pig ?rabbit ?dick ?jane ?betty " //
-            + "{" //
-            + "  ?w a ?t ."//
-            + "  ?x a ?y ."//
-            + "  ?duck a ?chicken ."//
-            + "  ?pig a ?rabbit ."//
-            + "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
-            + "  ?f a ?m ."//
-            + "  ?p <uri:talksTo> ?n . "//
-            + "  ?e a ?l ."//
-            + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
-            + "  ?d <uri:talksTo> ?f . "//
-            + "  ?c <uri:talksTo> ?e . "//
-            + "  ?n a ?o ."//
-            + "  ?a a ?h ."//
-            + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-            + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-            + "  ?r <uri:talksTo> ?a . "//
-            + "  ?dick <uri:talksTo> ?jane . "//
-            + "  ?jane <uri:talksTo> ?betty . "//
-            + "}";//
-    
-    private String q14 = ""//
-            + "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r ?x ?y ?w ?t ?duck ?chicken ?pig ?rabbit " //
-            + "{" //
-            + "  ?w a ?t ."//
-            + "  ?x a ?y ."//
-            + "  ?duck a ?chicken ."//
-            + "  ?pig a ?rabbit ."//
-            + "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
-            + "  ?f a ?m ."//
-            + "  ?p <uri:talksTo> ?n . "//
-            + "  ?e a ?l ."//
-            + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
-            + "  ?d <uri:talksTo> ?f . "//
-            + "  ?c <uri:talksTo> ?e . "//
-            + "  ?n a ?o ."//
-            + "  ?a a ?h ."//
-            + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-            + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-            + "  ?r <uri:talksTo> ?a . "//
-            + "  ?d <uri:talksTo> ?a . "//
-            + "}";//
-    
-    
-    private String q15 = ""//
-            + "SELECT ?f ?m ?d ?e ?l ?c " //
-            + "{" //
-            + "  ?f a ?m ."//
-            + "  ?e a ?l ."//
-            + "  ?d <uri:talksTo> ?f . "//
-            + "  ?c <uri:talksTo> ?e . "//
-            + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-            + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-            + "}";//
-    
-    private String q16 = ""//
-            + "SELECT ?f ?m ?d ?e ?l ?c " //
-            + "{" //
-            + "  ?d <uri:talksTo> ?f . "//
-            + "  ?c <uri:talksTo> ?e . "//
-            + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-            + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-            + "}";//
-    
-    private String q17 = ""//
-            + "SELECT ?dog ?cat ?chicken " //
-            + "{" //
-            + "  ?chicken <uri:talksTo> ?dog . "//
-            + "  ?cat <http://www.w3.org/2000/01/rdf-schema#label> ?chicken ."//
-            + "}";//
-    
-    private String q18 = ""//
-            + "SELECT ?dog ?chicken " //
-            + "{" //
-            + "  ?chicken <uri:talksTo> ?dog . "//
-            + "}";//
-    
-    private String q19 = ""//
-            + "SELECT ?cat ?chicken " //
-            + "{" //
-            + "  ?cat <http://www.w3.org/2000/01/rdf-schema#label> ?chicken ."//
-            + "}";//
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    //@Test
-    public void testTwoIndexLargeQuery() throws Exception {
-
-        SPARQLParser parser = new SPARQLParser();
-        
-
-        ParsedQuery pq1 = parser.parseQuery(q15, null);
-        ParsedQuery pq2 = parser.parseQuery(q7, null);
-        ParsedQuery pq3 = parser.parseQuery(q12, null);
-       
-        
-
-        System.out.println("Query is " + pq1.getTupleExpr());
-        
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(new Projection(pq3.getTupleExpr()));
-        //SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(new Projection(pq5.getTupleExpr()));
-       
-
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-       
-        list.add(extTup2);
-       //list.add(extTup3);
-       list.add(extTup1);
-        
-
-        IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(pq1.getTupleExpr(),list);
-        List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
-        Assert.assertEquals(4, indexSet.size());
-        
-//        System.out.println("Normalized indices are: ");
-//        for(ExternalTupleSet e: indexSet) {
-//            System.out.println(e.getTupleExpr());
-//        }
-        
-        Set<TupleExpr> processedTups = Sets.newHashSet(iep.getIndexedTuples());
-        
-        Assert.assertEquals(5, processedTups.size());
-        
-     //   System.out.println("Size is " + processedTups.size());
-        
-//        System.out.println("Indexed tuples are :" );
-//        for(TupleExpr te: processedTups) {
-//            System.out.println(te);
-//        }
-        
-        
-       
-        
-
-
-    }
-    
-    
-    
-    
-    
-    @Test
-    public void testThreeIndexQuery() throws Exception {
-
-        SPARQLParser parser = new SPARQLParser();
-        
-
-        ParsedQuery pq1 = parser.parseQuery(q16, null);
-        ParsedQuery pq2 = parser.parseQuery(q17, null);
-        ParsedQuery pq3 = parser.parseQuery(q18, null);
-        ParsedQuery pq4 = parser.parseQuery(q19, null);
-       
-        
-
-        System.out.println("Query is " + pq1.getTupleExpr());
-        
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(new Projection(pq3.getTupleExpr()));
-        SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(new Projection(pq4.getTupleExpr()));
-       
-
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-       
-        list.add(extTup2);
-        list.add(extTup3);
-        list.add(extTup1);
-        
-
-        IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(pq1.getTupleExpr(),list);
-        List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
-        Assert.assertEquals(6, indexSet.size());
-        
-//        System.out.println("Normalized indices are: ");
-//        for(ExternalTupleSet e: indexSet) {
-//            System.out.println(e.getTupleExpr());
-//        }
-        
-        Set<TupleExpr> processedTups = Sets.newHashSet(iep.getIndexedTuples());
-        
-        Assert.assertEquals(17, processedTups.size());
-        
-      //  System.out.println("Size is " + processedTups.size());
-        
-//        System.out.println("Indexed tuples are :" );
-//        for(TupleExpr te: processedTups) {
-//            System.out.println(te);
-//        }
-        
-        
-        TupleExecutionPlanGenerator tep = new TupleExecutionPlanGenerator();
-        List<TupleExpr> plans = Lists.newArrayList(tep.getPlans(processedTups.iterator()));
-        
-        
-      System.out.println("Size is " + plans.size());
-        
-      System.out.println("Possible indexed tuple plans are :" );
-      for(TupleExpr te: plans) {
-          System.out.println(te);
-      }
-       
-        
-
-
-    }
-    
-    
-    
-    
-    
+	private String q7 = ""//
+			+ "SELECT ?s ?t ?u " //
+			+ "{" //
+			+ "  ?s a ?t ."//
+			+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+			+ "  ?u <uri:talksTo> ?s . "//
+			+ "}";//
+
+	private String q12 = ""//
+			+ "SELECT ?b ?p ?dog ?cat " //
+			+ "{" //
+			+ "  ?b a ?p ."//
+			+ "  ?dog a ?cat. "//
+			+ "}";//
+
+	private String q15 = ""//
+			+ "SELECT ?f ?m ?d ?e ?l ?c " //
+			+ "{" //
+			+ "  ?f a ?m ."//
+			+ "  ?e a ?l ."//
+			+ "  ?d <uri:talksTo> ?f . "//
+			+ "  ?c <uri:talksTo> ?e . "//
+			+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+			+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+			+ "}";//
+
+	private String q16 = ""//
+			+ "SELECT ?f ?m ?d ?e ?l ?c " //
+			+ "{" //
+			+ "  ?d <uri:talksTo> ?f . "//
+			+ "  ?c <uri:talksTo> ?e . "//
+			+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+			+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+			+ "}";//
+
+	private String q17 = ""//
+			+ "SELECT ?dog ?cat ?chicken " //
+			+ "{" //
+			+ "  ?chicken <uri:talksTo> ?dog . "//
+			+ "  ?cat <http://www.w3.org/2000/01/rdf-schema#label> ?chicken ."//
+			+ "}";//
+
+	private String q18 = ""//
+			+ "SELECT ?dog ?chicken " //
+			+ "{" //
+			+ "  ?chicken <uri:talksTo> ?dog . "//
+			+ "}";//
+
+	private String q19 = ""//
+			+ "SELECT ?cat ?chicken " //
+			+ "{" //
+			+ "  ?cat <http://www.w3.org/2000/01/rdf-schema#label> ?chicken ."//
+			+ "}";//
+
+	@Test
+	public void testTwoIndexLargeQuery() throws Exception {
+
+		SPARQLParser parser = new SPARQLParser();
+
+		ParsedQuery pq1 = parser.parseQuery(q15, null);
+		ParsedQuery pq2 = parser.parseQuery(q7, null);
+		ParsedQuery pq3 = parser.parseQuery(q12, null);
+
+		System.out.println("Query is " + pq1.getTupleExpr());
+
+		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				new Projection(pq2.getTupleExpr()));
+		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				new Projection(pq3.getTupleExpr()));
+
+		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+
+		list.add(extTup2);
+		list.add(extTup1);
+
+		IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
+				pq1.getTupleExpr(), list);
+		List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
+		Assert.assertEquals(4, indexSet.size());
+
+		Set<TupleExpr> processedTups = Sets.newHashSet(iep.getIndexedTuples());
+		Assert.assertEquals(5, processedTups.size());
+
+	}
+
+	@Test
+	public void testThreeIndexQuery() throws Exception {
+
+		SPARQLParser parser = new SPARQLParser();
+
+		ParsedQuery pq1 = parser.parseQuery(q16, null);
+		ParsedQuery pq2 = parser.parseQuery(q17, null);
+		ParsedQuery pq3 = parser.parseQuery(q18, null);
+		ParsedQuery pq4 = parser.parseQuery(q19, null);
+
+		System.out.println("Query is " + pq1.getTupleExpr());
+
+		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				new Projection(pq2.getTupleExpr()));
+		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				new Projection(pq3.getTupleExpr()));
+		SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(
+				new Projection(pq4.getTupleExpr()));
+
+		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+
+		list.add(extTup2);
+		list.add(extTup3);
+		list.add(extTup1);
+
+		IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
+				pq1.getTupleExpr(), list);
+		List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
+		Assert.assertEquals(6, indexSet.size());
+
+		Set<TupleExpr> processedTups = Sets.newHashSet(iep.getIndexedTuples());
+
+		Assert.assertEquals(17, processedTups.size());
+
+		TupleExecutionPlanGenerator tep = new TupleExecutionPlanGenerator();
+		List<TupleExpr> plans = Lists.newArrayList(tep.getPlans(processedTups
+				.iterator()));
+
+		System.out.println("Size is " + plans.size());
+
+		System.out.println("Possible indexed tuple plans are :");
+		for (TupleExpr te : plans) {
+			System.out.println(te);
+		}
 
+	}
 
 }


[08/16] incubator-rya git commit: RYA-32 Improve how metadata and values are written to Accumulo PCJ tables

Posted by mi...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloConstantPcjIntegrationTest.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloConstantPcjIntegrationTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloConstantPcjIntegrationTest.java
new file mode 100644
index 0000000..bd84bf5
--- /dev/null
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloConstantPcjIntegrationTest.java
@@ -0,0 +1,410 @@
+package mvm.rya.indexing.external;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.List;
+
+import mvm.rya.api.persist.RyaDAOException;
+import mvm.rya.indexing.external.tupleSet.PcjTables.PcjException;
+import mvm.rya.indexing.external.tupleSet.PcjTables.PcjVarOrderFactory;
+
+import org.apache.accumulo.core.client.AccumuloException;
+import org.apache.accumulo.core.client.AccumuloSecurityException;
+import org.apache.accumulo.core.client.Connector;
+import org.apache.accumulo.core.client.TableExistsException;
+import org.apache.accumulo.core.client.TableNotFoundException;
+import org.apache.accumulo.core.client.mock.MockInstance;
+import org.apache.accumulo.core.client.security.tokens.PasswordToken;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.openrdf.model.URI;
+import org.openrdf.model.impl.LiteralImpl;
+import org.openrdf.model.impl.URIImpl;
+import org.openrdf.model.vocabulary.RDF;
+import org.openrdf.model.vocabulary.RDFS;
+import org.openrdf.query.BindingSet;
+import org.openrdf.query.MalformedQueryException;
+import org.openrdf.query.QueryEvaluationException;
+import org.openrdf.query.QueryLanguage;
+import org.openrdf.query.QueryResultHandlerException;
+import org.openrdf.query.TupleQueryResultHandler;
+import org.openrdf.query.TupleQueryResultHandlerException;
+import org.openrdf.repository.RepositoryException;
+import org.openrdf.repository.sail.SailRepository;
+import org.openrdf.repository.sail.SailRepositoryConnection;
+import org.openrdf.sail.SailException;
+
+import com.google.common.base.Optional;
+
+public class AccumuloConstantPcjIntegrationTest {
+
+	private SailRepositoryConnection conn, pcjConn;
+	private SailRepository repo, pcjRepo;
+	private Connector accCon;
+	String prefix = "table_";
+	String tablename = "table_INDEX_";
+	URI obj, obj2, subclass, subclass2, talksTo;
+
+	@Before
+	public void init() throws RepositoryException,
+			TupleQueryResultHandlerException, QueryEvaluationException,
+			MalformedQueryException, AccumuloException,
+			AccumuloSecurityException, TableExistsException,
+			TableNotFoundException, RyaDAOException {
+
+		repo = PcjIntegrationTestingUtil.getNonPcjRepo(prefix, "instance");
+		conn = repo.getConnection();
+
+		pcjRepo = PcjIntegrationTestingUtil.getPcjRepo(prefix, "instance");
+		pcjConn = pcjRepo.getConnection();
+
+		final URI sub = new URIImpl("uri:entity");
+		subclass = new URIImpl("uri:class");
+		obj = new URIImpl("uri:obj");
+		talksTo = new URIImpl("uri:talksTo");
+
+		conn.add(sub, RDF.TYPE, subclass);
+		conn.add(sub, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(sub, talksTo, obj);
+
+		final URI sub2 = new URIImpl("uri:entity2");
+		subclass2 = new URIImpl("uri:class2");
+		obj2 = new URIImpl("uri:obj2");
+
+		conn.add(sub2, RDF.TYPE, subclass2);
+		conn.add(sub2, RDFS.LABEL, new LiteralImpl("label2"));
+		conn.add(sub2, talksTo, obj2);
+
+		accCon = new MockInstance("instance").getConnector("root",new PasswordToken(""));
+
+	}
+
+	@After
+	public void close() throws RepositoryException, AccumuloException,
+			AccumuloSecurityException, TableNotFoundException {
+		PcjIntegrationTestingUtil.closeAndShutdown(conn, repo);
+		PcjIntegrationTestingUtil.closeAndShutdown(pcjConn, pcjRepo);
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, prefix);
+		PcjIntegrationTestingUtil.deleteIndexTables(accCon, 2, prefix);
+
+
+	}
+
+	@Test
+	public void testEvaluateTwoIndexVarInstantiate1() throws PcjException,
+			RepositoryException, AccumuloException, AccumuloSecurityException,
+			TableNotFoundException, TableExistsException,
+			MalformedQueryException, SailException, QueryEvaluationException,
+			TupleQueryResultHandlerException {
+
+		final URI superclass = new URIImpl("uri:superclass");
+		final URI superclass2 = new URIImpl("uri:superclass2");
+
+		conn.add(subclass, RDF.TYPE, superclass);
+		conn.add(subclass2, RDF.TYPE, superclass2);
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?dog ?pig ?duck  " //
+				+ "{" //
+				+ "  ?pig a ?dog . "//
+				+ "  ?pig <http://www.w3.org/2000/01/rdf-schema#label> ?duck "//
+				+ "}";//
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?o ?f ?e ?c ?l  " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final String queryString = ""//
+				+ "SELECT ?c ?l ?f ?o " //
+				+ "{" //
+				+ "  <uri:entity> a ?c . "//
+				+ "  <uri:entity> <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  <uri:entity> <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 1,
+				indexSparqlString, new String[] { "dog", "pig", "duck" },
+				Optional.<PcjVarOrderFactory> absent());
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 2,
+				indexSparqlString2, new String[] { "o", "f", "e", "c", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		final CountingResultHandler crh1 = new CountingResultHandler();
+		final CountingResultHandler crh2 = new CountingResultHandler();
+
+		conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString)
+				.evaluate(crh1);
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, prefix);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh2);
+
+		Assert.assertEquals(crh1.getCount(), crh2.getCount());
+
+	}
+
+	@Test
+	public void testEvaluateThreeIndexVarInstantiate() throws PcjException,
+			RepositoryException, AccumuloException, AccumuloSecurityException,
+			TableNotFoundException, TableExistsException,
+			MalformedQueryException, SailException, QueryEvaluationException,
+			TupleQueryResultHandlerException {
+
+		final URI superclass = new URIImpl("uri:superclass");
+		final URI superclass2 = new URIImpl("uri:superclass2");
+
+		final URI sub = new URIImpl("uri:entity");
+		subclass = new URIImpl("uri:class");
+		obj = new URIImpl("uri:obj");
+		talksTo = new URIImpl("uri:talksTo");
+
+		final URI howlsAt = new URIImpl("uri:howlsAt");
+		final URI subType = new URIImpl("uri:subType");
+
+		conn.add(subclass, RDF.TYPE, superclass);
+		conn.add(subclass2, RDF.TYPE, superclass2);
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+		conn.add(sub, howlsAt, superclass);
+		conn.add(superclass, subType, obj);
+
+		conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
+		conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?dog ?pig ?duck  " //
+				+ "{" //
+				+ "  ?pig a ?dog . "//
+				+ "  ?pig <http://www.w3.org/2000/01/rdf-schema#label> ?duck "//
+				+ "}";//
+
+		final String indexSparqlString2 = ""//
+				+ "SELECT ?o ?f ?e ?c ?l  " //
+				+ "{" //
+				+ "  ?e <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "}";//
+
+		final String indexSparqlString3 = ""//
+				+ "SELECT ?wolf ?sheep ?chicken  " //
+				+ "{" //
+				+ "  ?wolf <uri:howlsAt> ?sheep . "//
+				+ "  ?sheep <uri:subType> ?chicken. "//
+				+ "}";//
+
+		final String queryString = ""//
+				+ "SELECT ?c ?l ?f ?o " //
+				+ "{" //
+				+ "  <uri:entity> a ?c . "//
+				+ "  <uri:entity> <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  <uri:entity> <uri:talksTo> ?o . "//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+				+ "  ?c a ?f . " //
+				+ "  <uri:entity> <uri:howlsAt> ?f. "//
+				+ "  ?f <uri:subType> <uri:obj>. "//
+				+ "}";//
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 1,
+				indexSparqlString, new String[] { "dog", "pig", "duck" },
+				Optional.<PcjVarOrderFactory> absent());
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 2,
+				indexSparqlString2, new String[] { "o", "f", "e", "c", "l" },
+				Optional.<PcjVarOrderFactory> absent());
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 3,
+				indexSparqlString3,
+				new String[] { "wolf", "sheep", "chicken" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		final CountingResultHandler crh1 = new CountingResultHandler();
+		final CountingResultHandler crh2 = new CountingResultHandler();
+
+		conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString)
+				.evaluate(crh1);
+
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, prefix);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(
+				crh2);
+
+		Assert.assertEquals(crh1.getCount(), crh2.getCount());
+
+	}
+
+	@Test
+	public void testEvaluateFilterInstantiate() throws RepositoryException,
+			PcjException, MalformedQueryException, SailException,
+			QueryEvaluationException, TableNotFoundException,
+			TupleQueryResultHandlerException, AccumuloException,
+			AccumuloSecurityException {
+
+		final URI e1 = new URIImpl("uri:e1");
+		final URI e2 = new URIImpl("uri:e2");
+		final URI e3 = new URIImpl("uri:e3");
+		final URI f1 = new URIImpl("uri:f1");
+		final URI f2 = new URIImpl("uri:f2");
+		final URI f3 = new URIImpl("uri:f3");
+		final URI g1 = new URIImpl("uri:g1");
+		final URI g2 = new URIImpl("uri:g2");
+		final URI g3 = new URIImpl("uri:g3");
+
+		conn.add(e1, talksTo, f1);
+		conn.add(f1, talksTo, g1);
+		conn.add(g1, talksTo, e1);
+		conn.add(e2, talksTo, f2);
+		conn.add(f2, talksTo, g2);
+		conn.add(g2, talksTo, e2);
+		conn.add(e3, talksTo, f3);
+		conn.add(f3, talksTo, g3);
+		conn.add(g3, talksTo, e3);
+
+		final String queryString = ""//
+				+ "SELECT ?x ?y ?z " //
+				+ "{" //
+				+ "Filter(?x = <uri:e1>) . " //
+				+ " ?x <uri:talksTo> ?y. " //
+				+ " ?y <uri:talksTo> ?z. " //
+				+ " ?z <uri:talksTo> <uri:e1>. " //
+				+ "}";//
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?a ?b ?c ?d " //
+				+ "{" //
+				+ "Filter(?a = ?d) . " //
+				+ " ?a <uri:talksTo> ?b. " //
+				+ " ?b <uri:talksTo> ?c. " //
+				+ " ?c <uri:talksTo> ?d. " //
+				+ "}";//
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 1,
+				indexSparqlString, new String[] { "a", "b", "c", "d" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		final CountingResultHandler crh1 = new CountingResultHandler();
+		final CountingResultHandler crh2 = new CountingResultHandler();
+
+		conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString)
+				.evaluate(crh1);
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, prefix);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh2);
+
+		Assert.assertEquals(crh1.getCount(), crh2.getCount());
+
+	}
+
+	@Test
+	public void testEvaluateCompoundFilterInstantiate()
+			throws RepositoryException, PcjException, MalformedQueryException,
+			SailException, QueryEvaluationException,
+			TableNotFoundException,
+			TupleQueryResultHandlerException, AccumuloException, AccumuloSecurityException {
+
+		final URI e1 = new URIImpl("uri:e1");
+		final URI f1 = new URIImpl("uri:f1");
+
+		conn.add(e1, talksTo, e1);
+		conn.add(e1, talksTo, f1);
+		conn.add(f1, talksTo, e1);
+
+		final String queryString = ""//
+				+ "SELECT ?x ?y ?z " //
+				+ "{" //
+				+ "Filter(?x = <uri:e1> && ?y = <uri:e1>) . " //
+				+ " ?x <uri:talksTo> ?y. " //
+				+ " ?y <uri:talksTo> ?z. " //
+				+ " ?z <uri:talksTo> <uri:e1>. " //
+				+ "}";//
+
+		final String indexSparqlString = ""//
+				+ "SELECT ?a ?b ?c ?d " //
+				+ "{" //
+				+ "Filter(?a = ?d && ?b = ?d) . " //
+				+ " ?a <uri:talksTo> ?b. " //
+				+ " ?b <uri:talksTo> ?c. " //
+				+ " ?c <uri:talksTo> ?d. " //
+				+ "}";//
+
+		PcjIntegrationTestingUtil.createAndPopulatePcj(conn, accCon, tablename + 1,
+				indexSparqlString, new String[] { "a", "b", "c", "d" },
+				Optional.<PcjVarOrderFactory> absent());
+
+		final CountingResultHandler crh1 = new CountingResultHandler();
+		final CountingResultHandler crh2 = new CountingResultHandler();
+
+		conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString)
+				.evaluate(crh1);
+		PcjIntegrationTestingUtil.deleteCoreRyaTables(accCon, prefix);
+		pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(
+				crh2);
+
+		Assert.assertEquals(2, crh1.getCount());
+		Assert.assertEquals(crh1.getCount(), crh2.getCount());
+
+	}
+
+	public static class CountingResultHandler implements
+			TupleQueryResultHandler {
+		private int count = 0;
+
+		public int getCount() {
+			return count;
+		}
+
+		public void resetCount() {
+			this.count = 0;
+		}
+
+		@Override
+		public void startQueryResult(List<String> arg0)
+				throws TupleQueryResultHandlerException {
+		}
+
+		@Override
+		public void handleSolution(BindingSet arg0)
+				throws TupleQueryResultHandlerException {
+			count++;
+		}
+
+		@Override
+		public void endQueryResult() throws TupleQueryResultHandlerException {
+		}
+
+		@Override
+		public void handleBoolean(boolean arg0)
+				throws QueryResultHandlerException {
+		}
+
+		@Override
+		public void handleLinks(List<String> arg0)
+				throws QueryResultHandlerException {
+		}
+	}
+
+}


[07/16] incubator-rya git commit: RYA-32 Improve how metadata and values are written to Accumulo PCJ tables

Posted by mi...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloIndexSetTest.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloIndexSetTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloIndexSetTest.java
deleted file mode 100644
index c8ea57d..0000000
--- a/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloIndexSetTest.java
+++ /dev/null
@@ -1,4330 +0,0 @@
-package mvm.rya.indexing.external;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-
-import java.util.List;
-import java.util.Set;
-
-import junit.framework.Assert;
-import mvm.rya.indexing.IndexPlanValidator.IndexPlanValidator;
-import mvm.rya.indexing.external.tupleSet.AccumuloIndexSet;
-import mvm.rya.indexing.external.tupleSet.ExternalProcessorTest.ExternalTupleVstor;
-import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
-
-import org.apache.accumulo.core.client.AccumuloException;
-import org.apache.accumulo.core.client.AccumuloSecurityException;
-import org.apache.accumulo.core.client.Connector;
-import org.apache.accumulo.core.client.MutationsRejectedException;
-import org.apache.accumulo.core.client.TableExistsException;
-import org.apache.accumulo.core.client.TableNotFoundException;
-import org.apache.accumulo.core.client.mock.MockInstance;
-import org.junit.Before;
-import org.junit.Test;
-import org.openrdf.model.URI;
-import org.openrdf.model.impl.LiteralImpl;
-import org.openrdf.model.impl.URIImpl;
-import org.openrdf.model.vocabulary.RDF;
-import org.openrdf.model.vocabulary.RDFS;
-import org.openrdf.query.BindingSet;
-import org.openrdf.query.MalformedQueryException;
-import org.openrdf.query.QueryEvaluationException;
-import org.openrdf.query.QueryLanguage;
-import org.openrdf.query.QueryResultHandlerException;
-import org.openrdf.query.TupleQueryResultHandler;
-import org.openrdf.query.TupleQueryResultHandlerException;
-import org.openrdf.query.algebra.TupleExpr;
-import org.openrdf.query.parser.ParsedQuery;
-import org.openrdf.query.parser.sparql.SPARQLParser;
-import org.openrdf.repository.RepositoryException;
-import org.openrdf.repository.sail.SailRepository;
-import org.openrdf.repository.sail.SailRepositoryConnection;
-import org.openrdf.sail.Sail;
-import org.openrdf.sail.SailException;
-import org.openrdf.sail.memory.MemoryStore;
-
-import com.beust.jcommander.internal.Sets;
-import com.google.common.collect.Lists;
-
-public class AccumuloIndexSetTest {
-
-    private SailRepositoryConnection conn;
-    private Connector accCon;
-    String tablename = "table";
-    Sail s;
-    URI obj,obj2,subclass, subclass2, talksTo;
-   
-    
-    
-    
-    @Before
-    public void init() throws RepositoryException, TupleQueryResultHandlerException, QueryEvaluationException, MalformedQueryException, AccumuloException, AccumuloSecurityException, TableExistsException {
-
-        s = new MemoryStore();
-        SailRepository repo = new SailRepository(s);
-        repo.initialize();
-        conn = repo.getConnection();
-
-        URI sub = new URIImpl("uri:entity");
-        subclass = new URIImpl("uri:class");
-        obj = new URIImpl("uri:obj");
-        talksTo = new URIImpl("uri:talksTo");
-
-        conn.add(sub, RDF.TYPE, subclass);
-        conn.add(sub, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(sub, talksTo, obj);
-
-        URI sub2 = new URIImpl("uri:entity2");
-        subclass2 = new URIImpl("uri:class2");
-        obj2 = new URIImpl("uri:obj2");
-
-        conn.add(sub2, RDF.TYPE, subclass2);
-        conn.add(sub2, RDFS.LABEL, new LiteralImpl("label2"));
-        conn.add(sub2, talksTo, obj2);
-
-        accCon = new MockInstance().getConnector("root", "".getBytes());
-        accCon.tableOperations().create(tablename);
-        
-
-      
-
-    }
-    
-    
-    
-    
-    
-    @Test
-    public void testEvaluateSingeIndex() {
-        
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?e ?l ?c " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        
-        CountingResultHandler crh = new CountingResultHandler();
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString).evaluate(crh);
-        } catch (TupleQueryResultHandlerException e2) {
-            
-            e2.printStackTrace();
-        } catch (QueryEvaluationException e2) {
-            
-            e2.printStackTrace();
-        } catch (MalformedQueryException e2) {
-            
-            e2.printStackTrace();
-        } catch (RepositoryException e2) {
-            
-            e2.printStackTrace();
-        }
-
-       
-
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        AccumuloIndexSet ais = null; 
-        
-        try {
-            ais = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablename);
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (SailException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MutationsRejectedException e) {
-            
-            e.printStackTrace();
-        } catch (TableNotFoundException e) {
-            
-            e.printStackTrace();
-        }
-        
-//        Scanner scan = null;
-//        try {
-//            scan = accCon.createScanner(tablename, new Authorizations("auths"));
-//        } catch (TableNotFoundException e) {
-//            
-//            e.printStackTrace();
-//        }
-        
-//        scan.setRange(new Range());
-//        
-//        for (Map.Entry<Key, Value> entry : scan) {
-//            System.out.println("Key row string is " + entry.getKey().getRow().toString());
-//            System.out.println("Key is " + entry.getKey());
-//            System.out.println("Value is " + (new String(entry.getKey().getColumnQualifier().toString())));
-//
-//        }
-        
-        
-        
-        
-        index.add(ais);
-        
-        Assert.assertEquals((double)crh.getCount(), ais.cardinality());
-        
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "}";//
-
-        
-             
-        
-        CountingResultHandler crh1 = new CountingResultHandler();
-        
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh1);
-        } catch (TupleQueryResultHandlerException e1) {
-            
-            e1.printStackTrace();
-        } catch (QueryEvaluationException e1) {
-            
-            e1.printStackTrace();
-        } catch (MalformedQueryException e1) {
-            
-            e1.printStackTrace();
-        } catch (RepositoryException e1) {
-            
-            e1.printStackTrace();
-        }
-
-        
-      
-        
-        ParsedQuery pq = null;
-        SPARQLParser sp = new SPARQLParser();
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-       
-
-        
-        Sail processingSail = new ExternalSail(s, processor);
-        SailRepository smartSailRepo = new SailRepository(processingSail);
-        try {
-            smartSailRepo.initialize();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
-        CountingResultHandler crh2 = new CountingResultHandler();
-
-        try {
-            smartSailRepo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
-        Assert.assertEquals(crh1.getCount(), crh2.getCount());
-        
-        
-    }
-    
-    
-    
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexTwoVarOrder1() {
-        
-        
-        try {
-            accCon.tableOperations().create("table2");
-        } catch (AccumuloException e4) {
-            
-            e4.printStackTrace();
-        } catch (AccumuloSecurityException e4) {
-            
-            e4.printStackTrace();
-        } catch (TableExistsException e4) {
-            
-            e4.printStackTrace();
-        }
-        
-        
-        try {
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        } catch (RepositoryException e3) {
-            
-            e3.printStackTrace();
-        }
-       
-        
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?e ?l ?c " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?e ?o ?l " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        
-        
-        
-        
-
-        
-        CountingResultHandler crh1 = new CountingResultHandler();
-        CountingResultHandler crh2 = new CountingResultHandler();
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString).evaluate(crh1);
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e2) {
-            
-            e2.printStackTrace();
-        } catch (QueryEvaluationException e2) {
-            
-            e2.printStackTrace();
-        } catch (MalformedQueryException e2) {
-            
-            e2.printStackTrace();
-        } catch (RepositoryException e2) {
-            
-            e2.printStackTrace();
-        }
-
-       
-
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        AccumuloIndexSet ais1 = null;
-        AccumuloIndexSet ais2 = null;
-                
-        try {
-            ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablename);
-            ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (SailException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MutationsRejectedException e) {
-            
-            e.printStackTrace();
-        } catch (TableNotFoundException e) {
-            
-            e.printStackTrace();
-        }
-        
-//        Scanner scan = null;
-//        try {
-//            scan = accCon.createScanner(tablename, new Authorizations("auths"));
-//        } catch (TableNotFoundException e) {
-//            
-//            e.printStackTrace();
-//        }
-        
-//        scan.setRange(new Range());
-//        
-//        for (Map.Entry<Key, Value> entry : scan) {
-//            System.out.println("Key row string is " + entry.getKey().getRow().toString());
-//            System.out.println("Key is " + entry.getKey());
-//            System.out.println("Value is " + (new String(entry.getKey().getColumnQualifier().toString())));
-//
-//        }
-        
-        
-        
-        
-        index.add(ais1);
-        index.add(ais2);
-        
-        Assert.assertEquals((double)crh1.getCount(), ais1.cardinality());
-        Assert.assertEquals((double)crh2.getCount(), ais2.cardinality());
-          
-  
-        crh1 = new CountingResultHandler();
-        crh2 = new CountingResultHandler();
-        
-        
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh1);
-        } catch (TupleQueryResultHandlerException e1) {
-            
-            e1.printStackTrace();
-        } catch (QueryEvaluationException e1) {
-            
-            e1.printStackTrace();
-        } catch (MalformedQueryException e1) {
-            
-            e1.printStackTrace();
-        } catch (RepositoryException e1) {
-            
-            e1.printStackTrace();
-        }
-
-        
-  
-        
-        ParsedQuery pq = null;
-        SPARQLParser sp = new SPARQLParser();
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-      
-
-        
-        Sail processingSail = new ExternalSail(s, processor);
-        SailRepository smartSailRepo = new SailRepository(processingSail);
-        try {
-            smartSailRepo.initialize();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
-     
-
-        try {
-            smartSailRepo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
-        Assert.assertEquals(2, crh1.getCount());
-        Assert.assertEquals(2, crh2.getCount());
-        
-        
-        
-        
-        
-    }
-    
-    
-    
-    
-    
-    
-    
-    
-   @Test
-    public void testEvaluateTwoIndexTwoVarOrder2() {
-        
-        
-        try {
-            accCon.tableOperations().create("table2");
-        } catch (AccumuloException e4) {
-            
-            e4.printStackTrace();
-        } catch (AccumuloSecurityException e4) {
-            
-            e4.printStackTrace();
-        } catch (TableExistsException e4) {
-            
-            e4.printStackTrace();
-        }
-        
-        
-        try {
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        } catch (RepositoryException e3) {
-            
-            e3.printStackTrace();
-        }
-       
-        
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?c ?e ?l  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?e ?o ?l " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        
-        
-        
-        
-
-        
-        CountingResultHandler crh1 = new CountingResultHandler();
-        CountingResultHandler crh2 = new CountingResultHandler();
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString).evaluate(crh1);
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e2) {
-            
-            e2.printStackTrace();
-        } catch (QueryEvaluationException e2) {
-            
-            e2.printStackTrace();
-        } catch (MalformedQueryException e2) {
-            
-            e2.printStackTrace();
-        } catch (RepositoryException e2) {
-            
-            e2.printStackTrace();
-        }
-
-       
-
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        AccumuloIndexSet ais1 = null;
-        AccumuloIndexSet ais2 = null;
-                
-        try {
-            ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablename);
-            ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (SailException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MutationsRejectedException e) {
-            
-            e.printStackTrace();
-        } catch (TableNotFoundException e) {
-            
-            e.printStackTrace();
-        }
-      
-        
-        
-        index.add(ais1);
-        index.add(ais2);
-        
-        Assert.assertEquals((double)crh1.getCount(), ais1.cardinality());
-        Assert.assertEquals((double)crh2.getCount(), ais2.cardinality());
-          
-  
-        crh1 = new CountingResultHandler();
-        crh2 = new CountingResultHandler();
-        
-        
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh1);
-        } catch (TupleQueryResultHandlerException e1) {
-            
-            e1.printStackTrace();
-        } catch (QueryEvaluationException e1) {
-            
-            e1.printStackTrace();
-        } catch (MalformedQueryException e1) {
-            
-            e1.printStackTrace();
-        } catch (RepositoryException e1) {
-            
-            e1.printStackTrace();
-        }
-
-        
-     
-        
-        ParsedQuery pq = null;
-        SPARQLParser sp = new SPARQLParser();
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-        
-
-        
-        Sail processingSail = new ExternalSail(s, processor);
-        SailRepository smartSailRepo = new SailRepository(processingSail);
-        try {
-            smartSailRepo.initialize();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
-     
-
-        try {
-            smartSailRepo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
-        Assert.assertEquals(2, crh1.getCount());
-        Assert.assertEquals(2, crh2.getCount());
-         
-        
-        
-    }
-    
-    
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexTwoVarInvalidOrder() {
-        
-        
-        try {
-            accCon.tableOperations().create("table2");
-        } catch (AccumuloException e4) {
-            
-            e4.printStackTrace();
-        } catch (AccumuloSecurityException e4) {
-            
-            e4.printStackTrace();
-        } catch (TableExistsException e4) {
-            
-            e4.printStackTrace();
-        }
-        
-        
-        try {
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        } catch (RepositoryException e3) {
-            
-            e3.printStackTrace();
-        }
-       
-        
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?e ?c ?l  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?e ?o ?l " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        
-        
-        
-        
-
-        
-        CountingResultHandler crh1 = new CountingResultHandler();
-        CountingResultHandler crh2 = new CountingResultHandler();
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString).evaluate(crh1);
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString2).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e2) {
-            
-            e2.printStackTrace();
-        } catch (QueryEvaluationException e2) {
-            
-            e2.printStackTrace();
-        } catch (MalformedQueryException e2) {
-            
-            e2.printStackTrace();
-        } catch (RepositoryException e2) {
-            
-            e2.printStackTrace();
-        }
-
-       
-
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        AccumuloIndexSet ais1 = null;
-        AccumuloIndexSet ais2 = null;
-                
-        try {
-            ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablename);
-            ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (SailException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MutationsRejectedException e) {
-            
-            e.printStackTrace();
-        } catch (TableNotFoundException e) {
-            
-            e.printStackTrace();
-        }
-        
-
-        
-        
-        index.add(ais1);
-        index.add(ais2);
-        
-        Assert.assertEquals((double)crh1.getCount(), ais1.cardinality());
-        Assert.assertEquals((double)crh2.getCount(), ais2.cardinality());
-          
-  
-        crh1 = new CountingResultHandler();
-        crh2 = new CountingResultHandler();
-        
-        
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh1);
-        } catch (TupleQueryResultHandlerException e1) {
-            
-            e1.printStackTrace();
-        } catch (QueryEvaluationException e1) {
-            
-            e1.printStackTrace();
-        } catch (MalformedQueryException e1) {
-            
-            e1.printStackTrace();
-        } catch (RepositoryException e1) {
-            
-            e1.printStackTrace();
-        }
-
-        
-       
-        
-        ParsedQuery pq = null;
-        SPARQLParser sp = new SPARQLParser();
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-       
-
-        
-        Sail processingSail = new ExternalSail(s, processor);
-        SailRepository smartSailRepo = new SailRepository(processingSail);
-        try {
-            smartSailRepo.initialize();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
-
-        try {
-            smartSailRepo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        } 
-        
-        Assert.assertEquals(crh1.getCount(), crh2.getCount());
-         
-        
-        
-    }
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexThreeVarOrder1() {
-        
-        
-        
-        URI superclass = new URIImpl("uri:superclass");
-        URI superclass2 = new URIImpl("uri:superclass2");
-       
-
-        try {
-            conn.add(subclass, RDF.TYPE, superclass);
-            conn.add(subclass2, RDF.TYPE, superclass2);
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        } catch (RepositoryException e5) {
-            
-            e5.printStackTrace();
-        }
-        
-        
-        try {
-            if(accCon.tableOperations().exists("table2")){
-                accCon.tableOperations().delete("table2");
-            }
-            accCon.tableOperations().create("table2");
-        } catch (AccumuloException e4) {
-            
-            e4.printStackTrace();
-        } catch (AccumuloSecurityException e4) {
-            
-            e4.printStackTrace();
-        } catch (TableExistsException e4) {
-            
-            e4.printStackTrace();
-        } catch (TableNotFoundException e) {
-            
-            e.printStackTrace();
-        }
-        
-        
-        try {
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        } catch (RepositoryException e3) {
-            
-            e3.printStackTrace();
-        }
-       
-        
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?c ?e ?l  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?e ?c ?l ?f ?o" //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?f ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        
-        
-        
-        
-
-        
-        CountingResultHandler crh1 = new CountingResultHandler();
-        CountingResultHandler crh2 = new CountingResultHandler();
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString).evaluate(crh1);
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString2).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e2) {
-            
-            e2.printStackTrace();
-        } catch (QueryEvaluationException e2) {
-            
-            e2.printStackTrace();
-        } catch (MalformedQueryException e2) {
-            
-            e2.printStackTrace();
-        } catch (RepositoryException e2) {
-            
-            e2.printStackTrace();
-        }
-
-       
-
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        AccumuloIndexSet ais1 = null;
-        AccumuloIndexSet ais2 = null;
-                
-        try {
-            ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablename);
-            ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (SailException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MutationsRejectedException e) {
-            
-            e.printStackTrace();
-        } catch (TableNotFoundException e) {
-            
-            e.printStackTrace();
-        }
-      
-        
-        
-        index.add(ais1);
-        index.add(ais2);
-        
-        Assert.assertEquals((double)crh1.getCount(), ais1.cardinality());
-        Assert.assertEquals((double)crh2.getCount(), ais2.cardinality());
-          
-  
-        crh1 = new CountingResultHandler();
-        crh2 = new CountingResultHandler();
-        
-        
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh1);
-        } catch (TupleQueryResultHandlerException e1) {
-            
-            e1.printStackTrace();
-        } catch (QueryEvaluationException e1) {
-            
-            e1.printStackTrace();
-        } catch (MalformedQueryException e1) {
-            
-            e1.printStackTrace();
-        } catch (RepositoryException e1) {
-            
-            e1.printStackTrace();
-        }
-
-        
-    
-        
-        ParsedQuery pq = null;
-        SPARQLParser sp = new SPARQLParser();
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-  
-
-        
-        Sail processingSail = new ExternalSail(s, processor);
-        SailRepository smartSailRepo = new SailRepository(processingSail);
-        try {
-            smartSailRepo.initialize();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
-     
-
-        try {
-            smartSailRepo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
-        Assert.assertEquals(2, crh1.getCount());
-        Assert.assertEquals(2, crh2.getCount());
-         
-        
-        
-    }
-    
-    
-    
-    
-    
-    
-    
-    //@Test
-    public void testEvaluateTwoIndexThreeVarsDiffLabel() {
-        
-        
-        URI superclass = new URIImpl("uri:superclass");
-        URI superclass2 = new URIImpl("uri:superclass2");
-       
-
-        try {
-            conn.add(subclass, RDF.TYPE, superclass);
-            conn.add(subclass2, RDF.TYPE, superclass2);
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        } catch (RepositoryException e5) {
-            
-            e5.printStackTrace();
-        }
-        
-        
-        try {
-            if(accCon.tableOperations().exists("table2")){
-                accCon.tableOperations().delete("table2");
-            }
-            accCon.tableOperations().create("table2");
-        } catch (AccumuloException e4) {
-            
-            e4.printStackTrace();
-        } catch (AccumuloSecurityException e4) {
-            
-            e4.printStackTrace();
-        } catch (TableExistsException e4) {
-            
-            e4.printStackTrace();
-        } catch (TableNotFoundException e) {
-            
-            e.printStackTrace();
-        }
-        
-        
-        try {
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        } catch (RepositoryException e3) {
-            
-            e3.printStackTrace();
-        }
-       
-        
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?dog ?pig ?owl  " //
-                + "{" //
-                + "  ?pig a ?dog . "//
-                + "  ?pig <http://www.w3.org/2000/01/rdf-schema#label> ?owl "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?e ?c ?l ?f ?o" //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?f ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        
-        
-        
-        
-
-        
-        CountingResultHandler crh1 = new CountingResultHandler();
-        CountingResultHandler crh2 = new CountingResultHandler();
-//        try {
-//            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString).evaluate(crh1);
-//            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString2).evaluate(crh2);
-//        } catch (TupleQueryResultHandlerException e2) {
-//            
-//            e2.printStackTrace();
-//        } catch (QueryEvaluationException e2) {
-//            
-//            e2.printStackTrace();
-//        } catch (MalformedQueryException e2) {
-//            
-//            e2.printStackTrace();
-//        } catch (RepositoryException e2) {
-//            
-//            e2.printStackTrace();
-//        }
-
-       
-
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        AccumuloIndexSet ais1 = null;
-        AccumuloIndexSet ais2 = null;
-                
-        try {
-            ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablename);
-            ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (SailException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MutationsRejectedException e) {
-            
-            e.printStackTrace();
-        } catch (TableNotFoundException e) {
-            
-            e.printStackTrace();
-        }
-        
-//        
-//      Scanner scan = null;
-//      try {
-//          scan = accCon.createScanner(tablename, new Authorizations("auths"));
-//      } catch (TableNotFoundException e) {
-//          
-//          e.printStackTrace();
-//      }
-//      
-//      scan.setRange(new Range());
-//      
-//      for (Map.Entry<Key, Value> entry : scan) {
-//          System.out.println("Key row string is " + entry.getKey().getRow().toString());
-//          System.out.println("Key is " + entry.getKey());
-//          System.out.println("Value is " + (new String(entry.getKey().getColumnQualifier().toString())));
-//
-//      }
-        
-      
-        
-        index.add(ais2);
-        index.add(ais1);
-        
-        
-//        Assert.assertEquals((double)crh1.getCount(), ais1.cardinality());
-//        Assert.assertEquals((double)crh2.getCount(), ais2.cardinality());
-//          
-//  
-//        crh1 = new CountingResultHandler();
-//        crh2 = new CountingResultHandler();
-//        
-//        
-//        try {
-//            conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh1);
-//        } catch (TupleQueryResultHandlerException e1) {
-//            
-//            e1.printStackTrace();
-//        } catch (QueryEvaluationException e1) {
-//            
-//            e1.printStackTrace();
-//        } catch (MalformedQueryException e1) {
-//            
-//            e1.printStackTrace();
-//        } catch (RepositoryException e1) {
-//            
-//            e1.printStackTrace();
-//        }
-
-        
-        
-        ParsedQuery pq = null;
-        SPARQLParser sp = new SPARQLParser();
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-        processor.process(pq.getTupleExpr());
-
-        
-        Sail processingSail = new ExternalSail(s, processor);
-        SailRepository smartSailRepo = new SailRepository(processingSail);
-        try {
-            smartSailRepo.initialize();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
-     
-
-        try {
-            smartSailRepo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
-      //  Assert.assertEquals(2, crh1.getCount());
-        Assert.assertEquals(2, crh2.getCount());
-         
-        
-        
-    }
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexThreeVarOrder2() {
-        
-        
-        
-        URI superclass = new URIImpl("uri:superclass");
-        URI superclass2 = new URIImpl("uri:superclass2");
-       
-
-        try {
-            conn.add(subclass, RDF.TYPE, superclass);
-            conn.add(subclass2, RDF.TYPE, superclass2);
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        } catch (RepositoryException e5) {
-            
-            e5.printStackTrace();
-        }
-        
-        
-        try {
-            if(accCon.tableOperations().exists("table2")){
-                accCon.tableOperations().delete("table2");
-            }
-            accCon.tableOperations().create("table2");
-        } catch (AccumuloException e4) {
-            
-            e4.printStackTrace();
-        } catch (AccumuloSecurityException e4) {
-            
-            e4.printStackTrace();
-        } catch (TableExistsException e4) {
-            
-            e4.printStackTrace();
-        } catch (TableNotFoundException e) {
-            
-            e.printStackTrace();
-        }
-        
-        
-        try {
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        } catch (RepositoryException e3) {
-            
-            e3.printStackTrace();
-        }
-       
-        
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?c ?e ?l  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?o ?f ?e ?c ?l  " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?f ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        
-        
-        
-        
-
-        
-        CountingResultHandler crh1 = new CountingResultHandler();
-        CountingResultHandler crh2 = new CountingResultHandler();
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString).evaluate(crh1);
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString2).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e2) {
-            
-            e2.printStackTrace();
-        } catch (QueryEvaluationException e2) {
-            
-            e2.printStackTrace();
-        } catch (MalformedQueryException e2) {
-            
-            e2.printStackTrace();
-        } catch (RepositoryException e2) {
-            
-            e2.printStackTrace();
-        }
-
-       
-
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        AccumuloIndexSet ais1 = null;
-        AccumuloIndexSet ais2 = null;
-                
-        try {
-            ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablename);
-            ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (SailException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MutationsRejectedException e) {
-            
-            e.printStackTrace();
-        } catch (TableNotFoundException e) {
-            
-            e.printStackTrace();
-        }
-      
-        
-        index.add(ais2);
-        index.add(ais1);
-        
-        
-        Assert.assertEquals((double)crh1.getCount(), ais1.cardinality());
-        Assert.assertEquals((double)crh2.getCount(), ais2.cardinality());
-          
-  
-        crh1 = new CountingResultHandler();
-        crh2 = new CountingResultHandler();
-        
-//        
-//        try {
-//            conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh1);
-//        } catch (TupleQueryResultHandlerException e1) {
-//            
-//            e1.printStackTrace();
-//        } catch (QueryEvaluationException e1) {
-//            
-//            e1.printStackTrace();
-//        } catch (MalformedQueryException e1) {
-//            
-//            e1.printStackTrace();
-//        } catch (RepositoryException e1) {
-//            
-//            e1.printStackTrace();
-//        }
-
-        
-      
-        
-        ParsedQuery pq = null;
-        SPARQLParser sp = new SPARQLParser();
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-       
-
-        
-        Sail processingSail = new ExternalSail(s, processor);
-        SailRepository smartSailRepo = new SailRepository(processingSail);
-        try {
-            smartSailRepo.initialize();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
-     
-
-        try {
-            smartSailRepo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
- //       Assert.assertEquals(2, crh1.getCount());
-        Assert.assertEquals(2, crh2.getCount());
-         
-        
-        
-    }
-    
-    
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexThreeVarOrder3ThreeBindingSet() {
-        
-        
-        URI sub3 = new URIImpl("uri:entity3");
-        URI subclass3 = new URIImpl("uri:class3");
-        URI obj3 = new URIImpl("uri:obj3");
-                
-        URI superclass = new URIImpl("uri:superclass");
-        URI superclass2 = new URIImpl("uri:superclass2");
-        URI superclass3 = new URIImpl("uri:superclass3");
-       
-
-        try {
-            
-            conn.add(sub3, RDF.TYPE, subclass3);
-            conn.add(sub3, RDFS.LABEL, new LiteralImpl("label3"));
-            conn.add(sub3, talksTo, obj3);
-            
-            conn.add(subclass, RDF.TYPE, superclass);
-            conn.add(subclass2, RDF.TYPE, superclass2);
-            conn.add(subclass3, RDF.TYPE, superclass3);
-            
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-            conn.add(obj3, RDFS.LABEL, new LiteralImpl("label3"));
-            
-        } catch (RepositoryException e5) {
-            
-            e5.printStackTrace();
-        }
-        
-        
-        try {
-            if(accCon.tableOperations().exists("table2")){
-                accCon.tableOperations().delete("table2");
-            }
-            accCon.tableOperations().create("table2");
-        } catch (AccumuloException e4) {
-            
-            e4.printStackTrace();
-        } catch (AccumuloSecurityException e4) {
-            
-            e4.printStackTrace();
-        } catch (TableExistsException e4) {
-            
-            e4.printStackTrace();
-        } catch (TableNotFoundException e) {
-            
-            e.printStackTrace();
-        }
-        
-        
-        try {
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        } catch (RepositoryException e3) {
-            
-            e3.printStackTrace();
-        }
-        
-          
-        
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?c ?e ?l  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?o ?f ?l ?e ?c  " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?f ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        
-        
-        
-        
-
-        
-        CountingResultHandler crh1 = new CountingResultHandler();
-        CountingResultHandler crh2 = new CountingResultHandler();
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString).evaluate(crh1);
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString2).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e2) {
-            
-            e2.printStackTrace();
-        } catch (QueryEvaluationException e2) {
-            
-            e2.printStackTrace();
-        } catch (MalformedQueryException e2) {
-            
-            e2.printStackTrace();
-        } catch (RepositoryException e2) {
-            
-            e2.printStackTrace();
-        }
-
-       
-
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        AccumuloIndexSet ais1 = null;
-        AccumuloIndexSet ais2 = null;
-                
-        try {
-            ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablename);
-            ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (SailException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MutationsRejectedException e) {
-            
-            e.printStackTrace();
-        } catch (TableNotFoundException e) {
-            
-            e.printStackTrace();
-        }
-        
-        
-        
-//      Scanner scan1 = null;
-//      Scanner scan2 = null;
-//      try {
-//          scan1 = accCon.createScanner(tablename, new Authorizations("auths"));
-//          scan2 = accCon.createScanner("table2", new Authorizations("auths"));
-//      } catch (TableNotFoundException e) {
-//          
-//          e.printStackTrace();
-//      }
-//      
-//      scan1.setRange(new Range());
-//      
-//      for (Map.Entry<Key, Value> entry : scan1) {
-//          System.out.println("Key row string is " + entry.getKey().getRow().toString());
-//          System.out.println("Key is " + entry.getKey());
-//          System.out.println("Value is " + (new String(entry.getKey().getColumnQualifier().toString())));
-//
-//      }
-//      
-//      
-//      scan2.setRange(new Range());
-//      
-//      for (Map.Entry<Key, Value> entry : scan2) {
-//          System.out.println("Key row string is " + entry.getKey().getRow().toString());
-//          System.out.println("Key is " + entry.getKey());
-//          System.out.println("Value is " + (new String(entry.getKey().getColumnQualifier().toString())));
-//
-//      }
-        
-      
-        
-        index.add(ais2);
-        index.add(ais1);
-        
-        
-        Assert.assertEquals((double)crh1.getCount(), ais1.cardinality());
-        Assert.assertEquals((double)crh2.getCount(), ais2.cardinality());
-          
-  
-        crh1 = new CountingResultHandler();
-        crh2 = new CountingResultHandler();
-        
-        
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh1);
-        } catch (TupleQueryResultHandlerException e1) {
-            
-            e1.printStackTrace();
-        } catch (QueryEvaluationException e1) {
-            
-            e1.printStackTrace();
-        } catch (MalformedQueryException e1) {
-            
-            e1.printStackTrace();
-        } catch (RepositoryException e1) {
-            
-            e1.printStackTrace();
-        }
-
-        
-     
-        
-        ParsedQuery pq = null;
-        SPARQLParser sp = new SPARQLParser();
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-        
-
-        
-        Sail processingSail = new ExternalSail(s, processor);
-        SailRepository smartSailRepo = new SailRepository(processingSail);
-        try {
-            smartSailRepo.initialize();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
-        // new SPARQLResultsXMLWriter(System.out)
-
-        try {
-            smartSailRepo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
-        Assert.assertEquals(3, crh1.getCount());
-        Assert.assertEquals(3, crh2.getCount());
-         
-        
-        
-    }
-    
-    
-    
-    
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexThreeVarOrder5ThreeBindingSet() {
-        
-        
-        URI sub3 = new URIImpl("uri:entity3");
-        URI subclass3 = new URIImpl("uri:class3");
-        URI obj3 = new URIImpl("uri:obj3");
-                
-        URI superclass = new URIImpl("uri:superclass");
-        URI superclass2 = new URIImpl("uri:superclass2");
-        URI superclass3 = new URIImpl("uri:superclass3");
-       
-
-        try {
-            
-            conn.add(sub3, RDF.TYPE, subclass3);
-            conn.add(sub3, RDFS.LABEL, new LiteralImpl("label3"));
-            conn.add(sub3, talksTo, obj3);
-            
-            conn.add(subclass, RDF.TYPE, superclass);
-            conn.add(subclass2, RDF.TYPE, superclass2);
-            conn.add(subclass3, RDF.TYPE, superclass3);
-            
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-            conn.add(obj3, RDFS.LABEL, new LiteralImpl("label3"));
-            
-        } catch (RepositoryException e5) {
-            
-            e5.printStackTrace();
-        }
-        
-        
-        try {
-            if(accCon.tableOperations().exists("table2")){
-                accCon.tableOperations().delete("table2");
-            }
-            accCon.tableOperations().create("table2");
-        } catch (AccumuloException e4) {
-            
-            e4.printStackTrace();
-        } catch (AccumuloSecurityException e4) {
-            
-            e4.printStackTrace();
-        } catch (TableExistsException e4) {
-            
-            e4.printStackTrace();
-        } catch (TableNotFoundException e) {
-            
-            e.printStackTrace();
-        }
-        
-        
-        try {
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        } catch (RepositoryException e3) {
-            
-            e3.printStackTrace();
-        }
-        
-          
-        
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?c ?e ?l  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?o ?f ?e ?l ?c  " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?f ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        
-        
-        
-        
-
-        
-        CountingResultHandler crh1 = new CountingResultHandler();
-        CountingResultHandler crh2 = new CountingResultHandler();
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString).evaluate(crh1);
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString2).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e2) {
-            
-            e2.printStackTrace();
-        } catch (QueryEvaluationException e2) {
-            
-            e2.printStackTrace();
-        } catch (MalformedQueryException e2) {
-            
-            e2.printStackTrace();
-        } catch (RepositoryException e2) {
-            
-            e2.printStackTrace();
-        }
-
-       
-
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        AccumuloIndexSet ais1 = null;
-        AccumuloIndexSet ais2 = null;
-                
-        try {
-            ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablename);
-            ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (SailException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MutationsRejectedException e) {
-            
-            e.printStackTrace();
-        } catch (TableNotFoundException e) {
-            
-            e.printStackTrace();
-        }
-         
-        
-        index.add(ais2);
-        index.add(ais1);
-        
-        
-        Assert.assertEquals((double)crh1.getCount(), ais1.cardinality());
-        Assert.assertEquals((double)crh2.getCount(), ais2.cardinality());
-          
-  
-        crh1 = new CountingResultHandler();
-        crh2 = new CountingResultHandler();
-        
-        
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh1);
-        } catch (TupleQueryResultHandlerException e1) {
-            
-            e1.printStackTrace();
-        } catch (QueryEvaluationException e1) {
-            
-            e1.printStackTrace();
-        } catch (MalformedQueryException e1) {
-            
-            e1.printStackTrace();
-        } catch (RepositoryException e1) {
-            
-            e1.printStackTrace();
-        }
-
-        
-        
-        
-        ParsedQuery pq = null;
-        SPARQLParser sp = new SPARQLParser();
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-       
-
-        
-        Sail processingSail = new ExternalSail(s, processor);
-        SailRepository smartSailRepo = new SailRepository(processingSail);
-        try {
-            smartSailRepo.initialize();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
-        // new SPARQLResultsXMLWriter(System.out)
-
-        try {
-            smartSailRepo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
-        Assert.assertEquals(3, crh1.getCount());
-        Assert.assertEquals(3, crh2.getCount());
-         
-        
-        
-    }
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexThreeVarOrder4ThreeBindingSet() {
-        
-        
-        URI sub3 = new URIImpl("uri:entity3");
-        URI subclass3 = new URIImpl("uri:class3");
-        URI obj3 = new URIImpl("uri:obj3");
-                
-        URI superclass = new URIImpl("uri:superclass");
-        URI superclass2 = new URIImpl("uri:superclass2");
-        URI superclass3 = new URIImpl("uri:superclass3");
-       
-
-        try {
-            
-            conn.add(sub3, RDF.TYPE, subclass3);
-            conn.add(sub3, RDFS.LABEL, new LiteralImpl("label3"));
-            conn.add(sub3, talksTo, obj3);
-            
-            conn.add(subclass, RDF.TYPE, superclass);
-            conn.add(subclass2, RDF.TYPE, superclass2);
-            conn.add(subclass3, RDF.TYPE, superclass3);
-            
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-            conn.add(obj3, RDFS.LABEL, new LiteralImpl("label3"));
-            
-        } catch (RepositoryException e5) {
-            
-            e5.printStackTrace();
-        }
-        
-        
-        try {
-            if(accCon.tableOperations().exists("table2")){
-                accCon.tableOperations().delete("table2");
-            }
-            accCon.tableOperations().create("table2");
-        } catch (AccumuloException e4) {
-            
-            e4.printStackTrace();
-        } catch (AccumuloSecurityException e4) {
-            
-            e4.printStackTrace();
-        } catch (TableExistsException e4) {
-            
-            e4.printStackTrace();
-        } catch (TableNotFoundException e) {
-            
-            e.printStackTrace();
-        }
-        
-        
-        try {
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        } catch (RepositoryException e3) {
-            
-            e3.printStackTrace();
-        }
-        
-          
-        
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?c ?e ?l  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?o ?f ?c ?e ?l  " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?f ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        
-        
-        
-        
-
-        
-        CountingResultHandler crh1 = new CountingResultHandler();
-        CountingResultHandler crh2 = new CountingResultHandler();
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString).evaluate(crh1);
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString2).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e2) {
-            
-            e2.printStackTrace();
-        } catch (QueryEvaluationException e2) {
-            
-            e2.printStackTrace();
-        } catch (MalformedQueryException e2) {
-            
-            e2.printStackTrace();
-        } catch (RepositoryException e2) {
-            
-            e2.printStackTrace();
-        }
-
-       
-
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        AccumuloIndexSet ais1 = null;
-        AccumuloIndexSet ais2 = null;
-                
-        try {
-            ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablename);
-            ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (SailException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MutationsRejectedException e) {
-            
-            e.printStackTrace();
-        } catch (TableNotFoundException e) {
-            
-            e.printStackTrace();
-        }
-        
-        
-        
-//      Scanner scan1 = null;
-//      Scanner scan2 = null;
-//      try {
-//          scan1 = accCon.createScanner(tablename, new Authorizations("auths"));
-//          scan2 = accCon.createScanner("table2", new Authorizations("auths"));
-//      } catch (TableNotFoundException e) {
-//          
-//          e.printStackTrace();
-//      }
-//      
-//      scan1.setRange(new Range());
-//      
-//      for (Map.Entry<Key, Value> entry : scan1) {
-//          System.out.println("Key row string is " + entry.getKey().getRow().toString());
-//          System.out.println("Key is " + entry.getKey());
-//          System.out.println("Value is " + (new String(entry.getKey().getColumnQualifier().toString())));
-//
-//      }
-//      
-//      
-//      scan2.setRange(new Range());
-//      
-//      for (Map.Entry<Key, Value> entry : scan2) {
-//          System.out.println("Key row string is " + entry.getKey().getRow().toString());
-//          System.out.println("Key is " + entry.getKey());
-//          System.out.println("Value is " + (new String(entry.getKey().getColumnQualifier().toString())));
-//
-//      }
-        
-      
-        
-        index.add(ais2);
-        index.add(ais1);
-        
-        
-        Assert.assertEquals((double)crh1.getCount(), ais1.cardinality());
-        Assert.assertEquals((double)crh2.getCount(), ais2.cardinality());
-          
-  
-        crh1 = new CountingResultHandler();
-        crh2 = new CountingResultHandler();
-        
-        
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh1);
-        } catch (TupleQueryResultHandlerException e1) {
-            
-            e1.printStackTrace();
-        } catch (QueryEvaluationException e1) {
-            
-            e1.printStackTrace();
-        } catch (MalformedQueryException e1) {
-            
-            e1.printStackTrace();
-        } catch (RepositoryException e1) {
-            
-            e1.printStackTrace();
-        }
-
-        
-        
-        
-        ParsedQuery pq = null;
-        SPARQLParser sp = new SPARQLParser();
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-        
-
-        
-        Sail processingSail = new ExternalSail(s, processor);
-        SailRepository smartSailRepo = new SailRepository(processingSail);
-        try {
-            smartSailRepo.initialize();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
-        //new SPARQLResultsXMLWriter(System.out)
-
-        try {
-            smartSailRepo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
-        Assert.assertEquals(3, crh1.getCount());
-        Assert.assertEquals(3, crh2.getCount());
-         
-        
-        
-    }
-    
-    
-    
-    
-    
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexThreeVarOrder6ThreeBindingSet() {
-        
-        
-        URI sub3 = new URIImpl("uri:entity3");
-        URI subclass3 = new URIImpl("uri:class3");
-        URI obj3 = new URIImpl("uri:obj3");
-                
-        URI superclass = new URIImpl("uri:superclass");
-        URI superclass2 = new URIImpl("uri:superclass2");
-        URI superclass3 = new URIImpl("uri:superclass3");
-       
-
-        try {
-            
-            conn.add(sub3, RDF.TYPE, subclass3);
-            conn.add(sub3, RDFS.LABEL, new LiteralImpl("label3"));
-            conn.add(sub3, talksTo, obj3);
-            
-            conn.add(subclass, RDF.TYPE, superclass);
-            conn.add(subclass2, RDF.TYPE, superclass2);
-            conn.add(subclass3, RDF.TYPE, superclass3);
-            
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-            conn.add(obj3, RDFS.LABEL, new LiteralImpl("label3"));
-            
-        } catch (RepositoryException e5) {
-            
-            e5.printStackTrace();
-        }
-        
-        
-        try {
-            if(accCon.tableOperations().exists("table2")){
-                accCon.tableOperations().delete("table2");
-            }
-            accCon.tableOperations().create("table2");
-        } catch (AccumuloException e4) {
-            
-            e4.printStackTrace();
-        } catch (AccumuloSecurityException e4) {
-            
-            e4.printStackTrace();
-        } catch (TableExistsException e4) {
-            
-            e4.printStackTrace();
-        } catch (TableNotFoundException e) {
-            
-            e.printStackTrace();
-        }
-        
-        
-        try {
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        } catch (RepositoryException e3) {
-            
-            e3.printStackTrace();
-        }
-        
-          
-        
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?c ?e ?l  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?c ?l ?e ?o ?f " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?f ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        
-        
-        
-        
-
-        
-        CountingResultHandler crh1 = new CountingResultHandler();
-        CountingResultHandler crh2 = new CountingResultHandler();
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString).evaluate(crh1);
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString2).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e2) {
-            
-            e2.printStackTrace();
-        } catch (QueryEvaluationException e2) {
-            
-            e2.printStackTrace();
-        } catch (MalformedQueryException e2) {
-            
-            e2.printStackTrace();
-        } catch (RepositoryException e2) {
-            
-            e2.printStackTrace();
-        }
-
-       
-
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        AccumuloIndexSet ais1 = null;
-        AccumuloIndexSet ais2 = null;
-                
-        try {
-            ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablename);
-            ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (SailException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MutationsRejectedException e) {
-            
-            e.printStackTrace();
-        } catch (TableNotFoundException e) {
-            
-            e.printStackTrace();
-        }
-        
-        
-      
-        
-        index.add(ais2);
-        index.add(ais1);
-        
-        
-        Assert.assertEquals((double)crh1.getCount(), ais1.cardinality());
-        Assert.assertEquals((double)crh2.getCount(), ais2.cardinality());
-          
-  
-        crh1 = new CountingResultHandler();
-        crh2 = new CountingResultHandler();
-        
-        
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh1);
-        } catch (TupleQueryResultHandlerException e1) {
-            
-            e1.printStackTrace();
-        } catch (QueryEvaluationException e1) {
-            
-            e1.printStackTrace();
-        } catch (MalformedQueryException e1) {
-            
-            e1.printStackTrace();
-        } catch (RepositoryException e1) {
-            
-            e1.printStackTrace();
-        }
-
-        
-       
-        
-        ParsedQuery pq = null;
-        SPARQLParser sp = new SPARQLParser();
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-      
-
-        
-        Sail processingSail = new ExternalSail(s, processor);
-        SailRepository smartSailRepo = new SailRepository(processingSail);
-        try {
-            smartSailRepo.initialize();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
-        //new SPARQLResultsXMLWriter(System.out)
-
-        try {
-            smartSailRepo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
-        Assert.assertEquals(3, crh1.getCount());
-        Assert.assertEquals(3, crh2.getCount());
-         
-        
-        
-    }
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexThreeVarOrder7ThreeBindingSet() {
-        
-        
-        URI sub3 = new URIImpl("uri:entity3");
-        URI subclass3 = new URIImpl("uri:class3");
-        URI obj3 = new URIImpl("uri:obj3");
-                
-        URI superclass = new URIImpl("uri:superclass");
-        URI superclass2 = new URIImpl("uri:superclass2");
-        URI superclass3 = new URIImpl("uri:superclass3");
-       
-
-        try {
-            
-            conn.add(sub3, RDF.TYPE, subclass3);
-            conn.add(sub3, RDFS.LABEL, new LiteralImpl("label3"));
-            conn.add(sub3, talksTo, obj3);
-            
-            conn.add(subclass, RDF.TYPE, superclass);
-            conn.add(subclass2, RDF.TYPE, superclass2);
-            conn.add(subclass3, RDF.TYPE, superclass3);
-            
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-            conn.add(obj3, RDFS.LABEL, new LiteralImpl("label3"));
-            
-        } catch (RepositoryException e5) {
-            
-            e5.printStackTrace();
-        }
-        
-        
-        try {
-            if(accCon.tableOperations().exists("table2")){
-                accCon.tableOperations().delete("table2");
-            }
-            accCon.tableOperations().create("table2");
-        } catch (AccumuloException e4) {
-            
-            e4.printStackTrace();
-        } catch (AccumuloSecurityException e4) {
-            
-            e4.printStackTrace();
-        } catch (TableExistsException e4) {
-            
-            e4.printStackTrace();
-        } catch (TableNotFoundException e) {
-            
-            e.printStackTrace();
-        }
-        
-        
-        try {
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        } catch (RepositoryException e3) {
-            
-            e3.printStackTrace();
-        }
-        
-          
-        
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?c ?e ?l  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?o ?l ?c ?e ?f " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?f ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        
-        
-        
-        
-
-        
-        CountingResultHandler crh1 = new CountingResultHandler();
-        CountingResultHandler crh2 = new CountingResultHandler();
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString).evaluate(crh1);
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString2).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e2) {
-            
-            e2.printStackTrace();
-        } catch (QueryEvaluationException e2) {
-            
-            e2.printStackTrace();
-        } catch (MalformedQueryException e2) {
-            
-            e2.printStackTrace();
-        } catch (RepositoryException e2) {
-            
-            e2.printStackTrace();
-        }
-
-       
-
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        AccumuloIndexSet ais1 = null;
-        AccumuloIndexSet ais2 = null;
-                
-        try {
-            ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablename);
-            ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (SailException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MutationsRejectedException e) {
-            
-            e.printStackTrace();
-        } catch (TableNotFoundException e) {
-            
-            e.printStackTrace();
-        }
-        
-        
-      
-        
-        index.add(ais2);
-        index.add(ais1);
-        
-        
-        Assert.assertEquals((double)crh1.getCount(), ais1.cardinality());
-        Assert.assertEquals((double)crh2.getCount(), ais2.cardinality());
-          
-  
-        crh1 = new CountingResultHandler();
-        crh2 = new CountingResultHandler();
-        
-        
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh1);
-        } catch (TupleQueryResultHandlerException e1) {
-            
-            e1.printStackTrace();
-        } catch (QueryEvaluationException e1) {
-            
-            e1.printStackTrace();
-        } catch (MalformedQueryException e1) {
-            
-            e1.printStackTrace();
-        } catch (RepositoryException e1) {
-            
-            e1.printStackTrace();
-        }
-
-        
-        
-        
-        ParsedQuery pq = null;
-        SPARQLParser sp = new SPARQLParser();
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        }
-       
-        ExternalProcessor processor = new ExternalProcessor(index);
-
-
-        
-        Sail processingSail = new ExternalSail(s, processor);
-        SailRepository smartSailRepo = new SailRepository(processingSail);
-        try {
-            smartSailRepo.initialize();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
-        //new SPARQLResultsXMLWriter(System.out)
-
-        try {
-            smartSailRepo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (RepositoryException e) {
-            
-            e.printStackTrace();
-        }
-        
-        Assert.assertEquals(3, crh1.getCount());
-        Assert.assertEquals(3, crh2.getCount());
-         
-        
-        
-    }
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexThreeVarInvalidOrder1() {
-        
-        
-        
-        URI superclass = new URIImpl("uri:superclass");
-        URI superclass2 = new URIImpl("uri:superclass2");
-       
-
-        try {
-            conn.add(subclass, RDF.TYPE, superclass);
-            conn.add(subclass2, RDF.TYPE, superclass2);
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        } catch (RepositoryException e5) {
-            
-            e5.printStackTrace();
-        }
-        
-        
-        try {
-            if(accCon.tableOperations().exists("table2")){
-                accCon.tableOperations().delete("table2");
-            }
-            accCon.tableOperations().create("table2");
-        } catch (AccumuloException e4) {
-            
-            e4.printStackTrace();
-        } catch (AccumuloSecurityException e4) {
-            
-            e4.printStackTrace();
-        } catch (TableExistsException e4) {
-            
-            e4.printStackTrace();
-        } catch (TableNotFoundException e) {
-            
-            e.printStackTrace();
-        }
-        
-        
-        try {
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        } catch (RepositoryException e3) {
-            
-            e3.printStackTrace();
-        }
-       
-        
-        
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?c ?e ?l  " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-        
-        
-        String indexSparqlString2 = ""//
-                + "SELECT ?e ?o ?f ?c ?l  " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-        
-        
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?f ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        
-        
-        
-        
-
-        
-        CountingResultHandler crh1 = new CountingResultHandler();
-        CountingResultHandler crh2 = new CountingResultHandler();
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString).evaluate(crh1);
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString2).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e2) {
-            
-            e2.printStackTrace();
-        } catch (QueryEvaluationException e2) {
-            
-            e2.printStackTrace();
-        } catch (MalformedQueryException e2) {
-            
-            e2.printStackTrace();
-        } catch (RepositoryException e2) {
-            
-            e2.printStackTrace();
-        }
-
-       
-
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        AccumuloIndexSet ais1 = null;
-        AccumuloIndexSet ais2 = null;
-                
-        try {
-            ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablename);
-            ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-        } catch (MalformedQueryException e) {
-            
-            e.printStackTrace();
-        } catch (SailException e) {
-            
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            
-            e.printStackTrace();
-        } catch (MutationsRejectedException e) {
-            
-            e.printStackTrace();
-        } catch (TableNotFoundException e) {
-            
-            e.printStackTrace();
-        }
-      
-        
-        index.add(ais2

<TRUNCATED>


[09/16] incubator-rya git commit: RYA-32 Improve how metadata and values are written to Accumulo PCJ tables

Posted by mi...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/ValidIndexCombinationGeneratorTest.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/ValidIndexCombinationGeneratorTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/ValidIndexCombinationGeneratorTest.java
index 38f7813..3b74962 100644
--- a/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/ValidIndexCombinationGeneratorTest.java
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/ValidIndexCombinationGeneratorTest.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.IndexPlanValidator;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -19,602 +19,515 @@ package mvm.rya.indexing.IndexPlanValidator;
  * under the License.
  */
 
-
-
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Set;
-import junit.framework.Assert;
+
 import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
 import mvm.rya.indexing.external.tupleSet.SimpleExternalTupleSet;
+
+import org.junit.Assert;
 import org.junit.Test;
 import org.openrdf.query.MalformedQueryException;
 import org.openrdf.query.algebra.Projection;
 import org.openrdf.query.parser.ParsedQuery;
 import org.openrdf.query.parser.sparql.SPARQLParser;
-import com.google.common.collect.Lists;
 
+import com.google.common.collect.Lists;
 
 public class ValidIndexCombinationGeneratorTest {
 
-    
-    
-    
-    
-
-    @Test
-    public void singleIndex() {
-        String q1 = ""//
-                + "SELECT ?f ?m ?d " //
-                + "{" //
-                + "  ?f a ?m ."//
-                + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-                + "  ?d <uri:talksTo> ?f . "//
-                + "  ?f <uri:hangOutWith> ?m ." //
-                + "  ?m <uri:hangOutWith> ?d ." //
-                + "  ?f <uri:associatesWith> ?m ." //
-                + "  ?m <uri:associatesWith> ?d ." //
-                + "}";//
-        
-        
-       
-        
-        
-
-        SPARQLParser parser = new SPARQLParser();
-        ParsedQuery pq1 = null;
-       
-        
-        SimpleExternalTupleSet extTup1 = null;
-        
-        
-        
-        
-        
-        
-        try {
-            pq1 = parser.parseQuery(q1, null);
-            
-           
-
-            extTup1 = new SimpleExternalTupleSet((Projection) pq1.getTupleExpr());
-            
-          
-        } catch (MalformedQueryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-        
-        List<ExternalTupleSet> indexList = Lists.newArrayList();
-        indexList.add(extTup1);
-        
-        
-        ValidIndexCombinationGenerator vic = new ValidIndexCombinationGenerator(pq1.getTupleExpr());
-        Iterator<List<ExternalTupleSet>> combos = vic.getValidIndexCombos(indexList);
-        int size = 0;
-        while(combos.hasNext()) {
-            combos.hasNext();
-            size++;
-            combos.next();
-            combos.hasNext();
-        }
-        
-       Assert.assertTrue(!combos.hasNext());
-       Assert.assertEquals(1,size);
-        
-        
-    }
-    
-    
-    
-    
-    
-    
-    @Test
-    public void medQueryEightOverlapIndex() {
-        String q1 = ""//
-                + "SELECT ?f ?m ?d " //
-                + "{" //
-                + "  ?f a ?m ."//
-                + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-                + "  ?d <uri:talksTo> ?f . "//
-                + "  ?f <uri:hangOutWith> ?m ." //
-                + "  ?m <uri:hangOutWith> ?d ." //
-                + "  ?f <uri:associatesWith> ?m ." //
-                + "  ?m <uri:associatesWith> ?d ." //
-                + "}";//
-        
-        
-        String q2 = ""//
-                + "SELECT ?t ?s ?u " //
-                + "{" //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "  ?u <uri:talksTo> ?s . "//
-                + "}";//
-        
-        
-        String q3 = ""//
-                + "SELECT ?s ?t ?u " //
-                + "{" //
-                + "  ?s <uri:hangOutWith> ?t ." //
-                + "  ?t <uri:hangOutWith> ?u ." //
-                + "}";//
-        
-        String q4 = ""//
-                + "SELECT ?s ?t ?u " //
-                + "{" //
-                + "  ?s <uri:associatesWith> ?t ." //
-                + "  ?t <uri:associatesWith> ?u ." //
-                + "}";//
-        
-        
-        String q5 = ""//
-                + "SELECT ?t ?s ?u " //
-                + "{" //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "  ?u <uri:talksTo> ?s . "//
-                + "  ?s <uri:hangOutWith> ?t ." //
-                + "  ?t <uri:hangOutWith> ?u ." //
-                + "}";//
-        
-        String q6 = ""//
-                + "SELECT ?s ?t ?u " //
-                + "{" //
-                + "  ?s <uri:associatesWith> ?t ." //
-                + "  ?t <uri:associatesWith> ?u ." //
-                + "  ?s <uri:hangOutWith> ?t ." //
-                + "  ?t <uri:hangOutWith> ?u ." //
-                + "}";//
-        
-        
-        String q7 = ""//
-                + "SELECT ?s ?t ?u " //
-                + "{" //
-                + "  ?s <uri:associatesWith> ?t ." //
-                + "  ?t <uri:associatesWith> ?u ." //
-                + "  ?t <uri:hangOutWith> ?u ." //
-                + "}";//
-        
-        
-        
-        String q8 = ""//
-                + "SELECT ?t ?s ?u " //
-                + "{" //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "  ?u <uri:talksTo> ?s . "//
-                + "  ?s <uri:associatesWith> ?t ." //
-                + "}";//
-        
-        
-        String q9 = ""//
-                + "SELECT ?t ?s ?u " //
-                + "{" //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "}";//
-        
-        
-        
-        
-        
-        
-        
-        
-
-        SPARQLParser parser = new SPARQLParser();
-        ParsedQuery pq1 = null;
-        ParsedQuery pq2 = null;
-        ParsedQuery pq3 = null;
-        ParsedQuery pq4 = null;
-        ParsedQuery pq5 = null;
-        ParsedQuery pq6 = null;
-        ParsedQuery pq7 = null;
-        ParsedQuery pq8 = null;
-        ParsedQuery pq9 = null;
-        
-        SimpleExternalTupleSet extTup1 = null;
-        SimpleExternalTupleSet extTup2 = null;
-        SimpleExternalTupleSet extTup3 = null;
-        SimpleExternalTupleSet extTup4 = null;
-        SimpleExternalTupleSet extTup5 = null;
-        SimpleExternalTupleSet extTup6 = null;
-        SimpleExternalTupleSet extTup7 = null;
-        SimpleExternalTupleSet extTup8 = null;
-        
-        
-        
-        
-        
-        try {
-            pq1 = parser.parseQuery(q1, null);
-            pq2 = parser.parseQuery(q2, null);
-            pq3 = parser.parseQuery(q3, null);
-            pq4 = parser.parseQuery(q4, null);
-            pq5 = parser.parseQuery(q5, null);
-            pq6 = parser.parseQuery(q6, null);
-            pq7 = parser.parseQuery(q7, null);
-            pq8 = parser.parseQuery(q8, null);
-            pq9 = parser.parseQuery(q9, null);
-           
-
-            extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-            extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
-            extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
-            extTup4 = new SimpleExternalTupleSet((Projection) pq5.getTupleExpr());
-            extTup5 = new SimpleExternalTupleSet((Projection) pq6.getTupleExpr());
-            extTup6 = new SimpleExternalTupleSet((Projection) pq7.getTupleExpr());
-            extTup7 = new SimpleExternalTupleSet((Projection) pq8.getTupleExpr());
-            extTup8 = new SimpleExternalTupleSet((Projection) pq9.getTupleExpr());
-            
-          
-        } catch (MalformedQueryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-        
-        List<ExternalTupleSet> indexList = Lists.newArrayList();
-        indexList.add(extTup1);
-        indexList.add(extTup2);
-        indexList.add(extTup3);
-        indexList.add(extTup4);
-        indexList.add(extTup5);
-        indexList.add(extTup6);
-        indexList.add(extTup7);
-        indexList.add(extTup8);
-        
-        
-        ValidIndexCombinationGenerator vic = new ValidIndexCombinationGenerator(pq1.getTupleExpr());
-        Iterator<List<ExternalTupleSet>> combos = vic.getValidIndexCombos(indexList);
-        int size = 0;
-        while(combos.hasNext()) {
-            combos.hasNext();
-            size++;
-            combos.next();
-            combos.hasNext();
-        }
-        
-       Assert.assertTrue(!combos.hasNext());
-       Assert.assertEquals(21,size);
-        
-        
-    }
-    
-    
-    
-    
-    
-    @Test
-    public void largeQuerySixteenIndexTest() {
-        
-        
-        String q1 = ""//
-                + "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r " //
-                + "{" //
-                + "  ?f a ?m ."//
-                + "  ?e a ?l ."//
-                + "  ?n a ?o ."//
-                + "  ?a a ?h ."//
-                + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-                + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
-                + "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
-                + "  ?d <uri:talksTo> ?f . "//
-                + "  ?c <uri:talksTo> ?e . "//
-                + "  ?p <uri:talksTo> ?n . "//
-                + "  ?r <uri:talksTo> ?a . "//
-                + "}";//
-        
-        
-        String q2 = ""//
-                + "SELECT ?s ?t ?u " //
-                + "{" //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "  ?u <uri:talksTo> ?s . "//
-                + "}";//
-        
-        
-        
-        String q3 = ""//
-                + "SELECT  ?s ?t ?u ?d ?f ?g " //
-                + "{" //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "  ?u <uri:talksTo> ?s . "//
-                + "  ?d a ?f ."//
-                + "  ?f <http://www.w3.org/2000/01/rdf-schema#label> ?g ."//
-                + "  ?g <uri:talksTo> ?d . "//
-                + "}";//
-        
-     
-        
-        
-        SPARQLParser parser = new SPARQLParser();
-
-        ParsedQuery pq1 = null;
-        ParsedQuery pq2 = null;
-        ParsedQuery pq3 = null;
-       
-
-        try {
-            pq1 = parser.parseQuery(q1, null);
-            pq2 = parser.parseQuery(q2, null);
-            pq3 = parser.parseQuery(q3, null);
-          
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
-       
-     
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-
-        list.add(extTup2);
-        list.add(extTup1);
-      
-
-        IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(pq1.getTupleExpr(), list);
-        List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
-        
-        
-        Assert.assertEquals(16, indexSet.size());
-        
-        ValidIndexCombinationGenerator vic = new ValidIndexCombinationGenerator(pq1.getTupleExpr());
-        Iterator<List<ExternalTupleSet>> eSet = vic.getValidIndexCombos(Lists.newArrayList(indexSet));
-        
-        int size = 0;
-        while(eSet.hasNext()) {
-            size++;
-            Assert.assertTrue(eSet.hasNext());
-            eSet.next();
-        }
-        
-        
-        Assert.assertTrue(!eSet.hasNext());
-        Assert.assertEquals(75, size);
-        
-    }
-    
-    
-    
-    
-    
-    
-    @Test
-    public void largeQueryFourtyIndexTest() {
-        
-        
-        String q1 = ""//
-                + "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r " //
-                + "{" //
-                + "  ?f a ?m ."//
-                + "  ?e a ?l ."//
-                + "  ?n a ?o ."//
-                + "  ?a a ?h ."//
-                + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-                + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
-                + "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
-                + "  ?d <uri:talksTo> ?f . "//
-                + "  ?c <uri:talksTo> ?e . "//
-                + "  ?p <uri:talksTo> ?n . "//
-                + "  ?r <uri:talksTo> ?a . "//
-                + "}";//
-        
-        
-        String q2 = ""//
-                + "SELECT ?s ?t ?u " //
-                + "{" //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "  ?u <uri:talksTo> ?s . "//
-                + "}";//
-        
-        
-        
-        String q3 = ""//
-                + "SELECT  ?s ?t ?u ?d ?f ?g " //
-                + "{" //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "  ?u <uri:talksTo> ?s . "//
-                + "  ?d a ?f ."//
-                + "  ?f <http://www.w3.org/2000/01/rdf-schema#label> ?g ."//
-                + "  ?g <uri:talksTo> ?d . "//
-                + "}";//
-        
-        
-        
-        String q4 = ""//
-                + "SELECT  ?s ?t ?u ?d ?f ?g ?a ?b ?c" //
-                + "{" //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "  ?u <uri:talksTo> ?s . "//
-                + "  ?d a ?f ."//
-                + "  ?f <http://www.w3.org/2000/01/rdf-schema#label> ?g ."//
-                + "  ?g <uri:talksTo> ?d . "//
-                + "  ?a a ?b ."//
-                + "  ?b <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-                + "  ?c <uri:talksTo> ?a . "//
-                + "}";//
-        
-        
-        SPARQLParser parser = new SPARQLParser();
-
-        ParsedQuery pq1 = null;
-        ParsedQuery pq2 = null;
-        ParsedQuery pq3 = null;
-        ParsedQuery pq4 = null;
-       
-
-        try {
-            pq1 = parser.parseQuery(q1, null);
-            pq2 = parser.parseQuery(q2, null);
-            pq3 = parser.parseQuery(q3, null);
-            pq4 = parser.parseQuery(q4, null);
-           
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
-        SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
-     
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-
-        list.add(extTup2);
-        list.add(extTup1);
-        list.add(extTup3);
-
-        IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(pq1.getTupleExpr(), list);
-        List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
-        Assert.assertEquals(40, indexSet.size());
-        
-        ValidIndexCombinationGenerator vic = new ValidIndexCombinationGenerator(pq1.getTupleExpr());
-        Iterator<List<ExternalTupleSet>> eSet = vic.getValidIndexCombos(Lists.newArrayList(indexSet));
-        
-        int size = 0;
-        while(eSet.hasNext()) {
-            size++;
-            Assert.assertTrue(eSet.hasNext());
-            eSet.next();
-        }
-        
-        Assert.assertTrue(!eSet.hasNext());
-        Assert.assertEquals(123, size);
-    }
-    
-    
-    
-    
-    
-    @Test
-    public void overlappingFilterIndex() {
-        
-      
-        String q5 = ""//
-                + "SELECT ?s ?t " //
-                + "{" //
-                + "  ?s a \"Person\" ." //
-                + "  ?t a \"Student\" ."//
-                + "}";//
-        
-        
-        String q4 = ""//
-                + "SELECT ?s ?t " //
-                + "{" //
-                + "  ?s a ?t ."//
-                + "  ?s <uri:talksTo> ?t . "//
-                + "}";//
-        
-        
-        String q3 = ""//
-                + "SELECT ?s ?t  " //
-                + "{" //
-                + "  Filter(?s > 5). "//
-                + "  ?s a ?t ."//
-                + "  ?s <uri:talksTo> ?t . "//
-                + "}";//
-        
-        
-        String q2 = ""//
-                + "SELECT ?s ?t  " //
-                + "{" //
-                + "  Filter(?s > 5). "//
-                + "  ?s a \"Person\" ." //
-                + "  ?t a \"Student\" ."//
-                + "}";//
-        
-        
-        
-        String q1 = ""//
-                + "SELECT  ?s ?t  " //
-                + "{" //
-                + "  Filter(?s > 5). "//
-                + "  ?s a ?t ."//
-                + "  ?s <uri:talksTo> ?t . "//
-                + "  ?s a \"Person\" ." //
-                + "  ?t a \"Student\" ."//
-                + "}";//
-        
-     
-        
-        
-        SPARQLParser parser = new SPARQLParser();
-
-        ParsedQuery pq1 = null;
-        ParsedQuery pq2 = null;
-        ParsedQuery pq3 = null;
-        ParsedQuery pq4 = null;
-        ParsedQuery pq5 = null;
-       
-
-        try {
-            pq1 = parser.parseQuery(q1, null);
-            pq2 = parser.parseQuery(q2, null);
-            pq3 = parser.parseQuery(q3, null);
-            pq4 = parser.parseQuery(q4, null);
-            pq5 = parser.parseQuery(q5, null);
-          
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection) pq3.getTupleExpr());
-        SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection) pq4.getTupleExpr());
-        SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet((Projection) pq5.getTupleExpr());
-       
-     
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-
-        list.add(extTup2);
-        list.add(extTup1);
-        list.add(extTup3);
-        list.add(extTup4);
-      
-
-        IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(pq1.getTupleExpr(), list);
-        List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
-        
-
-        
-        Assert.assertEquals(4, indexSet.size());
-        
-        ValidIndexCombinationGenerator vic = new ValidIndexCombinationGenerator(pq1.getTupleExpr());
-        Iterator<List<ExternalTupleSet>> eSet = vic.getValidIndexCombos(Lists.newArrayList(indexSet));
-        
-        int size = 0;
-        while(eSet.hasNext()) {
-            size++;
-            Assert.assertTrue(eSet.hasNext());
-            List<ExternalTupleSet> eList = eSet.next();
-
-        }
-        
-        
-        Assert.assertTrue(!eSet.hasNext());
-        Assert.assertEquals(7, size);
-        
-    }
-    
-
-    
-    
-    
-    
-    
-    
-    
-    
-    
+	@Test
+	public void singleIndex() {
+		String q1 = ""//
+				+ "SELECT ?f ?m ?d " //
+				+ "{" //
+				+ "  ?f a ?m ."//
+				+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+				+ "  ?d <uri:talksTo> ?f . "//
+				+ "  ?f <uri:hangOutWith> ?m ." //
+				+ "  ?m <uri:hangOutWith> ?d ." //
+				+ "  ?f <uri:associatesWith> ?m ." //
+				+ "  ?m <uri:associatesWith> ?d ." //
+				+ "}";//
+
+		SPARQLParser parser = new SPARQLParser();
+		ParsedQuery pq1 = null;
+		SimpleExternalTupleSet extTup1 = null;
+
+		try {
+			pq1 = parser.parseQuery(q1, null);
+			extTup1 = new SimpleExternalTupleSet(
+					(Projection) pq1.getTupleExpr());
+
+		} catch (MalformedQueryException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		List<ExternalTupleSet> indexList = Lists.newArrayList();
+		indexList.add(extTup1);
+		ValidIndexCombinationGenerator vic = new ValidIndexCombinationGenerator(
+				pq1.getTupleExpr());
+		Iterator<List<ExternalTupleSet>> combos = vic
+				.getValidIndexCombos(indexList);
+		int size = 0;
+		while (combos.hasNext()) {
+			combos.hasNext();
+			size++;
+			combos.next();
+			combos.hasNext();
+		}
+		Assert.assertTrue(!combos.hasNext());
+		Assert.assertEquals(1, size);
+	}
+
+	@Test
+	public void medQueryEightOverlapIndex() {
+		String q1 = ""//
+				+ "SELECT ?f ?m ?d " //
+				+ "{" //
+				+ "  ?f a ?m ."//
+				+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+				+ "  ?d <uri:talksTo> ?f . "//
+				+ "  ?f <uri:hangOutWith> ?m ." //
+				+ "  ?m <uri:hangOutWith> ?d ." //
+				+ "  ?f <uri:associatesWith> ?m ." //
+				+ "  ?m <uri:associatesWith> ?d ." //
+				+ "}";//
+
+		String q2 = ""//
+				+ "SELECT ?t ?s ?u " //
+				+ "{" //
+				+ "  ?s a ?t ."//
+				+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+				+ "  ?u <uri:talksTo> ?s . "//
+				+ "}";//
+
+		String q3 = ""//
+				+ "SELECT ?s ?t ?u " //
+				+ "{" //
+				+ "  ?s <uri:hangOutWith> ?t ." //
+				+ "  ?t <uri:hangOutWith> ?u ." //
+				+ "}";//
+
+		String q4 = ""//
+				+ "SELECT ?s ?t ?u " //
+				+ "{" //
+				+ "  ?s <uri:associatesWith> ?t ." //
+				+ "  ?t <uri:associatesWith> ?u ." //
+				+ "}";//
+
+		String q5 = ""//
+				+ "SELECT ?t ?s ?u " //
+				+ "{" //
+				+ "  ?s a ?t ."//
+				+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+				+ "  ?u <uri:talksTo> ?s . "//
+				+ "  ?s <uri:hangOutWith> ?t ." //
+				+ "  ?t <uri:hangOutWith> ?u ." //
+				+ "}";//
+
+		String q6 = ""//
+				+ "SELECT ?s ?t ?u " //
+				+ "{" //
+				+ "  ?s <uri:associatesWith> ?t ." //
+				+ "  ?t <uri:associatesWith> ?u ." //
+				+ "  ?s <uri:hangOutWith> ?t ." //
+				+ "  ?t <uri:hangOutWith> ?u ." //
+				+ "}";//
+
+		String q7 = ""//
+				+ "SELECT ?s ?t ?u " //
+				+ "{" //
+				+ "  ?s <uri:associatesWith> ?t ." //
+				+ "  ?t <uri:associatesWith> ?u ." //
+				+ "  ?t <uri:hangOutWith> ?u ." //
+				+ "}";//
+
+		String q8 = ""//
+				+ "SELECT ?t ?s ?u " //
+				+ "{" //
+				+ "  ?s a ?t ."//
+				+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+				+ "  ?u <uri:talksTo> ?s . "//
+				+ "  ?s <uri:associatesWith> ?t ." //
+				+ "}";//
+
+		String q9 = ""//
+				+ "SELECT ?t ?s ?u " //
+				+ "{" //
+				+ "  ?s a ?t ."//
+				+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+				+ "}";//
+
+		SPARQLParser parser = new SPARQLParser();
+		ParsedQuery pq1 = null;
+		ParsedQuery pq2 = null;
+		ParsedQuery pq3 = null;
+		ParsedQuery pq4 = null;
+		ParsedQuery pq5 = null;
+		ParsedQuery pq6 = null;
+		ParsedQuery pq7 = null;
+		ParsedQuery pq8 = null;
+		ParsedQuery pq9 = null;
+
+		SimpleExternalTupleSet extTup1 = null;
+		SimpleExternalTupleSet extTup2 = null;
+		SimpleExternalTupleSet extTup3 = null;
+		SimpleExternalTupleSet extTup4 = null;
+		SimpleExternalTupleSet extTup5 = null;
+		SimpleExternalTupleSet extTup6 = null;
+		SimpleExternalTupleSet extTup7 = null;
+		SimpleExternalTupleSet extTup8 = null;
+
+		try {
+			pq1 = parser.parseQuery(q1, null);
+			pq2 = parser.parseQuery(q2, null);
+			pq3 = parser.parseQuery(q3, null);
+			pq4 = parser.parseQuery(q4, null);
+			pq5 = parser.parseQuery(q5, null);
+			pq6 = parser.parseQuery(q6, null);
+			pq7 = parser.parseQuery(q7, null);
+			pq8 = parser.parseQuery(q8, null);
+			pq9 = parser.parseQuery(q9, null);
+
+			extTup1 = new SimpleExternalTupleSet(
+					(Projection) pq2.getTupleExpr());
+			extTup2 = new SimpleExternalTupleSet(
+					(Projection) pq3.getTupleExpr());
+			extTup3 = new SimpleExternalTupleSet(
+					(Projection) pq4.getTupleExpr());
+			extTup4 = new SimpleExternalTupleSet(
+					(Projection) pq5.getTupleExpr());
+			extTup5 = new SimpleExternalTupleSet(
+					(Projection) pq6.getTupleExpr());
+			extTup6 = new SimpleExternalTupleSet(
+					(Projection) pq7.getTupleExpr());
+			extTup7 = new SimpleExternalTupleSet(
+					(Projection) pq8.getTupleExpr());
+			extTup8 = new SimpleExternalTupleSet(
+					(Projection) pq9.getTupleExpr());
+
+		} catch (MalformedQueryException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+
+		List<ExternalTupleSet> indexList = Lists.newArrayList();
+		indexList.add(extTup1);
+		indexList.add(extTup2);
+		indexList.add(extTup3);
+		indexList.add(extTup4);
+		indexList.add(extTup5);
+		indexList.add(extTup6);
+		indexList.add(extTup7);
+		indexList.add(extTup8);
+
+		ValidIndexCombinationGenerator vic = new ValidIndexCombinationGenerator(
+				pq1.getTupleExpr());
+		Iterator<List<ExternalTupleSet>> combos = vic
+				.getValidIndexCombos(indexList);
+		int size = 0;
+		while (combos.hasNext()) {
+			combos.hasNext();
+			size++;
+			combos.next();
+			combos.hasNext();
+		}
+
+		Assert.assertTrue(!combos.hasNext());
+		Assert.assertEquals(21, size);
+
+	}
+
+	@Test
+	public void largeQuerySixteenIndexTest() {
+
+		String q1 = ""//
+				+ "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r " //
+				+ "{" //
+				+ "  ?f a ?m ."//
+				+ "  ?e a ?l ."//
+				+ "  ?n a ?o ."//
+				+ "  ?a a ?h ."//
+				+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+				+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
+				+ "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
+				+ "  ?d <uri:talksTo> ?f . "//
+				+ "  ?c <uri:talksTo> ?e . "//
+				+ "  ?p <uri:talksTo> ?n . "//
+				+ "  ?r <uri:talksTo> ?a . "//
+				+ "}";//
+
+		String q2 = ""//
+				+ "SELECT ?s ?t ?u " //
+				+ "{" //
+				+ "  ?s a ?t ."//
+				+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+				+ "  ?u <uri:talksTo> ?s . "//
+				+ "}";//
+
+		String q3 = ""//
+				+ "SELECT  ?s ?t ?u ?d ?f ?g " //
+				+ "{" //
+				+ "  ?s a ?t ."//
+				+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+				+ "  ?u <uri:talksTo> ?s . "//
+				+ "  ?d a ?f ."//
+				+ "  ?f <http://www.w3.org/2000/01/rdf-schema#label> ?g ."//
+				+ "  ?g <uri:talksTo> ?d . "//
+				+ "}";//
+
+		SPARQLParser parser = new SPARQLParser();
+		ParsedQuery pq1 = null;
+		ParsedQuery pq2 = null;
+		ParsedQuery pq3 = null;
+		try {
+			pq1 = parser.parseQuery(q1, null);
+			pq2 = parser.parseQuery(q2, null);
+			pq3 = parser.parseQuery(q3, null);
+
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				(Projection) pq3.getTupleExpr());
+
+		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+
+		list.add(extTup2);
+		list.add(extTup1);
+
+		IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
+				pq1.getTupleExpr(), list);
+		List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
+
+		Assert.assertEquals(16, indexSet.size());
+
+		ValidIndexCombinationGenerator vic = new ValidIndexCombinationGenerator(
+				pq1.getTupleExpr());
+		Iterator<List<ExternalTupleSet>> eSet = vic.getValidIndexCombos(Lists
+				.newArrayList(indexSet));
+
+		int size = 0;
+		while (eSet.hasNext()) {
+			size++;
+			Assert.assertTrue(eSet.hasNext());
+			eSet.next();
+		}
+
+		Assert.assertTrue(!eSet.hasNext());
+		Assert.assertEquals(75, size);
+
+	}
+
+	@Test
+	public void largeQueryFourtyIndexTest() {
+
+		String q1 = ""//
+				+ "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r " //
+				+ "{" //
+				+ "  ?f a ?m ."//
+				+ "  ?e a ?l ."//
+				+ "  ?n a ?o ."//
+				+ "  ?a a ?h ."//
+				+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+				+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+				+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
+				+ "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
+				+ "  ?d <uri:talksTo> ?f . "//
+				+ "  ?c <uri:talksTo> ?e . "//
+				+ "  ?p <uri:talksTo> ?n . "//
+				+ "  ?r <uri:talksTo> ?a . "//
+				+ "}";//
+
+		String q2 = ""//
+				+ "SELECT ?s ?t ?u " //
+				+ "{" //
+				+ "  ?s a ?t ."//
+				+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+				+ "  ?u <uri:talksTo> ?s . "//
+				+ "}";//
+
+		String q3 = ""//
+				+ "SELECT  ?s ?t ?u ?d ?f ?g " //
+				+ "{" //
+				+ "  ?s a ?t ."//
+				+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+				+ "  ?u <uri:talksTo> ?s . "//
+				+ "  ?d a ?f ."//
+				+ "  ?f <http://www.w3.org/2000/01/rdf-schema#label> ?g ."//
+				+ "  ?g <uri:talksTo> ?d . "//
+				+ "}";//
+
+		String q4 = ""//
+				+ "SELECT  ?s ?t ?u ?d ?f ?g ?a ?b ?c" //
+				+ "{" //
+				+ "  ?s a ?t ."//
+				+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+				+ "  ?u <uri:talksTo> ?s . "//
+				+ "  ?d a ?f ."//
+				+ "  ?f <http://www.w3.org/2000/01/rdf-schema#label> ?g ."//
+				+ "  ?g <uri:talksTo> ?d . "//
+				+ "  ?a a ?b ."//
+				+ "  ?b <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+				+ "  ?c <uri:talksTo> ?a . "//
+				+ "}";//
+
+		SPARQLParser parser = new SPARQLParser();
+
+		ParsedQuery pq1 = null;
+		ParsedQuery pq2 = null;
+		ParsedQuery pq3 = null;
+		ParsedQuery pq4 = null;
+
+		try {
+			pq1 = parser.parseQuery(q1, null);
+			pq2 = parser.parseQuery(q2, null);
+			pq3 = parser.parseQuery(q3, null);
+			pq4 = parser.parseQuery(q4, null);
+
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+
+		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				(Projection) pq3.getTupleExpr());
+		SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(
+				(Projection) pq4.getTupleExpr());
+
+		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+
+		list.add(extTup2);
+		list.add(extTup1);
+		list.add(extTup3);
+
+		IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
+				pq1.getTupleExpr(), list);
+		List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
+		Assert.assertEquals(40, indexSet.size());
+
+		ValidIndexCombinationGenerator vic = new ValidIndexCombinationGenerator(
+				pq1.getTupleExpr());
+		Iterator<List<ExternalTupleSet>> eSet = vic.getValidIndexCombos(Lists
+				.newArrayList(indexSet));
+
+		int size = 0;
+		while (eSet.hasNext()) {
+			size++;
+			Assert.assertTrue(eSet.hasNext());
+			eSet.next();
+		}
+
+		Assert.assertTrue(!eSet.hasNext());
+		Assert.assertEquals(123, size);
+	}
+
+	@Test
+	public void overlappingFilterIndex() {
+
+		String q5 = ""//
+				+ "SELECT ?s ?t " //
+				+ "{" //
+				+ "  ?s a \"Person\" ." //
+				+ "  ?t a \"Student\" ."//
+				+ "}";//
+
+		String q4 = ""//
+				+ "SELECT ?s ?t " //
+				+ "{" //
+				+ "  ?s a ?t ."//
+				+ "  ?s <uri:talksTo> ?t . "//
+				+ "}";//
+
+		String q3 = ""//
+				+ "SELECT ?s ?t  " //
+				+ "{" //
+				+ "  Filter(?s > 5). "//
+				+ "  ?s a ?t ."//
+				+ "  ?s <uri:talksTo> ?t . "//
+				+ "}";//
+
+		String q2 = ""//
+				+ "SELECT ?s ?t  " //
+				+ "{" //
+				+ "  Filter(?s > 5). "//
+				+ "  ?s a \"Person\" ." //
+				+ "  ?t a \"Student\" ."//
+				+ "}";//
+
+		String q1 = ""//
+				+ "SELECT  ?s ?t  " //
+				+ "{" //
+				+ "  Filter(?s > 5). "//
+				+ "  ?s a ?t ."//
+				+ "  ?s <uri:talksTo> ?t . "//
+				+ "  ?s a \"Person\" ." //
+				+ "  ?t a \"Student\" ."//
+				+ "}";//
+
+		SPARQLParser parser = new SPARQLParser();
+
+		ParsedQuery pq1 = null;
+		ParsedQuery pq2 = null;
+		ParsedQuery pq3 = null;
+		ParsedQuery pq4 = null;
+		ParsedQuery pq5 = null;
+
+		try {
+			pq1 = parser.parseQuery(q1, null);
+			pq2 = parser.parseQuery(q2, null);
+			pq3 = parser.parseQuery(q3, null);
+			pq4 = parser.parseQuery(q4, null);
+			pq5 = parser.parseQuery(q5, null);
+
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+
+		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				(Projection) pq3.getTupleExpr());
+		SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(
+				(Projection) pq4.getTupleExpr());
+		SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet(
+				(Projection) pq5.getTupleExpr());
+
+		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+
+		list.add(extTup2);
+		list.add(extTup1);
+		list.add(extTup3);
+		list.add(extTup4);
+
+		IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
+				pq1.getTupleExpr(), list);
+		List<ExternalTupleSet> indexSet = iep.getNormalizedIndices();
+
+		Assert.assertEquals(4, indexSet.size());
+
+		ValidIndexCombinationGenerator vic = new ValidIndexCombinationGenerator(
+				pq1.getTupleExpr());
+		Iterator<List<ExternalTupleSet>> eSet = vic.getValidIndexCombos(Lists
+				.newArrayList(indexSet));
+
+		int size = 0;
+		while (eSet.hasNext()) {
+			size++;
+			Assert.assertTrue(eSet.hasNext());
+			List<ExternalTupleSet> eList = eSet.next();
+
+		}
+
+		Assert.assertTrue(!eSet.hasNext());
+		Assert.assertEquals(7, size);
+
+	}
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/VarConstantIndexListPrunerTest.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/VarConstantIndexListPrunerTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/VarConstantIndexListPrunerTest.java
index 181d4fb..f867052 100644
--- a/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/VarConstantIndexListPrunerTest.java
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/IndexPlanValidator/VarConstantIndexListPrunerTest.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.IndexPlanValidator;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -19,25 +19,16 @@ package mvm.rya.indexing.IndexPlanValidator;
  * under the License.
  */
 
-
-import static org.junit.Assert.*;
-
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
 
-import mvm.rya.indexing.external.ExternalProcessor;
 import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
 import mvm.rya.indexing.external.tupleSet.SimpleExternalTupleSet;
-import mvm.rya.indexing.external.tupleSet.ExternalProcessorTest.ExternalTupleVstor;
 
 import org.junit.Assert;
 import org.junit.Test;
 import org.openrdf.query.algebra.Projection;
-import org.openrdf.query.algebra.QueryModelNode;
-import org.openrdf.query.algebra.StatementPattern;
-import org.openrdf.query.algebra.TupleExpr;
-import org.openrdf.query.algebra.helpers.StatementPatternCollector;
 import org.openrdf.query.parser.ParsedQuery;
 import org.openrdf.query.parser.sparql.SPARQLParser;
 
@@ -45,285 +36,261 @@ import com.google.common.collect.Sets;
 
 public class VarConstantIndexListPrunerTest {
 
-    
-    
-    private String q7 = ""//
-            + "SELECT ?s ?t ?u " //
-            + "{" //
-            + "  ?s a ?t ."//
-            + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-            + "  ?u <uri:talksTo> ?s . "//
-            + "}";//
-    
-    
-    private String q8 = ""//
-            + "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r " //
-            + "{" //
-            + "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
-            + "  ?f a ?m ."//
-            + "  ?p <uri:talksTo> ?n . "//
-            + "  ?e a ?l ."//
-            + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
-            + "  ?d <uri:talksTo> ?f . "//
-            + "  ?c <uri:talksTo> ?e . "//
-            + "  ?n a ?o ."//
-            + "  ?a a ?h ."//
-            + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-            + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-            + "  ?r <uri:talksTo> ?a . "//
-            + "}";//
-    
-    
-    
-    
-    private String q11 = ""//
-            + "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r ?x ?y ?w ?t ?duck ?chicken ?pig ?rabbit " //
-            + "{" //
-            + "  ?w a ?t ."//
-            + "  ?x a ?y ."//
-            + "  ?duck a ?chicken ."//
-            + "  ?pig a ?rabbit ."//
-            + "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
-            + "  ?f a ?m ."//
-            + "  ?p <uri:talksTo> ?n . "//
-            + "  ?e a ?l ."//
-            + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
-            + "  ?d <uri:talksTo> ?f . "//
-            + "  ?c <uri:talksTo> ?e . "//
-            + "  ?n a ?o ."//
-            + "  ?a a ?h ."//
-            + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-            + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-            + "  ?r <uri:talksTo> ?a . "//
-            + "}";//
-    
-    
-    private String q12 = ""//
-            + "SELECT ?b ?p ?dog ?cat " //
-            + "{" //
-            + "  ?b a ?p ."//
-            + "  ?dog a ?cat. "//
-            + "}";//
-    
-    
-    
-    private String q13 = ""//
-            + "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r ?x ?y ?w ?t ?duck ?chicken ?pig ?rabbit ?dick ?jane ?betty " //
-            + "{" //
-            + "  ?w a ?t ."//
-            + "  ?x a ?y ."//
-            + "  ?duck a ?chicken ."//
-            + "  ?pig a ?rabbit ."//
-            + "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
-            + "  ?f a ?m ."//
-            + "  ?p <uri:talksTo> ?n . "//
-            + "  ?e a ?l ."//
-            + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
-            + "  ?d <uri:talksTo> ?f . "//
-            + "  ?c <uri:talksTo> ?e . "//
-            + "  ?n a ?o ."//
-            + "  ?a a ?h ."//
-            + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-            + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-            + "  ?r <uri:talksTo> ?a . "//
-            + "  ?dick <uri:talksTo> ?jane . "//
-            + "  ?jane <uri:talksTo> ?betty . "//
-            + "}";//
-    
-    private String q14 = ""//
-            + "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r ?x ?y ?w ?t ?duck ?chicken ?pig ?rabbit " //
-            + "{" //
-            + "  ?w a ?t ."//
-            + "  ?x a ?y ."//
-            + "  ?duck a ?chicken ."//
-            + "  ?pig a ?rabbit ."//
-            + "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
-            + "  ?f a ?m ."//
-            + "  ?p <uri:talksTo> ?n . "//
-            + "  ?e a ?l ."//
-            + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
-            + "  ?d <uri:talksTo> ?f . "//
-            + "  ?c <uri:talksTo> ?e . "//
-            + "  ?n a ?o ."//
-            + "  ?a a ?h ."//
-            + "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
-            + "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
-            + "  ?r <uri:talksTo> ?a . "//
-            + "  ?d <uri:talksTo> ?a . "//
-            + "}";//
-    
-    
-
-    private String q15 = ""//
-            + "SELECT ?s ?t ?u " //
-            + "{" //
-            + "  Filter(?s > 1)."//
-            + "  ?s a ?t ."//
-            + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-            + "  ?u <uri:talksTo> ?s . "//
-            + "}";//
-    
-    private String q16 = ""//
-            + "SELECT ?s ?t ?u " //
-            + "{" //
-            + "  Filter(?s > 2)."//
-            + "  ?s a ?t ."//
-            + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-            + "  ?u <uri:talksTo> ?s . "//
-            + "}";//
-    
-    private String q17 = ""//
-            + "SELECT ?s ?t ?u " //
-            + "{" //
-            + "  Filter(?t > 1)."//
-            + "  ?s a ?t ."//
-            + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-            + "  ?u <uri:talksTo> ?s . "//
-            + "}";//
-
-    
-    
-    @Test
-    public void testTwoIndexLargeQuery() throws Exception {
-
-        SPARQLParser parser = new SPARQLParser();
-        
-
-        ParsedQuery pq1 = parser.parseQuery(q11, null);
-        ParsedQuery pq2 = parser.parseQuery(q7, null);
-        ParsedQuery pq3 = parser.parseQuery(q12, null);
-        ParsedQuery pq4 = parser.parseQuery(q13, null);
-        ParsedQuery pq5 = parser.parseQuery(q8, null);
-        ParsedQuery pq6 = parser.parseQuery(q14, null);
-
-        System.out.println("Query is " + pq1.getTupleExpr());
-        
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection)pq3.getTupleExpr());
-        SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet((Projection)pq4.getTupleExpr());
-        SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet((Projection)pq5.getTupleExpr());
-        SimpleExternalTupleSet extTup5 = new SimpleExternalTupleSet((Projection)pq6.getTupleExpr());
-
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-        list.add(extTup1);
-        list.add(extTup2);
-        list.add(extTup3);
-        list.add(extTup4);
-        list.add(extTup5);
-
-        VarConstantIndexListPruner vci = new VarConstantIndexListPruner(pq1.getTupleExpr());
-        Set<ExternalTupleSet> processedIndexSet = vci.getRelevantIndices(list);
-        
-        System.out.println("Relevant indexes are: ");
-        for(ExternalTupleSet e: processedIndexSet) {
-            System.out.println(e);
-        }
-        
-        Set<ExternalTupleSet> indexSet = Sets.newHashSet();
-        indexSet.add(extTup1);
-        indexSet.add(extTup2);
-        indexSet.add(extTup4);
-        
-        Assert.assertTrue(Sets.intersection(indexSet, processedIndexSet).equals(processedIndexSet));
-        
-
-
-    }
-
-    
-    
-    
-    
-    @Test
-    public void testTwoIndexFilter1() throws Exception {
-
-        SPARQLParser parser = new SPARQLParser();
-        
-
-        ParsedQuery pq1 = parser.parseQuery(q15, null);
-        ParsedQuery pq2 = parser.parseQuery(q16, null);
-        ParsedQuery pq3 = parser.parseQuery(q17, null);
-       
-        System.out.println("Query is " + pq1.getTupleExpr());
-        
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-        SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet((Projection)pq3.getTupleExpr());
-       
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-        list.add(extTup1);
-        list.add(extTup2);
-        
-        VarConstantIndexListPruner vci = new VarConstantIndexListPruner(pq1.getTupleExpr());
-        Set<ExternalTupleSet> processedIndexSet = vci.getRelevantIndices(list);
-        
-        System.out.println("Relevant indexes are: ");
-        for(ExternalTupleSet e: processedIndexSet) {
-            System.out.println(e);
-        }
-        
-        Set<ExternalTupleSet> indexSet = Sets.newHashSet();
-        indexSet.add(extTup2);
-       
-        
-        Assert.assertTrue(Sets.intersection(indexSet, processedIndexSet).equals(processedIndexSet));
-        
-
-
-    }
-    
-    
-    
-    @Test
-    public void testTwoIndexFilter2() throws Exception {
-
-        
-        String q18 = ""//
-                + "SELECT ?s ?t ?u " //
-                + "{" //
-                + "  Filter(?s > 1 && ?t > 8)." //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "  ?u <uri:talksTo> ?s . "//
-                + "}";//
-        
-        
-        String q19 = ""//
-                + "SELECT ?s ?t ?u " //
-                + "{" //
-                + "  Filter(?s > 1)." //
-                + "  Filter(?t > 8)." //
-                + "  ?s a ?t ."//
-                + "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
-                + "  ?u <uri:talksTo> ?s . "//
-                + "}";//
-        
-        
-        
-        SPARQLParser parser = new SPARQLParser();
-        
-
-        ParsedQuery pq1 = parser.parseQuery(q18, null);
-        ParsedQuery pq2 = parser.parseQuery(q19, null);
-       
-        System.out.println("Query is " + pq1.getTupleExpr());
-        
-        SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet((Projection) pq2.getTupleExpr());
-        
-        List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
-        list.add(extTup1);
-        
-        VarConstantIndexListPruner vci = new VarConstantIndexListPruner(pq1.getTupleExpr());
-        Set<ExternalTupleSet> processedIndexSet = vci.getRelevantIndices(list);
-        
-        Assert.assertTrue(processedIndexSet.isEmpty());
-        
-
-
-    }
-    
-    
-    
-    
+	private String q7 = ""//
+			+ "SELECT ?s ?t ?u " //
+			+ "{" //
+			+ "  ?s a ?t ."//
+			+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+			+ "  ?u <uri:talksTo> ?s . "//
+			+ "}";//
+
+	private String q8 = ""//
+			+ "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r " //
+			+ "{" //
+			+ "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
+			+ "  ?f a ?m ."//
+			+ "  ?p <uri:talksTo> ?n . "//
+			+ "  ?e a ?l ."//
+			+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
+			+ "  ?d <uri:talksTo> ?f . "//
+			+ "  ?c <uri:talksTo> ?e . "//
+			+ "  ?n a ?o ."//
+			+ "  ?a a ?h ."//
+			+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+			+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+			+ "  ?r <uri:talksTo> ?a . "//
+			+ "}";//
+
+	private String q11 = ""//
+			+ "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r ?x ?y ?w ?t ?duck ?chicken ?pig ?rabbit " //
+			+ "{" //
+			+ "  ?w a ?t ."//
+			+ "  ?x a ?y ."//
+			+ "  ?duck a ?chicken ."//
+			+ "  ?pig a ?rabbit ."//
+			+ "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
+			+ "  ?f a ?m ."//
+			+ "  ?p <uri:talksTo> ?n . "//
+			+ "  ?e a ?l ."//
+			+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
+			+ "  ?d <uri:talksTo> ?f . "//
+			+ "  ?c <uri:talksTo> ?e . "//
+			+ "  ?n a ?o ."//
+			+ "  ?a a ?h ."//
+			+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+			+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+			+ "  ?r <uri:talksTo> ?a . "//
+			+ "}";//
+
+	private String q12 = ""//
+			+ "SELECT ?b ?p ?dog ?cat " //
+			+ "{" //
+			+ "  ?b a ?p ."//
+			+ "  ?dog a ?cat. "//
+			+ "}";//
+
+	private String q13 = ""//
+			+ "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r ?x ?y ?w ?t ?duck ?chicken ?pig ?rabbit ?dick ?jane ?betty " //
+			+ "{" //
+			+ "  ?w a ?t ."//
+			+ "  ?x a ?y ."//
+			+ "  ?duck a ?chicken ."//
+			+ "  ?pig a ?rabbit ."//
+			+ "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
+			+ "  ?f a ?m ."//
+			+ "  ?p <uri:talksTo> ?n . "//
+			+ "  ?e a ?l ."//
+			+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
+			+ "  ?d <uri:talksTo> ?f . "//
+			+ "  ?c <uri:talksTo> ?e . "//
+			+ "  ?n a ?o ."//
+			+ "  ?a a ?h ."//
+			+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+			+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+			+ "  ?r <uri:talksTo> ?a . "//
+			+ "  ?dick <uri:talksTo> ?jane . "//
+			+ "  ?jane <uri:talksTo> ?betty . "//
+			+ "}";//
+
+	private String q14 = ""//
+			+ "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r ?x ?y ?w ?t ?duck ?chicken ?pig ?rabbit " //
+			+ "{" //
+			+ "  ?w a ?t ."//
+			+ "  ?x a ?y ."//
+			+ "  ?duck a ?chicken ."//
+			+ "  ?pig a ?rabbit ."//
+			+ "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
+			+ "  ?f a ?m ."//
+			+ "  ?p <uri:talksTo> ?n . "//
+			+ "  ?e a ?l ."//
+			+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
+			+ "  ?d <uri:talksTo> ?f . "//
+			+ "  ?c <uri:talksTo> ?e . "//
+			+ "  ?n a ?o ."//
+			+ "  ?a a ?h ."//
+			+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+			+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+			+ "  ?r <uri:talksTo> ?a . "//
+			+ "  ?d <uri:talksTo> ?a . "//
+			+ "}";//
+
+	private String q15 = ""//
+			+ "SELECT ?s ?t ?u " //
+			+ "{" //
+			+ "  Filter(?s > 1)."//
+			+ "  ?s a ?t ."//
+			+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+			+ "  ?u <uri:talksTo> ?s . "//
+			+ "}";//
+
+	private String q16 = ""//
+			+ "SELECT ?s ?t ?u " //
+			+ "{" //
+			+ "  Filter(?s > 2)."//
+			+ "  ?s a ?t ."//
+			+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+			+ "  ?u <uri:talksTo> ?s . "//
+			+ "}";//
+
+	private String q17 = ""//
+			+ "SELECT ?s ?t ?u " //
+			+ "{" //
+			+ "  Filter(?t > 1)."//
+			+ "  ?s a ?t ."//
+			+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+			+ "  ?u <uri:talksTo> ?s . "//
+			+ "}";//
+
+	@Test
+	public void testTwoIndexLargeQuery() throws Exception {
+
+		SPARQLParser parser = new SPARQLParser();
+
+		ParsedQuery pq1 = parser.parseQuery(q11, null);
+		ParsedQuery pq2 = parser.parseQuery(q7, null);
+		ParsedQuery pq3 = parser.parseQuery(q12, null);
+		ParsedQuery pq4 = parser.parseQuery(q13, null);
+		ParsedQuery pq5 = parser.parseQuery(q8, null);
+		ParsedQuery pq6 = parser.parseQuery(q14, null);
+
+		System.out.println("Query is " + pq1.getTupleExpr());
+
+		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				(Projection) pq3.getTupleExpr());
+		SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(
+				(Projection) pq4.getTupleExpr());
+		SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet(
+				(Projection) pq5.getTupleExpr());
+		SimpleExternalTupleSet extTup5 = new SimpleExternalTupleSet(
+				(Projection) pq6.getTupleExpr());
+
+		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+		list.add(extTup1);
+		list.add(extTup2);
+		list.add(extTup3);
+		list.add(extTup4);
+		list.add(extTup5);
+
+		VarConstantIndexListPruner vci = new VarConstantIndexListPruner(
+				pq1.getTupleExpr());
+		List<ExternalTupleSet> processedIndexSet = vci.getRelevantIndices(list);
+
+		System.out.println("Relevant indexes are: ");
+		for (ExternalTupleSet e : processedIndexSet) {
+			System.out.println(e);
+		}
+
+		Set<ExternalTupleSet> indexSet = Sets.newHashSet();
+		indexSet.add(extTup1);
+		indexSet.add(extTup2);
+		indexSet.add(extTup4);
+
+		Assert.assertTrue(Sets.intersection(indexSet, Sets.<ExternalTupleSet> newHashSet(processedIndexSet))
+				.equals(Sets.<ExternalTupleSet> newHashSet(processedIndexSet)));
+
+	}
+
+	@Test
+	public void testTwoIndexFilter1() throws Exception {
+
+		SPARQLParser parser = new SPARQLParser();
+
+		ParsedQuery pq1 = parser.parseQuery(q15, null);
+		ParsedQuery pq2 = parser.parseQuery(q16, null);
+		ParsedQuery pq3 = parser.parseQuery(q17, null);
+
+		System.out.println("Query is " + pq1.getTupleExpr());
+
+		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+		SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				(Projection) pq3.getTupleExpr());
+
+		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+		list.add(extTup1);
+		list.add(extTup2);
+
+		VarConstantIndexListPruner vci = new VarConstantIndexListPruner(
+				pq1.getTupleExpr());
+		List<ExternalTupleSet> processedIndexSet = vci.getRelevantIndices(list);
+
+		System.out.println("Relevant indexes are: ");
+		for (ExternalTupleSet e : processedIndexSet) {
+			System.out.println(e);
+		}
+
+		Set<ExternalTupleSet> indexSet = Sets.newHashSet();
+		indexSet.add(extTup2);
+
+		Assert.assertTrue(Sets.intersection(indexSet,
+				Sets.<ExternalTupleSet> newHashSet(processedIndexSet)).equals(
+						Sets.<ExternalTupleSet> newHashSet(processedIndexSet)));
+
+	}
+
+	@Test
+	public void testTwoIndexFilter2() throws Exception {
+
+		String q18 = ""//
+				+ "SELECT ?s ?t ?u " //
+				+ "{" //
+				+ "  Filter(?s > 1 && ?t > 8)." //
+				+ "  ?s a ?t ."//
+				+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+				+ "  ?u <uri:talksTo> ?s . "//
+				+ "}";//
+
+		String q19 = ""//
+				+ "SELECT ?s ?t ?u " //
+				+ "{" //
+				+ "  Filter(?s > 1)." //
+				+ "  Filter(?t > 8)." //
+				+ "  ?s a ?t ."//
+				+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+				+ "  ?u <uri:talksTo> ?s . "//
+				+ "}";//
+
+		SPARQLParser parser = new SPARQLParser();
+
+		ParsedQuery pq1 = parser.parseQuery(q18, null);
+		ParsedQuery pq2 = parser.parseQuery(q19, null);
+
+		System.out.println("Query is " + pq1.getTupleExpr());
+
+		SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+
+		List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+		list.add(extTup1);
+
+		VarConstantIndexListPruner vci = new VarConstantIndexListPruner(
+				pq1.getTupleExpr());
+		List<ExternalTupleSet> processedIndexSet = vci.getRelevantIndices(list);
+
+		Assert.assertTrue(processedIndexSet.isEmpty());
 
+	}
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloConstantIndexSetTest.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloConstantIndexSetTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloConstantIndexSetTest.java
deleted file mode 100644
index 98acf39..0000000
--- a/extras/indexing/src/test/java/mvm/rya/indexing/external/AccumuloConstantIndexSetTest.java
+++ /dev/null
@@ -1,831 +0,0 @@
-package mvm.rya.indexing.external;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
-import java.util.Map.Entry;
-
-import junit.framework.Assert;
-import mvm.rya.indexing.IndexPlanValidator.IndexPlanValidator;
-import mvm.rya.indexing.external.tupleSet.AccumuloIndexSet;
-import mvm.rya.indexing.external.tupleSet.ExternalProcessorTest.ExternalTupleVstor;
-import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
-
-import org.apache.accumulo.core.client.AccumuloException;
-import org.apache.accumulo.core.client.AccumuloSecurityException;
-import org.apache.accumulo.core.client.Connector;
-import org.apache.accumulo.core.client.MutationsRejectedException;
-import org.apache.accumulo.core.client.Scanner;
-import org.apache.accumulo.core.client.TableExistsException;
-import org.apache.accumulo.core.client.TableNotFoundException;
-import org.apache.accumulo.core.client.mock.MockInstance;
-import org.apache.accumulo.core.data.Key;
-import org.apache.accumulo.core.data.Range;
-import org.apache.accumulo.core.data.Value;
-import org.apache.accumulo.core.security.Authorizations;
-import org.apache.hadoop.io.Text;
-import org.junit.Before;
-import org.junit.Test;
-import org.openrdf.model.URI;
-import org.openrdf.model.impl.LiteralImpl;
-import org.openrdf.model.impl.URIImpl;
-import org.openrdf.model.vocabulary.RDF;
-import org.openrdf.model.vocabulary.RDFS;
-import org.openrdf.query.BindingSet;
-import org.openrdf.query.MalformedQueryException;
-import org.openrdf.query.QueryEvaluationException;
-import org.openrdf.query.QueryLanguage;
-import org.openrdf.query.QueryResultHandlerException;
-import org.openrdf.query.TupleQueryResultHandler;
-import org.openrdf.query.TupleQueryResultHandlerException;
-import org.openrdf.query.algebra.TupleExpr;
-import org.openrdf.query.parser.ParsedQuery;
-import org.openrdf.query.parser.sparql.SPARQLParser;
-import org.openrdf.repository.RepositoryException;
-import org.openrdf.repository.sail.SailRepository;
-import org.openrdf.repository.sail.SailRepositoryConnection;
-import org.openrdf.sail.Sail;
-import org.openrdf.sail.SailException;
-import org.openrdf.sail.memory.MemoryStore;
-
-import com.beust.jcommander.internal.Sets;
-import com.google.common.collect.Lists;
-
-public class AccumuloConstantIndexSetTest {
-
-    
-    private SailRepositoryConnection conn;
-    private Connector accCon;
-    String tablename = "table";
-    Sail s;
-    URI obj, obj2, subclass, subclass2, talksTo;
-
-    @Before
-    public void init() throws RepositoryException, TupleQueryResultHandlerException, QueryEvaluationException,
-            MalformedQueryException, AccumuloException, AccumuloSecurityException, TableExistsException {
-
-        s = new MemoryStore();
-        SailRepository repo = new SailRepository(s);
-        repo.initialize();
-        conn = repo.getConnection();
-
-        URI sub = new URIImpl("uri:entity");
-        subclass = new URIImpl("uri:class");
-        obj = new URIImpl("uri:obj");
-        talksTo = new URIImpl("uri:talksTo");
-
-        conn.add(sub, RDF.TYPE, subclass);
-        conn.add(sub, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(sub, talksTo, obj);
-
-        URI sub2 = new URIImpl("uri:entity2");
-        subclass2 = new URIImpl("uri:class2");
-        obj2 = new URIImpl("uri:obj2");
-
-        conn.add(sub2, RDF.TYPE, subclass2);
-        conn.add(sub2, RDFS.LABEL, new LiteralImpl("label2"));
-        conn.add(sub2, talksTo, obj2);
-
-        accCon = new MockInstance().getConnector("root", "".getBytes());
-        accCon.tableOperations().create(tablename);
-
-    }
-    
-    
-    
-    
-    @Test
-    public void testEvaluateTwoIndexVarInstantiate1() {
-
-        URI superclass = new URIImpl("uri:superclass");
-        URI superclass2 = new URIImpl("uri:superclass2");
-
-        try {
-            conn.add(subclass, RDF.TYPE, superclass);
-            conn.add(subclass2, RDF.TYPE, superclass2);
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        } catch (RepositoryException e5) {
-            // TODO Auto-generated catch block
-            e5.printStackTrace();
-        }
-
-        try {
-            if (accCon.tableOperations().exists("table2")) {
-                accCon.tableOperations().delete("table2");
-            }
-            accCon.tableOperations().create("table2");
-        } catch (AccumuloException e4) {
-            // TODO Auto-generated catch block
-            e4.printStackTrace();
-        } catch (AccumuloSecurityException e4) {
-            // TODO Auto-generated catch block
-            e4.printStackTrace();
-        } catch (TableExistsException e4) {
-            // TODO Auto-generated catch block
-            e4.printStackTrace();
-        } catch (TableNotFoundException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-
-        try {
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        } catch (RepositoryException e3) {
-            // TODO Auto-generated catch block
-            e3.printStackTrace();
-        }
-
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?dog ?pig ?duck  " //
-                + "{" //
-                + "  ?pig a ?dog . "//
-                + "  ?pig <http://www.w3.org/2000/01/rdf-schema#label> ?duck "//
-                + "}";//
-
-        String indexSparqlString2 = ""//
-                + "SELECT ?o ?f ?e ?c ?l  " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        String queryString = ""//
-                + "SELECT ?c ?l ?f ?o " //
-                + "{" //
-                + "  <uri:entity> a ?c . "//
-                + "  <uri:entity> <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  <uri:entity> <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-     
-
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        AccumuloIndexSet ais1 = null;
-        AccumuloIndexSet ais2 = null;
-
-        try {
-            ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablename);
-            ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-        } catch (MalformedQueryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        } catch (SailException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        } catch (MutationsRejectedException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        } catch (TableNotFoundException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-
-      
-       
-        CountingResultHandler crh1 = new CountingResultHandler();
-        CountingResultHandler crh2 = new CountingResultHandler();
-
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh1);
-        } catch (TupleQueryResultHandlerException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        } catch (QueryEvaluationException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        } catch (MalformedQueryException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        } catch (RepositoryException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        }
-
-        ParsedQuery pq = null;
-        SPARQLParser sp = new SPARQLParser();
-        try {
-            pq = sp.parseQuery(queryString, null);
-        } catch (MalformedQueryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-
-        index.add(ais1);
-        index.add(ais2);
-        
-        ExternalProcessor processor = new ExternalProcessor(index);
-
-        Sail processingSail = new ExternalSail(s, processor);
-        SailRepository smartSailRepo = new SailRepository(processingSail);
-        try {
-            smartSailRepo.initialize();
-        } catch (RepositoryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-
-        
-        try {
-            smartSailRepo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        } catch (MalformedQueryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        } catch (RepositoryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-        
-       
-        Assert.assertEquals(crh1.getCount(), crh2.getCount());
-
-    }
-    
-    
-    
-    
-    
-  @Test
-    public void testEvaluateThreeIndexVarInstantiate() {
-
-        URI superclass = new URIImpl("uri:superclass");
-        URI superclass2 = new URIImpl("uri:superclass2");
-
-        URI sub = new URIImpl("uri:entity");
-        subclass = new URIImpl("uri:class");
-        obj = new URIImpl("uri:obj");
-        talksTo = new URIImpl("uri:talksTo");
-
-        URI howlsAt = new URIImpl("uri:howlsAt");
-        URI subType = new URIImpl("uri:subType");
-        
-
-        try {
-            conn.add(subclass, RDF.TYPE, superclass);
-            conn.add(subclass2, RDF.TYPE, superclass2);
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-            conn.add(sub, howlsAt, superclass);
-            conn.add(superclass, subType, obj);
-        } catch (RepositoryException e5) {
-            // TODO Auto-generated catch block
-            e5.printStackTrace();
-        }
-
-        try {
-            if (accCon.tableOperations().exists("table2")) {
-                accCon.tableOperations().delete("table2");
-            }
-            accCon.tableOperations().create("table2");
-
-            if (accCon.tableOperations().exists("table3")) {
-                accCon.tableOperations().delete("table3");
-            }
-            accCon.tableOperations().create("table3");
-        } catch (AccumuloException e4) {
-            // TODO Auto-generated catch block
-            e4.printStackTrace();
-        } catch (AccumuloSecurityException e4) {
-            // TODO Auto-generated catch block
-            e4.printStackTrace();
-        } catch (TableExistsException e4) {
-            // TODO Auto-generated catch block
-            e4.printStackTrace();
-        } catch (TableNotFoundException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-
-        try {
-            conn.add(obj, RDFS.LABEL, new LiteralImpl("label"));
-            conn.add(obj2, RDFS.LABEL, new LiteralImpl("label2"));
-        } catch (RepositoryException e3) {
-            // TODO Auto-generated catch block
-            e3.printStackTrace();
-        }
-
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?dog ?pig ?duck  " //
-                + "{" //
-                + "  ?pig a ?dog . "//
-                + "  ?pig <http://www.w3.org/2000/01/rdf-schema#label> ?duck "//
-                + "}";//
-
-        String indexSparqlString2 = ""//
-                + "SELECT ?o ?f ?e ?c ?l  " //
-                + "{" //
-                + "  ?e <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "}";//
-
-        String indexSparqlString3 = ""//
-                + "SELECT ?wolf ?sheep ?chicken  " //
-                + "{" //
-                + "  ?wolf <uri:howlsAt> ?sheep . "// 
-                + "  ?sheep <uri:subType> ?chicken. "//
-                + "}";//
-
-        String queryString = ""//
-                + "SELECT ?c ?l ?f ?o " //
-                + "{" //
-                + "  <uri:entity> a ?c . "//
-                + "  <uri:entity> <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  <uri:entity> <uri:talksTo> ?o . "//
-                + "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
-                + "  ?c a ?f . " //
-                + "  <uri:entity> <uri:howlsAt> ?f. "//
-                + "  ?f <uri:subType> <uri:obj>. "//
-                + "}";//
-
-
-        List<ExternalTupleSet> index = Lists.newArrayList();
-        AccumuloIndexSet ais1 = null;
-        AccumuloIndexSet ais2 = null;
-        AccumuloIndexSet ais3 = null;
-
-        try {
-            ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablename);
-            ais2 = new AccumuloIndexSet(indexSparqlString2, conn, accCon, "table2");
-            ais3 = new AccumuloIndexSet(indexSparqlString3, conn, accCon, "table3");
-        } catch (MalformedQueryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        } catch (SailException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        } catch (MutationsRejectedException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        } catch (TableNotFoundException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-
-        index.add(ais1);
-        index.add(ais3);
-        index.add(ais2);
-
-        CountingResultHandler crh1 = new CountingResultHandler();
-        CountingResultHandler crh2 = new CountingResultHandler();
-
-        try {
-            conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh1);
-        } catch (TupleQueryResultHandlerException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        } catch (QueryEvaluationException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        } catch (MalformedQueryException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        } catch (RepositoryException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        }
-
-        ExternalProcessor processor = new ExternalProcessor(index);
-
-        Sail processingSail = new ExternalSail(s, processor);
-        SailRepository smartSailRepo = new SailRepository(processingSail);
-        try {
-            smartSailRepo.initialize();
-        } catch (RepositoryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-
-        
-        try {
-            smartSailRepo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh2);
-        } catch (TupleQueryResultHandlerException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        } catch (MalformedQueryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        } catch (RepositoryException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-        
-       
-        
-        
-        
-       
-//         Scanner s = null;
-//        try {
-//            s = accCon.createScanner("table3", new Authorizations());
-//        } catch (TableNotFoundException e) {
-//            // TODO Auto-generated catch block
-//            e.printStackTrace();
-//        } 
-//         s.setRange(new Range());       
-//         Iterator<Entry<Key,Value>> i = s.iterator();
-//         
-//         while (i.hasNext()) {
-//             Entry<Key, Value> entry = i.next();
-//             Key k = entry.getKey();
-//             System.out.println(k);
-//
-//         } 
-        
-        
-        
-        
-        
-        
-        Assert.assertEquals(crh1.getCount(), crh2.getCount());
-
-        
-        
-        
-        
-
-    }
-    
-    
-    
-    
-    
-    
-  @Test
-  public void testEvaluateFilterInstantiate() {
-
-      URI e1 = new URIImpl("uri:e1");
-      URI e2 = new URIImpl("uri:e2");
-      URI e3 = new URIImpl("uri:e3");
-      URI f1 = new URIImpl("uri:f1");
-      URI f2 = new URIImpl("uri:f2");
-      URI f3 = new URIImpl("uri:f3");
-      URI g1 = new URIImpl("uri:g1");
-      URI g2 = new URIImpl("uri:g2");
-      URI g3 = new URIImpl("uri:g3");
-      
-
-      
-      try {
-          conn.add(e1, talksTo, f1);
-          conn.add(f1, talksTo, g1);
-          conn.add(g1, talksTo, e1);
-          conn.add(e2, talksTo, f2);
-          conn.add(f2, talksTo, g2);
-          conn.add(g2, talksTo, e2);
-          conn.add(e3, talksTo, f3);
-          conn.add(f3, talksTo, g3);
-          conn.add(g3, talksTo, e3);
-      } catch (RepositoryException e5) {
-          // TODO Auto-generated catch block
-          e5.printStackTrace();
-      }
-
-
-      String queryString = ""//
-              + "SELECT ?x ?y ?z " //
-              + "{" //
-              + "Filter(?x = <uri:e1>) . " //
-              + " ?x <uri:talksTo> ?y. " //
-              + " ?y <uri:talksTo> ?z. " //
-              + " ?z <uri:talksTo> <uri:e1>. " //
-              + "}";//
-      
-      
-      
-      String indexSparqlString = ""//
-              + "SELECT ?a ?b ?c ?d " //
-              + "{" //
-              + "Filter(?a = ?d) . " //
-              + " ?a <uri:talksTo> ?b. " //
-              + " ?b <uri:talksTo> ?c. " //
-              + " ?c <uri:talksTo> ?d. " //
-              + "}";//
-      
-
-
-      List<ExternalTupleSet> index = Lists.newArrayList();
-      AccumuloIndexSet ais1 = null;
-
-      try {
-          ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablename);
-      } catch (MalformedQueryException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      } catch (SailException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      } catch (QueryEvaluationException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      } catch (MutationsRejectedException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      } catch (TableNotFoundException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      }
-
-      index.add(ais1);
-   
-      CountingResultHandler crh1 = new CountingResultHandler();
-      CountingResultHandler crh2 = new CountingResultHandler();
-
-      try {
-          conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh1);
-      } catch (TupleQueryResultHandlerException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      } catch (QueryEvaluationException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      } catch (MalformedQueryException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      } catch (RepositoryException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      }
-
-      ExternalProcessor processor = new ExternalProcessor(index);
-
-      Sail processingSail = new ExternalSail(s, processor);
-      SailRepository smartSailRepo = new SailRepository(processingSail);
-      try {
-          smartSailRepo.initialize();
-      } catch (RepositoryException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      }
-
-      
-      try {
-          smartSailRepo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh2);
-      } catch (TupleQueryResultHandlerException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      } catch (QueryEvaluationException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      } catch (MalformedQueryException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      } catch (RepositoryException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      }
-      
-    
-      
-       
-      
-      
-      
-      
-      Assert.assertEquals(crh1.getCount(), crh2.getCount());
-
-      
-      
-      
-      
-
-  }
-  
-  
-  
-  
-  @Test
-  public void testEvaluateCompoundFilterInstantiate() {
-
-      URI e1 = new URIImpl("uri:e1");
-      URI f1 = new URIImpl("uri:f1");
-      
-      
-      try {
-          conn.add(e1, talksTo, e1);
-          conn.add(e1, talksTo, f1);
-          conn.add(f1, talksTo, e1);
-
-      } catch (RepositoryException e5) {
-          // TODO Auto-generated catch block
-          e5.printStackTrace();
-      }
-
-
-      String queryString = ""//
-              + "SELECT ?x ?y ?z " //
-              + "{" //
-              + "Filter(?x = <uri:e1> && ?y = <uri:e1>) . " //
-              + " ?x <uri:talksTo> ?y. " //
-              + " ?y <uri:talksTo> ?z. " //
-              + " ?z <uri:talksTo> <uri:e1>. " //
-              + "}";//
-      
-      
-      
-      String indexSparqlString = ""//
-              + "SELECT ?a ?b ?c ?d " //
-              + "{" //
-              + "Filter(?a = ?d && ?b = ?d) . " //
-              + " ?a <uri:talksTo> ?b. " //
-              + " ?b <uri:talksTo> ?c. " //
-              + " ?c <uri:talksTo> ?d. " //
-              + "}";//
-      
-
-
-      List<ExternalTupleSet> index = Lists.newArrayList();
-      AccumuloIndexSet ais1 = null;
-
-      try {
-          ais1 = new AccumuloIndexSet(indexSparqlString, conn, accCon, tablename);
-      } catch (MalformedQueryException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      } catch (SailException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      } catch (QueryEvaluationException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      } catch (MutationsRejectedException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      } catch (TableNotFoundException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      }
-
-      index.add(ais1);
-   
-      CountingResultHandler crh1 = new CountingResultHandler();
-      CountingResultHandler crh2 = new CountingResultHandler();
-
-      try {
-          conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh1);
-      } catch (TupleQueryResultHandlerException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      } catch (QueryEvaluationException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      } catch (MalformedQueryException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      } catch (RepositoryException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      }
-
-      ExternalProcessor processor = new ExternalProcessor(index);
-
-      Sail processingSail = new ExternalSail(s, processor);
-      SailRepository smartSailRepo = new SailRepository(processingSail);
-      try {
-          smartSailRepo.initialize();
-      } catch (RepositoryException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      }
-
-      
-      try {
-          smartSailRepo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh2);
-      } catch (TupleQueryResultHandlerException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      } catch (QueryEvaluationException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      } catch (MalformedQueryException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      } catch (RepositoryException e) {
-          // TODO Auto-generated catch block
-          e.printStackTrace();
-      }
-      
-//      System.out.println("Counts are " + crh1.getCount() + " and " + crh2.getCount());
-//      
-//      
-//    Scanner s = null;
-//   try {
-//       s = accCon.createScanner(tablename, new Authorizations());
-//   } catch (TableNotFoundException e) {
-//       // TODO Auto-generated catch block
-//       e.printStackTrace();
-//   } 
-//    s.setRange(new Range());       
-//    Iterator<Entry<Key,Value>> i = s.iterator();
-//    
-//    while (i.hasNext()) {
-//        Entry<Key, Value> entry = i.next();
-//        Key k = entry.getKey();
-//        System.out.println(k);
-//
-//    } 
-      
-      
-    Assert.assertEquals(2, crh1.getCount());
-    
-    Assert.assertEquals(crh1.getCount(), crh2.getCount());
-
-     
-      
-      
-
-  }
-  
-  
-  
-  
-  
-  
-  
-  
-  
-  
-    
-    
-    
-    
-
-    public static class CountingResultHandler implements TupleQueryResultHandler {
-        private int count = 0;
-
-        public int getCount() {
-            return count;
-        }
-
-        public void resetCount() {
-            this.count = 0;
-        }
-
-        @Override
-        public void startQueryResult(List<String> arg0) throws TupleQueryResultHandlerException {
-        }
-
-        @Override
-        public void handleSolution(BindingSet arg0) throws TupleQueryResultHandlerException {
-            count++;
-        }
-
-        @Override
-        public void endQueryResult() throws TupleQueryResultHandlerException {
-        }
-
-        @Override
-        public void handleBoolean(boolean arg0) throws QueryResultHandlerException {
-            // TODO Auto-generated method stub
-
-        }
-
-        @Override
-        public void handleLinks(List<String> arg0) throws QueryResultHandlerException {
-            // TODO Auto-generated method stub
-
-        }
-    }
-
-}


[13/16] incubator-rya git commit: RYA-32 Improve how metadata and values are written to Accumulo PCJ tables

Posted by mi...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/indexing/external/PrecompJoinOptimizer.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/external/PrecompJoinOptimizer.java b/extras/indexing/src/main/java/mvm/rya/indexing/external/PrecompJoinOptimizer.java
index 65a775f..eb0f042 100644
--- a/extras/indexing/src/main/java/mvm/rya/indexing/external/PrecompJoinOptimizer.java
+++ b/extras/indexing/src/main/java/mvm/rya/indexing/external/PrecompJoinOptimizer.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.external;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -19,13 +19,11 @@ package mvm.rya.indexing.external;
  * under the License.
  */
 
-
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
 import java.util.Set;
 
 import mvm.rya.api.RdfCloudTripleStoreConfiguration;
@@ -38,25 +36,22 @@ import mvm.rya.indexing.accumulo.ConfigUtils;
 import mvm.rya.indexing.external.QueryVariableNormalizer.VarCollector;
 import mvm.rya.indexing.external.tupleSet.AccumuloIndexSet;
 import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
+import mvm.rya.indexing.external.tupleSet.PcjTables;
+import mvm.rya.indexing.external.tupleSet.PcjTables.PcjException;
 import mvm.rya.rdftriplestore.inference.DoNotExpandSP;
 import mvm.rya.rdftriplestore.utils.FixedStatementPattern;
 
 import org.apache.accumulo.core.client.AccumuloException;
 import org.apache.accumulo.core.client.AccumuloSecurityException;
 import org.apache.accumulo.core.client.Connector;
-import org.apache.accumulo.core.client.Scanner;
 import org.apache.accumulo.core.client.TableNotFoundException;
-import org.apache.accumulo.core.data.Key;
-import org.apache.accumulo.core.data.Range;
-import org.apache.accumulo.core.data.Value;
-import org.apache.accumulo.core.security.Authorizations;
 import org.apache.hadoop.conf.Configurable;
 import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.io.Text;
 import org.openrdf.query.BindingSet;
 import org.openrdf.query.Dataset;
 import org.openrdf.query.MalformedQueryException;
 import org.openrdf.query.QueryEvaluationException;
+import org.openrdf.query.algebra.BinaryTupleOperator;
 import org.openrdf.query.algebra.BindingSetAssignment;
 import org.openrdf.query.algebra.Difference;
 import org.openrdf.query.algebra.Distinct;
@@ -86,688 +81,783 @@ import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 
-//optimizer which matches TupleExpressions associated with pre-computed queries
-//to sub-queries of a given query. Each matched sub-query is replaced by an indexing node
-//to delegate that portion of the query to the pre-computed query index
+/**
+ * {@link QueryOptimizer} which matches TupleExpressions associated with
+ * pre-computed queries to sub-queries of a given query. Each matched sub-query
+ * is replaced by an indexing node to delegate that portion of the query to the
+ * pre-computed query index.
+ * <p>
+ *
+ * A query can be broken up into "Join segments", which subsets of the query
+ * joined only by {@link Join} nodes. Any portions of the query that are
+ * attached by {@link BinaryTupleOperator} or {@link UnaryTupleOperator} nodes other
+ * than a Join node mark the beginning of a new Join segment. Pre-computed query
+ * indices, or {@link ExternalTupleset} objects, are compared against the query
+ * nodes in each of its Join segments and replace any nodes which match the
+ * nodes in the ExternalTupleSet's TupleExpr.
+ *
+ */
+
 public class PrecompJoinOptimizer implements QueryOptimizer, Configurable {
 
-    private List<ExternalTupleSet> indexSet;
-    private Configuration conf;
-    private boolean init = false;
-    
-    public PrecompJoinOptimizer() {
-    }
-    
-    public PrecompJoinOptimizer(Configuration conf) {
-        this.conf = conf;
-        try {
-            indexSet = getAccIndices(conf);
-            init = true;
-        } catch (MalformedQueryException e) {
-            e.printStackTrace();
-        } catch (SailException e) {
-            e.printStackTrace();
-        } catch (QueryEvaluationException e) {
-            e.printStackTrace();
-        } catch (TableNotFoundException e) {
-            e.printStackTrace();
-        } catch (AccumuloException e) {
-            e.printStackTrace();
-        } catch (AccumuloSecurityException e) {
-            e.printStackTrace();
-        }
-    }
-    
-    public PrecompJoinOptimizer(List<ExternalTupleSet> indices, boolean useOptimalPcj) {
-        this.indexSet = indices;
-        conf = new Configuration();
-        conf.setBoolean(ConfigUtils.USE_OPTIMAL_PCJ, useOptimalPcj);
-    }
-    
-    public void setConf(Configuration conf) {
-        this.conf = conf;
-        if (!init) {
-            try {
-                indexSet = getAccIndices(conf);
-                init = true;
-            } catch (MalformedQueryException e) {
-                e.printStackTrace();
-            } catch (SailException e) {
-                e.printStackTrace();
-            } catch (QueryEvaluationException e) {
-                e.printStackTrace();
-            } catch (TableNotFoundException e) {
-                e.printStackTrace();
-            } catch (AccumuloException e) {
-                e.printStackTrace();
-            } catch (AccumuloSecurityException e) {
-                e.printStackTrace();
-            }
-        }
-    }
-    
-    @Override
-    public Configuration getConf() {
-        return conf;
-    }
-    
-
-    @Override
-    public void optimize(TupleExpr tupleExpr, Dataset dataset, BindingSet bindings) {
-
-        IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(tupleExpr, indexSet);
-        JoinVisitor jv = new JoinVisitor();
-        
-        if (ConfigUtils.getUseOptimalPCJ(conf) && indexSet.size() > 0) {
-            
-            //get potential relevant index combinations
-            ValidIndexCombinationGenerator vic = new ValidIndexCombinationGenerator(tupleExpr);
-            Iterator<List<ExternalTupleSet>> iter = vic.getValidIndexCombos(iep.getNormalizedIndices());
-            TupleExpr bestTup = null;
-            TupleExpr tempTup = null;
-            double tempCost = 0;
-            double minCost = Double.MAX_VALUE;
-            
-            while (iter.hasNext()) {
-                //apply join visitor to place external index nodes in query
-                TupleExpr clone = tupleExpr.clone();
-                jv.setExternalTupList(iter.next());
-                jv.setSegmentFilters(new ArrayList<Filter>());
-                clone.visit(jv);
-                
-                //get all valid execution plans for given external index combination by considering all 
-                //permutations of nodes in TupleExpr
-                IndexPlanValidator ipv = new IndexPlanValidator(false);
-                Iterator<TupleExpr> validTups = ipv.getValidTuples(TupleReArranger.getTupleReOrderings(clone).iterator());
-                
-                //set valid plan according to a specified cost threshold, where cost depends on specified weights
-                //for number of external index nodes, common variables among joins in execution plan, and number of
-                //external products in execution plan
-                ThreshholdPlanSelector tps = new ThreshholdPlanSelector(tupleExpr);
-                tempTup = tps.getThreshholdQueryPlan(validTups, .4, .5, .2, .3);
-                
-                //choose best threshhold TupleExpr among all index node combinations
-                tempCost = tps.getCost(tempTup, .5, .2, .3);
-                if(tempCost < minCost ) {
-                    minCost = tempCost;
-                    bestTup = tempTup;
-                }    
-            }
-            if (bestTup != null) {
-                ((UnaryTupleOperator) tupleExpr).setArg(((UnaryTupleOperator) bestTup).getArg());
-            }
-            return;
-        } else {
-            if (indexSet.size() > 0) {
-                jv.setExternalTupList(iep.getNormalizedIndices());
-                tupleExpr.visit(jv);
-            }
-            return;
-        }
-    }
-
-    protected class JoinVisitor extends QueryModelVisitorBase<RuntimeException> {
-
-        private List<ExternalTupleSet> tupList;
-        private List<Filter> segmentFilters = Lists.newArrayList();
-        
-        public void setExternalTupList(List<ExternalTupleSet> tupList) {
-            this.tupList = tupList;
-        }
-        
-        public void setSegmentFilters(List<Filter> segmentFilters) {
-            this.segmentFilters = segmentFilters;
-        }
-        
-        @Override
-        public void meet(Join node) {
-
-            //get all filters with bindings in this segment
-            updateFilters(segmentFilters, true);
-            
-            try {
-                if (node.getLeftArg() instanceof FixedStatementPattern && node.getRightArg() instanceof DoNotExpandSP) {
-                    return;
-                }
-
-                //get nodes in this join segment
-                TupleExpr newJoin = null;
-                List<QueryModelNode> args = getJoinArgs(node, new ArrayList<QueryModelNode>(), false);
-                List<TupleExpr> joinArgs = Lists.newArrayList();
-                
-                for (QueryModelNode qNode : args) {
-                    assert (qNode instanceof TupleExpr);
-                    joinArgs.add((TupleExpr) qNode);
-                }
-                
-                //insert all matching ExternalTupleSets in tupList into this segment
-                joinArgs = matchExternalTupleSets(joinArgs, tupList);
-
-                //push down any filters that have bindings in lower segments
-                //and update the filters in this segment
-                updateFilters(segmentFilters, false);
-                
-                //form join from matching ExternalTupleSets, remaining nodes, and filters
-                //that can't be pushed down any further
-                newJoin = getNewJoin(joinArgs, getFilterChain(segmentFilters));
-
-                // Replace old join hierarchy
-                node.replaceWith(newJoin);
-
-                //visit remaining nodes to match ExternalTupleSets with nodes further down
-                for (TupleExpr te : joinArgs) {
-                    if (!(te instanceof StatementPattern) && !(te instanceof ExternalTupleSet)) {
-                        segmentFilters = Lists.newArrayList();
-                        te.visit(this);
-                    }
-                }
-
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-        }
-        
-        
-        @Override
-        public void meet(Filter node) {
-            segmentFilters.add(node);
-            node.getArg().visit(this);
-        }
-
-        //chain filters together and return front and back of chain
-        private List<TupleExpr> getFilterChain(List<Filter> filters) {
-            List<TupleExpr> filterTopBottom = Lists.newArrayList();
-            Filter filterChainTop = null;
-            Filter filterChainBottom = null;
-            
-            for (Filter filter: filters) {
-                if (filterChainTop == null) {
-                    filterChainTop = filter;
-                } else if (filterChainBottom == null) {
-                    filterChainBottom = filter;
-                    filterChainTop.setArg(filterChainBottom);
-                } else {
-                    filterChainBottom.setArg(filter);
-                    filterChainBottom = filter;
-                }
-            }
-            if(filterChainTop != null) {
-                filterTopBottom.add(filterChainTop);
-            }
-            if(filterChainBottom != null) {
-                filterTopBottom.add(filterChainBottom);
-            }
-            return filterTopBottom;
-        }
-        
-        //build newJoin node given remaining joinArgs and chain of filters
-        private TupleExpr getNewJoin(List<TupleExpr> args, List<TupleExpr> filterChain) {
-            TupleExpr newJoin;
-            List<TupleExpr> joinArgs = Lists.newArrayList(args);
-
-            if (joinArgs.size() > 1) {
-                if (filterChain.size() > 0) {
-                    TupleExpr finalJoinArg = joinArgs.remove(0);
-                    TupleExpr tempJoin;
-                    TupleExpr temp = filterChain.get(0);
-
-                    if (joinArgs.size() > 1) {
-                        tempJoin = new Join(joinArgs.remove(0), joinArgs.remove(0));
-                        for (TupleExpr te : joinArgs) {
-                            tempJoin = new Join(tempJoin, te);
-                        }
-                    } else {
-                        tempJoin = joinArgs.remove(0);
-                    }
-
-                    if (filterChain.size() == 1) {
-                        ((Filter) temp).setArg(tempJoin);
-                    } else {
-                        ((Filter) filterChain.get(1)).setArg(tempJoin);
-                    }
-                    newJoin = new Join(temp, finalJoinArg);
-                } else {
-                    newJoin = new Join(joinArgs.get(0), joinArgs.get(1));
-                    joinArgs.remove(0);
-                    joinArgs.remove(0);
-
-                    for (TupleExpr te : joinArgs) {
-                        newJoin = new Join(newJoin, te);
-                    }
-                }
-            } else if (joinArgs.size() == 1) {
-                if (filterChain.size() > 0) {
-                    newJoin = filterChain.get(0);
-                    if (filterChain.size() == 1) {
-                        ((Filter) newJoin).setArg(joinArgs.get(0));
-                    } else {
-                        ((Filter) filterChain.get(1)).setArg(joinArgs.get(0));
-                    }
-                } else {
-                    newJoin = joinArgs.get(0);
-                }
-            } else {
-                throw new IllegalStateException("JoinArgs size cannot be zero.");
-            }
-            return newJoin;
-        }
-
-      
-       private List<TupleExpr> matchExternalTupleSets(List<TupleExpr> joinArgs, List<ExternalTupleSet> tupList) {
-           
-           Set<QueryModelNode> argSet = Sets.newHashSet();
-           argSet.addAll(joinArgs);
-           
-           if(argSet.size() < joinArgs.size()) {
-               throw new IllegalArgumentException("Query has duplicate nodes in segment!");
-           }
-           
-           Set<QueryModelNode> firstJoinFilterCond = Sets.newHashSet();
-           
-           for(Filter filter: segmentFilters) {
-               firstJoinFilterCond.add(filter.getCondition());
-           }
-           
-           argSet.addAll(firstJoinFilterCond);
-             
-           //see if ExternalTupleSet nodes are a subset of joinArgs, and if so, replacing matching nodes
-           //with ExternalTupleSet
-            for (ExternalTupleSet tup : tupList) {
-                TupleExpr tupleArg = tup.getTupleExpr();
-                if (isTupleValid(tupleArg)) {
-                    List<QueryModelNode> tupJoinArgs = getJoinArgs(tupleArg, 
-                            new ArrayList<QueryModelNode>(), true);
-                    Set<QueryModelNode> tupJoinArgSet = Sets.newHashSet(tupJoinArgs);
-                    if(tupJoinArgSet.size() < tupJoinArgs.size()) {
-                        throw new IllegalArgumentException("ExternalTuple contains duplicate nodes!");
-                    }
-                    if (argSet.containsAll(tupJoinArgSet)) {
-                        argSet = Sets.newHashSet(Sets.difference(argSet, tupJoinArgSet));
-                        argSet.add((ExternalTupleSet) tup.clone());
-                    }
-                }
-            }
-          
-            //update segment filters by removing those use in ExternalTupleSet
-            Iterator<Filter> iter = segmentFilters.iterator();
-            
-            while(iter.hasNext()) {
-                Filter filt = iter.next();
-                if(!argSet.contains(filt.getCondition())) {
-                    filt.replaceWith(filt.getArg());
-                    iter.remove();
-                }    
-            }
-            
-            //update joinArgs
-            joinArgs = Lists.newArrayList();
-            for(QueryModelNode node: argSet) {
-                if(!(node instanceof ValueExpr)) {
-                    joinArgs.add((TupleExpr)node);
-                }
-            }
-           
-           return joinArgs;
-       }
-
-       
-        private void updateFilters(List<Filter> filters, boolean firstJoin) {
-
-            Iterator<Filter> iter = segmentFilters.iterator();
-
-            while (iter.hasNext()) {
-                if (!FilterRelocator.relocate(iter.next(), firstJoin)) {
-                    iter.remove();
-                }
-            }
-        }
-       
-        protected List<QueryModelNode> getJoinArgs(TupleExpr tupleExpr, List<QueryModelNode> joinArgs, boolean getFilters) {
-            if (tupleExpr instanceof Join) {
-                if (!(((Join) tupleExpr).getLeftArg() instanceof FixedStatementPattern)
-                        && !(((Join) tupleExpr).getRightArg() instanceof DoNotExpandSP)) {
-                    Join join = (Join) tupleExpr;
-                    getJoinArgs(join.getLeftArg(), joinArgs, getFilters);
-                    getJoinArgs(join.getRightArg(), joinArgs, getFilters);
-                } 
-            } else if(tupleExpr instanceof Filter) {
-                if (getFilters) {
-                    joinArgs.add(((Filter) tupleExpr).getCondition());
-                }
-                getJoinArgs(((Filter)tupleExpr).getArg(), joinArgs, getFilters);
-            } else if(tupleExpr instanceof Projection) {
-                getJoinArgs(((Projection)tupleExpr).getArg(), joinArgs, getFilters);
-            } else {
-                joinArgs.add(tupleExpr);
-            }
-
-            return joinArgs;
-        }
-    }
-    
-    protected static class FilterRelocator extends QueryModelVisitorBase<RuntimeException> {
-
-   
-        protected final Filter filter;
-
-        protected final Set<String> filterVars;
-        private boolean stopAtFirstJoin = false;
-        private boolean isFirstJoinFilter = false;
-        private boolean inSegment = true;
-        
-        
-        public FilterRelocator(Filter filter) {
-            this.filter = filter;
-            filterVars = VarNameCollector.process(filter.getCondition());
-        }
-        
-        public FilterRelocator(Filter filter, boolean stopAtFirstJoin) {
-            this.filter = filter;
-            filterVars = VarNameCollector.process(filter.getCondition());
-            this.stopAtFirstJoin = stopAtFirstJoin;
-        }
-        
-        public static boolean relocate(Filter filter) {
-            FilterRelocator fr = new FilterRelocator(filter);
-            filter.visit(fr);
-            return fr.inSegment;
-        }
-        
-        public static boolean relocate(Filter filter, boolean stopAtFirstJoin) {
-            if (stopAtFirstJoin) {
-                FilterRelocator fr = new FilterRelocator(filter, stopAtFirstJoin);
-                filter.visit(fr);
-                return fr.isFirstJoinFilter;
-            } else {
-                FilterRelocator fr = new FilterRelocator(filter);
-                filter.visit(fr);
-                return fr.inSegment;
-            }
-        }
-
-     
-        @Override
-        protected void meetNode(QueryModelNode node) {
-            // By default, do not traverse
-            assert node instanceof TupleExpr;
-            
-            if(node instanceof UnaryTupleOperator) {
-                if (((UnaryTupleOperator)node).getArg().getBindingNames().containsAll(filterVars)) {
-                    if (stopAtFirstJoin) {
-                        ((UnaryTupleOperator) node).getArg().visit(this);
-                    } else {
-                        inSegment = false;
-                        relocate(filter, ((UnaryTupleOperator) node).getArg());
-                    }
-                }
-            }
-            
-            relocate(filter, (TupleExpr) node);
-        }
-       
-
-        @Override
-        public void meet(Join join) {
-
-            if (stopAtFirstJoin) {
-                isFirstJoinFilter = true;
-                relocate(filter, join);
-            } else {
-
-                if (join.getLeftArg().getBindingNames().containsAll(filterVars)) {
-                    // All required vars are bound by the left expr
-                    join.getLeftArg().visit(this);
-                } else if (join.getRightArg().getBindingNames().containsAll(filterVars)) {
-                    // All required vars are bound by the right expr
-                    join.getRightArg().visit(this);
-                } else {
-                    relocate(filter, join);
-                }
-            }
-        }
-
-        @Override
-        public void meet(LeftJoin leftJoin) {
-            
-            if (leftJoin.getLeftArg().getBindingNames().containsAll(filterVars)) {
-                inSegment = false;
-                if (stopAtFirstJoin) {
-                    leftJoin.getLeftArg().visit(this);
-                } else {
-                    relocate(filter, leftJoin.getLeftArg());
-                }
-            }
-            else {
-                relocate(filter, leftJoin);
-            }
-        }
-
-        @Override
-        public void meet(Union union) {
-            Filter clone = new Filter();
-            clone.setCondition(filter.getCondition().clone());
-
-            relocate(filter, union.getLeftArg());
-            relocate(clone, union.getRightArg());
-            
-            inSegment = false;
-
-        }
-
-        @Override
-        public void meet(Difference node) {
-            Filter clone = new Filter();
-            clone.setCondition(filter.getCondition().clone());
-        
-            relocate(filter, node.getLeftArg());
-            relocate(clone, node.getRightArg());
-            
-            inSegment = false;
-        
-        }
-
-        @Override
-        public void meet(Intersection node) {
-            Filter clone = new Filter();
-            clone.setCondition(filter.getCondition().clone());
-        
-            relocate(filter, node.getLeftArg());
-            relocate(clone, node.getRightArg());
-            
-            inSegment = false;
-        
-        }
-
-        @Override
-        public void meet(Extension node) {
-            if (node.getArg().getBindingNames().containsAll(filterVars)) {
-                if (stopAtFirstJoin) {
-                    node.getArg().visit(this);
-                } else {
-                    relocate(filter, node.getArg());
-                    inSegment = false;
-                }
-            }
-            else {
-                relocate(filter, node);
-            }
-        }
-
-        @Override
-        public void meet(EmptySet node) {
-            if (filter.getParentNode() != null) {
-                // Remove filter from its original location
-                filter.replaceWith(filter.getArg());
-            }
-        }
-
-        @Override
-        public void meet(Filter filter) {
-            // Filters are commutative
-            filter.getArg().visit(this);
-        }
-
-        @Override
-        public void meet(Distinct node) {
-            node.getArg().visit(this);
-        }
-
-        @Override
-        public void meet(Order node) {
-            node.getArg().visit(this);
-        }
-
-        @Override
-        public void meet(QueryRoot node) {
-            node.getArg().visit(this);
-        }
-
-        @Override
-        public void meet(Reduced node) {
-            node.getArg().visit(this);
-        }
-
-        protected void relocate(Filter filter, TupleExpr newFilterArg) {
-            if (filter.getArg() != newFilterArg) {
-                if (filter.getParentNode() != null) {
-                    // Remove filter from its original location
-                    filter.replaceWith(filter.getArg());
-                }
-
-                // Insert filter at the new location
-                newFilterArg.replaceWith(filter);
-                filter.setArg(newFilterArg);
-            }
-        }
-    }
-    
-    
-    private static boolean isTupleValid(QueryModelNode node) {
-
-        ValidQueryVisitor vqv = new ValidQueryVisitor();
-        node.visit(vqv);
-
-        if (vqv.isValid() && vqv.getSPs().size() > 1) {   
-            if(vqv.getFilters().size() > 0) {
-                Set<String> spVars = getVarNames(vqv.getSPs());
-                Set<String> fVarNames = getVarNames(vqv.getFilters());
-                //check that all vars contained in filters also occur in SPs
-                return Sets.intersection(fVarNames,spVars).equals(fVarNames);
-            } else {
-                return true;
-            }
-        } else {
-            return false;
-        }
-    }
-    
-    
-    private static Set<String> getVarNames(Collection<QueryModelNode> nodes) {
-
-        List<String> tempVars;
-        Set<String> nodeVarNames = Sets.newHashSet();
-
-        for (QueryModelNode s : nodes) {
-            tempVars = VarCollector.process(s);
-            for (String t : tempVars)
-                nodeVarNames.add(t);
-        }
-        return nodeVarNames;
-    }
-    
-    
-    private static class ValidQueryVisitor extends QueryModelVisitorBase<RuntimeException> {
-
-        private boolean isValid = true;
-        private Set<QueryModelNode> filterSet = Sets.newHashSet();
-        private Set<QueryModelNode> spSet = Sets.newHashSet();
-        
-        public Set<QueryModelNode> getFilters() {
-            return filterSet;
-        }
-        
-        public Set<QueryModelNode> getSPs() {
-            return spSet;
-        }
-
-        public boolean isValid() {
-            return isValid;
-        }
-
-        public void meet(Projection node) {
-            node.getArg().visit(this);
-        }
-
-        @Override
-        public void meet(Filter node) {
-            filterSet.add(node.getCondition());
-            node.getArg().visit(this);
-        }
-        
-        @Override
-        public void meet(StatementPattern node) {
-            spSet.add(node);
-        }
-     
-        public void meetNode(QueryModelNode node) {
-
-            if (!((node instanceof Join) || (node instanceof StatementPattern) || (node instanceof BindingSetAssignment) || 
-                    (node instanceof Var) || (node instanceof Union) || (node instanceof LeftJoin))) {
-                isValid = false;
-                return;
-           
-            } else{
-                super.meetNode(node);
-            }
-        }
-
-    }
-    
-    
-    private static List<ExternalTupleSet> getAccIndices(Configuration conf) throws MalformedQueryException,
-            SailException, QueryEvaluationException, TableNotFoundException, AccumuloException,
-            AccumuloSecurityException {
-
-        List<String> tables = null;
-
-        if (conf instanceof RdfCloudTripleStoreConfiguration) {
-            tables = ((RdfCloudTripleStoreConfiguration) conf).getPcjTables();
-        }
-
-        String tablePrefix = conf.get(RdfCloudTripleStoreConfiguration.CONF_TBL_PREFIX);
-        Connector c = ConfigUtils.getConnector(conf);
-        Map<String, String> indexTables = Maps.newLinkedHashMap();
-
-        if (tables != null && !tables.isEmpty()) {
-            for (String table : tables) {
-                Scanner s = c.createScanner(table, new Authorizations());
-                s.setRange(Range.exact(new Text("~SPARQL")));
-                for (Entry<Key, Value> e : s) {
-                    indexTables.put(table, e.getValue().toString());
-                }
-            }
-        } else {
-            for (String table : c.tableOperations().list()) {
-                if (table.startsWith(tablePrefix + "INDEX")) {
-                    Scanner s = c.createScanner(table, new Authorizations());
-                    s.setRange(Range.exact(new Text("~SPARQL")));
-                    for (Entry<Key, Value> e : s) {
-                        indexTables.put(table, e.getValue().toString());
-                    }
-                }
-            }
-
-        }
-        List<ExternalTupleSet> index = Lists.newArrayList();
-
-        if (indexTables.isEmpty()) {
-            System.out.println("No Index found");
-        } else {
-            for (String table : indexTables.keySet()) {
-                String indexSparqlString = indexTables.get(table);
-                index.add(new AccumuloIndexSet(indexSparqlString, c, table));
-            }
-        }
-        return index;
-    }
+	private List<ExternalTupleSet> indexSet;
+	private Configuration conf;
+	private boolean init = false;
+
+	public PrecompJoinOptimizer() {
+	}
+
+	public PrecompJoinOptimizer(Configuration conf) {
+		this.conf = conf;
+		try {
+			indexSet = getAccIndices(conf);
+		} catch (MalformedQueryException | SailException
+				| QueryEvaluationException | TableNotFoundException
+				| AccumuloException | AccumuloSecurityException | PcjException e) {
+			e.printStackTrace();
+		}
+		init = true;
+	}
+
+	public PrecompJoinOptimizer(List<ExternalTupleSet> indices,
+			boolean useOptimalPcj) {
+		this.indexSet = indices;
+		conf = new Configuration();
+		conf.setBoolean(ConfigUtils.USE_OPTIMAL_PCJ, useOptimalPcj);
+	}
+
+	@Override
+	public void setConf(Configuration conf) {
+		this.conf = conf;
+		if (!init) {
+			try {
+				indexSet = getAccIndices(conf);
+			} catch (MalformedQueryException | SailException
+					| QueryEvaluationException | TableNotFoundException
+					| AccumuloException | AccumuloSecurityException
+					| PcjException e) {
+				e.printStackTrace();
+			}
+			init = true;
+		}
+	}
+
+	@Override
+	public Configuration getConf() {
+		return conf;
+	}
+
+	/**
+	 * @param tupleExpr
+	 *            -- query whose query plan will be optimized -- specified
+	 *            ExternalTupleSet nodes contained in will be placed in query
+	 *            plan where an ExternalTupleSet TupleExpr matches the query's
+	 *            sub-query
+	 */
+	@Override
+	public void optimize(TupleExpr tupleExpr, Dataset dataset,
+			BindingSet bindings) {
+
+		final IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator(
+				tupleExpr, indexSet);
+		final JoinVisitor jv = new JoinVisitor();
+
+		if (ConfigUtils.getUseOptimalPCJ(conf) && indexSet.size() > 0) {
+
+			// get potential relevant index combinations
+			final ValidIndexCombinationGenerator vic = new ValidIndexCombinationGenerator(
+					tupleExpr);
+			final Iterator<List<ExternalTupleSet>> iter = vic
+					.getValidIndexCombos(iep.getNormalizedIndices());
+			TupleExpr bestTup = null;
+			TupleExpr tempTup = null;
+			double tempCost = 0;
+			double minCost = Double.MAX_VALUE;
+
+			while (iter.hasNext()) {
+				// apply join visitor to place external index nodes in query
+				final TupleExpr clone = tupleExpr.clone();
+				jv.setExternalTupList(iter.next());
+				jv.setSegmentFilters(new ArrayList<Filter>());
+				clone.visit(jv);
+
+				// get all valid execution plans for given external index
+				// combination by considering all
+				// permutations of nodes in TupleExpr
+				final IndexPlanValidator ipv = new IndexPlanValidator(false);
+				final Iterator<TupleExpr> validTups = ipv
+						.getValidTuples(TupleReArranger.getTupleReOrderings(
+								clone).iterator());
+
+				// set valid plan according to a specified cost threshold, where
+				// cost depends on specified weights
+				// for number of external index nodes, common variables among
+				// joins in execution plan, and number of
+				// external products in execution plan
+				final ThreshholdPlanSelector tps = new ThreshholdPlanSelector(
+						tupleExpr);
+				tempTup = tps.getThreshholdQueryPlan(validTups, .4, .5, .2, .3);
+
+				// choose best threshhold TupleExpr among all index node
+				// combinations
+				tempCost = tps.getCost(tempTup, .5, .2, .3);
+				if (tempCost < minCost) {
+					minCost = tempCost;
+					bestTup = tempTup;
+				}
+			}
+			if (bestTup != null) {
+				((UnaryTupleOperator) tupleExpr)
+						.setArg(((UnaryTupleOperator) bestTup).getArg());
+			}
+			return;
+		} else {
+			if (indexSet.size() > 0) {
+				jv.setExternalTupList(iep.getNormalizedIndices());
+				tupleExpr.visit(jv);
+			}
+			return;
+		}
+	}
+
+	/**
+	 * Given a list of @ ExternalTuleSet} , this visitor navigates the query
+	 * {@link TupleExpr} specified in the
+	 * {@link PrecompJoinOptimizer#optimize(TupleExpr, Dataset, BindingSet) and
+	 * matches the TupleExpr in the ExternalTupleSet with sub-queries of the
+	 * query and replaces the sub-query with the ExternalTupleSet node.
+	 *
+	 */
+	protected class JoinVisitor extends QueryModelVisitorBase<RuntimeException> {
+
+		private List<ExternalTupleSet> tupList;
+		private List<Filter> segmentFilters = Lists.newArrayList();
+
+		public void setExternalTupList(List<ExternalTupleSet> tupList) {
+			this.tupList = tupList;
+		}
+
+		public void setSegmentFilters(List<Filter> segmentFilters) {
+			this.segmentFilters = segmentFilters;
+		}
+
+		@Override
+		public void meet(Join node) {
+
+			// get all filters with bindings in this segment
+			updateFilters(segmentFilters, true);
+
+			try {
+				if (node.getLeftArg() instanceof FixedStatementPattern
+						&& node.getRightArg() instanceof DoNotExpandSP) {
+					return;
+				}
+
+				// get nodes in this join segment
+				TupleExpr newJoin = null;
+				final List<QueryModelNode> args = getJoinArgs(node,
+						new ArrayList<QueryModelNode>(), false);
+				List<TupleExpr> joinArgs = Lists.newArrayList();
+
+				for (final QueryModelNode qNode : args) {
+					assert qNode instanceof TupleExpr;
+					joinArgs.add((TupleExpr) qNode);
+				}
+
+				// insert all matching ExternalTupleSets in tupList into this
+				// segment
+				joinArgs = matchExternalTupleSets(joinArgs, tupList);
+
+				// push down any filters that have bindings in lower segments
+				// and update the filters in this segment
+				updateFilters(segmentFilters, false);
+
+				// form join from matching ExternalTupleSets, remaining nodes,
+				// and filters
+				// that can't be pushed down any further
+				newJoin = getNewJoin(joinArgs, getFilterChain(segmentFilters));
+
+				// Replace old join hierarchy
+				node.replaceWith(newJoin);
+
+				// visit remaining nodes to match ExternalTupleSets with nodes
+				// further down
+				for (final TupleExpr te : joinArgs) {
+					if (!(te instanceof StatementPattern)
+							&& !(te instanceof ExternalTupleSet)) {
+						segmentFilters = Lists.newArrayList();
+						te.visit(this);
+					}
+				}
+
+			} catch (final Exception e) {
+				e.printStackTrace();
+			}
+		}
+
+		@Override
+		public void meet(Filter node) {
+			segmentFilters.add(node);
+			node.getArg().visit(this);
+		}
+
+		// chain filters together and return front and back of chain
+		private List<TupleExpr> getFilterChain(List<Filter> filters) {
+			final List<TupleExpr> filterTopBottom = Lists.newArrayList();
+			Filter filterChainTop = null;
+			Filter filterChainBottom = null;
+
+			for (final Filter filter : filters) {
+				filter.replaceWith(filter.getArg());
+				if (filterChainTop == null) {
+					filterChainTop = filter;
+				} else if (filterChainBottom == null) {
+					filterChainBottom = filter;
+					filterChainTop.setArg(filterChainBottom);
+				} else {
+					filterChainBottom.setArg(filter);
+					filterChainBottom = filter;
+				}
+			}
+			if (filterChainTop != null) {
+				filterTopBottom.add(filterChainTop);
+			}
+			if (filterChainBottom != null) {
+				filterTopBottom.add(filterChainBottom);
+			}
+			return filterTopBottom;
+		}
+
+		// build newJoin node given remaining joinArgs and chain of filters
+		private TupleExpr getNewJoin(List<TupleExpr> args,
+				List<TupleExpr> filterChain) {
+			TupleExpr newJoin;
+			final List<TupleExpr> joinArgs = Lists.newArrayList(args);
+
+			if (joinArgs.size() > 1) {
+				if (filterChain.size() > 0) {
+					final TupleExpr finalJoinArg = joinArgs.remove(0);
+					TupleExpr tempJoin;
+					final TupleExpr temp = filterChain.get(0);
+
+					if (joinArgs.size() > 1) {
+						tempJoin = new Join(joinArgs.remove(0),
+								joinArgs.remove(0));
+						for (final TupleExpr te : joinArgs) {
+							tempJoin = new Join(tempJoin, te);
+						}
+					} else {
+						tempJoin = joinArgs.remove(0);
+					}
+
+					if (filterChain.size() == 1) {
+						((Filter) temp).setArg(tempJoin);
+					} else {
+						((Filter) filterChain.get(1)).setArg(tempJoin);
+					}
+					newJoin = new Join(temp, finalJoinArg);
+				} else {
+					newJoin = new Join(joinArgs.get(0), joinArgs.get(1));
+					joinArgs.remove(0);
+					joinArgs.remove(0);
+
+					for (final TupleExpr te : joinArgs) {
+						newJoin = new Join(newJoin, te);
+					}
+				}
+			} else if (joinArgs.size() == 1) {
+				if (filterChain.size() > 0) {
+					newJoin = filterChain.get(0);
+					if (filterChain.size() == 1) {
+						((Filter) newJoin).setArg(joinArgs.get(0));
+					} else {
+						((Filter) filterChain.get(1)).setArg(joinArgs.get(0));
+					}
+				} else {
+					newJoin = joinArgs.get(0);
+				}
+			} else {
+				throw new IllegalStateException("JoinArgs size cannot be zero.");
+			}
+			return newJoin;
+		}
+
+		/**
+		 *
+		 * @param joinArgs
+		 *            -- list of non-join nodes contained in the join segment
+		 * @param tupList
+		 *            -- list of indices to match sub-queries in this join
+		 *            segment
+		 * @return updated list of non-join nodes, where any nodes matching an
+		 *         index are replaced by that index
+		 */
+		private List<TupleExpr> matchExternalTupleSets(
+				List<TupleExpr> joinArgs, List<ExternalTupleSet> tupList) {
+
+			List<TupleExpr> bsaList = new ArrayList<>();
+			Set<QueryModelNode> argSet = Sets.newHashSet();
+			for (TupleExpr te : joinArgs) {
+				if (te instanceof BindingSetAssignment) {
+					bsaList.add(te);
+				} else {
+					argSet.add(te);
+				}
+			}
+
+			if (argSet.size() + bsaList.size() < joinArgs.size()) {
+				throw new IllegalArgumentException(
+						"Query has duplicate nodes in segment!");
+			}
+
+			final Set<QueryModelNode> firstJoinFilterCond = Sets.newHashSet();
+
+			for (final Filter filter : segmentFilters) {
+				firstJoinFilterCond.add(filter.getCondition());
+			}
+
+			argSet.addAll(firstJoinFilterCond);
+
+			// see if ExternalTupleSet nodes are a subset of joinArgs, and if
+			// so, replacing matching nodes
+			// with ExternalTupleSet
+			for (final ExternalTupleSet tup : tupList) {
+				final TupleExpr tupleArg = tup.getTupleExpr();
+				if (isTupleValid(tupleArg)) {
+					final List<QueryModelNode> tupJoinArgs = getJoinArgs(
+							tupleArg, new ArrayList<QueryModelNode>(), true);
+					final Set<QueryModelNode> tupJoinArgSet = Sets
+							.newHashSet(tupJoinArgs);
+					if (tupJoinArgSet.size() < tupJoinArgs.size()) {
+						throw new IllegalArgumentException(
+								"ExternalTuple contains duplicate nodes!");
+					}
+					if (argSet.containsAll(tupJoinArgSet)) {
+						argSet = Sets.newHashSet(Sets.difference(argSet,
+								tupJoinArgSet));
+						argSet.add(tup.clone());
+					}
+				}
+			}
+
+			// update segment filters by removing those use in ExternalTupleSet
+			final Iterator<Filter> iter = segmentFilters.iterator();
+
+			while (iter.hasNext()) {
+				final Filter filt = iter.next();
+				if (!argSet.contains(filt.getCondition())) {
+					filt.replaceWith(filt.getArg());
+					iter.remove();
+				}
+			}
+
+			// update joinArgs
+			joinArgs = Lists.newArrayList();
+			for (final QueryModelNode node : argSet) {
+				if (!(node instanceof ValueExpr)) {
+					joinArgs.add((TupleExpr) node);
+				}
+			}
+			joinArgs.addAll(bsaList);
+
+			return joinArgs;
+		}
+
+		private void updateFilters(List<Filter> filters, boolean firstJoin) {
+
+			final Iterator<Filter> iter = segmentFilters.iterator();
+
+			while (iter.hasNext()) {
+				if (!FilterRelocator.relocate(iter.next(), firstJoin)) {
+					iter.remove();
+				}
+			}
+		}
+
+		/**
+		 *
+		 * @param tupleExpr
+		 *            -- the query
+		 * @param joinArgs
+		 *            -- the non-join nodes contained in the join segment
+		 * @param getFilters
+		 *            -- the filters contained in the query
+		 * @return -- the non-join nodes contained in the join segment
+		 */
+		protected List<QueryModelNode> getJoinArgs(TupleExpr tupleExpr,
+				List<QueryModelNode> joinArgs, boolean getFilters) {
+			if (tupleExpr instanceof Join) {
+				if (!(((Join) tupleExpr).getLeftArg() instanceof FixedStatementPattern)
+						&& !(((Join) tupleExpr).getRightArg() instanceof DoNotExpandSP)) {
+					final Join join = (Join) tupleExpr;
+					getJoinArgs(join.getLeftArg(), joinArgs, getFilters);
+					getJoinArgs(join.getRightArg(), joinArgs, getFilters);
+				}
+			} else if (tupleExpr instanceof Filter) {
+				if (getFilters) {
+					joinArgs.add(((Filter) tupleExpr).getCondition());
+				}
+				getJoinArgs(((Filter) tupleExpr).getArg(), joinArgs, getFilters);
+			} else if (tupleExpr instanceof Projection) {
+				getJoinArgs(((Projection) tupleExpr).getArg(), joinArgs,
+						getFilters);
+			} else {
+				joinArgs.add(tupleExpr);
+			}
+
+			return joinArgs;
+		}
+	}
+
+	/**
+	 * Relocates filters based on the binding variables contained in the
+	 * {@link Filter}. If you don't specify the FilterRelocator to stop at the
+	 * first {@link Join}, the relocator pushes the filter as far down the query
+	 * plan as possible, checking if the nodes below contain its binding
+	 * variables. If stopAtFirstJoin = true, the Filter is inserted at the first
+	 * Join node encountered. The relocator tracks whether the node stays in the
+	 * join segment or is inserted outside of the Join segment and returns true
+	 * if the Filter stays in the segment and false otherwise.
+	 *
+	 */
+
+	protected static class FilterRelocator extends
+			QueryModelVisitorBase<RuntimeException> {
+
+		protected final Filter filter;
+
+		protected final Set<String> filterVars;
+		private boolean stopAtFirstJoin = false;
+		private boolean isFirstJoinFilter = false;
+		private boolean inSegment = true;
+
+		public FilterRelocator(Filter filter) {
+			this.filter = filter;
+			filterVars = VarNameCollector.process(filter.getCondition());
+		}
+
+		public FilterRelocator(Filter filter, boolean stopAtFirstJoin) {
+			this.filter = filter;
+			filterVars = VarNameCollector.process(filter.getCondition());
+			this.stopAtFirstJoin = stopAtFirstJoin;
+		}
+
+		public static boolean relocate(Filter filter) {
+			final FilterRelocator fr = new FilterRelocator(filter);
+			filter.visit(fr);
+			return fr.inSegment;
+		}
+
+		public static boolean relocate(Filter filter, boolean stopAtFirstJoin) {
+			if (stopAtFirstJoin) {
+				final FilterRelocator fr = new FilterRelocator(filter,
+						stopAtFirstJoin);
+				filter.visit(fr);
+				return fr.isFirstJoinFilter;
+			} else {
+				final FilterRelocator fr = new FilterRelocator(filter);
+				filter.visit(fr);
+				return fr.inSegment;
+			}
+		}
+
+		@Override
+		protected void meetNode(QueryModelNode node) {
+			// By default, do not traverse
+			assert node instanceof TupleExpr;
+
+			if (node instanceof UnaryTupleOperator) {
+				if (((UnaryTupleOperator) node).getArg().getBindingNames()
+						.containsAll(filterVars)) {
+					if (stopAtFirstJoin) {
+						((UnaryTupleOperator) node).getArg().visit(this);
+					} else {
+						inSegment = false;
+						relocate(filter, ((UnaryTupleOperator) node).getArg());
+					}
+				}
+			}
+
+			relocate(filter, (TupleExpr) node);
+		}
+
+		@Override
+		public void meet(Join join) {
+
+			if (stopAtFirstJoin) {
+				isFirstJoinFilter = true;
+				relocate(filter, join);
+			} else {
+
+				if (join.getLeftArg().getBindingNames().containsAll(filterVars)) {
+					// All required vars are bound by the left expr
+					join.getLeftArg().visit(this);
+				} else if (join.getRightArg().getBindingNames()
+						.containsAll(filterVars)) {
+					// All required vars are bound by the right expr
+					join.getRightArg().visit(this);
+				} else {
+					relocate(filter, join);
+				}
+			}
+		}
+
+		@Override
+		public void meet(LeftJoin leftJoin) {
+
+			if (leftJoin.getLeftArg().getBindingNames().containsAll(filterVars)) {
+				inSegment = false;
+				if (stopAtFirstJoin) {
+					leftJoin.getLeftArg().visit(this);
+				} else {
+					relocate(filter, leftJoin.getLeftArg());
+				}
+			} else {
+				relocate(filter, leftJoin);
+			}
+		}
+
+		@Override
+		public void meet(Union union) {
+			final Filter clone = new Filter();
+			clone.setCondition(filter.getCondition().clone());
+
+			relocate(filter, union.getLeftArg());
+			relocate(clone, union.getRightArg());
+
+			inSegment = false;
+
+		}
+
+		@Override
+		public void meet(Difference node) {
+			final Filter clone = new Filter();
+			clone.setCondition(filter.getCondition().clone());
+
+			relocate(filter, node.getLeftArg());
+			relocate(clone, node.getRightArg());
+
+			inSegment = false;
+
+		}
+
+		@Override
+		public void meet(Intersection node) {
+			final Filter clone = new Filter();
+			clone.setCondition(filter.getCondition().clone());
+
+			relocate(filter, node.getLeftArg());
+			relocate(clone, node.getRightArg());
+
+			inSegment = false;
+
+		}
+
+		@Override
+		public void meet(Extension node) {
+			if (node.getArg().getBindingNames().containsAll(filterVars)) {
+				if (stopAtFirstJoin) {
+					node.getArg().visit(this);
+				} else {
+					relocate(filter, node.getArg());
+					inSegment = false;
+				}
+			} else {
+				relocate(filter, node);
+			}
+		}
+
+		@Override
+		public void meet(EmptySet node) {
+			if (filter.getParentNode() != null) {
+				// Remove filter from its original location
+				filter.replaceWith(filter.getArg());
+			}
+		}
+
+		@Override
+		public void meet(Filter filter) {
+			// Filters are commutative
+			filter.getArg().visit(this);
+		}
+
+		@Override
+		public void meet(Distinct node) {
+			node.getArg().visit(this);
+		}
+
+		@Override
+		public void meet(Order node) {
+			node.getArg().visit(this);
+		}
+
+		@Override
+		public void meet(QueryRoot node) {
+			node.getArg().visit(this);
+		}
+
+		@Override
+		public void meet(Reduced node) {
+			node.getArg().visit(this);
+		}
+
+		protected void relocate(Filter filter, TupleExpr newFilterArg) {
+			if (filter.getArg() != newFilterArg) {
+				if (filter.getParentNode() != null) {
+					// Remove filter from its original location
+					filter.replaceWith(filter.getArg());
+				}
+
+				// Insert filter at the new location
+				newFilterArg.replaceWith(filter);
+				filter.setArg(newFilterArg);
+			}
+		}
+	}
+
+	/**
+	 * This method determines whether an index node is valid. Criteria for a
+	 * valid node are that is have two or more {@link StatementPattern} nodes or
+	 * at least one {@link Filter} and one StatementPattern node. Additionally,
+	 * the number of variables in the Filter cannot exceed the number of
+	 * variables among all non-Filter nodes in the TupleExpr. Also, this method
+	 * calls the {@link ValidQueryVisitor} to determine if the
+	 * ExternalTupleSet's TupleExpr contains an invalid node type.
+	 *
+	 * @param node
+	 *            -- typically an {@link ExternalTupleSet} index node
+	 * @return
+	 */
+	private static boolean isTupleValid(QueryModelNode node) {
+
+		final ValidQueryVisitor vqv = new ValidQueryVisitor();
+		node.visit(vqv);
+
+		if (vqv.isValid() && vqv.getSPs().size() + vqv.getFilters().size() > 1) {
+			if (vqv.getFilters().size() > 0) {
+				final Set<String> spVars = getVarNames(vqv.getSPs());
+				final Set<String> fVarNames = getVarNames(vqv.getFilters());
+				// check that all vars contained in filters also occur in SPs
+				return Sets.intersection(fVarNames, spVars).equals(fVarNames);
+			} else {
+				return true;
+			}
+		} else {
+			return false;
+		}
+	}
+
+	private static Set<String> getVarNames(Collection<QueryModelNode> nodes) {
+
+		List<String> tempVars;
+		final Set<String> nodeVarNames = Sets.newHashSet();
+
+		for (final QueryModelNode s : nodes) {
+			tempVars = VarCollector.process(s);
+			for (final String t : tempVars) {
+				nodeVarNames.add(t);
+			}
+		}
+		return nodeVarNames;
+	}
+
+	/**
+	 * A visitor which checks a TupleExpr associated with an ExternalTupleSet to
+	 * determine whether the TupleExpr contains an invalid node.
+	 *
+	 */
+	private static class ValidQueryVisitor extends
+			QueryModelVisitorBase<RuntimeException> {
+
+		private boolean isValid = true;
+		private final Set<QueryModelNode> filterSet = Sets.newHashSet();
+		private final Set<QueryModelNode> spSet = Sets.newHashSet();
+
+		public Set<QueryModelNode> getFilters() {
+			return filterSet;
+		}
+
+		public Set<QueryModelNode> getSPs() {
+			return spSet;
+		}
+
+		public boolean isValid() {
+			return isValid;
+		}
+
+		@Override
+		public void meet(Projection node) {
+			node.getArg().visit(this);
+		}
+
+		@Override
+		public void meet(Filter node) {
+			filterSet.add(node.getCondition());
+			node.getArg().visit(this);
+		}
+
+		@Override
+		public void meet(StatementPattern node) {
+			spSet.add(node);
+		}
+
+		@Override
+		public void meetNode(QueryModelNode node) {
+
+			if (!(node instanceof Join || node instanceof StatementPattern
+					|| node instanceof BindingSetAssignment
+					|| node instanceof Var || node instanceof Union || node instanceof LeftJoin)) {
+				isValid = false;
+				return;
+
+			} else {
+				super.meetNode(node);
+			}
+		}
+
+	}
+
+	private static List<ExternalTupleSet> getAccIndices(Configuration conf)
+			throws MalformedQueryException, SailException,
+			QueryEvaluationException, TableNotFoundException,
+			AccumuloException, AccumuloSecurityException, PcjException {
+
+		List<String> tables = null;
+
+		if (conf instanceof RdfCloudTripleStoreConfiguration) {
+			tables = ((RdfCloudTripleStoreConfiguration) conf).getPcjTables();
+		}
+
+		final String tablePrefix = conf
+				.get(RdfCloudTripleStoreConfiguration.CONF_TBL_PREFIX);
+		final Connector c = ConfigUtils.getConnector(conf);
+		final Map<String, String> indexTables = Maps.newLinkedHashMap();
+		PcjTables pcj = new PcjTables();
+
+		if (tables != null && !tables.isEmpty()) {
+			for (final String table : tables) {
+				indexTables
+						.put(table, pcj.getPcjMetadata(c, table).getSparql());
+			}
+		} else {
+			for (final String table : c.tableOperations().list()) {
+				if (table.startsWith(tablePrefix + "INDEX")) {
+					indexTables.put(table, pcj.getPcjMetadata(c, table)
+							.getSparql());
+				}
+			}
+
+		}
+		final List<ExternalTupleSet> index = Lists.newArrayList();
+
+		if (indexTables.isEmpty()) {
+			System.out.println("No Index found");
+		} else {
+			for (final String table : indexTables.keySet()) {
+				final String indexSparqlString = indexTables.get(table);
+				index.add(new AccumuloIndexSet(indexSparqlString, c, table));
+			}
+		}
+		return index;
+	}
 }

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/AccumuloIndexSet.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/AccumuloIndexSet.java b/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/AccumuloIndexSet.java
index dda452d..456c465 100644
--- a/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/AccumuloIndexSet.java
+++ b/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/AccumuloIndexSet.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.external.tupleSet;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -19,608 +19,373 @@ package mvm.rya.indexing.external.tupleSet;
  * under the License.
  */
 
-
-
 import info.aduna.iteration.CloseableIteration;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
 import java.util.NoSuchElementException;
 import java.util.Set;
 
-import mvm.rya.accumulo.precompQuery.AccumuloPrecompQueryIndexer;
+import mvm.rya.accumulo.precompQuery.AccumuloPcjQuery;
+import mvm.rya.api.utils.IteratorWrapper;
+import mvm.rya.indexing.PcjQuery;
+import mvm.rya.indexing.external.tupleSet.PcjTables.PcjException;
+import mvm.rya.indexing.external.tupleSet.PcjTables.PcjMetadata;
+import mvm.rya.indexing.external.tupleSet.PcjTables.VariableOrder;
 import mvm.rya.rdftriplestore.evaluation.ExternalBatchingIterator;
 
 import org.apache.accumulo.core.client.AccumuloException;
 import org.apache.accumulo.core.client.AccumuloSecurityException;
-import org.apache.accumulo.core.client.BatchWriter;
 import org.apache.accumulo.core.client.Connector;
 import org.apache.accumulo.core.client.MutationsRejectedException;
-import org.apache.accumulo.core.client.Scanner;
 import org.apache.accumulo.core.client.TableNotFoundException;
-import org.apache.accumulo.core.data.Key;
-import org.apache.accumulo.core.data.Mutation;
-import org.apache.accumulo.core.data.Range;
-import org.apache.accumulo.core.data.Value;
-import org.apache.accumulo.core.security.Authorizations;
-import org.apache.commons.io.IOUtils;
 import org.apache.hadoop.io.Text;
-import org.openrdf.model.Literal;
-import org.openrdf.model.URI;
-import org.openrdf.model.impl.LiteralImpl;
-import org.openrdf.model.impl.URIImpl;
 import org.openrdf.query.Binding;
 import org.openrdf.query.BindingSet;
 import org.openrdf.query.MalformedQueryException;
 import org.openrdf.query.QueryEvaluationException;
 import org.openrdf.query.algebra.Projection;
-import org.openrdf.query.algebra.QueryModelNode;
-import org.openrdf.query.algebra.StatementPattern;
-import org.openrdf.query.algebra.ValueExpr;
+import org.openrdf.query.algebra.TupleExpr;
 import org.openrdf.query.algebra.Var;
 import org.openrdf.query.algebra.helpers.QueryModelVisitorBase;
-import org.openrdf.query.impl.EmptyBindingSet;
 import org.openrdf.query.parser.ParsedTupleQuery;
 import org.openrdf.query.parser.sparql.SPARQLParser;
-import org.openrdf.repository.sail.SailRepositoryConnection;
 import org.openrdf.sail.SailException;
 
-import com.beust.jcommander.internal.Sets;
 import com.google.common.base.Joiner;
-import com.google.common.collect.BiMap;
 import com.google.common.collect.HashBiMap;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
-
+import com.google.common.collect.Sets;
+
+/**
+ * During query planning, this node is inserted into the parsed query to
+ * represent part of the original query (a sub-query). This sub-query is the
+ * value returned by {@link ExternalTupleSet#getTupleExpr()}. The results
+ * associated with this sub-query are stored in an external Accumulo table,
+ * where accCon and tablename are the associated {@link Connector} and table
+ * name. During evaluation, the portion of the query in
+ * {@link AccumuloIndexSet} is evaluated by scanning the external Accumulo
+ * table. This class is extremely useful for caching queries and reusing results
+ * from previous SPARQL queries.
+ * <p>
+ *
+ * The the {@link TupleExpr} returned by {@link ExternalTupleSet#getTupleExpr()}
+ * may have different variables than the query and variables stored in the
+ * external Accumulo table. The mapping of variables from the TupleExpr to the
+ * table variables are given by {@link ExternalTupleSet#getTableVarMap()}. In
+ * addition to allowing the variables to differ, it is possible for TupleExpr to
+ * have fewer variables than the table query--that is, some of the variables in
+ * the table query may appear as constants in the TupleExpr. Theses expression
+ * are extracted from TupleExpr by the methods
+ * {@link AccumuloIndexSet#getConstantConstraints()} and by the Visitor
+ * {@link ValueMapVisitor} to be used as constraints when scanning the Accumulo
+ * table. This allows for the pre-computed results to be used for a larger class
+ * of sub-queries.
+ *
+ */
 public class AccumuloIndexSet extends ExternalTupleSet implements ExternalBatchingIterator {
 
-    private static final int WRITER_MAX_WRITE_THREADS = 30;
-    private static final long WRITER_MAX_LATNECY = Long.MAX_VALUE;
-    private static final long WRITER_MAX_MEMORY = 500L * 1024L * 1024L;
-    private Map<String,AccValueFactory> bindings;
-    private List<String> bindingslist;
-    private final Connector accCon;
-    private final String tablename;
-    private long tableSize = 0;
-    private List<String> varOrder = null; 
-    
-    
-    public static interface AccValueFactory {
-        public org.openrdf.model.Value create(String str);
-
-        public String create(org.openrdf.model.Value val);
-    }
-
-    public static class AccUrlFactory implements AccValueFactory {
-        @Override
-        public org.openrdf.model.Value create(final String str) {
-            return new URIImpl(str);
-        }
+    private final Connector accCon;  //connector to Accumulo table where results are stored
+    private final String tablename;  //name of Accumulo table
+    private List<String> varOrder = null; // orders in which results are written to table
+    private PcjTables pcj = new PcjTables();
 
-        @Override
-        public String create(org.openrdf.model.Value val) {
-            return val.stringValue();
-        }
+    @Override
+    public Map<String, Set<String>> getSupportedVariableOrders() {
+        return this.getSupportedVariableOrderMap();
     }
-    
-    public static class AccValueFactoryImpl implements AccValueFactory {
-        @Override
-        public org.openrdf.model.Value create(final String str) {
-            String[] split = str.split("\u0001");
-            if (split.length > 1 && split[1].equals("1")) {
-                return new URIImpl(split[0]);
-            }
-            if (split[0].contains(":")) {
-                return new URIImpl(split[0]);
-            }
-            return new LiteralImpl(split[0]);
-        }
 
-        @Override
-        public String create(org.openrdf.model.Value val) {
-            if (val instanceof URI) {
-                return val.stringValue() + "\u0001" + 1;
-            }
-            if (val instanceof Literal) {
-                Literal v = (Literal) val;
-                return v.getLabel() + "\u0001" + 2;
-            }
-            return null;
-        }
+    @Override
+    public String getSignature() {
+        return "AccumuloIndexSet(" + tablename + ") : " + Joiner.on(", ").join(this.getTupleExpr().getAssuredBindingNames());
     }
 
-    
-    //TODO set supportedVarOrderMap 
-    public AccumuloIndexSet(String sparql, SailRepositoryConnection conn, Connector accCon, String tablename) throws MalformedQueryException, SailException,
-            QueryEvaluationException, MutationsRejectedException, TableNotFoundException {
-        super(null);
+    /**
+     *
+     * @param sparql - name of sparql query whose results will be stored in PCJ table
+     * @param accCon - connection to a valid Accumulo instance
+     * @param tablename - name of an existing PCJ table
+     * @throws MalformedQueryException
+     * @throws SailException
+     * @throws QueryEvaluationException
+     * @throws MutationsRejectedException
+     * @throws TableNotFoundException
+     */
+    public AccumuloIndexSet(String sparql, Connector accCon, String tablename) throws MalformedQueryException, SailException, QueryEvaluationException,
+            MutationsRejectedException, TableNotFoundException {
         this.tablename = tablename;
         this.accCon = accCon;
-        SPARQLParser sp = new SPARQLParser();
-        ParsedTupleQuery pq = (ParsedTupleQuery) sp.parseQuery(sparql, null);
-
+        final SPARQLParser sp = new SPARQLParser();
+        final ParsedTupleQuery pq = (ParsedTupleQuery) sp.parseQuery(sparql, null);
         setProjectionExpr((Projection) pq.getTupleExpr());
-        CloseableIteration<BindingSet,QueryEvaluationException> iter = (CloseableIteration<BindingSet,QueryEvaluationException>) conn.getSailConnection()
-                .evaluate(getTupleExpr(), null, new EmptyBindingSet(), false);
-
-        BatchWriter w = accCon.createBatchWriter(tablename, WRITER_MAX_MEMORY, WRITER_MAX_LATNECY, WRITER_MAX_WRITE_THREADS);
-        this.bindingslist = Lists.newArrayList(pq.getTupleExpr().getAssuredBindingNames());
-
-        this.bindings = Maps.newHashMap();
-
-        pq.getTupleExpr().visit(new QueryModelVisitorBase<RuntimeException>() {
-            @Override
-            public void meet(Var node) {
-                QueryModelNode parent = node.getParentNode();
-                if (parent instanceof StatementPattern) {
-                    StatementPattern statement = (StatementPattern) parent;
-                    if (node.equals(statement.getSubjectVar())) {
-                        bindings.put(node.getName(), new AccUrlFactory());
-                    }
-                    if (node.equals(statement.getPredicateVar())) {
-                        bindings.put(node.getName(), new AccUrlFactory());
-                    }
-                    if (node.equals(statement.getObjectVar())) {
-                        bindings.put(node.getName(), new AccValueFactoryImpl());
-                    }
-                    if (node.equals(statement.getContextVar())) {
-                        // TODO is this correct?
-                        bindings.put(node.getName(), new AccUrlFactory());
-                    }
-                } else if(parent instanceof ValueExpr) {
-                    bindings.put(node.getName(), new AccValueFactoryImpl());
-                } 
-            };
-        });
-        
-        
-        
-        
-       
-        varOrder = new ArrayList<String>(bindingslist.size());
- 
-        while (iter.hasNext()) {
-            
-            BindingSet bs = iter.next();
-            List<String> shiftBindingList = null;
-            for (int j = 0; j < bindingslist.size(); j++) {
-                StringBuffer sb = new StringBuffer();
-                shiftBindingList = listShift(bindingslist, j);  //TODO calling this each time not efficient
-                String order = "";
-                for (String b : shiftBindingList) {
-                    String val = bindings.get(b).create(bs.getValue(b));
-                    sb.append(val).append("\u0000");
-                    if (order.length() == 0) {
-                        order = b;
-                    } else {
-                        order = order + "\u0000" + b;
-                    }
-                }
-                
-                if (varOrder.size() < bindingslist.size()) {
-                    varOrder.add(order);
-                }
-                
-                //System.out.println("String buffer is " + sb);
-                Mutation m = new Mutation(sb.deleteCharAt(sb.length() - 1).toString());
-                m.put(new Text(varOrder.get(j)), new Text(""), new org.apache.accumulo.core.data.Value(new byte[]{}));
-                w.addMutation(m);
-            }
-            tableSize += 1;
+        Set<VariableOrder> orders = null;
+        try {
+			orders = pcj.getPcjMetadata(accCon, tablename).getVarOrders();
+		} catch (final PcjException e) {
+			e.printStackTrace();
+		}
+
+        varOrder = Lists.newArrayList();
+        for(final VariableOrder var: orders) {
+            varOrder.add(var.toString());
         }
-        
         setLocalityGroups(tablename, accCon, varOrder);
-        this.setSupportedVariableOrderMap(createSupportedVarOrderMap(varOrder));
-         
-        
-        String orders = "";
-        
-        for(String s : varOrder) {
-            s = s.replace("\u0000", ";");
-            if(orders.length() == 0) {
-                orders = s;
-            } else {
-                orders = orders + "\u0000" + s;
-            }
-        }
-        
-        
-        Mutation m = new Mutation("~SPARQL");
-        Value v = new Value(sparql.getBytes());
-        m.put(new Text("" + tableSize), new Text(orders), v);
-        w.addMutation(m);
-     
-        w.close();
-        iter.close();
+        this.setSupportedVariableOrderMap(varOrder);
     }
 
-    
-    
-    
+    /**
+     *
+     * @param accCon - connection to a valid Accumulo instance
+     * @param tablename - name of an existing PCJ table
+     * @throws MalformedQueryException
+     * @throws SailException
+     * @throws QueryEvaluationException
+     * @throws MutationsRejectedException
+     * @throws TableNotFoundException
+     */
+	public AccumuloIndexSet(Connector accCon, String tablename)
+			throws MalformedQueryException, SailException,
+			QueryEvaluationException, MutationsRejectedException,
+			TableNotFoundException {
+		PcjMetadata meta = null;
+		try {
+			meta = pcj.getPcjMetadata(accCon, tablename);
+		} catch (final PcjException e) {
+			e.printStackTrace();
+		}
+
+		this.tablename = tablename;
+		this.accCon = accCon;
+		final SPARQLParser sp = new SPARQLParser();
+		final ParsedTupleQuery pq = (ParsedTupleQuery) sp.parseQuery(meta.getSparql(),
+				null);
+		setProjectionExpr((Projection) pq.getTupleExpr());
+		final Set<VariableOrder> orders = meta.getVarOrders();
+
+		varOrder = Lists.newArrayList();
+		for (final VariableOrder var : orders) {
+			varOrder.add(var.toString());
+		}
+		setLocalityGroups(tablename, accCon, varOrder);
+		this.setSupportedVariableOrderMap(varOrder);
+	}
+
+	/**
+	 * returns size of table for query planning
+	 */
     @Override
-    public Map<String, Set<String>> getSupportedVariableOrders() {
-   
-        return this.getSupportedVariableOrderMap();
-
+    public double cardinality() {
+    	double cardinality = 0;
+        try {
+			cardinality = pcj.getPcjMetadata(accCon, tablename).getCardinality();
+		} catch (PcjException e) {
+			e.printStackTrace();
+		}
+        return cardinality;
     }
-    
-    
-    @Override
-    public boolean supportsBindingSet(Set<String> bindingNames) {
-
-        Map<String, Set<String>> varOrderMap = this.getSupportedVariableOrders();
-        Collection<Set<String>> values = varOrderMap.values();
-        Set<String> bNames = Sets.newHashSet();
 
-        for (String s : this.getTupleExpr().getAssuredBindingNames()) {
-            if (bindingNames.contains(s)) {
-                bNames.add(s);
-            }
-        }
-
-        return values.contains(bNames);
-    }
-        
-    
-    private String getVarOrder(Set<String> variables) {
 
-        Map<String, Set<String>> varOrderMap = this.getSupportedVariableOrders();
+    /**
+     *
+     * @param tableName
+     * @param conn
+     * @param groups  - locality groups to be created
+     *
+     * Sets locality groups for more efficient scans - these are usually the variable
+     * orders in the table so that scans for specific orders are more efficient
+     */
+    private void setLocalityGroups(String tableName, Connector conn, List<String> groups) {
 
-        Set<Map.Entry<String, Set<String>>> entries = varOrderMap.entrySet();
+		final HashMap<String, Set<Text>> localityGroups = new HashMap<String, Set<Text>>();
+		for (int i = 0; i < groups.size(); i++) {
+			final HashSet<Text> tempColumn = new HashSet<Text>();
+			tempColumn.add(new Text(groups.get(i)));
+			final String groupName = groups.get(i).replace(VAR_ORDER_DELIM, "");
+			localityGroups.put(groupName, tempColumn);
+		}
 
-        for (Map.Entry<String, Set<String>> e : entries) {
+		try {
+			conn.tableOperations().setLocalityGroups(tableName, localityGroups);
+		} catch (AccumuloException | AccumuloSecurityException
+				| TableNotFoundException e) {
+			e.printStackTrace();
+		}
 
-            if (e.getValue().equals(variables)) {
-                return e.getKey();
-            }
+	}
 
-        }
 
-        return null;
 
+    @Override
+    public CloseableIteration<BindingSet,QueryEvaluationException> evaluate(BindingSet bindingset) throws QueryEvaluationException {
+        return this.evaluate(Collections.singleton(bindingset));
     }
 
-    private String prefixToOrder(String order) {
-
-        Map<String, String> invMap = HashBiMap.create(this.getTableVarMap()).inverse();
-        String[] temp = order.split("\u0000");
-
-        for (int i = 0; i < temp.length; i++) {
-            temp[i] = this.getTableVarMap().get(temp[i]);
+    /**
+     * Core evaluation method used during query evaluation - given a collection of binding set constraints, this
+     * method finds common binding labels between the constraints and table, uses those to build a prefix scan
+     * of the Accumulo table, and creates a solution binding set by iterating of the scan results.
+     */
+    @Override
+    public CloseableIteration<BindingSet,QueryEvaluationException> evaluate(final Collection<BindingSet> bindingset) throws QueryEvaluationException {
+        String localityGroup = "";
+        final Set<String> commonVars = Sets.newHashSet();
+        // if bindingset is empty, there are no results, so return empty iterator
+        if (bindingset.isEmpty()) {
+        	return new IteratorWrapper<BindingSet, QueryEvaluationException>(new HashSet<BindingSet>().iterator());
         }
-
-        order = Joiner.on("\u0000").join(temp);
-
-        for (String s : varOrder) {
-            if (s.startsWith(order)) {
-
-                temp = s.split("\u0000");
-
-                for (int i = 0; i < temp.length; i++) {
-                    temp[i] = invMap.get(temp[i]);
+      //to build range prefix, find common vars of bindingset and PCJ bindings
+        else {
+        	final BindingSet bs = bindingset.iterator().next();
+            for (final String b : this.getTupleExpr().getAssuredBindingNames()) {
+                final Binding v = bs.getBinding(b);
+                if (v != null) {
+                    commonVars.add(b);
                 }
-                return Joiner.on("\u0000").join(temp);
             }
         }
-        throw new NoSuchElementException("Order is not a prefix of any locality group value!");
+        //add any constant constraints to common vars to be used in range prefix
+        commonVars.addAll(getConstantConstraints());
+        PcjQuery apq = null;
+        List<String> fullVarOrder =  null;
+        String commonVarOrder = null;
+        try {
+            if (commonVars.size() > 0) {
+                commonVarOrder = getVarOrder(commonVars);
+                if(commonVarOrder == null) {
+                    throw new IllegalStateException("Index does not support binding set!");
+                }
+                fullVarOrder = Lists.newArrayList(prefixToOrder(commonVarOrder).split(VAR_ORDER_DELIM));
+                //use varOrder and tableVarMap to set correct scan column
+                localityGroup = orderToLocGroup(fullVarOrder);
+            } else {
+                localityGroup = varOrder.get(0);
+            }
+            apq = new AccumuloPcjQuery(accCon, tablename);
+            final ValueMapVisitor vmv = new ValueMapVisitor();
+            this.getTupleExpr().visit(vmv);
+
+            List<String> commonVarOrderList = null;
+            if(commonVarOrder != null) {
+            	commonVarOrderList = Lists.newArrayList(commonVarOrder.split(VAR_ORDER_DELIM));
+            } else {
+            	commonVarOrderList = new ArrayList<>();
+            }
+
+            return apq.queryPrecompJoin(commonVarOrderList, localityGroup, vmv.getValMap(),
+            		HashBiMap.create(this.getTableVarMap()).inverse(), bindingset);
+        } catch(final TableNotFoundException e) {
+            throw new QueryEvaluationException(e);
+        }
     }
 
+    /**
+     *
+     * @param order - variable order as indicated by query
+     * @return - locality group or column family used in scan - this
+     * is just the variable order expressed in terms of the variables stored
+     * in the table
+     */
     private String orderToLocGroup(List<String> order) {
         String localityGroup = "";
-        for (String s : order) {
+        for (final String s : order) {
             if (localityGroup.length() == 0) {
                 localityGroup = this.getTableVarMap().get(s);
             } else {
-                localityGroup = localityGroup + "\u0000" + this.getTableVarMap().get(s);
+                localityGroup = localityGroup + VAR_ORDER_DELIM + this.getTableVarMap().get(s);
             }
         }
         return localityGroup;
-
     }
-    
-    
-    private void setLocalityGroups(String tableName, Connector conn, List<String> groups) {
-        
-        HashMap<String, Set<Text>> localityGroups = new HashMap<String, Set<Text>>();
-
-        
-        
-        for (int i = 0; i < groups.size(); i++) {
-            HashSet<Text> tempColumn = new HashSet<Text>();
-            tempColumn.add(new Text(groups.get(i)));
-            String groupName = groups.get(i).replace("\u0000","");
-            localityGroups.put(groupName, tempColumn);
-        }
-        
 
-        try {
-            conn.tableOperations().setLocalityGroups(tableName, localityGroups);
-        } catch (AccumuloException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        } catch (AccumuloSecurityException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        } catch (TableNotFoundException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-
-        
-        
-    }
-    
-    
-    
-    
-    
-    
-    
-    private List<String> listShift(List<String> list, int j) {
-        
-        if(j >= list.size()) {
-            throw new IllegalArgumentException();
-        }
-        
-        List<String> shiftList = Lists.newArrayList();
-        for(int i=0; i<list.size(); i++) {
-            shiftList.add(list.get((i+j)%list.size()));
+    /**
+     *
+     * @param order - prefix of a full variable order
+     * @return - full variable order that includes all variables whose values
+     * are stored in the table - used to obtain the locality group
+     */
+    //given partial order of query vars, convert to PCJ vars and determine
+    //if converted partial order is a substring of a full var order of PCJ variables.
+    //if converted partial order is a prefix, convert corresponding full PCJ var order to query vars
+    private String prefixToOrder(String order) {
+        final Map<String, String> invMap = HashBiMap.create(this.getTableVarMap()).inverse();
+        String[] temp = order.split(VAR_ORDER_DELIM);
+        //get order in terms of PCJ variables
+        for (int i = 0; i < temp.length; i++) {
+            temp[i] = this.getTableVarMap().get(temp[i]);
         }
-
-        return shiftList;
-    }
-    
-    
-    
-    private Set<String> getConstantConstraints() {
-
-        Map<String, String> tableMap = this.getTableVarMap();
-        Set<String> keys = tableMap.keySet();
-        Set<String> constants = Sets.newHashSet();
-
-        for (String s : keys) {
-            if (s.startsWith("-const-")) {
-                constants.add(s);
+        order = Joiner.on(VAR_ORDER_DELIM).join(temp);
+        for (final String s : varOrder) {
+        	//verify that partial order is prefix of a PCJ varOrder
+            if (s.startsWith(order)) {
+                temp = s.split(VAR_ORDER_DELIM);
+                //convert full PCJ varOrder back to query varOrder
+                for (int i = 0; i < temp.length; i++) {
+                    temp[i] = invMap.get(temp[i]);
+                }
+                return Joiner.on(VAR_ORDER_DELIM).join(temp);
             }
-
         }
-
-        return constants;
-
-    }
-    
-    
-    
-    
-    public AccumuloIndexSet(String sparql, Connector accCon, String tablename) throws MalformedQueryException, SailException, QueryEvaluationException,
-            MutationsRejectedException, TableNotFoundException {
-        super(null);
-        this.tablename = tablename;
-        this.accCon = accCon;
-        SPARQLParser sp = new SPARQLParser();
-        ParsedTupleQuery pq = (ParsedTupleQuery) sp.parseQuery(sparql, null);
-
-        setProjectionExpr((Projection) pq.getTupleExpr());
-
-        this.bindingslist = Lists.newArrayList(pq.getTupleExpr().getAssuredBindingNames());
-
-        this.bindings = Maps.newHashMap();
-        pq.getTupleExpr().visit(new QueryModelVisitorBase<RuntimeException>() {
-            @Override
-            public void meet(Var node) {
-                QueryModelNode parent = node.getParentNode();
-                if (parent instanceof StatementPattern) {
-                    StatementPattern statement = (StatementPattern) parent;
-                    if (node.equals(statement.getSubjectVar())) {
-                        bindings.put(node.getName(), new AccUrlFactory());
-                    }
-                    if (node.equals(statement.getPredicateVar())) {
-                        bindings.put(node.getName(), new AccUrlFactory());
-                    }
-                    if (node.equals(statement.getObjectVar())) {
-                        bindings.put(node.getName(), new AccValueFactoryImpl());
-                    }
-                    if (node.equals(statement.getContextVar())) {
-                        // TODO is this correct?
-                        bindings.put(node.getName(), new AccUrlFactory());
-                    }
-                } else if(parent instanceof ValueExpr) {
-                    bindings.put(node.getName(), new AccValueFactoryImpl());
-                } 
-            };
-        });
-        
-        
-        
-        
-        Scanner s = accCon.createScanner(tablename, new Authorizations()); 
-        s.setRange(Range.exact(new Text("~SPARQL")));       
-        Iterator<Entry<Key,Value>> i = s.iterator();
-        
-        String[] tempVarOrders = null;
-        
-        if (i.hasNext()) {
-            Entry<Key, Value> entry = i.next();
-            Text ts = entry.getKey().getColumnFamily();
-            tempVarOrders = entry.getKey().getColumnQualifier().toString().split("\u0000");
-            tableSize = Long.parseLong(ts.toString());
-
-        } else {
-            throw new IllegalStateException("Index table contains no metadata!");
-        }      
-
-        
-        varOrder = Lists.newArrayList();
-        
-        for(String t: tempVarOrders) {
-            t = t.replace(";","\u0000");
-            varOrder.add(t);
-        }
-        
-        setLocalityGroups(tablename, accCon, varOrder);
-        this.setSupportedVariableOrderMap(createSupportedVarOrderMap(varOrder));
-        
+        throw new NoSuchElementException("Order is not a prefix of any locality group value!");
     }
 
-    
-    
-    
-    private Map<String, Set<String>> createSupportedVarOrderMap(List<String> orders) {
-        
-        Map<String, Set<String>> supportedVars = Maps.newHashMap();
-        
-        for (String t : orders) {
-            
-            String[] tempOrder = t.split("\u0000");
-            Set<String> varSet = Sets.newHashSet();
-            String u = "";
-            
-            for (String s : tempOrder) {
-                if(u.length() == 0) {
-                    u = s;
-                } else{
-                    u = u+ "\u0000" + s;
-                }
-                varSet.add(s);
-                supportedVars.put(u, new HashSet<String>(varSet));
-
+    /**
+     *
+     * @param variables
+     * @return - string representation of the Set variables, in an order that is in the
+     * table
+     */
+    private String getVarOrder(Set<String> variables) {
+        final Map<String, Set<String>> varOrderMap = this.getSupportedVariableOrders();
+        final Set<Map.Entry<String, Set<String>>> entries = varOrderMap.entrySet();
+        for (final Map.Entry<String, Set<String>> e : entries) {
+            if (e.getValue().equals(variables)) {
+                return e.getKey();
             }
-
         }
-        
-        return supportedVars;
-    }
-    
-    
-    
-    @Override
-    public void setProjectionExpr(Projection tupleExpr) {
-        super.setProjectionExpr(tupleExpr);
-        this.bindingslist = Lists.newArrayList(tupleExpr.getAssuredBindingNames());
-
-        this.bindings = Maps.newHashMap();
-        tupleExpr.visit(new QueryModelVisitorBase<RuntimeException>() {
-            @Override
-            public void meet(Var node) {
-                QueryModelNode parent = node.getParentNode();
-                if (parent instanceof StatementPattern) {
-                    StatementPattern statement = (StatementPattern) parent;
-                    if (node.equals(statement.getSubjectVar())) {
-                        bindings.put(node.getName(), new AccUrlFactory());
-                    }
-                    if (node.equals(statement.getPredicateVar())) {
-                        bindings.put(node.getName(), new AccUrlFactory());
-                    }
-                    if (node.equals(statement.getObjectVar())) {
-                        bindings.put(node.getName(), new AccValueFactoryImpl());
-                    }
-                    if (node.equals(statement.getContextVar())) {
-                        // TODO is this correct?
-                        bindings.put(node.getName(), new AccUrlFactory());
-                    }
-                } else if (parent instanceof ValueExpr) {  //Add bindings associated with Filters
-                    bindings.put(node.getName(), new AccValueFactoryImpl());
-                } 
-            };
-        });
-
-    }
-
-    @Override
-    public String getSignature() {
-        return "AccumuloIndexSet(" + tablename + ") : " + Joiner.on(", ").join(bindingslist);
-    }
-
-    @Override
-    public CloseableIteration<BindingSet,QueryEvaluationException> evaluate(BindingSet bindingset) throws QueryEvaluationException {
-        return this.evaluate(Collections.singleton(bindingset));
-    }
-    
-    @Override
-    public double cardinality() {
-        return tableSize;
+        return null;
     }
 
-    @Override
-    public CloseableIteration<BindingSet,QueryEvaluationException> evaluate(final Collection<BindingSet> bindingset) throws QueryEvaluationException {
-        
-        String localityGroup = "";
-        Set<String> commonVars = Sets.newHashSet();
-
-        if (!bindingset.isEmpty()) {
-
-            BindingSet bs = bindingset.iterator().next();
-            for (String b : bindingslist) {
-                Binding v = bs.getBinding(b);
-                if (v != null) {
-                    commonVars.add(b);
-                }
-
-            }
-        }
-        
-        commonVars.addAll(getConstantConstraints());
-        AccumuloPrecompQueryIndexer apq = null;
-        List<String> fullVarOrder =  null;
-        try {
-       
-            if (commonVars.size() > 0) {
-                String commonVarOrder = getVarOrder(commonVars);
-                if(commonVarOrder == null) {
-                    throw new IllegalStateException("Index does not support binding set!");
-                }
-                fullVarOrder = Lists.newArrayList(prefixToOrder(commonVarOrder).split("\u0000"));
-                localityGroup = orderToLocGroup(fullVarOrder);
-                fullVarOrder.add("" + commonVars.size());
-                
-            } else {
-                fullVarOrder = bindingslist;
-                localityGroup = orderToLocGroup(fullVarOrder);
-                fullVarOrder.add("" + 0);
+    /**
+     * @return - all constraints which correspond to variables
+     * in {@link AccumuloIndexSet#getTupleExpr()} which are set
+     * equal to a constant, but are non-constant in Accumulo table
+     */
+    private Set<String> getConstantConstraints() {
+        final Map<String, String> tableMap = this.getTableVarMap();
+        final Set<String> keys = tableMap.keySet();
+        final Set<String> constants = Sets.newHashSet();
+        for (final String s : keys) {
+            if (s.startsWith("-const-")) {
+                constants.add(s);
             }
-
-            
-            apq = new AccumuloPrecompQueryIndexer(accCon, tablename);
-            ValueMapVisitor vmv = new ValueMapVisitor();
-            this.getTupleExpr().visit(vmv);
-            
-            return apq.queryPrecompJoin(fullVarOrder, localityGroup, this.bindings, vmv.getValMap(), bindingset);
-            
-        } catch(TableNotFoundException e) {
-            throw new QueryEvaluationException(e);
-        } finally {
-            IOUtils.closeQuietly(apq);
         }
+        return constants;
     }
-    
-    
-    public class ValueMapVisitor extends QueryModelVisitorBase<RuntimeException> {
 
+    /**
+     *
+     * Extracts the values associated with constant labels in the query
+     * Used to create binding sets from range scan
+     */
+    public class ValueMapVisitor extends QueryModelVisitorBase<RuntimeException> {
         Map<String, org.openrdf.model.Value> valMap = Maps.newHashMap();
-
-        
         public Map<String, org.openrdf.model.Value> getValMap() {
             return valMap;
         }
-        
         @Override
         public void meet(Var node) {
             if (node.getName().startsWith("-const-")) {
                 valMap.put(node.getName(), node.getValue());
             }
-
         }
-
     }
-    
-    
+
 }
-    
+
+


[14/16] incubator-rya git commit: RYA-32 Improve how metadata and values are written to Accumulo PCJ tables

Posted by mi...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/VarConstantIndexListPruner.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/VarConstantIndexListPruner.java b/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/VarConstantIndexListPruner.java
index 7e72821..577663b 100644
--- a/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/VarConstantIndexListPruner.java
+++ b/extras/indexing/src/main/java/mvm/rya/indexing/IndexPlanValidator/VarConstantIndexListPruner.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.IndexPlanValidator;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -20,6 +20,7 @@ package mvm.rya.indexing.IndexPlanValidator;
  */
 
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -33,7 +34,7 @@ import org.openrdf.query.algebra.ValueConstant;
 import org.openrdf.query.algebra.Var;
 import org.openrdf.query.algebra.helpers.QueryModelVisitorBase;
 
-import com.beust.jcommander.internal.Maps;
+import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 
 
@@ -54,9 +55,10 @@ public class VarConstantIndexListPruner implements IndexListPruner {
         queryFilterCount = cc.getFilterCount();
     }
 
-    public Set<ExternalTupleSet> getRelevantIndices(List<ExternalTupleSet> indexList) {
+    @Override
+	public List<ExternalTupleSet> getRelevantIndices(List<ExternalTupleSet> indexList) {
 
-        Set<ExternalTupleSet> relIndexSet = Sets.newHashSet();
+        List<ExternalTupleSet> relIndexSet = new ArrayList<>();
 
         for (ExternalTupleSet e : indexList) {
 
@@ -73,14 +75,14 @@ public class VarConstantIndexListPruner implements IndexListPruner {
 
         ConstantCollector cc = new ConstantCollector();
         index.visit(cc);
-        
+
         Map<String, Integer> indexConstantMap = cc.getConstantMap();
         int indexSpCount = cc.getSpCount();
         int indexFilterCount = cc.getFilterCount();
         Set<String> indexConstants = indexConstantMap.keySet();
 
-        if ((indexSpCount > querySpCount) || (indexFilterCount > queryFilterCount) 
-                || !(Sets.intersection(indexConstants, queryConstantMap.keySet()).equals(indexConstants))) {
+        if (indexSpCount > querySpCount || indexFilterCount > queryFilterCount
+                || !Sets.intersection(indexConstants, queryConstantMap.keySet()).equals(indexConstants)) {
             return false;
         }
 
@@ -99,31 +101,31 @@ public class VarConstantIndexListPruner implements IndexListPruner {
         private Map<String, Integer> constantMap = Maps.newHashMap();
         private int spCount = 0;
         private int filterCount = 0;
-        
-        
+
+
         @Override
         public void meet(StatementPattern node) throws RuntimeException {
-            
+
            spCount++;
            super.meet(node);
 
         }
-        
-        
+
+
         @Override
         public void meet(Filter node) throws RuntimeException {
-            
+
            filterCount++;
            super.meet(node);
 
         }
-        
-        
-        
-        
+
+
+
+
         @Override
         public void meet(Var node) throws RuntimeException {
-            
+
             if (node.isConstant()) {
                 String key = node.getValue().toString();
                 if(constantMap.containsKey(key)){
@@ -136,12 +138,13 @@ public class VarConstantIndexListPruner implements IndexListPruner {
             }
 
         }
-        
-        
-        public void meet(ValueConstant node) throws RuntimeException {
-            
+
+
+        @Override
+		public void meet(ValueConstant node) throws RuntimeException {
+
             String key = node.getValue().toString();
-            
+
             if(constantMap.containsKey(key)) {
                 int count = constantMap.get(key);
                 count += 1;
@@ -149,23 +152,23 @@ public class VarConstantIndexListPruner implements IndexListPruner {
             } else {
                 constantMap.put(key,1);
             }
-            
+
         }
-        
+
 
         public Map<String, Integer> getConstantMap() {
             return constantMap;
         }
-        
+
         public int getSpCount(){
             return spCount;
         }
 
-        
+
         public int getFilterCount() {
             return filterCount;
         }
-        
+
     }
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/indexing/PcjQuery.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/PcjQuery.java b/extras/indexing/src/main/java/mvm/rya/indexing/PcjQuery.java
new file mode 100644
index 0000000..57f77cf
--- /dev/null
+++ b/extras/indexing/src/main/java/mvm/rya/indexing/PcjQuery.java
@@ -0,0 +1,40 @@
+package mvm.rya.indexing;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+import info.aduna.iteration.CloseableIteration;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.accumulo.core.client.TableNotFoundException;
+import org.openrdf.model.Value;
+import org.openrdf.query.BindingSet;
+import org.openrdf.query.QueryEvaluationException;
+
+public interface PcjQuery {
+
+    public abstract CloseableIteration<BindingSet, QueryEvaluationException> queryPrecompJoin(List<String> varOrder,
+            String localityGroup, Map<String, Value> valMap, Map<String, String> varMap, Collection<BindingSet> constraints) throws QueryEvaluationException,TableNotFoundException;
+
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/indexing/PrecompQueryIndexer.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/PrecompQueryIndexer.java b/extras/indexing/src/main/java/mvm/rya/indexing/PrecompQueryIndexer.java
deleted file mode 100644
index 1aecd98..0000000
--- a/extras/indexing/src/main/java/mvm/rya/indexing/PrecompQueryIndexer.java
+++ /dev/null
@@ -1,63 +0,0 @@
-package mvm.rya.indexing;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-import info.aduna.iteration.CloseableIteration;
-
-import java.io.Closeable;
-import java.io.Flushable;
-import java.io.IOException;
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-
-import mvm.rya.indexing.external.tupleSet.AccumuloIndexSet.AccValueFactory;
-
-import org.apache.accumulo.core.client.TableNotFoundException;
-import org.openrdf.model.Value;
-import org.openrdf.query.BindingSet;
-import org.openrdf.query.QueryEvaluationException;
-
-
-
-public interface PrecompQueryIndexer extends Closeable, Flushable {
-
-    
-    public abstract void storeBindingSet(BindingSet bs) throws IOException ;
-
-    public abstract void storeBindingSets(Collection<BindingSet> bindingSets)
-            throws IOException, IllegalArgumentException;
-
-
-    public abstract CloseableIteration<BindingSet, QueryEvaluationException> queryPrecompJoin(List<String> varOrder, 
-            String localityGroup, Map<String, AccValueFactory> bindings, Map<String,Value> valMap, 
-            Collection<BindingSet> constraints) throws QueryEvaluationException,TableNotFoundException;
-
-   
-
-    @Override
-    public abstract void flush() throws IOException;
-
-    @Override
-    public abstract void close() throws IOException;
-}
-
- 

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/indexing/accumulo/entity/EntityOptimizer.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/accumulo/entity/EntityOptimizer.java b/extras/indexing/src/main/java/mvm/rya/indexing/accumulo/entity/EntityOptimizer.java
index e46c321..bb792ac 100644
--- a/extras/indexing/src/main/java/mvm/rya/indexing/accumulo/entity/EntityOptimizer.java
+++ b/extras/indexing/src/main/java/mvm/rya/indexing/accumulo/entity/EntityOptimizer.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.accumulo.entity;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -27,7 +27,6 @@ import java.util.Set;
 
 import mvm.rya.accumulo.AccumuloRdfConfiguration;
 import mvm.rya.api.RdfCloudTripleStoreConfiguration;
-import mvm.rya.api.persist.RdfEvalStatsDAO;
 import mvm.rya.api.persist.joinselect.SelectivityEvalDAO;
 import mvm.rya.indexing.accumulo.ConfigUtils;
 import mvm.rya.joinselect.AccumuloSelectivityEvalDAO;
@@ -46,7 +45,6 @@ import org.openrdf.query.algebra.Join;
 import org.openrdf.query.algebra.QueryModelNode;
 import org.openrdf.query.algebra.StatementPattern;
 import org.openrdf.query.algebra.TupleExpr;
-import org.openrdf.query.algebra.Var;
 import org.openrdf.query.algebra.evaluation.QueryOptimizer;
 import org.openrdf.query.algebra.helpers.QueryModelVisitorBase;
 
@@ -60,12 +58,12 @@ public class EntityOptimizer implements QueryOptimizer, Configurable {
     private RdfCloudTripleStoreConfiguration conf;
     private boolean isEvalDaoSet = false;
 
-    
+
     public EntityOptimizer() {
-        
+
     }
-    
-    public EntityOptimizer(RdfCloudTripleStoreConfiguration conf) { 
+
+    public EntityOptimizer(RdfCloudTripleStoreConfiguration conf) {
         if(conf.isUseStats() && conf.isUseSelectivity()) {
             try {
                 eval = new AccumuloSelectivityEvalDAO(conf, ConfigUtils.getConnector(conf));
@@ -76,7 +74,7 @@ public class EntityOptimizer implements QueryOptimizer, Configurable {
             } catch (AccumuloSecurityException e) {
                 e.printStackTrace();
             }
-            
+
             isEvalDaoSet = true;
         } else {
             eval = null;
@@ -84,13 +82,13 @@ public class EntityOptimizer implements QueryOptimizer, Configurable {
         }
         this.conf = conf;
     }
-    
+
     public EntityOptimizer(SelectivityEvalDAO<RdfCloudTripleStoreConfiguration> eval) {
         this.eval = eval;
         this.conf = eval.getConf();
         isEvalDaoSet = true;
     }
-    
+
     @Override
     public void setConf(Configuration conf) {
         if(conf instanceof RdfCloudTripleStoreConfiguration) {
@@ -98,7 +96,7 @@ public class EntityOptimizer implements QueryOptimizer, Configurable {
         } else {
             this.conf = new AccumuloRdfConfiguration(conf);
         }
-        
+
         if (!isEvalDaoSet) {
             if(this.conf.isUseStats() && this.conf.isUseSelectivity()) {
                 try {
@@ -110,16 +108,16 @@ public class EntityOptimizer implements QueryOptimizer, Configurable {
                 } catch (AccumuloSecurityException e) {
                     e.printStackTrace();
                 }
-                
+
                 isEvalDaoSet = true;
             } else {
                 eval = null;
                 isEvalDaoSet = true;
             }
         }
-        
+
     }
-    
+
     @Override
     public Configuration getConf() {
         return conf;
@@ -151,7 +149,7 @@ public class EntityOptimizer implements QueryOptimizer, Configurable {
                     constructTuple(varMap, joinArgs, s);
                 }
                 List<TupleExpr> filterChain = getFilterChain(joinArgs);
-                
+
                 for (TupleExpr te : joinArgs) {
                     if (!(te instanceof StatementPattern) || !(te instanceof EntityTupleSet)) {
                         te.visit(this);
@@ -164,12 +162,12 @@ public class EntityOptimizer implements QueryOptimizer, Configurable {
                 e.printStackTrace();
             }
         }
-        
+
         private List<TupleExpr> getFilterChain(List<TupleExpr> joinArgs) {
             List<TupleExpr> filterTopBottom = Lists.newArrayList();
             TupleExpr filterChainTop = null;
             TupleExpr filterChainBottom = null;
-            
+
             for(int i = 0; i < joinArgs.size(); i++) {
                 if(joinArgs.get(i) instanceof Filter) {
                     if(filterChainTop == null) {
@@ -194,7 +192,7 @@ public class EntityOptimizer implements QueryOptimizer, Configurable {
             }
             return filterTopBottom;
         }
-        
+
         private TupleExpr getNewJoin(List<TupleExpr> joinArgs, List<TupleExpr> filterChain) {
             TupleExpr newJoin;
 
@@ -278,7 +276,7 @@ public class EntityOptimizer implements QueryOptimizer, Configurable {
         private void removeInvalidBins(HashMultimap<String, StatementPattern> varMap, boolean newMap) {
 
             Set<String> keys = Sets.newHashSet(varMap.keySet());
-            
+
             if (newMap) {
                 for (String s : keys) {
                     Set<StatementPattern> spSet = Sets.newHashSet(varMap.get(s));
@@ -333,7 +331,7 @@ public class EntityOptimizer implements QueryOptimizer, Configurable {
                 tempPriority = bin.size();
                 tempPriority *= getCardinality(bin);
                 tempPriority *= getMinCardSp(bin);
-                
+
                 // weight starQuery where common Var is constant slightly more -- this factor is subject
                 // to change
                 if(s.startsWith("-const-")) {
@@ -380,11 +378,11 @@ public class EntityOptimizer implements QueryOptimizer, Configurable {
             double cardinality = Double.MAX_VALUE;
             double tempCard = -1;
 
-            
+
             if(eval == null) {
                 return 1;
             }
-            
+
             List<StatementPattern> nodes = Lists.newArrayList(spNodes);
 
             AccumuloSelectivityEvalDAO ase = (AccumuloSelectivityEvalDAO) eval;
@@ -418,7 +416,7 @@ public class EntityOptimizer implements QueryOptimizer, Configurable {
                     Join join = (Join) tupleExpr;
                     getJoinArgs(join.getLeftArg(), joinArgs);
                     getJoinArgs(join.getRightArg(), joinArgs);
-                } 
+                }
             } else if(tupleExpr instanceof Filter) {
                 joinArgs.add(tupleExpr);
                 getJoinArgs(((Filter)tupleExpr).getArg(), joinArgs);
@@ -431,6 +429,6 @@ public class EntityOptimizer implements QueryOptimizer, Configurable {
 
     }
 
-    
+
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/indexing/accumulo/entity/EntityTupleSet.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/accumulo/entity/EntityTupleSet.java b/extras/indexing/src/main/java/mvm/rya/indexing/accumulo/entity/EntityTupleSet.java
index dbe7a53..3944a59 100644
--- a/extras/indexing/src/main/java/mvm/rya/indexing/accumulo/entity/EntityTupleSet.java
+++ b/extras/indexing/src/main/java/mvm/rya/indexing/accumulo/entity/EntityTupleSet.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.accumulo.entity;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -24,7 +24,6 @@ import info.aduna.iteration.CloseableIteration;
 
 import java.util.Collection;
 import java.util.Collections;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
@@ -56,7 +55,7 @@ import com.google.common.base.Joiner;
 
 public class EntityTupleSet extends ExternalSet implements ExternalBatchingIterator {
 
-    
+
     private StarQuery starQuery;
     private RdfCloudTripleStoreConfiguration conf;
     private Set<String> variables;
@@ -65,30 +64,30 @@ public class EntityTupleSet extends ExternalSet implements ExternalBatchingItera
     private double minCard;
     private Connector accCon = null;
     private boolean evalOptUsed = false;
-    
+
     public EntityTupleSet() {
-        
+
     }
-    
+
     public EntityTupleSet(StarQuery sq, RdfCloudTripleStoreConfiguration conf) {
         this.starQuery = sq;
         this.conf = conf;
-        
+
         variables = Sets.newHashSet();
         if(!starQuery.commonVarConstant()) {
             variables.add(starQuery.getCommonVarName());
         }
         variables.addAll(starQuery.getUnCommonVars());
-        
+
         init();
-        
+
     }
-    
+
     public EntityTupleSet(StarQuery sq, RdfCloudTripleStoreConfiguration conf, boolean evalOptUsed) {
         this(sq,conf);
         this.evalOptUsed = evalOptUsed;
     }
-    
+
     private void init() {
 
         try {
@@ -129,11 +128,11 @@ public class EntityTupleSet extends ExternalSet implements ExternalBatchingItera
     public Set<String> getAssuredBindingNames() {
         return starQuery.getAssuredBindingNames();
     }
-    
+
     public Set<String> getVariables() {
         return variables;
     }
-    
+
 
     @Override
     public String getSignature() {
@@ -147,29 +146,29 @@ public class EntityTupleSet extends ExternalSet implements ExternalBatchingItera
     public void setStarQuery(StarQuery sq) {
         this.starQuery = sq;
     }
-    
+
 
     @Override
     public EntityTupleSet clone() {
         StarQuery sq = new StarQuery(starQuery);
         return new EntityTupleSet(sq, conf);
     }
-    
-    
+
+
     @Override
     public double cardinality() {
         return cardinality;
     }
-        
-        
+
+
     public double getMinSpCard() {
         return minCard;
     }
-    
-    
+
+
     @Override
     public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(BindingSet bindings) throws QueryEvaluationException {
-        
+
         // if starQuery contains node with cardinality less than 1000 and node
         // only has one variable, and number of SPs in starQuery is greater than 2, it is
         // more efficient to first evaluate this node and then pass the bindings
@@ -180,7 +179,7 @@ public class EntityTupleSet extends ExternalSet implements ExternalBatchingItera
                 RdfCloudTripleStoreConnection conn = getRyaSailConnection();
                 CloseableIteration<BindingSet, QueryEvaluationException> sol = (CloseableIteration<BindingSet, QueryEvaluationException>) conn
                         .evaluate(minSp, null, bindings, false);
-                
+
                 Set<BindingSet> bSet = Sets.newHashSet();
                 while (sol.hasNext()) {
                     //TODO this is not optimal - should check if bindings variables intersect minSp variables
@@ -196,8 +195,8 @@ public class EntityTupleSet extends ExternalSet implements ExternalBatchingItera
 
                 StarQuery sq = new StarQuery(spList);
                 conn.close();
-                
-                return (new EntityTupleSet(sq, conf, true)).evaluate(bSet);
+
+                return new EntityTupleSet(sq, conf, true).evaluate(bSet);
 
             } catch (Exception e) {
                 throw new QueryEvaluationException(e);
@@ -206,27 +205,27 @@ public class EntityTupleSet extends ExternalSet implements ExternalBatchingItera
             this.evalOptUsed = true;
             return this.evaluate(Collections.singleton(bindings));
         }
-        
+
     }
-   
-        
+
+
     private int numberOfSpVars(StatementPattern sp) {
         List<Var> varList = sp.getVarList();
         int varCount = 0;
-        
+
         for(int i = 0; i < 3; i++) {
            if(!varList.get(i).isConstant()) {
                varCount++;
            }
         }
-        
+
         return varCount;
     }
-        
-    
+
+
     @Override
     public CloseableIteration<BindingSet,QueryEvaluationException> evaluate(final Collection<BindingSet> bindingset) throws QueryEvaluationException {
-        
+
         if(bindingset.size() < 2 && !this.evalOptUsed) {
             BindingSet bs = new QueryBindingSet();
             if (bindingset.size() == 1) {
@@ -245,8 +244,8 @@ public class EntityTupleSet extends ExternalSet implements ExternalBatchingItera
             IOUtils.closeQuietly(adi);
         }
     }
-    
-    
+
+
     private RdfCloudTripleStoreConnection getRyaSailConnection() throws AccumuloException,
             AccumuloSecurityException, SailException {
         final RdfCloudTripleStore store = new RdfCloudTripleStore();
@@ -259,6 +258,6 @@ public class EntityTupleSet extends ExternalSet implements ExternalBatchingItera
 
         return (RdfCloudTripleStoreConnection) store.getConnection();
     }
-    
-    
+
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/indexing/accumulo/entity/StarQuery.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/accumulo/entity/StarQuery.java b/extras/indexing/src/main/java/mvm/rya/indexing/accumulo/entity/StarQuery.java
index e9d2f85..b40beb6 100644
--- a/extras/indexing/src/main/java/mvm/rya/indexing/accumulo/entity/StarQuery.java
+++ b/extras/indexing/src/main/java/mvm/rya/indexing/accumulo/entity/StarQuery.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.accumulo.entity;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -28,7 +28,6 @@ import java.util.Set;
 import mvm.rya.accumulo.documentIndex.TextColumn;
 import mvm.rya.api.domain.RyaType;
 import mvm.rya.api.domain.RyaURI;
-import mvm.rya.api.persist.joinselect.SelectivityEvalDAO;
 import mvm.rya.api.resolver.RdfToRyaConversions;
 import mvm.rya.api.resolver.RyaContext;
 import mvm.rya.api.resolver.RyaTypeResolverException;
@@ -47,7 +46,7 @@ import com.google.common.collect.Sets;
 import com.google.common.primitives.Bytes;
 
 public class StarQuery {
-    
+
     private List<StatementPattern> nodes;
     private TextColumn[] nodeColumnCond;
     private String commonVarName;
@@ -56,17 +55,17 @@ public class StarQuery {
     private String contextURI ="";
     private Map<String,Integer> varPos = Maps.newHashMap();
     private boolean isCommonVarURI = false;
-   
-    
+
+
     public StarQuery(List<StatementPattern> nodes) {
         this.nodes = nodes;
         if(nodes.size() == 0) {
             throw new IllegalArgumentException("Nodes cannot be empty!");
         }
         nodeColumnCond = new TextColumn[nodes.size()];
-        Var tempContext = (Var) nodes.get(0).getContextVar();
+        Var tempContext = nodes.get(0).getContextVar();
         if(tempContext != null) {
-            context = (Var)tempContext.clone();
+            context = tempContext.clone();
         } else {
             context = new Var();
         }
@@ -76,51 +75,51 @@ public class StarQuery {
             e.printStackTrace();
         }
     }
-    
-    
+
+
     public StarQuery(Set<StatementPattern> nodes) {
         this(Lists.newArrayList(nodes));
     }
-    
+
     public int size() {
         return nodes.size();
     }
-    
+
     public StarQuery(StarQuery other) {
        this(other.nodes);
     }
-    
-    
+
+
     public List<StatementPattern> getNodes() {
         return nodes;
     }
-    
-    
+
+
     public TextColumn[] getColumnCond() {
         return nodeColumnCond;
     }
-    
-    
+
+
     public boolean isCommonVarURI() {
         return isCommonVarURI;
     }
-    
+
     public String getCommonVarName() {
         return commonVarName;
     }
-    
+
     public Var getCommonVar() {
         return commonVar;
     }
-    
+
     public boolean commonVarHasValue() {
         return commonVar.getValue() != null;
     }
-    
+
     public boolean commonVarConstant() {
         return commonVar.isConstant();
     }
-    
+
     public String getCommonVarValue() {
         if(commonVarHasValue()) {
             return commonVar.getValue().stringValue();
@@ -128,75 +127,75 @@ public class StarQuery {
             return null;
         }
     }
-    
-    
+
+
     public Set<String> getUnCommonVars() {
         return varPos.keySet();
     }
-    
-    
+
+
     public Map<String,Integer> getVarPos() {
         return varPos;
     }
-    
+
     public boolean hasContext() {
         return context.getValue() != null;
     }
-    
+
     public String getContextURI() {
         return contextURI;
     }
-    
-    
-    
-    
+
+
+
+
     public Set<String> getBindingNames() {
-        
+
         Set<String> bindingNames = Sets.newHashSet();
-        
+
         for(StatementPattern sp: nodes) {
-            
+
             if(bindingNames.size() == 0) {
                 bindingNames = sp.getBindingNames();
             } else {
                 bindingNames = Sets.union(bindingNames, sp.getBindingNames());
             }
-            
+
         }
-        
+
         return bindingNames;
-        
+
     }
-    
-    
-    
-    
+
+
+
+
     public Set<String> getAssuredBindingNames() {
-        
+
         Set<String> bindingNames = Sets.newHashSet();
-        
+
         for(StatementPattern sp: nodes) {
-            
+
             if(bindingNames.size() == 0) {
                 bindingNames = sp.getAssuredBindingNames();
             } else {
                 bindingNames = Sets.union(bindingNames, sp.getAssuredBindingNames());
             }
-            
+
         }
-        
+
         return bindingNames;
-        
+
     }
-    
-    
-    
-    
-    
-    
-    
+
+
+
+
+
+
+
     public CardinalityStatementPattern getMinCardSp(AccumuloSelectivityEvalDAO ase) {
-        
+
         StatementPattern minSp = null;
         double cardinality = Double.MAX_VALUE;
         double tempCard = -1;
@@ -213,43 +212,43 @@ public class StarQuery {
             } catch (TableNotFoundException e) {
                 e.printStackTrace();
             }
-            
+
 
         }
 
         return new CardinalityStatementPattern(minSp, cardinality) ;
     }
-    
-    
-    
+
+
+
     public class CardinalityStatementPattern {
-        
+
         private StatementPattern sp;
         private double cardinality;
-        
+
         public CardinalityStatementPattern(StatementPattern sp, double cardinality) {
             this.sp = sp;
             this.cardinality = cardinality;
         }
-        
+
         public StatementPattern getSp() {
             return sp;
         }
-        
+
         public double getCardinality() {
             return cardinality;
         }
-        
+
     }
-    
-    
+
+
    public double getCardinality( AccumuloSelectivityEvalDAO ase) {
-       
+
         double cardinality = Double.MAX_VALUE;
         double tempCard = -1;
 
         ase.setDenormalized(true);
-        
+
         try {
 
             for (int i = 0; i < nodes.size(); i++) {
@@ -267,50 +266,50 @@ public class StarQuery {
         } catch (Exception e) {
             e.printStackTrace();
         }
-        
+
         ase.setDenormalized(false);
 
         return cardinality/(nodes.size() + 1);
 
     }
-    
-    
-    
+
+
+
     public static Set<String> getCommonVars(StarQuery query, BindingSet bs) {
-        
+
         Set<String> starQueryVarNames = Sets.newHashSet();
-        
+
         if(bs == null || bs.size() == 0) {
             return Sets.newHashSet();
         }
-        
+
         Set<String> bindingNames = bs.getBindingNames();
         starQueryVarNames.addAll(query.getUnCommonVars());
         if(!query.commonVarConstant()) {
             starQueryVarNames.add(query.getCommonVarName());
         }
-        
+
         return Sets.intersection(bindingNames, starQueryVarNames);
-        
-        
+
+
     }
-    
-    
-    
-    
-    
-    
+
+
+
+
+
+
     public static StarQuery getConstrainedStarQuery(StarQuery query, BindingSet bs) {
-        
+
         if(bs.size() == 0) {
             return query;
         }
-        
+
         Set<String> bindingNames = bs.getBindingNames();
         Set<String> unCommonVarNames = query.getUnCommonVars();
         Set<String> intersectVar = Sets.intersection(bindingNames, unCommonVarNames);
-        
-        
+
+
         if (!query.commonVarConstant()) {
 
             Value v = bs.getValue(query.getCommonVarName());
@@ -319,7 +318,7 @@ public class StarQuery {
                 query.commonVar.setValue(v);
             }
         }
-        
+
         for(String s: intersectVar) {
             try {
                 query.nodeColumnCond[query.varPos.get(s)] = query.setValue(query.nodeColumnCond[query.varPos.get(s)], bs.getValue(s));
@@ -327,11 +326,11 @@ public class StarQuery {
                 e.printStackTrace();
             }
         }
-        
+
         return query;
     }
-    
-    
+
+
     private TextColumn setValue(TextColumn tc, Value v) throws RyaTypeResolverException {
 
         String cq = tc.getColumnQualifier().toString();
@@ -355,22 +354,22 @@ public class StarQuery {
         return tc;
 
     }
-    
-    
-    
+
+
+
     //assumes nodes forms valid star query with only one common variable
     //assumes nodes and commonVar has been set
     private TextColumn nodeToTextColumn(StatementPattern node, int i) throws RyaTypeResolverException {
-        
+
         RyaContext rc = RyaContext.getInstance();
-        
+
         Var subjVar = node.getSubjectVar();
         Var predVar = node.getPredicateVar();
         Var objVar = node.getObjectVar();
-        
+
         RyaURI predURI = (RyaURI) RdfToRyaConversions.convertValue(node.getPredicateVar().getValue());
-        
-        
+
+
         //assumes StatementPattern contains at least on variable
         if (subjVar.isConstant()) {
             if (commonVarConstant()) {
@@ -415,46 +414,46 @@ public class StarQuery {
                 return tc;
 
             }
-            
-            
+
+
         }
-        
-      
+
+
     }
-    
-    
-    
-    
+
+
+
+
     //called in constructor after nodes set
     //assumes nodes and nodeColumnCond are same size
     private void init() throws RyaTypeResolverException {
-        
-        
+
+
         commonVar = this.getCommonVar(nodes);
         if(!commonVar.isConstant()) {
             commonVarName = commonVar.getName();
         } else {
             commonVarName = commonVar.getName().substring(7);
         }
-        
+
         if(hasContext()) {
             RyaURI ctxtURI = (RyaURI) RdfToRyaConversions.convertValue(context.getValue());
             contextURI = ctxtURI.getData();
         }
-        
+
         for(int i = 0; i < nodes.size(); i++){
             nodeColumnCond[i] = nodeToTextColumn(nodes.get(i), i);
         }
-          
+
     }
-    
-    
-    
-    
-   
-    
-    
-    
+
+
+
+
+
+
+
+
     // called after nodes set
     // assumes nodes forms valid query with single, common variable
     private Var getCommonVar(List<StatementPattern> nodes) {
@@ -505,8 +504,8 @@ public class StarQuery {
         }
 
     }
-    
-    
+
+
     //assumes bindings is not of size 0
     private static boolean isBindingsetValid(Set<String> bindings) {
 
@@ -531,46 +530,46 @@ public class StarQuery {
         }
 
     }
-    
-    
-    
-    
-    
+
+
+
+
+
     public static boolean isValidStarQuery(Collection<StatementPattern> nodes) {
-        
+
         Set<String> bindings = null;
         boolean contextSet = false;
         Var context = null;
-        
+
         if(nodes.size() < 2) {
             return false;
         }
-        
+
         for(StatementPattern sp: nodes) {
-            
+
             Var tempContext = sp.getContextVar();
             Var predVar = sp.getPredicateVar();
-            
+
             //does not support variable context
             if(tempContext != null && !tempContext.isConstant()) {
-               return false; 
+               return false;
             }
             if(!contextSet) {
                 context = tempContext;
                 contextSet = true;
             } else {
-                
+
                 if(context == null && tempContext != null) {
                     return false;
                 } else if (context != null && !context.equals(tempContext)) {
                     return false;
                 }
             }
-            
+
             if(!predVar.isConstant()) {
                 return false;
             }
-            
+
             if(bindings == null ) {
                 bindings = sp.getBindingNames();
                 if(bindings.size() == 0) {
@@ -582,55 +581,56 @@ public class StarQuery {
                     return false;
                 }
             }
-            
+
         }
-        
-        
+
+
         return isBindingsetValid(bindings);
     }
-    
-    
-    
-    
-    
+
+
+
+
+
 //    private static Set<String> getSpVariables(StatementPattern sp) {
-//        
+//
 //        Set<String> variables = Sets.newHashSet();
 //        List<Var> varList = sp.getVarList();
-//        
+//
 //        for(Var v: varList) {
 //            if(!v.isConstant()) {
 //                variables.add(v.getName());
 //            }
 //        }
-//        
+//
 //        return variables;
-//        
+//
 //    }
-//    
-    
-    
-    
-    
-    
-    public String toString() {
-        
+//
+
+
+
+
+
+    @Override
+	public String toString() {
+
         String s = "Term conditions: " + "\n";
-        
-        for(int i = 0; i < this.nodeColumnCond.length; i++) {
-            s = s + nodeColumnCond[i].toString() + "\n";
+
+        for (TextColumn element : this.nodeColumnCond) {
+            s = s + element.toString() + "\n";
         }
-        
-        s = s + "Common Var: " + this.commonVar.toString() + "\n"; 
+
+        s = s + "Common Var: " + this.commonVar.toString() + "\n";
         s = s + "Context: " + this.contextURI;
-        
+
         return s;
-        
+
     }
-    
-    
-    
-    
-    
+
+
+
+
+
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/indexing/accumulo/geo/GeoTupleSet.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/accumulo/geo/GeoTupleSet.java b/extras/indexing/src/main/java/mvm/rya/indexing/accumulo/geo/GeoTupleSet.java
index e7a5d68..2bc1bb0 100644
--- a/extras/indexing/src/main/java/mvm/rya/indexing/accumulo/geo/GeoTupleSet.java
+++ b/extras/indexing/src/main/java/mvm/rya/indexing/accumulo/geo/GeoTupleSet.java
@@ -8,9 +8,9 @@ package mvm.rya.indexing.accumulo.geo;
  * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -31,14 +31,12 @@ import mvm.rya.indexing.IteratorFactory;
 import mvm.rya.indexing.SearchFunction;
 import mvm.rya.indexing.StatementContraints;
 import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
-import mvm.rya.indexing.external.tupleSet.SimpleExternalTupleSet;
 
 import org.apache.hadoop.conf.Configuration;
 import org.openrdf.model.Statement;
 import org.openrdf.model.URI;
 import org.openrdf.query.BindingSet;
 import org.openrdf.query.QueryEvaluationException;
-import org.openrdf.query.algebra.QueryModelVisitor;
 
 import com.google.common.base.Joiner;
 import com.google.common.collect.Maps;
@@ -46,14 +44,14 @@ import com.vividsolutions.jts.geom.Geometry;
 import com.vividsolutions.jts.io.ParseException;
 import com.vividsolutions.jts.io.WKTReader;
 
-//Indexing Node for geo expressions to be inserted into execution plan 
+//Indexing Node for geo expressions to be inserted into execution plan
 //to delegate geo portion of query to geo index
 public class GeoTupleSet extends ExternalTupleSet {
 
     private Configuration conf;
     private GeoIndexer geoIndexer;
     private IndexingExpr filterInfo;
-   
+
 
     public GeoTupleSet(IndexingExpr filterInfo, GeoIndexer geoIndexer) {
         this.filterInfo = filterInfo;
@@ -66,7 +64,8 @@ public class GeoTupleSet extends ExternalTupleSet {
         return filterInfo.getBindingNames();
     }
 
-    public GeoTupleSet clone() {
+    @Override
+	public GeoTupleSet clone() {
         return new GeoTupleSet(filterInfo, geoIndexer);
     }
 
@@ -74,15 +73,15 @@ public class GeoTupleSet extends ExternalTupleSet {
     public double cardinality() {
         return 0.0; // No idea how the estimate cardinality here.
     }
-    
-   
+
+
     @Override
     public String getSignature() {
         return "(GeoTuple Projection) " + "variables: " + Joiner.on(", ").join(this.getBindingNames()).replaceAll("\\s+", " ");
     }
-    
-    
-    
+
+
+
     @Override
     public boolean equals(Object other) {
         if (other == this) {
@@ -94,16 +93,16 @@ public class GeoTupleSet extends ExternalTupleSet {
         GeoTupleSet arg = (GeoTupleSet) other;
         return this.filterInfo.equals(arg.filterInfo);
     }
-    
+
     @Override
     public int hashCode() {
         int result = 17;
         result = 31*result + filterInfo.hashCode();
-        
+
         return result;
     }
-    
-    
+
+
 
     /**
      * Returns an iterator over the result set of the contained IndexingExpr.
@@ -114,37 +113,37 @@ public class GeoTupleSet extends ExternalTupleSet {
     @Override
     public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(BindingSet bindings)
             throws QueryEvaluationException {
-        
-      
+
+
         URI funcURI = filterInfo.getFunction();
-        SearchFunction searchFunction = (new GeoSearchFunctionFactory(conf)).getSearchFunction(funcURI);
+        SearchFunction searchFunction = new GeoSearchFunctionFactory(conf).getSearchFunction(funcURI);
         if(filterInfo.getArguments().length > 1) {
             throw new IllegalArgumentException("Index functions do not support more than two arguments.");
         }
-        
+
         String queryText = filterInfo.getArguments()[0].stringValue();
-        
+
         return IteratorFactory.getIterator(filterInfo.getSpConstraint(), bindings, queryText, searchFunction);
     }
 
 
-    
+
     //returns appropriate search function for a given URI
     //search functions used in GeoMesaGeoIndexer to access index
     public class GeoSearchFunctionFactory {
-        
+
         Configuration conf;
-        
+
         private final Map<URI, SearchFunction> SEARCH_FUNCTION_MAP = Maps.newHashMap();
 
         public GeoSearchFunctionFactory(Configuration conf) {
             this.conf = conf;
         }
-        
+
 
         /**
          * Get a {@link GeoSearchFunction} for a given URI.
-         * 
+         *
          * @param searchFunction
          * @return
          */
@@ -359,6 +358,6 @@ public class GeoTupleSet extends ExternalTupleSet {
         }
 
     }
-   
+
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/indexing/external/ExternalIndexMain.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/external/ExternalIndexMain.java b/extras/indexing/src/main/java/mvm/rya/indexing/external/ExternalIndexMain.java
deleted file mode 100644
index c4e55be..0000000
--- a/extras/indexing/src/main/java/mvm/rya/indexing/external/ExternalIndexMain.java
+++ /dev/null
@@ -1,219 +0,0 @@
-package mvm.rya.indexing.external;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-
-import java.io.File;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-
-import mvm.rya.accumulo.AccumuloRdfConfiguration;
-import mvm.rya.accumulo.AccumuloRyaDAO;
-import mvm.rya.indexing.accumulo.ConfigUtils;
-import mvm.rya.indexing.external.tupleSet.AccumuloIndexSet;
-import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
-import mvm.rya.rdftriplestore.RdfCloudTripleStore;
-
-import org.apache.accumulo.core.client.AccumuloException;
-import org.apache.accumulo.core.client.AccumuloSecurityException;
-import org.apache.accumulo.core.client.Connector;
-import org.apache.accumulo.core.client.Instance;
-import org.apache.accumulo.core.client.Scanner;
-import org.apache.accumulo.core.client.ZooKeeperInstance;
-import org.apache.accumulo.core.data.Key;
-import org.apache.accumulo.core.data.Range;
-import org.apache.accumulo.core.data.Value;
-import org.apache.accumulo.core.security.Authorizations;
-import org.apache.commons.io.FileUtils;
-import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.io.Text;
-import org.openrdf.query.BindingSet;
-import org.openrdf.query.QueryLanguage;
-import org.openrdf.query.QueryResultHandlerException;
-import org.openrdf.query.TupleQueryResultHandler;
-import org.openrdf.query.TupleQueryResultHandlerException;
-import org.openrdf.repository.sail.SailRepository;
-import org.openrdf.repository.sail.SailRepositoryConnection;
-import org.openrdf.sail.Sail;
-
-import com.beust.jcommander.internal.Maps;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Lists;
-
-public class ExternalIndexMain {
-
-    private static String userStr = "";
-    private static String passStr = "";
-
-    private static String instStr = "";
-    private static String zooStr = "";
-
-    private static String tablePrefix = "";
-
-    private static String AUTHS = "";
-
-    public static void main(String[] args) throws Exception {
-        Preconditions.checkArgument(args.length == 6, "java " + ExternalIndexMain.class.getCanonicalName()
-                + " sparqlFile cbinstance cbzk cbuser cbpassword rdfTablePrefix.");
-
-        final String sparqlFile = args[0];
-
-        instStr = args[1];
-        zooStr = args[2];
-        userStr = args[3];
-        passStr = args[4];
-        tablePrefix = args[5];
-
-        String queryString = FileUtils.readFileToString(new File(sparqlFile));
-
-
-        // Look for Extra Indexes
-        Instance inst = new ZooKeeperInstance(instStr, zooStr);
-        Connector c = inst.getConnector(userStr, passStr.getBytes());
-
-        System.out.println("Searching for Indexes");
-        Map<String, String> indexTables = Maps.newLinkedHashMap();
-        for (String table : c.tableOperations().list()) {
-            if (table.startsWith(tablePrefix + "INDEX_")) {
-                Scanner s = c.createScanner(table, new Authorizations());
-                s.setRange(Range.exact(new Text("~SPARQL")));
-                for (Entry<Key, Value> e : s) {
-                    indexTables.put(table, e.getValue().toString());
-                }
-            }
-        }
-
-        List<ExternalTupleSet> index = Lists.newArrayList();
-
-        if (indexTables.isEmpty()) {
-            System.out.println("No Index found");
-        } else {
-            for (String table : indexTables.keySet()) {
-                String indexSparqlString = indexTables.get(table);
-                System.out.println("====================== INDEX FOUND ======================");
-                System.out.println(" table : " + table);
-                System.out.println(" sparql : ");
-                System.out.println(indexSparqlString);
-
-                index.add(new AccumuloIndexSet(indexSparqlString, c, table));
-            }
-        }
-
-        // Connect to Rya
-        Sail s = getRyaSail();
-        SailRepository repo = new SailRepository(s);
-        repo.initialize();
-
-        // Perform Query
-
-        CountingTupleQueryResultHandler count = new CountingTupleQueryResultHandler();
-
-        SailRepositoryConnection conn;
-        if (index.isEmpty()) {
-            conn = repo.getConnection();
-
-        } else {
-            ExternalProcessor processor = new ExternalProcessor(index);
-
-            Sail processingSail = new ExternalSail(s, processor);
-            SailRepository smartSailRepo = new SailRepository(processingSail);
-            smartSailRepo.initialize();
-
-            conn = smartSailRepo.getConnection();
-        }
-
-        startTime = System.currentTimeMillis();
-        lastTime = startTime;
-        System.out.println("Query Started");
-        conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(count);
-
-        System.out.println("Count of Results found : " + count.i);
-        System.out.println("Total query time (s) : " + (System.currentTimeMillis() - startTime) / 1000.);
-    }
-
-    static long lastTime = 0;
-    static long startTime = 0;
-
-    private static class CountingTupleQueryResultHandler implements TupleQueryResultHandler {
-        public int i = 0;
-
-        @Override
-        public void handleBoolean(boolean value) throws QueryResultHandlerException {
-        }
-
-        @Override
-        public void handleLinks(List<String> linkUrls) throws QueryResultHandlerException {
-        }
-
-        @Override
-        public void startQueryResult(List<String> bindingNames) throws TupleQueryResultHandlerException {
-            System.out.println("First Result Recieved (s) : " + (System.currentTimeMillis() - startTime) / 1000.);
-        }
-
-        @Override
-        public void endQueryResult() throws TupleQueryResultHandlerException {
-        }
-
-        @Override
-        public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException {
-            i++;
-            if (i % 10 == 0) {
-                long mark = System.currentTimeMillis();
-                System.out.println("Count : " + i + ". Time (s) : " + (mark - lastTime) / 1000.);
-                lastTime = mark;
-            }
-
-        }
-
-    }
-
-    private static Configuration getConf() {
-
-        Configuration conf = new Configuration();
-
-        conf.set(ConfigUtils.CLOUDBASE_USER, userStr);
-        conf.set(ConfigUtils.CLOUDBASE_PASSWORD, passStr);
-
-        conf.set(ConfigUtils.CLOUDBASE_INSTANCE, instStr);
-        conf.set(ConfigUtils.CLOUDBASE_ZOOKEEPERS, zooStr);
-
-        conf.set(ConfigUtils.CLOUDBASE_AUTHS, AUTHS);
-        conf.setBoolean(ConfigUtils.DISPLAY_QUERY_PLAN, true);
-        return conf;
-    }
-
-    private static Sail getRyaSail() throws AccumuloException, AccumuloSecurityException {
-
-        Connector connector = ConfigUtils.getConnector(getConf());
-
-        final RdfCloudTripleStore store = new RdfCloudTripleStore();
-        AccumuloRyaDAO crdfdao = new AccumuloRyaDAO();
-        crdfdao.setConnector(connector);
-
-        AccumuloRdfConfiguration conf = new AccumuloRdfConfiguration(getConf());
-        conf.setTablePrefix(tablePrefix);
-        crdfdao.setConf(conf);
-        store.setRyaDAO(crdfdao);
-
-        return store;
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/indexing/external/ExternalProcessor.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/external/ExternalProcessor.java b/extras/indexing/src/main/java/mvm/rya/indexing/external/ExternalProcessor.java
deleted file mode 100644
index 2c6d924..0000000
--- a/extras/indexing/src/main/java/mvm/rya/indexing/external/ExternalProcessor.java
+++ /dev/null
@@ -1,726 +0,0 @@
-package mvm.rya.indexing.external;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import mvm.rya.indexing.external.QueryVariableNormalizer.VarCollector;
-import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
-
-import org.openrdf.query.algebra.BindingSetAssignment;
-import org.openrdf.query.algebra.Filter;
-import org.openrdf.query.algebra.Join;
-import org.openrdf.query.algebra.Projection;
-import org.openrdf.query.algebra.QueryModelNode;
-import org.openrdf.query.algebra.StatementPattern;
-import org.openrdf.query.algebra.TupleExpr;
-import org.openrdf.query.algebra.Var;
-import org.openrdf.query.algebra.helpers.QueryModelVisitorBase;
-import org.openrdf.query.algebra.helpers.StatementPatternCollector;
-
-import com.google.common.collect.BiMap;
-import com.google.common.collect.HashBiMap;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
-
-/**
- * Processes a {@link TupleExpr} and replaces sets of elements in the tree with {@link ExternalTupleSet} objects.
- */
-public class ExternalProcessor {
-
-    private List<ExternalTupleSet> indexSet;
-
-    public ExternalProcessor(List<ExternalTupleSet> indexSet) {
-        this.indexSet = indexSet;
-    }
-
-    /**
-     * Iterates through list of indexes and replaces all subtrees of query which match index with external tuple object built from index.
-     * 
-     * @param query
-     * @return TupleExpr
-     */
-    public TupleExpr process(TupleExpr query) {
-        TupleExpr rtn = query.clone();
-
-        
-        //move BindingSetAssignment Nodes out of the way
-        organizeBSAs(rtn);
-        
-        
-        // test to see if query contains no other nodes
-        // than filter, join, projection, and statement pattern and
-        // test whether query contains duplicate StatementPatterns and filters
-        if (isTupleValid(rtn)) {
-
-            for (ExternalTupleSet index : indexSet) {
-
-                // test to see if index contains at least one StatementPattern,
-                // that StatementPatterns are unique,
-                // and that all variables found in filters occur in some
-                // StatementPattern
-                if (isTupleValid(index.getTupleExpr())) {
-
-                    List<TupleExpr> normalize = getMatches(rtn, index.getTupleExpr());
-                    
-                    for (TupleExpr tup : normalize) {
-                        ExternalTupleSet eTup = (ExternalTupleSet) index.clone();
-                        setTableMap(tup, eTup);
-                        setSupportedVarOrderMap(eTup);
-                        eTup.setProjectionExpr((Projection) tup);
-                        SPBubbleDownVisitor indexVistor = new SPBubbleDownVisitor(eTup);
-                        rtn.visit(indexVistor);
-                        FilterBubbleManager fbmv = new FilterBubbleManager(eTup);
-                        rtn.visit(fbmv);
-                        SubsetEqualsVisitor subIndexVis = new SubsetEqualsVisitor(eTup);
-                        rtn.visit(subIndexVis);
-
-                    }
-                }
-
-            }
-          
-            return rtn;
-        } else {
-            throw new IllegalArgumentException("Invalid Query.");
-        }
-    }
-    
-    
-    private void setTableMap(TupleExpr tupleMatch, ExternalTupleSet index) {
-        
-        List<String> replacementVars = Lists.newArrayList(tupleMatch.getBindingNames());
-        List<String> tableVars = Lists.newArrayList(index.getTupleExpr().getBindingNames());
-        
-        Map<String,String> tableMap = Maps.newHashMap();
-        
-        for(int i = 0; i < tableVars.size(); i++) {
-            tableMap.put(replacementVars.get(i), tableVars.get(i));
-        }
-        index.setTableVarMap(tableMap);
-          
-        
-    }
-    
-    private void setSupportedVarOrderMap(ExternalTupleSet index) {
-
-        Map<String, Set<String>> supportedVarOrders = Maps.newHashMap();
-        BiMap<String, String> biMap = HashBiMap.create(index.getTableVarMap()).inverse();
-        Map<String, Set<String>> oldSupportedVarOrders = index.getSupportedVariableOrderMap();
-
-        Set<String> temp = null;
-        Set<String> keys = oldSupportedVarOrders.keySet();
-
-        for (String s : keys) {
-            temp = oldSupportedVarOrders.get(s);
-            Set<String> newSet = Sets.newHashSet();
-
-            for (String t : temp) {
-                newSet.add(biMap.get(t));
-            }
-            
-            String[] tempStrings = s.split("\u0000");
-            String v = "";
-            for(String u: tempStrings) {
-                if(v.length() == 0){
-                    v = v + biMap.get(u);
-                } else {
-                    v = v + "\u0000" + biMap.get(u);
-                }
-            }
-
-            supportedVarOrders.put(v, newSet);
-
-        }
-
-        index.setSupportedVariableOrderMap(supportedVarOrders);
-
-    }
-    
-    
-
-    private List<TupleExpr> getMatches(TupleExpr query, TupleExpr tuple) {
-
-        try {
-            List<TupleExpr> list = QueryVariableNormalizer.getNormalizedIndex(query, tuple);
-           // System.out.println("Match list is " + list);
-            return list;
-        } catch (Exception e) {
-            System.out.println(e);
-        }
-
-        return new ArrayList<TupleExpr>();
-
-    }
-
-    // determines whether query is valid, which requires that a
-    // query must contain a StatementPattern, not contain duplicate
-    // Statement Patterns or Filters, not be comprised of only Projection,
-    // Join, StatementPattern, and Filter nodes, and that any variable
-    // appearing in a Filter must appear in a StatementPattern.
-    private static boolean isTupleValid(QueryModelNode node) {
-
-        ValidQueryVisitor vqv = new ValidQueryVisitor();
-        node.visit(vqv);
-
-        Set<String> spVars = getVarNames(getQNodes("sp", node));
-
-        if (vqv.isValid() && (spVars.size() > 0)) {
-
-            FilterCollector fvis = new FilterCollector();
-            node.visit(fvis);
-            List<QueryModelNode> fList = fvis.getFilters();
-            return (fList.size() == Sets.newHashSet(fList).size() && getVarNames(fList).size() <= spVars.size());
-
-        } else {
-            return false;
-        }
-    }
-
-    private static Set<QueryModelNode> getQNodes(QueryModelNode queryNode) {
-        Set<QueryModelNode> rtns = new HashSet<QueryModelNode>();
-
-        StatementPatternCollector spc = new StatementPatternCollector();
-        queryNode.visit(spc);
-        rtns.addAll(spc.getStatementPatterns());
-
-        FilterCollector fvis = new FilterCollector();
-        queryNode.visit(fvis);
-        rtns.addAll(fvis.getFilters());
-
-        ExternalTupleCollector eVis = new ExternalTupleCollector();
-        queryNode.visit(eVis);
-        rtns.addAll(eVis.getExtTup());
-
-        return rtns;
-    }
-
-    private static Set<QueryModelNode> getQNodes(String node, QueryModelNode queryNode) {
-
-        if (node.equals("sp")) {
-            Set<QueryModelNode> eSet = new HashSet<QueryModelNode>();
-            StatementPatternCollector spc = new StatementPatternCollector();
-            queryNode.visit(spc);
-            List<StatementPattern> spList = spc.getStatementPatterns();
-            eSet.addAll(spList);
-            // returns empty set if list contains duplicate StatementPatterns
-            if (spList.size() > eSet.size()) {
-                return Sets.newHashSet();
-            } else {
-                return eSet;
-            }
-        } else if (node.equals("filter")) {
-
-            FilterCollector fvis = new FilterCollector();
-            queryNode.visit(fvis);
-
-            return Sets.newHashSet(fvis.getFilters());
-        } else {
-
-            throw new IllegalArgumentException("Invalid node type.");
-        }
-    }
-
-    // moves StatementPatterns in query that also occur in index to bottom of
-    // query tree.
-    private static class SPBubbleDownVisitor extends QueryModelVisitorBase<RuntimeException> {
-
-        private TupleExpr tuple;
-        private QueryModelNode indexQNode;
-        private Set<QueryModelNode> sSet = Sets.newHashSet();
-
-        public SPBubbleDownVisitor(ExternalTupleSet index) {
-
-            this.tuple = index.getTupleExpr();
-            indexQNode = ((Projection) tuple).getArg();
-            sSet = getQNodes("sp", indexQNode);
-
-        }
-
-        public void meet(Projection node) {
-            // moves external tuples above statement patterns before attempting
-            // to bubble down index statement patterns found in query tree
-            
-            organizeExtTuples(node);
-            
-            super.meet(node);
-        }
-
-        public void meet(Join node) {
-            // if right node contained in index, move it to bottom of query tree
-            if (sSet.contains(node.getRightArg())) {
-
-                Set<QueryModelNode> eSet = getQNodes("sp", node);
-                Set<QueryModelNode> compSet = Sets.difference(eSet, sSet);
-
-                if (eSet.containsAll(sSet)) {
-
-                    QNodeExchanger qne = new QNodeExchanger(node.getRightArg(), compSet);
-                    node.visit(qne);
-                    node.replaceChildNode(node.getRightArg(), qne.getReplaced());
-
-                    super.meet(node);
-                }
-                return;
-            }
-            // if left node contained in index, move it to bottom of query tree
-            else if (sSet.contains(node.getLeftArg())) {
-
-                Set<QueryModelNode> eSet = getQNodes("sp", node);
-                Set<QueryModelNode> compSet = Sets.difference(eSet, sSet);
-
-                if (eSet.containsAll(sSet)) {
-
-                    QNodeExchanger qne = new QNodeExchanger(node.getLeftArg(), compSet);
-                    node.visit(qne);
-                    node.replaceChildNode(node.getLeftArg(), qne.getReplaced());
-
-                    super.meet(node);
-                }
-                return;
-
-            } else {
-                super.meet(node);
-            }
-
-        }
-
-        // moves all ExternalTupleSets in query tree above remaining
-        // StatementPatterns
-        private static void organizeExtTuples(QueryModelNode node) {
-
-            ExternalTupleCollector eVis = new ExternalTupleCollector();
-            node.visit(eVis);
-
-            ExtTupleExchangeVisitor oev = new ExtTupleExchangeVisitor(eVis.getExtTup());
-            node.visit(oev);
-        }
-
-    }
-
-    // given a replacement QueryModelNode and compSet, this visitor replaces the
-    // first
-    // element in the query tree that occurs in compSet with replacement and
-    // returns
-    // the element that was replaced.
-    private static class QNodeExchanger extends QueryModelVisitorBase<RuntimeException> {
-
-        private QueryModelNode toBeReplaced;
-        private QueryModelNode replacement;
-        private Set<QueryModelNode> compSet;
-
-        public QNodeExchanger(QueryModelNode replacement, Set<QueryModelNode> compSet) {
-            this.replacement = replacement;
-            this.toBeReplaced = replacement;
-            this.compSet = compSet;
-        }
-
-        public QueryModelNode getReplaced() {
-            return toBeReplaced;
-        }
-
-        public void meet(Join node) {
-
-            if (compSet.contains(node.getRightArg())) {
-                this.toBeReplaced = node.getRightArg();
-                node.replaceChildNode(node.getRightArg(), replacement);
-                return;
-            } else if (compSet.contains(node.getLeftArg())) {
-                this.toBeReplaced = node.getLeftArg();
-                node.replaceChildNode(node.getLeftArg(), replacement);
-                return;
-            } else {
-                super.meet(node);
-            }
-
-        }
-
-    }
-
-    // moves filter that occurs in both query and index down the query tree so
-    // that that it is positioned
-    // above statement patterns associated with index. Precondition for calling
-    // this method is that
-    // SPBubbleDownVisitor has been called to position index StatementPatterns
-    // within query tree.
-    //TODO this visitor assumes that all filters are positioned at top of query tree
-    //could lead to problems if filter optimizer called before external processor
-    private static class FilterBubbleDownVisitor extends QueryModelVisitorBase<RuntimeException> {
-
-        private QueryModelNode filter;
-        private Set<QueryModelNode> compSet;
-        private boolean filterPlaced = false;
-
-        public FilterBubbleDownVisitor(QueryModelNode filter, Set<QueryModelNode> compSet) {
-            this.filter = filter;
-            this.compSet = compSet;
-
-        }
-
-        public boolean filterPlaced() {
-            return filterPlaced;
-        }
-
-        public void meet(Join node) {
-
-            if (!compSet.contains(node.getRightArg())) {
-                // looks for placed to position filter node. if right node is
-                // contained in index
-                // and left node is statement pattern node contained in index or
-                // is a join, place
-                // filter above join.
-                if (node.getLeftArg() instanceof Join || !(compSet.contains(node.getLeftArg()))) {
-                    
-                    QueryModelNode pNode = node.getParentNode();
-                    ((Filter) filter).setArg(node);
-                    pNode.replaceChildNode(node, filter);
-                    filterPlaced = true;
-                    
-                    return;
-                } // otherwise place filter below join and above right arg
-                else {
-                    ((Filter) filter).setArg(node.getRightArg());
-                    node.replaceChildNode(node.getRightArg(), filter);
-                    filterPlaced = true;
-                    return;
-
-                }
-            } else if ((node.getLeftArg() instanceof StatementPattern) && !compSet.contains(node.getLeftArg())) {
-                
-                ((Filter) filter).setArg(node.getLeftArg());
-                node.replaceChildNode(node.getLeftArg(), filter);
-                filterPlaced = true;
-                
-                return;
-            } else {
-                super.meet(node);
-            }
-        }
-
-    }
-
-    private static Set<String> getVarNames(Collection<QueryModelNode> nodes) {
-
-        List<String> tempVars;
-        Set<String> nodeVarNames = Sets.newHashSet();
-
-        for (QueryModelNode s : nodes) {
-            tempVars = VarCollector.process(s);
-            for (String t : tempVars)
-                nodeVarNames.add(t);
-        }
-        return nodeVarNames;
-
-    }
-
-    // visitor which determines whether or not to reposition a filter by calling
-    // FilterBubbleDownVisitor
-    private static class FilterBubbleManager extends QueryModelVisitorBase<RuntimeException> {
-
-        private TupleExpr tuple;
-        private QueryModelNode indexQNode;
-        private Set<QueryModelNode> sSet = Sets.newHashSet();
-        private Set<QueryModelNode> bubbledFilters = Sets.newHashSet();
-
-        public FilterBubbleManager(ExternalTupleSet index) {
-            this.tuple = index.getTupleExpr();
-            indexQNode = ((Projection) tuple).getArg();
-            sSet = getQNodes(indexQNode);
-
-        }
-
-        public void meet(Filter node) {
-
-            Set<QueryModelNode> eSet = getQNodes(node);
-            Set<QueryModelNode> compSet = Sets.difference(eSet, sSet);
-
-            // if index contains filter node and it hasn't already been moved,
-            // move it down
-            // query tree just above position of statement pattern nodes found
-            // in both query tree
-            // and index (assuming that SPBubbleDownVisitor has already been
-            // called)
-            if (sSet.contains(node.getCondition()) && !bubbledFilters.contains(node.getCondition())) {
-                FilterBubbleDownVisitor fbdv = new FilterBubbleDownVisitor((Filter) node.clone(), compSet);
-                node.visit(fbdv);
-                bubbledFilters.add(node.getCondition());
-                // checks if filter correctly placed, and if it has been,
-                // removes old copy of filter
-                if (fbdv.filterPlaced()) {
-
-                    QueryModelNode pNode = node.getParentNode();
-                    TupleExpr cNode = node.getArg();
-                    pNode.replaceChildNode(node, cNode);
-                   
-
-                    super.meetNode(pNode);
-                }
-                super.meet(node);
-
-            } else {
-                super.meet(node);
-            }
-        }
-    }
-
-    // iterates through the query tree and attempts to match subtrees with
-    // index. When a match is
-    // found, the subtree is replaced by an ExternalTupleSet formed from the
-    // index. Pre-condition for
-    // calling this method is that both SPBubbleDownVisitor and
-    // FilterBubbleManager have been called
-    // to position the StatementPatterns and Filters.
-    private static class SubsetEqualsVisitor extends QueryModelVisitorBase<RuntimeException> {
-
-        private TupleExpr tuple;
-        private QueryModelNode indexQNode;
-        private ExternalTupleSet set;
-        private Set<QueryModelNode> sSet = Sets.newHashSet();
-
-        public SubsetEqualsVisitor(ExternalTupleSet index) {
-            this.tuple = index.getTupleExpr();
-            this.set = index;
-            indexQNode = ((Projection) tuple).getArg();
-            sSet = getQNodes(indexQNode);
-
-        }
-
-        public void meet(Join node) {
-
-            Set<QueryModelNode> eSet = getQNodes(node);
-
-            if (eSet.containsAll(sSet) && !(node.getRightArg() instanceof BindingSetAssignment)) {
-
-//                System.out.println("Eset is " + eSet + " and sSet is " + sSet);
-                
-                if (eSet.equals(sSet)) {
-                    node.replaceWith(set);
-                    return;
-                } else {
-                    if (node.getLeftArg() instanceof StatementPattern && sSet.size() == 1) {
-                        if(sSet.contains(node.getLeftArg())) {
-                            node.setLeftArg(set);
-                        } else if(sSet.contains(node.getRightArg())) {
-                            node.setRightArg(set);
-                        } else {
-                            return;
-                        }
-                    }
-                    else {
-                        super.meet(node);
-                    }
-                }
-            } else if (eSet.containsAll(sSet)) {
-
-                super.meet(node);
-
-            }
-
-        }
-      //TODO might need to include BindingSetAssignment Condition here
-      //to account for index consisting of only filter and BindingSetAssignment nodes
-        public void meet(Filter node) {
-
-            Set<QueryModelNode> eSet = getQNodes(node);
-
-            if (eSet.containsAll(sSet)) {
-
-                if (eSet.equals(sSet)) {
-                    node.replaceWith(set);
-                    return;
-                } else {
-                    super.meet(node);
-                }
-            }
-        }
-    }
-
-    // visitor which determines whether a query is valid (i.e. it does not
-    // contain nodes other than
-    // Projection, Join, Filter, StatementPattern )
-    private static class ValidQueryVisitor extends QueryModelVisitorBase<RuntimeException> {
-
-        private boolean isValid = true;
-
-        public boolean isValid() {
-            return isValid;
-        }
-
-        public void meet(Projection node) {
-            node.getArg().visit(this);
-        }
-
-        public void meet(Filter node) {
-            node.getArg().visit(this);
-        }
-        
-      
-       
-        
-       
-        public void meetNode(QueryModelNode node) {
-
-            if (!((node instanceof Join) || (node instanceof StatementPattern) || (node instanceof BindingSetAssignment) || (node instanceof Var))) {
-                isValid = false;
-                return;
-           
-            } else{
-                super.meetNode(node);
-            }
-        }
-
-    }
-
-    // repositions ExternalTuples above StatementPatterns within query tree
-    private static class ExtTupleExchangeVisitor extends QueryModelVisitorBase<RuntimeException> {
-
-        private Set<QueryModelNode> extTuples;
-
-        public ExtTupleExchangeVisitor(Set<QueryModelNode> extTuples) {
-            this.extTuples = extTuples;
-        }
-
-        public void meet(Join queryNode) {
-
-            // if query tree contains external tuples and they are not
-            // positioned above statement pattern node
-            // reposition
-            if (this.extTuples.size() > 0 && !(queryNode.getRightArg() instanceof ExternalTupleSet)
-                    && !(queryNode.getRightArg() instanceof BindingSetAssignment)) {
-                QNodeExchanger qnev = new QNodeExchanger((QueryModelNode) queryNode.getRightArg(), this.extTuples);
-                queryNode.visit(qnev);
-                queryNode.setRightArg((TupleExpr)qnev.getReplaced());
-                super.meet(queryNode);
-            } else {
-                super.meet(queryNode);
-            }
-
-        }
-
-    }
-
-    private static class ExternalTupleCollector extends QueryModelVisitorBase<RuntimeException> {
-
-        private Set<QueryModelNode> eSet = new HashSet<QueryModelNode>();
-
-        @Override
-        public void meetNode(QueryModelNode node) throws RuntimeException {
-            if (node instanceof ExternalTupleSet) {
-                eSet.add(node);
-            }
-            super.meetNode(node);
-        }
-
-        public Set<QueryModelNode> getExtTup() {
-            return eSet;
-        }
-
-    }
-
-    private static class FilterCollector extends QueryModelVisitorBase<RuntimeException> {
-
-        private List<QueryModelNode> filterList = Lists.newArrayList();
-
-        public List<QueryModelNode> getFilters() {
-            return filterList;
-        }
-
-        @Override
-        public void meet(Filter node) {
-            filterList.add(node.getCondition());
-            super.meet(node);
-        }
-
-    }
-
-    private static void organizeBSAs(QueryModelNode node) {
-
-        BindingSetAssignmentCollector bsac = new BindingSetAssignmentCollector();
-        node.visit(bsac);
-
-        if (bsac.containsBSAs()) {
-            Set<QueryModelNode> bsaSet = bsac.getBindingSetAssignments();
-            BindingSetAssignmentExchangeVisitor bsaev = new BindingSetAssignmentExchangeVisitor(bsaSet);
-            node.visit(bsaev);
-        }
-    }
-
-    // repositions ExternalTuples above StatementPatterns within query tree
-    private static class BindingSetAssignmentExchangeVisitor extends QueryModelVisitorBase<RuntimeException> {
-
-        private Set<QueryModelNode> bsas;
-
-        public BindingSetAssignmentExchangeVisitor(Set<QueryModelNode> bsas) {
-            this.bsas = bsas;
-        }
-
-        public void meet(Join queryNode) {
-
-            // if query tree contains external tuples and they are not
-            // positioned above statement pattern node
-            // reposition
-            if (this.bsas.size() > 0 && !(queryNode.getRightArg() instanceof BindingSetAssignment)) {
-                QNodeExchanger qnev = new QNodeExchanger((QueryModelNode) queryNode.getRightArg(), bsas);
-                queryNode.visit(qnev);
-                queryNode.replaceChildNode(queryNode.getRightArg(), qnev.getReplaced());
-                super.meet(queryNode);
-            } else {
-                super.meet(queryNode);
-            }
-
-        }
-
-    }
-        
-        
-    public static class BindingSetAssignmentCollector extends QueryModelVisitorBase<RuntimeException> {
-
-        private Set<QueryModelNode> bindingSetList = Sets.newHashSet();
-
-        public Set<QueryModelNode> getBindingSetAssignments() {
-            return bindingSetList;
-        }
-
-        public boolean containsBSAs() {
-            return (bindingSetList.size() > 0);
-        }
-
-        @Override
-        public void meet(BindingSetAssignment node) {
-            bindingSetList.add(node);
-            super.meet(node);
-        }
-
-    }
-
-    // TODO insert BindingSetAssignments at bottom of query tree --this approach assumes
-    // BindingSetAssignments always removed during creation of ExternalTupleSets within
-    // query. There may be cases where this precondition does not hold (all BindingSetAssignments
-    // not removed). For now assuming it always holds.
-   
-}

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/indexing/external/ExternalSail.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/external/ExternalSail.java b/extras/indexing/src/main/java/mvm/rya/indexing/external/ExternalSail.java
deleted file mode 100644
index 772ffa4..0000000
--- a/extras/indexing/src/main/java/mvm/rya/indexing/external/ExternalSail.java
+++ /dev/null
@@ -1,86 +0,0 @@
-package mvm.rya.indexing.external;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-
-import info.aduna.iteration.CloseableIteration;
-
-import org.openrdf.model.ValueFactory;
-import org.openrdf.query.BindingSet;
-import org.openrdf.query.Dataset;
-import org.openrdf.query.QueryEvaluationException;
-import org.openrdf.query.algebra.Projection;
-import org.openrdf.query.algebra.QueryRoot;
-import org.openrdf.query.algebra.TupleExpr;
-import org.openrdf.sail.Sail;
-import org.openrdf.sail.SailConnection;
-import org.openrdf.sail.SailException;
-import org.openrdf.sail.helpers.SailBase;
-import org.openrdf.sail.helpers.SailConnectionWrapper;
-
-public class ExternalSail extends SailBase {
-    private final Sail s;
-    private final ExternalProcessor processor;
-
-    public ExternalSail(Sail s, ExternalProcessor processor) {
-        this.s = s;
-        this.processor = processor;
-    }
-
-    @Override
-    protected SailConnection getConnectionInternal() throws SailException {
-        return new ProcessingSailConnection();
-    }
-
-    @Override
-    public boolean isWritable() throws SailException {
-        return s.isWritable();
-    }
-
-    @Override
-    public ValueFactory getValueFactory() {
-        return s.getValueFactory();
-    }
-
-    @Override
-    protected void shutDownInternal() throws SailException {
-        s.shutDown();
-    }
-
-    private class ProcessingSailConnection extends SailConnectionWrapper {
-
-        public ProcessingSailConnection() throws SailException {
-            super(s.getConnection());
-        }
-
-        @Override
-        public CloseableIteration<? extends BindingSet, QueryEvaluationException> evaluate(TupleExpr tupleExpr, Dataset dataset,
-                BindingSet bindings, boolean includeInferred) throws SailException {
-            if ((tupleExpr instanceof Projection) || (tupleExpr instanceof QueryRoot)) {
-                TupleExpr processedExpression = processor.process(tupleExpr);
-                return super.evaluate(processedExpression, dataset, bindings, includeInferred);
-            } else {
-                return super.evaluate(tupleExpr, dataset, bindings, includeInferred);
-            }
-            
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/main/java/mvm/rya/indexing/external/ExternalSailExample.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/external/ExternalSailExample.java b/extras/indexing/src/main/java/mvm/rya/indexing/external/ExternalSailExample.java
deleted file mode 100644
index 082dd99..0000000
--- a/extras/indexing/src/main/java/mvm/rya/indexing/external/ExternalSailExample.java
+++ /dev/null
@@ -1,124 +0,0 @@
-package mvm.rya.indexing.external;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-
-import java.util.List;
-
-import mvm.rya.indexing.external.tupleSet.AccumuloIndexSet;
-import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
-
-import org.apache.accumulo.core.client.Connector;
-import org.apache.accumulo.core.client.mock.MockInstance;
-import org.openrdf.model.URI;
-import org.openrdf.model.impl.LiteralImpl;
-import org.openrdf.model.impl.URIImpl;
-import org.openrdf.model.vocabulary.RDF;
-import org.openrdf.model.vocabulary.RDFS;
-import org.openrdf.query.QueryLanguage;
-import org.openrdf.query.algebra.helpers.QueryModelTreePrinter;
-import org.openrdf.query.parser.ParsedQuery;
-import org.openrdf.query.parser.sparql.SPARQLParser;
-import org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLWriter;
-import org.openrdf.repository.sail.SailRepository;
-import org.openrdf.repository.sail.SailRepositoryConnection;
-import org.openrdf.sail.Sail;
-import org.openrdf.sail.memory.MemoryStore;
-
-import com.google.common.collect.Lists;
-
-public class ExternalSailExample {
-
-    public static void main(String[] args) throws Exception {
-
-        Sail s = new MemoryStore();
-        SailRepository repo = new SailRepository(s);
-        repo.initialize();
-        SailRepositoryConnection conn = repo.getConnection();
-
-        URI sub = new URIImpl("uri:entity");
-        URI subclass = new URIImpl("uri:class");
-        URI obj = new URIImpl("uri:obj");
-        URI talksTo = new URIImpl("uri:talksTo");
-
-        conn.add(sub, RDF.TYPE, subclass);
-        conn.add(sub, RDFS.LABEL, new LiteralImpl("label"));
-        conn.add(sub, talksTo, obj);
-
-        URI sub2 = new URIImpl("uri:entity2");
-        URI subclass2 = new URIImpl("uri:class2");
-        URI obj2 = new URIImpl("uri:obj2");
-
-        conn.add(sub2, RDF.TYPE, subclass2);
-        conn.add(sub2, RDFS.LABEL, new LiteralImpl("label2"));
-        conn.add(sub2, talksTo, obj2);
-
-        // TODO Auto-generated method stub
-        String indexSparqlString = ""//
-                + "SELECT ?e ?l ?c " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l "//
-                + "}";//
-
-        conn.prepareTupleQuery(QueryLanguage.SPARQL, indexSparqlString).evaluate(new SPARQLResultsXMLWriter(System.out));
-
-        SPARQLParser sp = new SPARQLParser();
-        ParsedQuery pq = sp.parseQuery(indexSparqlString, null);
-        System.out.println(pq);
-
-        List<ExternalTupleSet> index = Lists.newArrayList();
-
-        Connector accCon = new MockInstance().getConnector("root", "".getBytes());
-        String tablename = "table";
-        accCon.tableOperations().create(tablename);
-        index.add(new AccumuloIndexSet(indexSparqlString, conn, accCon, tablename));
-
-        String queryString = ""//
-                + "SELECT ?e ?c ?l ?o " //
-                + "{" //
-                + "  ?e a ?c . "//
-                + "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
-                + "  ?e <uri:talksTo> ?o . "//
-                + "}";//
-
-        conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(new SPARQLResultsXMLWriter(System.out));
-
-        pq = sp.parseQuery(queryString, null);
-        QueryModelTreePrinter mp = new QueryModelTreePrinter();
-        pq.getTupleExpr().visit(mp);
-        System.out.println(mp.getTreeString());
-        System.out.println(pq.getTupleExpr());
-
-        System.out.println("++++++++++++");
-        ExternalProcessor processor = new ExternalProcessor(index);
-        System.out.println(processor.process(pq.getTupleExpr()));
-
-        System.out.println("----------------");
-        Sail processingSail = new ExternalSail(s, processor);
-        SailRepository smartSailRepo = new SailRepository(processingSail);
-        smartSailRepo.initialize();
-
-        smartSailRepo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(new SPARQLResultsXMLWriter(System.out));
-
-    }
-
-}


[04/16] incubator-rya git commit: RYA-32 Improve how metadata and values are written to Accumulo PCJ tables

Posted by mi...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerTest2.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerTest2.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerTest2.java
new file mode 100644
index 0000000..ea173bb
--- /dev/null
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerTest2.java
@@ -0,0 +1,1130 @@
+package mvm.rya.indexing.external;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+import mvm.rya.indexing.external.PcjIntegrationTestingUtil.BindingSetAssignmentCollector;
+import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
+import mvm.rya.indexing.external.tupleSet.SimpleExternalTupleSet;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.openrdf.query.algebra.Projection;
+import org.openrdf.query.algebra.QueryModelNode;
+import org.openrdf.query.algebra.StatementPattern;
+import org.openrdf.query.algebra.TupleExpr;
+import org.openrdf.query.algebra.helpers.StatementPatternCollector;
+import org.openrdf.query.parser.ParsedQuery;
+import org.openrdf.query.parser.sparql.SPARQLParser;
+
+import com.google.common.collect.Sets;
+
+public class PrecompJoinOptimizerTest2 {
+
+	private final String queryString = ""//
+			+ "SELECT ?e ?c ?l ?o " //
+			+ "{" //
+			+ "  ?e a ?c . "//
+			+ "  ?c a ?l . "//
+			+ "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . "//
+			+ "  ?e <uri:talksTo> ?o  "//
+			+ "}";//
+
+	private final String indexSparqlString = ""//
+			+ "SELECT ?x ?y ?z " //
+			+ "{" //
+			+ "  ?x <http://www.w3.org/2000/01/rdf-schema#label> ?z. "//
+			+ "  ?x a ?y . "//
+			+ "  ?y a ?z  "//
+			+ "}";//
+
+	private final String q1 = ""//
+			+ "SELECT ?e ?l ?c " //
+			+ "{" //
+			+ "  ?e a ?c . "//
+			+ "  ?c <http://www.w3.org/2000/01/rdf-schema#label> ?l. "//
+			+ "  ?l <uri:talksTo> ?e . "//
+			+ "}";//
+
+	private final String q2 = ""//
+			+ "SELECT ?a ?t ?v  " //
+			+ "{" //
+			+ "  ?a a ?t . "//
+			+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?v . "//
+			+ "  ?v <uri:talksTo> ?a . "//
+			+ "}";//
+
+	private final String q5 = ""//
+			+ "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r " //
+			+ "{" //
+			+ "  ?f a ?m ."//
+			+ "  ?e a ?l ."//
+			+ "  ?n a ?o ."//
+			+ "  ?a a ?h ."//
+			+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+			+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+			+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
+			+ "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
+			+ "  ?d <uri:talksTo> ?f . "//
+			+ "  ?c <uri:talksTo> ?e . "//
+			+ "  ?p <uri:talksTo> ?n . "//
+			+ "  ?r <uri:talksTo> ?a . "//
+			+ "}";//
+
+	private final String q7 = ""//
+			+ "SELECT ?s ?t ?u " //
+			+ "{" //
+			+ "  ?s a ?t ."//
+			+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+			+ "  ?u <uri:talksTo> ?s . "//
+			+ "}";//
+
+	private final String q8 = ""//
+			+ "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r " //
+			+ "{" //
+			+ "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
+			+ "  ?f a ?m ."//
+			+ "  ?p <uri:talksTo> ?n . "//
+			+ "  ?e a ?l ."//
+			+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
+			+ "  ?d <uri:talksTo> ?f . "//
+			+ "  ?c <uri:talksTo> ?e . "//
+			+ "  ?n a ?o ."//
+			+ "  ?a a ?h ."//
+			+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+			+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+			+ "  ?r <uri:talksTo> ?a . "//
+			+ "}";//
+
+	private final String q11 = ""//
+			+ "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r ?x ?y ?w ?t ?duck ?chicken ?pig ?rabbit " //
+			+ "{" //
+			+ "  ?w a ?t ."//
+			+ "  ?x a ?y ."//
+			+ "  ?duck a ?chicken ."//
+			+ "  ?pig a ?rabbit ."//
+			+ "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
+			+ "  ?f a ?m ."//
+			+ "  ?p <uri:talksTo> ?n . "//
+			+ "  ?e a ?l ."//
+			+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
+			+ "  ?d <uri:talksTo> ?f . "//
+			+ "  ?c <uri:talksTo> ?e . "//
+			+ "  ?n a ?o ."//
+			+ "  ?a a ?h ."//
+			+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+			+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+			+ "  ?r <uri:talksTo> ?a . "//
+			+ "}";//
+
+	private final String q12 = ""//
+			+ "SELECT ?b ?p ?dog ?cat " //
+			+ "{" //
+			+ "  ?b a ?p ."//
+			+ "  ?dog a ?cat. "//
+			+ "}";//
+
+	private final String q13 = ""//
+			+ "SELECT ?f ?m ?d ?e ?l ?c ?n ?o ?p ?a ?h ?r ?x ?y ?w ?t ?duck ?chicken ?pig ?rabbit ?dick ?jane ?betty " //
+			+ "{" //
+			+ "  ?w a ?t ."//
+			+ "  ?x a ?y ."//
+			+ "  ?duck a ?chicken ."//
+			+ "  ?pig a ?rabbit ."//
+			+ "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?r ."//
+			+ "  ?f a ?m ."//
+			+ "  ?p <uri:talksTo> ?n . "//
+			+ "  ?e a ?l ."//
+			+ "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?p ."//
+			+ "  ?d <uri:talksTo> ?f . "//
+			+ "  ?c <uri:talksTo> ?e . "//
+			+ "  ?n a ?o ."//
+			+ "  ?a a ?h ."//
+			+ "  ?m <http://www.w3.org/2000/01/rdf-schema#label> ?d ."//
+			+ "  ?l <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+			+ "  ?r <uri:talksTo> ?a . "//
+			+ "  ?dick <uri:talksTo> ?jane . "//
+			+ "  ?jane <uri:talksTo> ?betty . "//
+			+ "}";//
+
+	private final String q14 = ""//
+			+ "SELECT ?harry ?susan ?mary " //
+			+ "{" //
+			+ "  ?harry <uri:talksTo> ?susan . "//
+			+ "  ?susan <uri:talksTo> ?mary . "//
+			+ "}";//
+
+	String q15 = ""//
+			+ "SELECT ?a ?b ?c ?d ?e ?f ?q " //
+			+ "{" //
+			+ " GRAPH ?x { " //
+			+ "  ?a a ?b ."//
+			+ "  ?b <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+			+ "  ?d <uri:talksTo> ?e . "//
+			+ "  FILTER ( ?e < ?f && (?a > ?b || ?c = ?d) ). " //
+			+ "  FILTER(bound(?f) && sameTerm(?a,?b)&&bound(?q)). " //
+			+ "  ?b a ?q ."//
+			+ "		}"//
+			+ "}";//
+
+	String q16 = ""//
+			+ "SELECT ?g ?h ?i " //
+			+ "{" //
+			+ " GRAPH ?y { " //
+			+ "  ?g a ?h ."//
+			+ "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?i ."//
+			+ "		}"//
+			+ "}";//
+
+	String q17 = ""//
+			+ "SELECT ?j ?k ?l ?m ?n ?o " //
+			+ "{" //
+			+ " GRAPH ?z { " //
+			+ "  ?j <uri:talksTo> ?k . "//
+			+ "  FILTER ( ?k < ?l && (?m > ?n || ?o = ?j) ). " //
+			+ "		}"//
+			+ "}";//
+
+	String q18 = ""//
+			+ "SELECT ?r ?s ?t ?u " //
+			+ "{" //
+			+ " GRAPH ?q { " //
+			+ "  FILTER(bound(?r) && sameTerm(?s,?t)&&bound(?u)). " //
+			+ "  ?t a ?u ."//
+			+ "		}"//
+			+ "}";//
+
+	String q19 = ""//
+			+ "SELECT ?a ?b ?c ?d ?e ?f ?q ?g ?h " //
+			+ "{" //
+			+ " GRAPH ?x { " //
+			+ "  ?a a ?b ."//
+			+ "  ?b <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+			+ "  ?d <uri:talksTo> ?e . "//
+			+ "  FILTER ( ?e < ?f && (?a > ?b || ?c = ?d) ). " //
+			+ "  FILTER(bound(?f) && sameTerm(?a,?b)&&bound(?q)). " //
+			+ "  FILTER(?g IN (1,2,3) && ?h NOT IN(5,6,7)). " //
+			+ "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?g. "//
+			+ "  ?b a ?q ."//
+			+ "		}"//
+			+ "}";//
+
+	String q20 = ""//
+			+ "SELECT ?m ?n " //
+			+ "{" //
+			+ " GRAPH ?q { " //
+			+ "  FILTER(?m IN (1,2,3) && ?n NOT IN(5,6,7)). " //
+			+ "  ?n <http://www.w3.org/2000/01/rdf-schema#label> ?m. "//
+			+ "		}"//
+			+ "}";//
+
+	String q21 = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
+			+ "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
+			+ "SELECT ?feature ?point ?wkt " //
+			+ "{" //
+			+ "  ?feature a geo:Feature . "//
+			+ "  ?feature geo:hasGeometry ?point . "//
+			+ "  ?point a geo:Point . "//
+			+ "  ?point geo:asWKT ?wkt . "//
+			+ "  FILTER(geof:sfWithin(?wkt, \"Polygon\")) " //
+			+ "}";//
+
+	String q22 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
+			+ "SELECT ?person ?commentmatch ?labelmatch" //
+			+ "{" //
+			+ "  ?person a <http://example.org/ontology/Person> . "//
+			+ "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?labelmatch . "//
+			+ "  ?person <http://www.w3.org/2000/01/rdf-schema#comment> ?commentmatch . "//
+			+ "  FILTER(fts:text(?labelmatch, \"bob\")) . " //
+			+ "  FILTER(fts:text(?commentmatch, \"bob\"))  " //
+			+ "}";//
+
+	String q23 = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
+			+ "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
+			+ "SELECT ?a ?b ?c " //
+			+ "{" //
+			+ "  ?a a geo:Feature . "//
+			+ "  ?b a geo:Point . "//
+			+ "  ?b geo:asWKT ?c . "//
+			+ "  FILTER(geof:sfWithin(?c, \"Polygon\")) " //
+			+ "}";//
+
+	String q24 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
+			+ "SELECT ?f ?g " //
+			+ "{" //
+			+ "  ?f <http://www.w3.org/2000/01/rdf-schema#comment> ?g . "//
+			+ "  FILTER(fts:text(?g, \"bob\"))  " //
+			+ "}";//
+
+	String q25 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
+			+ "SELECT ?person ?commentmatch ?labelmatch ?point" //
+			+ "{" //
+			+ "  ?person a ?point. " //
+			+ "  ?person a <http://example.org/ontology/Person> . "//
+			+ "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?labelmatch . "//
+			+ "  ?person <http://www.w3.org/2000/01/rdf-schema#comment> ?commentmatch . "//
+			+ "  FILTER((?person > ?point) || (?person = ?labelmatch)). "
+			+ "  FILTER(fts:text(?labelmatch, \"bob\")) . " //
+			+ "  FILTER(fts:text(?commentmatch, \"bob\"))  " //
+			+ "}";//
+
+	String q26 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
+			+ "SELECT ?a ?b ?c  " //
+			+ "{" //
+			+ "  ?a a ?c. " //
+			+ "  ?a a <http://example.org/ontology/Person> . "//
+			+ "  ?a <http://www.w3.org/2000/01/rdf-schema#label> ?b . "//
+			+ "  FILTER((?a > ?c) || (?a = ?b)). "
+			+ "  FILTER(fts:text(?b, \"bob\")) . " //
+			+ "}";//
+
+	String q27 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
+			+ "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
+			+ "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
+			+ "SELECT ?person ?commentmatch ?labelmatch ?other ?feature ?point ?wkt ?g ?h" //
+			+ "{" //
+			+ "  ?person a <http://example.org/ontology/Person> . "//
+			+ "  ?person <http://www.w3.org/2000/01/rdf-schema#label> ?labelmatch . "//
+			+ "  ?person <http://www.w3.org/2000/01/rdf-schema#comment> ?commentmatch . "//
+			+ "  FILTER((?person > ?other) || (?person = ?labelmatch)). "
+			+ "  ?person a ?other. "//
+			+ "  FILTER(fts:text(?labelmatch, \"bob\")) . " //
+			+ "  FILTER(fts:text(?commentmatch, \"bob\"))  " //
+			+ " ?feature a geo:Feature . "//
+			+ "  ?point a geo:Point . "//
+			+ "  ?point geo:asWKT ?wkt . "//
+			+ "  FILTER(geof:sfWithin(?wkt, \"Polygon\")) " //
+			+ "  FILTER(?g IN (1,2,3) && ?h NOT IN(5,6,7)). " //
+			+ "  ?h <http://www.w3.org/2000/01/rdf-schema#label> ?g. "//
+			+ "}";//
+
+	String q28 = ""//
+			+ "SELECT ?m ?n " //
+			+ "{" //
+			+ "  FILTER(?m IN (1,2,3) && ?n NOT IN(5,6,7)). " //
+			+ "  ?n <http://www.w3.org/2000/01/rdf-schema#label> ?m. "//
+			+ "}";//
+
+	String q29 = ""//
+			+ "SELECT ?m ?n ?o" //
+			+ "{" //
+			+ "  FILTER(?m IN (1,2,3) && ?n NOT IN(5,6,7)). " //
+			+ "  ?n <http://www.w3.org/2000/01/rdf-schema#label> ?m. "//
+			+ "  ?m a ?o." //
+			+ "  FILTER(ISNUMERIC(?o))." + "}";//
+
+	String q30 = ""//
+			+ "SELECT ?pig ?dog ?owl" //
+			+ "{" //
+			+ "  FILTER(?pig IN (1,2,3) && ?dog NOT IN(5,6,7)). " //
+			+ "  ?dog <http://www.w3.org/2000/01/rdf-schema#label> ?pig. "//
+			+ "  ?pig a ?owl. " //
+			+ "  FILTER(ISNUMERIC(?owl))." + "}";//
+
+	String q31 = ""//
+			+ "SELECT ?q ?r ?s " //
+			+ "{" //
+			+ "  {?q a ?r} UNION {?r a ?s} ."//
+			+ "  ?r a ?s ."//
+			+ "}";//
+
+	String q33 = ""//
+			+ "SELECT ?q ?r ?s ?t " //
+			+ "{" //
+			+ "  OPTIONAL {?q a ?r} ."//
+			+ "  ?s a ?t ."//
+			+ "}";//
+
+	String q34 = ""//
+			+ "SELECT ?q ?r  " //
+			+ "{" //
+			+ "  FILTER(?q > ?r) ."//
+			+ "  ?q a ?r ."//
+			+ "}";//
+
+	String q35 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
+			+ "SELECT ?s ?t ?u ?v ?w ?x ?y ?z " //
+			+ "{" //
+			+ "  FILTER(?s > ?t)."//
+			+ "  ?s a ?t ."//
+			+ "  FILTER(?u > ?v)."//
+			+ "  ?u a ?v ."//
+			+ "  ?w <http://www.w3.org/2000/01/rdf-schema#label> ?x ."//
+			+ "  FILTER(fts:text(?x, \"bob\")) . " //
+			+ "  ?y <http://www.w3.org/2000/01/rdf-schema#label> ?z ."//
+			+ "  FILTER(fts:text(?z, \"bob\")) . " //
+			+ "}";//
+
+	String q36 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
+			+ "SELECT ?dog ?cat  " //
+			+ "{" //
+			+ "  ?dog <http://www.w3.org/2000/01/rdf-schema#label> ?cat ."//
+			+ "  FILTER(fts:text(?cat, \"bob\")) . " //
+			+ "}";//
+
+	String q37 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
+			+ "SELECT ?s ?t " //
+			+ "{" //
+			+ "  FILTER(?s > ?t)."//
+			+ "  ?s a ?t ."//
+			+ "  FILTER(?s > ?t)."//
+			+ "  ?s a ?t ."//
+			+ "  FILTER(?s > ?t)."//
+			+ "  ?s a ?t ."//
+			+ "}";//
+
+	String q38 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
+			+ "SELECT ?s ?t " //
+			+ "{" //
+			+ "  FILTER(?s > ?t)."//
+			+ "  ?s a ?t ."//
+			+ "  ?t <http://www.w3.org/2000/01/rdf-schema#label> ?s ."//
+			+ "  FILTER(?s > ?t)."//
+			+ "}";//
+
+	String q39 = "PREFIX fts: <http://rdf.useekm.com/fts#> "//
+			+ "SELECT ?s ?t " //
+			+ "{" //
+			+ " VALUES(?s) { (<ub:poodle>)(<ub:pitbull>)} ." //
+			+ " ?t <ub:peesOn> <ub:rug> ." //
+			+ " ?t <http://www.w3.org/2000/01/rdf-schema#label> ?s ."//
+			+ "}";//
+
+	String q40 = "PREFIX fts: <http://rdf.useekm.com/fts#> "//
+			+ "SELECT ?u ?v " //
+			+ "{" //
+			+ " ?v <ub:peesOn> <ub:rug> ." //
+			+ " ?v <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+			+ "}";//
+
+	String q41 = "PREFIX fts: <http://rdf.useekm.com/fts#> "//
+			+ "SELECT ?s ?t ?w ?x" //
+			+ "{" //
+			+ " FILTER(?s > ?t)."//
+			+ " VALUES(?s) { (<ub:poodle>)(<ub:pitbull>)} ." //
+			+ " VALUES(?w) { (<ub:persian>) (<ub:siamese>) } ." //
+			+ " ?t <ub:peesOn> <ub:rug> ." //
+			+ " ?t <http://www.w3.org/2000/01/rdf-schema#label> ?s ."//
+			+ " ?w <ub:peesOn> <ub:rug> ." //
+			+ " ?w <http://www.w3.org/2000/01/rdf-schema#label> ?x ."//
+			+ "}";//
+
+	String q42 = "PREFIX fts: <http://rdf.useekm.com/fts#> "//
+			+ "SELECT ?u ?v " //
+			+ "{" //
+			+ " FILTER(?u > ?v)."//
+			+ " ?v <ub:peesOn> <ub:rug> ." //
+			+ " ?v <http://www.w3.org/2000/01/rdf-schema#label> ?u ."//
+			+ "}";//
+
+	String q43 = "PREFIX fts: <http://rdf.useekm.com/fts#> "//
+			+ "SELECT ?a ?b " //
+			+ "{" //
+			+ " ?b <ub:peesOn> <ub:rug> ." //
+			+ " ?b <http://www.w3.org/2000/01/rdf-schema#label> ?a ."//
+			+ "}";//
+
+	@Test
+	public void testVarRelableIndexSmaller() throws Exception {
+
+		final SPARQLParser parser1 = new SPARQLParser();
+		final SPARQLParser parser2 = new SPARQLParser();
+
+		final ParsedQuery pq1 = parser1.parseQuery(queryString, null);
+		final ParsedQuery pq2 = parser2.parseQuery(indexSparqlString, null);
+
+		System.out.println("Query is " + pq1.getTupleExpr());
+		System.out.println("Index is " + pq2.getTupleExpr());
+
+		final SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(
+				new Projection(pq2.getTupleExpr()));
+
+		final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+		list.add(extTup);
+
+		final TupleExpr tup = pq1.getTupleExpr().clone();
+		final PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, false);
+		pcj.optimize(tup, null, null);
+
+		final Set<StatementPattern> qSet = Sets
+				.newHashSet(StatementPatternCollector.process(pq1
+						.getTupleExpr()));
+		final Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
+				.getTupleSets(tup);
+
+		final Set<StatementPattern> set = Sets.newHashSet();
+		for (final QueryModelNode s : eTupSet) {
+			set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
+					.getTupleExpr()));
+		}
+
+		Assert.assertTrue(qSet.containsAll(set) && set.size() != 0);
+	}
+
+	@Test
+	public void testVarRelableIndexSameSize() throws Exception {
+
+		final SPARQLParser parser1 = new SPARQLParser();
+		final SPARQLParser parser2 = new SPARQLParser();
+
+		final ParsedQuery pq1 = parser1.parseQuery(q1, null);
+		final ParsedQuery pq2 = parser2.parseQuery(q2, null);
+
+		final SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(
+				new Projection(pq2.getTupleExpr()));
+
+		final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+		list.add(extTup);
+
+		final TupleExpr tup = pq1.getTupleExpr().clone();
+		final PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, false);
+		pcj.optimize(tup, null, null);
+
+		final Set<StatementPattern> qSet = Sets
+				.newHashSet(StatementPatternCollector.process(pq1
+						.getTupleExpr()));
+		final Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
+				.getTupleSets(tup);
+
+		final Set<StatementPattern> set = Sets.newHashSet();
+		for (final QueryModelNode s : eTupSet) {
+			set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
+					.getTupleExpr()));
+		}
+
+		Assert.assertTrue(set.equals(qSet));
+
+	}
+
+	@Test
+	public void testTwoIndexLargeQuery() throws Exception {
+
+		final SPARQLParser parser1 = new SPARQLParser();
+		final SPARQLParser parser2 = new SPARQLParser();
+		final SPARQLParser parser3 = new SPARQLParser();
+
+		final ParsedQuery pq1 = parser1.parseQuery(q11, null);
+		final ParsedQuery pq2 = parser2.parseQuery(q7, null);
+		final ParsedQuery pq3 = parser3.parseQuery(q12, null);
+
+		System.out.println("Query is " + pq1.getTupleExpr());
+		System.out.println("Indexes are " + pq2.getTupleExpr() + " and "
+				+ pq3.getTupleExpr());
+
+		final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+		final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				(Projection) pq3.getTupleExpr());
+
+		final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+		list.add(extTup1);
+		list.add(extTup2);
+
+		TupleExpr tup = pq1.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, false);
+		pcj.optimize(tup, null, null);
+
+		Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector
+				.process(pq1.getTupleExpr()));
+		Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
+				.getTupleSets(tup);
+
+		Set<StatementPattern> set = Sets.newHashSet();
+		for (QueryModelNode s : eTupSet) {
+			set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
+					.getTupleExpr()));
+		}
+
+		Assert.assertTrue(set.equals(qSet));
+
+	}
+
+	@Test
+	public void testThreeIndexLargeQuery() throws Exception {
+
+		final SPARQLParser parser1 = new SPARQLParser();
+		final SPARQLParser parser2 = new SPARQLParser();
+		final SPARQLParser parser3 = new SPARQLParser();
+		final SPARQLParser parser4 = new SPARQLParser();
+
+		final ParsedQuery pq1 = parser1.parseQuery(q13, null);
+		final ParsedQuery pq2 = parser2.parseQuery(q5, null);
+		final ParsedQuery pq3 = parser3.parseQuery(q12, null);
+		final ParsedQuery pq4 = parser4.parseQuery(q14, null);
+
+		System.out.println("Query is " + pq1.getTupleExpr());
+		System.out.println("Indexes are " + pq2.getTupleExpr() + " , "
+				+ pq3.getTupleExpr() + " , " + pq4.getTupleExpr());
+
+		final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+		final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				(Projection) pq3.getTupleExpr());
+		final SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(
+				(Projection) pq4.getTupleExpr());
+
+		final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+		list.add(extTup1);
+		list.add(extTup2);
+		list.add(extTup3);
+
+		TupleExpr tup = pq1.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, false);
+		pcj.optimize(tup, null, null);
+
+		Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector
+				.process(pq1.getTupleExpr()));
+		Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
+				.getTupleSets(tup);
+
+		Set<StatementPattern> set = Sets.newHashSet();
+		for (QueryModelNode s : eTupSet) {
+			set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
+					.getTupleExpr()));
+		}
+
+		Assert.assertTrue(set.equals(qSet));
+
+	}
+
+	@Test
+	public void testSingleIndexLargeQuery() throws Exception {
+
+		final SPARQLParser parser1 = new SPARQLParser();
+		final SPARQLParser parser2 = new SPARQLParser();
+
+		final ParsedQuery pq1 = parser1.parseQuery(q8, null);
+		final ParsedQuery pq2 = parser2.parseQuery(q7, null);
+
+		final SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(
+				new Projection(pq2.getTupleExpr()));
+
+		final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+		list.add(extTup);
+
+		TupleExpr tup = pq1.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, false);
+		pcj.optimize(tup, null, null);
+
+		Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector
+				.process(pq1.getTupleExpr()));
+		Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
+				.getTupleSets(tup);
+
+		Set<StatementPattern> set = Sets.newHashSet();
+		for (QueryModelNode s : eTupSet) {
+			set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
+					.getTupleExpr()));
+		}
+
+		Assert.assertTrue(set.equals(qSet));
+
+	}
+
+	@Test
+	public void testContextFilter1() throws Exception {
+
+		final SPARQLParser parser1 = new SPARQLParser();
+		final SPARQLParser parser2 = new SPARQLParser();
+		final SPARQLParser parser3 = new SPARQLParser();
+		final SPARQLParser parser4 = new SPARQLParser();
+
+		final ParsedQuery pq1 = parser1.parseQuery(q15, null);
+		final ParsedQuery pq2 = parser2.parseQuery(q16, null);
+		final ParsedQuery pq3 = parser3.parseQuery(q17, null);
+		final ParsedQuery pq4 = parser4.parseQuery(q18, null);
+
+		System.out.println("Query is " + pq1.getTupleExpr());
+		System.out.println("Indexes are " + pq2.getTupleExpr() + " , "
+				+ pq3.getTupleExpr() + " , " + pq4.getTupleExpr());
+
+		final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+		final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				(Projection) pq3.getTupleExpr());
+		final SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(
+				(Projection) pq4.getTupleExpr());
+
+		final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+		list.add(extTup1);
+		list.add(extTup2);
+		list.add(extTup3);
+
+		TupleExpr tup = pq1.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, false);
+		pcj.optimize(tup, null, null);
+
+		Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector
+				.process(pq1.getTupleExpr()));
+		Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
+				.getTupleSets(tup);
+
+		Set<StatementPattern> set = Sets.newHashSet();
+		for (QueryModelNode s : eTupSet) {
+			set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
+					.getTupleExpr()));
+		}
+
+		Assert.assertTrue(qSet.containsAll(set) && eTupSet.size() == 1);
+	}
+
+	@Test
+	public void testGeoFilter() throws Exception {
+
+		final SPARQLParser parser1 = new SPARQLParser();
+		final SPARQLParser parser2 = new SPARQLParser();
+
+		String query1 = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
+				+ "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
+				+ "SELECT ?a ?b ?c " //
+				+ "{" //
+				+ "  ?a a geo:Feature . "//
+				+ "  ?b a geo:Point . "//
+				+ "  ?b geo:asWKT ?c . "//
+				+ "  FILTER(geof:sfWithin(?b, \"Polygon\")) " //
+				+ "}";//
+
+		String query2 = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
+				+ "SELECT ?f ?g " //
+				+ "{" //
+				+ "  ?f a geo:Feature . "//
+				+ "  ?g a geo:Point . "//
+				+ "}";//
+
+		final ParsedQuery pq1 = parser1.parseQuery(query1, null);
+		final ParsedQuery pq2 = parser2.parseQuery(query2, null);
+
+		final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+
+		final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+		list.add(extTup1);
+
+		TupleExpr tup = pq1.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, false);
+		pcj.optimize(tup, null, null);
+
+		Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector
+				.process(pq1.getTupleExpr()));
+		Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
+				.getTupleSets(tup);
+
+		Set<StatementPattern> set = Sets.newHashSet();
+		for (QueryModelNode s : eTupSet) {
+			set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
+					.getTupleExpr()));
+		}
+
+		Assert.assertTrue(qSet.containsAll(set) && eTupSet.size() == 1);
+	}
+
+	@Test
+	public void testContextFilter2() throws Exception {
+
+		final SPARQLParser parser1 = new SPARQLParser();
+		final SPARQLParser parser2 = new SPARQLParser();
+
+		String query1 = ""//
+				+ "SELECT ?k ?l ?m ?n " //
+				+ "{" //
+				+ " GRAPH ?z { " //
+				+ " ?l <uri:talksTo> ?n . "//
+				+ " ?l a ?n."//
+				+ " ?k a ?m."//
+				+ "  FILTER ((?k < ?l) && (?m < ?n)). " //
+				+ "		}"//
+				+ "}";//
+
+		String query2 = ""//
+				+ "SELECT ?s ?t " //
+				+ "{" //
+				+ " GRAPH ?r { " //
+				+ " ?s <uri:talksTo> ?t . "//
+				+ " ?s a ?t."//
+				+ "	}"//
+				+ "}";//
+
+		final ParsedQuery pq1 = parser1.parseQuery(query1, null);
+		final ParsedQuery pq2 = parser2.parseQuery(query2, null);
+
+		final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+
+		final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+		list.add(extTup1);
+
+		TupleExpr tup = pq1.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, false);
+		pcj.optimize(tup, null, null);
+
+		Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector
+				.process(pq1.getTupleExpr()));
+		Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
+				.getTupleSets(tup);
+
+		Set<StatementPattern> set = Sets.newHashSet();
+		for (QueryModelNode s : eTupSet) {
+			set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
+					.getTupleExpr()));
+		}
+
+		Assert.assertTrue(qSet.containsAll(set) && eTupSet.size() == 1);
+	}
+
+	@Test
+	public void testGeoIndexFunction() throws Exception {
+
+		final SPARQLParser parser1 = new SPARQLParser();
+		final SPARQLParser parser2 = new SPARQLParser();
+
+		final ParsedQuery pq1 = parser1.parseQuery(q21, null);
+		final ParsedQuery pq2 = parser2.parseQuery(q23, null);
+
+		System.out.println("Query is " + pq1.getTupleExpr());
+		System.out.println("Index is " + pq2.getTupleExpr());
+
+		final SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(
+				new Projection(pq2.getTupleExpr()));
+
+		final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+		list.add(extTup);
+
+		TupleExpr tup = pq1.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, false);
+		pcj.optimize(tup, null, null);
+
+		Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector
+				.process(pq1.getTupleExpr()));
+		Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
+				.getTupleSets(tup);
+
+		Set<StatementPattern> set = Sets.newHashSet();
+		for (QueryModelNode s : eTupSet) {
+			set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
+					.getTupleExpr()));
+		}
+
+		Assert.assertTrue(qSet.containsAll(set) && set.size() != 0);
+
+	}
+
+	@Test
+	public void testFreeTextIndexFunction() throws Exception {
+
+		final SPARQLParser parser1 = new SPARQLParser();
+		final SPARQLParser parser2 = new SPARQLParser();
+
+		final ParsedQuery pq1 = parser1.parseQuery(q22, null);
+		final ParsedQuery pq2 = parser2.parseQuery(q24, null);
+
+		System.out.println("Query is " + pq1.getTupleExpr());
+		System.out.println("Index is " + pq2.getTupleExpr());
+
+		final SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(
+				(Projection) pq2.getTupleExpr());
+
+		final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+		list.add(extTup);
+
+		TupleExpr tup = pq1.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, false);
+		pcj.optimize(tup, null, null);
+
+		Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector
+				.process(pq1.getTupleExpr()));
+		Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
+				.getTupleSets(tup);
+
+		Set<StatementPattern> set = Sets.newHashSet();
+		for (QueryModelNode s : eTupSet) {
+			set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
+					.getTupleExpr()));
+		}
+
+		Assert.assertTrue(qSet.containsAll(set) && set.size() != 0);
+
+	}
+
+	@Test
+	public void testThreeIndexGeoFreeCompareFilterMix() throws Exception {
+
+		final SPARQLParser parser1 = new SPARQLParser();
+		final SPARQLParser parser2 = new SPARQLParser();
+		final SPARQLParser parser3 = new SPARQLParser();
+
+		final ParsedQuery pq1 = parser1.parseQuery(q25, null);
+		final ParsedQuery pq2 = parser2.parseQuery(q24, null);
+		final ParsedQuery pq3 = parser3.parseQuery(q26, null);
+
+		final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				new Projection(pq2.getTupleExpr()));
+		final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				new Projection(pq3.getTupleExpr()));
+
+		final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+		list.add(extTup1);
+		list.add(extTup2);
+
+		TupleExpr tup = pq1.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, false);
+		pcj.optimize(tup, null, null);
+
+		Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector
+				.process(pq1.getTupleExpr()));
+		Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
+				.getTupleSets(tup);
+
+		Set<StatementPattern> set = Sets.newHashSet();
+		for (QueryModelNode s : eTupSet) {
+			set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
+					.getTupleExpr()));
+		}
+
+		Assert.assertTrue(set.equals(qSet) && eTupSet.size() == 2);
+
+	}
+
+	@Test
+	public void testFourIndexGeoFreeCompareFilterMix() throws Exception {
+
+		final SPARQLParser parser1 = new SPARQLParser();
+		final SPARQLParser parser2 = new SPARQLParser();
+		final SPARQLParser parser3 = new SPARQLParser();
+		final SPARQLParser parser4 = new SPARQLParser();
+		final SPARQLParser parser5 = new SPARQLParser();
+
+		final ParsedQuery pq1 = parser1.parseQuery(q27, null);
+		final ParsedQuery pq2 = parser2.parseQuery(q23, null);
+		final ParsedQuery pq3 = parser3.parseQuery(q26, null);
+		final ParsedQuery pq4 = parser4.parseQuery(q24, null);
+		final ParsedQuery pq5 = parser5.parseQuery(q28, null);
+
+		System.out.println("Query is " + pq1.getTupleExpr());
+		System.out.println("Indexes are " + pq2.getTupleExpr() + " , "
+				+ pq3.getTupleExpr() + " , " + pq4.getTupleExpr() + " and "
+				+ pq5.getTupleExpr());
+
+		final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				new Projection(pq2.getTupleExpr()));
+		final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				new Projection(pq3.getTupleExpr()));
+		final SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(
+				new Projection(pq4.getTupleExpr()));
+		final SimpleExternalTupleSet extTup4 = new SimpleExternalTupleSet(
+				new Projection(pq5.getTupleExpr()));
+
+		final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+		list.add(extTup4);
+		list.add(extTup1);
+		list.add(extTup2);
+		list.add(extTup3);
+
+		TupleExpr tup = pq1.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, false);
+		pcj.optimize(tup, null, null);
+
+		Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector
+				.process(pq1.getTupleExpr()));
+		Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
+				.getTupleSets(tup);
+
+		Set<StatementPattern> set = Sets.newHashSet();
+		for (QueryModelNode s : eTupSet) {
+			set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
+					.getTupleExpr()));
+		}
+
+		Assert.assertTrue(set.equals(qSet));
+
+	}
+
+	@Test
+	public void testThreeIndexGeoFreeCompareFilterMix2() throws Exception {
+
+		final SPARQLParser parser1 = new SPARQLParser();
+		final SPARQLParser parser2 = new SPARQLParser();
+		final SPARQLParser parser3 = new SPARQLParser();
+		final SPARQLParser parser4 = new SPARQLParser();
+
+		final ParsedQuery pq1 = parser1.parseQuery(q27, null);
+		final ParsedQuery pq2 = parser2.parseQuery(q23, null);
+		final ParsedQuery pq3 = parser3.parseQuery(q26, null);
+		final ParsedQuery pq4 = parser4.parseQuery(q28, null);
+
+		final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				new Projection(pq2.getTupleExpr()));
+		final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				new Projection(pq3.getTupleExpr()));
+		final SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(
+				new Projection(pq4.getTupleExpr()));
+
+		final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+
+		list.add(extTup1);
+		list.add(extTup3);
+		list.add(extTup2);
+
+		TupleExpr tup = pq1.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, false);
+		pcj.optimize(tup, null, null);
+
+		Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector
+				.process(pq1.getTupleExpr()));
+		Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
+				.getTupleSets(tup);
+
+		Set<StatementPattern> set = Sets.newHashSet();
+		for (QueryModelNode s : eTupSet) {
+			set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
+					.getTupleExpr()));
+		}
+
+		Assert.assertTrue(qSet.containsAll(set));
+
+	}
+
+	@Test
+	public void testISNUMERIC() throws Exception {
+
+		final SPARQLParser parser1 = new SPARQLParser();
+		final SPARQLParser parser2 = new SPARQLParser();
+
+		final ParsedQuery pq1 = parser1.parseQuery(q29, null);
+		final ParsedQuery pq2 = parser2.parseQuery(q30, null);
+
+		final SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(
+				new Projection(pq2.getTupleExpr()));
+
+		final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+		list.add(extTup);
+
+		TupleExpr tup = pq1.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, false);
+		pcj.optimize(tup, null, null);
+
+		Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector
+				.process(pq1.getTupleExpr()));
+		Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
+				.getTupleSets(tup);
+
+		Set<StatementPattern> set = Sets.newHashSet();
+		for (QueryModelNode s : eTupSet) {
+			set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
+					.getTupleExpr()));
+		}
+
+		Assert.assertTrue(set.equals(qSet) && eTupSet.size() == 1);
+
+	}
+
+	@Test
+	public void testTwoRepeatedIndex() throws Exception {
+
+		final SPARQLParser parser1 = new SPARQLParser();
+		final SPARQLParser parser2 = new SPARQLParser();
+		final SPARQLParser parser3 = new SPARQLParser();
+
+		final ParsedQuery pq1 = parser1.parseQuery(q35, null);
+		final ParsedQuery pq2 = parser2.parseQuery(q34, null);
+		final ParsedQuery pq3 = parser3.parseQuery(q36, null);
+
+		System.out.println("Query is " + pq1.getTupleExpr());
+		System.out.println("Indexes are " + pq2.getTupleExpr() + " and "
+				+ pq3.getTupleExpr());
+
+		final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				new Projection(pq2.getTupleExpr()));
+		final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				new Projection(pq3.getTupleExpr()));
+
+		final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+		list.add(extTup1);
+		list.add(extTup2);
+
+		TupleExpr tup = pq1.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, false);
+		pcj.optimize(tup, null, null);
+
+		Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector
+				.process(pq1.getTupleExpr()));
+		Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
+				.getTupleSets(tup);
+
+		Set<StatementPattern> set = Sets.newHashSet();
+		for (QueryModelNode s : eTupSet) {
+			set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
+					.getTupleExpr()));
+		}
+
+		Assert.assertEquals(4, eTupSet.size());
+		Assert.assertEquals(qSet, set);
+
+	}
+
+	@Test
+	public void testBindingSetAssignment2() throws Exception {
+
+		final SPARQLParser parser = new SPARQLParser();
+
+		final ParsedQuery pq1 = parser.parseQuery(q41, null);
+		final ParsedQuery pq2 = parser.parseQuery(q42, null);
+		final ParsedQuery pq3 = parser.parseQuery(q43, null);
+
+		final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(
+				new Projection(pq2.getTupleExpr()));
+		final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(
+				new Projection(pq3.getTupleExpr()));
+
+		final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+		list.add(extTup1);
+		list.add(extTup2);
+
+		TupleExpr tup = pq1.getTupleExpr().clone();
+		PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, false);
+		pcj.optimize(tup, null, null);
+
+		Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector
+				.process(pq1.getTupleExpr()));
+		Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil
+				.getTupleSets(tup);
+
+		Set<StatementPattern> set = Sets.newHashSet();
+		for (QueryModelNode s : eTupSet) {
+			set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s)
+					.getTupleExpr()));
+		}
+
+		Assert.assertEquals(2, eTupSet.size());
+		Assert.assertEquals(qSet, set);
+
+		BindingSetAssignmentCollector bsac1 = new BindingSetAssignmentCollector();
+		BindingSetAssignmentCollector bsac2 = new BindingSetAssignmentCollector();
+		pq1.getTupleExpr().visit(bsac1);
+		tup.visit(bsac2);
+
+		Assert.assertEquals(bsac1.getBindingSetAssignments(),
+				bsac2.getBindingSetAssignments());
+
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/c12f58f4/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerVarToConstTest.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerVarToConstTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerVarToConstTest.java
new file mode 100644
index 0000000..bae935c
--- /dev/null
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/external/PrecompJoinOptimizerVarToConstTest.java
@@ -0,0 +1,430 @@
+package mvm.rya.indexing.external;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+import mvm.rya.indexing.external.PrecompJoinOptimizer;
+import mvm.rya.indexing.external.tupleSet.ExternalTupleSet;
+import mvm.rya.indexing.external.tupleSet.SimpleExternalTupleSet;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.openrdf.query.algebra.Projection;
+import org.openrdf.query.algebra.QueryModelNode;
+import org.openrdf.query.algebra.StatementPattern;
+import org.openrdf.query.algebra.TupleExpr;
+import org.openrdf.query.algebra.helpers.StatementPatternCollector;
+import org.openrdf.query.parser.ParsedQuery;
+import org.openrdf.query.parser.sparql.SPARQLParser;
+
+import com.google.common.collect.Sets;
+
+public class PrecompJoinOptimizerVarToConstTest {
+
+
+
+
+    String q15 = ""//
+            + "SELECT ?a ?b ?c ?d ?e ?f ?q " //
+            + "{" //
+            + " GRAPH ?x { " //
+            + "  ?a a ?b ."//
+            + "  ?b <http://www.w3.org/2000/01/rdf-schema#label> ?c ."//
+            + "  ?d <uri:talksTo> ?e . "//
+            + "  FILTER ( ?e < ?f && (?a > ?b || ?c = ?d) ). " //
+            + "  FILTER(bound(?f) && sameTerm(?a,?b)&&bound(?q)). " //
+            + "  ?b a ?q ."//
+            + "     }"//
+            + "}";//
+
+
+
+
+    String q17 = ""//
+            + "SELECT ?j ?k ?l ?m ?n ?o " //
+            + "{" //
+            + " GRAPH ?z { " //
+            + "  ?l a ?m. " //
+            + "  ?n a ?o. " //
+            + "  ?j <uri:talksTo> ?k . "//
+            + "  FILTER ( ?k < ?l && (?m > ?n || ?o = ?j) ). " //
+            + "     }"//
+            + "}";//
+
+    String q18 = ""//
+            + "SELECT ?r ?s ?t ?u " //
+            + "{" //
+            + " GRAPH ?q { " //
+            + "  FILTER(bound(?r) && sameTerm(?s,?t)&&bound(?u)). " //
+            + "  ?t a ?u ."//
+            + "  ?s a ?r ."//
+            + "     }"//
+            + "}";//
+
+
+
+    String q19 = ""//
+            + "SELECT ?a ?c ?d ?f ?q " //
+            + "{" //
+            + " GRAPH ?x { " //
+            + "  ?f a ?a ."//
+            + " \"3\" a ?c . "//
+            + "  ?d <uri:talksTo> \"5\" . "//
+            + "  FILTER ( \"5\" < ?f && (?a > \"3\" || ?c = ?d) ). " //
+            + "  FILTER(bound(?f) && sameTerm(?a,\"3\") && bound(?q)). " //
+            + "  \"3\" a ?q ."//
+            + "  ?a a ?f ."//
+            + "     }"//
+            + "}";//
+
+
+
+
+
+
+    String q21 = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
+            + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
+            + "SELECT ?feature ?point " //
+            + "{" //
+            + "  ?feature a geo:Feature . "//
+            + "  ?feature geo:hasGeometry ?point . "//
+            + "  ?point a geo:Point . "//
+            + "  ?point geo:asWKT \"wkt\" . "//
+            + "  FILTER(geof:sfWithin(\"wkt\", \"Polygon\")) " //
+            + "}";//
+
+
+     String q22 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
+             + "SELECT ?person " //
+             + "{" //
+             + "  ?person a <http://example.org/ontology/Person> . "//
+             + "  ?person <http://www.w3.org/2000/01/rdf-schema#label> \"sally\" . "//
+             + "  ?person <http://www.w3.org/2000/01/rdf-schema#label> \"john\" . "//
+             + "  FILTER(fts:text(\"sally\", \"bob\")) . " //
+             + "  FILTER(fts:text(\"john\", \"harry\"))  " //
+             + "  ?person <uri:hasName> \"bob\". "//
+             + "  ?person <uri:hasName> \"harry\". "//
+             + "}";//
+
+
+     String q23 = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
+                + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
+                + "SELECT ?a ?b ?c ?d " //
+                + "{" //
+                + "  ?a a geo:Feature . "//
+                + "  ?b a geo:Point . "//
+                + "  ?b geo:asWKT ?c . "//
+                + "  FILTER(geof:sfWithin(?c, ?d)) " //
+                + "}";//
+
+
+     String q24 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
+             + "SELECT ?f ?g ?h" //
+             + "{" //
+             + "  ?f <http://www.w3.org/2000/01/rdf-schema#label> ?g . "//
+             + "  FILTER(fts:text(?g,?h)).  " //
+             + " ?f <uri:hasName> ?h. " //
+             + "}";//
+
+
+     String q25 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
+             + "SELECT ?person ?point" //
+             + "{" //
+             + "  ?person <http://www.w3.org/2000/01/rdf-schema#label> \"label\" . "//
+             + "  FILTER(fts:text(\"label\", \"bob\")) . " //
+             + "  ?person <uri:hasName> \"bob\" . " //
+             + "  ?person a ?point. " //
+             + "  \"bob\" a <http://example.org/ontology/Person> . "//
+             + "  ?person <http://www.w3.org/2000/01/rdf-schema#commentmatch> \"comment\" . "//
+             + "  FILTER((?person > ?point) || (?person = \"comment\")). "
+             + "  FILTER(fts:text(\"comment\", \"bob\"))  " //
+             + "}";//
+
+
+     String q26 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
+             + "SELECT ?a ?b ?c ?d " //
+             + "{" //
+             + "  ?a a ?c. " //
+             + "  ?d a <http://example.org/ontology/Person> . "//
+             + "  ?a <http://www.w3.org/2000/01/rdf-schema#commentmatch> ?b . "//
+             + "  FILTER((?a > ?c) || (?a = ?b)). "
+             + "  FILTER(fts:text(?b, ?d)) . " //
+             + "}";//
+
+
+
+     String q27 = "PREFIX fts: <http://rdf.useekm.com/fts#>  "//
+             + "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
+             + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
+             + "SELECT ?person ?feature ?point " //
+             + "{" //
+             + "  ?person <http://www.w3.org/2000/01/rdf-schema#label> \"label\" . "//
+             + "  FILTER(fts:text(\"label\", \"bob\")) . " //
+             + "  ?person <uri:hasName> \"bob\" . " //
+             + "  ?person a ?point. " //
+             + "  \"bob\" a <http://example.org/ontology/Person> . "//
+             + "  ?person <http://www.w3.org/2000/01/rdf-schema#commentmatch> \"comment\" . "//
+             + "  FILTER((?person > ?point) || (?person = \"comment\")). "
+             + "  FILTER(fts:text(\"comment\", \"bob\"))  " //
+             + "  ?feature a geo:Feature . "//
+             + "  ?point a geo:Point . "//
+             + "  ?point geo:asWKT \"wkt\" . "//
+             + "  FILTER(geof:sfWithin(\"wkt\", \"Polygon\")) " //
+             + "}";//
+
+     String q28 = ""//
+             + "SELECT ?m ?n " //
+             + "{" //
+             + "  FILTER(?m IN (1,2,3) && ?n NOT IN(5,6,7)). " //
+             + "  ?n <http://www.w3.org/2000/01/rdf-schema#label> ?m. "//
+             + "}";//
+
+
+    @Test
+    public void testContextFilterFourIndex() throws Exception {
+
+        final SPARQLParser parser1 = new SPARQLParser();
+        final SPARQLParser parser3 = new SPARQLParser();
+        final SPARQLParser parser4 = new SPARQLParser();
+        final ParsedQuery pq1 = parser1.parseQuery(q19, null);
+        final ParsedQuery pq3 = parser3.parseQuery(q17, null);
+        final ParsedQuery pq4 = parser4.parseQuery(q18, null);
+
+        System.out.println("Query is " + pq1.getTupleExpr());
+        System.out.println("Indexes are " + pq3.getTupleExpr()+ " , " +pq4.getTupleExpr());
+
+        final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(new Projection(pq3.getTupleExpr()));
+        final SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(new Projection(pq4.getTupleExpr()));
+
+        final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+
+        list.add(extTup3);
+        list.add(extTup2);
+
+        final TupleExpr tup = pq1.getTupleExpr().clone();
+
+		final PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, false);
+        pcj.optimize(tup, null, null);
+
+        System.out.println("Processed query is " + tup);
+
+        final Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector.process(pq1.getTupleExpr()));
+
+        final Set<QueryModelNode> eTupSet =  PcjIntegrationTestingUtil.getTupleSets(tup);
+        Assert.assertEquals(2, eTupSet.size());
+
+        final Set<StatementPattern> set = Sets.newHashSet();
+
+        for (final QueryModelNode s : eTupSet) {
+            final Set<StatementPattern> tempSet = Sets.newHashSet(StatementPatternCollector.process(((ExternalTupleSet) s)
+                    .getTupleExpr()));
+            set.addAll(tempSet);
+        }
+        Assert.assertTrue(qSet.containsAll(set));
+    }
+
+
+
+
+    @Test
+    public void testGeoIndexFunction() throws Exception {
+
+        final SPARQLParser parser1 = new SPARQLParser();
+        final SPARQLParser parser2 = new SPARQLParser();
+
+        final ParsedQuery pq1 = parser1.parseQuery(q21, null);
+        final ParsedQuery pq2 = parser2.parseQuery(q23, null);
+        System.out.println("Query is " + pq1.getTupleExpr());
+        System.out.println("Index is " + pq2.getTupleExpr());
+
+        final SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
+
+        final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+        list.add(extTup);
+
+        final TupleExpr tup = pq1.getTupleExpr().clone();
+		final PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, false);
+        pcj.optimize(tup, null, null);
+
+        System.out.println("Processed query is " + tup);
+
+        final Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector.process(pq1.getTupleExpr()));
+        final Set<QueryModelNode> eTupSet =  PcjIntegrationTestingUtil.getTupleSets(tup);
+        final Set<StatementPattern> set = Sets.newHashSet();
+
+        Assert.assertEquals(1, eTupSet.size());
+
+        for (final QueryModelNode s : eTupSet) {
+            final Set<StatementPattern> tempSet = Sets.newHashSet(StatementPatternCollector.process(((ExternalTupleSet) s)
+                    .getTupleExpr()));
+            set.addAll(tempSet);
+
+        }
+
+        Assert.assertTrue(qSet.containsAll(set));
+    }
+
+
+
+    @Test
+    public void testFreeTestIndexFunction() throws Exception {
+
+        final SPARQLParser parser1 = new SPARQLParser();
+        final SPARQLParser parser2 = new SPARQLParser();
+
+        final ParsedQuery pq1 = parser1.parseQuery(q22, null);
+        final ParsedQuery pq2 = parser2.parseQuery(q24, null);
+
+        System.out.println("Query is " + pq1.getTupleExpr());
+        System.out.println("Index is " + pq2.getTupleExpr());
+
+        final SimpleExternalTupleSet extTup = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
+
+        final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+        list.add(extTup);
+
+        final TupleExpr tup = pq1.getTupleExpr().clone();
+		final PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, false);
+        pcj.optimize(tup, null, null);
+
+        System.out.println("Processed query is " + tup);
+
+        final Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector.process(pq1.getTupleExpr()));
+
+        final Set<QueryModelNode> eTupSet =  PcjIntegrationTestingUtil.getTupleSets(tup);
+        final Set<StatementPattern> set = Sets.newHashSet();
+
+        Assert.assertEquals(2, eTupSet.size());
+
+        for (final QueryModelNode s : eTupSet) {
+            final Set<StatementPattern> tempSet = Sets.newHashSet(StatementPatternCollector.process(((ExternalTupleSet) s)
+                    .getTupleExpr()));
+            set.addAll(tempSet);
+
+        }
+        Assert.assertTrue(qSet.containsAll(set));
+    }
+
+
+    @Test
+    public void testThreeIndexGeoFreeCompareFilterMix() throws Exception {
+
+        final SPARQLParser parser1 = new SPARQLParser();
+        final SPARQLParser parser2 = new SPARQLParser();
+        final SPARQLParser parser3 = new SPARQLParser();
+
+        final ParsedQuery pq1 = parser1.parseQuery(q25, null);
+        final ParsedQuery pq2 = parser2.parseQuery(q24, null);
+        final ParsedQuery pq3 = parser3.parseQuery(q26, null);
+
+        System.out.println("Query is " + pq1.getTupleExpr());
+        System.out.println("Indexes are " + pq2.getTupleExpr() + " and " + pq3.getTupleExpr());
+
+
+        final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
+        final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(new Projection(pq3.getTupleExpr()));
+
+        final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+        list.add(extTup1);
+        list.add(extTup2);
+
+        final TupleExpr tup = pq1.getTupleExpr().clone();
+		final PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, false);
+        pcj.optimize(tup, null, null);
+
+        System.out.println("Processed query is " + tup);
+
+        final Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector.process(pq1.getTupleExpr()));
+        final Set<QueryModelNode> eTupSet =  PcjIntegrationTestingUtil.getTupleSets(tup);
+        final Set<StatementPattern> set = Sets.newHashSet();
+
+        Assert.assertEquals(2, eTupSet.size());
+
+        for (final QueryModelNode s : eTupSet) {
+            final Set<StatementPattern> tempSet = Sets.newHashSet(StatementPatternCollector.process(((ExternalTupleSet) s)
+                    .getTupleExpr()));
+            set.addAll(tempSet);
+
+        }
+        Assert.assertTrue(qSet.containsAll(set));
+
+    }
+
+    @Test
+    public void testFourIndexGeoFreeCompareFilterMix() throws Exception {
+
+        final SPARQLParser parser1 = new SPARQLParser();
+        final SPARQLParser parser2 = new SPARQLParser();
+        final SPARQLParser parser3 = new SPARQLParser();
+        final SPARQLParser parser4 = new SPARQLParser();
+
+
+        final ParsedQuery pq1 = parser1.parseQuery(q27, null);
+        final ParsedQuery pq2 = parser2.parseQuery(q23, null);
+        final ParsedQuery pq3 = parser3.parseQuery(q26, null);
+        final ParsedQuery pq4 = parser4.parseQuery(q24, null);
+
+        System.out.println("Query is " + pq1.getTupleExpr());
+        System.out.println("Indexes are " + pq2.getTupleExpr() + " , " + pq3.getTupleExpr() + " , " + pq4.getTupleExpr());
+
+        final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
+        final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(new Projection(pq3.getTupleExpr()));
+        final SimpleExternalTupleSet extTup3 = new SimpleExternalTupleSet(new Projection(pq4.getTupleExpr()));
+
+        final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
+
+        list.add(extTup1);
+        list.add(extTup2);
+        list.add(extTup3);
+
+        final TupleExpr tup = pq1.getTupleExpr().clone();
+		final PrecompJoinOptimizer pcj = new PrecompJoinOptimizer(list, false);
+        pcj.optimize(tup, null, null);
+
+        System.out.println("Processed query is " + tup);
+
+        final Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector.process(pq1.getTupleExpr()));
+        final Set<QueryModelNode> eTupSet =  PcjIntegrationTestingUtil.getTupleSets(tup);
+        final Set<StatementPattern> set = Sets.newHashSet();
+
+        Assert.assertEquals(3, eTupSet.size());
+
+        for (final QueryModelNode s : eTupSet) {
+            final Set<StatementPattern> tempSet = Sets.newHashSet(StatementPatternCollector.process(((ExternalTupleSet) s)
+                    .getTupleExpr()));
+            set.addAll(tempSet);
+
+        }
+
+        Assert.assertTrue(qSet.containsAll(set));
+
+    }
+
+
+
+
+
+
+
+}


[16/16] incubator-rya git commit: RYA-32 Adding License Headers

Posted by mi...@apache.org.
RYA-32 Adding License Headers


Project: http://git-wip-us.apache.org/repos/asf/incubator-rya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-rya/commit/6cd8aeb7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-rya/tree/6cd8aeb7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-rya/diff/6cd8aeb7

Branch: refs/heads/develop
Commit: 6cd8aeb71ee28252023965d2acbffc19eb32296b
Parents: c12f58f
Author: Aaron Mihalik <mi...@alum.mit.edu>
Authored: Sun Feb 7 13:24:38 2016 -0500
Committer: Aaron Mihalik <mi...@alum.mit.edu>
Committed: Sun Feb 7 13:24:38 2016 -0500

----------------------------------------------------------------------
 .../external/tupleSet/AccumuloPcjSerializer.java | 19 +++++++++++++++++++
 .../indexing/external/tupleSet/PcjTables.java    | 19 +++++++++++++++++++
 .../external/PcjIntegrationTestingUtil.java      | 19 +++++++++++++++++++
 .../external/tupleSet/AccumuloIndexSetTest.java  | 19 +++++++++++++++++++
 .../tupleSet/AccumuloPcjSerialzerTest.java       | 19 +++++++++++++++++++
 .../tupleSet/PcjTablesIntegrationTests.java      | 19 +++++++++++++++++++
 .../external/tupleSet/PcjTablesTests.java        | 19 +++++++++++++++++++
 7 files changed, 133 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/6cd8aeb7/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/AccumuloPcjSerializer.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/AccumuloPcjSerializer.java b/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/AccumuloPcjSerializer.java
index 5aefc40..452ea61 100644
--- a/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/AccumuloPcjSerializer.java
+++ b/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/AccumuloPcjSerializer.java
@@ -1,5 +1,24 @@
 package mvm.rya.indexing.external.tupleSet;
 
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
 import static mvm.rya.api.RdfCloudTripleStoreConstants.DELIM_BYTE;
 import static mvm.rya.api.RdfCloudTripleStoreConstants.DELIM_BYTES;
 import static mvm.rya.api.RdfCloudTripleStoreConstants.TYPE_DELIM_BYTE;

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/6cd8aeb7/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/PcjTables.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/PcjTables.java b/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/PcjTables.java
index e422cba..f87df51 100644
--- a/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/PcjTables.java
+++ b/extras/indexing/src/main/java/mvm/rya/indexing/external/tupleSet/PcjTables.java
@@ -1,5 +1,24 @@
 package mvm.rya.indexing.external.tupleSet;
 
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/6cd8aeb7/extras/indexing/src/test/java/mvm/rya/indexing/external/PcjIntegrationTestingUtil.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/PcjIntegrationTestingUtil.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/PcjIntegrationTestingUtil.java
index d76695c..111de19 100644
--- a/extras/indexing/src/test/java/mvm/rya/indexing/external/PcjIntegrationTestingUtil.java
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/external/PcjIntegrationTestingUtil.java
@@ -1,5 +1,24 @@
 package mvm.rya.indexing.external;
 
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
 import static com.google.common.base.Preconditions.checkNotNull;
 
 import java.util.Collection;

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/6cd8aeb7/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/AccumuloIndexSetTest.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/AccumuloIndexSetTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/AccumuloIndexSetTest.java
index dca641c..cd52e9a 100644
--- a/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/AccumuloIndexSetTest.java
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/AccumuloIndexSetTest.java
@@ -1,5 +1,24 @@
 package mvm.rya.indexing.external.tupleSet;
 
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
 import info.aduna.iteration.CloseableIteration;
 
 import java.util.HashMap;

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/6cd8aeb7/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/AccumuloPcjSerialzerTest.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/AccumuloPcjSerialzerTest.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/AccumuloPcjSerialzerTest.java
index 9fb5126..2fcacb0 100644
--- a/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/AccumuloPcjSerialzerTest.java
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/AccumuloPcjSerialzerTest.java
@@ -1,5 +1,24 @@
 package mvm.rya.indexing.external.tupleSet;
 
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
 import mvm.rya.api.resolver.RyaTypeResolverException;
 
 import org.junit.Test;

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/6cd8aeb7/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/PcjTablesIntegrationTests.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/PcjTablesIntegrationTests.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/PcjTablesIntegrationTests.java
index b8b6a57..e3adc16 100644
--- a/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/PcjTablesIntegrationTests.java
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/PcjTablesIntegrationTests.java
@@ -1,5 +1,24 @@
 package mvm.rya.indexing.external.tupleSet;
 
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
 import static com.google.common.base.Preconditions.checkNotNull;
 import static org.junit.Assert.assertEquals;
 

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/6cd8aeb7/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/PcjTablesTests.java
----------------------------------------------------------------------
diff --git a/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/PcjTablesTests.java b/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/PcjTablesTests.java
index 0a8ebc8..6d5da92 100644
--- a/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/PcjTablesTests.java
+++ b/extras/indexing/src/test/java/mvm/rya/indexing/external/tupleSet/PcjTablesTests.java
@@ -1,5 +1,24 @@
 package mvm.rya.indexing.external.tupleSet;
 
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
 import static org.junit.Assert.assertEquals;
 
 import java.util.Set;