Search in sources :

Example 96 with Expr

use of org.apache.jena.sparql.expr.Expr 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)

Example 97 with Expr

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

the class qexpr method main2.

public static void main2(String... argv) {
    CmdLineArgs cl = new CmdLineArgs(argv);
    ArgDecl helpDecl = new ArgDecl(ArgDecl.NoValue, "h", "help");
    cl.add(helpDecl);
    ArgDecl verboseDecl = new ArgDecl(ArgDecl.NoValue, "v", "verbose");
    cl.add(verboseDecl);
    ArgDecl versionDecl = new ArgDecl(ArgDecl.NoValue, "ver", "version", "V");
    cl.add(versionDecl);
    ArgDecl quietDecl = new ArgDecl(ArgDecl.NoValue, "q", "quiet");
    cl.add(quietDecl);
    ArgDecl reduceDecl = new ArgDecl(ArgDecl.NoValue, "reduce", "fold", "simplify");
    cl.add(reduceDecl);
    ArgDecl strictDecl = new ArgDecl(ArgDecl.NoValue, "strict");
    cl.add(strictDecl);
    ArgDecl printDecl = new ArgDecl(ArgDecl.HasValue, "print");
    cl.add(printDecl);
    try {
        cl.process();
    } catch (IllegalArgumentException ex) {
        System.err.println(ex.getMessage());
        usage(System.err);
        throw new CmdException();
    }
    if (cl.contains(helpDecl)) {
        usage();
        throw new TerminationException(0);
    }
    if (cl.contains(versionDecl)) {
        System.out.println("ARQ Version: " + ARQ.VERSION + " (Jena: " + Jena.VERSION + ")");
        throw new TerminationException(0);
    }
    // ==== General things
    boolean verbose = cl.contains(verboseDecl);
    boolean quiet = cl.contains(quietDecl);
    if (cl.contains(strictDecl))
        ARQ.setStrictMode();
    boolean actionCopySubstitute = cl.contains(reduceDecl);
    boolean actionPrintPrefix = false;
    boolean actionPrintSPARQL = false;
    boolean actionPrint = cl.contains(printDecl);
    for (String v : cl.getValues(printDecl)) {
        if (v.equalsIgnoreCase("prefix") || v.equalsIgnoreCase("op")) {
            actionPrintPrefix = true;
        } else if (v.equalsIgnoreCase("expr")) {
            actionPrintSPARQL = true;
        } else {
            System.err.println("Unknown print form: " + v);
            throw new TerminationException(0);
        }
    }
    for (int i = 0; i < cl.getNumPositional(); i++) {
        String exprStr = cl.getPositionalArg(i);
        exprStr = cl.indirect(exprStr);
        try {
            PrefixMapping pmap = PrefixMapping.Factory.create();
            pmap.setNsPrefixes(ARQConstants.getGlobalPrefixMap());
            pmap.setNsPrefix("", "http://example/");
            pmap.setNsPrefix("ex", "http://example/ns#");
            Expr expr = ExprUtils.parse(exprStr, pmap);
            if (actionPrint) {
                IndentedWriter iOut = IndentedWriter.stdout;
                if (actionPrintSPARQL) {
                    ExprUtils.fmtSPARQL(iOut, expr);
                    iOut.ensureStartOfLine();
                }
                if (actionPrintPrefix) {
                    WriterSSE.out(iOut, expr, new Prologue(pmap));
                    iOut.ensureStartOfLine();
                }
                iOut.flush();
                continue;
            }
            if (verbose)
                System.out.print(expr.toString() + " => ");
            try {
                if (actionCopySubstitute) {
                    Expr e = ExprLib.foldConstants(expr);
                    System.out.println(e);
                } else {
                    // Default action
                    ARQ.getContext().set(ARQConstants.sysCurrentTime, NodeFactoryExtra.nowAsDateTime());
                    FunctionEnv env = new ExecutionContext(ARQ.getContext(), null, null, null);
                    NodeValue r = expr.eval(null, env);
                    // System.out.println(r.asQuotedString()) ;
                    Node n = r.asNode();
                    String s = NodeFmtLib.displayStr(n);
                    System.out.println(s);
                }
            } catch (ExprEvalException ex) {
                System.out.println("Exception: " + ex.getMessage());
                throw new TerminationException(2);
            }
        } catch (QueryParseException ex) {
            System.err.println("Parse error: " + ex.getMessage());
            throw new TerminationException(2);
        }
    }
}
Also used : NodeValue(org.apache.jena.sparql.expr.NodeValue) CmdException(org.apache.jena.cmd.CmdException) Node(org.apache.jena.graph.Node) ArgDecl(org.apache.jena.cmd.ArgDecl) CmdLineArgs(org.apache.jena.cmd.CmdLineArgs) QueryParseException(org.apache.jena.query.QueryParseException) IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) FunctionEnv(org.apache.jena.sparql.function.FunctionEnv) PrefixMapping(org.apache.jena.shared.PrefixMapping) TerminationException(org.apache.jena.cmd.TerminationException) Prologue(org.apache.jena.sparql.core.Prologue) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) Expr(org.apache.jena.sparql.expr.Expr) ExprEvalException(org.apache.jena.sparql.expr.ExprEvalException)

