Search in sources :

Example 6 with QueryExecution

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();
    }
}
Also used : Triple(org.apache.jena.graph.Triple) MockSecurityEvaluator(org.apache.jena.permissions.MockSecurityEvaluator) Node(org.apache.jena.graph.Node) ResultSet(org.apache.jena.query.ResultSet) SecurityEvaluator(org.apache.jena.permissions.SecurityEvaluator) MockSecurityEvaluator(org.apache.jena.permissions.MockSecurityEvaluator) SecuredModel(org.apache.jena.permissions.model.SecuredModel) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test)

Example 7 with QueryExecution

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());
    }
}
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 8 with QueryExecution

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

Example 9 with QueryExecution

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

Example 10 with QueryExecution

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);
    }
}
Also used : HttpQuery(org.apache.jena.sparql.engine.http.HttpQuery) Query(org.apache.jena.query.Query) CmdException(jena.cmd.CmdException) QueryExceptionHTTP(org.apache.jena.sparql.engine.http.QueryExceptionHTTP) QueryExecution(org.apache.jena.query.QueryExecution) CmdException(jena.cmd.CmdException)

Aggregations

QueryExecution (org.apache.jena.query.QueryExecution)17 ResultSet (org.apache.jena.query.ResultSet)14 Query (org.apache.jena.query.Query)9 QuerySolution (org.apache.jena.query.QuerySolution)8 Test (org.junit.Test)7 Model (org.apache.jena.rdf.model.Model)5 Node (org.apache.jena.graph.Node)4 Dataset (org.apache.jena.query.Dataset)4 Resource (org.apache.jena.rdf.model.Resource)4 AbstractRegexpBasedTest (org.apache.jena.arq.AbstractRegexpBasedTest)3 MockSecurityEvaluator (org.apache.jena.permissions.MockSecurityEvaluator)3 SecurityEvaluator (org.apache.jena.permissions.SecurityEvaluator)3 SecuredModel (org.apache.jena.permissions.model.SecuredModel)3 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 Triple (org.apache.jena.graph.Triple)2 Literal (org.apache.jena.rdf.model.Literal)2 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 CmdException (jena.cmd.CmdException)1