Search in sources :

Example 96 with Var

use of org.apache.jena.sparql.core.Var in project jena by apache.

the class SelectHandlerTest method testAddStringVar.

@Test
public void testAddStringVar() {
    Var v = Var.alloc("foo");
    handler.addVar("rand()", v);
    VarExprList expr = query.getProject();
    assertEquals(1, expr.size());
    Expr e = expr.getExpr(Var.alloc("foo"));
    assertNotNull("expression should not be null", e);
    assertTrue("Should be an E_Random", e instanceof E_Random);
}
Also used : Expr(org.apache.jena.sparql.expr.Expr) E_Random(org.apache.jena.sparql.expr.E_Random) Var(org.apache.jena.sparql.core.Var) VarExprList(org.apache.jena.sparql.core.VarExprList) Test(org.junit.Test)

Example 97 with Var

use of org.apache.jena.sparql.core.Var in project jena by apache.

the class SolutionModifierHandlerTest method testSetVarsHaving.

@Test
public void testSetVarsHaving() {
    Var v = Var.alloc("v");
    solutionModifier.addHaving(v);
    String[] s = byLine(query.toString());
    assertContainsRegex(HAVING + var("v"), s);
    Map<Var, Node> values = new HashMap<>();
    values.put(v, Var.alloc("v2"));
    solutionModifier.setVars(values);
    s = byLine(query.toString());
    assertContainsRegex(HAVING + var("v2"), s);
}
Also used : HashMap(java.util.HashMap) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) Test(org.junit.Test)

Example 98 with Var

use of org.apache.jena.sparql.core.Var in project jena by apache.

the class SolutionModifierHandlerTest method testSetVarsOrderBy.

@Test
public void testSetVarsOrderBy() {
    Var v = Var.alloc("v");
    solutionModifier.addOrderBy(v);
    String[] s = byLine(query.toString());
    assertContainsRegex(ORDER_BY + var("v"), s);
    Map<Var, Node> values = new HashMap<>();
    values.put(v, Var.alloc("v2"));
    solutionModifier.setVars(values);
    s = byLine(query.toString());
    assertContainsRegex(ORDER_BY + var("v2"), s);
}
Also used : HashMap(java.util.HashMap) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) Test(org.junit.Test)

Example 99 with Var

use of org.apache.jena.sparql.core.Var in project jena by apache.

the class ValuesHandlerTest method twoVarTwoBlocksWithVarReplacement.

@Test
public void twoVarTwoBlocksWithVarReplacement() {
    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, null);
    handler.addValueVar(v2, null);
    handler.addValueRow(Arrays.asList(n, n2));
    handler.addValueRow(Arrays.asList(nn, nn2));
    Map<Var, Node> replaceVars = new HashMap<Var, Node>();
    replaceVars.put(v2, NodeFactory.createBlankNode());
    handler.setVars(replaceVars);
    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(nn);
    for (Binding b : lb) {
        assertTrue(b.contains(v));
        assertFalse(b.contains(v2));
        assertTrue(ln.contains(b.get(v)));
        ln.remove(b.get(v));
    }
}
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 100 with Var

use of org.apache.jena.sparql.core.Var in project jena by apache.

the class AbstractQueryBuilderTest method testMakeVar.

@Test
public void testMakeVar() {
    Var v = AbstractQueryBuilder.makeVar(null);
    assertEquals(Var.ANON, v);
    v = AbstractQueryBuilder.makeVar("a");
    assertEquals(Var.alloc("a"), v);
    v = AbstractQueryBuilder.makeVar("?a");
    assertEquals(Var.alloc("a"), v);
    Node n = NodeFactory.createVariable("foo");
    v = AbstractQueryBuilder.makeVar(n);
    assertEquals(Var.alloc("foo"), v);
    NodeFront nf = new NodeFront(n);
    v = AbstractQueryBuilder.makeVar(nf);
    assertEquals(Var.alloc("foo"), v);
    v = AbstractQueryBuilder.makeVar(Node_RuleVariable.WILD);
    assertNull(v);
    ExprVar ev = new ExprVar("bar");
    v = AbstractQueryBuilder.makeVar(ev);
    assertEquals(Var.alloc("bar"), v);
    ev = new ExprVar(n);
    v = AbstractQueryBuilder.makeVar(ev);
    assertEquals(Var.alloc("foo"), v);
    ev = new ExprVar(Var.ANON);
    v = AbstractQueryBuilder.makeVar(ev);
    assertEquals(Var.ANON, v);
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) FrontsNode(org.apache.jena.graph.FrontsNode) Node(org.apache.jena.graph.Node) Test(org.junit.Test)

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