TupleReArranger { public static List<TupleExpr> getTupleReOrderings(TupleExpr te) { joinArgs = Maps.newHashMap(); filterArgs = Maps.newHashMap(); NodeCollector nc = new NodeCollector(); te.visit(nc); joinArgs = nc.getPerms(); List<Join> joins = Lists.newArrayList(joinArgs.keySet()); return getPlans(getReOrderings(joins), te); }  static Iterator<TupleExpr> getPlans(Iterator<TupleExpr> indexPlans); static List<TupleExpr> getTupleReOrderings(TupleExpr te);  }
@Test public void tupleReArrangeTest1() throws MalformedQueryException { String queryString = "" + "SELECT ?a ?b ?c ?d ?e" + "{" + "{ ?a a ?b . ?a <http: + " UNION { ?a <uri:talksTo> ?d . ?a <http: + "}"; 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" + "{" + " ?e <uri:laughsAt> ?x ." + " ?e <uri:livesIn> ?y . " + "{ ?a a ?b . ?a <http: + " UNION { ?a <uri:talksTo> ?d . ?a <http: + "}"; 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" + "{" + " Filter(?c = <uri:label2>)" + " Filter(?x = <uri:somethingFunny>) " + " ?e <uri:laughsAt> ?x ." + " ?e <uri:livesIn> ?y . " + "{ ?a a ?b . ?a <http: + " UNION { ?a <uri:talksTo> ?d . ?a <http: + "}"; 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" + "{" + " Filter(?c = <uri:label2>)" + " Filter(?x = <uri:somethingFunny>) " + " Filter(?d = <uri:Fred> ) " + " ?e <uri:laughsAt> ?x ." + " ?e <uri:livesIn> ?y . " + "{ ?a a ?b . ?a <http: + " UNION { ?a <uri:talksTo> ?d . ?a <http: + "}"; SPARQLParser sp = new SPARQLParser(); ParsedQuery pq = sp.parseQuery(queryString, null); TupleExpr te = pq.getTupleExpr(); 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()); }