
A discrete event simulation package for Java
With applications in computer systems modelling.
Fred Howell and Ross McNab
Abstract
simjava is a toolkit for building working models of
complex systems. It is based around a discrete
event simulation kernel and includes facilities for
representing simulation objects as animated icons
on screen. Simjava simulations may be incorporated
as ``live diagrams'' into web documents.
This paper describes the design, component model,
applications and future of simjava.
Introduction
Our motivation for writing simulations in Java
was to allow ``live diagrams'' to be incorporated into
web pages. We had been developing C++ based visual
simulations of computer architectures and parallel software
systems as part of the HASE package (Ibbett, Heywood and Howell 1995),
and saw the emergence of Java as an opportunity to make simulation
models more widely available and easily accessible.
Like many of the other groups who have written
Java simulation libraries, we based the system on an existing
C++ simulation package.
The design aim was for simjava to be a set of
``simulation foundation classes'' useful for constructing and animating
discrete event simulation models. The computer architecture and software
models we were interested in required the full flexibility of a
programming language for expressing behaviours of components, rather
than queueing models. Simjava is actually a collection of three
packages, eduni.simjava, eduni.simanim and
eduni.simdiag. eduni.simjava is a package
for building stand alone text only java simulations, which
produce a trace file as the output by default.
eduni.simanim is tightly integrated with
the text only simulation package, and provides a skeleton applet for
easily building a visualisation of a simulation. eduni.simdiag
is a collection of JavaBeans based classes for displaying simulation
results.
Using a programming language to build models (rather than building
them graphically) has the advantage that complex regular interconnections
are straightforward to specify, which was crucial for some of the networks
we were interested in simulating. It also allows the inclusion of
existing libraries of code to build simulations.
The simjava package has been designed for simulating fairly
static networks of active entities which communicate by sending
passive event objects via ports. This model is appropriate for
hardware and distributed software systems modelling. The next
section describes some alternative approaches.
Other java simulation environments
There are now several other Java based discrete simulation
environments available. Like simjava they are
mostly Java versions of existing simulation languages,
so all implement different flavours of simulation.
The advice is to use one which you're used to and which
fits the application. In particular most of the below
provide better direct support for queue modelling and
statistics classes than Simjava. Page (1997) maintains
a well structured survey of web simulation environments.
Simkit (Buss and Stork 1996,1997) provides a set of
MODSIM II influenced Java classes, with no model animation
but a better set of statistics classes than Simjava provides.
JavaSim (Little 1997) is a text only Java version of C++SIM, itself based
on SIMULA. JSIM (Nair 1997) supports a good graphical environment
for displaying queues, and uses a Java database for storing results.
DEVS-Java (Zeigler 1997) is a Java port of the DEVS-C++
modelling environment.
Goad (1997) describes an interesting approach to
the problems of building simulations out of components, using an
ML like language which unifies the behaviour and connections between
components.
An good example of a Java model built without using a simulation
package is a model of a virtual memory system (Chakraborty and Bhowmik 1997).
Digital Workshop (Lynch and Fishwick 1996) has graphical model construction
for simulating simple digital circuits, as well as design animation.
The design
A simjava simulation contains a number
of entities each of which runs in parallel in its
own thread. An entity's behaviour is encoded in Java
using its body() method. Entities have access
to a small number of simulation primitives:
- sim_schedule() sends event objects to other entities
via ports.
- sim_hold() holds for some simulation
time.
- sim_wait() waits for an event object
to arrive.
- sim_select() selects events from
the deferred queue.
- sim_trace() writes a timestamped
message to the trace file.
This model is based on the HASE++ simulation library for
C++ (Howell 1996b), which in turn was based on the SIM++ library
from Jade Simulations Inc (Jade 1992). It is appropriate for
any system which can be described as a network of communicating
objects, and has been applied to communications protocols,
parallel software modelling and computer architectures.
How to build a simulation
The building blocks of a simulation are entities.
Modelling a system involves extending class sim_entity
to include the behaviour of each different type of component.
For example, the following class describes a source entity
which generates 100 events on its output port.
class Source extends Sim_entity {
private Sim_port out;
public Source(String name) {
super(name); out = new Sim_port("out"); add_port(out);
}
public void body() {
for (int i=0; i<100; i++) {
sim_schedule(out,0.0,1);
sim_hold(10.0);
}
}
}
A complete simulation is built by providing a main()
method which creates all the entities and joins them together.
Running the simulation produces a trace file of all
the events which occurred, along with any results the
entities have collected about their progress.
A set of statistics classes are included in
the package for generating and analysing distributions.
How it works
The basic sequential discrete event simulation algorithm
is as follows. A central object Sim_system
maintains a timestamp ordered queue of future events. Initially
all entities are created and their body() methods are
run. When an entity calls a simulation function (say
sim_hold(10.0)) the Sim_system object
halts that entity's thread and places an event on the future queue
to signify that the hold will complete at (current simulation
time + 10.0). When all entities have halted, Sim_system
pops the next event off the queue, advances the simulation
time accordingly and restarts entities as appropriate. This continues
until no more events are generated.
If the Java virtual machine support native threads, then all
entities starting at exactly the same simulation time
may run in parallel.
simanim - turning simulations into applets
simanim is a companion package to simjava
which may be used if a graphical representation and animation
of a simulation model is desired. It is based on
earlier work on the HASE simulation system (Ibbett, Heywood and Howell 1995).
To build a simple animated model the user gives a .gif image
and (x,y) coordinates for each entity. Providing extra .gif images
allows the entity's icon to change according to its internal state.
Other entity parameters may be displayed alongside the icon, and updated
as the simulation runs.
Two methods of simanim create the animation:
anim_init() is overridden to add buttons,
controls and parameter fields, e.g.:
anim_layout() is extended to position
the entity icons and draw ports, e.g.:
simanim provides a standard panel for controlling
the simulation run:
As the simulation runs, the current simulation time is displayed,
events are shown moving down links and entity icons
switch according to their state.
simjava and simanim are standard java packages,
and may be used alongside other java packages
for building a simulation application.
Both simanim and simjava
run concurrently; the output of the simjava simulation
code is fed into simanim to produce the animation.
The simjava entity interaction model
In simjava event objects are passed to other entities via
ports using sim_schedule(). They are automatically queued,
and retrieved as required by the receiver using sim_select()
and sim_wait(). sim_select() is used to select
from events which have already arrived, and sim_wait() waits
for the next future event.
This is subtley different from a message passing interface, such as MPI.
MPI_Send() is similar to sim_schedule(),
but MPI_Recv() is like a combination of sim_select()
and sim_wait(). The other difference from message
passing interaction models is that in simjava all
events are globally sorted by simulation timestamp to
ensure that messages never arrive out of order.
JavaBeans uses another style of interaction. An object
wishing to receive events implements the EventListener
interface, providing a method for handling input events,
for example handleAnEvent(AnEventObject aeo). To send
events, an object maintains a vector of destination
EventListener objects. It calls the handleAnEvent()
method for all the elements of the vector to send an event onwards.
This is similar to SGI's OpenInventor method for interaction.
The basis is the field. An object may have several input
and/or output fields. An input field of one object
may be have its value set from an output field of another
using connectFrom(). A method evaluate()
sets the values of an object's output fields. When the value
on an input field changes, the method inputChanged()
is called. This system is integrated with the OpenInventor
3D graphics system, and allows animations to be built up by
connecting engine objects to transformation objects
in the scene graph. A similar method is used within
the OpenInventor-inspired VRML-2, which uses ROUTEs
to connect standard engines such as PositionInterpolator,
and Sensor objects to generate events.
simdiag for displaying results
The extended event handling model of JDK 1.1 offers a real
opportunity to make component software a reality. The idea
is that the interface to software `components' (or `beans') is restricted
to a stream of user defined events. These components
can be wired together on the fly by calling an addEventListener()
method.
This is ideal for processing simulation results, where there
is a trace 'stream' which can be tapped into and displayed.
The simdiag package provides two types of bean,
those which listen to a stream of events during a simulation
(TraceEventListeners), and those which plot graphs (GraphEventListeners).
The figure below illustrates the simulation producing a stream
of events which are displayed in a timing diagram and then forwarded
to a `TraceSaver' for storage to a file.

The timing diagram bean (illustrated below) shows how the state of
each entity changes over time.

The graph bean displays any 2D data set, and can be updated dynamically
while the simulation is running.

Applications
The first version of simjava was released in September 1996.
Since then there have been several projects making use of it and
new releases for the new versions of Java (JDK1.1).
At Edinburgh University these include:
- Distributed agreement protocols for concurrency control.
This project has been looking at modelling failure
scenarios in networks, with links being broken on the
fly and the algorithms having to rollback. Having
a simulation environment in Java has been particularly
useful for this project as one of the protocols
investigated (supplied by Microsoft) was specified in Java.
- Computer architecture interactive learning. This project
has been including active diagrams of caches, the DLX pipeline
and memory systems as part of an electronic book for teaching
computer architecture.
- Interconnection network simulations.
This project has been investigating visual modelling
of multiprocessor interconnection networks, varying
workloads to see the effects on the hardware.
- Web cache modelling.
The InterSim project at the Edinburgh Parallel Computing Centre
aims to do large scale simulations of internet traffic using
parallel supercomputers, and is currently using simjava as
a visual front end.
- A VRML version of simanim (Dobbie,1997) was developed, ``3DAnim''
in which entities were represented by 3D VRML objects rather
than .gif icons. The only change needed to the user's simulation
code is that a Z coordinate is required for each entity. Using
3D space allows for less visually cluttered models, and also
allows building models of systems which are logically three
dimensional (such as the Cray T3D's torus interconnection).
The figure below shows a 3D version of an omega network.
- Producing MIDI from trace files. Just listening to a car
engine can provide many clues as to how well it is working;
simulations are also complex systems so to investigate whether
it would be helpful to use audio, some experiments have taken
trace file data and converted them to MIDI format, with each
event being assigned an instrument and pitch (a similar
system was part of the Pablo performance environment). There is currently
no standard Java MIDI api, so the conversion has to be done
externally from the applet.
Conclusion and future work
simjava provides the basic elements necessary for building
animated discrete event simulations in Java, and is designed to be
flexible and extendable.
Where simulation speed is paramount, a C++ based simulation package
currently gives a factor eight improvement in simulation run times
(McNab and Howell 1996).
The software package is freely available in full source
code from the home page (Howell 1997), and it is hoped that it will
prove useful as a basis for java simulation projects.
It has been used as the basis for distributed simjava
(Page, Moose and Griffin 1997), a version of SimJava which uses Java
RMI to distribute the entities of a simulation onto different computers.
Interesting areas for future java based simulation packages
to develop are cooperative use and construction of simulations,
component reuse using the JavaBeans model,
distributed simulations, and more dynamic interaction and
interpretation of simulation results.
References
- [1]
McNab, R. and Howell, F.W. 1996.
``Using Java for Discrete Event Simulation''
in proc. Twelfth UK Computer and Telecommunications Performance
Engineering Workshop (UKPEW), Univ. of Edinburgh, 219-228
- [2] Howell, F.W. 1996b. ``Hase++ : a discrete event simulation library for C++.''
- [3] Ibbett, R.N., Heywood,P.E. and Howell,F.W.. 1995. "Hase : a flexible toolset for computer architects." The Computer Journal, 38(10):755-764.
- [4] Jade Simulations International Corp. 1992. ``Sim++ user manual.''
- [5] Howell, F.W. 1997. The simjava home page.
- [6] Little, M.C. 1997. "The C++SIM home page (includes JavaSim notes)".
- [7] Page, E. 1997. ``A Survey of Web based simulation''.
- [8] Goad, C. 1997, ``A Language-Level Attack on Compositional
Simulation'', Inflorescence Inc.
- [9] Sun Microsystems. 1997. JavaBeans specification.
- [10] Buss, A.H. and Stork, K.A. 1996. ``Discrete Event Simulation on the World
Wide Web Using Java'' pp. 780-785. Proceedings of the 1996 Winter
Simulation Conference. D. Morrice and J. Charnes, Eds., Coronado, CA, 8-11 December.
- [11] Buss, A.H. and Stork, K.A. 1997.
Simkit home page.
- [12] Nair, R.S. 1997. ``JSIM: A Java based query driven simulation and
animation environment''. MSc Thesis, University of Georgia.
(JSIM Home Page)
- [13] Page, E.H., Moose, R.L. and Griffin, S.P. 1997.
``Web-Based Simulation in Simjava using Remote
Method Invocation'', Submitted to: 1997 Winter Simulation Conference, Atlanta, GA, 7-10
December. (see home page)
- [14] Lynch, D. and Fishwick, P. 1996. ``Digital Workshop'',
- [15] Dobbie, M. 1997. ``3D Animation of Computer Architectures'',
4th Year Honours Report, Dept of Computer Science, University of Edinburgh.
- [16] Chakraborty, A. and Bhowmik, J. 1997. ``VM Memory simulator''
- [17] Zeigler, B.P. 1997. ``DEVS-JAVA modelling and simulation environment'',