Best strategy to follow a state transition?

B

Ben Engbers

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
 
J

John B. Matthews

Ben Engbers said:
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>
 
E

Eric Sosman

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?
 
D

Daniel Pitts

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.


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.
 
D

Daniele Futtorovic

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>.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top