use of org.apache.jena.sparql.syntax.ElementPathBlock in project jena by apache.
the class WhereClauseTest method testSetVarsInOptional_Node_Variable.
@ContractTest
public void testSetVarsInOptional_Node_Variable() {
Node v = NodeFactory.createVariable("v");
Node one = NodeFactory.createURI("one");
Node two = NodeFactory.createURI("two");
Node three = NodeFactory.createURI("three");
WhereClause<?> whereClause = getProducer().newInstance();
AbstractQueryBuilder<?> builder = whereClause.addOptional(new Triple(one, two, v));
ElementPathBlock epb = new ElementPathBlock();
ElementOptional optional = new ElementOptional(epb);
TriplePath tp = new TriplePath(new Triple(one, two, Var.alloc(v)));
epb.addTriplePath(tp);
WhereValidator visitor = new WhereValidator(optional);
builder.build().getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
builder.setVar(v, three);
epb = new ElementPathBlock();
optional = new ElementOptional(epb);
tp = new TriplePath(new Triple(one, two, three));
epb.addTriplePath(tp);
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 testSetVarsInOptional.
@ContractTest
public void testSetVarsInOptional() {
Var v = Var.alloc("v");
WhereClause<?> whereClause = getProducer().newInstance();
AbstractQueryBuilder<?> builder = whereClause.addOptional(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), v));
ElementPathBlock epb = new ElementPathBlock();
ElementOptional optional = new ElementOptional(epb);
TriplePath tp = new TriplePath(NodeFactory.createURI("one"), new P_Link(NodeFactory.createURI("two")), v);
epb.addTriple(tp);
WhereValidator visitor = new WhereValidator(optional);
builder.build().getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
builder.setVar(v, NodeFactory.createURI("three"));
epb = new ElementPathBlock();
optional = new ElementOptional(epb);
Triple t = new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three"));
epb.addTriple(t);
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 testAddOptionalObjects.
@ContractTest
public void testAddOptionalObjects() {
WhereClause<?> whereClause = getProducer().newInstance();
AbstractQueryBuilder<?> builder = whereClause.addOptional(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three"));
ElementPathBlock epb = new ElementPathBlock();
ElementOptional optional = new ElementOptional(epb);
Triple t = new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three"));
epb.addTriple(t);
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 SelectBuilderTest method testList.
@Test
public void testList() {
builder.addVar("*").addWhere(builder.list("<one>", "?two", "'three'"), "<foo>", "<bar>");
Query query = builder.build();
Node one = NodeFactory.createURI("one");
Node two = Var.alloc("two").asNode();
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 UpdateBuilderTest method testInsertAndDeleteWithVariableNode.
@Test
public void testInsertAndDeleteWithVariableNode() {
UpdateBuilder builder = new UpdateBuilder();
Node v = NodeFactory.createVariable("v");
builder.addInsert(new Quad(g, s, v, o));
builder.addDelete(new Triple(s, v, o));
builder.addWhere(null, v, "foo");
Update update = builder.build();
assertTrue(update instanceof UpdateModify);
UpdateModify um = (UpdateModify) update;
List<Quad> quads = um.getInsertQuads();
assertEquals(1, quads.size());
Quad q = quads.get(0);
assertEquals(g, q.getGraph());
assertEquals(s, q.getSubject());
assertEquals(v, q.getPredicate());
assertEquals(o, q.getObject());
assertTrue(Var.isVar(q.getPredicate()));
quads = um.getDeleteQuads();
assertEquals(1, quads.size());
q = quads.get(0);
assertEquals(Quad.defaultGraphNodeGenerated, q.getGraph());
assertEquals(s, q.getSubject());
assertEquals(v, q.getPredicate());
assertEquals(o, q.getObject());
assertTrue(Var.isVar(q.getPredicate()));
Element e = um.getWherePattern();
assertTrue(e instanceof ElementGroup);
ElementGroup eg = (ElementGroup) e;
assertEquals(1, eg.getElements().size());
ElementPathBlock epb = (ElementPathBlock) eg.getElements().get(0);
Triple t = epb.getPattern().get(0).asTriple();
assertEquals(Node.ANY, t.getSubject());
assertEquals(v, t.getPredicate());
assertEquals(builder.makeNode("foo"), t.getObject());
assertTrue(Var.isVar(t.getPredicate()));
}
Aggregations