use of com.hp.hpl.jena.query.ResultSet 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();
}
}
}
}
use of com.hp.hpl.jena.query.ResultSet 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) });
}
}
use of com.hp.hpl.jena.query.ResultSet 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();
}
}
}
}
use of com.hp.hpl.jena.query.ResultSet in project stanbol by apache.
the class RunSingleSPARQLTest method testCreateSPARQLQueryExecutionFactory.
/**
* Test of testCreateSPARQLQueryExecutionFactory() method, of class RunSingleSPARQL.
*/
@Test
public void testCreateSPARQLQueryExecutionFactory() {
Map<String, String> map = new HashMap<String, String>();
map.put("rdfs", "<http://www.w3.org/2000/01/rdf-schema#>");
map.put("xsd", "<http://www.w3.org/2000/01/rdf-schema#>");
map.put("owl", "<http://www.w3.org/2000/01/rdf-schema#>");
map.put("rdf", "<http://www.w3.org/1999/02/22-rdf-syntax-ns#>");
map.put("ex", "<http://www.semanticweb.org/ontologies/2010/6/ProvaParent.owl#>");
String query = "SELECT * WHERE {?p rdf:type ex:Person .}";
RunSingleSPARQL instance = new RunSingleSPARQL(owl, map);
QueryExecution queryExecution = instance.createSPARQLQueryExecutionFactory(query);
if (queryExecution == null) {
fail("Some errors occurred in createSPARQLQueryExecutionFactory of KReSRunSPARQL");
}
ResultSet result = queryExecution.execSelect();
if (result != null) {
int m = 0;
while (result.hasNext()) {
result.next();
m++;
}
queryExecution.close();
assertEquals(3, m);
// TODO review the generated test code and remove the default call to fail.
} else {
fail("Some errors occur in createSPARQLQueryExecutionFactory of KReSRunSPARQL");
}
}
Aggregations