JMSL Tutorial: Collections

We now want to break away from strict sequential launching of children, and use a Behavior to choose which child to launch each time the SequentialCollection repeats. A custom Behavior is plugged into a SequentialCollection with a call to
setBehavior(Behavior someBehavior)
For example, JMSL's UniformRandomBehavior chooses the next child at random. You would set this behavior like so:
myCollection.setBehavior(new UniformRandomBehavior());
NOTE: To reset the behavior to strictly sequential, make a call to
myCollection.setBehavior(null)
NOTE: When acting with a Behavior, the SequentialCollection's repeatCount determines the total number of children launched, as opposed to the number of times ALL the children are launched in sequence.
 

Any class that implements the Behavior interface can be plugged into a SequentialCollection to control the order of execution. Let's look at that interface now:

public Composable choose(SequentialCollection col)
Let's see how UniformRandomBehavior is implemented:
// Chooses one child with uniformly random distribution
public Composable choose(SequentialCollection col) {
     return (Composable)col.get(JMSLRandom.choose(col.size()));
}
We define a Behavior exactly like this from scratch for this example (see source)

You need a Java-enabled browser to view this applet.
 
 

Here is the complete source code for this applet.

While we're on the subject of randomness, let's bring your attention to the line that reads

JMSLRandom.randomize();
Call that once in your application to initialize JMSLRandom's random number generator with the real time clock, otherwise your random sequences will be identical every time you run your application (sometimes good for debugging, but generally not desirable).

Next we will look at ParallelCollections.
 
Previous Tutorial Index Tutorial Contents Next

  (C) 1997 Phil Burk and Nick Didkovsky, All Rights Reserved
  JMSL is based upon HMSL (C) Phil Burk, Larry Polansky and David Rosenboom.