use of org.apache.jena.sparql.core.Var in project jena by apache.
the class TransformDistinctToReduced method transform1.
// Best is this is after TransformTopN but they are order independent
// TopN of "reduced or distinct of order" is handled.
//@Override
public Op transform1(OpDistinct opDistinct, Op subOp) {
if (subOp instanceof OpProject) {
OpProject opProject = (OpProject) subOp;
if (opProject.getSubOp() instanceof OpOrder) {
OpOrder opOrder = (OpOrder) opProject.getSubOp();
Set<Var> projectVars = new HashSet<>(opProject.getVars());
if (isSafe(projectVars, opOrder)) {
return OpReduced.create(subOp);
}
}
}
return super.transform(opDistinct, subOp);
}
use of org.apache.jena.sparql.core.Var in project jena by apache.
the class ExprVar method copySubstitute.
@Override
public Expr copySubstitute(Binding binding) {
Var v = varNode;
if (binding == null || !binding.contains(v))
return new ExprVar(v);
Node v2 = binding.get(v);
return v2.isVariable() ? new ExprVar(v2) : eval(binding, null);
}
use of org.apache.jena.sparql.core.Var in project jena by apache.
the class ExprLib method evalOrElse.
private static NodeValue evalOrElse(Expr expr, Binding binding, FunctionEnv functionEnv, NodeValue exceptionValue) {
if (expr.isConstant())
// Easy case.
return expr.getConstant();
if (expr.isVariable()) {
// The case of the expr being a single variable.
Var v = expr.asVar();
Node n = binding.get(v);
if (n == null)
return exceptionValue;
NodeValue nv = NodeValue.makeNode(n);
return nv;
}
try {
return expr.eval(binding, functionEnv);
} catch (ExprEvalException ex) {
return exceptionValue;
}
}
use of org.apache.jena.sparql.core.Var in project jena by apache.
the class ExprLib method isAssignmentSafeEquality.
/**
* @param graphHasStringEquality True if the graph triple matching equates xsd:string and plain literal
* @param graphHasNumercialValueEquality True if the graph triple matching equates numeric values
*/
public static boolean isAssignmentSafeEquality(Expr expr, boolean graphHasStringEquality, boolean graphHasNumercialValueEquality) {
if (!(expr instanceof E_Equals) && !(expr instanceof E_SameTerm))
return false;
// Corner case: sameTerm is false for string/plain literal,
// but true in the graph.
ExprFunction2 eq = (ExprFunction2) expr;
Expr left = eq.getArg1();
Expr right = eq.getArg2();
Var var = null;
NodeValue constant = null;
if (left.isVariable() && right.isConstant()) {
var = left.asVar();
constant = right.getConstant();
} else if (right.isVariable() && left.isConstant()) {
var = right.asVar();
constant = left.getConstant();
}
// Not between a variable and a constant
if (var == null || constant == null)
return false;
if (!constant.isLiteral())
// URIs, bNodes. Any bNode will have come from a substitution - not legal syntax in filters
return true;
if (expr instanceof E_SameTerm) {
if (graphHasStringEquality && constant.isString())
// Graph is not same term
return false;
if (graphHasNumercialValueEquality && constant.isNumber())
return false;
return true;
}
// Final check for "=" where a FILTER = can do value matching when the graph does not.
if (expr instanceof E_Equals) {
if (!graphHasStringEquality && constant.isString())
return false;
if (!graphHasNumercialValueEquality && constant.isNumber())
return false;
return true;
}
// Unreachable.
throw new ARQInternalErrorException();
}
use of org.apache.jena.sparql.core.Var in project jena by apache.
the class TestSortedDataBag method randomBindings.
private List<Binding> randomBindings(int numBindings) {
random = new Random();
Var[] vars = new Var[] { Var.alloc("1"), Var.alloc("2"), Var.alloc("3"), Var.alloc("4"), Var.alloc("5"), Var.alloc("6"), Var.alloc("7"), Var.alloc("8"), Var.alloc("9"), Var.alloc("0") };
List<Binding> toReturn = new ArrayList<>();
for (int i = 0; i < numBindings; i++) {
toReturn.add(randomBinding(vars));
}
return toReturn;
}
Aggregations