[
https://issues.apache.org/jira/browse/JENA-1170?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15262922#comment-15262922
]
Claus Stadler commented on JENA-1170:
-------------------------------------
My use case is an isomorphy cache; or rather the unit test[1] of it:
I take a query and add it to the cache with a fake result table.
Then I rename all of the query's variables and the cache component should be
able to find out that there exists a mapping of the variables that makes them
equal (or in the case of quad patterns, makes one a subset of the other).
So I don't have to care about scoping issues, because a global rename is just
fine for my use case.
The snippet is essentially:
{{code:java}}
Collection<Var> vars = PatternVars.vars(baseQuery.getQueryPattern());
Generator<Var> gen = new
VarGeneratorBlacklist(VarGeneratorImpl2.create("v"), vars);
Map<Var, Node> varMap = vars.stream()
.collect(Collectors.toMap(
v -> v,
v -> (Node)gen.next()));
Query renamedQuery = QueryTransformOps.transform(baseQuery, varMap);
// ...
sparqlViewCache.index(baseQfpc, table);
CacheResult cr = sparqlViewCache.lookup(renamedQfpc);
{{code}}
The VarGeneratorBlacklist does not generate variables mentioned in 'vars' - not
sure if Jena has such a util already - if so, a pointer is appreciated so I
could remove my own solution :)
(it could be done with a stream and a filter lambda x -> !vars.contains(x) but
still its more convenient just passing vars to an util function)
[1]
https://github.com/AKSW/jena-sparql-api/blob/develop/jena-sparql-api-cache/src/test/java/org/aksw/jena_sparql_api/concept_cache/main/TestSparqlViewCacheVariableRenaming.java#L97
> 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)