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;
}
}
}
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);
}
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;
}
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);
}
}
Aggregations