Search in sources :

Example 11 with BindingBuilder

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

the class BuilderBinding method buildBinding.

private static Binding buildBinding(ItemList list) {
    // (row or (binding
    if (list.size() == 0)
        BuilderLib.broken(list, "Empty list");
    Item head = list.get(0);
    if (!head.isSymbolIgnoreCase(Tags.tagRow) && !head.isSymbolIgnoreCase(Tags.tagBinding))
        BuilderLib.broken(list, "Does not start (" + Tags.tagRow + " ...) or (" + Tags.tagBinding + " ...)", head);
    BindingBuilder builder = Binding.builder();
    for (int i = 1; i < list.size(); i++) {
        Item item = list.get(i);
        BuilderLib.checkList(item, "Attempt to build a binding pair from non-list: " + item);
        ItemList pair = item.getList();
        BuilderLib.checkLength(2, pair, "Need a pair for a binding");
        Var v = BuilderNode.buildVar(pair.get(0));
        Item cdr = pair.get(1);
        // undef
        if (cdr.isSymbolIgnoreCase(Tags.tagUndef) || cdr.isSymbolIgnoreCase(Tags.tagNull))
            continue;
        Node node = BuilderNode.buildNode(item.getList().get(1));
        if (node == null)
            BuilderLib.broken(item.getList().get(1), "Null node from " + item.getList().get(1));
        if (node.isVariable())
            BuilderLib.broken(item.getList().get(1), "No variables as table values: " + FmtUtils.stringForNode(node));
        if (!node.isConcrete())
            BuilderLib.broken(item.getList().get(1), "Only concrete nodes as table values: " + FmtUtils.stringForNode(node));
        builder.add(v, node);
    }
    return builder.build();
}
Also used : Item(org.apache.jena.sparql.sse.Item) BindingBuilder(org.apache.jena.sparql.engine.binding.BindingBuilder) Var(org.apache.jena.sparql.core.Var) ItemList(org.apache.jena.sparql.sse.ItemList) Node(org.apache.jena.graph.Node)

Example 12 with BindingBuilder

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

the class ValuesHandler method asElement.

public ElementData asElement() {
    ElementData ed = new ElementData();
    // remove all the vars that have been set
    List<Var> vars = new ArrayList<Var>(valuesTable.keySet());
    vars.removeAll(valueMap.keySet());
    if (vars.isEmpty()) {
        return ed;
    }
    vars.forEach(var -> ed.add(var));
    // List<Binding> bindings = new ArrayList<Binding>();
    int count = valuesTable.get(vars.get(0)).size();
    BindingBuilder builder = Binding.builder();
    for (int i = 0; i < count; i++) {
        builder.reset();
        for (Var var : vars) {
            List<Node> lst = valuesTable.get(var);
            // must be square
            if (lst.size() != count) {
                throw new QueryBuildException(String.format("The number of data items for %s (%s) is not the same as for %s (%s)", var, lst.size(), vars.get(0), count));
            }
            Node n = lst.get(i);
            if (n != null) {
                if (valueMap.containsKey(n)) {
                    n = valueMap.get(n);
                }
                builder.add(var, n);
            }
        }
        if (builder.isEmpty())
            continue;
        ed.add(builder.build());
    }
    return ed;
}
Also used : BindingBuilder(org.apache.jena.sparql.engine.binding.BindingBuilder) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) QueryBuildException(org.apache.jena.query.QueryBuildException) ArrayList(java.util.ArrayList) ElementData(org.apache.jena.sparql.syntax.ElementData)

Example 13 with BindingBuilder

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

the class SolverLibTDB method convToBinding.

static Binding convToBinding(BindingNodeId bindingNodeIds, NodeTable nodeTable) {
    if (true)
        return new BindingTDB(bindingNodeIds, nodeTable);
    else {
        // Makes nodes immediately. Causing unnecessary NodeTable accesses
        // (e.g. project)
        BindingBuilder builder = Binding.builder();
        for (Var v : bindingNodeIds) {
            NodeId id = bindingNodeIds.get(v);
            Node n = nodeTable.getNodeForNodeId(id);
            builder.add(v, n);
        }
        return builder.build();
    }
}
Also used : BindingBuilder(org.apache.jena.sparql.engine.binding.BindingBuilder) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) NodeId(org.apache.jena.tdb.store.NodeId)

