private TupleExpr getTupleExpr(String query) throws MalformedQueryException {

    SPARQLParser sp = new SPARQLParser();
    ParsedQuery pq = sp.parseQuery(query, null);

    return pq.getTupleExpr();
  }
  @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));
  }
  @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 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 PCJOptimizer pcj = new PCJOptimizer(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);
  }
  @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);
  }
Example #6
0
 public String query(String queryString)
     throws RepositoryException, MalformedQueryException, QueryEvaluationException,
         TupleQueryResultHandlerException, RDFHandlerException, IOException {
   ByteArrayOutputStream bout = new ByteArrayOutputStream();
   RepositoryConnection con = getRepository().getConnection();
   SPARQLParser parser = new SPARQLParser();
   ParsedQuery parsedQuery = parser.parseQuery(queryString, null);
   if (parsedQuery instanceof ParsedTupleQuery) {
     SPARQLResultsXMLWriter writer = new SPARQLResultsXMLWriter(bout);
     TupleQuery query = con.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
     query.evaluate(writer);
   } else if (parsedQuery instanceof ParsedGraphQuery) {
     RDFXMLPrettyWriter writer = new RDFXMLPrettyWriter(bout);
     GraphQuery query = con.prepareGraphQuery(QueryLanguage.SPARQL, queryString);
     query.evaluate(writer);
   } else if (parsedQuery instanceof ParsedBooleanQuery) {
     BooleanQuery query = con.prepareBooleanQuery(QueryLanguage.SPARQL, queryString);
     boolean result = query.evaluate();
     String resultString = BOOLEAN_RESULT_RESULT_PREFIX + result + BOOLEAN_RESULT_RESULT_SUFFIX;
     bout.write(resultString.getBytes());
   }
   con.close();
   return bout.toString();
 }
Example #7
0
  @Override
  public IResultSet<Value, URI, Literal> selectQuery(String query, String graph) {
    connect();

    final SPARQLParser parser = new SPARQLParser();

    CloseableIteration<? extends BindingSet, QueryEvaluationException> sparqlResults = null;
    ParsedQuery parsedQuery = null;

    try {
      parsedQuery = parser.parseQuery(query, null);
    } catch (MalformedQueryException e) {
      e.printStackTrace();
    }
    try {
      sparqlResults =
          connection.evaluate(
              parsedQuery.getTupleExpr(), parsedQuery.getDataset(), new EmptyBindingSet(), false);
    } catch (SailException e) {
      e.printStackTrace();
    }

    return new SailResultSet(sparqlResults);
  }
  @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);
  }
  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));
  }
  @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 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 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 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));
  }