use of org.apache.jena.query.QueryExecution in project jena by apache.
the class QueryEngineTest method testRestrictedQueryType.
@Test
public void testRestrictedQueryType() {
final SecurityEvaluator eval = new MockSecurityEvaluator(true, true, true, true, true, true) {
@Override
public boolean evaluate(final 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 {
final String query = "prefix fn: <http://www.w3.org/2005/xpath-functions#> " + " SELECT ?foo ?bar WHERE " + " { ?foo a <http://example.com/class> ; " + "?bar [] ." + " } ";
try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
final ResultSet results = qexec.execSelect();
int count = 0;
for (; results.hasNext(); ) {
count++;
results.nextSolution();
}
Assert.assertEquals(4, count);
}
} finally {
model.close();
}
}
use of org.apache.jena.query.QueryExecution in project jena by apache.
the class SelectBuilderTest method testAggregatorsInSelect.
@Test
public void testAggregatorsInSelect() throws ParseException {
builder.addVar("?x").addVar("count(*)", "?c").addWhere("?x", "?p", "?o").addGroupBy("?x");
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(1));
m.add(r, m.getProperty("urn:p:two"), m.createTypedLiteral(3));
m.add(r, m.getProperty("urn:p:three"), m.createTypedLiteral(5));
QueryExecution qexec = QueryExecutionFactory.create(builder.build(), m);
ResultSet results = qexec.execSelect();
assertTrue(results.hasNext());
for (; results.hasNext(); ) {
QuerySolution soln = results.nextSolution();
assertTrue(soln.contains("c"));
assertTrue(soln.contains("x"));
assertEquals(3, soln.get("c").asLiteral().getInt());
}
builder.addVar("min(?o)", "?min").addVar("max(?o)", "?max");
qexec = QueryExecutionFactory.create(builder.build(), m);
results = qexec.execSelect();
assertTrue(results.hasNext());
for (; results.hasNext(); ) {
QuerySolution soln = results.nextSolution();
assertTrue(soln.contains("c"));
assertTrue(soln.contains("x"));
assertTrue(soln.contains("?min"));
assertEquals(3, soln.get("c").asLiteral().getInt());
assertEquals(1, soln.get("min").asLiteral().getInt());
assertEquals(5, soln.get("max").asLiteral().getInt());
}
}
use of org.apache.jena.query.QueryExecution in project jena by apache.
the class ExTDB4 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);
ResultSet results = qexec.execSelect();
ResultSetFormatter.out(results);
qexec.close();
dataset.close();
}
use of org.apache.jena.query.QueryExecution in project jena by apache.
the class ExTDB_Txn1 method execQuery.
public static void execQuery(String sparqlQueryString, Dataset dataset) {
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();
}
}
use of org.apache.jena.query.QueryExecution in project jena by apache.
the class rsparql method exec.
@Override
protected void exec() {
Query query = modQuery.getQuery();
try {
String serviceURL = modRemote.getServiceURL();
QueryExecution qe = QueryExecutionFactory.sparqlService(serviceURL, query);
if (modRemote.usePost())
HttpQuery.urlLimit = 0;
QueryExecUtils.executeQuery(query, qe, modResults.getResultsFormat());
} catch (QueryExceptionHTTP ex) {
throw new CmdException("HTTP Exeception", ex);
} catch (Exception ex) {
System.out.flush();
ex.printStackTrace(System.err);
}
}
Aggregations