Example 14 with BindingBuilder

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

the class TextQueryPF method resultsToQueryIterator.

private QueryIterator resultsToQueryIterator(Binding binding, Node subj, Node score, Node literal, Node graph, Node prop, Collection<TextHit> results, ExecutionContext execCxt) {
    log.trace("resultsToQueryIterator CALLED with results: {}", results);
    Var sVar = Var.isVar(subj) ? Var.alloc(subj) : null;
    Var scoreVar = (score == null) ? null : Var.alloc(score);
    Var literalVar = (literal == null) ? null : Var.alloc(literal);
    Var graphVar = (graph == null) ? null : Var.alloc(graph);
    Var propVar = (prop == null) ? null : Var.alloc(prop);
    Function<TextHit, Binding> converter = (TextHit hit) -> {
        if (score == null && literal == null)
            return sVar != null ? BindingFactory.binding(binding, sVar, hit.getNode()) : BindingFactory.binding(binding);
        BindingBuilder bmap = Binding.builder(binding);
        addIf(bmap, sVar, hit.getNode());
        addIf(bmap, scoreVar, NodeFactoryExtra.floatToNode(hit.getScore()));
        addIf(bmap, literalVar, hit.getLiteral());
        addIf(bmap, graphVar, hit.getGraph());
        addIf(bmap, propVar, hit.getProp());
        log.trace("resultsToQueryIterator RETURNING bmap: {}", bmap);
        return bmap.build();
    };
    Iterator<Binding> bIter = Iter.map(results.iterator(), converter);
    QueryIterator qIter = QueryIterPlainWrapper.create(bIter, execCxt);
    return qIter;
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) BindingBuilder(org.apache.jena.sparql.engine.binding.BindingBuilder) QueryIterator(org.apache.jena.sparql.engine.QueryIterator)

Example 15 with BindingBuilder

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

the class LinearIndex method toBinding.

static Binding toBinding(HashIndexTable.Key key, Map<Var, Integer> mappings) {
    Node[] values = key.getNodes();
    BindingBuilder b = Binding.builder();
    for (Map.Entry<Var, Integer> mapping : mappings.entrySet()) {
        Node value = values[mapping.getValue()];
        if (value != null)
            b.add(mapping.getKey(), value);
    }
    return b.build();
}
Also used : BindingBuilder(org.apache.jena.sparql.engine.binding.BindingBuilder) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) Map(java.util.Map)

Aggregations

BindingBuilder (org.apache.jena.sparql.engine.binding.BindingBuilder)30 Node (org.apache.jena.graph.Node)19 Var (org.apache.jena.sparql.core.Var)16 Binding (org.apache.jena.sparql.engine.binding.Binding)14 ArrayList (java.util.ArrayList)6 Iterator (java.util.Iterator)4 Iter (org.apache.jena.atlas.iterator.Iter)4 Triple (org.apache.jena.graph.Triple)4 ExecutionContext (org.apache.jena.sparql.engine.ExecutionContext)4 Predicate (java.util.function.Predicate)3 Graph (org.apache.jena.graph.Graph)3 List (java.util.List)2 Objects (java.util.Objects)2 Function (java.util.function.Function)2 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)2 Quad (org.apache.jena.sparql.core.Quad)2 Substitute (org.apache.jena.sparql.core.Substitute)2 SolverLib.nodeTopLevel (org.apache.jena.sparql.engine.main.solver.SolverLib.nodeTopLevel)2 SolverLib.sameTermAs (org.apache.jena.sparql.engine.main.solver.SolverLib.sameTermAs)2 SolverLib.tripleHasEmbTripleWithVars (org.apache.jena.sparql.engine.main.solver.SolverLib.tripleHasEmbTripleWithVars)2