Search in sources :

Example 11 with ExprVar

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

the class TransformQuadGraph method transform.

@Override
public Op transform(OpGraph opGraph, Op op) {
    //System.err.println("transform(OpGraph)\n"+opGraph+op) ;
    // ?? Could just leave the (graph) in place always - just rewrite BGPs. 
    boolean noPattern = false;
    /* One case to consider is when the pattern for the GRAPH
         * statement includes uses the variable inside the GRAPH clause. 
         * In this case, we must rename away the inner variable
         * to allow stream execution via index joins, 
         * and then put back the value via an assign.
         * (This is what QueryIterGraph does using a streaming join
         * for triples)
         */
    // Note: op is already quads by this point.
    // Must test scoping by the subOp of GRAPH
    QuadSlot qSlot = tracker.peek();
    Node actualName = qSlot.actualGraphName;
    Node rewriteName = qSlot.rewriteGraphName;
    if (OpBGP.isBGP(op)) {
        // Empty BGP
        if (((OpBGP) op).getPattern().isEmpty())
            noPattern = true;
    } else if (op instanceof OpTable) {
        // Empty BGP compiled to a unit table
        if (((OpTable) op).isJoinIdentity())
            noPattern = true;
    }
    if (noPattern) {
        // which are ways of accessing the names in the dataset.
        return new OpDatasetNames(opGraph.getNode());
    }
    if (actualName != rewriteName)
        op = OpAssign.assign(op, Var.alloc(actualName), new ExprVar(rewriteName));
    // have been converted to quads.
    return op;
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) QuadSlot(org.apache.jena.sparql.algebra.AlgebraQuad.QuadSlot) Node(org.apache.jena.graph.Node)

Example 12 with ExprVar

use of org.apache.jena.sparql.expr.ExprVar 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 13 with ExprVar

use of org.apache.jena.sparql.expr.ExprVar 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)

Example 14 with ExprVar

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

the class TestUserFunctionsInSparql method setup.

@BeforeClass
public static void setup() {
    UserDefinedFunctionFactory.getFactory().clear();
    //Define a square function
    Expr square = new E_Multiply(new ExprVar("x"), new ExprVar("x"));
    UserDefinedFunctionFactory.getFactory().add("http://example/square", square, new ArrayList<>(square.getVarsMentioned()));
}
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) BeforeClass(org.junit.BeforeClass)

Example 15 with ExprVar

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

the class TestSortedDataBag method testSorting.

private void testSorting(int numBindings, int threshold) {
    List<Binding> unsorted = randomBindings(numBindings);
    List<SortCondition> conditions = new ArrayList<>();
    conditions.add(new SortCondition(new ExprVar("8"), Query.ORDER_ASCENDING));
    conditions.add(new SortCondition(new ExprVar("1"), Query.ORDER_ASCENDING));
    conditions.add(new SortCondition(new ExprVar("0"), Query.ORDER_DESCENDING));
    BindingComparator comparator = new BindingComparator(conditions);
    List<Binding> sorted = new ArrayList<>();
    SortedDataBag<Binding> db = new SortedDataBag<>(new ThresholdPolicyCount<Binding>(threshold), SerializationFactoryFinder.bindingSerializationFactory(), comparator);
    try {
        db.addAll(unsorted);
        Iterator<Binding> iter = db.iterator();
        while (iter.hasNext()) {
            sorted.add(iter.next());
        }
        Iter.close(iter);
    } finally {
        db.close();
    }
    Collections.sort(unsorted, comparator);
    assertEquals(unsorted, sorted);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) ExprVar(org.apache.jena.sparql.expr.ExprVar) SortCondition(org.apache.jena.query.SortCondition) BindingComparator(org.apache.jena.sparql.engine.binding.BindingComparator) SortedDataBag(org.apache.jena.atlas.data.SortedDataBag) ArrayList(java.util.ArrayList)

Aggregations

ExprVar (org.apache.jena.sparql.expr.ExprVar)20 Expr (org.apache.jena.sparql.expr.Expr)12 Var (org.apache.jena.sparql.core.Var)8 Test (org.junit.Test)7 Op (org.apache.jena.sparql.algebra.Op)5 ArrayList (java.util.ArrayList)4 Node (org.apache.jena.graph.Node)4 Triple (org.apache.jena.graph.Triple)4 E_Multiply (org.apache.jena.sparql.expr.E_Multiply)4 SortCondition (org.apache.jena.query.SortCondition)3 Binding (org.apache.jena.sparql.engine.binding.Binding)3 BindingComparator (org.apache.jena.sparql.engine.binding.BindingComparator)3 E_Regex (org.apache.jena.sparql.expr.E_Regex)3 SortedDataBag (org.apache.jena.atlas.data.SortedDataBag)2 InternalErrorException (org.apache.jena.atlas.lib.InternalErrorException)2 OpBGP (org.apache.jena.sparql.algebra.op.OpBGP)2 BasicPattern (org.apache.jena.sparql.core.BasicPattern)2 VarExprList (org.apache.jena.sparql.core.VarExprList)2 NodeValueInteger (org.apache.jena.sparql.expr.nodevalue.NodeValueInteger)2 ElementFilter (org.apache.jena.sparql.syntax.ElementFilter)2