Search in sources :

Example 46 with VarExprList

use of org.apache.jena.sparql.core.VarExprList in project jena by apache.

the class TransformRemoveAssignment method transform.

@Override
public Op transform(OpExtend opExtend, Op subOp) {
    VarExprList assignments = processAssignments(opExtend);
    if (assignments == null)
        return super.transform(opExtend, subOp);
    this.aboveExtend = true;
    // Rewrite appropriately
    if (this.topmostOnly) {
        // hence call getSubOp() rather than using the provided subOp
        if (assignments.size() > 0) {
            return OpExtend.create(opExtend.getSubOp(), assignments);
        } else {
            return opExtend.getSubOp();
        }
    } else {
        // Otherwise preserve any transformations from lower down the tree
        if (assignments.size() > 0) {
            return OpExtend.create(subOp, assignments);
        } else {
            return subOp;
        }
    }
}
Also used : VarExprList(org.apache.jena.sparql.core.VarExprList)

Example 47 with VarExprList

use of org.apache.jena.sparql.core.VarExprList 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 48 with VarExprList

use of org.apache.jena.sparql.core.VarExprList in project jena by apache.

the class AbstractRewriter method rewrite.

/**
	 * Rewrite a variable expression list.
	 * @param lst The variable expression list.
	 * @return the rewritten variable expresson list.
	 */
public final VarExprList rewrite(VarExprList lst) {
    VarExprList retval = new VarExprList();
    for (Var v : lst.getVars()) {
        Node n = values.get(v);
        if (n != null) {
            if (n.isVariable()) {
                retval.add(Var.alloc(n));
            }
        } else {
            retval.add(v);
        }
    }
    for (Map.Entry<Var, Expr> entry : lst.getExprs().entrySet()) {
        Expr target = ExprUtils.nodeToExpr(entry.getKey());
        Node n = values.get(entry.getKey());
        Var v = entry.getKey();
        Expr e = entry.getValue();
        if (n != null) {
            if (n.isVariable()) {
                v = Var.alloc(n);
                if (target.equals(e)) {
                    e = ExprUtils.nodeToExpr(n);
                }
            } else {
                v = null;
            }
        }
        if (v != null) {
            retval.add(v, e);
        }
    }
    return retval;
}
Also used : Expr(org.apache.jena.sparql.expr.Expr) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) VarExprList(org.apache.jena.sparql.core.VarExprList) BindingHashMap(org.apache.jena.sparql.engine.binding.BindingHashMap) Map(java.util.Map)

Example 49 with VarExprList

use of org.apache.jena.sparql.core.VarExprList in project jena by apache.

the class SelectHandler method parseExpr.

/**
	 * Parse an expression string into an expression.
	 * 
	 * This must be able to be parsed as though it were written "SELECT "+s
	 * 
	 * @param s
	 *            the select string to parse.
	 * @return the epxression
	 * @throws QueryParseException
	 *             on error
	 */
private Expr parseExpr(String s) throws QueryParseException {
    try {
        ARQParser parser = new ARQParser(new StringReader("SELECT " + s));
        parser.setQuery(new Query());
        parser.getQuery().setPrefixMapping(query.getPrefixMapping());
        parser.SelectClause();
        Query q = parser.getQuery();
        VarExprList vel = q.getProject();
        return vel.getExprs().values().iterator().next();
    } catch (ParseException ex) {
        throw new QueryParseException(ex.getMessage(), ex.currentToken.beginLine, ex.currentToken.beginLine);
    } catch (TokenMgrError tErr) {
        throw new QueryParseException(tErr.getMessage(), -1, -1);
    } catch (Error err) {
        // The token stream can throw java.lang.Error's
        String tmp = err.getMessage();
        if (tmp == null)
            throw new QueryParseException(err, -1, -1);
        throw new QueryParseException(tmp, -1, -1);
    }
}
Also used : Query(org.apache.jena.query.Query) ARQParser(org.apache.jena.sparql.lang.arq.ARQParser) StringReader(java.io.StringReader) TokenMgrError(org.apache.jena.sparql.lang.arq.TokenMgrError) TokenMgrError(org.apache.jena.sparql.lang.arq.TokenMgrError) ParseException(org.apache.jena.sparql.lang.arq.ParseException) QueryParseException(org.apache.jena.query.QueryParseException) VarExprList(org.apache.jena.sparql.core.VarExprList) QueryParseException(org.apache.jena.query.QueryParseException)

Aggregations

VarExprList (org.apache.jena.sparql.core.VarExprList)49 Var (org.apache.jena.sparql.core.Var)21 Test (org.junit.Test)15 Op (org.apache.jena.sparql.algebra.Op)12 Expr (org.apache.jena.sparql.expr.Expr)8 Query (org.apache.jena.query.Query)7 NodeValueInteger (org.apache.jena.sparql.expr.nodevalue.NodeValueInteger)7 ContractTest (org.xenei.junit.contract.ContractTest)6 E_Random (org.apache.jena.sparql.expr.E_Random)4 QueryParseException (org.apache.jena.query.QueryParseException)3 ExprAggregator (org.apache.jena.sparql.expr.ExprAggregator)2 ExprVar (org.apache.jena.sparql.expr.ExprVar)2 StringReader (java.io.StringReader)1 Map (java.util.Map)1 ExprRewriter (org.apache.jena.arq.querybuilder.rewriters.ExprRewriter)1 Node (org.apache.jena.graph.Node)1 SortCondition (org.apache.jena.query.SortCondition)1 OpAssign (org.apache.jena.sparql.algebra.op.OpAssign)1 OpExtend (org.apache.jena.sparql.algebra.op.OpExtend)1 BindingHashMap (org.apache.jena.sparql.engine.binding.BindingHashMap)1