Search in sources :

Example 96 with ResultSet

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

the class FusekiBuilder method addServiceEP.

private static void addServiceEP(DataService dataService, OperationName opName, Resource svc, Property property) {
    String p = "<" + property.getURI() + ">";
    ResultSet rs = query("SELECT * { ?svc " + p + " ?ep}", svc.getModel(), "svc", svc);
    for (; rs.hasNext(); ) {
        QuerySolution soln = rs.next();
        String epName = soln.getLiteral("ep").getLexicalForm();
        Endpoint operation = new Endpoint(opName, epName);
        addServiceEP(dataService, opName, epName);
    //log.info("  " + opName.name + " = " + dataAccessPoint.getName() + "/" + epName) ;
    }
}
Also used : Endpoint(org.apache.jena.fuseki.server.Endpoint) QuerySolution(org.apache.jena.query.QuerySolution) ResultSet(org.apache.jena.query.ResultSet)

Example 97 with ResultSet

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

the class FusekiBuilder method getOne.

public static RDFNode getOne(Resource svc, String property) {
    String ln = property.substring(property.indexOf(':') + 1);
    ResultSet rs = FusekiLib.query("SELECT * { ?svc " + property + " ?x}", svc.getModel(), "svc", svc);
    if (!rs.hasNext())
        throw new FusekiConfigException("No " + ln + " for service " + FusekiLib.nodeLabel(svc));
    RDFNode x = rs.next().get("x");
    if (rs.hasNext())
        throw new FusekiConfigException("Multiple " + ln + " for service " + FusekiLib.nodeLabel(svc));
    return x;
}
Also used : FusekiConfigException(org.apache.jena.fuseki.FusekiConfigException) ResultSet(org.apache.jena.query.ResultSet) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 98 with ResultSet

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

the class TestEmbeddedFuseki method embedded_03.

// Different dataset name.
@Test
public void embedded_03() {
    DatasetGraph dsg = dataset();
    int port = FusekiLib.choosePort();
    FusekiEmbeddedServer server = FusekiEmbeddedServer.create().setPort(port).add("/ds1", dsg).build();
    server.start();
    try {
        // Add while live.
        Txn.executeWrite(dsg, () -> {
            Quad q = SSE.parseQuad("(_ :s :p _:b)");
            dsg.add(q);
        });
        query("http://localhost:" + port + "/ds1/query", "SELECT * { ?s ?p ?o}", qExec -> {
            ResultSet rs = qExec.execSelect();
            int x = ResultSetFormatter.consume(rs);
            assertEquals(1, x);
        });
    } finally {
        server.stop();
    }
}
Also used : Quad(org.apache.jena.sparql.core.Quad) ResultSet(org.apache.jena.query.ResultSet) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test)

Example 99 with ResultSet

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

the class TestMultipleEmbedded method multiple_03.

// Two servers, two datasets.
@Test
public void multiple_03() {
    DatasetGraph dsg1 = dataset();
    DatasetGraph dsg2 = dataset();
    // Same name.
    int port1 = FusekiLib.choosePort();
    FusekiEmbeddedServer server1 = FusekiEmbeddedServer.create().setPort(port1).add("/ds", dsg1).build().start();
    Txn.executeWrite(dsg1, () -> dsg1.add(q1));
    int port2 = FusekiLib.choosePort();
    FusekiEmbeddedServer server2 = FusekiEmbeddedServer.create().setPort(port2).add("/ds", dsg2).build().start();
    Txn.executeWrite(dsg2, () -> dsg2.add(q2));
    query("http://localhost:" + port1 + "/ds/", "SELECT * {?s ?p 1}", qExec -> {
        ResultSet rs = qExec.execSelect();
        int x = ResultSetFormatter.consume(rs);
        assertEquals(1, x);
    });
    query("http://localhost:" + port2 + "/ds/", "SELECT * {?s ?p 1}", qExec -> {
        ResultSet rs = qExec.execSelect();
        int x = ResultSetFormatter.consume(rs);
        assertEquals(0, x);
    });
    server1.stop();
    // server2 still running
    query("http://localhost:" + port2 + "/ds/", "SELECT * {?s ?p 2}", qExec -> {
        ResultSet rs = qExec.execSelect();
        int x = ResultSetFormatter.consume(rs);
        assertEquals(1, x);
    });
    server2.stop();
}
Also used : ResultSet(org.apache.jena.query.ResultSet) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test)

Example 100 with ResultSet

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

the class AlgebraExec method main.

public static void main(String[] argv) {
    String BASE = "http://example/";
    BasicPattern bp = new BasicPattern();
    Var var_x = Var.alloc("x");
    Var var_z = Var.alloc("z");
    // ---- Build expression
    bp.add(new Triple(var_x, NodeFactory.createURI(BASE + "p"), var_z));
    Op op = new OpBGP(bp);
    //Expr expr = ExprUtils.parse("?z < 2 ") ;
    Expr expr = new E_LessThan(new ExprVar(var_z), NodeValue.makeNodeInteger(2));
    op = OpFilter.filter(expr, op);
    // ---- Example setup
    Model m = makeModel();
    m.write(System.out, "TTL");
    System.out.println("--------------");
    System.out.print(op);
    System.out.println("--------------");
    // ---- Execute expression
    QueryIterator qIter = Algebra.exec(op, m.getGraph());
    // -------- Either read the query iterator directly ...
    if (false) {
        for (; qIter.hasNext(); ) {
            Binding b = qIter.nextBinding();
            Node n = b.get(var_x);
            System.out.println(NodeFmtLib.displayStr(n));
            System.out.println(b);
        }
        qIter.close();
    } else {
        // -------- Or make ResultSet from it (but not both - reading an
        //          iterator consumes the current solution)
        List<String> varNames = new ArrayList<>();
        varNames.add("x");
        varNames.add("z");
        ResultSet rs = new ResultSetStream(varNames, m, qIter);
        ResultSetFormatter.out(rs);
        qIter.close();
    }
    System.exit(0);
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) Binding(org.apache.jena.sparql.engine.binding.Binding) Op(org.apache.jena.sparql.algebra.Op) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) OpBGP(org.apache.jena.sparql.algebra.op.OpBGP) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) BasicPattern(org.apache.jena.sparql.core.BasicPattern) E_LessThan(org.apache.jena.sparql.expr.E_LessThan) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream) Triple(org.apache.jena.graph.Triple) Expr(org.apache.jena.sparql.expr.Expr) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) ResultSet(org.apache.jena.query.ResultSet)

Aggregations

ResultSet (org.apache.jena.query.ResultSet)166 QuerySolution (org.apache.jena.query.QuerySolution)107 QueryExecution (org.apache.jena.query.QueryExecution)105 Test (org.junit.Test)98 ArrayList (java.util.ArrayList)73 Query (org.apache.jena.query.Query)51 Dataset (org.apache.jena.query.Dataset)43 Resource (org.apache.jena.rdf.model.Resource)41 Literal (org.apache.jena.rdf.model.Literal)20 BuilderResultSet (org.apache.jena.sparql.sse.builders.BuilderResultSet)17 Model (org.apache.jena.rdf.model.Model)14 ByteArrayInputStream (java.io.ByteArrayInputStream)12 BaseTest (org.apache.jena.atlas.junit.BaseTest)12 Node (org.apache.jena.graph.Node)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 HashMap (java.util.HashMap)8 RDFNode (org.apache.jena.rdf.model.RDFNode)8 IOException (java.io.IOException)7 InputStream (java.io.InputStream)6 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)6