Search in sources :

Example 1 with E_LessThan

use of org.apache.jena.sparql.expr.E_LessThan in project jena by apache.

the class WhereHandlerTest method testSetVarsInFilter.

@Test
public void testSetVarsInFilter() throws ParseException {
    handler.addFilter("?one < ?v");
    handler.build();
    E_LessThan expr = new ExprFactory().lt(NodeFactory.createVariable("one"), NodeFactory.createVariable("v"));
    WhereValidator visitor = new WhereValidator(new ElementFilter(expr));
    handler.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
    Map<Var, Node> values = new HashMap<>();
    values.put(Var.alloc("v"), NodeFactory.createLiteral(LiteralLabelFactory.createTypedLiteral(10)));
    handler.setVars(values);
    handler.build();
    expr = new ExprFactory().lt(NodeFactory.createVariable("one"), NodeFactory.createLiteral(LiteralLabelFactory.createTypedLiteral(10)));
    visitor = new WhereValidator(new ElementFilter(expr));
    handler.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) Var(org.apache.jena.sparql.core.Var) ElementFilter(org.apache.jena.sparql.syntax.ElementFilter) Node(org.apache.jena.graph.Node) E_LessThan(org.apache.jena.sparql.expr.E_LessThan) ExprFactory(org.apache.jena.arq.querybuilder.ExprFactory) Test(org.junit.Test)

Example 2 with E_LessThan

use of org.apache.jena.sparql.expr.E_LessThan in project jena by apache.

the class SolutionModifierTest method testAddHavingString.

@ContractTest
public void testAddHavingString() throws ParseException {
    SolutionModifierClause<?> solutionModifier = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = solutionModifier.addHaving("?foo<10");
    Query query = builder.build();
    assertTrue(query.hasHaving());
    List<Expr> having = query.getHavingExprs();
    assertEquals(1, having.size());
    Expr exp = having.get(0);
    assertTrue(exp.isFunction());
    assertTrue(exp.getFunction() instanceof E_LessThan);
    List<Expr> args = exp.getFunction().getArgs();
    assertEquals(new ExprVar("foo"), args.get(0));
    assertEquals(new NodeValueInteger(10), args.get(1));
    builder = solutionModifier.addHaving("?bar < 10");
    query = builder.build();
    assertTrue(query.hasHaving());
    having = query.getHavingExprs();
    assertEquals(2, having.size());
    exp = having.get(0);
    assertTrue(exp.isFunction());
    assertTrue(exp.getFunction() instanceof E_LessThan);
    args = exp.getFunction().getArgs();
    assertEquals(new ExprVar("foo"), args.get(0));
    assertEquals(new NodeValueInteger(10), args.get(1));
    exp = having.get(1);
    assertTrue(exp.isFunction());
    assertTrue(exp.getFunction() instanceof E_LessThan);
    args = exp.getFunction().getArgs();
    assertEquals(new ExprVar("bar"), args.get(0));
    assertEquals(new NodeValueInteger(10), args.get(1));
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) Query(org.apache.jena.query.Query) Expr(org.apache.jena.sparql.expr.Expr) NodeValueInteger(org.apache.jena.sparql.expr.nodevalue.NodeValueInteger) E_LessThan(org.apache.jena.sparql.expr.E_LessThan) ContractTest(org.xenei.junit.contract.ContractTest)

Example 3 with E_LessThan

use of org.apache.jena.sparql.expr.E_LessThan in project jena by apache.

the class WhereClauseTest method testSetVarsInFilter.

@ContractTest
public void testSetVarsInFilter() throws ParseException {
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addFilter("?one < ?v");
    ExprVar one = new ExprVar(NodeFactory.createVariable("one"));
    ExprVar v = new ExprVar(NodeFactory.createVariable("v"));
    Expr expr = new E_LessThan(one, v);
    ElementFilter filter = new ElementFilter(expr);
    WhereValidator visitor = new WhereValidator(filter);
    builder.build().getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
    Node literal = NodeFactory.createLiteral(LiteralLabelFactory.createTypedLiteral(10));
    builder.setVar(Var.alloc("v"), literal);
    NodeValueInteger lit = new NodeValueInteger(10);
    expr = new E_LessThan(one, lit);
    filter = new ElementFilter(expr);
    builder.build().getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) Expr(org.apache.jena.sparql.expr.Expr) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) ElementFilter(org.apache.jena.sparql.syntax.ElementFilter) Node(org.apache.jena.graph.Node) NodeValueInteger(org.apache.jena.sparql.expr.nodevalue.NodeValueInteger) E_LessThan(org.apache.jena.sparql.expr.E_LessThan) ContractTest(org.xenei.junit.contract.ContractTest)

