Search in sources :

Example 1 with BindingBuilder

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

the class TestQueryIterSort method randomBinding.

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

Example 2 with BindingBuilder

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

the class TestIndexTable method setup.

@Before
public void setup() {
    vars = new Var[] { Var.alloc("a"), Var.alloc("b"), Var.alloc("c") };
    order1 = new LinkedHashSet<>();
    order2 = new LinkedHashSet<>();
    for (int i = 0; i < vars.length; i++) {
        order1.add(vars[i]);
        order2.add(vars[vars.length - i - 1]);
    }
    fData = new ArrayList<>();
    pData = new ArrayList<>();
    for (int i = 10; i <= 100; i += 10) {
        BindingBuilder bindingFull = Binding.builder();
        BindingBuilder bindingPart = Binding.builder();
        for (int b = 0; b < vars.length; b++) {
            // 10,11,12 - 20,21,22 - 30,31,32 ... 100,101,102
            bindingFull.add(vars[b], makeIntNode(i + b));
            // skips 21, 42, 70, 91
            if ((i + b) % 7 != 0)
                bindingPart.add(vars[b], makeIntNode(i + b));
        }
        fData.add(bindingFull.build());
        pData.add(bindingPart.build());
    }
}
Also used : BindingBuilder(org.apache.jena.sparql.engine.binding.BindingBuilder) Before(org.junit.Before)

Example 3 with BindingBuilder

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

the class SolverRX4 method match.

// RDF term matcher - may recurse and call matchTriple is a triple term is in the pattern.
private static boolean match(BindingBuilder bb, Node nData, Node nPattern) {
    if (nPattern == null)
        return true;
    if (nData == Node.ANY)
        return true;
    // Deep substitute. This happens anyway as we walk structures.
    // nPattern = Substitute.substitute(nPattern, input);
    // Shallow substitute
    nPattern = Var.lookup(v -> bb.get(v), nPattern);
    // nPattern.isConcrete() : either nPattern is an RDF term or is <<>> with no variables.
    if (nPattern.isConcrete()) {
        // Term comparison.
        return sameTermAs(nData, nPattern);
    }
    // Easy case - nPattern is a variable.
    if (Var.isVar(nPattern)) {
        Var var = Var.alloc(nPattern);
        bb.add(var, nData);
        return true;
    }
    // nPattern is <<>> with variables. Is the data a <<>>?
    if (!nData.isNodeTriple())
        return false;
    // nData is <<>>, nPattern is <<>>
    // Unpack, match components.
    Triple tPattern = nPattern.getTriple();
    Triple tData = nData.getTriple();
    return SolverRX3.bindTriple(bb, tData, tPattern);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Iterator(java.util.Iterator) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Predicate(java.util.function.Predicate) SolverLib.nodeTopLevel(org.apache.jena.sparql.engine.main.solver.SolverLib.nodeTopLevel) Triple(org.apache.jena.graph.Triple) SolverLib.tripleHasEmbTripleWithVars(org.apache.jena.sparql.engine.main.solver.SolverLib.tripleHasEmbTripleWithVars) BindingBuilder(org.apache.jena.sparql.engine.binding.BindingBuilder) Graph(org.apache.jena.graph.Graph) SolverLib.sameTermAs(org.apache.jena.sparql.engine.main.solver.SolverLib.sameTermAs) Substitute(org.apache.jena.sparql.core.Substitute) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) Quad(org.apache.jena.sparql.core.Quad) Iter(org.apache.jena.atlas.iterator.Iter) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) Triple(org.apache.jena.graph.Triple) Var(org.apache.jena.sparql.core.Var)

Example 4 with BindingBuilder

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

the class StageMatchTriple method accessTriple.

private static Iterator<Binding> accessTriple(Binding binding, Graph graph, Triple pattern, Predicate<Triple> filter, ExecutionContext execCxt) {
    Node s = substituteFlat(pattern.getSubject(), binding);
    Node p = substituteFlat(pattern.getPredicate(), binding);
    Node o = substituteFlat(pattern.getObject(), binding);
    BindingBuilder resultsBuilder = Binding.builder(binding);
    Node s2 = tripleNode(s);
    Node p2 = tripleNode(p);
    Node o2 = tripleNode(o);
    ExtendedIterator<Triple> graphIter = graph.find(s2, p2, o2);
    ExtendedIterator<Binding> iter = graphIter.mapWith(r -> mapper(resultsBuilder, s, p, o, r)).filterDrop(Objects::isNull);
    return iter;
}
Also used : Triple(org.apache.jena.graph.Triple) Binding(org.apache.jena.sparql.engine.binding.Binding) Objects(java.util.Objects) Binding(org.apache.jena.sparql.engine.binding.Binding) Iterator(java.util.Iterator) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) Predicate(java.util.function.Predicate) Triple(org.apache.jena.graph.Triple) ExtendedIterator(org.apache.jena.util.iterator.ExtendedIterator) Iter(org.apache.jena.atlas.iterator.Iter) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) BindingBuilder(org.apache.jena.sparql.engine.binding.BindingBuilder) Graph(org.apache.jena.graph.Graph) BindingBuilder(org.apache.jena.sparql.engine.binding.BindingBuilder) Node(org.apache.jena.graph.Node) Objects(java.util.Objects)

