org.apache.jena.sparql.expr.ExprList Java Examples

The following examples show how to use org.apache.jena.sparql.expr.ExprList. 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: JenaUtil.java    From shacl with Apache License 2.0 6 votes vote down vote up
private static Node invokeFunction(Resource function, ExprList args, Dataset dataset) {

		if (dataset == null) {
	        dataset = ARQFactory.get().getDataset(ModelFactory.createDefaultModel());
	    }
		
		E_Function expr = new E_Function(function.getURI(), args);
		DatasetGraph dsg = dataset.asDatasetGraph();
		Context cxt = ARQ.getContext().copy();
		cxt.set(ARQConstants.sysCurrentTime, NodeFactoryExtra.nowAsDateTime());
		FunctionEnv env = new ExecutionContext(cxt, dsg.getDefaultGraph(), dsg, null);
		try {
			NodeValue r = expr.eval(BindingRoot.create(), env);
			if(r != null) {
				return r.asNode();
			}
		}
		catch(ExprEvalException ex) {
		}
		return null;
	}
 
Example #2
Source File: ST_Decr.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@Override
public NodeValue exec(Binding binding, ExprList args, String uri, FunctionEnv env) {
    if (args == null) {
        throw new ARQInternalErrorException("FunctionBase: Null args list");
    }
    if (args.size() != 0) {
        throw new ExprEvalException("Expecting zero argument");
    }
    IndentedWriter writer = ContextUtils.getTemplateOutput(env.getContext());
    if(writer != null) {
    	writer.decIndent();
    } else {
    	LOG.warn("calling st:decr() outside TEMPLATE context.");
    }
    return EMPTY_NODE;
}
 
Example #3
Source File: ST_Incr.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@Override
public NodeValue exec(Binding binding, ExprList args, String uri, FunctionEnv env) {
    if (args == null) {
        throw new ARQInternalErrorException("FunctionBase: Null args list");
    }
    if (args.size() != 0) {
        throw new ExprEvalException("Expecting zero argument");
    }
    IndentedWriter writer = ContextUtils.getTemplateOutput(env.getContext());
    if(writer != null) {
    	writer.incIndent();
    } else {
    	LOG.warn("calling st:incr() outside TEMPLATE context.");
    }
    return EMPTY_NODE;
}
 
Example #4
Source File: PlanFactory.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
/**
 * Makes the plan for a SPARQL ITERATOR clause.
 *
 * @param elementIterator the SPARQL ITERATOR
 * @return -
 */
static IteratorPlan makeIteratorPlan(
        final ElementIterator elementIterator)
        throws SPARQLExtException {
    Objects.requireNonNull(elementIterator, "The Iterator must not be null");

    List<Var> vars = elementIterator.getVars();
    Expr expr = elementIterator.getExpr();

    Objects.requireNonNull(vars, "The variables of the Iterator must not be null");
    Objects.requireNonNull(expr, "The Expr in the iterator must not be null");
    checkIsTrue(expr.isFunction(), "Iterator should be a function:"
            + " <iri>(...) AS ?var1 ?var2 ...");

    ExprFunction function = expr.getFunction();
    String iri = function.getFunctionIRI();
    ExprList exprList = new ExprList(function.getArgs());
    return new IteratorPlan(iri, exprList, vars);
}
 
Example #5
Source File: FUN_Property.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@Override
public NodeValue exec(Binding binding, ExprList args, String uri, FunctionEnv env) {
    if (args == null) {
        throw new ARQInternalErrorException("FunctionBase: Null args list");
    }
    if (args.size() != 2) {
        throw new ExprEvalException("Expecting two argument");
    }
    NodeValue file = args.get(0).eval(binding, env);
    NodeValue propertyNode = args.get(1).eval(binding, env);
    if (!propertyNode.isString()) {
        throw new ExprEvalException("Second argument must be a string. Got " + propertyNode);
    }
    Properties properties;
    try {
        properties = getProperties(file, env);
    } catch (IOException ex) {
        throw new ExprEvalException("IOException while extracting properties document " + file, ex);
    }
    String prop = properties.getProperty(propertyNode.getString());
    if (prop == null) {
        throw new ExprEvalException("Property " + prop + " not found in properties document " + file);
    }
    return new NodeValueString(prop);
}
 
