Chained adaptation choosen over direct route

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Chained adaptation choosen over direct route

Andy Gibson

Hi,

 

I have a situation where traits is choosing to use 2 adapters instead of 1 (see the code below)

 

The GAD -> IProcess ->IProperties chain is used as opposed to the direct GAD->IAvailableProperties.

If I remove the IRefreshableProperties layer of inheritance then the direct adaptation route is taken.

 

The online documentation states:

 

In some cases, no single adapter class exists that adapts the object to the required interface, but a series of adapter classes exist that together perform the required adaptation. In such cases, the necessary set of adapter objects are created, and the “last” link in the chain, the one that actually implements the required interface, is assigned as the trait value. When a situation like this arises, the adapted object assigned to the trait always contains the smallest set of adapter objects needed to adapt the original object.

 

Which seems to contradict the behaviour I’m seeing. Is my understanding correct?

Also, is anyone aware of any tools / scripts for analysing adaptation routes and warning of potential issues like this?

 

Regards,

Andy Gibson

 

 

from enthought.traits.api import (Interface, adapts, Adapter,implements)

 

class IProperties(Interface):

    pass

 

class IRefreshableProperties(IProperties):

    pass

 

class IAvailableProperties(IRefreshableProperties):

    pass

 

class IProcess(Interface):

    pass

 

class GAD():

    pass

 

class GADToIAvailablePropertiesAdapter(Adapter):

    adapts(GAD, to=[IAvailableProperties], cached=True)

   

class GADToIProcessAdapter(Adapter):

    adapts(GAD, to=[IProcess], cached=True)

 

class IProcessIPropertiesAdapter(Adapter):

    adapts(IProcess, to=[IProperties], cached=True)

   

gad = GAD()

proc = IProperties(gad)

print proc

 

Regards,

Andy


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Chained adaptation choosen over direct route

Pietro Berkes
Dear Andy,

this looks like a bug of the old adaptation mechanism. My best guess is that it is comparing the path
GAD -> IProcess -> IProperties
with
GAD -> IProperties -> IRefreshableProperties -> IAvailableProperties,
even thought in the second, there aren't two extra adaptation steps, but rather two steps up a class hierarchy.

The adaptation mechanism has been completely rewritten earlier this year, and now behaves as you expected. If that is an option for you, I would recommend downloading the latest traits code and installing it on your machine. It might be worth going through the documentation regarding adaptation, as part of the old API has been deprecated in favor of a new, more modern one based on decorators.

All the best,
Pietro



On Mon, Aug 12, 2013 at 12:11 PM, Andy Gibson <[hidden email]> wrote:

Hi,

 

I have a situation where traits is choosing to use 2 adapters instead of 1 (see the code below)

 

The GAD -> IProcess ->IProperties chain is used as opposed to the direct GAD->IAvailableProperties.

If I remove the IRefreshableProperties layer of inheritance then the direct adaptation route is taken.

 

The online documentation states:

 

In some cases, no single adapter class exists that adapts the object to the required interface, but a series of adapter classes exist that together perform the required adaptation. In such cases, the necessary set of adapter objects are created, and the “last” link in the chain, the one that actually implements the required interface, is assigned as the trait value. When a situation like this arises, the adapted object assigned to the trait always contains the smallest set of adapter objects needed to adapt the original object.

 

Which seems to contradict the behaviour I’m seeing. Is my understanding correct?

Also, is anyone aware of any tools / scripts for analysing adaptation routes and warning of potential issues like this?

 

Regards,

Andy Gibson

 

 

from enthought.traits.api import (Interface, adapts, Adapter,implements)

 

class IProperties(Interface):

    pass

 

class IRefreshableProperties(IProperties):

    pass

 

class IAvailableProperties(IRefreshableProperties):

    pass

 

class IProcess(Interface):

    pass

 

class GAD():

    pass

 

class GADToIAvailablePropertiesAdapter(Adapter):

    adapts(GAD, to=[IAvailableProperties], cached=True)

   

class GADToIProcessAdapter(Adapter):

    adapts(GAD, to=[IProcess], cached=True)

 

class IProcessIPropertiesAdapter(Adapter):

    adapts(IProcess, to=[IProperties], cached=True)

   

gad = GAD()

proc = IProperties(gad)

print proc

 

Regards,

Andy


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev




--
Pietro Berkes
Scientific software developer
Enthought UK


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Chained adaptation choosen over direct route

Andy Gibson

Hi Pietro,

 

Thanks for the response and confirming that what I’ve seen is a bug; I’ll take a look at the new API docs.

 

I don’t think we’ll be in a position to upgrade to the new API in the near future so I’d still be interested if anyone had any analysis tools that I could use to check where multiple adaptation routes exist.

 

Regards,

Andy

From: [hidden email] [mailto:[hidden email]] On Behalf Of Pietro Berkes
Sent: 12 August 2013 13:47
To: [hidden email]
Subject: Re: [Enthought-Dev] Chained adaptation choosen over direct route

 

Dear Andy,

 

this looks like a bug of the old adaptation mechanism. My best guess is that it is comparing the path

GAD -> IProcess -> IProperties

with

GAD -> IProperties -> IRefreshableProperties -> IAvailableProperties,

even thought in the second, there aren't two extra adaptation steps, but rather two steps up a class hierarchy.

 

The adaptation mechanism has been completely rewritten earlier this year, and now behaves as you expected. If that is an option for you, I would recommend downloading the latest traits code and installing it on your machine. It might be worth going through the documentation regarding adaptation, as part of the old API has been deprecated in favor of a new, more modern one based on decorators.

 

All the best,

Pietro

 

 

On Mon, Aug 12, 2013 at 12:11 PM, Andy Gibson <[hidden email]> wrote:

Hi,

 

I have a situation where traits is choosing to use 2 adapters instead of 1 (see the code below)

 

The GAD -> IProcess ->IProperties chain is used as opposed to the direct GAD->IAvailableProperties.

If I remove the IRefreshableProperties layer of inheritance then the direct adaptation route is taken.

 

The online documentation states:

 

In some cases, no single adapter class exists that adapts the object to the required interface, but a series of adapter classes exist that together perform the required adaptation. In such cases, the necessary set of adapter objects are created, and the “last” link in the chain, the one that actually implements the required interface, is assigned as the trait value. When a situation like this arises, the adapted object assigned to the trait always contains the smallest set of adapter objects needed to adapt the original object.

 

Which seems to contradict the behaviour I’m seeing. Is my understanding correct?

Also, is anyone aware of any tools / scripts for analysing adaptation routes and warning of potential issues like this?

 

Regards,

Andy Gibson

 

 

from enthought.traits.api import (Interface, adapts, Adapter,implements)

 

class IProperties(Interface):

    pass

 

class IRefreshableProperties(IProperties):

    pass

 

class IAvailableProperties(IRefreshableProperties):

    pass

 

class IProcess(Interface):

    pass

 

class GAD():

    pass

 

class GADToIAvailablePropertiesAdapter(Adapter):

    adapts(GAD, to=[IAvailableProperties], cached=True)

   

class GADToIProcessAdapter(Adapter):

    adapts(GAD, to=[IProcess], cached=True)

 

class IProcessIPropertiesAdapter(Adapter):

    adapts(IProcess, to=[IProperties], cached=True)

   

gad = GAD()

proc = IProperties(gad)

print proc

 

Regards,

Andy


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev



 

--
Pietro Berkes
Scientific software developer
Enthought UK


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev