Search in sources :

Example 1 with UpdateModify

use of org.apache.jena.sparql.modify.request.UpdateModify 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());
}
Also used : Triple(org.apache.jena.graph.Triple) Quad(org.apache.jena.sparql.core.Quad) UpdateModify(org.apache.jena.sparql.modify.request.UpdateModify) Var(org.apache.jena.sparql.core.Var) Element(org.apache.jena.sparql.syntax.Element) UpdateBuilder(org.apache.jena.arq.querybuilder.UpdateBuilder) Update(org.apache.jena.update.Update) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) Test(org.junit.Test)

Example 2 with UpdateModify

use of org.apache.jena.sparql.modify.request.UpdateModify in project jena by apache.

the class UpdateBuilder method buildWhere.

// build updates with where clauses
private Update buildWhere() {
    UpdateModify retval = new UpdateModify();
    if (with != null) {
        Node graph = values.get(with);
        if (graph == null) {
            graph = with;
        }
        retval.setWithIRI(graph);
    }
    QuadAcc acc;
    Iterator<Quad> iter;
    if (!inserts.isEmpty()) {
        retval.setHasInsertClause(true);
        acc = retval.getInsertAcc();
        iter = getQuads(inserts);
        while (iter.hasNext()) {
            acc.addQuad(iter.next());
        }
    }
    if (!deletes.isEmpty()) {
        retval.setHasDeleteClause(true);
        acc = retval.getDeleteAcc();
        iter = getQuads(deletes);
        while (iter.hasNext()) {
            acc.addQuad(iter.next());
        }
    }
    retval.setElement(whereProcessor.setVars(values).build());
    return retval;
}
Also used : Quad(org.apache.jena.sparql.core.Quad) UpdateModify(org.apache.jena.sparql.modify.request.UpdateModify) Node(org.apache.jena.graph.Node) QuadAcc(org.apache.jena.sparql.modify.request.QuadAcc)

Example 3 with UpdateModify

use of org.apache.jena.sparql.modify.request.UpdateModify in project jena by apache.

the class UpdateBuilderTest method testInsertAndDelete.

@Test
public void testInsertAndDelete() {
    UpdateBuilder builder = new UpdateBuilder();
    builder.addInsert(new Quad(g, s, p, o));
    builder.addDelete(new Triple(s, p, o));
    builder.addWhere(null, p, "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(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());
}
Also used : Triple(org.apache.jena.graph.Triple) Quad(org.apache.jena.sparql.core.Quad) UpdateModify(org.apache.jena.sparql.modify.request.UpdateModify) Element(org.apache.jena.sparql.syntax.Element) Update(org.apache.jena.update.Update) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) Test(org.junit.Test)

Example 4 with UpdateModify

use of org.apache.jena.sparql.modify.request.UpdateModify 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()));
}
Also used : Triple(org.apache.jena.graph.Triple) Quad(org.apache.jena.sparql.core.Quad) UpdateModify(org.apache.jena.sparql.modify.request.UpdateModify) Node(org.apache.jena.graph.Node) Element(org.apache.jena.sparql.syntax.Element) Update(org.apache.jena.update.Update) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) Test(org.junit.Test)

Example 5 with UpdateModify

use of org.apache.jena.sparql.modify.request.UpdateModify in project jena by apache.

the class UpdateBuilderTest method testInsertAndDeleteWithVarReplacement.

@Test
public void testInsertAndDeleteWithVarReplacement() {
    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());
}
Also used : Triple(org.apache.jena.graph.Triple) Quad(org.apache.jena.sparql.core.Quad) UpdateModify(org.apache.jena.sparql.modify.request.UpdateModify) Var(org.apache.jena.sparql.core.Var) Element(org.apache.jena.sparql.syntax.Element) Update(org.apache.jena.update.Update) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) Test(org.junit.Test)

Aggregations

Quad (org.apache.jena.sparql.core.Quad)7 UpdateModify (org.apache.jena.sparql.modify.request.UpdateModify)7 Update (org.apache.jena.update.Update)6 Triple (org.apache.jena.graph.Triple)5 Element (org.apache.jena.sparql.syntax.Element)5 ElementGroup (org.apache.jena.sparql.syntax.ElementGroup)5 ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)5 Test (org.junit.Test)5 Node (org.apache.jena.graph.Node)4 Var (org.apache.jena.sparql.core.Var)2 SEPASparqlParsingException (it.unibo.arces.wot.sepa.commons.exceptions.SEPASparqlParsingException)1 HashSet (java.util.HashSet)1 UpdateBuilder (org.apache.jena.arq.querybuilder.UpdateBuilder)1 Prologue (org.apache.jena.sparql.core.Prologue)1 ParserSPARQL11Update (org.apache.jena.sparql.lang.ParserSPARQL11Update)1 UpdateRequestSink (org.apache.jena.sparql.modify.UpdateRequestSink)1 QuadAcc (org.apache.jena.sparql.modify.request.QuadAcc)1 UpdateBinaryOp (org.apache.jena.sparql.modify.request.UpdateBinaryOp)1 UpdateCreate (org.apache.jena.sparql.modify.request.UpdateCreate)1 UpdateData (org.apache.jena.sparql.modify.request.UpdateData)1