Search in sources :

Example 6 with BindingMap

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

the class TestDistinctDataBag method randomBinding.

private Binding randomBinding(Var[] vars) {
    BindingMap binding = BindingFactory.create();
    binding.add(vars[0], NodeFactory.createBlankNode());
    binding.add(vars[1], NodeFactory.createURI(randomURI()));
    binding.add(vars[2], NodeFactory.createURI(randomURI()));
    binding.add(vars[3], NodeFactory.createLiteral(randomString(20)));
    binding.add(vars[4], NodeFactory.createBlankNode());
    binding.add(vars[5], NodeFactory.createURI(randomURI()));
    binding.add(vars[6], NodeFactory.createURI(randomURI()));
    binding.add(vars[7], NodeFactory.createLiteral(randomString(5)));
    binding.add(vars[8], NodeFactory.createLiteral("" + random.nextInt(), XSDDatatype.XSDinteger));
    binding.add(vars[9], NodeFactory.createBlankNode());
    return binding;
}
Also used : BindingMap(org.apache.jena.sparql.engine.binding.BindingMap)

Example 7 with BindingMap

use of org.apache.jena.sparql.engine.binding.BindingMap 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)

Example 8 with BindingMap

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

the class QueryIterGroup method calc.

private static Iterator<Binding> calc(final QueryIterator iter, final VarExprList groupVarExpr, final List<ExprAggregator> aggregators, final ExecutionContext execCxt) {
    return new IteratorDelayedInitialization<Binding>() {

        @Override
        protected Iterator<Binding> initializeIterator() {
            boolean noAggregators = (aggregators == null || aggregators.isEmpty());
            // Phase 1 : assign bindings to buckets by key and pump through the aggregators.
            Multimap<Binding, Pair<Var, Accumulator>> accumulators = HashMultimap.create();
            while (iter.hasNext()) {
                Binding b = iter.nextBinding();
                Binding key = genKey(groupVarExpr, b, execCxt);
                if (noAggregators) {
                    // Put in a dummy to remember the input.
                    accumulators.put(key, placeholder);
                    continue;
                }
                // Create if does not exist.
                if (!accumulators.containsKey(key)) {
                    for (ExprAggregator agg : aggregators) {
                        Accumulator x = agg.getAggregator().createAccumulator();
                        Var v = agg.getVar();
                        accumulators.put(key, Pair.create(v, x));
                    }
                }
                // Do the per-accumulator calculation.
                for (Pair<Var, Accumulator> pair : accumulators.get(key)) pair.getRight().accumulate(b, execCxt);
            }
            if (accumulators.isEmpty()) {
                if (noAggregators) {
                    // ==> No result rows.
                    return Iter.nullIterator();
                }
                BindingMap binding = BindingFactory.create();
                for (ExprAggregator agg : aggregators) {
                    Var v = agg.getVar();
                    Node value = agg.getAggregator().getValueEmpty();
                    if (value != null) {
                        binding.add(v, value);
                    }
                }
                if (binding == null)
                    // This does not happen if there are any aggregators. 
                    return Iter.nullIterator();
                // cast to get the static type inference to work.
                return Iter.singletonIter((Binding) binding);
            }
            if (noAggregators)
                // We used placeholder so there are always the key. 
                return accumulators.keySet().iterator();
            List<Binding> results = new ArrayList<>();
            for (Binding k : accumulators.keySet()) {
                Collection<Pair<Var, Accumulator>> accs = accumulators.get(k);
                BindingMap b = BindingFactory.create(k);
                for (Pair<Var, Accumulator> pair : accs) {
                    Var v = pair.getLeft();
                    NodeValue value = pair.getRight().getValue();
                    Node n = (value == null) ? null : value.asNode();
                    if (v == null || n == null) {
                    } else
                        b.add(v, n);
                }
                results.add(b);
            }
            return results.iterator();
        }
    };
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Accumulator(org.apache.jena.sparql.expr.aggregate.Accumulator) NodeValue(org.apache.jena.sparql.expr.NodeValue) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap) IteratorDelayedInitialization(org.apache.jena.atlas.iterator.IteratorDelayedInitialization) ExprAggregator(org.apache.jena.sparql.expr.ExprAggregator) Pair(org.apache.jena.atlas.lib.Pair)

Example 9 with BindingMap

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

the class JSONInputIterator method parseNextBinding.

private boolean parseNextBinding() {
    if (lookingAt(TokenType.LBRACE)) {
        nextToken();
        BindingMap b = BindingFactory.create();
        do {
            if (isPropertyName()) {
                Token t = nextToken();
                String var = t.getImage();
                checkColon();
                Node n = parseNode();
                b.add(Var.alloc(var), n);
                checkComma(TokenType.RBRACE);
            } else if (lookingAt(TokenType.RBRACE)) {
                nextToken();
                checkComma(TokenType.RBRACKET);
                break;
            } else {
                exception(peekToken(), "Unexpected Token encountered, expected a property name to indicate the value for a variable");
            }
        } while (true);
        this.binding = b;
        return true;
    } else if (lookingAt(TokenType.RBRACKET)) {
        // End of Bindings Array
        nextToken();
        if (lookingAt(TokenType.RBRACE)) {
            nextToken();
            parseToEnd();
        } else {
            exception(peekToken(), "Unexpected Token encountered, expected a } to end the results object");
        }
    } else {
        exception(peekToken(), "Unexpected Token encountered, expected a { for the start of a binding of ] to end the array of bindings");
    }
    return false;
}
Also used : Node(org.apache.jena.graph.Node) Token(org.apache.jena.riot.tokens.Token) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap)

Example 10 with BindingMap

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

the class TestResultSet method make.

private ResultSet make(String var1, Node val1, String var2, Node val2) {
    BindingMap b = BindingFactory.create();
    b.add(Var.alloc(var1), val1);
    b.add(Var.alloc(var2), val2);
    List<String> vars = new ArrayList<>();
    vars.add(var1);
    vars.add(var2);
    QueryIterator qIter = QueryIterSingleton.create(b, null);
    ResultSet rs = new ResultSetStream(vars, null, qIter);
    return rs;
}
Also used : QueryIterator(org.apache.jena.sparql.engine.QueryIterator) ArrayList(java.util.ArrayList) BuilderResultSet(org.apache.jena.sparql.sse.builders.BuilderResultSet) ResultSet(org.apache.jena.query.ResultSet) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream)

Aggregations

BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)29 Node (org.apache.jena.graph.Node)18 Var (org.apache.jena.sparql.core.Var)12 Binding (org.apache.jena.sparql.engine.binding.Binding)10 ArrayList (java.util.ArrayList)8 QueryIterPlainWrapper (org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper)5 ResultSetStream (org.apache.jena.sparql.engine.ResultSetStream)4 ResultSet (org.apache.jena.query.ResultSet)3 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)3 SQLException (java.sql.SQLException)2 SqlNode (org.apache.jena.sdb.core.sqlnode.SqlNode)2 BuilderResultSet (org.apache.jena.sparql.sse.builders.BuilderResultSet)2 ResultSet (java.sql.ResultSet)1 List (java.util.List)1 Function (java.util.function.Function)1 CSVParser (org.apache.jena.atlas.csv.CSVParser)1 IteratorDelayedInitialization (org.apache.jena.atlas.iterator.IteratorDelayedInitialization)1 JsonArray (org.apache.jena.atlas.json.JsonArray)1 JsonObject (org.apache.jena.atlas.json.JsonObject)1 JsonValue (org.apache.jena.atlas.json.JsonValue)1