Search in sources :

Example 1 with ElementGroup

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

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

the class QueryExecDataset method describe.

// -- Describe
@Override
public Graph describe(Graph graph) {
    checkNotClosed();
    Model model = ModelFactory.createModelForGraph(graph);
    if (!query.isDescribeType())
        throw new QueryExecException("Attempt to get a DESCRIBE result from a " + labelForQuery(query) + " query");
    query.setResultVars();
    // columns).
    if (query.getQueryPattern() == null)
        query.setQueryPattern(new ElementGroup());
    Set<Node> set = new HashSet<>();
    RowSet rows = execute();
    // Prefixes for result (after initialization)
    insertPrefixesInto(graph);
    // Variables in DESCRIBE
    if (rows != null) {
        // Single pass over rows.
        rows.forEachRemaining(row -> {
            for (Var var : rows.getResultVars()) {
                Node n = row.get(var);
                if (n != null)
                    set.add(n);
            }
        });
    }
    // Any URIs in the DESCRIBE
    if (query.getResultURIs() != null) {
        query.getResultURIs().forEach(set::add);
    }
    // DescribeHandlers work on models.
    // Create new handlers for this process.
    List<DescribeHandler> dhList = DescribeHandlerRegistry.get().newHandlerList();
    getContext().put(ARQConstants.sysCurrentDataset, getDataset());
    // Notify start of describe phase
    for (DescribeHandler dh : dhList) dh.start(model, getContext());
    // Do describe for each resource found.
    for (Node n : set) {
        RDFNode rdfNode = ModelUtils.convertGraphNodeToRDFNode(n, model);
        if (rdfNode instanceof Resource) {
            for (DescribeHandler dh : dhList) {
                dh.describe((Resource) rdfNode);
            }
        } else {
            // Can't describe literals
            continue;
        }
    }
    for (DescribeHandler dh : dhList) dh.finish();
    this.close();
    return graph;
}
Also used : DescribeHandler(org.apache.jena.sparql.core.describe.DescribeHandler) Var(org.apache.jena.sparql.core.Var) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node) Resource(org.apache.jena.rdf.model.Resource) QueryExecException(org.apache.jena.query.QueryExecException) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) Model(org.apache.jena.rdf.model.Model) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 3 with ElementGroup

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

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

the class BuildElementVisitor method visit.

@Override
public void visit(ElementUnion el) {
    List<Element> lst = el.getElements();
    if (lst.size() <= 1) {
        ElementGroup eg = new ElementGroup();
        if (lst.size() == 1) {
            el.getElements().get(0).visit(this);
            eg.addElement(result);
        }
        result = eg;
    } else {
        updateList(lst);
        result = el;
    }
}
Also used : Element(org.apache.jena.sparql.syntax.Element) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup)

Example 5 with ElementGroup

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

the class ElementRewriter method visit.

@Override
public void visit(ElementGroup el) {
    ElementGroup retval = new ElementGroup();
    for (Element e : el.getElements()) {
        e.visit(this);
        retval.addElement(getResult());
    }
    push(retval);
}
Also used : Element(org.apache.jena.sparql.syntax.Element) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup)

Aggregations

ElementGroup (org.apache.jena.sparql.syntax.ElementGroup)36 Element (org.apache.jena.sparql.syntax.Element)23 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 ElementSubQuery (org.apache.jena.sparql.syntax.ElementSubQuery)3 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 OpService (org.apache.jena.sparql.algebra.op.OpService)2 DescribeHandler (org.apache.jena.sparql.core.describe.DescribeHandler)2