Search in sources :

Example 96 with Binding

use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.

the class E_BNode method evalSpecial.

// Not really a special form but we need access to 
// the binding to use a key.
@Override
public NodeValue evalSpecial(Binding binding, FunctionEnv env) {
    Expr expr = null;
    if (args.size() == 1)
        expr = getArg(1);
    if (expr == null)
        return NodeValue.makeNode(NodeFactory.createBlankNode());
    NodeValue x = expr.eval(binding, env);
    if (!x.isString())
        throw new ExprEvalException("Not a string: " + x);
    Integer key = System.identityHashCode(binding);
    // IdentityHashMap
    // Normally bindings have structural equality (e.g. DISTINCT)
    // we want identify as OpAssign/OpExtend mutates a binding to add new pairs.
    @SuppressWarnings("unchecked") IdentityHashMap<Binding, LabelToNodeMap> mapping = (IdentityHashMap<Binding, LabelToNodeMap>) env.getContext().get(keyMap);
    if (mapping == null) {
        mapping = new IdentityHashMap<>();
        env.getContext().set(keyMap, mapping);
    }
    LabelToNodeMap mapper = mapping.get(binding);
    if (mapper == null) {
        mapper = LabelToNodeMap.createBNodeMap();
        mapping.put(binding, mapper);
    }
    Node bnode = mapper.asNode(x.getString());
    return NodeValue.makeNode(bnode);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) IdentityHashMap(java.util.IdentityHashMap) LabelToNodeMap(org.apache.jena.sparql.util.LabelToNodeMap) Node(org.apache.jena.graph.Node)

Example 97 with Binding

use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.

the class Eval method evalDS.

static Table evalDS(OpDatasetNames opDSN, Evaluator evaluator) {
    Node graphNode = opDSN.getGraphNode();
    if (graphNode.isURI()) {
        if (evaluator.getExecContext().getDataset().containsGraph(graphNode)) {
            return new TableUnit();
        } else // WRONG
        {
            return new TableEmpty();
        }
    }
    if (!Var.isVar(graphNode))
        throw new ARQInternalErrorException("OpDatasetNames: Not a URI or variable: " + graphNode);
    DatasetGraph dsg = evaluator.getExecContext().getDataset();
    Iterator<Node> iter = dsg.listGraphNodes();
    List<Binding> list = new ArrayList<>((int) dsg.size());
    for (; iter.hasNext(); ) {
        Node gn = iter.next();
        Binding b = BindingFactory.binding(Var.alloc(graphNode), gn);
        list.add(b);
    }
    QueryIterator qIter = new QueryIterPlainWrapper(list.iterator(), evaluator.getExecContext());
    return TableFactory.create(qIter);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) QueryIterPlainWrapper(org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) Node(org.apache.jena.graph.Node) ARQInternalErrorException(org.apache.jena.sparql.ARQInternalErrorException) ArrayList(java.util.ArrayList) TableEmpty(org.apache.jena.sparql.algebra.table.TableEmpty) TableUnit(org.apache.jena.sparql.algebra.table.TableUnit) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Example 98 with Binding

use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.

the class Eval 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 99 with Binding

use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.

the class ResultSetStream method nextSolution.

/** Moves onto the next result possibility.
     *  The returned object is actual the binding for this
     *  result.
     */
@Override
public QuerySolution nextSolution() {
    if (queryExecutionIter == null)
        //             ( queryExecution != null && ! queryExecution.isActive() ) )
        throw new NoSuchElementException(this.getClass() + ".next");
    Binding binding = nextBinding();
    currentQuerySolution = new ResultBinding(model, binding);
    return currentQuerySolution;
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) ResultBinding(org.apache.jena.sparql.core.ResultBinding) NoSuchElementException(java.util.NoSuchElementException) ResultBinding(org.apache.jena.sparql.core.ResultBinding)

Example 100 with Binding

use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.

the class QueryIter2LoopOnLeft method hasNextBinding.

@Override
protected final boolean hasNextBinding() {
    if (slot != null)
        return true;
    while (getLeft().hasNext()) {
        Binding bindingLeft = getLeft().nextBinding();
        slot = getNextSlot(bindingLeft);
        if (slot != null) {
            slot = bindingLeft;
            return true;
        }
    }
    getLeft().close();
    return false;
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding)

Aggregations

Binding (org.apache.jena.sparql.engine.binding.Binding)132 Node (org.apache.jena.graph.Node)39 ArrayList (java.util.ArrayList)36 Var (org.apache.jena.sparql.core.Var)33 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)27 Test (org.junit.Test)21 QueryIterPlainWrapper (org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper)16 BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)10 BindingComparator (org.apache.jena.sparql.engine.binding.BindingComparator)8 BuilderBinding (org.apache.jena.sparql.sse.builders.BuilderBinding)8 NoSuchElementException (java.util.NoSuchElementException)7 Triple (org.apache.jena.graph.Triple)7 ResultSetStream (org.apache.jena.sparql.engine.ResultSetStream)7 ARQInternalErrorException (org.apache.jena.sparql.ARQInternalErrorException)5 Op (org.apache.jena.sparql.algebra.Op)5 BasicPattern (org.apache.jena.sparql.core.BasicPattern)5 ResultSet (org.apache.jena.query.ResultSet)4 SortCondition (org.apache.jena.query.SortCondition)4 TableN (org.apache.jena.sparql.algebra.table.TableN)4 File (java.io.File)3