Best strategy to follow a state transition?

Discussion in 'Java' started by Ben Engbers, Sep 21, 2012.

  1. Ben Engbers

    Ben Engbers Guest

    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
    Ben Engbers, Sep 21, 2012
    #1
    1. Advertising

  2. In article <505c4afc$0$6941$4all.nl>,
    Ben Engbers <> 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>
    John B. Matthews, Sep 21, 2012
    #2
    1. Advertising

  3. Ben Engbers

    Eric Sosman Guest

    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
    d
    Eric Sosman, Sep 21, 2012
    #3
  4. Ben Engbers

    Daniel Pitts Guest

    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.
    Daniel Pitts, Sep 21, 2012
    #4
  5. 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.
    Daniele Futtorovic, Sep 21, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Divyang M
    Replies:
    9
    Views:
    623
    Divyang M
    May 18, 2005
  2. Divyang M
    Replies:
    1
    Views:
    569
    Jerzy Gbur
    May 15, 2005
  3. hari_krishna

    FSM State transition coverage

    hari_krishna, Aug 22, 2006, in forum: VHDL
    Replies:
    0
    Views:
    934
    hari_krishna
    Aug 22, 2006
  4. Robert Winsor

    Sub-bit transition state?

    Robert Winsor, Oct 28, 2006, in forum: VHDL
    Replies:
    1
    Views:
    467
    Robert Winsor
    Oct 28, 2006
  5. Ben Engbers

    State transition

    Ben Engbers, Sep 21, 2012, in forum: Java
    Replies:
    3
    Views:
    309
    Ben Engbers
    Sep 21, 2012
Loading...

Share This Page