use of org.apache.jena.query.QuerySolution in project jena by apache.
the class NearbyFFTest method testExec_query.
/**
* Test of exec method, of class NearbyFF.
*/
@Test
public void testExec_query() {
Dataset dataset = SpatialIndexTestData.createTestDataset();
String query = "PREFIX spatialF: <http://jena.apache.org/function/spatial#>\n" + "\n" + "SELECT ?result\n" + "WHERE{\n" + " BIND( \"<http://www.opengis.net/def/crs/EPSG/0/4326> POINT(51.50853 -0.12574)\"^^<http://www.opengis.net/ont/geosparql#wktLiteral> AS ?geom1)" + " BIND( \"<http://www.opengis.net/def/crs/EPSG/0/4326> POINT(48.857487 2.373047)\"^^<http://www.opengis.net/ont/geosparql#wktLiteral> AS ?geom2)" + " BIND( spatialF:nearby(?geom1, ?geom2, 345, <http://www.opengis.net/def/uom/OGC/1.0/kilometer>) AS ?result) \n" + "}ORDER by ?result";
List<Literal> results = new ArrayList<>();
try (QueryExecution qe = QueryExecutionFactory.create(query, dataset)) {
ResultSet rs = qe.execSelect();
while (rs.hasNext()) {
QuerySolution qs = rs.nextSolution();
Literal result = qs.getLiteral("result");
results.add(result);
}
}
List<Literal> expResults = Arrays.asList(ResourceFactory.createTypedLiteral(Boolean.TRUE.toString(), XSDDatatype.XSDboolean));
//
//
assertEquals(expResults, results);
}
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);
try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
ResultSet results = qexec.execSelect();
for (; results.hasNext(); ) {
QuerySolution soln = results.nextSolution();
int count = soln.getLiteral("count").getInt();
System.out.println("count = " + count);
}
}
}
use of org.apache.jena.query.QuerySolution 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.QuerySolution in project jena by apache.
the class FusekiConfig method servicesAndDatasets$.
private static List<DataAccessPoint> servicesAndDatasets$(Resource server, Model model) {
DatasetDescriptionMap dsDescMap = new DatasetDescriptionMap();
// ---- Services
// Server to services.
ResultSet rs = BuildLib.query("SELECT * { ?s fu:services [ list:member ?service ] }", model, "s", server);
List<DataAccessPoint> accessPoints = new ArrayList<>();
// If none, look for services by type.
if (!rs.hasNext())
// No "fu:services ( .... )" so try looking for services directly.
// This means Fuseki2, service configuration files (no server section) work for --conf.
rs = BuildLib.query("SELECT ?service { ?service a fu:Service }", model);
// rs is a result set of services to process.
for (; rs.hasNext(); ) {
QuerySolution soln = rs.next();
Resource svc = soln.getResource("service");
DataAccessPoint acc = buildDataAccessPoint(svc, dsDescMap);
if (acc != null)
accessPoints.add(acc);
}
return accessPoints;
}
use of org.apache.jena.query.QuerySolution in project jena by apache.
the class TestSecurityFilterFuseki method query.
private Set<Node> query(String user, String password, String queryString) {
Set<Node> results = new HashSet<>();
try (RDFConnection conn = RDFConnection.connectPW(baseUrl, user, password)) {
conn.queryResultSet(queryString, rs -> {
List<QuerySolution> list = Iter.toList(rs);
list.stream().map(qs -> qs.get("s")).filter(Objects::nonNull).map(RDFNode::asNode).forEach(n -> results.add(n));
});
}
return results;
}
Aggregations