Search in sources :

Example 21 with Expr

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

the class NodeTransformLib method transformVars.

/** Apply the NodeTransform to the variables of a VarExprList. */
public static VarExprList transformVars(NodeTransform transform, VarExprList varExprList) {
    VarExprList varExprList2 = new VarExprList();
    boolean changed = false;
    // Just variables.
    for (Var v : varExprList.getVars()) {
        Expr expr = varExprList.getExpr(v);
        Var v2 = (Var) transform.apply(v);
        if (!Objects.equals(v, v2))
            changed = true;
        varExprList2.add(v2, expr);
    }
    if (!changed)
        return varExprList;
    return varExprList2;
}
Also used : Expr(org.apache.jena.sparql.expr.Expr)

Example 22 with Expr

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

the class NodeTransformLib method transform.

public static ExprList transform(NodeTransform nodeTransform, ExprList exprList) {
    ExprList exprList2 = new ExprList();
    boolean changed = false;
    for (Expr expr : exprList) {
        Expr expr2 = transform(nodeTransform, expr);
        if (expr != expr2)
            changed = true;
        exprList2.add(expr2);
    }
    if (!changed)
        return exprList;
    return exprList2;
}
Also used : Expr(org.apache.jena.sparql.expr.Expr) ExprList(org.apache.jena.sparql.expr.ExprList)

Example 23 with Expr

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

the class SyntaxVarScope method fmtExprList.

private static String fmtExprList(VarExprList exprList) {
    StringBuilder sb = new StringBuilder();
    boolean first = true;
    for (Var v : exprList.getVars()) {
        Expr e = exprList.getExpr(v);
        if (!first) {
            sb.append(" ");
        }
        first = false;
        sb.append("(").append(e).append(" AS ").append(v).append(")");
    }
    return sb.toString();
}
Also used : Expr(org.apache.jena.sparql.expr.Expr) Var(org.apache.jena.sparql.core.Var)

Example 24 with Expr

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

the class QueryTransformOps method transformVarExprList.

// ** Mutates the VarExprList
private static void transformVarExprList(VarExprList varExprList, ExprTransform exprTransform) {
    Map<Var, Expr> map = varExprList.getExprs();
    for (Var v : varExprList.getVars()) {
        Expr e = varExprList.getExpr(v);
        ExprVar ev = new ExprVar(v);
        Expr ev2 = exprTransform.transform(ev);
        if (ev != ev2) {
            if (e != null)
                throw new ARQException("Can't substitute " + v + " because it's used as an AS variable");
            if (ev2.isConstant() || ev2.isVariable()) {
                // Convert to (substitute value AS ?var)
                map.put(v, ev2);
                continue;
            } else
                throw new ARQException("Can't substitute " + v + " because it's not a simple value: " + ev2);
        }
        if (e == null)
            continue;
        // Didn't change the variable.
        Expr e2 = ExprTransformer.transform(exprTransform, e);
        if (e != e2)
            // replace
            map.put(v, e2);
    }
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) Expr(org.apache.jena.sparql.expr.Expr) ARQException(org.apache.jena.sparql.ARQException) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var)

Example 25 with Expr

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

the class TransformElementLib method apply.

public static Node apply(Node n, ExprTransform exprTransform) {
    if (exprTransform == null)
        return n;
    Expr e = null;
    if (Var.isVar(n)) {
        Var v = Var.alloc(n);
        ExprVar expr = new ExprVar(v);
        e = exprTransform.transform(expr);
    } else {
        NodeValue nv = NodeValue.makeNode(n);
        e = exprTransform.transform(nv);
    }
    if (e instanceof ExprVar)
        return ((ExprVar) e).asVar();
    if (e instanceof NodeValue)
        return ((NodeValue) e).asNode();
    throw new InternalErrorException("Managed to turn a node " + n + " into " + e);
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) NodeValue(org.apache.jena.sparql.expr.NodeValue) Expr(org.apache.jena.sparql.expr.Expr) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) InternalErrorException(org.apache.jena.atlas.lib.InternalErrorException)

Aggregations

Expr (org.apache.jena.sparql.expr.Expr)82 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)5 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 ExprAggregator (org.apache.jena.sparql.expr.ExprAggregator)3 InternalErrorException (org.apache.jena.atlas.lib.InternalErrorException)2 Query (org.apache.jena.query.Query)2