use of org.apache.jena.sparql.syntax.ElementPathBlock in project jena by apache.
the class WhereHandlerTest method testAddOptionalWhereHandler.
@Test
public void testAddOptionalWhereHandler() {
WhereHandler pattern = new WhereHandler(new Query());
Var s = Var.alloc("s");
Node q = NodeFactory.createURI("urn:q");
Node v = NodeFactory.createURI("urn:v");
Var x = Var.alloc("x");
Node n123 = NodeFactory.createLiteral(LiteralLabelFactory.createTypedLiteral(123));
pattern.addWhere(new TriplePath(new Triple(s, q, n123)));
pattern.addWhere(new TriplePath(new Triple(s, v, x)));
handler.addOptional(pattern);
handler.build();
ElementPathBlock epb = new ElementPathBlock();
ElementOptional optional = new ElementOptional(epb);
TriplePath tp = new TriplePath(new Triple(s, q, n123));
epb.addTriplePath(tp);
tp = new TriplePath(new Triple(s, v, x));
epb.addTriplePath(tp);
WhereValidator visitor = new WhereValidator(optional);
handler.getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
}
use of org.apache.jena.sparql.syntax.ElementPathBlock in project jena by apache.
the class WhereHandlerTest method testAddOptionalObjects.
@Test
public void testAddOptionalObjects() {
handler.addOptional(new TriplePath(new Triple(NodeFactory.createURI("one"), ResourceFactory.createResource("two").asNode(), ResourceFactory.createLangLiteral("three", "en-US").asNode())));
handler.build();
ElementPathBlock epb = new ElementPathBlock();
ElementOptional optional = new ElementOptional(epb);
TriplePath tp = new TriplePath(new Triple(NodeFactory.createURI("one"), ResourceFactory.createResource("two").asNode(), ResourceFactory.createLangLiteral("three", "en-US").asNode()));
epb.addTriplePath(tp);
WhereValidator visitor = new WhereValidator(optional);
handler.getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
}
use of org.apache.jena.sparql.syntax.ElementPathBlock in project jena by apache.
the class WhereClauseTest method addGraph_GSPO.
public void addGraph_GSPO() {
final Node s = NodeFactory.createURI("s");
final Node p = NodeFactory.createURI("p");
final Node o = NodeFactory.createURI("o");
WhereClause<?> whereClause = getProducer().newInstance();
AbstractQueryBuilder<?> builder = whereClause.addGraph("<g>", s, p, o);
Query query = builder.build();
ElementPathBlock epb = new ElementPathBlock();
ElementNamedGraph eng = new ElementNamedGraph(NodeFactory.createURI("g"), epb);
epb.addTriplePath(new TriplePath(new Triple(s, p, o)));
WhereValidator visitor = new WhereValidator(eng);
query.getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
}
use of org.apache.jena.sparql.syntax.ElementPathBlock 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.sparql.syntax.ElementPathBlock 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