Search in sources :

Example 66 with QueryIterator

use of org.apache.jena.sparql.engine.QueryIterator in project jena by apache.

the class SpatialOperationPFBase method exec.

@Override
public QueryIterator exec(Binding binding, PropFuncArg argSubject, Node predicate, PropFuncArg argObject, ExecutionContext execCxt) {
    if (server == null) {
        if (!warningIssued) {
            Log.warn(getClass(), "No spatial index - no spatial search performed");
            warningIssued = true;
        }
        // Not a text dataset - no-op
        return IterLib.result(binding, execCxt);
    }
    argSubject = Substitute.substitute(argSubject, binding);
    argObject = Substitute.substitute(argObject, binding);
    if (!argSubject.isNode())
        throw new InternalErrorException("Subject is not a node (it was earlier!)");
    Node s = argSubject.getArg();
    if (s.isLiteral())
        // Does not match
        return IterLib.noResults(execCxt);
    SpatialMatch match = objectToStruct(argObject);
    if (match == null) {
        // can't match
        return IterLib.noResults(execCxt);
    }
    // ----
    QueryIterator qIter = (Var.isVar(s)) ? variableSubject(binding, s, match, execCxt) : concreteSubject(binding, s, match, execCxt);
    if (match.getLimit() >= 0)
        qIter = new QueryIterSlice(qIter, 0, match.getLimit(), execCxt);
    return qIter;
}
Also used : QueryIterSlice(org.apache.jena.sparql.engine.iterator.QueryIterSlice) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) Node(org.apache.jena.graph.Node) InternalErrorException(org.apache.jena.atlas.lib.InternalErrorException)

Example 67 with QueryIterator

use of org.apache.jena.sparql.engine.QueryIterator 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)

Example 68 with QueryIterator

use of org.apache.jena.sparql.engine.QueryIterator in project jena by apache.

the class QueryIterConcat method output.

@Override
public void output(IndentedWriter out, SerializationContext sCxt) {
    out.println(Lib.className(this));
    out.incIndent();
    for (QueryIterator qIter : iteratorList) {
        qIter.output(out, sCxt);
    }
    out.decIndent();
    out.ensureStartOfLine();
}
Also used : QueryIterator(org.apache.jena.sparql.engine.QueryIterator)

Example 69 with QueryIterator

use of org.apache.jena.sparql.engine.QueryIterator in project jena by apache.

the class QueryIterProjectMerge method nextStage.

@Override
protected QueryIterator nextStage(Binding binding) {
    QueryIterator qIter = engine.executeOp(opProject.getSubOp(), QueryIterSingleton.create(binding, getExecContext()));
    qIter = new QueryIterConvert(qIter, new ProjectEnsureBindingConverter(binding, opProject.getVars()), getExecContext());
    return qIter;
}
Also used : QueryIterator(org.apache.jena.sparql.engine.QueryIterator)

Example 70 with QueryIterator

use of org.apache.jena.sparql.engine.QueryIterator in project jena by apache.

the class QueryExecUtils method execute.

public static void execute(Op op, DatasetGraph dsg, ResultsFormat outputFormat) {
    QueryIterator qIter = Algebra.exec(op, dsg);
    List<String> vars = null;
    if (op instanceof OpProject)
        vars = Var.varNames(((OpProject) op).getVars());
    else
        // The variables defined in patterns (not Filters, nor NOT EXISTS,
        // nor ORDER BY)
        vars = Var.varNames(OpVars.visibleVars(op));
    ResultSet results = ResultSetFactory.create(qIter, vars);
    outputResultSet(results, null, outputFormat);
}
Also used : OpProject(org.apache.jena.sparql.algebra.op.OpProject) QueryIterator(org.apache.jena.sparql.engine.QueryIterator)

Aggregations

QueryIterator (org.apache.jena.sparql.engine.QueryIterator)126 Binding (org.apache.jena.sparql.engine.binding.Binding)27 Op (org.apache.jena.sparql.algebra.Op)15 Node (org.apache.jena.graph.Node)13 TableN (org.apache.jena.sparql.algebra.table.TableN)13 ArrayList (java.util.ArrayList)12 ResultSetStream (org.apache.jena.sparql.engine.ResultSetStream)10 Var (org.apache.jena.sparql.core.Var)9 ResultSet (org.apache.jena.query.ResultSet)8 QueryIterPlainWrapper (org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper)8 Triple (org.apache.jena.graph.Triple)6 ExecutionContext (org.apache.jena.sparql.engine.ExecutionContext)6 BasicPattern (org.apache.jena.sparql.core.BasicPattern)5 QueryIterConcat (org.apache.jena.sparql.engine.iterator.QueryIterConcat)5 Table (org.apache.jena.sparql.algebra.Table)4 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)4 HashSet (java.util.HashSet)3 Plan (org.apache.jena.sparql.engine.Plan)3 BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)3 Procedure (org.apache.jena.sparql.procedure.Procedure)3