Search in sources :

Example 56 with ResultSet

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

the class FusekiConfig method readSystemDatabase.

// ---- System database
/** Read the system database */
public static List<DataAccessPoint> readSystemDatabase(Dataset ds) {
    DatasetDescriptionRegistry dsDescMap = FusekiServer.registryForBuild();
    String qs = StrUtils.strjoinNL(SystemState.PREFIXES, "SELECT * {", "  GRAPH ?g {", "     ?s fu:name ?name ;", "        fu:status ?status .", "  }", "}");
    List<DataAccessPoint> refs = new ArrayList<>();
    ds.begin(ReadWrite.WRITE);
    try {
        ResultSet rs = FusekiLib.query(qs, ds);
        for (; rs.hasNext(); ) {
            QuerySolution row = rs.next();
            Resource s = row.getResource("s");
            Resource g = row.getResource("g");
            Resource rStatus = row.getResource("status");
            //String name = row.getLiteral("name").getLexicalForm() ;
            DatasetStatus status = DatasetStatus.status(rStatus);
            Model m = ds.getNamedModel(g.getURI());
            // Rebase the resource of the service description to the containing graph.
            Resource svc = m.wrapAsResource(s.asNode());
            DataAccessPoint ref = FusekiBuilder.buildDataAccessPoint(svc, dsDescMap);
            refs.add(ref);
        }
        ds.commit();
        return refs;
    } finally {
        ds.end();
    }
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) ArrayList(java.util.ArrayList) ResultSet(org.apache.jena.query.ResultSet)

Example 57 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 58 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 59 with ResultSet

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

the class EvaluatorSimple method dump.

private static void dump(Table table) {
    System.out.println("Table: " + Lib.className(table));
    QueryIterator qIter = table.iterator(null);
    ResultSet rs = new ResultSetStream(table.getVarNames(), null, table.iterator(null));
    ResultSetFormatter.out(rs);
}
Also used : QueryIterator(org.apache.jena.sparql.engine.QueryIterator) ResultSet(org.apache.jena.query.ResultSet) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream)

Example 60 with ResultSet

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

the class Service method exec.

/**
     * Executes a service operator
     * 
     * @param op
     *            Service
     * @param context
     *            Context
     * @return Query iterator of service results
     */
public static QueryIterator exec(OpService op, Context context) {
    if (context != null && context.isFalse(serviceAllowed))
        throw new QueryExecException("SERVICE execution disabled");
    if (!op.getService().isURI())
        throw new QueryExecException("Service URI not bound: " + op.getService());
    // This relies on the observation that the query was originally correct,
    // so reversing the scope renaming is safe (it merely restores the
    // algebra expression).
    // Any variables that reappear should be internal ones that were hidden
    // by renaming in the first place.
    // Any substitution is also safe because it replaced variables by
    // values.
    Op opRemote = Rename.reverseVarRename(op.getSubOp(), true);
    // JENA-494 There is a bug here that the renaming means that if this is
    // deeply nested and joined to other things at the same level of you end
    // up with the variables being disjoint and the same results
    // The naive fix for this is to map the variables visible in the inner
    // operator to those visible in the rewritten operator
    // There may be some cases where the re-mapping is incorrect due to
    // deeply nested SERVICE clauses
    Map<Var, Var> varMapping = new HashMap<>();
    Set<Var> originalVars = OpVars.visibleVars(op);
    Set<Var> remoteVars = OpVars.visibleVars(opRemote);
    boolean requiresRemapping = false;
    for (Var v : originalVars) {
        if (v.getName().contains("/")) {
            // A variable which was scope renamed so has a different name
            String origName = v.getName().substring(v.getName().lastIndexOf('/') + 1);
            Var remoteVar = Var.alloc(origName);
            if (remoteVars.contains(remoteVar)) {
                varMapping.put(remoteVar, v);
                requiresRemapping = true;
            }
        } else {
            // A variable which does not have a different name
            if (remoteVars.contains(v))
                varMapping.put(v, v);
        }
    }
    // Explain.explain("HTTP", opRemote, context) ;
    Query query;
    //@formatter:off
    // Comment (for the future?)
    //        if ( false )
    //        {
    //            // ***** Interacts with substitution.
    //            Element el = op.getServiceElement().getElement() ;
    //            if ( el instanceof ElementSubQuery )
    //                query = ((ElementSubQuery)el).getQuery() ;
    //            else
    //            {
    //                query = QueryFactory.create() ;
    //                query.setQueryPattern(el) ;
    //                query.setResultVars() ;
    //            }
    //        }
    //        else
    //@formatter:on
    query = OpAsQuery.asQuery(opRemote);
    Explain.explain("HTTP", query, context);
    String uri = op.getService().getURI();
    HttpQuery httpQuery = configureQuery(uri, context, query);
    InputStream in = httpQuery.exec();
    // Read the whole of the results now.
    // Avoids the problems with calling back into the same system e.g.
    // Fuseki+SERVICE <http://localhost:3030/...>
    ResultSet rs = ResultSetFactory.fromXML(in);
    QueryIterator qIter = QueryIter.materialize(new QueryIteratorResultSet(rs));
    // And close connection now, not when qIter is closed.
    IO.close(in);
    // nested SERVICE clauses
    if (requiresRemapping) {
        qIter = QueryIter.map(qIter, varMapping);
    }
    return qIter;
}
Also used : Op(org.apache.jena.sparql.algebra.Op) OpAsQuery(org.apache.jena.sparql.algebra.OpAsQuery) Query(org.apache.jena.query.Query) HashMap(java.util.HashMap) Var(org.apache.jena.sparql.core.Var) InputStream(java.io.InputStream) QueryExecException(org.apache.jena.query.QueryExecException) QueryIteratorResultSet(org.apache.jena.sparql.engine.iterator.QueryIteratorResultSet) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) QueryIteratorResultSet(org.apache.jena.sparql.engine.iterator.QueryIteratorResultSet) ResultSet(org.apache.jena.query.ResultSet)

Aggregations

ResultSet (org.apache.jena.query.ResultSet)75 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)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 QuerySolution (org.apache.jena.query.QuerySolution)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 Query (org.apache.jena.query.Query)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 Binding (org.apache.jena.sparql.engine.binding.Binding)4 AbstractRegexpBasedTest (org.apache.jena.arq.AbstractRegexpBasedTest)3