Search in sources :

Example 61 with Var

use of org.apache.jena.sparql.core.Var in project jena by apache.

the class OpAssign method assign.

/** Create an v or add to an existing one.
     * This operation collapses what would otherwise be stacks
     * of OpAssign.
     */
public static Op assign(Op op, VarExprList exprs) {
    if (!(op instanceof OpAssign))
        return create(op, exprs);
    OpAssign opAssign = (OpAssign) op;
    for (Var var : exprs.getVars()) {
        if (opAssign.assignments.contains(var))
            return create(op, exprs);
    }
    opAssign.assignments.addAll(exprs);
    return opAssign;
}
Also used : Var(org.apache.jena.sparql.core.Var)

Example 62 with Var

use of org.apache.jena.sparql.core.Var in project jena by apache.

the class OpExtend method extend.

/** Create an OpExtend or add to an existing one.
     * This operation collapses what would otherwise be stacks
     * of OpExtend.
     */
public static Op extend(Op op, VarExprList exprs) {
    if (!(op instanceof OpExtend))
        return create(op, exprs);
    OpExtend opExtend = (OpExtend) op;
    for (Var var : exprs.getVars()) {
        if (opExtend.assignments.contains(var))
            return create(op, exprs);
    }
    opExtend.assignments.addAll(exprs);
    return opExtend;
}
Also used : Var(org.apache.jena.sparql.core.Var)

Example 63 with Var

use of org.apache.jena.sparql.core.Var in project jena by apache.

the class TransformEliminateAssignments method processVarExprList.

private VarExprList processVarExprList(VarExprList exprs, ExprTransform transform) {
    VarExprList newExprs = new VarExprList();
    for (Var v : exprs.getVars()) {
        Expr e = exprs.getExpr(v);
        Expr e2 = ExprTransformer.transform(transform, e);
        newExprs.add(v, e2);
    }
    return newExprs;
}
Also used : Var(org.apache.jena.sparql.core.Var) VarExprList(org.apache.jena.sparql.core.VarExprList)

Example 64 with Var

use of org.apache.jena.sparql.core.Var in project jena by apache.

the class TransformEliminateAssignments method transform.

@Override
public Op transform(OpExtend opExtend, Op subOp) {
    // the assigned value is unnecessary or only used once
    if (!this.tracker.insideProjection())
        return super.transform(opExtend, subOp);
    // Track the assignments for future reference
    this.tracker.putAssignments(opExtend.getVarExprList());
    // Eliminate and inline assignments
    VarExprList unusedAssignments = processUnused(opExtend.getVarExprList());
    VarExprList newAssignments = new VarExprList();
    for (Var assignVar : opExtend.getVarExprList().getVars()) {
        // If unused eliminate
        if (unusedAssignments != null && unusedAssignments.contains(assignVar))
            continue;
        Expr currExpr = opExtend.getVarExprList().getExpr(assignVar);
        // See what vars are used in the current expression
        Set<Var> vars = new HashSet<>();
        ExprVars.nonOpVarsMentioned(vars, currExpr);
        // See if we can inline anything
        for (Var var : vars) {
            // Usage count will be 2 if we can eliminate the assignment
            // First usage is when it is introduced by the assignment and
            // the second is when it is used now used in another assignment
            Expr e = getAssignExpr(var);
            if (this.tracker.getUsageCount(var) == 2 && hasAssignment(var) && canInline(e)) {
                // Can go back and eliminate that assignment
                subOp = eliminateAssignment(subOp, var);
                // Replace the variable usage with the expression within
                // expression
                currExpr = ExprTransformer.transform(new ExprTransformSubstitute(var, e), currExpr);
                this.tracker.getAssignments().remove(var);
                // Need to update any assignments we may be tracking that
                // refer to the variable we just inlined
                this.tracker.updateAssignments(var, e);
                // VarExprList we are currently building
                if (newAssignments.contains(var) && newAssignments.getExpr(var).equals(e)) {
                    newAssignments.getVars().remove(var);
                    newAssignments.getExprs().remove(var);
                }
            }
        }
        newAssignments.add(assignVar, currExpr);
    }
    // May be able to eliminate the extend entirely in some cases
    if (newAssignments.size() > 0) {
        return OpExtend.create(subOp, newAssignments);
    } else {
        return subOp;
    }
}
Also used : Var(org.apache.jena.sparql.core.Var) VarExprList(org.apache.jena.sparql.core.VarExprList)

Example 65 with Var

use of org.apache.jena.sparql.core.Var in project jena by apache.

the class TransformEliminateAssignments method transform.

@Override
public Op transform(OpTopN opTop, Op subOp) {
    if (!this.isApplicable())
        return super.transform(opTop, subOp);
    // See what vars are used in the sort conditions
    Collection<Var> vars = new ArrayList<>();
    for (SortCondition cond : opTop.getConditions()) {
        ExprVars.varsMentioned(vars, cond.getExpression());
    }
    // Are any of these vars single usage?
    List<SortCondition> conditions = null;
    for (Var var : vars) {
        // Usage count will be 2 if we can eliminate the assignment
        // First usage is when it is introduced by the assignment and the
        // second is when it is used now in this filter
        Expr e = getAssignExpr(var);
        if (this.tracker.getUsageCount(var) == 2 && hasAssignment(var) && canInline(e) && shouldInline(e)) {
            // Can go back and eliminate that assignment
            subOp = eliminateAssignment(subOp, var);
            // Replace the variable usage with the expression within the
            // sort conditions
            conditions = processConditions(opTop.getConditions(), conditions, var);
            this.tracker.getAssignments().remove(var);
        }
    }
    // Create a new order if we've substituted any expressions
    if (conditions != null) {
        return new OpTopN(subOp, opTop.getLimit(), conditions);
    }
    return super.transform(opTop, subOp);
}
Also used : SortCondition(org.apache.jena.query.SortCondition) Var(org.apache.jena.sparql.core.Var)

Aggregations

Var (org.apache.jena.sparql.core.Var)264 Node (org.apache.jena.graph.Node)83 ArrayList (java.util.ArrayList)53 Test (org.junit.Test)47 Binding (org.apache.jena.sparql.engine.binding.Binding)33 VarExprList (org.apache.jena.sparql.core.VarExprList)30 Op (org.apache.jena.sparql.algebra.Op)29 Expr (org.apache.jena.sparql.expr.Expr)28 Triple (org.apache.jena.graph.Triple)17 HashMap (java.util.HashMap)15 ContractTest (org.xenei.junit.contract.ContractTest)13 BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)12 Query (org.apache.jena.query.Query)11 ExprList (org.apache.jena.sparql.expr.ExprList)11 SortCondition (org.apache.jena.query.SortCondition)10 ExprVar (org.apache.jena.sparql.expr.ExprVar)10 HashSet (java.util.HashSet)9 Pair (org.apache.jena.atlas.lib.Pair)9 SqlColumn (org.apache.jena.sdb.core.sqlexpr.SqlColumn)9 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)9