Example #6
Source File: PlanFactory.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(ElementBox el) {
    final List<Expr> texprs = new ArrayList<>();
    texprs.add(new E_Function(ST.incr, new ExprList()));
    List<Element> elements = el.getTExpressions();
    for (int i = 0; i < elements.size(); i++) {
        elements.get(i).visit(this);
        if (result instanceof E_Function && ((E_Function) result).getFunctionIRI().equals(ST.concat)) {
            texprs.addAll(((E_Function) result).getArgs());
        } else {
            texprs.add(result);
        }
    }
    texprs.add(new E_Function(ST.decr, new ExprList()));
    result = new E_Function(ST.concat, new ExprList(texprs));
}
 
Example #7
Source File: IteratorStreamFunctionBase.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@Override
public final void exec(
        final Binding binding,
        final ExprList args,
        final FunctionEnv env,
        final Consumer<List<List<NodeValue>>> collectionListNodeValue) {

    this.env = env;
    if (args == null) {
        throw new ARQInternalErrorException("IteratorFunctionBase:"
                + " Null args list");
    }

    List<NodeValue> evalArgs = new ArrayList<>();
    for (Expr e : args) {
        try {
            NodeValue x = e.eval(binding, env);
            evalArgs.add(x);
        } catch (ExprEvalException ex) {
            LOG.trace("Cannot evaluate node " + e + ": " + ex.getMessage());
            evalArgs.add(null);
        }
    }
    exec(evalArgs, collectionListNodeValue);
}
 
Example #8
Source File: IteratorFunctionBase3.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void checkBuild(ExprList args) {
    if (args.size() != 3) {
        throw new QueryBuildException("Selector '"
                + this.getClass().getName() + "' takes three arguments");
    }
}
 
Example #9
Source File: QueryAggregatesNormalizer.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
private void normalizeCallParams(ExprNormalizer enzer, SPARQLExtQuery query) {
    if (query.hasName()) {
        query.setName(enzer.normalize(query.getName()));
    }
    if (query.hasCallParameters()) {
        final List<Expr> parameters = query.getCallParameters().getList();
        final List<Expr> nzed = new ArrayList<>();
        parameters.forEach((p) -> {
            nzed.add(enzer.normalize(p));
        });
        query.setCallParameters(new ExprList(nzed));
    }
}
 
Example #10
Source File: ExprNormalizer.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
private Expr normalize(ExprFunctionN func) {
    ExprList args = new ExprList();
    for (Expr expr : func.getArgs()) {
        Expr arg = normalize(expr);
        args.add(arg);
    }
    return func.copy(args);
}
 
Example #11
Source File: ITER_WebSocket.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public void checkBuild(ExprList args) {
    if (args.size() != 1 && args.size() != 2) {
        throw new QueryBuildException("Function '"
                + this.getClass().getName() + "' takes two arguments: (1) "
                + "the server URI to connect to, (2) the message to be sent to"
                + " the server (e.g., a json query).");
    }
}
 
Example #12
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 #13
Source File: PlanFactory.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(ElementFormat el) {
    final List<Expr> texprs = new ArrayList<>();
    texprs.add(el.getExpr().getExpr());
    el.getTExpressions().forEach((e) -> {
        e.visit(this);
        texprs.add(result);
    });
    result = new E_Function(ST.format, new ExprList(texprs));
}
 
Example #14
Source File: EvalUtils.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
public static List<List<Node>> eval(
        final ExprList exprs,
        final List<Binding> bindings,
        final FunctionEnv env) {
    final List<List<Node>> nodesList = new ArrayList<>();
    for (Binding binding : bindings) {
        List<Node> nodes = eval(exprs, binding, env);
        if (nodes != null) {
            nodesList.add(nodes);
        }
    }
    return nodesList;
}
 
Example #15
Source File: EvalUtils.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
public static List<Node> eval(
        final ExprList exprs,
        final Binding binding,
        final FunctionEnv env) {
    Objects.requireNonNull(binding);
    Objects.requireNonNull(env);
    if (exprs == null) {
        return null;
    }
    final List<Node> nodes = new ArrayList<>();
    exprs.forEach((expr) -> {
        nodes.add(eval(expr, binding, env));
    });
    return nodes;
}
 
Example #16
Source File: SPARQLExtFmtUtils.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
public static void fmtSPARQL(IndentedWriter iOut, ExprList exprs, SerializationContext pmap) {
    SPARQLExtFmtExprSPARQL fmt = new SPARQLExtFmtExprSPARQL(iOut, pmap);
    String sep = "";
    for (Expr expr : exprs) {
        iOut.print(sep);
        sep = " , ";
        fmt.format(expr);
    }
}
 
Example #17
Source File: IteratorStreamFunctionBase1.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void checkBuild(ExprList args) {
    if (args.size() != 1) {
        throw new QueryBuildException("Selector '"
                + this.getClass().getName() + "' takes one argument");
    }
}
 
