Dear Andy, Thank you very much one more time.
Please, some comments follow in line. On 30 Jan 2016, at 17:53, Andy Seaborne <[email protected]<mailto:[email protected]>> wrote: On 30/01/16 15:52, Carlo.Allocca wrote: Hello Lorenz, Thank you for your help. It was clear to me that I have to remove from the parent expression and such is that using a patter-based approach make it a bit difficult. I tried three of them and I got to the same results. You need to process the parent, not just the ElementFilter. How can I process the parent? My understanding is: 1. Building the Abstract Query Tree: Op op = Algebra.compile(q); For example, given the query PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX ex: <http://www.semanticweb.org/dataset1/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT DISTINCT ?ind ?boss ?g WHERE { { ?ind rdf:type ?z } UNION { ?boss ex:isBossOf ?ind FILTER ( ?boss = "mathieu" ) } } I obtain (distinct (project (?ind ?boss ?g) (union (bgp (triple ?ind <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?z)) (filter (= ?boss "mathieu") (bgp (triple ?boss <http://www.semanticweb.org/dataset1/isBossOf> ?ind)))))) 2) Travere the tree to modify its internal structure. In terms of code, I can write: (A) Transform transform = new TransformRemoveOp(q,tp) ; (B) op = Transformer.transform(transform, op); where TransformRemoveOp implements Transform interface. It seems that there is no link between children and parent that can be used to figure out at which level the process is and modify the Tree structure, accordingly. Please correct me if I am wrong. In fact running it over the above example, I got the following: =========== DURING [TransformRemoveOp::transform(OpBGP opBGP)] opBGPCounter 1 [TransformRemoveOp::transform(OpBGP opBGP)] (bgp (triple ?ind <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?z)) [TransformRemoveOp::transform(OpBGP opBGP)] opBGPCounter 2 [TransformRemoveOp::transform(OpBGP opBGP)] (bgp (triple ?boss <http://www.semanticweb.org/dataset1/isBossOf> ?ind)) [TransformRemoveOp::transform(OpBGP opBGP)] opBGP is empty (bgp ) [TransformRemoveOp::transform(OpFilter opFilter, Op subOp)] opFilter (filter (= ?boss "mathieu") (bgp )) [TransformRemoveOp::transform(OpFilter opFilter, Op subOp)] subOp Name bgp [TransformRemoveOp::transform(OpUnion opUnion, Op left, Op right)] left: (bgp (triple ?ind <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?z)) [TransformRemoveOp::transform(OpUnion opUnion, Op left, Op right)] right: (filter (exprlist (= ?boss "mathieu") true) (bgp )) [TransformRemoveOp::transform(OpProject opProject, Op subOp)] (project (?ind ?boss ?g) (union (bgp (triple ?ind <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?z)) (filter (exprlist (= ?boss "mathieu") true) (bgp )))) [TransformRemoveOp::transform(OpDistinct opDistinct, Op subOp)] (distinct (project (?ind ?boss ?g) (union (bgp (triple ?ind <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?z)) (filter (exprlist (= ?boss "mathieu") true) (bgp ))))) Moreover, implementing a first version of the OpFilter as reported below: @Override public Op transform(OpFilter opFilter, Op subOp) { System.out.println("[TransformRemoveOp::transform(OpFilter opFilter, Op subOp)] opFilter " + opFilter.toString()); System.out.println("[TransformRemoveOp::transform(OpFilter opFilter, Op subOp)] subOp Name " + subOp.getName()); System.out.println(""); if (isParent == false) { return opFilter; } //...get the variables of the triple pattern that we want to delete Set<Var> tpVars = new HashSet(); Node subj = this.triple.getSubject(); if (subj.isVariable()) { tpVars.add((Var) subj); } Node pred = this.triple.getPredicate(); if (pred.isVariable()) { tpVars.add((Var) pred); } Node obj = this.triple.getObject(); if (obj.isVariable()) { tpVars.add((Var) obj); } //...get the variables of the FILTER expression Op opNew = opFilter.copy(subOp); Set<Var> expVars = ((OpFilter) opNew).getExprs().getVarsMentioned(); //...check whether the FILTER expression contains any of the triple pattern variable boolean isContained = false; for (Var var : expVars) { //..if it does then we have to delete the entire FILTER expression if (tpVars.contains(var)) { isContained = true; break; } } //... if the filter contains any variable of the triple that has been removed, then.... if (isContained) { Op newOP; Expr e; if (subOp instanceof OpBGP) { if (((OpBGP) subOp).getPattern().getList().isEmpty()) { e = new NodeValueBoolean(true); newOP = OpFilter.filter(e, opFilter);//filter(e, ); return newOP; } else { e = new NodeValueBoolean(false); newOP = OpFilter.filter(e, opFilter);//filter(e, ); return newOP; } } } return opFilter; } It produces the following SPARQL query: SELECT DISTINCT ?ind ?boss ?g WHERE { { ?ind a ?z } UNION { # Empty BGP FILTER ( ?boss = "mathieu" ) FILTER ( true ) } } It seems quite challenging even transform the expression of "?boss = “mathieu” into “true” (as reported above) does not work as It should. If anyone could help me who is more experienced with this kind of task, I would be very very grateful. I would like to see something concrete in sense of line of code as I am using. Many Thanks, Best Regards, Carlo On 30 Jan 2016, at 17:53, Andy Seaborne <[email protected]<mailto:[email protected]>> wrote: element has active parts -- The Open University is incorporated by Royal Charter (RC 000391), an exempt charity in England & Wales and a charity registered in Scotland (SC 038302). The Open University is authorised and regulated by the Financial Conduct Authority.