Example 4 with E_LessThan

use of org.apache.jena.sparql.expr.E_LessThan in project jena by apache.

the class ExprFactoryTest method ltTest.

@Test
public void ltTest() {
    Expr e = factory.lt(factory.none(), factory.none());
    assertTrue(e instanceof E_LessThan);
}
Also used : Expr(org.apache.jena.sparql.expr.Expr) E_LessThan(org.apache.jena.sparql.expr.E_LessThan) Test(org.junit.Test)

Example 5 with E_LessThan

use of org.apache.jena.sparql.expr.E_LessThan in project jena by apache.

the class AlgebraExec method main.

public static void main(String[] argv) {
    String BASE = "http://example/";
    BasicPattern bp = new BasicPattern();
    Var var_x = Var.alloc("x");
    Var var_z = Var.alloc("z");
    // ---- Build expression
    bp.add(new Triple(var_x, NodeFactory.createURI(BASE + "p"), var_z));
    Op op = new OpBGP(bp);
    //Expr expr = ExprUtils.parse("?z < 2 ") ;
    Expr expr = new E_LessThan(new ExprVar(var_z), NodeValue.makeNodeInteger(2));
    op = OpFilter.filter(expr, op);
    // ---- Example setup
    Model m = makeModel();
    m.write(System.out, "TTL");
    System.out.println("--------------");
    System.out.print(op);
    System.out.println("--------------");
    // ---- Execute expression
    QueryIterator qIter = Algebra.exec(op, m.getGraph());
    // -------- Either read the query iterator directly ...
    if (false) {
        for (; qIter.hasNext(); ) {
            Binding b = qIter.nextBinding();
            Node n = b.get(var_x);
            System.out.println(NodeFmtLib.displayStr(n));
            System.out.println(b);
        }
        qIter.close();
    } else {
        // -------- Or make ResultSet from it (but not both - reading an
        //          iterator consumes the current solution)
        List<String> varNames = new ArrayList<>();
        varNames.add("x");
        varNames.add("z");
        ResultSet rs = new ResultSetStream(varNames, m, qIter);
        ResultSetFormatter.out(rs);
        qIter.close();
    }
    System.exit(0);
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) Binding(org.apache.jena.sparql.engine.binding.Binding) Op(org.apache.jena.sparql.algebra.Op) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) OpBGP(org.apache.jena.sparql.algebra.op.OpBGP) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) BasicPattern(org.apache.jena.sparql.core.BasicPattern) E_LessThan(org.apache.jena.sparql.expr.E_LessThan) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream) Triple(org.apache.jena.graph.Triple) Expr(org.apache.jena.sparql.expr.Expr) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) ResultSet(org.apache.jena.query.ResultSet)

Aggregations

E_LessThan (org.apache.jena.sparql.expr.E_LessThan)8 Expr (org.apache.jena.sparql.expr.Expr)5 ExprVar (org.apache.jena.sparql.expr.ExprVar)5 WhereValidator (org.apache.jena.arq.querybuilder.WhereValidator)4 Node (org.apache.jena.graph.Node)4 ElementFilter (org.apache.jena.sparql.syntax.ElementFilter)4 Var (org.apache.jena.sparql.core.Var)3 NodeValueInteger (org.apache.jena.sparql.expr.nodevalue.NodeValueInteger)3 Test (org.junit.Test)3 ContractTest (org.xenei.junit.contract.ContractTest)3 ArrayList (java.util.ArrayList)2 ExprFactory (org.apache.jena.arq.querybuilder.ExprFactory)2 Triple (org.apache.jena.graph.Triple)2 ResultSet (org.apache.jena.query.ResultSet)2 Op (org.apache.jena.sparql.algebra.Op)2 OpBGP (org.apache.jena.sparql.algebra.op.OpBGP)2 BasicPattern (org.apache.jena.sparql.core.BasicPattern)2 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)2 Binding (org.apache.jena.sparql.engine.binding.Binding)2 BigInteger (java.math.BigInteger)1