Search in sources :

Example 1 with Graph

use of com.hp.hpl.jena.graph.Graph in project goci by EBISPOT.

the class SparqlTemplate method query.

public <T> T query(String sparql, ResultSetMapper<T> rsm, Object... args) {
    sparql = getPrefixString().concat(sparql);
    Graph g = getJenaQueryExecutionService().getDefaultGraph();
    Map<String, Object> bindingMap = new HashMap<String, Object>();
    int i = 0;
    for (Object o : args) {
        String argName = "?_arg" + i++;
        sparql = sparql.replaceFirst("\\?\\?", argName);
        bindingMap.put(argName, o);
    }
    Query q1 = QueryFactory.create(sparql, Syntax.syntaxARQ);
    QuerySolutionMap initialBinding = new QuerySolutionMap();
    for (String argName : bindingMap.keySet()) {
        Object argValue = bindingMap.get(argName);
        RDFNode arg;
        if (argValue instanceof URI) {
            arg = new ResourceImpl(argValue.toString());
        } else {
            arg = getLiteralNode(argValue);
        }
        initialBinding.add(argName, arg);
    }
    ParameterizedSparqlString queryString = new ParameterizedSparqlString(q1.toString(), initialBinding);
    QueryExecution execute = null;
    try {
        execute = getJenaQueryExecutionService().getQueryExecution(g, queryString.asQuery(), false);
        ResultSet results = execute.execSelect();
        return rsm.mapResultSet(results);
    } catch (LodeException e) {
        throw new SparqlQueryException("Failed to execute query '" + sparql + "'", e);
    } finally {
        if (execute != null) {
            execute.close();
            if (g != null) {
                g.close();
            }
        }
    }
}
Also used : Query(com.hp.hpl.jena.query.Query) HashMap(java.util.HashMap) SparqlQueryException(uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException) ParameterizedSparqlString(com.hp.hpl.jena.query.ParameterizedSparqlString) ParameterizedSparqlString(com.hp.hpl.jena.query.ParameterizedSparqlString) URI(java.net.URI) QueryExecution(com.hp.hpl.jena.query.QueryExecution) QuerySolutionMap(com.hp.hpl.jena.query.QuerySolutionMap) Graph(com.hp.hpl.jena.graph.Graph) ResourceImpl(com.hp.hpl.jena.rdf.model.impl.ResourceImpl) LodeException(uk.ac.ebi.fgpt.lode.exception.LodeException) ResultSet(com.hp.hpl.jena.query.ResultSet) RDFNode(com.hp.hpl.jena.rdf.model.RDFNode)

Example 2 with Graph

use of com.hp.hpl.jena.graph.Graph in project goci by EBISPOT.

the class SparqlTemplate method ask.

public boolean ask(String sparql) {
    sparql = getPrefixString().concat(sparql);
    Graph g = getJenaQueryExecutionService().getDefaultGraph();
    Query q1 = QueryFactory.create(sparql, Syntax.syntaxARQ);
    QueryExecution execute = null;
    try {
        execute = getJenaQueryExecutionService().getQueryExecution(g, q1, false);
        return execute.execAsk();
    } catch (LodeException e) {
        throw new SparqlQueryException("Failed to execute ask '" + sparql + "'", e);
    } finally {
        if (execute != null) {
            execute.close();
            if (g != null) {
                g.close();
            }
        }
    }
}
Also used : Graph(com.hp.hpl.jena.graph.Graph) Query(com.hp.hpl.jena.query.Query) LodeException(uk.ac.ebi.fgpt.lode.exception.LodeException) SparqlQueryException(uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException) QueryExecution(com.hp.hpl.jena.query.QueryExecution)

Example 3 with Graph

use of com.hp.hpl.jena.graph.Graph in project goci by EBISPOT.

the class SparqlTemplate method query.

public <T> T query(String sparql, ResultSetMapper<T> rsm) {
    sparql = getPrefixString().concat(sparql);
    Graph g = getJenaQueryExecutionService().getDefaultGraph();
    Query q1 = QueryFactory.create(sparql, Syntax.syntaxARQ);
    QueryExecution execute = null;
    try {
        execute = getJenaQueryExecutionService().getQueryExecution(g, q1, false);
        ResultSet results = execute.execSelect();
        return rsm.mapResultSet(results);
    } catch (LodeException e) {
        throw new SparqlQueryException("Failed to execute query '" + sparql + "'", e);
    } finally {
        if (execute != null) {
            execute.close();
            if (g != null) {
                g.close();
            }
        }
    }
}
Also used : Graph(com.hp.hpl.jena.graph.Graph) Query(com.hp.hpl.jena.query.Query) LodeException(uk.ac.ebi.fgpt.lode.exception.LodeException) SparqlQueryException(uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException) ResultSet(com.hp.hpl.jena.query.ResultSet) QueryExecution(com.hp.hpl.jena.query.QueryExecution)

Aggregations

Graph (com.hp.hpl.jena.graph.Graph)3 Query (com.hp.hpl.jena.query.Query)3 QueryExecution (com.hp.hpl.jena.query.QueryExecution)3 LodeException (uk.ac.ebi.fgpt.lode.exception.LodeException)3 SparqlQueryException (uk.ac.ebi.spot.goci.sparql.exception.SparqlQueryException)3 ResultSet (com.hp.hpl.jena.query.ResultSet)2 ParameterizedSparqlString (com.hp.hpl.jena.query.ParameterizedSparqlString)1 QuerySolutionMap (com.hp.hpl.jena.query.QuerySolutionMap)1 RDFNode (com.hp.hpl.jena.rdf.model.RDFNode)1 ResourceImpl (com.hp.hpl.jena.rdf.model.impl.ResourceImpl)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1