Search in sources :

Example 11 with ExecutionContext

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

the class SolverRX3 method rdfStarTripleSub.

private static Iterator<Binding> rdfStarTripleSub(Binding input, Triple xPattern, ExecutionContext execCxt) {
    Triple tPattern = Substitute.substitute(xPattern, input);
    Node s = nodeTopLevel(tPattern.getSubject());
    Node p = nodeTopLevel(tPattern.getPredicate());
    Node o = nodeTopLevel(tPattern.getObject());
    Graph graph = execCxt.getActiveGraph();
    ExtendedIterator<Triple> graphIter = graph.find(s, p, o);
    ExtendedIterator<Binding> matched = graphIter.mapWith(tData -> matchTriple(input, tData, tPattern)).filterDrop(Objects::isNull);
    return matched;
}
Also used : Triple(org.apache.jena.graph.Triple) Binding(org.apache.jena.sparql.engine.binding.Binding) Binding(org.apache.jena.sparql.engine.binding.Binding) Iterator(java.util.Iterator) SolverLib.nodeTopLevel(org.apache.jena.sparql.engine.main.solver.SolverLib.nodeTopLevel) Triple(org.apache.jena.graph.Triple) SolverLib.tripleHasEmbTripleWithVars(org.apache.jena.sparql.engine.main.solver.SolverLib.tripleHasEmbTripleWithVars) BindingBuilder(org.apache.jena.sparql.engine.binding.BindingBuilder) Graph(org.apache.jena.graph.Graph) Objects(java.util.Objects) SolverLib.sameTermAs(org.apache.jena.sparql.engine.main.solver.SolverLib.sameTermAs) Substitute(org.apache.jena.sparql.core.Substitute) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) ExtendedIterator(org.apache.jena.util.iterator.ExtendedIterator) Iter(org.apache.jena.atlas.iterator.Iter) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) Graph(org.apache.jena.graph.Graph) Node(org.apache.jena.graph.Node) Objects(java.util.Objects)

Example 12 with ExecutionContext

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

the class StageMatchData method accessQuad.

static Iterator<Binding> accessQuad(Binding binding, Node graphName, Triple pattern, Predicate<Quad> filter, boolean anyGraph, ExecutionContext execCxt) {
    // Assumes if anyGraph, then graphName ==ANY.
    // graphName == null for triples.
    Node g = graphName;
    Node s = pattern.getSubject();
    Node p = pattern.getPredicate();
    Node o = pattern.getObject();
    Node[] matchConst = new Node[4];
    Var[] vars = new Var[4];
    boolean b = prepareQuad(binding, graphName, pattern, matchConst, vars);
    if (!b)
        return Iter.nullIterator();
    Node gm = matchConst[QG];
    Node sm = matchConst[QS];
    Node pm = matchConst[QP];
    Node om = matchConst[QO];
    DatasetGraph dsg = execCxt.getDataset();
    // Union -> findNG - do not include default graph.
    Iterator<Quad> iterMatches = anyGraph ? dsg.findNG(gm, sm, pm, om) : dsg.find(gm, sm, pm, om);
    if (false) {
        // Debug
        List<Quad> x = Iter.toList(iterMatches);
        System.out.println(x);
        iterMatches = x.iterator();
    }
    // ** Allow a triple or quad filter here.
    if (filter != null)
        iterMatches = Iter.filter(iterMatches, filter);
    // Assumes that tuples are not shared.
    if (anyGraph) {
        iterMatches = Iter.map(iterMatches, quadsToUnion);
        // Guaranteed
        iterMatches = Iter.distinct(iterMatches);
    // This depends on the way indexes are chosen and
    // the indexing pattern. It assumes that the index
    // chosen ends in G so same triples are adjacent
    // in a union query.
    // 
    // If any slot is defined, then the index will be X??G.
    // If no slot is defined, then the index will be ???G.
    // Must be guaranteed.
    // 
    // iterMatches = Iter.distinctAdjacent(iterMatches);
    }
    BindingBuilder bindingBuilder = BindingFactory.builder(binding);
    Function<Quad, Binding> binder = quad -> quadToBinding(bindingBuilder, quad, matchConst, vars);
    return Iter.iter(iterMatches).map(binder).removeNulls();
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Binding(org.apache.jena.sparql.engine.binding.Binding) Iterator(java.util.Iterator) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Predicate(java.util.function.Predicate) Triple(org.apache.jena.graph.Triple) BindingBuilder(org.apache.jena.sparql.engine.binding.BindingBuilder) Function(java.util.function.Function) List(java.util.List) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) Quad(org.apache.jena.sparql.core.Quad) Iter(org.apache.jena.atlas.iterator.Iter) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) BindingFactory(org.apache.jena.sparql.engine.binding.BindingFactory) Quad(org.apache.jena.sparql.core.Quad) BindingBuilder(org.apache.jena.sparql.engine.binding.BindingBuilder) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Example 13 with ExecutionContext

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

the class QueryIterService method nextStage.

