ThreshholdPlanSelector implements IndexedQueryPlanSelector { public double getCost(TupleExpr te, double indexWeight, double commonVarWeight, double dirProdWeight) { if (indexWeight + commonVarWeight + dirProdWeight != 1) { throw new IllegalArgumentException("Weights must sum to 1!"); } if(te == null) { throw new IllegalArgumentException("TupleExpr cannot be null!"); } QueryNodeCount qnc = new QueryNodeCount(); te.visit(qnc); double nodeCount = qnc.getNodeCount(); double commonJoinVars = qnc.getCommonJoinVarCount(); double joinVars = qnc.getJoinVarCount(); double joinCount = qnc.getJoinCount(); double dirProdCount = qnc.getDirProdCount(); double dirProductScale; if(queryNodeCount > nodeCount) { dirProductScale = 1/ (queryNodeCount - nodeCount); } else { dirProductScale = 1/ (queryNodeCount - nodeCount + 1); } double joinVarRatio; double dirProductRatio; if(joinVars != 0) { joinVarRatio = (joinVars - commonJoinVars) / joinVars; } else { joinVarRatio = 0; } if(joinCount != 0) { dirProductRatio = dirProdCount / joinCount; } else { dirProductRatio = 0; } double cost = indexWeight * (nodeCount / queryNodeCount) + commonVarWeight*joinVarRatio + dirProdWeight *dirProductRatio*dirProductScale; return cost; } ThreshholdPlanSelector(TupleExpr query); @Override TupleExpr getThreshholdQueryPlan(Iterator<TupleExpr> tuples, double threshhold, double indexWeight,
            double commonVarWeight, double extProdWeight); double getCost(TupleExpr te, double indexWeight, double commonVarWeight, double dirProdWeight);  }
@Test public void testCost1() throws Exception { String q1 = "" + "SELECT ?f ?m ?d ?h ?i " + "{" + " ?f a ?m ." + " ?m <http: + " ?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: + " ?l <http: + "}"; 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: + " ?l <http: + "}"; String q2 = "" + "SELECT ?s ?t ?u " + "{" + " ?s a ?t ." + " ?t <http: + " ?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, new AccumuloIndexSetProvider(new Configuration(), eList)); pcj.optimize(te, null, null); ThreshholdPlanSelector tps = new ThreshholdPlanSelector( pq1.getTupleExpr()); double cost = tps.getCost(te, .4, .3, .3); Assert.assertEquals(.575, cost, .0001); }