Search in sources :

Example 31 with Binding

use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.

the class TestQuery method query_recursive_01.

@Test
public void query_recursive_01() {
    String query = "SELECT * WHERE { SERVICE <" + serviceQuery() + "> { ?s ?p ?o . BIND(?o AS ?x) } }";
    try (QueryExecution qExec = QueryExecutionFactory.sparqlService(serviceQuery(), query)) {
        ResultSet rs = qExec.execSelect();
        Var x = Var.alloc("x");
        while (rs.hasNext()) {
            Binding b = rs.nextBinding();
            Assert.assertNotNull(b.get(x));
        }
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Var(org.apache.jena.sparql.core.Var) ServerTest(org.apache.jena.fuseki.ServerTest) Test(org.junit.Test)

Example 32 with Binding

use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.

the class AlgebraEx method main.

public static void main(String[] args) {
    String s = "SELECT DISTINCT ?s { ?s ?p ?o }";
    // Parse
    Query query = QueryFactory.create(s);
    System.out.println(query);
    // Generate algebra
    Op op = Algebra.compile(query);
    op = Algebra.optimize(op);
    System.out.println(op);
    // Execute it.
    QueryIterator qIter = Algebra.exec(op, ExQuerySelect1.createModel());
    // Results
    for (; qIter.hasNext(); ) {
        Binding b = qIter.nextBinding();
        System.out.println(b);
    }
    qIter.close();
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Op(org.apache.jena.sparql.algebra.Op) Query(org.apache.jena.query.Query) QueryIterator(org.apache.jena.sparql.engine.QueryIterator)

Example 33 with Binding

use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.

the class AlgebraGenerator method compileModifiers.

/** Compile query modifiers */
protected Op compileModifiers(Query query, Op pattern) {
    /* The modifier order in algebra is:
          * 
          * Limit/Offset
          *   Distinct/reduce
          *     project
          *       OrderBy
          *         Bindings
          *           having
          *             select expressions
          *               group
          */
    // Preparation: sort SELECT clause into assignments and projects.
    VarExprList projectVars = query.getProject();
    // Assignments to be done.
    VarExprList exprs = new VarExprList();
    // projection variables
    List<Var> vars = new ArrayList<>();
    Op op = pattern;
    if (query.hasGroupBy()) {
        // When there is no GroupBy but there are some aggregates, it's a group of no variables.
        op = OpGroup.create(op, query.getGroupBy(), query.getAggregators());
    }
    // Look for assignments in SELECT expressions.
    if (!projectVars.isEmpty() && !query.isQueryResultStar()) {
        // through in SELECT *
        if (projectVars.size() == 0 && query.isSelectType())
            Log.warn(this, "No project variables");
        // Separate assignments and variable projection.
        for (Var v : query.getProject().getVars()) {
            Expr e = query.getProject().getExpr(v);
            if (e != null) {
                Expr e2 = ExprLib.replaceAggregateByVariable(e);
                exprs.add(v, e2);
            }
            // Include in project
            vars.add(v);
        }
    }
    // ---- Assignments from SELECT and other places (so available to ORDER and HAVING)
    for (Var v : exprs.getVars()) {
        Expr e = exprs.getExpr(v);
        op = OpExtend.create(op, v, e);
    }
    // ---- HAVING
    if (query.hasHaving()) {
        for (Expr expr : query.getHavingExprs()) {
            // HAVING expression to refer to the aggregate via the variable.
            Expr expr2 = ExprLib.replaceAggregateByVariable(expr);
            op = OpFilter.filter(expr2, op);
        }
    }
    // ---- VALUES
    if (query.hasValues()) {
        Table table = TableFactory.create(query.getValuesVariables());
        for (Binding binding : query.getValuesData()) table.addBinding(binding);
        OpTable opTable = OpTable.create(table);
        op = OpJoin.create(op, opTable);
    }
    // ---- ToList
    if (context.isTrue(ARQ.generateToList))
        // Listify it.
        op = new OpList(op);
    // ---- ORDER BY
    if (query.getOrderBy() != null) {
        List<SortCondition> scList = new ArrayList<>();
        // Aggregates in ORDER BY
        for (SortCondition sc : query.getOrderBy()) {
            Expr e = sc.getExpression();
            e = ExprLib.replaceAggregateByVariable(e);
            scList.add(new SortCondition(e, sc.getDirection()));
        }
        op = new OpOrder(op, scList);
    }
    if (vars.size() > 0)
        op = new OpProject(op, vars);
    // ---- DISTINCT
    if (query.isDistinct())
        op = OpDistinct.create(op);
    // ---- REDUCED
    if (query.isReduced())
        op = OpReduced.create(op);
    // ---- LIMIT/OFFSET
    if (query.hasLimit() || query.hasOffset())
        op = new OpSlice(op, query.getOffset(), /*start*/
        query.getLimit());
    return op;
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) SortCondition(org.apache.jena.query.SortCondition)

Example 34 with Binding

use of org.apache.jena.sparql.engine.binding.Binding 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)

Example 35 with Binding

use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.

the class TestDistinctDataBag method testTemporaryFilesAreCleanedUpAfterCompletion.

@Test
public void testTemporaryFilesAreCleanedUpAfterCompletion() {
    List<Binding> undistinct = new ArrayList<>();
    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") };
    for (int i = 0; i < 500; i++) {
        undistinct.add(randomBinding(vars));
    }
    DistinctDataBag<Binding> db = new DistinctDataBag<>(new ThresholdPolicyCount<Binding>(10), SerializationFactoryFinder.bindingSerializationFactory(), new BindingComparator(new ArrayList<SortCondition>()));
    List<File> spillFiles = new ArrayList<>();
    try {
        db.addAll(undistinct);
        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 : Binding(org.apache.jena.sparql.engine.binding.Binding) BuilderBinding(org.apache.jena.sparql.sse.builders.BuilderBinding) BindingComparator(org.apache.jena.sparql.engine.binding.BindingComparator) Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList) DistinctDataBag(org.apache.jena.atlas.data.DistinctDataBag) Random(java.util.Random) File(java.io.File) Test(org.junit.Test)

Aggregations

Binding (org.apache.jena.sparql.engine.binding.Binding)132 Node (org.apache.jena.graph.Node)39 ArrayList (java.util.ArrayList)36 Var (org.apache.jena.sparql.core.Var)33 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)27 Test (org.junit.Test)21 QueryIterPlainWrapper (org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper)16 BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)10 BindingComparator (org.apache.jena.sparql.engine.binding.BindingComparator)8 BuilderBinding (org.apache.jena.sparql.sse.builders.BuilderBinding)8 NoSuchElementException (java.util.NoSuchElementException)7 Triple (org.apache.jena.graph.Triple)7 ResultSetStream (org.apache.jena.sparql.engine.ResultSetStream)7 ARQInternalErrorException (org.apache.jena.sparql.ARQInternalErrorException)5 Op (org.apache.jena.sparql.algebra.Op)5 BasicPattern (org.apache.jena.sparql.core.BasicPattern)5 ResultSet (org.apache.jena.query.ResultSet)4 SortCondition (org.apache.jena.query.SortCondition)4 TableN (org.apache.jena.sparql.algebra.table.TableN)4 File (java.io.File)3