Search in sources :

Example 96 with Op

use of org.apache.jena.sparql.algebra.Op in project jena by apache.

the class TestSemanticEquivalence method test.

/**
     * Tests whether a query gives the same results when run both with and
     * without a given optimizer
     * 
     * @param queryStr
     *            Query
     * @param ds
     *            Dataset
     * @param opt
     *            Optimizer
     * @param expected
     *            Expected number of results
     */
public static void test(String queryStr, Dataset ds, Symbol opt, int expected) {
    Query q = QueryFactory.create(queryStr);
    if (!q.isSelectType())
        Assert.fail("Only SELECT queries are testable with this method");
    Op op = Algebra.compile(q);
    // Track current state
    boolean isEnabled = ARQ.isTrue(opt);
    boolean isDisabled = ARQ.isFalse(opt);
    try {
        // Run first without optimization
        ARQ.set(opt, false);
        ResultSetRewindable rs;
        try (QueryExecution qe = QueryExecutionFactory.create(q, ds)) {
            rs = ResultSetFactory.makeRewindable(qe.execSelect());
            if (expected != rs.size()) {
                System.err.println("Non-optimized results not as expected");
                TextOutput output = new TextOutput((SerializationContext) null);
                output.format(System.out, rs);
                rs.reset();
            }
            Assert.assertEquals(expected, rs.size());
        }
        // Run with optimization
        ARQ.set(opt, true);
        ResultSetRewindable rsOpt;
        try (QueryExecution qeOpt = QueryExecutionFactory.create(q, ds)) {
            rsOpt = ResultSetFactory.makeRewindable(qeOpt.execSelect());
            if (expected != rsOpt.size()) {
                System.err.println("Optimized results not as expected");
                TextOutput output = new TextOutput((SerializationContext) null);
                output.format(System.out, rsOpt);
                rsOpt.reset();
            }
            Assert.assertEquals(expected, rsOpt.size());
        }
        Assert.assertTrue(ResultSetCompare.isomorphic(rs, rsOpt));
    } finally {
        // Restore previous state
        if (isEnabled) {
            ARQ.set(opt, true);
        } else if (isDisabled) {
            ARQ.set(opt, false);
        } else {
            ARQ.unset(opt);
        }
    }
}
Also used : TextOutput(org.apache.jena.sparql.resultset.TextOutput) Op(org.apache.jena.sparql.algebra.Op)

Example 97 with Op

use of org.apache.jena.sparql.algebra.Op in project jena by apache.

the class AbstractTestTransform method testOp.

public void testOp(String input, Transform transform, String... output) {
    Op op1 = SSE.parseOp(input);
    test(op1, transform, output);
}
Also used : Op(org.apache.jena.sparql.algebra.Op)

Example 98 with Op

use of org.apache.jena.sparql.algebra.Op in project jena by apache.

the class AbstractTestTransform method testQuery.

public void testQuery(String input, Transform transform, String... output) {
    Query q = QueryFactory.create(input);
    Op op = Algebra.compile(q);
    test(op, transform, output);
}
Also used : Op(org.apache.jena.sparql.algebra.Op) Query(org.apache.jena.query.Query)

Example 99 with Op

use of org.apache.jena.sparql.algebra.Op in project jena by apache.

the class TestOptimizer method combine_extend_03.

@Test
public void combine_extend_03() {
    // Technically illegal SPARQL here but useful to validate that the optimizer doesn't do the wrong thing
    Op extend = OpExtend.create(OpTable.unit(), new VarExprList(Var.alloc("x"), new NodeValueInteger(1)));
    extend = OpExtend.create(extend, new VarExprList(Var.alloc("x"), new NodeValueInteger(2)));
    String opExpectedString = StrUtils.strjoinNL("(extend ((?x 2))", "  (extend ((?x 1))", "    (table unit)))");
    check(extend, new TransformExtendCombine(), opExpectedString);
}
Also used : Op(org.apache.jena.sparql.algebra.Op) NodeValueInteger(org.apache.jena.sparql.expr.nodevalue.NodeValueInteger) VarExprList(org.apache.jena.sparql.core.VarExprList) Test(org.junit.Test)

Example 100 with Op

use of org.apache.jena.sparql.algebra.Op in project jena by apache.

the class TestOptimizer method combine_assign_02.

@Test
public void combine_assign_02() {
    Op assign = OpAssign.create(OpTable.unit(), new VarExprList(Var.alloc("x"), new NodeValueInteger(1)));
    assign = OpAssign.create(assign, new VarExprList(Var.alloc("y"), new ExprVar("x")));
    String opExpectedString = StrUtils.strjoinNL("(assign ((?x 1) (?y ?x))", "  (table unit))");
    check(assign, new TransformExtendCombine(), opExpectedString);
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) Op(org.apache.jena.sparql.algebra.Op) NodeValueInteger(org.apache.jena.sparql.expr.nodevalue.NodeValueInteger) VarExprList(org.apache.jena.sparql.core.VarExprList) Test(org.junit.Test)

Aggregations

Op (org.apache.jena.sparql.algebra.Op)166 Var (org.apache.jena.sparql.core.Var)29 VarExprList (org.apache.jena.sparql.core.VarExprList)18 Query (org.apache.jena.query.Query)17 ExprList (org.apache.jena.sparql.expr.ExprList)17 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)15 Test (org.junit.Test)14 Triple (org.apache.jena.graph.Triple)13 Expr (org.apache.jena.sparql.expr.Expr)13 BasicPattern (org.apache.jena.sparql.core.BasicPattern)11 ArrayList (java.util.ArrayList)9 Node (org.apache.jena.graph.Node)8 OpBGP (org.apache.jena.sparql.algebra.op.OpBGP)7 NodeValueInteger (org.apache.jena.sparql.expr.nodevalue.NodeValueInteger)7 Binding (org.apache.jena.sparql.engine.binding.Binding)5 ExprVar (org.apache.jena.sparql.expr.ExprVar)5 BaseTest (org.apache.jena.atlas.junit.BaseTest)3 Pair (org.apache.jena.atlas.lib.Pair)3 Table (org.apache.jena.sparql.algebra.Table)3 TransformCopy (org.apache.jena.sparql.algebra.TransformCopy)3