Search in sources :

Example 81 with Binding

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

the class TestExpressions3 method evalExpr.

// From algebra/SSE
private static void evalExpr(String exprString, String bindingStr, boolean expected) {
    Binding binding = binding(bindingStr);
    Expr expr = SSE.parseExpr(exprString);
    NodeValue nv = expr.eval(binding, FunctionEnvBase.createTest());
    boolean b = XSDFuncOp.booleanEffectiveValue(nv);
    assertEquals(exprString, expected, b);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) BuilderBinding(org.apache.jena.sparql.sse.builders.BuilderBinding)

Example 82 with Binding

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

the class ValuesHandlerTest method oneVarOneData.

@Test
public void oneVarOneData() {
    Node n = NodeFactory.createLiteral("hello");
    Var v = Var.alloc("x");
    handler.addValueVar(v, Arrays.asList(n));
    handler.build();
    List<Var> vars = query.getValuesVariables();
    assertEquals(1, vars.size());
    assertEquals(v, vars.get(0));
    assertNotNull(query.getValuesData());
    List<Binding> lb = query.getValuesData();
    assertEquals(1, lb.size());
    Binding b = lb.get(0);
    assertTrue(b.contains(v));
    assertEquals(n, b.get(v));
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) Test(org.junit.Test)

Example 83 with Binding

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

the class ValuesHandlerTest method testAddNotSquare.

@Test
public void testAddNotSquare() {
    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");
    Var v3 = Var.alloc("z");
    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(v2, Arrays.asList(n3));
    handler2.addValueVar(v3, Arrays.asList(nn3));
    handler.addAll(handler2);
    handler.build();
    List<Var> vars = query.getValuesVariables();
    assertEquals(3, vars.size());
    assertTrue(vars.contains(v));
    assertTrue(vars.contains(v2));
    assertTrue(vars.contains(v3));
    assertNotNull(query.getValuesData());
    List<Binding> lb = query.getValuesData();
    assertEquals(3, lb.size());
    for (Binding b : lb) {
        assertTrue(b.contains(v2));
        Node node = b.get(v2);
        if (node.equals(nn)) {
            assertEquals(n, b.get(v));
            assertFalse(b.contains(v3));
        } else if (node.equals(nn2)) {
            assertEquals(n2, b.get(v));
            assertFalse(b.contains(v3));
        } else if (node.equals(n3)) {
            assertFalse(b.contains(v));
            assertEquals(nn3, b.get(v3));
        } else {
            fail("Wrong data in table");
        }
    }
}
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) Test(org.junit.Test)

Example 84 with Binding

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

the class ValuesHandlerTest method oneVarTwoData.

@Test
public void oneVarTwoData() {
    Node n = NodeFactory.createLiteral("hello");
    Node n2 = NodeFactory.createLiteral("there");
    Var v = Var.alloc("x");
    handler.addValueVar(v, Arrays.asList(n, n2));
    handler.build();
    List<Var> vars = query.getValuesVariables();
    assertEquals(1, vars.size());
    assertEquals(v, vars.get(0));
    assertNotNull(query.getValuesData());
    List<Binding> lb = query.getValuesData();
    assertEquals(2, lb.size());
    List<Node> ln = new ArrayList<Node>();
    ln.add(n);
    ln.add(n2);
    for (Binding b : lb) {
        assertTrue(b.contains(v));
        assertTrue(ln.contains(b.get(v)));
        ln.remove(b.get(v));
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 85 with Binding

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

the class LibSDB method findTriples.

/** Find triples, in the default graph or a named graph. */
public static Iterator<Triple> findTriples(DatasetGraph dsg, Node g, Node s, Node p, Node o) {
    if (Var.isVar(g))
        throw new InternalErrorException("Graph node is a variable : " + g);
    final Node vs = varOrConst(s, "s");
    final Node vp = varOrConst(p, "p");
    final Node vo = varOrConst(o, "o");
    // Evaluate as an algebra expression
    Triple triple = new Triple(vs, vp, vo);
    BasicPattern pattern = new BasicPattern();
    pattern.add(triple);
    Op op = (g != null) ? new OpQuadPattern(g, pattern) : new OpBGP(pattern);
    Plan plan = QueryEngineSDB.getFactory().create(op, dsg, BindingRoot.create(), null);
    QueryIterator qIter = plan.iterator();
    Iterator<Binding> iter;
    if (SDB.getContext().isTrue(SDB.streamGraphAPI)) {
        // Assumes iterator closed properly.
        iter = qIter;
    } else {
        // ---- Safe version: 
        List<Binding> x = Iter.toList(qIter);
        Iterator<Binding> qIter2 = x.iterator();
        qIter.close();
        iter = qIter2;
    }
    return Iter.map(iter, (b) -> bindingToTriple(vs, vp, vo, b));
}
Also used : Triple(org.apache.jena.graph.Triple) Binding(org.apache.jena.sparql.engine.binding.Binding) Op(org.apache.jena.sparql.algebra.Op) OpQuadPattern(org.apache.jena.sparql.algebra.op.OpQuadPattern) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) Node(org.apache.jena.graph.Node) OpBGP(org.apache.jena.sparql.algebra.op.OpBGP) InternalErrorException(org.apache.jena.atlas.lib.InternalErrorException) BasicPattern(org.apache.jena.sparql.core.BasicPattern) Plan(org.apache.jena.sparql.engine.Plan)

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