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;
}
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;
}
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;
}
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;
}
}
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);
}
Aggregations