Agent and listener

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

Agent and listener

Paolo Di Tommaso
Dear all, 

I've a quick question about the Gpars Agent's listener.

Is it possible to have an agent listener invoked when a message is sent to it? 

For example, look at this code: 


        def state = new Agent<List>([])
        int i = 0
        state.addListener { a, b -> i++ }

        state << { it.add(1) }
        state << { it.add(2) }
        state.await()
        assert state.val == [1,2]
        assert i == 2 

The second assertion will fail, since the event listener is invoked only when the agent state changes by calling the updateValue method (as far as I know). 

Is it safe doing something like this in order to force the listener invocation (without creating a new list instance)? 

        state << { it.add(1); updateValue(it) }
        state << { it.add(2); updateValue(it) }



Is there an better way to do that?


Cheers, 
p
 
Reply | Threaded
Open this post in threaded view
|

Re: Agent and listener

Dierk König
You may want to have a look at
 whenBound {}
and
 wheneverBound {}
on Agent.

cheers
Dierk

Am 06.02.2014 um 19:01 schrieb Paolo Di Tommaso <[hidden email]>:

> Dear all,
>
> I've a quick question about the Gpars Agent's listener.
>
> Is it possible to have an agent listener invoked when a message is sent to it?
>
> For example, look at this code:
>
>
>         def state = new Agent<List>([])
>         int i = 0
>         state.addListener { a, b -> i++ }
>
>         state << { it.add(1) }
>         state << { it.add(2) }
>         state.await()
>         assert state.val == [1,2]
>         assert i == 2
>
> The second assertion will fail, since the event listener is invoked only when the agent state changes by calling the updateValue method (as far as I know).
>
> Is it safe doing something like this in order to force the listener invocation (without creating a new list instance)?
>
>         state << { it.add(1); updateValue(it) }
>         state << { it.add(2); updateValue(it) }
>
>
>
> Is there an better way to do that?
>
>
> Cheers,
> p
>  


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Agent and listener

Paolo Di Tommaso
Hi Dierk, 

I tried that, but Agent class does not provided the method whenBound nor wheneverBound. 

I'm using Gpars 1.0, maybe it is available in a newer version ?


Cheers,
Paolo



On Thu, Feb 6, 2014 at 9:31 PM, Dierk König <[hidden email]> wrote:
You may want to have a look at
 whenBound {}
and
 wheneverBound {}
on Agent.

cheers
Dierk

Am 06.02.2014 um 19:01 schrieb Paolo Di Tommaso <[hidden email]>:

> Dear all,
>
> I've a quick question about the Gpars Agent's listener.
>
> Is it possible to have an agent listener invoked when a message is sent to it?
>
> For example, look at this code:
>
>
>         def state = new Agent<List>([])
>         int i = 0
>         state.addListener { a, b -> i++ }
>
>         state << { it.add(1) }
>         state << { it.add(2) }
>         state.await()
>         assert state.val == [1,2]
>         assert i == 2
>
> The second assertion will fail, since the event listener is invoked only when the agent state changes by calling the updateValue method (as far as I know).
>
> Is it safe doing something like this in order to force the listener invocation (without creating a new list instance)?
>
>         state << { it.add(1); updateValue(it) }
>         state << { it.add(2); updateValue(it) }
>
>
>
> Is there an better way to do that?
>
>
> Cheers,
> p
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Agent and listener

Dierk König
oops, sorry, it is probably the DataflowQueue that you want to use here.

cheers
Dierk

Am 06.02.2014 um 22:24 schrieb Paolo Di Tommaso <[hidden email]>:

> Hi Dierk,
>
> I tried that, but Agent class does not provided the method whenBound nor wheneverBound.
>
> I'm using Gpars 1.0, maybe it is available in a newer version ?
>
>
> Cheers,
> Paolo
>
>
>
> On Thu, Feb 6, 2014 at 9:31 PM, Dierk König <[hidden email]> wrote:
> You may want to have a look at
>  whenBound {}
> and
>  wheneverBound {}
> on Agent.
>
> cheers
> Dierk
>
> Am 06.02.2014 um 19:01 schrieb Paolo Di Tommaso <[hidden email]>:
>
> > Dear all,
> >
> > I've a quick question about the Gpars Agent's listener.
> >
> > Is it possible to have an agent listener invoked when a message is sent to it?
> >
> > For example, look at this code:
> >
> >
> >         def state = new Agent<List>([])
> >         int i = 0
> >         state.addListener { a, b -> i++ }
> >
> >         state << { it.add(1) }
> >         state << { it.add(2) }
> >         state.await()
> >         assert state.val == [1,2]
> >         assert i == 2
> >
> > The second assertion will fail, since the event listener is invoked only when the agent state changes by calling the updateValue method (as far as I know).
> >
> > Is it safe doing something like this in order to force the listener invocation (without creating a new list instance)?
> >
> >         state << { it.add(1); updateValue(it) }
> >         state << { it.add(2); updateValue(it) }
> >
> >
> >
> > Is there an better way to do that?
> >
> >
> > Cheers,
> > p
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Agent and listener

Paolo Di Tommaso
Hi Dierk, 

well, I think the Agent works better in my use case, because I want to maintain and update an internal state. 

I solved invoking the updateValue and passing always the same object instance. I works nicely. 


Cheers,
Paolo



On Mon, Feb 10, 2014 at 2:24 PM, Dierk König <[hidden email]> wrote:
oops, sorry, it is probably the DataflowQueue that you want to use here.

cheers
Dierk

Am 06.02.2014 um 22:24 schrieb Paolo Di Tommaso <[hidden email]>:

> Hi Dierk,
>
> I tried that, but Agent class does not provided the method whenBound nor wheneverBound.
>
> I'm using Gpars 1.0, maybe it is available in a newer version ?
>
>
> Cheers,
> Paolo
>
>
>
> On Thu, Feb 6, 2014 at 9:31 PM, Dierk König <[hidden email]> wrote:
> You may want to have a look at
>  whenBound {}
> and
>  wheneverBound {}
> on Agent.
>
> cheers
> Dierk
>
> Am 06.02.2014 um 19:01 schrieb Paolo Di Tommaso <[hidden email]>:
>
> > Dear all,
> >
> > I've a quick question about the Gpars Agent's listener.
> >
> > Is it possible to have an agent listener invoked when a message is sent to it?
> >
> > For example, look at this code:
> >
> >
> >         def state = new Agent<List>([])
> >         int i = 0
> >         state.addListener { a, b -> i++ }
> >
> >         state << { it.add(1) }
> >         state << { it.add(2) }
> >         state.await()
> >         assert state.val == [1,2]
> >         assert i == 2
> >
> > The second assertion will fail, since the event listener is invoked only when the agent state changes by calling the updateValue method (as far as I know).
> >
> > Is it safe doing something like this in order to force the listener invocation (without creating a new list instance)?
> >
> >         state << { it.add(1); updateValue(it) }
> >         state << { it.add(2); updateValue(it) }
> >
> >
> >
> > Is there an better way to do that?
> >
> >
> > Cheers,
> > p
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email