IndexPlanValidator implements TupleValidator { @Override public Iterator<TupleExpr> getValidTuples(Iterator<TupleExpr> tupleIter) { final Iterator<TupleExpr> iter = tupleIter; return new Iterator<TupleExpr>() { private TupleExpr next = null; private boolean hasNextCalled = false; private boolean isEmpty = false; @Override public boolean hasNext() { if (!hasNextCalled && !isEmpty) { while (iter.hasNext()) { TupleExpr temp = iter.next(); if (isValid(temp)) { next = temp; hasNextCalled = true; return true; } } isEmpty = true; return false; } else { return !isEmpty; } } @Override public TupleExpr 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!"); } }; } IndexPlanValidator(boolean omitCrossProd); void setOmitCrossProd(boolean omitCrossProd); @Override boolean isValid(TupleExpr te); int getValidTupleSize(Iterator<TupleExpr> iter); @Override Iterator<TupleExpr> getValidTuples(Iterator<TupleExpr> tupleIter);  }
@Test public void testValidTupleIterator() throws Exception { final 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 ." + "}"; final String q2 = "" + "SELECT ?t ?s ?u " + "{" + " ?s a ?t ." + " ?t <http: + " ?u <uri:talksTo> ?s . " + "}"; final String q3 = "" + "SELECT ?s ?t ?u " + "{" + " ?s <uri:hangOutWith> ?t ." + " ?t <uri:hangOutWith> ?u ." + "}"; final String q4 = "" + "SELECT ?s ?t ?u " + "{" + " ?s <uri:associatesWith> ?t ." + " ?t <uri:associatesWith> ?u ." + "}"; final SPARQLParser parser = new SPARQLParser(); 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 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(extTup2); list.add(extTup1); list.add(extTup3); final IndexedExecutionPlanGenerator iep = new IndexedExecutionPlanGenerator( pq1.getTupleExpr(), list); final Iterator<TupleExpr> plans = new TupleExecutionPlanGenerator() .getPlans(iep.getIndexedTuples()); final IndexPlanValidator ipv = new IndexPlanValidator(true); final 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); }