Search in sources :

Example 6 with UpdateModify

use of org.apache.jena.sparql.modify.request.UpdateModify 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 7 with UpdateModify

use of org.apache.jena.sparql.modify.request.UpdateModify in project SEPA by arces-wot.

the class JenaSparqlParsing method getUpdateGraphURIs.

/*
	 * 2.2.3 Specifying an RDF Dataset
	 * 
	 * SPARQL Update requests are executed against a Graph Store, a mutable
	 * container of RDF graphs managed by a SPARQL service.
	 * 
	 * The WHERE clause of a SPARQL update DELETE/INSERT operation [UPDATE] matches
	 * against data in an RDF Dataset, which is a subset of the Graph Store.
	 * 
	 * The RDF Dataset for an update operation may be specified either in the
	 * operation string itself using the USING, USING NAMED, and/or WITH keywords,
	 * or it may be specified via the using-graph-uri and using-named-graph-uri
	 * parameters.
	 * 
	 * It is an error to supply the using-graph-uri or using-named-graph-uri
	 * parameters when using this protocol to convey a SPARQL 1.1 Update request
	 * that contains an operation that uses the USING, USING NAMED, or WITH clause.
	 * 
	 * A SPARQL Update processor should treat each occurrence of the
	 * using-graph-uri=g parameter in an update protocol operation as if a USING <g>
	 * clause were included for every operation in the SPARQL 1.1 Update request.
	 * 
	 * Similarly, a SPARQL Update processor should treat each occurrence of the
	 * using-named-graph-uri=g parameter in an update protocol operation as if a
	 * USING NAMED <g> clause were included for every operation in the SPARQL 1.1
	 * Update request.
	 */
public Set<String> getUpdateGraphURIs(String sparql) throws SEPASparqlParsingException {
    UpdateRequest upd = new UpdateRequest();
    UpdateRequestSink sink = new UpdateRequestSink(upd);
    Set<String> rdfDataSet = new HashSet<String>();
    try {
        new ParserSPARQL11Update().parse(sink, new Prologue(), sparql);
    } catch (Exception e) {
        logger.error("SPARQL: " + sparql + " MESSAGE: " + e.getMessage());
        throw new SEPASparqlParsingException("SPARQL: " + sparql + " MESSAGE: " + e.getMessage());
    // logger.warn("Parsing exception "+e.getMessage());
    // rdfDataSet.add("*");
    // return rdfDataSet;
    }
    for (Update op : upd.getOperations()) {
        if (op instanceof UpdateModify) {
            UpdateModify tmp = (UpdateModify) op;
            // WITH
            Node node = tmp.getWithIRI();
            if (node != null)
                if (node.isURI()) {
                    rdfDataSet.add(node.getURI());
                }
            // USING
            for (Node n : tmp.getUsing()) {
                if (n.isURI())
                    rdfDataSet.add(n.getURI());
                else if (n.isVariable())
                    // TODO: check
                    rdfDataSet.add("*");
            }
            // USING NAMED
            for (Node n : tmp.getUsingNamed()) {
                if (n.isURI())
                    rdfDataSet.add(n.getURI());
                else if (n.isVariable())
                    // TODO: check
                    rdfDataSet.add("*");
            }
            // QUADS
            for (Quad q : tmp.getInsertQuads()) {
                Node n = q.getGraph();
                if (n.isURI())
                    if (!n.getURI().equals(arqDefaultGraphNodeUri))
                        rdfDataSet.add(n.getURI());
                    else if (n.isVariable())
                        // TODO: check
                        rdfDataSet.add("*");
            }
            for (Quad q : tmp.getDeleteQuads()) {
                Node n = q.getGraph();
                if (n.isURI())
                    if (!n.getURI().equals(arqDefaultGraphNodeUri))
                        rdfDataSet.add(n.getURI());
                    else if (n.isVariable())
                        // TODO: check
                        rdfDataSet.add("*");
            }
        } else if (op instanceof UpdateBinaryOp) {
            UpdateBinaryOp tmp = (UpdateBinaryOp) op;
            Node node = tmp.getDest().getGraph();
            // ADD, COPY, MOVE
            if (node.isURI())
                rdfDataSet.add(node.getURI());
            else if (node.isVariable())
                // TODO: check
                rdfDataSet.add("*");
        } else if (op instanceof UpdateCreate) {
            UpdateCreate tmp = (UpdateCreate) op;
            Node node = tmp.getGraph();
            // CREATE
            if (node.isURI())
                rdfDataSet.add(node.getURI());
            else if (node.isVariable())
                // TODO: check
                rdfDataSet.add("*");
        } else if (op instanceof UpdateData) {
            UpdateData tmp = (UpdateData) op;
            // UPDATE DATA
            for (Quad q : tmp.getQuads()) {
                Node node = q.getGraph();
                if (node.isURI())
                    rdfDataSet.add(node.getURI());
                else if (node.isVariable())
                    // TODO: check
                    rdfDataSet.add("*");
            }
        } else if (op instanceof UpdateDeleteWhere) {
            UpdateDeleteWhere tmp = (UpdateDeleteWhere) op;
            // UPDATE DELETE WHERE
            for (Quad q : tmp.getQuads()) {
                Node node = q.getGraph();
                if (node.isURI())
                    rdfDataSet.add(node.getURI());
                else if (node.isVariable())
                    // TODO: check
                    rdfDataSet.add("*");
            }
        } else if (op instanceof UpdateDropClear) {
            UpdateDropClear tmp = (UpdateDropClear) op;
            Node node = tmp.getGraph();
            // DROP, CLEAR
            if (node.isURI())
                rdfDataSet.add(node.getURI());
            else if (node.isVariable())
                // TODO: check
                rdfDataSet.add("*");
        } else if (op instanceof UpdateLoad) {
            UpdateLoad tmp = (UpdateLoad) op;
            Node node = tmp.getDest();
            // LOAD
            if (node.isURI())
                rdfDataSet.add(node.getURI());
            else if (node.isVariable())
                // TODO: check
                rdfDataSet.add("*");
        }
    }
    return rdfDataSet;
}
Also used : Quad(org.apache.jena.sparql.core.Quad) UpdateRequest(org.apache.jena.update.UpdateRequest) UpdateLoad(org.apache.jena.sparql.modify.request.UpdateLoad) Node(org.apache.jena.graph.Node) ParserSPARQL11Update(org.apache.jena.sparql.lang.ParserSPARQL11Update) ParserSPARQL11Update(org.apache.jena.sparql.lang.ParserSPARQL11Update) Update(org.apache.jena.update.Update) UpdateBinaryOp(org.apache.jena.sparql.modify.request.UpdateBinaryOp) SEPASparqlParsingException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASparqlParsingException) UpdateDropClear(org.apache.jena.sparql.modify.request.UpdateDropClear) UpdateDeleteWhere(org.apache.jena.sparql.modify.request.UpdateDeleteWhere) Prologue(org.apache.jena.sparql.core.Prologue) SEPASparqlParsingException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASparqlParsingException) UpdateModify(org.apache.jena.sparql.modify.request.UpdateModify) UpdateCreate(org.apache.jena.sparql.modify.request.UpdateCreate) UpdateData(org.apache.jena.sparql.modify.request.UpdateData) UpdateRequestSink(org.apache.jena.sparql.modify.UpdateRequestSink) HashSet(java.util.HashSet)

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