You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jena.apache.org by "Andy Seaborne (JIRA)" <ji...@apache.org> on 2016/04/27 17:41:13 UTC

[jira] [Commented] (JENA-1170) QueryTransformLib cannot rename varibales

    [ https://issues.apache.org/jira/browse/JENA-1170?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15260336#comment-15260336 ] 

Andy Seaborne commented on JENA-1170:
-------------------------------------

I think it is good idea to keep the focus of syntax transformations to clearly defined cases where syntax is the right point to do it. Rewrite for substituting variables for values (a form of parametrized query) is the design goal.

SHACL looks like it will need this - or rather a modified version that does AS renaming in SELECT and some other specal cases. 

Could you say more about your use case? There is a simple way to do it in some case - add expression (?a as ?x) to the SELECT clause.

The current "initial bindings" API calls can then be replaced by this (with care) and then remote queries work.

[2] is not design for Query objects. Algebra transformation is general backbone in ARQ. An ExprTransform is more general, e.g. can replace a variable with an expression. Also, it needs to be sensitive to algebra scoping which does nto apply to query syntax transformation.

Adding variable rename seems reasonable but needs further checks (e.g, what is the variable is already in use? what if the variable is "hidden" by a nested form, a MINUS or a EXISTS?

If you use the development builds, and fix up that exception, you'll notice that the SELECT acquires an AS expression.

Warning: Transformer is due for a complete rewrite to better cover nested and hidden scopes.: https://github.com/afs/jena/tree/walker

> QueryTransformLib cannot rename varibales
> -----------------------------------------
>
>                 Key: JENA-1170
>                 URL: https://issues.apache.org/jira/browse/JENA-1170
>             Project: Apache Jena
>          Issue Type: Improvement
>          Components: ARQ
>    Affects Versions: Jena 3.0.1
>            Reporter: Claus Stadler
>
> Renaming variables of a query - a rather basic use case when transforming queries - does not work with the snippet below due to a raised exception:
> {code:java}
> Map<Var, Node> vmap = Collections.singletonMap(Var.alloc("a"), (Node)Var.alloc("x"));
> Query foo = QueryFactory.create("Select ?a { ?a ?b ?c }");
> Query bar = QueryTransformOps.transform(foo, vmap);
> {code}
> The reason is, that the way QueryTransformOps.transformVarExprList[1] is implemented, prevents this. Also, a much nicer (unfortunately private) implementation already exists at Transformer.ApplyTransformVisitor.process[2] 
> I suggest to make [2] publicly accessible (e.g. as part of one of the TransformLibs or a VarExprUtils) and implement [1] based on that:
> {code:java}
> class QueryTransformOps {
>     private static void transformVarExprList(VarExprList varExprList, ExprTransform exprTransform)
>     {
>         VarExprList tmp = VarExprListUtils.transform(varExprList, exprTransform);
>         VarExprListUtils.replace(varExprList, tmp);
>     }
> }
> class VarExprListUtils {
>     public static void replace(VarExprList dst, VarExprList src) {
>         if(dst != src) {
>             dst.clear();
>             copy(dst, src);
>         }
>     }
>     public static void copy(VarExprList dst, VarExprList src) {
>         for(Var v : src.getVars()) {
>             Expr e = src.getExpr(v);
>             if(e == null) {
>                 src.add(v);
>             } else {
>                 src.add(v, e);
>             }
>         }
>     }
> }
> {code}
> [1] https://github.com/apache/jena/blob/master/jena-arq/src/main/java/org/apache/jena/sparql/syntax/syntaxtransform/QueryTransformOps.java#L74
> [2] https://github.com/apache/jena/blob/master/jena-arq/src/main/java/org/apache/jena/sparql/algebra/Transformer.java#L243
> As a side note, it would be convenient, if the transform method also accepted a Map<Var, Var> - i.e. changing the method signature to accept a Map<Var, ? extends Node>



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)