Search in sources :

Example 31 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) {
    // ?? 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 32 with ExprVar

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

the class TransformElementLib method applyVar.

public static Var applyVar(Var v, ExprTransform exprTransform) {
    if (exprTransform == null)
        return v;
    ExprVar expr = new ExprVar(v);
    Expr e = exprTransform.transform(expr);
    if (e instanceof ExprVar)
        return ((ExprVar) e).asVar();
    throw new InternalErrorException("Managed to turn a variable " + v + " into " + e);
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) Expr(org.apache.jena.sparql.expr.Expr) InternalErrorException(org.apache.jena.atlas.lib.InternalErrorException)

Example 33 with ExprVar

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

the class TestQueryIterSort method setup.

@Before
public void setup() {
    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") };
    unsorted = new ArrayList<>();
    for (int i = 0; i < 500; i++) {
        unsorted.add(randomBinding(vars));
    }
    List<SortCondition> conditions = new ArrayList<>();
    conditions.add(new SortCondition(new ExprVar("8"), Query.ORDER_ASCENDING));
    comparator = new BindingComparator(conditions);
    iterator = new CallbackIterator(unsorted.iterator(), 25, null);
    iterator.setCallback(new Callback() {

        @Override
        public void call() {
            throw new QueryCancelledException();
        }
    });
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) SortCondition(org.apache.jena.query.SortCondition) BindingComparator(org.apache.jena.sparql.engine.binding.BindingComparator) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList) Random(java.util.Random) QueryCancelledException(org.apache.jena.query.QueryCancelledException) Before(org.junit.Before)

Example 34 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 : ExprVar(org.apache.jena.sparql.expr.ExprVar) SortCondition(org.apache.jena.query.SortCondition)

Example 35 with ExprVar

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

the class TestSortedDataBag method testTemporaryFilesAreCleanedUpAfterCompletion.

@Test
public void testTemporaryFilesAreCleanedUpAfterCompletion() {
    List<Binding> unsorted = randomBindings(500);
    List<SortCondition> conditions = new ArrayList<>();
    conditions.add(new SortCondition(new ExprVar("8"), Query.ORDER_ASCENDING));
    BindingComparator comparator = new BindingComparator(conditions);
    SortedDataBag<Binding> db = new SortedDataBag<>(new ThresholdPolicyCount<Binding>(10), SerializationFactoryFinder.bindingSerializationFactory(), comparator);
    List<File> spillFiles = new ArrayList<>();
    try {
        db.addAll(unsorted);
        spillFiles.addAll(db.getSpillFiles());
        int count = 0;
        for (File file : spillFiles) {
            if (file.exists()) {
                count++;
            }
        }
        // 500 bindings divided into 50 chunks (49 in files, and 1 in memory)
        assertEquals(49, count);
        Iterator<Binding> iter = db.iterator();
        while (iter.hasNext()) {
            iter.next();
        }
        Iter.close(iter);
    } finally {
        db.close();
    }
    int count = 0;
    for (File file : spillFiles) {
        if (file.exists()) {
            count++;
        }
    }
    assertEquals(0, count);
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) SortCondition(org.apache.jena.query.SortCondition) File(java.io.File) Test(org.junit.Test)

Aggregations

ExprVar (org.apache.jena.sparql.expr.ExprVar)51 Expr (org.apache.jena.sparql.expr.Expr)34 Test (org.junit.Test)17 ContractTest (org.xenei.junit.contract.ContractTest)15 Var (org.apache.jena.sparql.core.Var)12 SortCondition (org.apache.jena.query.SortCondition)11 Node (org.apache.jena.graph.Node)9 Op (org.apache.jena.sparql.algebra.Op)7 Triple (org.apache.jena.graph.Triple)6 Query (org.apache.jena.query.Query)6 E_LessThan (org.apache.jena.sparql.expr.E_LessThan)5 E_Random (org.apache.jena.sparql.expr.E_Random)5 NodeValueInteger (org.apache.jena.sparql.expr.nodevalue.NodeValueInteger)5 OpBGP (org.apache.jena.sparql.algebra.op.OpBGP)4 BasicPattern (org.apache.jena.sparql.core.BasicPattern)4 E_Multiply (org.apache.jena.sparql.expr.E_Multiply)4 ElementFilter (org.apache.jena.sparql.syntax.ElementFilter)4 ArrayList (java.util.ArrayList)3 E_Regex (org.apache.jena.sparql.expr.E_Regex)3 Random (java.util.Random)2