Search in sources :

Example 66 with Var

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);
}
Also used : OpProject(org.apache.jena.sparql.algebra.op.OpProject) OpOrder(org.apache.jena.sparql.algebra.op.OpOrder) Var(org.apache.jena.sparql.core.Var) HashSet(java.util.HashSet)

Example 67 with Var

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);
}
Also used : Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node)

Example 68 with Var

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;
    }
}
Also used : Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node)

Example 69 with Var

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();
}
Also used : Var(org.apache.jena.sparql.core.Var) ARQInternalErrorException(org.apache.jena.sparql.ARQInternalErrorException)

Example 70 with Var

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;
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Random(java.util.Random) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList)

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