Search in sources :

Example 11 with ResultSet

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();
    }
}
Also used : Quad(org.apache.jena.sparql.core.Quad) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Graph(org.apache.jena.graph.Graph) HttpEntity(org.apache.http.HttpEntity) UpdateRequest(org.apache.jena.update.UpdateRequest) ResultSet(org.apache.jena.query.ResultSet) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) DataService(org.apache.jena.fuseki.server.DataService) Test(org.junit.Test)

Example 12 with ResultSet

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);
    }
}
Also used : TerminationException(jena.cmd.TerminationException) ARQInternalErrorException(org.apache.jena.sparql.ARQInternalErrorException) ResultSet(org.apache.jena.query.ResultSet) NotFoundException(org.apache.jena.shared.NotFoundException)

Example 13 with ResultSet

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();
    }
}
Also used : MockSecurityEvaluator(org.apache.jena.permissions.MockSecurityEvaluator) 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 14 with ResultSet

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();
    }
}
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 15 with ResultSet

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

Aggregations

ResultSet (org.apache.jena.query.ResultSet)79 Test (org.junit.Test)43 BuilderResultSet (org.apache.jena.sparql.sse.builders.BuilderResultSet)33 BaseTest (org.apache.jena.atlas.junit.BaseTest)27 QueryExecution (org.apache.jena.query.QueryExecution)18 QuerySolution (org.apache.jena.query.QuerySolution)15 ByteArrayInputStream (java.io.ByteArrayInputStream)13 Query (org.apache.jena.query.Query)11 ResultSetRewindable (org.apache.jena.query.ResultSetRewindable)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)8 ResultSetStream (org.apache.jena.sparql.engine.ResultSetStream)8 ArrayList (java.util.ArrayList)7 Node (org.apache.jena.graph.Node)7 Model (org.apache.jena.rdf.model.Model)6 ConfigTest (org.apache.jena.tdb.ConfigTest)6 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)5 Var (org.apache.jena.sparql.core.Var)5 NodeAlignment (mom.trd.opentheso.bdd.helper.nodes.NodeAlignment)4 Binding (org.apache.jena.sparql.engine.binding.Binding)4