use of org.apache.jena.arq.querybuilder.SelectBuilder in project jena by apache.
the class WhereClauseTest method testAddUnion.
@ContractTest
public void testAddUnion() {
SelectBuilder sb = new SelectBuilder();
sb.addPrefix("pfx", "uri").addVar("?x").addWhere("<one>", "<two>", "three");
WhereClause<?> whereClause = getProducer().newInstance();
whereClause.getWhereHandler().addWhere(new TriplePath(Triple.ANY));
AbstractQueryBuilder<?> builder = whereClause.addUnion(sb);
ElementUnion union = new ElementUnion();
ElementPathBlock epb = new ElementPathBlock();
union.addElement(epb);
epb.addTriple(Triple.ANY);
Query subQuery = new Query();
ElementSubQuery esq = new ElementSubQuery(subQuery);
union.addElement(esq);
epb = new ElementPathBlock();
subQuery.setQuerySelectType();
subQuery.addProjectVars(Arrays.asList("x"));
subQuery.setQueryPattern(epb);
Triple t = new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createLiteral("three"));
epb.addTriple(t);
WhereValidator visitor = new WhereValidator(union);
Query result = builder.build();
result.getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
assertEquals("uri", result.getPrefixMapping().getNsPrefixURI("pfx"));
}
use of org.apache.jena.arq.querybuilder.SelectBuilder in project jena by apache.
the class WhereClauseTest method testSetVarsInSubQuery.
@ContractTest
public void testSetVarsInSubQuery() {
Var v = Var.alloc("v");
SelectBuilder sb = new SelectBuilder();
sb.addPrefix("pfx", "uri").addWhere("<one>", "<two>", v);
WhereClause<?> whereClause = getProducer().newInstance();
AbstractQueryBuilder<?> builder = whereClause.addSubQuery(sb);
Query subQuery = new Query();
subQuery.setQuerySelectType();
subQuery.setQueryResultStar(true);
ElementSubQuery esq = new ElementSubQuery(subQuery);
ElementPathBlock epb = new ElementPathBlock();
subQuery.setQueryPattern(epb);
Triple t = new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createVariable("v"));
epb.addTriple(t);
WhereValidator visitor = new WhereValidator(esq);
Query result = builder.build();
result.getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
builder.setVar(v, NodeFactory.createURI("three"));
subQuery = new Query();
subQuery.setQuerySelectType();
subQuery.setQueryResultStar(true);
esq = new ElementSubQuery(subQuery);
epb = new ElementPathBlock();
subQuery.setQueryPattern(epb);
t = new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three"));
epb.addTriple(t);
visitor = new WhereValidator(esq);
result = builder.build();
result.getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
}
Aggregations