use of org.apache.jena.query.ResultSet in project jena by apache.
the class TestEmbeddedFuseki method embedded_04.
@Test
public void embedded_04() {
DatasetGraph dsg = dataset();
Txn.executeWrite(dsg, () -> {
Quad q = SSE.parseQuad("(_ :s :p _:b)");
dsg.add(q);
});
// A service with just being able to do quads operations
// That is, GET, POST, PUT on "/data" in N-quads and TriG.
DataService dataService = new DataService(dsg);
dataService.addEndpoint(OperationName.Quads_RW, "");
dataService.addEndpoint(OperationName.Query, "");
dataService.addEndpoint(OperationName.Update, "");
int port = FusekiLib.choosePort();
FusekiEmbeddedServer server = FusekiEmbeddedServer.create().setPort(port).add("/data", dataService).build();
server.start();
try {
// Put data in.
String data = "(graph (:s :p 1) (:s :p 2) (:s :p 3))";
Graph g = SSE.parseGraph(data);
HttpEntity e = graphToHttpEntity(g);
HttpOp.execHttpPut("http://localhost:" + port + "/data", e);
// Get data out.
try (TypedInputStream in = HttpOp.execHttpGet("http://localhost:" + port + "/data")) {
Graph g2 = GraphFactory.createDefaultGraph();
RDFDataMgr.read(g2, in, RDFLanguages.contentTypeToLang(in.getContentType()));
assertTrue(g.isIsomorphicWith(g2));
}
// Query.
query("http://localhost:" + port + "/data", "SELECT * { ?s ?p ?o}", qExec -> {
ResultSet rs = qExec.execSelect();
int x = ResultSetFormatter.consume(rs);
assertEquals(3, x);
});
// Update
UpdateRequest req = UpdateFactory.create("CLEAR DEFAULT");
UpdateExecutionFactory.createRemote(req, "http://localhost:" + port + "/data").execute();
// Query again.
query("http://localhost:" + port + "/data", "SELECT * { ?s ?p ?o}", qExec -> {
ResultSet rs = qExec.execSelect();
int x = ResultSetFormatter.consume(rs);
assertEquals(0, x);
});
} finally {
server.stop();
}
}
use of org.apache.jena.query.ResultSet in project jena by apache.
the class ModResultsIn method getResultSet.
public ResultSet getResultSet() {
if (resultSet != null)
return resultSet;
if (resultsFilename == null) {
System.err.println("No result file name");
throw new TerminationException(1);
}
try {
if (resultsFilename.equals("-"))
return ResultSetFactory.load(System.in, inputFormat);
ResultSet rs = ResultSetFactory.load(resultsFilename, inputFormat);
if (rs == null) {
System.err.println("Failed to read the result set");
throw new TerminationException(9);
}
resultSet = rs;
return resultSet;
} catch (NotFoundException ex) {
System.err.println("File not found: " + resultsFilename);
throw new TerminationException(9);
} catch (ARQInternalErrorException intEx) {
System.err.println(intEx.getMessage());
if (intEx.getCause() != null) {
System.err.println("Cause:");
intEx.getCause().printStackTrace(System.err);
System.err.println();
}
intEx.printStackTrace(System.err);
throw new TerminationException(99);
}
}
use of org.apache.jena.query.ResultSet in project jena by apache.
the class QueryEngineTest method testOpenQueryType.
@Test
public void testOpenQueryType() {
final SecurityEvaluator eval = new MockSecurityEvaluator(true, true, true, true, true, true);
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(8, count);
}
} finally {
model.close();
}
}
use of org.apache.jena.query.ResultSet 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.ResultSet 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());
}
}
Aggregations