org.apache.jena.sparql.util.ExprUtils Java Examples

The following examples show how to use org.apache.jena.sparql.util.ExprUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ST_Format.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public NodeValue exec(Binding binding, ExprList args, String uri, FunctionEnv env) {
    final boolean isDebugStConcat = ContextUtils.isDebugStConcat(env.getContext());
    if (args == null) // The contract on the function interface is that this should not happen.
    {
        throw new ARQInternalErrorException("FunctionBase: Null args list");
    }

    List<NodeValue> evalArgs = new ArrayList<>();
    for (Expr e : args) {
        try {
            NodeValue x = e.eval(binding, env);
            evalArgs.add(x);
        } catch (Exception ex) {
            StringWriter sw = new StringWriter();
            ex.printStackTrace(new PrintWriter(sw));
            String message = String.format("Error executing st:format with expression %s and binding %s: %s", ExprUtils.fmtSPARQL(args), LogUtils.compress(binding).toString(), sw.toString());
            if (LOG.isDebugEnabled()) {
                LOG.debug(message, ex);
            }
            if (isDebugStConcat) {
                evalArgs.add(new NodeValueString(String.format("\n<<<<<<<<<< %s >>>>>>>>>>\n", message)));
            }
        }
    }

    NodeValue nv = exec(evalArgs, env);
    return nv;
}
 
Example #2
Source File: FunctionExpression.java    From shacl with Apache License 2.0 5 votes vote down vote up
public FunctionExpression(RDFNode expr, Resource function, List<NodeExpression> args) {
	
	super(expr);
	
	this.args = args;
	this.function = function;
	
	if(function.getNameSpace().equals(SPARQL.NS)) {
		ItemList il = new ItemList();
		il.add(function.asNode());
		for(int i = 0; i < args.size(); i++) {
			il.add(Var.alloc("a" + i));
		}
		BuilderExpr.Build build = bob.getBuild(function.getLocalName());
		if(build == null) {
			throw new IllegalArgumentException("Unknown SPARQL built-in " + function.getLocalName());
		}
		this.expr = build.make(il);
	}
	else {
		StringBuffer sb = new StringBuffer();
		sb.append("<");
		sb.append(function);
		sb.append(">(");
		for(int i = 0; i < args.size(); i++) {
			if(i > 0) {
				sb.append(",");
			}
			sb.append("?a" + i);
		}
		sb.append(")");
		this.expr = ExprUtils.parse(sb.toString());
	}
}
 
Example #3
Source File: FunctionTest.java    From tarql with BSD 2-Clause "Simplified" License 4 votes vote down vote up
protected Node eval(String expression) {
	Expr expr = ExprUtils.parse(expression, prefixes);
	return expr.eval(BindingFactory.root(), env).asNode();
}