Search in sources :

Example 6 with Element

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

the class UpdateEngineWorker method visit.

@Override
public void visit(UpdateDeleteWhere update) {
    List<Quad> quads = update.getQuads();
    // Removed from SPARQL : Convert bNodes to named variables first.
    //quads = convertBNodesToVariables(quads) ;
    // Convert quads to a pattern.
    Element el = elementFromQuads(quads);
    // Decided to serialize the bindings, but could also have decided to
    // serialize the quads after applying the template instead.
    ThresholdPolicy<Binding> policy = ThresholdPolicyFactory.policyFromContext(datasetGraph.getContext());
    DataBag<Binding> db = BagFactory.newDefaultBag(policy, SerializationFactoryFinder.bindingSerializationFactory());
    try {
        Iterator<Binding> bindings = evalBindings(el);
        db.addAll(bindings);
        Iter.close(bindings);
        Iterator<Binding> it = db.iterator();
        execDelete(datasetGraph, quads, null, it);
        Iter.close(it);
    } finally {
        db.close();
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Element(org.apache.jena.sparql.syntax.Element)

Example 7 with Element

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

the class UpdateEngineWorker method visit.

@Override
public void visit(UpdateModify update) {
    Node withGraph = update.getWithIRI();
    Element elt = update.getWherePattern();
    // null or a dataset for USING clause.
    // USING/USING NAMED
    DatasetGraph dsg = processUsing(update);
    // USING overrides WITH
    if (dsg == null && withGraph != null) {
        // Subtle difference : WITH <uri>... WHERE {}
        // and an empty/unknown graph <uri>
        //   rewrite with GRAPH -> no match.
        //   redo as dataset with different default graph -> match
        // SPARQL is unclear about what happens when the graph does not exist.
        //   but the rewrite with ElementNamedGraph is closer to SPARQL.
        // Better, treat as
        // WHERE { GRAPH <with> { ... } }
        // This is the SPARQL wording (which is a bit loose).  
        elt = new ElementNamedGraph(withGraph, elt);
    }
    if (dsg == null)
        dsg = datasetGraph;
    Query query = elementToQuery(elt);
    ThresholdPolicy<Binding> policy = ThresholdPolicyFactory.policyFromContext(datasetGraph.getContext());
    DataBag<Binding> db = BagFactory.newDefaultBag(policy, SerializationFactoryFinder.bindingSerializationFactory());
    try {
        Iterator<Binding> bindings = evalBindings(query, dsg, inputBinding, context);
        if (false) {
            List<Binding> x = Iter.toList(bindings);
            System.out.printf("====>> Bindings (%d)\n", x.size());
            Iter.print(System.out, x.iterator());
            System.out.println("====<<");
            bindings = Iter.iter(x);
        }
        db.addAll(bindings);
        Iter.close(bindings);
        Iterator<Binding> it = db.iterator();
        execDelete(datasetGraph, update.getDeleteQuads(), withGraph, it);
        Iter.close(it);
        Iterator<Binding> it2 = db.iterator();
        execInsert(datasetGraph, update.getInsertQuads(), withGraph, it2);
        Iter.close(it2);
    } finally {
        db.close();
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) ElementNamedGraph(org.apache.jena.sparql.syntax.ElementNamedGraph) Query(org.apache.jena.query.Query) Node(org.apache.jena.graph.Node) Element(org.apache.jena.sparql.syntax.Element)

Example 8 with Element

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

the class TestParameterizedSparqlString method test_param_string_injection_05.

@Test
public void test_param_string_injection_05() {
    // This injection attempt results in a valid query but a failed
    // injection
    String str = "PREFIX : <http://example/>\nSELECT * WHERE { <s> <p> ?var2 . }";
    ParameterizedSparqlString pss = new ParameterizedSparqlString(str);
    pss.setLiteral("var2", "hello\" . ?s ?p ?o");
    Query q = pss.asQuery();
    Element el = q.getQueryPattern();
    if (el instanceof ElementTriplesBlock) {
        Assert.assertEquals(1, ((ElementTriplesBlock) q.getQueryPattern()).getPattern().size());
    } else if (el instanceof ElementGroup) {
        Assert.assertEquals(1, ((ElementGroup) el).getElements().size());
        el = ((ElementGroup) el).getElements().get(0);
        if (el instanceof ElementTriplesBlock) {
            Assert.assertEquals(1, ((ElementTriplesBlock) el).getPattern().size());
        }
    }
}
Also used : Element(org.apache.jena.sparql.syntax.Element) ElementTriplesBlock(org.apache.jena.sparql.syntax.ElementTriplesBlock) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) Test(org.junit.Test)

Example 9 with Element

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

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

Aggregations

Element (org.apache.jena.sparql.syntax.Element)21 Test (org.junit.Test)9 ElementGroup (org.apache.jena.sparql.syntax.ElementGroup)8 Query (org.apache.jena.query.Query)7 Quad (org.apache.jena.sparql.core.Quad)6 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)5 ElementTriplesBlock (org.apache.jena.sparql.syntax.ElementTriplesBlock)4 Node (org.apache.jena.graph.Node)3 Var (org.apache.jena.sparql.core.Var)3 Binding (org.apache.jena.sparql.engine.binding.Binding)3 ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)3 UpdateBuilder (org.apache.jena.arq.querybuilder.UpdateBuilder)2 Triple (org.apache.jena.graph.Triple)2 UpdateModify (org.apache.jena.sparql.modify.request.UpdateModify)2 ElementNamedGraph (org.apache.jena.sparql.syntax.ElementNamedGraph)2 Update (org.apache.jena.update.Update)2 ContractTest (org.xenei.junit.contract.ContractTest)2 InternalErrorException (org.apache.jena.atlas.lib.InternalErrorException)1 BasicPattern (org.apache.jena.sparql.core.BasicPattern)1 E_Random (org.apache.jena.sparql.expr.E_Random)1