Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #18873 > unrolled thread

Best strategy to follow a state transition?

Started byBen Engbers <Ben.dot.Engbers@Be-Logical.dot.nl>
First post2012-09-21 13:09 +0200
Last post2012-09-22 00:08 +0200
Articles 5 — 5 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Best strategy to follow a state transition? Ben Engbers <Ben.dot.Engbers@Be-Logical.dot.nl> - 2012-09-21 13:09 +0200
    Re: Best strategy to follow a state transition? "John B. Matthews" <nospam@nospam.invalid> - 2012-09-21 07:36 -0400
    Re: Best strategy to follow a state transition? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-21 09:51 -0400
      Re: Best strategy to follow a state transition? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-09-21 10:26 -0700
    Re: Best strategy to follow a state transition? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-09-22 00:08 +0200

#18873 — Best strategy to follow a state transition?

FromBen Engbers <Ben.dot.Engbers@Be-Logical.dot.nl>
Date2012-09-21 13:09 +0200
SubjectBest strategy to follow a state transition?
Message-ID<505c4afc$0$6941$e4fe514c@news2.news.xs4all.nl>
Hi,

In my program, I know that at some moment the value of a variable X will
change from value 1 to value 2. I am not interested in the value of X
itself but the transition should trigger an event. Example, if the value
of a stock will reach a certain value, I want to sell but after that I'm
not interested in the value anymore (until it might reach another
break-value).

Of course I could program this as:
if oldvalue <= testvalue and newvalue > testvalue then
  do something;
  oldvalue = newvalue;
end
But suppose that there are a lot of variables that I need to watch and I
don't want to write a test for every one.

Wat is the best strategy to watch the transition?

Ben

[toc] | [next] | [standalone]


#18874

From"John B. Matthews" <nospam@nospam.invalid>
Date2012-09-21 07:36 -0400
Message-ID<nospam-80C602.07363821092012@news.aioe.org>
In reply to#18873
In article <505c4afc$0$6941$e4fe514c@news2.news.xs4all.nl>,
 Ben Engbers <Ben.dot.Engbers@Be-Logical.dot.nl> wrote:

> In my program, I know that at some moment the value of a variable X 
> will change from value 1 to value 2. I am not interested in the value 
> of X itself but the transition should trigger an event. Example, if 
> the value of a stock will reach a certain value, I want to sell but 
> after that I'm not interested in the value anymore (until it might 
> reach another break-value).
> 
> Of course I could program this as:
> if oldvalue <= testvalue and newvalue > testvalue then
>   do something;
>   oldvalue = newvalue;
> end
> But suppose that there are a lot of variables that I need to watch 
> and I don't want to write a test for every one.
> 
> Wat is the best strategy to watch the transition?

It sounds like you want a bound property:

<http://docs.oracle.com/javase/tutorial/javabeans/writing/properties.html>

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

[toc] | [prev] | [next] | [standalone]


#18876

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-09-21 09:51 -0400
Message-ID<k3hrdu$97b$1@dont-email.me>
In reply to#18873
On 9/21/2012 7:09 AM, Ben Engbers wrote:
> Hi,
>
> In my program, I know that at some moment the value of a variable X will
> change from value 1 to value 2. I am not interested in the value of X
> itself but the transition should trigger an event. Example, if the value
> of a stock will reach a certain value, I want to sell but after that I'm
> not interested in the value anymore (until it might reach another
> break-value).
>
> Of course I could program this as:
> if oldvalue <= testvalue and newvalue > testvalue then
>    do something;
>    oldvalue = newvalue;
> end
> But suppose that there are a lot of variables that I need to watch and I
> don't want to write a test for every one.

     If the program does not test whether a transition occurred
for some variable, then the program follows the same path whether
the transition occurs or not -- that is, in the absence of a test
the program is oblivious to the transition.

     There are probably a gazillion ways to arrange the pieces of
the tests, but no way to avoid making them.

> Wat is the best strategy to watch the transition?

     Insufficient information.  A few issues that would probably
