Search in sources :

Example 66 with Binding

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

the class ResultSetPeeking method nextBinding.

@Override
public Binding nextBinding() {
    if (this.hasPeeked()) {
        Binding b = this.peeked;
        this.peeked = null;
        this.rowNumber++;
        return b;
    } else if (this.canPeek()) {
        Binding b = this.peekBinding();
        this.peeked = null;
        this.rowNumber++;
        return b;
    } else {
        throw new NoSuchElementException();
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) ResultBinding(org.apache.jena.sparql.core.ResultBinding) NoSuchElementException(java.util.NoSuchElementException)

Example 67 with Binding

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

the class TestResultSet method test_RS_peeking_9.

@Test
public void test_RS_peeking_9() {
    // Check that peeking causes the correct row to be returned when we actually access the rows
    Node first = NodeFactory.createURI("tag:first");
    Node second = NodeFactory.createURI("tag:second");
    Var x = Var.alloc("x");
    ResultSet inner = new ResultSetMem(make("x", first), make("x", second));
    ResultSetPeekable rs = ResultSetFactory.makePeekable(inner);
    assertTrue(rs.hasNext());
    // Peek and check row is as expected
    Binding peeked = rs.peekBinding();
    assertNotNull(peeked);
    assertTrue(first.equals(peeked.get(x)));
    // Check first row is as expected
    Binding next = rs.nextBinding();
    assertNotNull(next);
    assertTrue(first.equals(next.get(x)));
    // Repeat for second row
    peeked = rs.peekBinding();
    assertNotNull(peeked);
    assertTrue(second.equals(peeked.get(x)));
    next = rs.nextBinding();
    assertNotNull(next);
    assertTrue(second.equals(next.get(x)));
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) BuilderResultSet(org.apache.jena.sparql.sse.builders.BuilderResultSet) ResultSet(org.apache.jena.query.ResultSet) BaseTest(org.apache.jena.atlas.junit.BaseTest) Test(org.junit.Test)

Example 68 with Binding

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

the class TestResultSet method make2.

private ResultSet make2(String var, Node val) {
    BindingMap b1 = BindingFactory.create();
    b1.add(Var.alloc(var), val);
    BindingMap b2 = BindingFactory.create();
    b2.add(Var.alloc(var), val);
    List<String> vars = new ArrayList<>();
    vars.add(var);
    List<Binding> solutions = new ArrayList<>();
    solutions.add(b1);
    solutions.add(b2);
    QueryIterator qIter = new QueryIterPlainWrapper(solutions.iterator(), null);
    ResultSet rs = new ResultSetStream(vars, null, qIter);
    return rs;
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) QueryIterPlainWrapper(org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper) 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)

Example 69 with Binding

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

the class ValuesHandlerTest method twoVarTwoBlocksReplaceDataVar.

@Test
public void twoVarTwoBlocksReplaceDataVar() {
    Node n = NodeFactory.createLiteral("hello");
    Node nn = NodeFactory.createLiteral("hola");
    Node n2 = NodeFactory.createLiteral("there");
    Var nn2 = Var.alloc("z");
    Var v = Var.alloc("x");
    Var v2 = Var.alloc("y");
    handler.addValueVar(v, null);
    handler.addValueVar(v2, null);
    handler.addValueRow(Arrays.asList(n, n2));
    handler.addValueRow(Arrays.asList(nn, nn2));
    Node rep = NodeFactory.createLiteral("aqui");
    Map<Var, Node> replaceVars = new HashMap<Var, Node>();
    replaceVars.put(nn2, rep);
    handler.setVars(replaceVars);
    handler.build();
    List<Var> vars = query.getValuesVariables();
    assertEquals(2, vars.size());
    assertTrue(vars.contains(v));
    assertTrue(vars.contains(v2));
    assertNotNull(query.getValuesData());
    List<Binding> lb = query.getValuesData();
    assertEquals(2, lb.size());
    List<Node> ln = new ArrayList<Node>();
    ln.add(n);
    ln.add(nn);
    List<Node> ln2 = new ArrayList<Node>();
    ln2.add(n2);
    ln2.add(rep);
    for (Binding b : lb) {
        assertTrue(b.contains(v));
        assertTrue(ln.contains(b.get(v)));
        ln.remove(b.get(v));
        assertTrue(b.contains(v2));
        assertTrue(ln2.contains(b.get(v2)));
        ln2.remove(b.get(v2));
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) HashMap(java.util.HashMap) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 70 with Binding

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

the class ValuesHandlerTest method testAddSquare.

@Test
public void testAddSquare() {
    Node n = NodeFactory.createLiteral("hello");
    Node nn = NodeFactory.createLiteral("hola");
    Node n2 = NodeFactory.createLiteral("there");
    Node nn2 = NodeFactory.createLiteral("aqui");
    Var v = Var.alloc("x");
    Var v2 = Var.alloc("y");
    handler.addValueVar(v, Arrays.asList(n, n2));
    handler.addValueVar(v2, Arrays.asList(nn, nn2));
    ValuesHandler handler2 = new ValuesHandler(new Query());
    Node n3 = NodeFactory.createLiteral("why");
    Node nn3 = NodeFactory.createLiteral("quando");
    handler2.addValueVar(v, Arrays.asList(n3));
    handler2.addValueVar(v2, Arrays.asList(nn3));
    handler.addAll(handler2);
    handler.build();
    List<Var> vars = query.getValuesVariables();
    assertEquals(2, vars.size());
    assertTrue(vars.contains(v));
    assertTrue(vars.contains(v2));
    assertNotNull(query.getValuesData());
    List<Binding> lb = query.getValuesData();
    assertEquals(3, lb.size());
    List<Node> ln = new ArrayList<Node>();
    ln.add(n);
    ln.add(n2);
    ln.add(n3);
    List<Node> ln2 = new ArrayList<Node>();
    ln2.add(nn);
    ln2.add(nn2);
    ln2.add(nn3);
    for (Binding b : lb) {
        assertTrue(b.contains(v));
        assertTrue(ln.contains(b.get(v)));
        ln.remove(b.get(v));
        assertTrue(b.contains(v2));
        assertTrue(ln2.contains(b.get(v2)));
        ln2.remove(b.get(v2));
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Query(org.apache.jena.query.Query) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

Binding (org.apache.jena.sparql.engine.binding.Binding)132 Node (org.apache.jena.graph.Node)39 ArrayList (java.util.ArrayList)36 Var (org.apache.jena.sparql.core.Var)33 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)27 Test (org.junit.Test)21 QueryIterPlainWrapper (org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper)16 BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)10 BindingComparator (org.apache.jena.sparql.engine.binding.BindingComparator)8 BuilderBinding (org.apache.jena.sparql.sse.builders.BuilderBinding)8 NoSuchElementException (java.util.NoSuchElementException)7 Triple (org.apache.jena.graph.Triple)7 ResultSetStream (org.apache.jena.sparql.engine.ResultSetStream)7 ARQInternalErrorException (org.apache.jena.sparql.ARQInternalErrorException)5 Op (org.apache.jena.sparql.algebra.Op)5 BasicPattern (org.apache.jena.sparql.core.BasicPattern)5 ResultSet (org.apache.jena.query.ResultSet)4 SortCondition (org.apache.jena.query.SortCondition)4 TableN (org.apache.jena.sparql.algebra.table.TableN)4 File (java.io.File)3