You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Diogo FC Patrao <dj...@gmail.com> on 2013/08/27 03:22:01 UTC

Can't get no (Transformer)

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

Re: Can't get no (Transformer)

Posted by Diogo FC Patrao <dj...@gmail.com>.
Hi, Rob, Andy.

Yes! That solved it!

Thanks a lot!

--
diogo patrão




On Tue, Aug 27, 2013 at 2:04 PM, Andy Seaborne <an...@apache.org> wrote:

> On 27/08/13 17:16, Rob Vesse wrote:
>
>> 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
>>
>
> Yes.  In
>
>
> public Op transform(OpUnion opUnion, Op left, Op right)
>
> opUnion is the original input, and left/right the new sub ops due to the
> transforming the elements below this union, so you can see both original
> and sub-writes.
>
>         Andy
>
>
>
>
>
>>
>> On 8/26/13 6:22 PM, "Diogo FC Patrao" <dj...@gmail.com> 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://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://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://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://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://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://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://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://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://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://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://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://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://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
>>>
>>
>>
>

Re: Can't get no (Transformer)

Posted by Andy Seaborne <an...@apache.org>.
On 27/08/13 17:16, Rob Vesse wrote:
> 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

Yes.  In

public Op transform(OpUnion opUnion, Op left, Op right)

opUnion is the original input, and left/right the new sub ops due to the 
transforming the elements below this union, so you can see both original 
and sub-writes.

	Andy



>
>
> On 8/26/13 6:22 PM, "Diogo FC Patrao" <dj...@gmail.com> 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
>


Re: Can't get no (Transformer)

Posted by Rob Vesse <rv...@yarcdata.com>.
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" <dj...@gmail.com> 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