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();
}
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;
}
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();
}
}
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;
}
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();
}
Aggregations