use of org.apache.jena.query.QueryExecution in project jena by apache.
the class QueryEngineTest method testSelectAllType.
@Test
public void testSelectAllType() {
final SecurityEvaluator eval = new MockSecurityEvaluator(true, true, true, true, true, true) {
@Override
public boolean evaluate(Object principal, final Action action, final Node graphIRI, final Triple triple) {
if (triple.getSubject().isURI() && triple.getSubject().getURI().equals("http://example.com/resource/1")) {
return false;
}
return super.evaluate(principal, action, graphIRI, triple);
}
};
final SecuredModel model = Factory.getInstance(eval, "http://example.com/securedModel", baseModel);
try {
String query = "SELECT ?s ?p ?o WHERE " + " { ?s ?p ?o } ";
try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
final ResultSet results = qexec.execSelect();
int count = 0;
for (; results.hasNext(); ) {
count++;
results.nextSolution();
}
// 2x 3 values + type triple
Assert.assertEquals(8, count);
}
query = "SELECT ?s ?p ?o WHERE " + " { GRAPH ?g {?s ?p ?o } }";
try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
final ResultSet results = qexec.execSelect();
int count = 0;
for (; results.hasNext(); ) {
count++;
results.nextSolution();
}
// 2x 3 values + type triple
// no named graphs so no results.
Assert.assertEquals(0, count);
}
} finally {
model.close();
}
}
use of org.apache.jena.query.QueryExecution in project jena by apache.
the class StoreUtils method storeGraphNames.
public static Iterator<Node> storeGraphNames(Store store) {
List<Node> x = new ArrayList<Node>();
String qs = "SELECT ?g { GRAPH ?g { }}";
QueryExecution qExec = QueryExecutionFactory.create(qs, SDBFactory.connectDataset(store));
ResultSet rs = qExec.execSelect();
Var var_g = Var.alloc("g");
while (rs.hasNext()) {
Node n = rs.nextBinding().get(var_g);
x.add(n);
}
return x.iterator();
}
use of org.apache.jena.query.QueryExecution 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.QueryExecution 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());
}
}
}
use of org.apache.jena.query.QueryExecution 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());
}
}
Aggregations