Search in sources :

Example 81 with Var

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);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Item(org.apache.jena.sparql.sse.Item) QueryIterPlainWrapper(org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) Var(org.apache.jena.sparql.core.Var) ItemList(org.apache.jena.sparql.sse.ItemList) ArrayList(java.util.ArrayList) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream)

Example 82 with Var

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;
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Item(org.apache.jena.sparql.sse.Item) Table(org.apache.jena.sparql.algebra.Table) Var(org.apache.jena.sparql.core.Var) ItemList(org.apache.jena.sparql.sse.ItemList)

Example 83 with Var

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);
}
Also used : ARQException(org.apache.jena.sparql.ARQException) Var(org.apache.jena.sparql.core.Var)

Example 84 with Var

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);
}
Also used : Var(org.apache.jena.sparql.core.Var) ItemList(org.apache.jena.sparql.sse.ItemList)

Example 85 with Var

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;
}
Also used : Item(org.apache.jena.sparql.sse.Item) Var(org.apache.jena.sparql.core.Var) ItemList(org.apache.jena.sparql.sse.ItemList) Node(org.apache.jena.graph.Node) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap)

Aggregations

Var (org.apache.jena.sparql.core.Var)264 Node (org.apache.jena.graph.Node)83 ArrayList (java.util.ArrayList)53 Test (org.junit.Test)47 Binding (org.apache.jena.sparql.engine.binding.Binding)33 VarExprList (org.apache.jena.sparql.core.VarExprList)30 Op (org.apache.jena.sparql.algebra.Op)29 Expr (org.apache.jena.sparql.expr.Expr)28 Triple (org.apache.jena.graph.Triple)17 HashMap (java.util.HashMap)15 ContractTest (org.xenei.junit.contract.ContractTest)13 BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)12 Query (org.apache.jena.query.Query)11 ExprList (org.apache.jena.sparql.expr.ExprList)11 SortCondition (org.apache.jena.query.SortCondition)10 ExprVar (org.apache.jena.sparql.expr.ExprVar)10 HashSet (java.util.HashSet)9 Pair (org.apache.jena.atlas.lib.Pair)9 SqlColumn (org.apache.jena.sdb.core.sqlexpr.SqlColumn)9 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)9