Search in sources :

Example 1 with ExprBuildException

use of org.apache.jena.sparql.sse.builders.ExprBuildException in project jena by apache.

the class ExprTransformExpand method transform.

@Override
public Expr transform(ExprFunctionN func, ExprList args) {
    ExprFunction f = func.getFunction();
    if (this.shouldExpand(f)) {
        UserDefinedFunctionDefinition def = this.definitions.get(f.getFunction().getFunctionIRI());
        UserDefinedFunction uFunc = (UserDefinedFunction) def.newFunctionInstance();
        //Need to watch out for the case where the arguments supplied to the invoked
        //function are in a different order to the arguments supplied to the defined
        //function
        //Thus we will build the list of arguments used to expand the inner function
        //manually
        List<Var> defArgs = def.getArgList();
        ExprList subArgs = new ExprList();
        for (int i = 0; i < args.size(); i++) {
            Expr arg = args.get(i);
            String var = arg.getVarName();
            if (var == null) {
                //Non-variable args may be passed as-is
                subArgs.add(arg);
            } else {
                //We then use the arg as-is to substitute
                if (i > defArgs.size())
                    throw new ExprBuildException("Unable to expand function dependency, the function <" + def.getUri() + "> is called but uses an argument ?" + var + " which is not an argument to the outer function");
                subArgs.add(arg);
            }
        }
        //Expand the function
        uFunc.build(def.getUri(), subArgs);
        return uFunc.getActualExpr();
    } else {
        return super.transform(func, args);
    }
}
Also used : ExprBuildException(org.apache.jena.sparql.sse.builders.ExprBuildException) Var(org.apache.jena.sparql.core.Var)

Example 2 with ExprBuildException

use of org.apache.jena.sparql.sse.builders.ExprBuildException in project jena by apache.

the class UserDefinedFunction method build.

/**
     * Builds the expression substituting the arguments given into the base expression to yield the actual expression to evaluate
     * @throws ExprBuildException Thrown if an expression cannot be generated
     */
@Override
public void build(String uri, ExprList args) {
    //Substitutes the arguments into the base expression to give the actual expression to evaluate
    if (uri == null || !uri.equals(this.getUri()))
        throw new ExprBuildException("Incorrect URI passed to build() call, expected <" + this.getUri() + "> but got <" + uri + ">");
    if (this.getArgList().size() != args.size())
        throw new ExprBuildException("Incorrect number of arguments for user defined <" + this.getUri() + "> function");
    Map<String, Expr> substitutions = new HashMap<>();
    for (int i = 0; i < this.getArgList().size(); i++) {
        substitutions.put(this.getArgList().get(i).getVarName(), args.get(i));
    }
    this.actualExpr = ExprTransformer.transform(new ExprTransformSubstitute(substitutions), this.getBaseExpr());
}
Also used : ExprBuildException(org.apache.jena.sparql.sse.builders.ExprBuildException) HashMap(java.util.HashMap)

Example 3 with ExprBuildException

use of org.apache.jena.sparql.sse.builders.ExprBuildException in project jena by apache.

the class ExprUtils method evalPrint.

public static void evalPrint(Expr expr, Binding binding) {
    try {
        NodeValue r = eval(expr, binding);
        //System.out.println(r.asQuotedString()) ;
        Node n = r.asNode();
        String s = FmtUtils.stringForNode(n);
        System.out.println(s);
    } catch (ExprEvalException ex) {
        System.out.println("Exception: " + ex.getMessage());
        return;
    } catch (ExprBuildException ex) {
        System.err.println("Build exception: " + ex.getMessage());
        return;
    }
}
Also used : ExprBuildException(org.apache.jena.sparql.sse.builders.ExprBuildException) Node(org.apache.jena.graph.Node)

Aggregations

ExprBuildException (org.apache.jena.sparql.sse.builders.ExprBuildException)3 HashMap (java.util.HashMap)1 Node (org.apache.jena.graph.Node)1 Var (org.apache.jena.sparql.core.Var)1