use of org.apache.jena.sparql.syntax.ElementPathBlock in project jena by apache.
the class UpdateBuilderTest method testInsertAndDeleteWithVar.
@Test
public void testInsertAndDeleteWithVar() {
UpdateBuilder builder = new UpdateBuilder();
Var v = Var.alloc("v");
builder.addInsert(new Quad(g, s, v, o));
builder.addDelete(new Triple(s, v, o));
builder.addWhere(null, v, "foo");
builder.setVar(v, p);
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(p, q.getPredicate());
assertEquals(o, q.getObject());
quads = um.getDeleteQuads();
assertEquals(1, quads.size());
q = quads.get(0);
assertEquals(Quad.defaultGraphNodeGenerated, q.getGraph());
assertEquals(s, q.getSubject());
assertEquals(p, q.getPredicate());
assertEquals(o, q.getObject());
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(p, t.getPredicate());
assertEquals(builder.makeNode("foo"), t.getObject());
}
use of org.apache.jena.sparql.syntax.ElementPathBlock in project jena by apache.
the class WhereHandler method addOptional.
/**
* Add an optional triple to the where clause
*
* @param t The triple path to add.
* @throws IllegalArgumentException If the triple is not a valid triple for a
* where clause.
*/
public void addOptional(TriplePath t) throws IllegalArgumentException {
testTriple(t);
ElementPathBlock epb = new ElementPathBlock();
epb.addTriple(t);
ElementOptional opt = new ElementOptional(epb);
getClause().addElement(opt);
}
use of org.apache.jena.sparql.syntax.ElementPathBlock in project jena by apache.
the class WhereHandler method addGraph.
/**
* Add a graph to the where clause.
*
* Short hand for graph { s, p, o }
*
* @param graph The name of the graph.
* @param subQuery A triple path to add to the graph.
*/
public void addGraph(Node graph, TriplePath subQuery) {
ElementPathBlock epb = new ElementPathBlock();
epb.addTriple(subQuery);
getClause().addElement(new ElementNamedGraph(graph, epb));
}
use of org.apache.jena.sparql.syntax.ElementPathBlock in project jena by apache.
the class WhereHandlerTest method testAddSubQueryWithVarExpressions.
@Test
public void testAddSubQueryWithVarExpressions() throws ParseException {
SelectBuilder sb = new SelectBuilder();
sb.addPrefix("pfx", "uri").addVar("count(*)", "?x").addWhere("<one>", "<two>", "three");
handler.addSubQuery(sb);
handler.build();
Triple t1 = new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createLiteral("three"));
TriplePath tp = new TriplePath(t1);
ElementPathBlock epb = new ElementPathBlock();
epb.addTriple(tp);
Query q = new Query();
q.addResultVar("x", q.allocAggregate(new AggCount()));
q.setQuerySelectType();
q.setQueryPattern(epb);
ElementSubQuery esq = new ElementSubQuery(q);
WhereValidator wv = new WhereValidator(esq);
query.getQueryPattern().visit(wv);
assertTrue(wv.matching);
}
use of org.apache.jena.sparql.syntax.ElementPathBlock in project jena by apache.
the class WhereHandlerTest method testAddAllOnEmpty.
@Test
public void testAddAllOnEmpty() {
Query query2 = new Query();
WhereHandler handler2 = new WhereHandler(query2);
handler2.addWhere(new TriplePath(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createLiteral("three"))));
handler.addAll(handler2);
handler.build();
Triple t1 = new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createLiteral("three"));
TriplePath tp = new TriplePath(t1);
ElementPathBlock epb = new ElementPathBlock();
epb.addTriple(tp);
WhereValidator wv = new WhereValidator(epb);
query.getQueryPattern().visit(wv);
assertTrue(wv.matching);
}
Aggregations