Search in sources :

Example 81 with Expr

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

the class ProcedureEval method exec.

@Override
public QueryIterator exec(Binding binding, Node name, ExprList args, ExecutionContext execCxt) {
    // Eval if possible.
    ExprList evalArgs = new ExprList();
    for (Expr e : args) {
        if (e.isVariable()) {
            Var v = e.getExprVar().asVar();
            // Special case - allow unevaluated variables.
            if (binding.contains(v))
                evalArgs.add(e.eval(binding, execCxt));
            else
                evalArgs.add(e);
        } else {
            NodeValue x = e.eval(binding, execCxt);
            evalArgs.add(x);
        }
    }
    return execEval(binding, evalArgs, execCxt);
}
Also used : NodeValue(org.apache.jena.sparql.expr.NodeValue) Expr(org.apache.jena.sparql.expr.Expr) ExprList(org.apache.jena.sparql.expr.ExprList) Var(org.apache.jena.sparql.core.Var)

Example 82 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#");
            //              Node n = asNode() ;
            //              return makeNode(n) ;
            Expr expr = ExprUtils.parse(exprStr, pmap);
            if (verbose)
                System.out.print(expr.toString() + " => ");
            if (actionPrint) {
                if (actionPrintSPARQL)
                    System.out.println(ExprUtils.fmtSPARQL(expr));
                if (actionPrintPrefix)
                    WriterSSE.out(IndentedWriter.stdout, expr, new Prologue(pmap));
                continue;
            }
            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(jena.cmd.CmdException) Node(org.apache.jena.graph.Node) ArgDecl(jena.cmd.ArgDecl) CmdLineArgs(jena.cmd.CmdLineArgs) QueryParseException(org.apache.jena.query.QueryParseException) FunctionEnv(org.apache.jena.sparql.function.FunctionEnv) PrefixMapping(org.apache.jena.shared.PrefixMapping) TerminationException(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 83 with Expr

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

the class AbstractRewriter method rewrite.

/**
	 * Rewrite a variable expression list.
	 * @param lst The variable expression list.
	 * @return the rewritten variable expresson list.
	 */
public final VarExprList rewrite(VarExprList lst) {
    VarExprList retval = new VarExprList();
    for (Var v : lst.getVars()) {
        Node n = values.get(v);
        if (n != null) {
            if (n.isVariable()) {
                retval.add(Var.alloc(n));
            }
        } else {
            retval.add(v);
        }
    }
    for (Map.Entry<Var, Expr> entry : lst.getExprs().entrySet()) {
        Expr target = ExprUtils.nodeToExpr(entry.getKey());
        Node n = values.get(entry.getKey());
        Var v = entry.getKey();
        Expr e = entry.getValue();
        if (n != null) {
            if (n.isVariable()) {
                v = Var.alloc(n);
                if (target.equals(e)) {
                    e = ExprUtils.nodeToExpr(n);
                }
            } else {
                v = null;
            }
        }
        if (v != null) {
            retval.add(v, e);
        }
    }
    return retval;
}
Also used : Expr(org.apache.jena.sparql.expr.Expr) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) VarExprList(org.apache.jena.sparql.core.VarExprList) BindingHashMap(org.apache.jena.sparql.engine.binding.BindingHashMap) Map(java.util.Map)

Aggregations

Expr (org.apache.jena.sparql.expr.Expr)83 Var (org.apache.jena.sparql.core.Var)28 ExprList (org.apache.jena.sparql.expr.ExprList)15 NodeValue (org.apache.jena.sparql.expr.NodeValue)15 Op (org.apache.jena.sparql.algebra.Op)13 ExprVar (org.apache.jena.sparql.expr.ExprVar)12 FunctionEnvBase (org.apache.jena.sparql.function.FunctionEnvBase)9 Test (org.junit.Test)9 VarExprList (org.apache.jena.sparql.core.VarExprList)8 Node (org.apache.jena.graph.Node)7 E_Random (org.apache.jena.sparql.expr.E_Random)7 ArrayList (java.util.ArrayList)6 Triple (org.apache.jena.graph.Triple)4 E_Multiply (org.apache.jena.sparql.expr.E_Multiply)4 E_Regex (org.apache.jena.sparql.expr.E_Regex)4 ContractTest (org.xenei.junit.contract.ContractTest)4 IndentedLineBuffer (org.apache.jena.atlas.io.IndentedLineBuffer)3 Query (org.apache.jena.query.Query)3 SortCondition (org.apache.jena.query.SortCondition)3 ExprAggregator (org.apache.jena.sparql.expr.ExprAggregator)3