Simulated components in jasima are instances of Java classes derived (directly or indirectly) from jasima.core.simulation.SimComponent
. SimComponent
is an interface which is normally not used directly. Instead simulation components are usually implemented by classes inherited from jasima.core.simulation.SimComponentBase
. SimComponentBase
already offers meaningful default implementations of all methods. While a SimComponentBase
can be added to a simulation, it doesn’t do anything. To specify the behaviour of a component, you usually override a single or multiple of its lifecycle methods (usually at least init
). Lifecycle methods are called by the simulation at the occurrence of a certain lifecycle event. These methods are available:
Event |
Method to Override |
Description |
INIT |
init()
|
simulation initializing |
SIM_START |
simStart()
|
usually used to schedule initial events; called after init() |
RESET_STATS |
resetStats()
|
called upon a statistics reset; there is at least one statistics reset at the beginning of a simulation run |
SIM_END |
simEnd()
|
called on simulation end to do any clean-up |
DONE |
done()
|
additional event triggered after simEnd() but before produceResults() |
ProduceResults |
produceResults(Map<String, Object>)
|
called to add the component’s results to the simulation results |
Lets now see how all of this is used to implement the event-oriented version of the M/M/1 model in MM1ModelEvents.java
. The model consists of only a single component, the class MM1ModelEvents
. Besides the static main()
method to run the model (see above), it is defining attributes to hold parameters and additional variables as required during a simulation run. We the override the two lifecycle methods init()
and produceResults(Map<String, Object>)
. To implement the desired behaviour of our M/M/1 model, the class contains three additional methods that are called during the simulation run at the right moments to handle the component-specific events.
public class MM1ModelEvents extends SimComponentBase { (1)
1 |
MM1ModelEvents is extending jasima.core.simulation.SimComponentBase |
Parameters and additional variables are standard Java fields. For parameters there are usually getter and setter methods (not used in the example to keep it as short as possible).
// parameters
private int numJobs = 1000;
private double trafficIntensity = 0.85;
// fields used during simulation run
private Q<Integer> q; // queue for waiting jobs
private DblSequence iats; // inter-arrival times
private DblSequence serviceTimes; // service times
private Integer currentJob; // job currently processed by the server
private int numServed = 0; // counter for number of serviced jobs
private int numCreated = 0; // counter for total number of jobs created
The init()
method performs anything to initialize the component. In the example it creates the queue for waiting jobs, initializes the DblSequence
objects for the inter-arrival and service times. In addition we also schedule the first event, which is the arrival of the first job.
@Override
public void init() {
// create new queue
q = new Q<>();
// initialize random number streams/sequences
iats = initRndGen(new DblExp(INTER_ARRIVAL_TIME), "arrivals");
serviceTimes = initRndGen(new DblExp(INTER_ARRIVAL_TIME * trafficIntensity), "services");
// schedule first arrival
scheduleIn(iats.nextDbl(), this::createNext);
}