Search in sources :

Example 76 with Expr

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

the class TransformFilterPlacementConservative method insertAnyFilter.

// ---- Utilities
/** For any expression now in scope, wrap the op with a filter */
private static Op insertAnyFilter(ExprList exprs, Set<Var> patternVarsScope, Op op) {
    for (Iterator<Expr> iter = exprs.iterator(); iter.hasNext(); ) {
        Expr expr = iter.next();
        // Cache
        Set<Var> exprVars = expr.getVarsMentioned();
        if (patternVarsScope.containsAll(exprVars)) {
            if (op == null)
                op = OpTable.unit();
            op = OpFilter.filter(expr, op);
            iter.remove();
        }
    }
    return op;
}
Also used : Expr(org.apache.jena.sparql.expr.Expr) Var(org.apache.jena.sparql.core.Var)

Example 77 with Expr

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

the class TransformFilterPlacement method processExtendAssign.

/** Try to optimize (filter (extend ...)) , (filter (let ...)) */
private Placement processExtendAssign(ExprList exprs, OpExtendAssign input) {
    // We assume that each (extend) and (assign) is usually in simple form -
    // always one assignment. We cope with the general form (multiple
    // assignments) but do not attempt reordering of assignments.
    // There are three cases:
    // 1 - expressions that can be pushed into the subop.
    // 2 - expressions that are covered when the extend/assign has applied. [wrapping]
    // 3 - expressions that are not covered even at the outermost level. [unplaced]
    List<Var> vars1 = input.getVarExprList().getVars();
    Op subOp = input.getSubOp();
    // Case 1 : Do as much inner placement as possible.
    ExprList remaining = exprs;
    Placement p = transform(exprs, input.getSubOp());
    if (isChange(p)) {
        subOp = p.op;
        remaining = p.unplaced;
    }
    // Case 2 : wrapping
    // Case 3 : unplaced
    // Variables in subop and introduced by (extend)/(assign)
    Set<Var> subVars = OpVars.fixedVars(subOp);
    subVars.addAll(input.getVarExprList().getVars());
    ExprList wrapping = new ExprList();
    ExprList unplaced = new ExprList();
    for (Expr expr : remaining) {
        Set<Var> exprVars = expr.getVarsMentioned();
        if (subVars.containsAll(exprVars))
            wrapping.add(expr);
        else
            unplaced.add(expr);
    }
    Op result = input.copy(subOp);
    if (!wrapping.isEmpty())
        result = OpFilter.filterBy(wrapping, result);
    return result(result, unplaced);
}
Also used : Op(org.apache.jena.sparql.algebra.Op) Expr(org.apache.jena.sparql.expr.Expr) ExprList(org.apache.jena.sparql.expr.ExprList) Var(org.apache.jena.sparql.core.Var)

Example 78 with Expr

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

the class TransformFilterPlacement method placeDisjunction.

private Placement placeDisjunction(ExprList exprs, OpDisjunction input) {
    if (false) {
        // Push everything, always
        // Left as a safty fall back.
        List<Op> x = new ArrayList<>();
        input.getElements().forEach(op -> {
            Placement p = transform(exprs, op);
            if (isNoChange(p)) {
                x.add(buildFilter(exprs, op));
            } else {
                Op op1 = buildFilter(p);
                x.add(op1);
            }
        });
        return result(input.copy(x), emptyList);
    }
    // Don't push any expressions that aren't used in any of the arms of the disjunction.
    // This is more about being tidy.
    List<Expr> unplaced = new ArrayList<>(exprs.getList());
    //List<Placement> x = input.getElements().stream().map(op->transform(exprs, op)).collect(Collectors.toList()) ;
    List<Placement> placements = new ArrayList<>(exprs.size());
    Boolean someChange = Boolean.FALSE;
    for (Op op : input.getElements()) {
        Placement p = transform(exprs, op);
        if (isChange(p)) {
            unplaced.retainAll(p.unplaced.getList());
            someChange = Boolean.TRUE;
        } else
            p = result(op, exprs);
        placements.add(p);
    }
    ;
    if (!someChange)
        return noChangePlacement;
    List<Expr> retained = new ArrayList<>(exprs.getList());
    retained.removeAll(unplaced);
    // Mutate placements to remove the expres going outside.
    List<Op> ops = new ArrayList<>(input.size());
    for (Placement p : placements) {
        // No "noChange" at this point.
        p.unplaced.getListRaw().removeAll(unplaced);
        ops.add(buildFilter(p));
    }
    ;
    return result(input.copy(ops), new ExprList(unplaced));
}
Also used : Op(org.apache.jena.sparql.algebra.Op) Expr(org.apache.jena.sparql.expr.Expr) ExprList(org.apache.jena.sparql.expr.ExprList)

Example 79 with Expr

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

the class TransformFilterPlacement method wrapQuadPattern.

/** Wrap the Graph node, Basic Pattern with any applicable expressions from the ExprList
     *  but do not break up the BasicPattern in any way.
     */
private static Placement wrapQuadPattern(ExprList exprsIn, Node graphNode, BasicPattern pattern) {
    Set<Var> vs = new HashSet<>();
    VarUtils.addVars(vs, pattern);
    if (Var.isVar(graphNode))
        vs.add(Var.alloc(graphNode));
    ExprList pushed = new ExprList();
    ExprList unpushed = new ExprList();
    for (Expr e : exprsIn) {
        Set<Var> eVars = e.getVarsMentioned();
        if (vs.containsAll(eVars)) {
            pushed.add(e);
        } else {
            unpushed.add(e);
        }
    }
    // Can't push anything into a filter around this quadpattern
    if (pushed.size() == 0)
        return null;
    // Safe to place some conditions around the quadpattern
    return new Placement(OpFilter.filterBy(pushed, new OpQuadPattern(graphNode, pattern)), unpushed);
}
Also used : Expr(org.apache.jena.sparql.expr.Expr) ExprList(org.apache.jena.sparql.expr.ExprList) Var(org.apache.jena.sparql.core.Var)

Example 80 with Expr

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

the class VariableUsageVisitor method visit.

@Override
public void visit(OpFilter opFilter) {
    Collection<Var> vars = new ArrayList<>();
    for (Expr expr : opFilter.getExprs().getList()) {
        ExprVars.varsMentioned(vars, expr);
    }
    action(vars);
}
Also used : Expr(org.apache.jena.sparql.expr.Expr) Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList)

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