Search in sources :

Example 21 with ElementGroup

use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.

the class UpdateEngineWorker method elementFromQuads.

protected Element elementFromQuads(List<Quad> quads) {
    ElementGroup el = new ElementGroup();
    ElementTriplesBlock x = new ElementTriplesBlock();
    // Maybe empty??
    el.addElement(x);
    Node g = Quad.defaultGraphNodeGenerated;
    for (Quad q : quads) {
        if (q.getGraph() != g) {
            g = q.getGraph();
            x = new ElementTriplesBlock();
            if (g == null || g == Quad.defaultGraphNodeGenerated)
                el.addElement(x);
            else {
                ElementNamedGraph eng = new ElementNamedGraph(g, x);
                el.addElement(eng);
            }
        }
        x.addTriple(q.asTriple());
    }
    return el;
}
Also used : ElementNamedGraph(org.apache.jena.sparql.syntax.ElementNamedGraph) Node(org.apache.jena.graph.Node) ElementTriplesBlock(org.apache.jena.sparql.syntax.ElementTriplesBlock) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup)

Example 22 with ElementGroup

use of org.apache.jena.sparql.syntax.ElementGroup 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 23 with ElementGroup

use of org.apache.jena.sparql.syntax.ElementGroup 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)

Example 24 with ElementGroup

use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.

the class UpdateBuilderTest method testInsertAndDeleteWithVariableNodeReplacement.

@Test
public void testInsertAndDeleteWithVariableNodeReplacement() {
    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");
    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) 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 25 with ElementGroup

use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.

the class ARQParserBase method createQueryPattern.

protected ElementGroup createQueryPattern(Template t) {
    ElementGroup elg = new ElementGroup();
    Map<Node, BasicPattern> graphs = t.getGraphPattern();
    for (Node n : graphs.keySet()) {
        Element el = new ElementPathBlock(graphs.get(n));
        if (!Quad.defaultGraphNodeGenerated.equals(n)) {
            ElementGroup e = new ElementGroup();
            e.addElement(el);
            el = new ElementNamedGraph(n, e);
        }
        elg.addElement(el);
    }
    return elg;
}
Also used : ElementNamedGraph(org.apache.jena.sparql.syntax.ElementNamedGraph) Node(org.apache.jena.graph.Node) Element(org.apache.jena.sparql.syntax.Element) BasicPattern(org.apache.jena.sparql.core.BasicPattern) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock)

Aggregations

ElementGroup (org.apache.jena.sparql.syntax.ElementGroup)37 Element (org.apache.jena.sparql.syntax.Element)24 ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)11 Test (org.junit.Test)11 Triple (org.apache.jena.graph.Triple)9 ElementTriplesBlock (org.apache.jena.sparql.syntax.ElementTriplesBlock)9 Node (org.apache.jena.graph.Node)8 Query (org.apache.jena.query.Query)6 Var (org.apache.jena.sparql.core.Var)6 Quad (org.apache.jena.sparql.core.Quad)5 UpdateModify (org.apache.jena.sparql.modify.request.UpdateModify)5 Update (org.apache.jena.update.Update)5 Op (org.apache.jena.sparql.algebra.Op)3 TriplePath (org.apache.jena.sparql.core.TriplePath)3 ElementNamedGraph (org.apache.jena.sparql.syntax.ElementNamedGraph)3 ElementSubQuery (org.apache.jena.sparql.syntax.ElementSubQuery)3 HashSet (java.util.HashSet)2 SelectClause (org.apache.jena.arq.querybuilder.clauses.SelectClause)2 ElementRewriter (org.apache.jena.arq.querybuilder.rewriters.ElementRewriter)2 IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)2