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


Groups > comp.lang.java.programmer > #18884

Re: Best strategy to follow a state transition?

From Daniel Pitts <newsgroup.nospam@virtualinfinity.net>
Newsgroups comp.lang.java.programmer
Subject Re: Best strategy to follow a state transition?
References <505c4afc$0$6941$e4fe514c@news2.news.xs4all.nl> <k3hrdu$97b$1@dont-email.me>
Message-ID <Cs17s.126$l36.109@newsfe20.iad> (permalink)
Date 2012-09-21 10:26 -0700

Show all headers | View raw


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.

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web