Example 5 with BindingBuilder

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

the class StageMatchData method accessQuad.

static Iterator<Binding> accessQuad(Binding binding, Node graphName, Triple pattern, Predicate<Quad> filter, boolean anyGraph, ExecutionContext execCxt) {
    // Assumes if anyGraph, then graphName ==ANY.
    // graphName == null for triples.
    Node g = graphName;
    Node s = pattern.getSubject();
    Node p = pattern.getPredicate();
    Node o = pattern.getObject();
    Node[] matchConst = new Node[4];
    Var[] vars = new Var[4];
    boolean b = prepareQuad(binding, graphName, pattern, matchConst, vars);
    if (!b)
        return Iter.nullIterator();
    Node gm = matchConst[QG];
    Node sm = matchConst[QS];
    Node pm = matchConst[QP];
    Node om = matchConst[QO];
    DatasetGraph dsg = execCxt.getDataset();
    // Union -> findNG - do not include default graph.
    Iterator<Quad> iterMatches = anyGraph ? dsg.findNG(gm, sm, pm, om) : dsg.find(gm, sm, pm, om);
    if (false) {
        // Debug
        List<Quad> x = Iter.toList(iterMatches);
        System.out.println(x);
        iterMatches = x.iterator();
    }
    // ** Allow a triple or quad filter here.
    if (filter != null)
        iterMatches = Iter.filter(iterMatches, filter);
    // Assumes that tuples are not shared.
    if (anyGraph) {
        iterMatches = Iter.map(iterMatches, quadsToUnion);
        // Guaranteed
        iterMatches = Iter.distinct(iterMatches);
    // This depends on the way indexes are chosen and
    // the indexing pattern. It assumes that the index
    // chosen ends in G so same triples are adjacent
    // in a union query.
    // 
    // If any slot is defined, then the index will be X??G.
    // If no slot is defined, then the index will be ???G.
    // Must be guaranteed.
    // 
    // iterMatches = Iter.distinctAdjacent(iterMatches);
    }
    BindingBuilder bindingBuilder = BindingFactory.builder(binding);
    Function<Quad, Binding> binder = quad -> quadToBinding(bindingBuilder, quad, matchConst, vars);
    return Iter.iter(iterMatches).map(binder).removeNulls();
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Binding(org.apache.jena.sparql.engine.binding.Binding) Iterator(java.util.Iterator) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Predicate(java.util.function.Predicate) Triple(org.apache.jena.graph.Triple) BindingBuilder(org.apache.jena.sparql.engine.binding.BindingBuilder) Function(java.util.function.Function) List(java.util.List) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) Quad(org.apache.jena.sparql.core.Quad) Iter(org.apache.jena.atlas.iterator.Iter) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) BindingFactory(org.apache.jena.sparql.engine.binding.BindingFactory) Quad(org.apache.jena.sparql.core.Quad) BindingBuilder(org.apache.jena.sparql.engine.binding.BindingBuilder) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Aggregations

BindingBuilder (org.apache.jena.sparql.engine.binding.BindingBuilder)30 Node (org.apache.jena.graph.Node)19 Var (org.apache.jena.sparql.core.Var)16 Binding (org.apache.jena.sparql.engine.binding.Binding)14 ArrayList (java.util.ArrayList)6 Iterator (java.util.Iterator)4 Iter (org.apache.jena.atlas.iterator.Iter)4 Triple (org.apache.jena.graph.Triple)4 ExecutionContext (org.apache.jena.sparql.engine.ExecutionContext)4 Predicate (java.util.function.Predicate)3 Graph (org.apache.jena.graph.Graph)3 List (java.util.List)2 Objects (java.util.Objects)2 Function (java.util.function.Function)2 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)2 Quad (org.apache.jena.sparql.core.Quad)2 Substitute (org.apache.jena.sparql.core.Substitute)2 SolverLib.nodeTopLevel (org.apache.jena.sparql.engine.main.solver.SolverLib.nodeTopLevel)2 SolverLib.sameTermAs (org.apache.jena.sparql.engine.main.solver.SolverLib.sameTermAs)2 SolverLib.tripleHasEmbTripleWithVars (org.apache.jena.sparql.engine.main.solver.SolverLib.tripleHasEmbTripleWithVars)2