use of org.apache.jena.sparql.syntax.ElementData 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.syntax.ElementData in project jena by apache.
the class ElementRewriter method visit.
@Override
public void visit(ElementData el) {
ElementData retval = new ElementData();
for (Var v : el.getVars()) {
retval.add(v);
}
for (Binding binding : el.getRows()) {
retval.add(binding);
}
push(retval);
}
use of org.apache.jena.sparql.syntax.ElementData in project jena by apache.
the class WhereHandlerTest method testSetVarsInWhereValues.
@Test
public void testSetVarsInWhereValues() {
Var v = Var.alloc("v");
Node value = NodeFactory.createLiteral(LiteralLabelFactory.createTypedLiteral(10));
Map<Var, Node> values = new HashMap<>();
values.put(v, value);
handler.addValueVar(query.getPrefixMapping(), "?x", "<one>", "?v");
handler.setVars(values);
handler.build();
ElementData edat = new ElementData();
Var x = Var.alloc("x");
edat.add(x);
Binding binding1 = BindingFactory.binding(x, NodeFactory.createURI("one"));
edat.add(binding1);
Binding binding2 = BindingFactory.binding(x, value);
edat.add(binding2);
WhereValidator visitor = new WhereValidator(edat);
query.getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
}
use of org.apache.jena.sparql.syntax.ElementData in project jena by apache.
the class WhereHandlerTest method testAddValueVar_pfx_obj.
@Test
public void testAddValueVar_pfx_obj() {
handler.addValueVar(query.getPrefixMapping(), "?v");
handler.build();
Var v = Var.alloc("v");
ElementData edat = new ElementData();
edat.add(v);
WhereValidator visitor = new WhereValidator(edat);
query.getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
}
use of org.apache.jena.sparql.syntax.ElementData in project jena by apache.
the class WhereClauseTest method testAddWhereValueVars.
@ContractTest
public void testAddWhereValueVars() {
final Var v = Var.alloc("v");
Map<Object, List<?>> map = new HashMap<Object, List<?>>();
map.put(Var.alloc("v"), Arrays.asList("<one>", "<two>"));
map.put("?x", Arrays.asList("three", "four"));
WhereClause<?> whereClause = getProducer().newInstance();
AbstractQueryBuilder<?> builder = whereClause.addWhereValueVars(map);
Query query = builder.build();
Var x = Var.alloc("x");
ElementData edat = new ElementData();
edat.add(x);
edat.add(v);
setupBindings(edat, x, v);
WhereValidator visitor = new WhereValidator(edat);
query.getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
}
Aggregations