Search in sources :

Example 61 with Expr

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

the class TransformFilterDisjunction method explodeDisjunction.

/** Explode a expr into a list of disjunctions */
private static List<Expr> explodeDisjunction(List<Expr> exprList, Expr expr) {
    if (!(expr instanceof E_LogicalOr)) {
        exprList.add(expr);
        return exprList;
    }
    E_LogicalOr exprOr = (E_LogicalOr) expr;
    Expr e1 = exprOr.getArg1();
    Expr e2 = exprOr.getArg2();
    explodeDisjunction(exprList, e1);
    explodeDisjunction(exprList, e2);
    return exprList;
}
Also used : Expr(org.apache.jena.sparql.expr.Expr) E_LogicalOr(org.apache.jena.sparql.expr.E_LogicalOr)

Example 62 with Expr

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

the class TransformFilterDisjunction method expandDisjunction.

// Todo:
// 1 - convert TransformEqualityFilter to use ExprLib for testing.
// 2 - Scan for safe equality filters in disjunction.
public static Op expandDisjunction(Expr expr, Op subOp) {
    //        if ( !( expr instanceof E_LogicalOr ) )
    //            return null ;
    List<Expr> exprList = explodeDisjunction(new ArrayList<Expr>(), expr);
    // All disjunctions - some can be done efficiently via assignments, some can not.
    // Really should only do if every disjunction can turned into a assign-grounded pattern
    // otherwise the full is done anyway. 
    List<Expr> exprList2 = null;
    Op op = null;
    for (Expr e : exprList) {
        Op op2 = TransformFilterEquality.processFilter(e, subOp);
        if (op2 == null) {
            // Not done.
            if (exprList2 == null)
                exprList2 = new ArrayList<>();
            exprList2.add(e);
        //continue ;
        // Can't do one so don't do any as the original pattern is still executed. 
        }
        op = OpDisjunction.create(op, op2);
    }
    if (exprList2 != null && !exprList2.isEmpty()) {
        // These are left as disjunctions.
        Expr eOther = null;
        for (Expr e : exprList2) {
            if (eOther == null)
                eOther = e;
            else
                eOther = new E_LogicalOr(eOther, e);
        }
        Op opOther = OpFilter.filter(eOther, subOp);
        op = OpDisjunction.create(op, opOther);
    }
    return op;
}
Also used : Op(org.apache.jena.sparql.algebra.Op) Expr(org.apache.jena.sparql.expr.Expr) ArrayList(java.util.ArrayList) E_LogicalOr(org.apache.jena.sparql.expr.E_LogicalOr)

Example 63 with Expr

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

the class SyntaxVarScope method checkExprVarUse.

private static void checkExprVarUse(Query query) {
    if (query.hasGroupBy()) {
        VarExprList groupKey = query.getGroupBy();
        // Copy - we need to add variables
        // SELECT (count(*) AS ?C)  (?C+1 as ?D) 
        List<Var> inScopeVars = new ArrayList<>(groupKey.getVars());
        VarExprList exprList = query.getProject();
        for (Var v : exprList.getVars()) {
            // In scope?
            Expr e = exprList.getExpr(v);
            if (e == null) {
                if (!inScopeVars.contains(v)) {
                    throw new QueryParseException("Non-group key variable in SELECT: " + v, -1, -1);
                }
            } else {
                Set<Var> eVars = e.getVarsMentioned();
                for (Var v2 : eVars) {
                    if (!inScopeVars.contains(v2)) {
                        throw new QueryParseException("Non-group key variable in SELECT: " + v2 + " in expression " + e, -1, -1);
                    }
                }
            }
            inScopeVars.add(v);
        }
    }
}
Also used : Expr(org.apache.jena.sparql.expr.Expr) Var(org.apache.jena.sparql.core.Var) VarExprList(org.apache.jena.sparql.core.VarExprList) QueryParseException(org.apache.jena.query.QueryParseException)

Example 64 with Expr

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

the class TestUserDefinedFunctionFactory method test_user_defined_function_factory_add_02.

@Test
public void test_user_defined_function_factory_add_02() {
    Expr e1 = new E_Multiply(new ExprVar("x"), new ExprVar("x"));
    Expr e2 = new E_Multiply(new ExprVar("y"), new ExprVar("y"));
    UserDefinedFunctionFactory.getFactory().add("http://example/square", e1, new ArrayList<>(e1.getVarsMentioned()));
    Assert.assertTrue(UserDefinedFunctionFactory.getFactory().isRegistered("http://example/square"));
    Assert.assertEquals(e1, UserDefinedFunctionFactory.getFactory().get("http://example/square").getBaseExpr());
    UserDefinedFunctionFactory.getFactory().add("http://example/square", e2, new ArrayList<>(e2.getVarsMentioned()));
    Assert.assertTrue(UserDefinedFunctionFactory.getFactory().isRegistered("http://example/square"));
    Assert.assertEquals(e2, UserDefinedFunctionFactory.getFactory().get("http://example/square").getBaseExpr());
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) Expr(org.apache.jena.sparql.expr.Expr) E_Multiply(org.apache.jena.sparql.expr.E_Multiply) Test(org.junit.Test)

Example 65 with Expr

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

the class TestUserDefinedFunctionFactory method test_user_defined_function_factory_add_01.

@Test
public void test_user_defined_function_factory_add_01() {
    Expr e = new E_Multiply(new ExprVar("x"), new ExprVar("x"));
    UserDefinedFunctionFactory.getFactory().add("http://example/square", e, new ArrayList<>(e.getVarsMentioned()));
    Assert.assertTrue(UserDefinedFunctionFactory.getFactory().isRegistered("http://example/square"));
    Assert.assertEquals(e, UserDefinedFunctionFactory.getFactory().get("http://example/square").getBaseExpr());
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) Expr(org.apache.jena.sparql.expr.Expr) E_Multiply(org.apache.jena.sparql.expr.E_Multiply) Test(org.junit.Test)

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