WhereHandler implements Handler { public void addUnion(AbstractQueryBuilder<?> subQuery) { ElementUnion union = null; ElementGroup clause = getClause(); if (!clause.isEmpty()) { Element lastElement = clause.getElements().get(clause.getElements().size() - 1); if (lastElement instanceof ElementUnion) { union = (ElementUnion) lastElement; } else { union = new ElementUnion(); union.addElement(clause); query.setQueryPattern(union); } } else { union = new ElementUnion(); clause.addElement(union); } if (subQuery instanceof SelectClause && ((SelectClause<?>)subQuery).getVars().size() > 0) { union.addElement(makeSubQuery(subQuery)); } else { PrologHandler ph = new PrologHandler(query); ph.addPrefixes(subQuery.getPrologHandler().getPrefixes()); union.addElement(subQuery.getWhereHandler().getClause()); } } WhereHandler(Query query);  WhereHandler(); Element getQueryPattern(); Query getQuery(); void addAll(WhereHandler whereHandler); Element getElement(); ElementGroup getClause(); void addWhere(TriplePath t); void addWhere(ValuesHandler values); void addOptional(TriplePath t); void addOptional(WhereHandler whereHandler); void addFilter(String expression); void addFilter(Expr expr); void addSubQuery(AbstractQueryBuilder<?> subQuery); ElementSubQuery makeSubQuery(AbstractQueryBuilder<?> subQuery); void addUnion(AbstractQueryBuilder<?> subQuery); void addGraph(Node graph, WhereHandler subQuery); void addGraph(Node graph, TriplePath subQuery); void addBind(Expr expr, Var var); void addBind(String expression, Var var); @Override void setVars(Map<Var, Node> values); @Override void build(); Node list(Object... objs); void addMinus( AbstractQueryBuilder<?> qb ); void addValueVar(PrefixMapping prefixMapping, Object var); void addValueVar(PrefixMapping prefixMapping, Object var, Object... objects); void addValueVars(PrefixMapping prefixMapping, Map<?,K> dataTable); void addValueRow(PrefixMapping prefixMapping, Object... values); void addValueRow(PrefixMapping prefixMapping, Collection<?> values); List<Var> getValuesVars(); Map<Var,List<Node>> getValuesMap(); void clearValues();  }
@Test public void testAddUnion() { Triple t1 = new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three")); Triple t2 = new Triple(NodeFactory.createURI("uno"), NodeFactory.createURI("dos"), NodeFactory.createURI("tres")); SelectBuilder sb1 = new SelectBuilder() .addWhere( t1 ); SelectBuilder sb2 = new SelectBuilder() .addWhere( t2 ); handler.addUnion(sb1); handler.addUnion(sb2); handler.build(); ElementUnion union = new ElementUnion(); ElementPathBlock epb1 = new ElementPathBlock(); epb1.addTriple(t1); union.addElement( epb1 ); ElementPathBlock epb2 = new ElementPathBlock(); epb2.addTriple(t2); union.addElement( epb2 ); WhereValidator visitor = new WhereValidator( union ); handler.getQueryPattern().visit( visitor ); assertTrue( visitor.matching ); }