Example #18
Source File: ExpandPrefixedNameFunction.java    From tarql with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public NodeValue exec(Binding binding, ExprList args, String uri, FunctionEnv env) {
	if (args == null) {
		throw new ARQInternalErrorException("ExpandPrefixFunction: Null args list");
	}
	if (args.size() != 1) {
		throw new ExprEvalException("ExpandPrefixFunction: Wrong number of arguments: Wanted 1, got " + args.size());
	}
       return exec(args.get(0).eval(binding, env), env.getContext());
}
 
Example #19
Source File: IteratorFunctionBase0.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void checkBuild(ExprList args) {
    if (args.size() != 0) {
        throw new QueryBuildException("Iterator function '"
                + this.getClass().getName() + "' takes no arguments");
    }
}
 
Example #20
Source File: IteratorFunctionBase1.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void checkBuild(ExprList args) {
    if (args.size() != 1) {
        throw new QueryBuildException("Selector '"
                + this.getClass().getName() + "' takes one argument");
    }
}
 
Example #21
Source File: IteratorStreamFunctionBase5.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void checkBuild(ExprList args) {
    if (args.size() != 5) {
        throw new QueryBuildException("Selector '"
                + this.getClass().getName() + "' takes 5 arguments");
    }
}
 
Example #22
Source File: IteratorStreamFunctionBase0.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void checkBuild(ExprList args) {
    if (!args.isEmpty()) {
        throw new QueryBuildException("Selector '"
                + this.getClass().getName() + "' takes zero arguments");
    }
}
 
Example #23
Source File: IteratorStreamFunctionBase2.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void checkBuild(ExprList args) {
    if (args.size() != 2) {
        throw new QueryBuildException("Selector '"
                + this.getClass().getName() + "' takes two arguments");
    }
}
 
Example #24
Source File: IteratorFunctionBase5.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void checkBuild(ExprList args) {
    if (args.size() != 5) {
        throw new QueryBuildException("Selector '"
                + this.getClass().getName() + "' takes 5 arguments");
    }
}
 
Example #25
Source File: IteratorFunctionBase4.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void checkBuild(ExprList args) {
    if (args.size() != 4) {
        throw new QueryBuildException("Selector '"
                + this.getClass().getName() + "' takes 4 arguments");
    }
}
 
Example #26
Source File: IteratorFunctionBase2.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void checkBuild(ExprList args) {
    if (args.size() != 2) {
        throw new QueryBuildException("Selector '"
                + this.getClass().getName() + "' takes two arguments");
    }
}
 
Example #27
Source File: IteratorStreamFunctionBase3.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void checkBuild(ExprList args) {
    if (args.size() != 3) {
        throw new QueryBuildException("Selector '"
                + this.getClass().getName() + "' takes three arguments");
    }
}
 
Example #28
Source File: JenaUtil.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static Node invokeFunction3(Resource function, RDFNode argument1, RDFNode argument2, RDFNode argument3, Dataset dataset) {
	ExprList args = new ExprList();
	args.add(argument1 != null ? NodeValue.makeNode(argument1.asNode()) : new ExprVar("arg1"));
	args.add(argument2 != null ? NodeValue.makeNode(argument2.asNode()) : new ExprVar("arg2"));
	args.add(argument3 != null ? NodeValue.makeNode(argument3.asNode()) : new ExprVar("arg3"));
	return invokeFunction(function, args, dataset);
}
 
Example #29
Source File: CustomAggregate.java    From xcurator with Apache License 2.0 5 votes vote down vote up
/** Function called on each row in a group */
@Override
public void accumulate(Binding binding, FunctionEnv functionEnv) {
    ExprList exprList = agg.getExprList() ;
    for(Expr expr: exprList) {
        try {
            NodeValue nv = expr.eval(binding, functionEnv) ;
            // Evaluation succeeded.
            if ( nv.isLiteral())
                count ++ ;
        } catch (ExprEvalException ex) {}
    }
}
 
Example #30
Source File: ExpandPrefixFunction.java    From tarql with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public NodeValue exec(Binding binding, ExprList args, String uri, FunctionEnv env) {
	if (args == null) {
		throw new ARQInternalErrorException("ExpandPrefixFunction: Null args list");
	}
	if (args.size() != 1) {
		throw new ExprEvalException("ExpandPrefixFunction: Wrong number of arguments: Wanted 1, got " + args.size());
	}
       return exec(args.get(0).eval(binding, env), env.getContext());
}