Search in sources :

Example 1 with ParserSPARQL11Update

use of org.apache.jena.sparql.lang.ParserSPARQL11Update 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

SEPASparqlParsingException (it.unibo.arces.wot.sepa.commons.exceptions.SEPASparqlParsingException)1 HashSet (java.util.HashSet)1 Node (org.apache.jena.graph.Node)1 Prologue (org.apache.jena.sparql.core.Prologue)1 Quad (org.apache.jena.sparql.core.Quad)1 ParserSPARQL11Update (org.apache.jena.sparql.lang.ParserSPARQL11Update)1 UpdateRequestSink (org.apache.jena.sparql.modify.UpdateRequestSink)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 UpdateDeleteWhere (org.apache.jena.sparql.modify.request.UpdateDeleteWhere)1 UpdateDropClear (org.apache.jena.sparql.modify.request.UpdateDropClear)1 UpdateLoad (org.apache.jena.sparql.modify.request.UpdateLoad)1 UpdateModify (org.apache.jena.sparql.modify.request.UpdateModify)1 Update (org.apache.jena.update.Update)1 UpdateRequest (org.apache.jena.update.UpdateRequest)1