Search in sources :

Example 1 with SolverLibTDB.convFromBinding

use of org.apache.jena.tdb2.solver.SolverLibTDB.convFromBinding in project jena by apache.

the class PatternMatchTDB2 method execute.

// The worker.  Callers choose the NodeTupleTable.
// graphNode may be Node.ANY, meaning we should make triples unique.
// graphNode may be null, meaning default graph
private static QueryIterator execute(NodeTupleTable nodeTupleTable, Node graphNode, BasicPattern pattern, QueryIterator input, Predicate<Tuple<NodeId>> filter, ExecutionContext execCxt) {
    if (Quad.isUnionGraph(graphNode))
        graphNode = Node.ANY;
    if (Quad.isDefaultGraph(graphNode))
        graphNode = null;
    List<Triple> triples = pattern.getList();
    boolean anyGraph = (graphNode == null ? false : (Node.ANY.equals(graphNode)));
    int tupleLen = nodeTupleTable.getTupleTable().getTupleLen();
    if (graphNode == null) {
        if (3 != tupleLen)
            throw new TDBException("SolverLib: Null graph node but tuples are of length " + tupleLen);
    } else {
        if (4 != tupleLen)
            throw new TDBException("SolverLib: Graph node specified but tuples are of length " + tupleLen);
    }
    // Convert from a QueryIterator (Bindings of Var/Node) to BindingNodeId
    NodeTable nodeTable = nodeTupleTable.getNodeTable();
    Iterator<BindingNodeId> chain = Iter.map(input, SolverLibTDB.convFromBinding(nodeTable));
    List<Abortable> killList = new ArrayList<>();
    for (Triple triple : triples) {
        Tuple<Node> patternTuple = null;
        if (graphNode == null)
            // 3-tuples
            patternTuple = TupleFactory.create3(triple.getSubject(), triple.getPredicate(), triple.getObject());
        else
            // 4-tuples.
            patternTuple = TupleFactory.create4(graphNode, triple.getSubject(), triple.getPredicate(), triple.getObject());
        // Plain RDF, no RDF-star
        // chain = solve(nodeTupleTable, tuple, anyGraph, chain, filter, execCxt)
        // ;
        // RDF-star SA
        chain = matchQuadPattern(chain, graphNode, triple, nodeTupleTable, patternTuple, anyGraph, filter, execCxt);
        chain = makeAbortable(chain, killList);
    }
    Iterator<Binding> iterBinding = SolverLibTDB.convertToNodes(chain, nodeTable);
    // "killList" will be aborted on timeout.
    return new QueryIterAbortable(iterBinding, killList, input, execCxt);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) QueryIterAbortable(org.apache.jena.sparql.engine.iterator.QueryIterAbortable) TDBException(org.apache.jena.tdb2.TDBException) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) Triple(org.apache.jena.graph.Triple) QueryIterAbortable(org.apache.jena.sparql.engine.iterator.QueryIterAbortable) SolverLib.makeAbortable(org.apache.jena.sparql.engine.main.solver.SolverLib.makeAbortable) Abortable(org.apache.jena.sparql.engine.iterator.Abortable) NodeTable(org.apache.jena.tdb2.store.nodetable.NodeTable)

Aggregations

ArrayList (java.util.ArrayList)1 Node (org.apache.jena.graph.Node)1 Triple (org.apache.jena.graph.Triple)1 Binding (org.apache.jena.sparql.engine.binding.Binding)1 Abortable (org.apache.jena.sparql.engine.iterator.Abortable)1 QueryIterAbortable (org.apache.jena.sparql.engine.iterator.QueryIterAbortable)1 SolverLib.makeAbortable (org.apache.jena.sparql.engine.main.solver.SolverLib.makeAbortable)1 TDBException (org.apache.jena.tdb2.TDBException)1 NodeTable (org.apache.jena.tdb2.store.nodetable.NodeTable)1