@Override
protected QueryIterator nextStage(Binding outerBinding) {
    boolean silent = opService.getSilent();
    ExecutionContext execCxt = getExecContext();
    Context cxt = execCxt.getContext();
    ServiceExecutorRegistry registry = ServiceExecutorRegistry.get(cxt);
    ServiceExecution svcExec = null;
    OpService substitutedOp = (OpService) QC.substitute(opService, outerBinding);
    try {
        // ---- Find handler
        if (registry != null) {
            for (ServiceExecutorFactory factory : registry.getFactories()) {
                // Internal consistency check
                if (factory == null) {
                    Log.warn(this, "SERVICE <" + opService.getService().toString() + ">: Null item in custom ServiceExecutionRegistry");
                    continue;
                }
                svcExec = factory.createExecutor(substitutedOp, opService, outerBinding, execCxt);
                if (svcExec != null)
                    break;
            }
        }
        // ---- Execute
        if (svcExec == null)
            throw new QueryExecException("No SERVICE handler");
        QueryIterator qIter = svcExec.exec();
        qIter = QueryIter.makeTracked(qIter, getExecContext());
        // There should be no variables in common because of the OpSubstitute.substitute
        return new QueryIterCommonParent(qIter, outerBinding, getExecContext());
    } catch (RuntimeException ex) {
        if (silent) {
            Log.warn(this, "SERVICE " + NodeFmtLib.strTTL(substitutedOp.getService()) + " : " + ex.getMessage());
            // Return the input
            return QueryIterSingleton.create(outerBinding, getExecContext());
        }
        throw ex;
    }
}
Also used : Context(org.apache.jena.sparql.util.Context) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) ServiceExecutorFactory(org.apache.jena.sparql.service.ServiceExecutorFactory) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) ServiceExecution(org.apache.jena.sparql.service.ServiceExecution) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) QueryIterCommonParent(org.apache.jena.sparql.engine.iterator.QueryIterCommonParent) OpService(org.apache.jena.sparql.algebra.op.OpService) ServiceExecutorRegistry(org.apache.jena.sparql.service.ServiceExecutorRegistry) QueryExecException(org.apache.jena.query.QueryExecException)

Example 14 with ExecutionContext

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

the class TestPath method eval.

private static List<Binding> eval(Graph graph, Node start, String pathStr, Node finish) {
    Path path = SSE.parsePath(pathStr, pmap);
    QueryIterator qIter = PathLib.execTriplePath(BindingFactory.root(), start, path, finish, new ExecutionContext(ARQ.getContext(), graph, null, null));
    return Iter.toList(qIter);
}
Also used : WriterPath(org.apache.jena.sparql.sse.writers.WriterPath) BuilderPath(org.apache.jena.sparql.sse.builders.BuilderPath) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) QueryIterator(org.apache.jena.sparql.engine.QueryIterator)

Example 15 with ExecutionContext

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

the class SolverRX method matchQuadPattern.

// Entry point from SolverLib.
/*package*/
static Iterator<BindingNodeId> matchQuadPattern(Iterator<BindingNodeId> chain, Node graphNode, Triple tPattern, NodeTupleTable nodeTupleTable, Tuple<Node> patternTuple, boolean anyGraph, Predicate<Tuple<NodeId>> filter, ExecutionContext execCxt) {
    if (DATAPATH) {
        if (!tripleHasEmbTripleWithVars(tPattern))
            // No RDF-star <<>> with variables.
            return StageMatchTuple.access(nodeTupleTable, chain, patternTuple, filter, anyGraph, execCxt);
    }
    // RDF-star <<>> with variables.
    // This path should work regardless.
    boolean isTriple = (patternTuple.len() == 3);
    NodeTable nodeTable = nodeTupleTable.getNodeTable();
    Function<BindingNodeId, Iterator<BindingNodeId>> step = bnid -> find(bnid, nodeTupleTable, graphNode, tPattern, anyGraph, filter, execCxt);
    return Iter.flatMap(chain, step);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Iterator(java.util.Iterator) Predicate(java.util.function.Predicate) NodeId(org.apache.jena.tdb2.store.NodeId) SolverLib.nodeTopLevel(org.apache.jena.sparql.engine.main.solver.SolverLib.nodeTopLevel) Triple(org.apache.jena.graph.Triple) SolverLib.tripleHasEmbTripleWithVars(org.apache.jena.sparql.engine.main.solver.SolverLib.tripleHasEmbTripleWithVars) NodeTupleTable(org.apache.jena.tdb2.store.nodetupletable.NodeTupleTable) Function(java.util.function.Function) NodeTable(org.apache.jena.tdb2.store.nodetable.NodeTable) SolverRX4(org.apache.jena.sparql.engine.main.solver.SolverRX4) Substitute(org.apache.jena.sparql.core.Substitute) Node(org.apache.jena.graph.Node) Quad(org.apache.jena.sparql.core.Quad) TupleLib(org.apache.jena.tdb2.lib.TupleLib) TupleFactory(org.apache.jena.atlas.lib.tuple.TupleFactory) SolverLibTDB.convFromBinding(org.apache.jena.tdb2.solver.SolverLibTDB.convFromBinding) Iter(org.apache.jena.atlas.iterator.Iter) InternalErrorException(org.apache.jena.atlas.lib.InternalErrorException) Tuple(org.apache.jena.atlas.lib.tuple.Tuple) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) BindingFactory(org.apache.jena.sparql.engine.binding.BindingFactory) Iterator(java.util.Iterator) NodeTable(org.apache.jena.tdb2.store.nodetable.NodeTable)

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