Search in sources :

Example 1 with Var

use of com.hp.hpl.jena.sparql.core.Var 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 Var

use of com.hp.hpl.jena.sparql.core.Var in project stanbol by apache.

the class RdfIndexingSource method debug.

public void debug() {
    String entityVar = "s";
    String fieldVar = "p";
    String valueVar = "o";
    StringBuilder qb = new StringBuilder();
    qb.append(String.format("SELECT ?%s ?%s ?%s \n", entityVar, fieldVar, // for the select
    valueVar));
    qb.append("{ \n");
    qb.append(String.format("    ?%s ?%s ?%s . \n", entityVar, fieldVar, // for the where
    valueVar));
    qb.append("} \n");
    log.debug("EntityDataIterator Query: \n" + qb.toString());
    Query q = QueryFactory.create(qb.toString(), Syntax.syntaxARQ);
    ResultSet rs = QueryExecutionFactory.create(q, indexingDataset.toDataset()).execSelect();
    Var s = Var.alloc(entityVar);
    Var p = Var.alloc(fieldVar);
    Var o = Var.alloc(valueVar);
    while (rs.hasNext()) {
        Binding b = rs.nextBinding();
        log.debug("{} {} {}", new Object[] { b.get(s), b.get(p), b.get(o) });
    }
}
Also used : Binding(com.hp.hpl.jena.sparql.engine.binding.Binding) Query(com.hp.hpl.jena.query.Query) Var(com.hp.hpl.jena.sparql.core.Var) ResultSet(com.hp.hpl.jena.query.ResultSet)

Aggregations

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