Search in sources :

Example 1 with ResultSet

use of org.apache.jena.query.ResultSet in project jena by apache.

the class QueryTest method unique.

private static ResultSetRewindable unique(ResultSetRewindable results) {
    // VERY crude.  Utilises the fact that bindings have value equality.
    List<Binding> x = new ArrayList<>();
    Set<Binding> seen = new HashSet<>();
    for (; results.hasNext(); ) {
        Binding b = results.nextBinding();
        if (seen.contains(b))
            continue;
        seen.add(b);
        x.add(b);
    }
    QueryIterator qIter = new QueryIterPlainWrapper(x.iterator());
    ResultSet rs = new ResultSetStream(results.getResultVars(), ModelFactory.createDefaultModel(), qIter);
    return ResultSetFactory.makeRewindable(rs);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) QueryIterPlainWrapper(org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) ArrayList(java.util.ArrayList) ResultSet(org.apache.jena.query.ResultSet) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream) HashSet(java.util.HashSet)

Example 2 with ResultSet

use of org.apache.jena.query.ResultSet in project jena by apache.

the class QueryTest method convertToStrings.

private ResultSetRewindable convertToStrings(ResultSetRewindable resultsActual) {
    List<Binding> bindings = new ArrayList<>();
    while (resultsActual.hasNext()) {
        Binding b = resultsActual.nextBinding();
        BindingMap b2 = BindingFactory.create();
        for (String vn : resultsActual.getResultVars()) {
            Var v = Var.alloc(vn);
            Node n = b.get(v);
            String s;
            if (n == null)
                s = "";
            else if (n.isBlank())
                s = "_:" + n.getBlankNodeLabel();
            else
                s = NodeFunctions.str(n);
            b2.add(v, NodeFactory.createLiteral(s));
        }
        bindings.add(b2);
    }
    ResultSet rs = new ResultSetStream(resultsActual.getResultVars(), null, new QueryIterPlainWrapper(bindings.iterator()));
    return ResultSetFactory.makeRewindable(rs);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) QueryIterPlainWrapper(org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper) Var(org.apache.jena.sparql.core.Var) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) ResultSet(org.apache.jena.query.ResultSet) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream)

Example 3 with ResultSet

use of org.apache.jena.query.ResultSet in project jena by apache.

the class StoreUtils method containsGraph.

public static boolean containsGraph(Store store, Node graphNode) {
    String qs = "SELECT * { GRAPH " + FmtUtils.stringForNode(graphNode) + " { ?s ?p ?o }} LIMIT 1";
    Dataset ds = SDBFactory.connectDataset(store);
    try (QueryExecution qExec = QueryExecutionFactory.create(qs, ds)) {
        ResultSet rs = qExec.execSelect();
        return rs.hasNext();
    }
}
Also used : Dataset(org.apache.jena.query.Dataset) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution)

Example 4 with ResultSet

use of org.apache.jena.query.ResultSet in project jena by apache.

the class TestMultipleEmbedded method multiple_04.

// Two servers, one dataset under two names.
@Test
public void multiple_04() {
    DatasetGraph dsg = dataset();
    int port1 = FusekiLib.choosePort();
    FusekiEmbeddedServer server1 = FusekiEmbeddedServer.create().setPort(port1).add("/ds1", dsg).build().start();
    Txn.executeWrite(dsg, () -> dsg.add(q1));
    int port2 = FusekiLib.choosePort();
    FusekiEmbeddedServer server2 = FusekiEmbeddedServer.create().setPort(port2).add("/ds2", dsg).build().start();
    Txn.executeWrite(dsg, () -> dsg.add(q2));
    query("http://localhost:" + port1 + "/ds1", "SELECT * {?s ?p ?o}", qExec -> {
        ResultSet rs = qExec.execSelect();
        int x = ResultSetFormatter.consume(rs);
        assertEquals(2, x);
    });
    query("http://localhost:" + port2 + "/ds2", "SELECT * {?s ?p ?o}", qExec -> {
        ResultSet rs = qExec.execSelect();
        int x = ResultSetFormatter.consume(rs);
        assertEquals(2, x);
    });
    server1.stop();
    server2.stop();
}
Also used : ResultSet(org.apache.jena.query.ResultSet) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test)

Example 5 with ResultSet

use of org.apache.jena.query.ResultSet in project jena by apache.

the class TestEmbeddedFuseki method embedded_01.

// Test - build on default port. 
@Test
public void embedded_01() {
    DatasetGraph dsg = dataset();
    // Default port.
    int port = 3330;
    FusekiEmbeddedServer server = FusekiEmbeddedServer.create().add("/ds", dsg).build();
    assertTrue(server.getDataAccessPointRegistry().isRegistered("/ds"));
    server.start();
    query("http://localhost:" + port + "/ds/query", "SELECT * { ?s ?p ?o}", qExec -> {
        ResultSet rs = qExec.execSelect();
        assertFalse(rs.hasNext());
    });
    server.stop();
}
Also used : ResultSet(org.apache.jena.query.ResultSet) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test)

Aggregations

ResultSet (org.apache.jena.query.ResultSet)162 QuerySolution (org.apache.jena.query.QuerySolution)103 QueryExecution (org.apache.jena.query.QueryExecution)101 Test (org.junit.Test)96 ArrayList (java.util.ArrayList)72 Query (org.apache.jena.query.Query)47 Dataset (org.apache.jena.query.Dataset)43 Resource (org.apache.jena.rdf.model.Resource)40 Literal (org.apache.jena.rdf.model.Literal)19 BuilderResultSet (org.apache.jena.sparql.sse.builders.BuilderResultSet)17 BaseTest (org.apache.jena.atlas.junit.BaseTest)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 Model (org.apache.jena.rdf.model.Model)10 Node (org.apache.jena.graph.Node)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 HashMap (java.util.HashMap)8 RDFNode (org.apache.jena.rdf.model.RDFNode)7 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)6 IOException (java.io.IOException)5 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)5