use of org.apache.jena.sparql.syntax.ElementPathBlock in project jena by apache.
the class WhereHandlerTest method testMakeSubQueryFromWhere.
@Test
public void testMakeSubQueryFromWhere() {
AbstractQueryBuilder<?> sb = new WhereBuilder().addWhere("?x", RDF.type, RDF.Alt);
ElementSubQuery esq = handler.makeSubQuery(sb);
Triple t1 = new Triple(NodeFactory.createVariable("x"), RDF.type.asNode(), RDF.Alt.asNode());
TriplePath tp = new TriplePath(t1);
ElementPathBlock epb = new ElementPathBlock();
epb.addTriple(tp);
Query q = new Query();
q.setQuerySelectType();
q.setQueryResultStar(true);
q.setQueryPattern(epb);
ElementSubQuery esq2 = new ElementSubQuery(q);
WhereValidator wv = new WhereValidator(esq2);
esq.visit(wv);
assertTrue(wv.matching);
}
use of org.apache.jena.sparql.syntax.ElementPathBlock in project jena by apache.
the class WhereClauseTest method testAddOptionalGroupPattern_VariableNode.
@ContractTest
public void testAddOptionalGroupPattern_VariableNode() {
Node s = NodeFactory.createVariable("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));
SelectBuilder pattern = new SelectBuilder();
pattern.addWhere(new Triple(s, q, n123));
pattern.addWhere(new Triple(s, v, x));
WhereClause<?> whereClause = getProducer().newInstance();
AbstractQueryBuilder<?> builder = whereClause.addOptional(pattern);
ElementPathBlock epb = new ElementPathBlock();
ElementOptional optional = new ElementOptional(epb);
TriplePath tp = new TriplePath(new Triple(Var.alloc(s), q, n123));
epb.addTriplePath(tp);
tp = new TriplePath(new Triple(Var.alloc(s), v, x));
epb.addTriplePath(tp);
WhereValidator visitor = new WhereValidator(optional);
builder.build().getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
}
use of org.apache.jena.sparql.syntax.ElementPathBlock in project jena by apache.
the class WhereClauseTest method testList.
@ContractTest
public void testList() {
WhereClause<?> whereClause = getProducer().newInstance();
AbstractQueryBuilder<?> builder = whereClause.addWhere(whereClause.list("<one>", "?two", "'three'"), "<foo>", "<bar>");
Query query = builder.build();
Node one = NodeFactory.createURI("one");
Var two = Var.alloc("two");
Node three = NodeFactory.createLiteral("three");
Node foo = NodeFactory.createURI("foo");
Node bar = NodeFactory.createURI("bar");
ElementPathBlock epb = new ElementPathBlock();
Node firstObject = NodeFactory.createBlankNode();
Node secondObject = NodeFactory.createBlankNode();
Node thirdObject = NodeFactory.createBlankNode();
epb.addTriplePath(new TriplePath(new Triple(firstObject, RDF.first.asNode(), one)));
epb.addTriplePath(new TriplePath(new Triple(firstObject, RDF.rest.asNode(), secondObject)));
epb.addTriplePath(new TriplePath(new Triple(secondObject, RDF.first.asNode(), two)));
epb.addTriplePath(new TriplePath(new Triple(secondObject, RDF.rest.asNode(), thirdObject)));
epb.addTriplePath(new TriplePath(new Triple(thirdObject, RDF.first.asNode(), three)));
epb.addTriplePath(new TriplePath(new Triple(thirdObject, RDF.rest.asNode(), RDF.nil.asNode())));
epb.addTriplePath(new TriplePath(new Triple(firstObject, foo, bar)));
WhereValidator visitor = new WhereValidator(epb);
query.getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
}
use of org.apache.jena.sparql.syntax.ElementPathBlock in project jena by apache.
the class WhereClauseTest method testAddOptionalObjectsWithPath.
@ContractTest
public void testAddOptionalObjectsWithPath() {
WhereClause<?> whereClause = getProducer().newInstance();
PrefixMapping pmap = new PrefixMappingImpl();
pmap.setNsPrefix("ts", "urn:test:");
Path path = PathParser.parse("ts:two/ts:dos", pmap);
AbstractQueryBuilder<?> builder = whereClause.addOptional(NodeFactory.createURI("one"), path, NodeFactory.createURI("three"));
ElementPathBlock epb = new ElementPathBlock();
ElementOptional optional = new ElementOptional(epb);
TriplePath tp = new TriplePath(NodeFactory.createURI("one"), path, NodeFactory.createURI("three"));
epb.addTriplePath(tp);
WhereValidator visitor = new WhereValidator(optional);
builder.build().getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
}
use of org.apache.jena.sparql.syntax.ElementPathBlock in project jena by apache.
the class WhereClauseTest method testSetVarsInSubQuery_Node_Variable.
@ContractTest
public void testSetVarsInSubQuery_Node_Variable() {
Node v = NodeFactory.createVariable("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);
TriplePath tp = new TriplePath(NodeFactory.createURI("one"), new P_Link(NodeFactory.createURI("two")), NodeFactory.createVariable("v"));
epb.addTriple(tp);
WhereValidator visitor = new WhereValidator(esq);
builder.build().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);
tp = new TriplePath(NodeFactory.createURI("one"), new P_Link(NodeFactory.createURI("two")), NodeFactory.createURI("three"));
epb.addTriple(tp);
visitor = new WhereValidator(esq);
builder.build().getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
}
Aggregations