Search in sources :

Example 36 with ExecutionContext

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

the class RefEval method evalQuadPattern.

static Table evalQuadPattern(OpQuadPattern opQuad, Evaluator evaluator) {
    if (opQuad.isEmpty())
        return TableFactory.createUnit();
    ExecutionContext cxt = evaluator.getExecContext();
    DatasetGraph ds = cxt.getDataset();
    BasicPattern pattern = opQuad.getBasicPattern();
    if (!opQuad.getGraphNode().isVariable()) {
        if (!opQuad.getGraphNode().isURI()) {
            throw new ARQInternalErrorException("Not a URI or variable: " + opQuad.getGraphNode());
        }
        Graph g = null;
        if (opQuad.isDefaultGraph())
            g = ds.getDefaultGraph();
        else
            g = ds.getGraph(opQuad.getGraphNode());
        if (g == null)
            return new TableEmpty();
        ExecutionContext cxt2 = new ExecutionContext(cxt, g);
        QueryIterator qIter = executeBGP(pattern, QueryIterRoot.create(cxt2), cxt2);
        return TableFactory.create(qIter);
    } else {
        // Variable.
        Var gVar = Var.alloc(opQuad.getGraphNode());
        // Or just just devolve to OpGraph and get OpUnion chain of OpJoin
        QueryIterConcat concat = new QueryIterConcat(cxt);
        for (Iterator<Node> graphNodes = cxt.getDataset().listGraphNodes(); graphNodes.hasNext(); ) {
            Node gn = graphNodes.next();
            // Op tableVarURI = TableFactory.create(gn.getName(), Node.createURI(uri)) ;
            Graph g = cxt.getDataset().getGraph(gn);
            Binding b = BindingFactory.binding(BindingRoot.create(), gVar, gn);
            ExecutionContext cxt2 = new ExecutionContext(cxt, g);
            // Eval the pattern, eval the variable, join.
            // Pattern may be non-linear in the variable - do a pure execution.
            Table t1 = TableFactory.create(gVar, gn);
            QueryIterator qIter = executeBGP(pattern, QueryIterRoot.create(cxt2), cxt2);
            Table t2 = TableFactory.create(qIter);
            Table t3 = evaluator.join(t1, t2);
            concat.add(t3.iterator(cxt2));
        }
        return TableFactory.create(concat);
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Table(org.apache.jena.sparql.algebra.Table) Var(org.apache.jena.sparql.core.Var) ARQInternalErrorException(org.apache.jena.sparql.ARQInternalErrorException) Node(org.apache.jena.graph.Node) BasicPattern(org.apache.jena.sparql.core.BasicPattern) TableEmpty(org.apache.jena.sparql.algebra.table.TableEmpty) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) QueryIterConcat(org.apache.jena.sparql.engine.iterator.QueryIterConcat) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) OpGraph(org.apache.jena.sparql.algebra.op.OpGraph) Graph(org.apache.jena.graph.Graph) QueryIterator(org.apache.jena.sparql.engine.QueryIterator)

Example 37 with ExecutionContext

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

the class RefEval method evalGraph.

static Table evalGraph(OpGraph opGraph, Evaluator evaluator) {
    ExecutionContext execCxt = evaluator.getExecContext();
    if (!Var.isVar(opGraph.getNode())) {
        DatasetGraph dsg = execCxt.getDataset();
        Node graphNode = opGraph.getNode();
        if (!dsg.containsGraph(graphNode))
            return new TableEmpty();
        Graph graph = execCxt.getDataset().getGraph(opGraph.getNode());
        if (// But contains was true?!!
        graph == null)
            throw new InternalErrorException("Graph was present, now it's not");
        ExecutionContext execCxt2 = new ExecutionContext(execCxt, graph);
        Evaluator e2 = EvaluatorFactory.create(execCxt2);
        return eval(e2, opGraph.getSubOp());
    }
    // Graph node is a variable.
    Var gVar = Var.alloc(opGraph.getNode());
    Table current = null;
    for (Iterator<Node> iter = execCxt.getDataset().listGraphNodes(); iter.hasNext(); ) {
        Node gn = iter.next();
        Graph graph = execCxt.getDataset().getGraph(gn);
        ExecutionContext execCxt2 = new ExecutionContext(execCxt, graph);
        Evaluator e2 = EvaluatorFactory.create(execCxt2);
        Table tableVarURI = TableFactory.create(gVar, gn);
        // Evaluate the pattern, join with this graph node possibility.
        // XXX If Var.ANON then no-opt.
        Table patternTable = eval(e2, opGraph.getSubOp());
        Table stepResult = evaluator.join(patternTable, tableVarURI);
        if (current == null)
            current = stepResult;
        else
            current = evaluator.union(current, stepResult);
    }
    if (current == null)
        // Nothing to loop over
        return new TableEmpty();
    return current;
}
Also used : ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) OpGraph(org.apache.jena.sparql.algebra.op.OpGraph) Graph(org.apache.jena.graph.Graph) Table(org.apache.jena.sparql.algebra.Table) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) TableEmpty(org.apache.jena.sparql.algebra.table.TableEmpty) ARQInternalErrorException(org.apache.jena.sparql.ARQInternalErrorException) InternalErrorException(org.apache.jena.atlas.lib.InternalErrorException) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Example 38 with ExecutionContext

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

