Search in sources :

Example 21 with SortCondition

use of org.apache.jena.query.SortCondition in project jena by apache.

the class TransformOrderByDistinctApplication method transform.

@Override
public Op transform(OpDistinct opDistinct, Op subOp) {
    if (subOp instanceof OpProject) {
        OpProject project = (OpProject) subOp;
        // Inner operation must be an ORDER BY
        if (project.getSubOp() instanceof OpOrder) {
            List<Var> projectVars = project.getVars();
            OpOrder order = (OpOrder) project.getSubOp();
            // Everything we wish to order by must only use variables that
            // appear in the project list
            boolean ok = true;
            for (SortCondition condition : order.getConditions()) {
                if (!isValidSortCondition(condition, projectVars)) {
                    ok = false;
                    break;
                }
            }
            // Everything checks out so we can make the change
            if (ok) {
                OpProject newProject = new OpProject(order.getSubOp(), project.getVars());
                OpDistinct newDistinct = new OpDistinct(newProject);
                return new OpOrder(newDistinct, order.getConditions());
            }
        }
    }
    // If we reach here then this transform is not applicable
    return super.transform(opDistinct, subOp);
}
Also used : SortCondition(org.apache.jena.query.SortCondition) OpProject(org.apache.jena.sparql.algebra.op.OpProject) OpOrder(org.apache.jena.sparql.algebra.op.OpOrder) Var(org.apache.jena.sparql.core.Var) OpDistinct(org.apache.jena.sparql.algebra.op.OpDistinct)

Example 22 with SortCondition

use of org.apache.jena.query.SortCondition in project jena by apache.

the class SolutionModifierHandler method setVars.

@Override
public void setVars(Map<Var, Node> values) {
    if (values.isEmpty()) {
        return;
    }
    ExprRewriter exprRewriter = new ExprRewriter(values);
    ExprList having = exprRewriter.rewrite(new ExprList(query.getHavingExprs()));
    List<SortCondition> orderBy = exprRewriter.rewriteSortConditionList(query.getOrderBy());
    VarExprList groupBy = exprRewriter.rewrite(query.getGroupBy());
    query.getHavingExprs().clear();
    query.getHavingExprs().addAll(having.getList());
    if (orderBy != null) {
        if (query.getOrderBy() == null) {
            for (SortCondition sc : orderBy) {
                query.addOrderBy(sc);
            }
        } else {
            query.getOrderBy().clear();
            query.getOrderBy().addAll(orderBy);
        }
    }
    query.getGroupBy().clear();
    query.getGroupBy().addAll(groupBy);
}
Also used : SortCondition(org.apache.jena.query.SortCondition) ExprRewriter(org.apache.jena.arq.querybuilder.rewriters.ExprRewriter) ExprList(org.apache.jena.sparql.expr.ExprList) VarExprList(org.apache.jena.sparql.core.VarExprList) VarExprList(org.apache.jena.sparql.core.VarExprList)

Example 23 with SortCondition

use of org.apache.jena.query.SortCondition in project jena by apache.

the class SolutionModifierHandler method addAll.

/**
	 * Copy all the modifications from the Solution Modifier argument
	 * @param solutionModifier The solution modifier to copy from.
	 */
public void addAll(SolutionModifierHandler solutionModifier) {
    List<SortCondition> lst = solutionModifier.query.getOrderBy();
    if (lst != null) {
        for (SortCondition sc : lst) {
            query.addOrderBy(sc);
        }
    }
    query.getGroupBy().addAll(solutionModifier.query.getGroupBy());
    query.getHavingExprs().addAll(solutionModifier.query.getHavingExprs());
    query.setLimit(solutionModifier.query.getLimit());
    query.setOffset(solutionModifier.query.getOffset());
}
Also used : SortCondition(org.apache.jena.query.SortCondition)

Aggregations

SortCondition (org.apache.jena.query.SortCondition)23 Var (org.apache.jena.sparql.core.Var)10 ArrayList (java.util.ArrayList)5 Binding (org.apache.jena.sparql.engine.binding.Binding)4 BindingComparator (org.apache.jena.sparql.engine.binding.BindingComparator)4 ExprVar (org.apache.jena.sparql.expr.ExprVar)3 SortedDataBag (org.apache.jena.atlas.data.SortedDataBag)2 Op (org.apache.jena.sparql.algebra.Op)2 OpOrder (org.apache.jena.sparql.algebra.op.OpOrder)2 OpProject (org.apache.jena.sparql.algebra.op.OpProject)2 Expr (org.apache.jena.sparql.expr.Expr)2 File (java.io.File)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 ExprRewriter (org.apache.jena.arq.querybuilder.rewriters.ExprRewriter)1 Node (org.apache.jena.graph.Node)1 QueryCancelledException (org.apache.jena.query.QueryCancelledException)1 QueryExecException (org.apache.jena.query.QueryExecException)1 OpDistinct (org.apache.jena.sparql.algebra.op.OpDistinct)1 VarExprList (org.apache.jena.sparql.core.VarExprList)1