use of org.apache.jena.sparql.syntax.ElementUnion in project jena by apache.
the class ElementRewriter method visit.
@Override
public void visit(ElementUnion el) {
ElementUnion retval = new ElementUnion();
for (Element e : el.getElements()) {
e.visit(this);
retval.addElement(getResult());
}
push(retval);
}
use of org.apache.jena.sparql.syntax.ElementUnion in project jena by apache.
the class WhereHandlerTest method testSetVarsInUnion.
@Test
public void testSetVarsInUnion() {
Var v = Var.alloc("v");
SelectBuilder sb = new SelectBuilder();
sb.addPrefix("pfx", "uri").addWhere("<one>", "<two>", v);
handler.addUnion(sb);
SelectBuilder sb2 = new SelectBuilder().addWhere("<uno>", "<dos>", "<tres>");
handler.addUnion(sb2);
handler.build();
Node one = NodeFactory.createURI("one");
Node two = NodeFactory.createURI("two");
Node three = NodeFactory.createURI("three");
Node uno = NodeFactory.createURI("uno");
Node dos = NodeFactory.createURI("dos");
Node tres = NodeFactory.createURI("tres");
ElementUnion union = new ElementUnion();
ElementPathBlock epb = new ElementPathBlock();
Triple t = new Triple(one, two, v.asNode());
epb.addTriple(t);
union.addElement(epb);
ElementPathBlock epb2 = new ElementPathBlock();
t = new Triple(uno, dos, tres);
epb2.addTriple(t);
union.addElement(epb2);
WhereValidator visitor = new WhereValidator(union);
handler.getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
Map<Var, Node> values = new HashMap<>();
values.put(v, three);
handler.setVars(values);
handler.build();
union = new ElementUnion();
epb = new ElementPathBlock();
t = new Triple(one, two, three);
epb.addTriple(t);
union.addElement(epb);
epb2 = new ElementPathBlock();
t = new Triple(uno, dos, tres);
epb2.addTriple(t);
union.addElement(epb2);
visitor = new WhereValidator(union);
handler.getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
}
use of org.apache.jena.sparql.syntax.ElementUnion in project jena by apache.
the class WhereClauseTest method testSetVarsInUnion_Node_Variable.
@ContractTest
public void testSetVarsInUnion_Node_Variable() {
Node v = NodeFactory.createVariable("v");
SelectBuilder sb1 = new SelectBuilder().addPrefix("pfx", "uri").addWhere("<one>", "<two>", v);
WhereClause<?> whereClause = getProducer().newInstance();
whereClause.addUnion(sb1);
SelectBuilder sb2 = new SelectBuilder().addWhere("<uno>", "<dos>", "<tres>");
AbstractQueryBuilder<?> builder = whereClause.addUnion(sb2);
Query query = builder.build();
Node one = NodeFactory.createURI("one");
Node two = NodeFactory.createURI("two");
Node three = NodeFactory.createURI("three");
Node uno = NodeFactory.createURI("uno");
Node dos = NodeFactory.createURI("dos");
Node tres = NodeFactory.createURI("tres");
ElementUnion union = new ElementUnion();
ElementPathBlock epb = new ElementPathBlock();
Triple t = new Triple(one, two, Var.alloc(v));
epb.addTriple(t);
union.addElement(epb);
ElementPathBlock epb2 = new ElementPathBlock();
t = new Triple(uno, dos, tres);
epb2.addTriple(t);
union.addElement(epb2);
WhereValidator visitor = new WhereValidator(union);
query.getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
builder.setVar(v, NodeFactory.createURI("three"));
query = builder.build();
union = new ElementUnion();
epb = new ElementPathBlock();
t = new Triple(one, two, three);
epb.addTriple(t);
union.addElement(epb);
epb2 = new ElementPathBlock();
t = new Triple(uno, dos, tres);
epb2.addTriple(t);
union.addElement(epb2);
visitor = new WhereValidator(union);
query.getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
}
use of org.apache.jena.sparql.syntax.ElementUnion in project jena by apache.
the class WhereHandlerTest method testAddUnionWithVar.
@Test
public void testAddUnionWithVar() {
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 sb = new SelectBuilder().addVar("x").addWhere(t1);
handler.addUnion(sb);
SelectBuilder sb2 = new SelectBuilder().addWhere(t2);
handler.addUnion(sb2);
handler.build();
ElementUnion union = new ElementUnion();
Query q = new Query();
q.setQuerySelectType();
ElementPathBlock epb1 = new ElementPathBlock();
epb1.addTriple(t1);
q.setQueryPattern(epb1);
q.addProjectVars(Arrays.asList(Var.alloc("x")));
ElementSubQuery sq = new ElementSubQuery(q);
union.addElement(sq);
ElementPathBlock epb2 = new ElementPathBlock();
epb2.addTriple(t2);
union.addElement(epb2);
WhereValidator visitor = new WhereValidator(union);
handler.getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
}
use of org.apache.jena.sparql.syntax.ElementUnion in project jena by apache.
the class WhereHandlerTest method testAddUnionToExistingWithVar.
@Test
public void testAddUnionToExistingWithVar() {
handler.addWhere(new TriplePath(new Triple(NodeFactory.createURI("s"), NodeFactory.createURI("p"), NodeFactory.createURI("o"))));
SelectBuilder sb = new SelectBuilder().addVar("x").addWhere(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three")));
handler.addUnion(sb);
handler.build();
Query q = new Query();
q.setQuerySelectType();
ElementPathBlock epb1 = new ElementPathBlock();
epb1.addTriple(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three")));
q.setQueryPattern(epb1);
q.addProjectVars(Arrays.asList(Var.alloc("x")));
ElementSubQuery sq = new ElementSubQuery(q);
ElementPathBlock epb2 = new ElementPathBlock();
epb2.addTriple(new Triple(NodeFactory.createURI("s"), NodeFactory.createURI("p"), NodeFactory.createURI("o")));
ElementUnion union = new ElementUnion();
union.addElement(epb2);
union.addElement(sq);
WhereValidator visitor = new WhereValidator(union);
handler.getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
}
Aggregations