I think the problem is that in the cases where this does not apply you
return the original input rather than making a copy so you overwrite
some/all of your changes

Try replacing the following:

return opUnion;

With the following:

return super.transform(opUnion, left, right);

NB - I haven't tested this, but based on my experience of extending
TransformCopy this is likely the culprit.

Rob


On 8/26/13 6:22 PM, "Diogo FC Patrao" <[email protected]> wrote:

>Hi all,
>
>I'm having trouble figuring out how Transformer class is working. See my
>classes below:
>
>
>public class GrumpyOptimizer extends TransformCopy {
>
>@Override
> public Op transform(OpUnion opUnion, Op left, Op right) {
> if ( !( left instanceof OpService && right instanceof OpService))
> return opUnion;
> Node l = ((OpService)left).getService();
>Node r = ((OpService)right).getService();
>
>// both runs go inside this if
> if (l.toString().compareToIgnoreCase(r.toString()) > 0 ) {
>return new OpUnion( right, left );
> }
>return opUnion;
> }
>
>}
>
>// --- test
>
> GrumpyOptimizer go = new GrumpyOptimizer();
>
> System.out.println("Teste 1");
>
>  Query query = QueryFactory.create("SELECT ?a {" +
>
>//** "{ SERVICE <http://3.com/> { ?a <
>http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://marafo.com/teste>
>} }" +
>
>//** "UNION " +
>
>  "{ SERVICE <http://2.com/> { ?a <
>http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://marafo.com/teste>
>} }" +
>
>  "UNION " +
>
>  "{ SERVICE <http://1.com/> { ?a <
>http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
><http://marafo.com/chatuba>
>} } }" );
>
>
> Op op = Algebra.compile(query);
>
> System.out.println(op);
>
>  op = Transformer.transform(go, op);
>
> System.out.println(op);
>
>Ok; when running the code above keeping the commented lines, I get the
>expected result below:
>
>Teste optimizer
>(project (?a)
>  (union
>    (service <http://2.com/>
>      (bgp (triple ?a <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <
>http://marafo.com/teste>)))
>    (service <http://1.com/>
>      (bgp (triple ?a <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <
>http://marafo.com/chatuba>)))))
>
>(project (?a)
>  (union
>    (service <http://1.com/>
>      (bgp (triple ?a <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <
>http://marafo.com/chatuba>)))
>    (service <http://2.com/>
>      (bgp (triple ?a <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <
>http://marafo.com/teste>)))))
>
>
>It "orders" the services by endpoint URL; however, If I uncomment the
>lines
>marked with //**, nothing changes:
>
>Teste optimizer
>(project (?a)
>  (union
>    (union
>      (service <http://3.com/>
>        (bgp (triple ?a <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
><
>http://marafo.com/teste>)))
>      (service <http://2.com/>
>        (bgp (triple ?a <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
><
>http://marafo.com/teste>))))
>    (service <http://1.com/>
>      (bgp (triple ?a <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <
>http://marafo.com/chatuba>)))))
>
>(project (?a)
>  (union
>    (union
>      (service <http://3.com/>
>        (bgp (triple ?a <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
><
>http://marafo.com/teste>)))
>      (service <http://2.com/>
>        (bgp (triple ?a <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
><
>http://marafo.com/teste>))))
>    (service <http://1.com/>
>      (bgp (triple ?a <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <
>http://marafo.com/chatuba>)))))
>
>I wasn't expecting that the whole thing would be sorted, but at least the
>first two OpService should. In case you're wondering, it gets into the IF
>that flip the nodes in both cases.
>
>It's running JENA 2.10.1-SNAPSHOT (apr-4).
>
>Thanks in advance!
>
>--
>diogo patrĂ£o

Reply via email to