Example 98 with Expr

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

the class SolutionModifierTest method testAddOrderByExprDescending.

@ContractTest
public void testAddOrderByExprDescending() {
    SolutionModifierClause<?> solutionModifier = getProducer().newInstance();
    Expr e = new E_Random();
    AbstractQueryBuilder<?> builder = solutionModifier.addOrderBy(e, Order.DESCENDING);
    Query query = builder.build();
    assertTrue(query.hasOrderBy());
    List<SortCondition> orderBy = query.getOrderBy();
    assertEquals(1, orderBy.size());
    assertEquals(Query.ORDER_DESCENDING, orderBy.get(0).getDirection());
    assertEquals(e, orderBy.get(0).getExpression());
    builder = solutionModifier.addOrderBy("bar");
    query = builder.build();
    assertTrue(query.hasOrderBy());
    orderBy = query.getOrderBy();
    assertEquals(2, orderBy.size());
    assertEquals(Query.ORDER_DESCENDING, orderBy.get(0).getDirection());
    assertEquals(e, orderBy.get(0).getExpression());
    assertEquals(Query.ORDER_DEFAULT, orderBy.get(1).getDirection());
    assertEquals(new ExprVar("bar"), orderBy.get(1).getExpression());
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) SortCondition(org.apache.jena.query.SortCondition) Expr(org.apache.jena.sparql.expr.Expr) Query(org.apache.jena.query.Query) E_Random(org.apache.jena.sparql.expr.E_Random) ContractTest(org.xenei.junit.contract.ContractTest)

Example 99 with Expr

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

the class SolutionModifierTest method testSetVarsHaving_Node_Variable.

@ContractTest
public void testSetVarsHaving_Node_Variable() throws ParseException {
    Node v = NodeFactory.createVariable("v");
    SolutionModifierClause<?> solutionModifier = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = solutionModifier.addHaving(v);
    List<Expr> exprs = builder.build().getHavingExprs();
    Assert.assertEquals(1, exprs.size());
    Assert.assertEquals(new ExprVar(Var.alloc(v)), exprs.get(0));
    builder.setVar(v, Var.alloc("v2"));
    exprs = builder.build().getHavingExprs();
    Assert.assertEquals(1, exprs.size());
    Assert.assertEquals(new ExprVar(Var.alloc("v2")), exprs.get(0));
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) Expr(org.apache.jena.sparql.expr.Expr) Node(org.apache.jena.graph.Node) ContractTest(org.xenei.junit.contract.ContractTest)

Example 100 with Expr

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

the class ExprFactoryTest method isLiteralTest.

@Test
public void isLiteralTest() {
    Expr e = factory.isLiteral(factory.none());
    assertTrue(e instanceof E_IsLiteral);
}
Also used : E_IsLiteral(org.apache.jena.sparql.expr.E_IsLiteral) Expr(org.apache.jena.sparql.expr.Expr) Test(org.junit.Test)

Aggregations

Expr (org.apache.jena.sparql.expr.Expr)187 Test (org.junit.Test)102 ExprVar (org.apache.jena.sparql.expr.ExprVar)34 Var (org.apache.jena.sparql.core.Var)30 ExprList (org.apache.jena.sparql.expr.ExprList)16 Op (org.apache.jena.sparql.algebra.Op)15 NodeValue (org.apache.jena.sparql.expr.NodeValue)12 ContractTest (org.xenei.junit.contract.ContractTest)11 Query (org.apache.jena.query.Query)10 Node (org.apache.jena.graph.Node)9 E_Random (org.apache.jena.sparql.expr.E_Random)9 ArrayList (java.util.ArrayList)8 VarExprList (org.apache.jena.sparql.core.VarExprList)8 Triple (org.apache.jena.graph.Triple)6 SortCondition (org.apache.jena.query.SortCondition)6 E_Regex (org.apache.jena.sparql.expr.E_Regex)6 E_LessThan (org.apache.jena.sparql.expr.E_LessThan)5 LibTestExpr (org.apache.jena.sparql.expr.LibTestExpr)5 OpBGP (org.apache.jena.sparql.algebra.op.OpBGP)4 BasicPattern (org.apache.jena.sparql.core.BasicPattern)4