Search in sources :

Example 6 with SEPASparqlParsingException

use of it.unibo.arces.wot.sepa.commons.exceptions.SEPASparqlParsingException 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)6 SEPASecurityException (it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException)3 ErrorResponse (it.unibo.arces.wot.sepa.commons.response.ErrorResponse)3 HashSet (java.util.HashSet)3 SEPAProtocolException (it.unibo.arces.wot.sepa.commons.exceptions.SEPAProtocolException)2 InternalUpdateRequest (it.unibo.arces.wot.sepa.engine.scheduling.InternalUpdateRequest)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParseException (com.google.gson.JsonParseException)1 JsonParser (com.google.gson.JsonParser)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 SEPAProcessingException (it.unibo.arces.wot.sepa.commons.exceptions.SEPAProcessingException)1 Response (it.unibo.arces.wot.sepa.commons.response.Response)1 SPARQL11ProtocolException (it.unibo.arces.wot.sepa.engine.protocol.sparql11.SPARQL11ProtocolException)1 InternalQueryRequest (it.unibo.arces.wot.sepa.engine.scheduling.InternalQueryRequest)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 BindException (java.net.BindException)1 URI (java.net.URI)1