Search in sources :

Example 1 with RowSet

use of org.apache.jena.sparql.exec.RowSet in project jena by apache.

the class Service method exec.

public static QueryIterator exec(OpService op, Context context) {
    if (context == null)
        context = emptyContext;
    if (context != null && context.isFalse(httpServiceAllowed))
        throw new QueryExecException("SERVICE execution disabled");
    if (!op.getService().isURI())
        throw new QueryExecException("Service URI not bound: " + op.getService());
    boolean silent = op.getSilent();
    // [QExec] Add getSubOpUnmodified();
    if (!op.getService().isURI())
        throw new QueryExecException("Service URI not bound: " + op.getService());
    String serviceURL = op.getService().getURI();
    Op opRemote = op.getSubOp();
    Query query;
    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();
        }
    }
    // 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 replaces variables by
    // values.
    // 
    // It is safer to rename/unrename than skipping SERVICE during rename
    // to avoid substituting hidden variables.
    Op opRestored = Rename.reverseVarRename(opRemote, true);
    query = OpAsQuery.asQuery(opRestored);
    // Transforming: Same object means "no change"
    boolean requiresRemapping = false;
    Map<Var, Var> varMapping = null;
    if (!opRestored.equals(opRemote)) {
        varMapping = new HashMap<>();
        Set<Var> originalVars = OpVars.visibleVars(op);
        Set<Var> remoteVars = OpVars.visibleVars(opRestored);
        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);
            }
        }
    }
    // -- Setup
    // boolean withCompression = context.isTrueOrUndef(httpQueryCompression);
    long timeoutMillis = timeoutFromContext(context);
    // RegistryServiceModifier is applied by QueryExecHTTP
    Params serviceParams = getServiceParamsFromContext(serviceURL, context);
    HttpClient httpClient = chooseHttpClient(serviceURL, context);
    QuerySendMode querySendMode = chooseQuerySendMode(serviceURL, context, QuerySendMode.asGetWithLimitBody);
    // -- End setup
    // Build the execution
    QueryExecHTTP qExec = QueryExecHTTP.newBuilder().endpoint(serviceURL).timeout(timeoutMillis, TimeUnit.MILLISECONDS).query(query).params(serviceParams).context(context).httpClient(httpClient).sendMode(querySendMode).build();
    try {
        // Detach from the network stream.
        RowSet rowSet = qExec.select().materialize();
        QueryIterator qIter = QueryIterPlainWrapper.create(rowSet);
        if (requiresRemapping)
            qIter = QueryIter.map(qIter, varMapping);
        return qIter;
    } catch (HttpException ex) {
        throw QueryExceptionHTTP.rewrap(ex);
    }
}
Also used : Op(org.apache.jena.sparql.algebra.Op) ElementSubQuery(org.apache.jena.sparql.syntax.ElementSubQuery) OpAsQuery(org.apache.jena.sparql.algebra.OpAsQuery) Query(org.apache.jena.query.Query) Var(org.apache.jena.sparql.core.Var) Element(org.apache.jena.sparql.syntax.Element) RowSet(org.apache.jena.sparql.exec.RowSet) HttpParams(org.apache.jena.sparql.engine.http.HttpParams) QueryExecException(org.apache.jena.query.QueryExecException) ElementSubQuery(org.apache.jena.sparql.syntax.ElementSubQuery) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) HttpClient(java.net.http.HttpClient) RegistryHttpClient(org.apache.jena.http.RegistryHttpClient) HttpException(org.apache.jena.atlas.web.HttpException)

Example 2 with RowSet

use of org.apache.jena.sparql.exec.RowSet in project jena by apache.

the class QueryExecHTTP method select.

@Override
public RowSet select() {
    checkNotClosed();
    check(QueryType.SELECT);
    RowSet rs = execRowSet();
    return rs;
}
Also used : RowSet(org.apache.jena.sparql.exec.RowSet)

Example 3 with RowSet

use of org.apache.jena.sparql.exec.RowSet in project jena by apache.

the class TestProtobufResultSet method test.

private static ResultSetRewindable test(ResultSetRewindable resultSet) {
    resultSet.reset();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ResultSetFormatter.output(out, resultSet, ResultSetLang.RS_Protobuf);
    resultSet.reset();
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    RowSet rs$ = ProtobufRDF.readRowSet(in);
    ResultSetRewindable resultSet2 = ResultSetFactory.makeRewindable(rs$);
    // Includes bnode labels.
    ResultSetCompare.equalsExact(resultSet, resultSet2);
    resultSet.reset();
    resultSet2.reset();
    return resultSet2;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BuilderRowSet(org.apache.jena.sparql.sse.builders.BuilderRowSet) RowSet(org.apache.jena.sparql.exec.RowSet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResultSetRewindable(org.apache.jena.query.ResultSetRewindable)

Example 4 with RowSet

use of org.apache.jena.sparql.exec.RowSet in project jena by apache.

the class RowSetReaderJSON method process.

private static QueryExecResult process(InputStream in, Context context) {
    if (context == null)
        context = ARQ.getContext();
    RowSetJSON exec = new RowSetJSON(context);
    exec.parse(in);
    if (exec.rows != null) {
        // RowSet rs = RowSetStream.create(exec.vars, exec.rows.iterator());
        RowSet rs = RowSetStream.create(exec.vars, exec.rows.iterator());
        return new QueryExecResult(rs);
    } else
        return new QueryExecResult(exec.booleanResult);
}
Also used : QueryExecResult(org.apache.jena.sparql.exec.QueryExecResult) RowSet(org.apache.jena.sparql.exec.RowSet)

Example 5 with RowSet

use of org.apache.jena.sparql.exec.RowSet in project jena by apache.

the class TestSolverTDB method solve_04.

@Test
public void solve_04() {
    // Below everything.
    RowSet rs1 = exec("(bgp (:a :p :a))");
    RowSet rs2 = results("empty");
    equals(rs1, rs2);
}
Also used : RowSet(org.apache.jena.sparql.exec.RowSet) ConfigTest(org.apache.jena.tdb2.ConfigTest) Test(org.junit.Test)

Aggregations

RowSet (org.apache.jena.sparql.exec.RowSet)43 Test (org.junit.Test)36 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)12 EnvTest (org.apache.jena.test.conn.EnvTest)10 QueryExec (org.apache.jena.sparql.exec.QueryExec)9 RDFLink (org.apache.jena.rdflink.RDFLink)7 ConfigTest (org.apache.jena.tdb.ConfigTest)7 ConfigTest (org.apache.jena.tdb2.ConfigTest)7 Binding (org.apache.jena.sparql.engine.binding.Binding)6 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 HttpRequestModifier (org.apache.jena.http.sys.HttpRequestModifier)2 ResultSetRewindable (org.apache.jena.query.ResultSetRewindable)2 Quad (org.apache.jena.sparql.core.Quad)2 Var (org.apache.jena.sparql.core.Var)2 QueryExecResult (org.apache.jena.sparql.exec.QueryExecResult)2 HttpClient (java.net.http.HttpClient)1 CSVParser (org.apache.jena.atlas.csv.CSVParser)1 HttpException (org.apache.jena.atlas.web.HttpException)1