use of org.apache.jena.sparql.core.Var in project jena by apache.
the class BuilderResultSet method build.
public static ResultSet build(Item item) {
BuilderLib.checkTagged(item, Tags.tagResultSet, "Not a (resultset ...)");
ItemList list = item.getList();
List<Var> vars = BuilderNode.buildVarList(list.get(1));
// skip tag, skip vars.
int start = 2;
List<Binding> bindings = new ArrayList<>();
for (int i = start; i < list.size(); i++) {
Item itemRow = list.get(i);
Binding b = BuilderBinding.build(itemRow);
bindings.add(b);
}
QueryIterator qIter = new QueryIterPlainWrapper(bindings.listIterator());
return new ResultSetStream(Var.varNames(vars), null, qIter);
}
use of org.apache.jena.sparql.core.Var in project jena by apache.
the class BuilderTable method build.
public static Table build(Item item) {
BuilderLib.checkTagged(item, Tags.tagTable, "Not a (table ...)");
ItemList list = item.getList();
int start = 1;
if (list.size() == 1)
// Null table;
return TableFactory.createEmpty();
// Maybe vars.
List<Var> vars = null;
if (list.size() > 1) {
Item item0 = list.get(1);
if (item0.isTagged(Tags.tagVars)) {
vars = BuilderNode.buildVarList(item0);
list = list.cdr();
}
}
if (list.size() == 2 && list.get(1).isSymbol()) {
// Short hand for well known tables
String symbol = list.get(1).getSymbol();
if (symbol.equals("unit"))
return TableFactory.createUnit();
if (symbol.equals("empty"))
return TableFactory.createEmpty();
BuilderLib.broken(list, "Don't recognized table symbol");
}
Table table = TableFactory.create(vars);
int count = 0;
Binding lastBinding = null;
for (int i = start; i < list.size(); i++) {
Item itemRow = list.get(i);
Binding b = BuilderBinding.build(itemRow);
table.addBinding(b);
lastBinding = b;
count++;
}
if (table.size() == 1) {
// One row, no bindings.
if (lastBinding.isEmpty())
return TableFactory.createUnit();
}
return table;
}
use of org.apache.jena.sparql.core.Var in project jena by apache.
the class ElementData method add.
public void add(Binding binding) {
Iterator<Var> iter = binding.vars();
while (iter.hasNext()) {
Var v = iter.next();
if (!vars.contains(v))
throw new ARQException("Variable " + v + " not already declared for ElementData");
}
rows.add(binding);
}
use of org.apache.jena.sparql.core.Var in project jena by apache.
the class BuilderExpr method buildNamedExpr.
private static void buildNamedExpr(Item item, VarExprList varExprList) {
if (item.isNode()) {
Var v = BuilderNode.buildVar(item);
varExprList.add(v);
return;
}
if (!item.isList() || item.getList().size() != 2)
BuilderLib.broken(item, "Not a var or var/expression pair");
ItemList list = item.getList();
if (list.size() == 1) {
Var v = BuilderNode.buildVar(list.car());
varExprList.add(v);
return;
}
if (list.size() != 2)
BuilderLib.broken(list, "Not a var or var/expression pair");
Var var = BuilderNode.buildVar(list.get(0));
Expr expr = BuilderExpr.buildExpr(list.get(1));
varExprList.add(var, expr);
}
use of org.apache.jena.sparql.core.Var 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);
BindingMap binding = BindingFactory.create();
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;
BuilderLib.checkNode(cdr);
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), "Ony concrete nodes as table values: " + FmtUtils.stringForNode(node));
binding.add(v, node);
}
return binding;
}
Aggregations