matter a lot in the choice of an approach:

     - Are the variables "related" or "independent?"  For example,
       if X and Y have the same threshold and you know X is always
       greater than Y, then if X is below the threshold you can
       infer that Y is, too, and needn't make a separate test.

     - How many transitions do you care about?  If X rises above
       its threshold, do you care whether it then falls below it
       and rises again?

     - If you care about multiple transitions, how do you want to
       behave when X jiggles insanely in a narrow region containing
       its threshold?  Do you want a notification for every crossing,
       or only the first until a "significant" later excursion, or
       only the first until T seconds have elapsed, or ...?

     - How is the overall program structured?  Would you like to
       write `if(var.transitioned())' at points of interest, or
       would you like to "register an observer" to be notified of
       transition events, or would you like transition events to
       be queued when they occur and handled later, or what?

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#18884

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-09-21 10:26 -0700
Message-ID<Cs17s.126$l36.109@newsfe20.iad>
In reply to#18876
On 9/21/12 6:51 AM, Eric Sosman wrote:
> On 9/21/2012 7:09 AM, Ben Engbers wrote:
>> Hi,
>>
>> In my program, I know that at some moment the value of a variable X will
>> change from value 1 to value 2. I am not interested in the value of X
>> itself but the transition should trigger an event. Example, if the value
>> of a stock will reach a certain value, I want to sell but after that I'm
>> not interested in the value anymore (until it might reach another
>> break-value).
>>
>> Of course I could program this as:
>> if oldvalue <= testvalue and newvalue > testvalue then
>>    do something;
>>    oldvalue = newvalue;
>> end
>> But suppose that there are a lot of variables that I need to watch and I
>> don't want to write a test for every one.
>
>      If the program does not test whether a transition occurred
> for some variable, then the program follows the same path whether
> the transition occurs or not -- that is, in the absence of a test
> the program is oblivious to the transition.
>
>      There are probably a gazillion ways to arrange the pieces of
> the tests, but no way to avoid making them.
>
>> Wat is the best strategy to watch the transition?
>
>      Insufficient information.  A few issues that would probably
> matter a lot in the choice of an approach:
>
>      - Are the variables "related" or "independent?"  For example,
>        if X and Y have the same threshold and you know X is always
>        greater than Y, then if X is below the threshold you can
>        infer that Y is, too, and needn't make a separate test.
>
>      - How many transitions do you care about?  If X rises above
>        its threshold, do you care whether it then falls below it
>        and rises again?
>
>      - If you care about multiple transitions, how do you want to
>        behave when X jiggles insanely in a narrow region containing
>        its threshold?  Do you want a notification for every crossing,
>        or only the first until a "significant" later excursion, or
>        only the first until T seconds have elapsed, or ...?
>
>      - How is the overall program structured?  Would you like to
>        write `if(var.transitioned())' at points of interest, or
>        would you like to "register an observer" to be notified of
>        transition events, or would you like transition events to
>        be queued when they occur and handled later, or what?
>

On top of Eric's questions and advice, I'd like to point out that if you 
have two separate concerns, where one concern is maintaining the 
variables, and a separate concern (such as a new requirement that isn't 
related to the first concern) is to monitor those variables, one 
approach would be to use Aspect Oriented Programming, to put advice 
around all of the variable updates.

Alternatively, if your variables are only accessed by getters/setters or 
simple mutators, you can add your check directly after that update. Note 
that if you have multiple threads, you'll need to decide how to handle 
atomicity of these updates, as well as how best to *dispatch* the 
transitions.

[toc] | [prev] | [next] | [standalone]


#18891

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2012-09-22 00:08 +0200
Message-ID<k3ioh9$a21$1@dont-email.me>
In reply to#18873
On 21/09/2012 13:09, Ben Engbers allegedly wrote:
> Hi,
> 
> In my program, I know that at some moment the value of a variable X will
> change from value 1 to value 2. I am not interested in the value of X
> itself but the transition should trigger an event. Example, if the value
> of a stock will reach a certain value, I want to sell but after that I'm
> not interested in the value anymore (until it might reach another
> break-value).
> 
> Of course I could program this as:
> if oldvalue <= testvalue and newvalue > testvalue then
>   do something;
>   oldvalue = newvalue;
> end
> But suppose that there are a lot of variables that I need to watch and I
> don't want to write a test for every one.
> 
> Wat is the best strategy to watch the transition?
> 
> Ben

Listeners.

<http://docs.oracle.com/javase/tutorial/uiswing/events/changelistener.html>

See also the (somewhat older)
<http://docs.oracle.com/javase/7/docs/api/java/util/Observable.html>.

-- 
DF.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web