the class ExprFunctionOp method eval.

// ---- Evaluation
@Override
public final NodeValue eval(Binding binding, FunctionEnv env) {
    // Substitute?
    // Apply optimize transforms after substitution?
    // if ( opRun == null )
    // {
    // opRun = op ;
    // if ( env.getContext().isTrueOrUndef(ARQ.propertyFunctions) )
    // opRun = Optimize.apply("Property Functions", new TransformPropertyFunction(env.getContext()), opRun) ;
    // }
    ExecutionContext execCxt = new ExecutionContext(env.getContext(), env.getActiveGraph(), env.getDataset(), QC.getFactory(env.getContext()));
    QueryIterator qIter1 = QueryIterSingleton.create(binding, execCxt);
    QueryIterator qIter = QC.execute(op, qIter1, execCxt);
    // Wrap with something to check for closed iterators.
    qIter = QueryIteratorCheck.check(qIter, execCxt);
    // Call the per-operation functionality.
    try {
        return eval(binding, qIter, env);
    } finally {
        qIter.close();
    }
}
Also used : ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) QueryIterator(org.apache.jena.sparql.engine.QueryIterator)

Example 39 with ExecutionContext

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

the class ProcEval method build.

// ----
public static Procedure build(Node procId, PropFuncArg subjArg, PropFuncArg objArg, ExecutionContext execCxt) {
    Context context = execCxt.getContext();
    PropertyFunctionRegistry reg = PropertyFunctionRegistry.chooseRegistry(context);
    PropertyFunctionFactory f = reg.get(procId.getURI());
    PropertyFunction pf = f.create(procId.getURI());
    pf.build(subjArg, procId, objArg, execCxt);
    // Make wrapper
    return new ProcedurePF(pf, subjArg, procId, objArg);
}
Also used : Context(org.apache.jena.sparql.util.Context) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext)

Example 40 with ExecutionContext

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

the class ProcEval method build.

// ----
public static Procedure build(Node procId, ExprList args, ExecutionContext execCxt) {
    Context context = execCxt.getContext();
    ProcedureRegistry reg = chooseProcedureRegistry(context);
    ProcedureFactory f = reg.get(procId.getURI());
    Procedure proc = f.create(procId.getURI());
    // Allow args to build as well.
    args.prepareExprs(context);
    proc.build(procId, args, execCxt);
    return proc;
}
Also used : Context(org.apache.jena.sparql.util.Context) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) OpProcedure(org.apache.jena.sparql.algebra.op.OpProcedure)

Aggregations

ExecutionContext (org.apache.jena.sparql.engine.ExecutionContext)40 Context (org.apache.jena.sparql.util.Context)19 Node (org.apache.jena.graph.Node)14 SerializationContext (org.apache.jena.sparql.serializer.SerializationContext)12 Test (org.junit.Test)12 Binding (org.apache.jena.sparql.engine.binding.Binding)11 Triple (org.apache.jena.graph.Triple)9 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)9 Iterator (java.util.Iterator)8 Iter (org.apache.jena.atlas.iterator.Iter)8 Var (org.apache.jena.sparql.core.Var)8 Predicate (java.util.function.Predicate)7 Graph (org.apache.jena.graph.Graph)7 InternalErrorException (org.apache.jena.atlas.lib.InternalErrorException)6 Function (java.util.function.Function)5 ARQInternalErrorException (org.apache.jena.sparql.ARQInternalErrorException)5 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)5 Quad (org.apache.jena.sparql.core.Quad)5 Substitute (org.apache.jena.sparql.core.Substitute)5 BindingFactory (org.apache.jena.sparql.engine.binding.BindingFactory)5