Search in sources :

Example 51 with QuerySolution

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);
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) Dataset(org.apache.jena.query.Dataset) Literal(org.apache.jena.rdf.model.Literal) ArrayList(java.util.ArrayList) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test)

Example 52 with QuerySolution

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);
        }
    }
}
Also used : Query(org.apache.jena.query.Query) QuerySolution(org.apache.jena.query.QuerySolution) Dataset(org.apache.jena.query.Dataset) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution)

Example 53 with QuerySolution

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());
    }
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test) AbstractRegexpBasedTest(org.apache.jena.arq.AbstractRegexpBasedTest)

Example 54 with QuerySolution

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;
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) ResultSet(org.apache.jena.query.ResultSet)

Example 55 with QuerySolution

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;
}
Also used : Password(org.eclipse.jetty.util.security.Password) DatasetGraphFactory(org.apache.jena.sparql.core.DatasetGraphFactory) java.util(java.util) SetUtils(org.apache.jena.atlas.lib.SetUtils) DataAccessCtl(org.apache.jena.fuseki.access.DataAccessCtl) BeforeClass(org.junit.BeforeClass) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Credential(org.eclipse.jetty.util.security.Credential) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) JettyLib(org.apache.jena.fuseki.jetty.JettyLib) FusekiServer(org.apache.jena.fuseki.main.FusekiServer) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) RDFNode(org.apache.jena.rdf.model.RDFNode) QueryExceptionHTTP(org.apache.jena.sparql.engine.http.QueryExceptionHTTP) Model(org.apache.jena.rdf.model.Model) UserStore(org.eclipse.jetty.security.UserStore) QuerySolution(org.apache.jena.query.QuerySolution) Quad(org.apache.jena.sparql.core.Quad) TDBFactory(org.apache.jena.tdb.TDBFactory) Assert.fail(org.junit.Assert.fail) Iter(org.apache.jena.atlas.iterator.Iter) Parameterized(org.junit.runners.Parameterized) SecurityContextView(org.apache.jena.fuseki.access.SecurityContextView) AfterClass(org.junit.AfterClass) SecurityRegistry(org.apache.jena.fuseki.access.SecurityRegistry) HttpException(org.apache.jena.atlas.web.HttpException) SecurityContext(org.apache.jena.fuseki.access.SecurityContext) DatabaseMgr(org.apache.jena.tdb2.DatabaseMgr) Test(org.junit.Test) AccessTestLib(org.apache.jena.fuseki.main.access.AccessTestLib) Node(org.apache.jena.graph.Node) RDFConnection(org.apache.jena.rdfconnection.RDFConnection) Assert.assertEquals(org.junit.Assert.assertEquals) RDFConnection(org.apache.jena.rdfconnection.RDFConnection) QuerySolution(org.apache.jena.query.QuerySolution) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node)

Aggregations

QuerySolution (org.apache.jena.query.QuerySolution)125 ResultSet (org.apache.jena.query.ResultSet)103 QueryExecution (org.apache.jena.query.QueryExecution)96 ArrayList (java.util.ArrayList)70 Test (org.junit.Test)69 Dataset (org.apache.jena.query.Dataset)44 Query (org.apache.jena.query.Query)44 Resource (org.apache.jena.rdf.model.Resource)43 Literal (org.apache.jena.rdf.model.Literal)21 RDFNode (org.apache.jena.rdf.model.RDFNode)20 Model (org.apache.jena.rdf.model.Model)12 HashMap (java.util.HashMap)7 ResultSetRewindable (org.apache.jena.query.ResultSetRewindable)6 Extractor (infoeval.main.WikiData.Extractor)4 Node (org.apache.jena.graph.Node)4 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)4 IOException (java.io.IOException)3 Date (java.sql.Date)3 SimpleDateFormat (java.text.SimpleDateFormat)3 LinkedList (java.util.LinkedList)3