A Simple Discrete-Event Simulation: Part 4

Here is code that actually runs. It creates a single entity, then updates it in a loop until the ending time is exceeded. The next steps will be to see how it works with multiple entities of the same kind and then with entities of different kinds.

This example is quite trivial so far. It doesn’t do a thing that’s meaningful but we can see the underlying mechanisms building up. It should get interesting quickly enough.

I also had to modify the getFirstItem method in the futureEventsQueue object. It has to test whether the returned array is of zero length, which indicates that the future events queue is empty.

Here’s the browser output, now that I’ve started to build something the user can see via HTML. The first line is a clock that gets updated with each event. The remainder is a block that logs all the events, which in this case involve only a single entity.


101 minutes

entity 1 created at time 0
entity 1 updated at time 11
entity 1 updated at time 21
entity 1 updated at time 31
entity 1 updated at time 41
entity 1 updated at time 51
entity 1 updated at time 61
entity 1 updated at time 71
entity 1 updated at time 81
entity 1 updated at time 91
entity 1 terminated at time 101

This entry was posted in Simulation and tagged , . Bookmark the permalink.

Leave a Reply