Search in sources :

Example 1 with QueryParseException

use of com.hp.hpl.jena.query.QueryParseException in project nextprot-api by calipho-sib.

the class SparqlServiceImpl method findEntries.

@Override
@Cacheable("sparql")
public List<String> findEntries(String sparql, String sparqlEndpointUrl, String sparqlTitle) {
    String query = SparqlUtils.buildQuery(prefix, sparql);
    List<String> results = new ArrayList<String>();
    QueryExecution qExec = null;
    try {
        qExec = QueryExecutionFactory.sparqlService(sparqlEndpointUrl, query);
    } catch (QueryParseException qe) {
        String msg = ExceptionUtils.fixLineNumberInErrorMessage(qe.getLocalizedMessage());
        throw new NextProtException("Malformed SPARQL: " + msg);
    }
    ResultSet rs = qExec.execSelect();
    /**
     * This give an empty graph....
     * Model m = rs.getResourceModel();
     * Graph g = m.getGraph();
     * System.err.println("The graph is" + g);
     */
    Var x = Var.alloc("entry");
    while (rs.hasNext()) {
        Binding b = rs.nextBinding();
        Node entryNode = b.get(x);
        if (entryNode == null) {
            qExec.close();
            throw new NextProtException("Bind your protein result to a variable called ?entry. Example: \"?entry :classifiedWith cv:KW-0813.\"");
        } else if (entryNode.toString().indexOf(ENTRY_SUFFIX_URI) == -1) {
            qExec.close();
            throw new NextProtException("Any entry found in the output, however was found: " + entryNode.toString());
        }
        String entry = entryNode.toString().replace(ENTRY_SUFFIX_URI, "").trim();
        results.add(entry);
    }
    qExec.close();
    return results;
}
Also used : Binding(com.hp.hpl.jena.sparql.engine.binding.Binding) NextProtException(org.nextprot.api.commons.exception.NextProtException) Var(com.hp.hpl.jena.sparql.core.Var) Node(com.hp.hpl.jena.graph.Node) ArrayList(java.util.ArrayList) ResultSet(com.hp.hpl.jena.query.ResultSet) QueryExecution(com.hp.hpl.jena.query.QueryExecution) QueryParseException(com.hp.hpl.jena.query.QueryParseException) Cacheable(org.springframework.cache.annotation.Cacheable)

Example 2 with QueryParseException

use of com.hp.hpl.jena.query.QueryParseException in project nextprot-api by calipho-sib.

the class SparqlServiceImpl method sparqlSelect.

@Override
public SparqlResult sparqlSelect(String sparql, String sparqlEndpointUrl, int timeout, String queryTitle, String testId, ResultsFormat format) {
    SparqlResult result = null;
    try {
        QueryExecution qExec = QueryExecutionFactory.sparqlService(sparqlEndpointUrl, sparql);
        qExec.setTimeout(timeout);
        ResultSet rs = qExec.execSelect();
        result = SparqlUtils.convertResultToFormat(rs, format);
        qExec.close();
    } catch (QueryParseException qe) {
        String msg = ExceptionUtils.fixLineNumberInErrorMessage(qe.getLocalizedMessage());
        throw new NextProtException("Malformed SPARQL: " + msg);
    }
    return result;
}
Also used : NextProtException(org.nextprot.api.commons.exception.NextProtException) ResultSet(com.hp.hpl.jena.query.ResultSet) SparqlResult(org.nextprot.api.commons.utils.SparqlResult) QueryExecution(com.hp.hpl.jena.query.QueryExecution) QueryParseException(com.hp.hpl.jena.query.QueryParseException)

Aggregations

QueryExecution (com.hp.hpl.jena.query.QueryExecution)2 QueryParseException (com.hp.hpl.jena.query.QueryParseException)2 ResultSet (com.hp.hpl.jena.query.ResultSet)2 NextProtException (org.nextprot.api.commons.exception.NextProtException)2 Node (com.hp.hpl.jena.graph.Node)1 Var (com.hp.hpl.jena.sparql.core.Var)1 Binding (com.hp.hpl.jena.sparql.engine.binding.Binding)1 ArrayList (java.util.ArrayList)1 SparqlResult (org.nextprot.api.commons.utils.SparqlResult)1 Cacheable (org.springframework.cache.annotation.Cacheable)1