use of org.apache.jena.query.QuerySolution in project jena by apache.
the class SelectBuilderTest method testAggregatorsInSubQuery.
@Test
public void testAggregatorsInSubQuery() 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("max(?o)", "?max").addWhere("?x", "?p", "?o").addGroupBy("?x");
builder.addPrefix("xsd", XSD.getURI()).addVar("?x").addVar("min(?o2)", "?min").addWhere("?x", "?p2", "?o2").addSubQuery(sb).addFilter("?max = '6'^^xsd:int").addGroupBy("?x");
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("min"));
assertEquals("urn:two", soln.get("?x").asResource().getURI());
assertEquals(2, soln.get("?min").asLiteral().getInt());
}
}
use of org.apache.jena.query.QuerySolution in project jena by apache.
the class JenaOSGITest method runQuery.
private void runQuery(Dataset dataset) {
Query query = QueryFactory.create("" + "PREFIX foaf: <http://xmlns.com/foaf/0.1/>" + "SELECT ?bob WHERE { " + " GRAPH <http://example.com/graph> { " + " ?alice foaf:knows ?bob . " + " }" + "}");
try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
ResultSet results = qexec.execSelect();
assertTrue(results.hasNext());
QuerySolution r = results.next();
assertEquals(bob, r.get("bob").asResource());
}
}
use of org.apache.jena.query.QuerySolution in project jena by apache.
the class TestDatasetWithLuceneStoredLiterals method doTestSearchWithLiteralsMultiple.
protected List<Node> doTestSearchWithLiteralsMultiple(String turtle, String queryString, String expectedEntityURI) {
List<Node> literals = new LinkedList<>();
Model model = dataset.getDefaultModel();
Reader reader = new StringReader(turtle);
dataset.begin(ReadWrite.WRITE);
model.read(reader, "", "TURTLE");
dataset.commit();
Query query = QueryFactory.create(queryString);
dataset.begin(ReadWrite.READ);
try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
ResultSet results = qexec.execSelect();
int count;
for (count = 0; results.hasNext(); count++) {
QuerySolution soln = results.nextSolution();
String entityURI = soln.getResource("s").getURI();
assertEquals(expectedEntityURI, entityURI);
Literal literal = soln.getLiteral("literal");
assertNotNull(literal);
literals.add(literal.asNode());
}
} finally {
dataset.end();
}
return literals;
}
Aggregations