use of org.apache.jena.query.QuerySolution in project jena by apache.
the class ResultSetApply method apply.
public void apply() {
proc.start(rs);
for (; rs.hasNext(); ) {
QuerySolution qs = rs.next();
proc.start(qs);
for (String varName : rs.getResultVars()) {
RDFNode node = qs.get(varName);
// node may be null
proc.binding(varName, node);
}
proc.finish(qs);
}
proc.finish(rs);
}
use of org.apache.jena.query.QuerySolution in project jena by apache.
the class ResultSetUtils method resultSetToStringList.
/**
* Extracts a List filled with the binding of selectElement variable for each
* query solution, turned into a string (URIs or lexical forms).
* Exhausts the result set. Create a rewindable one to use multiple times.
* @see org.apache.jena.query.ResultSetFactory
*/
public static List<String> resultSetToStringList(ResultSet rs, String selectElement, String literalOrResource) {
// feature suggested by James Howison
List<String> items = new ArrayList<>();
while (rs.hasNext()) {
QuerySolution qs = rs.nextSolution();
RDFNode rn = qs.get(selectElement);
if (rn.isLiteral())
items.add(((Literal) rn).getLexicalForm());
else if (rn.isURIResource())
items.add(((Resource) rn).getURI());
else if (rn.isAnon()) {
items.add(((Resource) rn).getId().getLabelString());
} else
throw new ARQException("Unknow thing in results : " + rn);
}
return items;
}
use of org.apache.jena.query.QuerySolution in project jena by apache.
the class ResultSetUtils method resultSetToList.
/**
* Extracts a List filled with the binding of selectElement variable for each
* query solution as RDFNodes (Resources or Literals).
* Exhausts the result set. Create a rewindable one to use multiple times.
*
* @see org.apache.jena.query.ResultSetFactory
*/
public static List<RDFNode> resultSetToList(ResultSet rs, String selectElement) {
// feature suggested by James Howison
List<RDFNode> items = new ArrayList<>();
while (rs.hasNext()) {
QuerySolution qs = rs.nextSolution();
RDFNode n = qs.get(selectElement);
items.add(n);
}
return items;
}
use of org.apache.jena.query.QuerySolution in project jena by apache.
the class ExTDB5 method main.
public static void main(String... argv) {
// Direct way: Make a TDB-back Jena model in the named directory.
String directory = "MyDatabases/DB1";
Dataset dataset = TDBFactory.createDataset(directory);
// Potentially expensive query.
String sparqlQueryString = "SELECT (count(*) AS ?count) { ?s ?p ?o }";
// See http://incubator.apache.org/jena/documentation/query/app_api.html
Query query = QueryFactory.create(sparqlQueryString);
QueryExecution qexec = QueryExecutionFactory.create(query, dataset);
try {
ResultSet results = qexec.execSelect();
for (; results.hasNext(); ) {
QuerySolution soln = results.nextSolution();
int count = soln.getLiteral("count").getInt();
System.out.println("count = " + count);
}
} finally {
qexec.close();
}
// Close the dataset.
dataset.close();
}
use of org.apache.jena.query.QuerySolution in project jena by apache.
the class SelectBuilderTest method testVarReplacementInSubQuery.
@Test
public void testVarReplacementInSubQuery() throws ParseException {
Model m = ModelFactory.createDefaultModel();
Resource r = m.createResource("urn:one");
m.add(r, m.getProperty("urn:p:one"), m.createTypedLiteral(1));
m.add(r, m.getProperty("urn:p:two"), m.createTypedLiteral(3));
m.add(r, m.getProperty("urn:p:three"), m.createTypedLiteral(5));
r = m.createResource("urn:two");
m.add(r, m.getProperty("urn:p:one"), m.createTypedLiteral(2));
m.add(r, m.getProperty("urn:p:two"), m.createTypedLiteral(4));
m.add(r, m.getProperty("urn:p:three"), m.createTypedLiteral(6));
SelectBuilder sb = new SelectBuilder().addVar("?x").addVar("?p").addWhere("?x", "?p", "?o").addFilter("?o < ?limit");
builder.addPrefix("xsd", XSD.getURI()).addVar("?x").addVar("count(?p)", "?c").addWhere("?x", "?p", "?o2").addSubQuery(sb).addGroupBy("?x");
builder.setVar("?limit", 4);
QueryExecution qexec = QueryExecutionFactory.create(builder.build(), m);
ResultSet results = qexec.execSelect();
assertTrue(results.hasNext());
for (; results.hasNext(); ) {
QuerySolution soln = results.nextSolution();
assertTrue(soln.contains("x"));
assertTrue(soln.contains("c"));
if ("urn:one".equals(soln.get("?x").asResource().getURI())) {
assertEquals(2, soln.get("?c").asLiteral().getInt());
} else {
assertEquals(1, soln.get("?c").asLiteral().getInt());
}
}
}
Aggregations