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. 

I am reading and learning about more Jena packages to figure out how to write 
the appropriate lines of code. 
In case you have any advice, it would be very appreciated. 

Many Thanks, 
Best Regards, 
Carlo


________________________________________
From: Lorenz Bühmann [[email protected]]
Sent: Saturday, January 30, 2016 1:51 PM
To: [email protected]
Subject: Re: How to remove consistently a triple pattern given a SPARQL query?

Hello Carlo,

you have to remove the whole filter from it's parent expression, which
makes it a little bit more difficult in a visitor pattern-based
implementation. But should be rather simple, just some lines of code.

Lorenz

> Dear Andy and all,
>
> Thank yo very much for your help to accomplish this task.
> I am using the package org.apache.jena.sparql.syntax as suggested.
>
> The way I am doing it is the following:
>
>
>
> ===== MAIN CLASS
> // This is the main method to implement the remove of a triple from a SPARQL 
> query
>   private static Query RemoveOpImplementation(Query q, Triple tp) {
>
>
>
>          Op op = Algebra.compile(q);
>          Transform transform = new TransformRemoveOp(q,tp) ;
>          op = Transformer.transform(transform, op) ;
>          Query queryWithoutTriplePattern = OpAsQuery.asQuery(op) ;
>
> }
>
>
> ===== TransformRemoveOp
> // the implement the transform
> public class TransformRemoveOp implements Transform {
>
> }
>
> Again, removing the triple is quite straightforward
>
>
>      @Override
>      public Op transform(OpBGP opBGP) {
>          System.out.println("[TransformRemoveOp::transform(OpBGP opBGP)] " + 
> opBGP.toString());
>          System.out.println("");
>          Op opNew = opBGP.copy();
>          BasicPattern newBP = ((OpBGP) opNew).getPattern();
>          List<Triple> tripleList = newBP.getList();
>
>          Iterator<Triple> itr = tripleList.iterator();
>          while (itr.hasNext()) {
>              Triple tp = itr.next();
>              if (tp.matches(this.triple)) {
>                  itr.remove();
>              }
>          }
>          return opNew;
>      }
>
>
> but not the Filter
>
>
>
>
>          @Override
>      public Op transform(OpFilter opFilter, Op subOp) {
>
>          //...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
>          for (Var var : expVars) {
>              //..if it does then we have to delete the entire FILTER 
> expression
>              if (tpVars.contains(var)) {
>                  System.out.println("[TransformRemoveOp::transform(OpFilter 
> opFilter, Op subOp)] FILTER TO BE REMOVED!!!HOW? ");
>                  System.out.println("");
>              }
>          }
>          return opFilter;
>      }
>
> Sorry for posting again and again the same question.
> What am I missing or not understanding?
> How should I use the abstract syntax tree to do it?
>
> I am looking at the code of the example reported in Jena, but still I am 
> strangling to find it out.
> Please, any example code that I could have look at?
>
> Many Thanks in advance for your help and support.
> Best Regards,
> Carlo
> On 29 Jan 2016, at 14:16, Andy Seaborne 
> <[email protected]<mailto:[email protected]>> wrote:
>
> On 29/01/16 12:57, Carlo.Allocca wrote:
> Dear Andy and All,
>
> I tried to use as many suggested info as I could. As result, I obtained the 
> following:
>
> I am implementing a public class RemoveOpTransform implements 
> ElementTransform. And, again It is quite straightforward to remove the triple 
> pattern from the ElementPathBlock
>
>     @Override
>      public Element transform(ElementPathBlock eltPB) {
>
> The idea of the transform design is to not change the inoput but to create a 
> new copy if you change it.  If you don't change it you can return the input.
>
>
>          System.out.println("[RemoveOpTransform::transform(ElementPathBlock 
> arg0)] " + eltPB.toString());
>          Iterator<TriplePath> l = eltPB.patternElts();
>          while (l.hasNext()) {
>              TriplePath tp = l.next();
>              if (tp.asTriple().matches(this.triple)) {
>                  l.remove();
>                  
> System.out.println("[RemoveOpTransform::transform(ElementPathBlock arg0)] 
> TRIPLE MAP JUST REMOVED!!! ");
>                  System.out.println("");
>                  return this.transform(eltPB);//eltPB;
>              }
>          }
>          return eltPB;
>      }
>
> But I could not figure out how to remove the associated FILTER when  
> transform(ElementFilter arg0, Expr arg1) as below
>
> To remove a filter altogether you need to delete the ElementFilter object 
> from the AST (abstract syntax tree).  You can't do that inside the 
> transform/ElementFilter.
>
> It means looking in the element containing the ElementFilter which will be in 
> a ElementGroup of some kind, probably as part of an ElementUnion.
>
> An alternative is to replace the filter expression with a no-op like
>
> FILTER(true)
>
> and live with that.
>
>     Andy
>
>
>      @Override
>      public Element transform(ElementFilter arg0, Expr arg1) {
>
>          //...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
>          Set<Var> expVars = arg1.getVarsMentioned();
>
>          //...check whether the FILTER expression contains any of the triple 
> pattern variable
>          for (Var var : expVars) {
>              //..if it does then we have to delete the entire FILTER 
> expression
>              if (tpVars.contains(var)) {
>                  
> System.out.println("[SQRemoveTripleVisitor::visit(ElementFilter arg0)] FILTER 
> TO BE REMOVED!!!HOW? ");
>                  System.out.println("");
>              }
>          }
>
>          return arg0;
>      }
>
> It appears to me that I am at the same point of the previous approach.
> What am I doing wrong or missing? Or what I should ready to figure it out?
>
> Thank you very much for your kind help and support.
>
> Best Regards,
> Carlo
>
>
>
>
> On 29 Jan 2016, at 10:32, Andy Seaborne 
> <[email protected]<mailto:[email protected]>> wrote:
>
> You'll still have to clean up structures.  SPIN for this is encoding the 
> query in RDF but the rewriting of that structure needs the same (in the 
> abstract) processing.  You may be able to do it with a series of SPARQL 
> Updates (that's speculation).
>
> Andy
>
> On 29/01/16 00:26, Carlo.Allocca wrote:
> Dear Martynas,
>
>
> Thank you very much for your help.
> At the moment, I am trying what Andy suggested.
> I hope to make it. Otherwise, I will look at SPIN API.
>
> Many Thanks,
> Best Regards,
> Carlo
>
>
> On 28 Jan 2016, at 23:48, Martynas Jusevičius 
> <[email protected]<mailto:[email protected]>> wrote:
>
> You could do it using SPIN API as well: http://spinrdf.org/sp.html
>
> On Thu, Jan 28, 2016 at 7:39 PM, Carlo.Allocca 
> <[email protected]<mailto:[email protected]>> wrote:
> Dear Andy,
>
> Thank you very much for your promtly reply.
>
> I need a little bit of exploring and thinking to apply what you suggested.
> Indeed, I will look at each single info you pointed out.
>
> I will keep this thread updated.
>
> Many Thanks,
> Best Regards,
> Carlo
>
> On 28 Jan 2016, at 18:29, Andy Seaborne 
> <[email protected]<mailto:[email protected]>> wrote:
>
> The approach I would take is to do this is multiple passes:
>
> 1/ remove from ElementPathBlock
>
> 2/ Look for elements that are now "empty"
> e.g ElementFilter with empty
> but also as there one arm of a UNION.
>
> (2) is only one pass as the rewrite is done bottom up.
>
>
> I would do this as a transformation rather than modifying the syntax tree in 
> place.
>
> org.apache.jena.sparql.syntax.syntaxtransform
>
>
> See also
> ElementTransformCleanGroupsOfOne
>
> which might be helpful to understand - its doing something different though.
>
> It might be easier to work on the algebra, it's more regular.  Then use 
> OpAsQuery to turn algebnra into a Query.
>
> I could not figure out how to remove the associated FILTER
> when visit(ElementFilter el) as below.
>
> because you need to alter the ElementUnion which holds the ElementFilter.
>
> {...} UNION {} will include a blank row for the {} on the right hand side.
>
>       Andy
>
>
> On 28/01/16 13:14, Carlo.Allocca wrote:
> Dear All,
>
> I am using Jena for my project and I would like to submit the following 
> question: How to remove consistently a triple pattern given a SPARQL query?
>
> EXAMPLE:
>
> For example, considering the query “qString1” and the tp=“?boss ex:isBossOf 
> ?ind .”
> I need to remove tp from qString1, obtaining  qString1. For “Consistently”  I 
> mean that the clause FILTER gets affected (as it contains var that appears in 
> the triple pattern too), therefore it needs to be deleted too.
>
>        String qString1 =
>                 " SELECT DISTINCT ?ind ?boss ?g where "
>                + "{ "
>                + " ?ind rdf:type ex:AssociatedResearcher ."
>                + " {?ind rdf:type ?z. } UNION "
>                + " {?boss ex:isBossOf ?ind . Filter(?boss=\”rossi\”)} "
>                + "}";
>
>
>        String qString2 =
>                 " SELECT DISTINCT ?ind ?boss ?g “
>                 + “WHERE "
>                + "{ "
>                + " ?ind rdf:type ex:AssociatedResearcher ."
>                + " {?ind rdf:type ?z. } UNION "
>                + " {} "
>                + "}";
>
> The solution that I am trying to implement is based on a visitor 
> “SQRemoveTripleVisitor extends ElementVisitorBase”.
> It is quite straightforward to remove the triple pattern when 
> visit(ElementPathBlock el)
>
>    @Override
>    public void visit(ElementPathBlock el) {
>        if (el == null) {
>            throw new 
> IllegalStateException("[SQRemoveTripleVisitor::visit(ElementPathBlock el)] 
> The ElementPathBlock is null!!");
>        }
>
>        ListIterator<TriplePath> it = el.getPattern().iterator();
>        while (it.hasNext()) {
>            final TriplePath tp1 = it.next();
>
>            if (this.tp != null) {
>                if (this.tp.matches(tp1.asTriple())) {
>                    it.remove();
>                }
>            }
>        }
>
>    }
>
> I could not figure out how to remove the associated FILTER when 
> visit(ElementFilter el) as below.
>
>
> @Override
>    public void visit(ElementFilter el) {
>
>        //...get the variables of the FILTER expression
>        Expr filterExp = el.getExpr();//.getVarsMentioned().contains(el);
>        Set<Var> expVars = filterExp.getVarsMentioned();
>
>        //...get the variables of the triple pattern that we want to delete
>        Set<Var> tpVars = new HashSet();
>        Node subj = this.tp.getSubject();
>        if (subj.isVariable()) {
>            tpVars.add((Var) subj);
>        }
>        Node pred = this.tp.getPredicate();
>        if (pred.isVariable()) {
>            tpVars.add((Var) pred);
>        }
>        Node obj = this.tp.getObject();
>        if (obj.isVariable()) {
>            tpVars.add((Var) obj);
>        }
>
>        //...check whether the FILTER expression contains any of the triple 
> pattern’s variable
>        for(Var var:expVars){
>            //…if it does then we have to delete the entire FILTER expression
>            if(tpVars.contains(var)){
>                
> System.out.println("[SQRemoveTripleVisitor::visit(ElementFilter el)] I NEED 
> TO REMOVE THE FILTER!!!!!! ");
>
>            }
>        }
>
>    }
>
> Please, may I ask for any help or idea on how to remove the filter?
>
>
> Many Thanks in advance.
>
> Best Regards,
> Carlo
>
>
> -- 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.
>
--
Lorenz Bühmann
AKSW group, University of Leipzig
Group: http://aksw.org - semantic web research center

Reply via email to