23. Building a Simulation

Part of CS:2820 Object Oriented Software Development Notes, Fall 2015
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

 

Let's Build a Simulation

We have a Java program that builds an internal representation of a road network on the computer, and we have a pending event set mechanism (actually two of them), so it's time to marry the two and create a simulation of a road network.

Of course, we need to add some things to our road network to do this:

Vehicle Sources and Destinations

One way to add vehicles to a highway network is to add some new classes of intersection, vehicle sources and destinations. In the real world, these might model roads coming into the highway network from the outside world and roads leaving the highway network, or they might model parking lots.

In our model, we'll keep things simple with the following two new intersection types:

Adding source intersections to the program

We need to add parsing support for source intersections. Step 1 in this venture is to just parse the details of the source and store the parameter values. This is straightforward code that imitates the kind of code we've already written. Half of this code is copied from other intersection types, with calls to SyntaxSupport methods to pick up the parameters and some code to check the sanity of the values.

class Source extends Intersection {
        int carCount = 99;
        float arrivalInterval = 99.99f;

        // initializer
        public Source( Scanner sc, String name ) throws IllegalName {
                this.name = name;

                // scan and process one intersection
                if (RoadNetwork.findIntersection( name ) != null) {
                        Errors.warning( toString() + " -- redefined." );
                        throw new IllegalName();
                }
                carCount = ScanSupport.nextInt(
                        sc, );
                        () -> StopLight.this.toString()
                );
                arrivalInterval = ScanSupport.nextFloat(
                        sc, );
                        () -> StopLight.this.toString()
                );
                ScanSupport.lineEnd(
                        sc,
                        () -> StopLight.this.toString()
                );

                // check sanity of field values
                if (carCount < 1) {
                        Errors.warning(
                                Road.this.toString() +
                                " -- never produces any cars?"
                        );
                }
                if (arrivalInterval < 0.0f) {
                        Errors.warning(
                                Road.this.toString() +
                                " -- negative time interval?"
                        );
                }
        
                // BUG: schedule the first arrival
        }

        // simulation methods
        void arrivalEvent() {
                // BUG new vehicle arrives
        }

        // other methods
        public String toString() {
                return (
                        "Intersection " +
                        name +
                        " Source " +
                        carCount +
                        " " +
                        arrivalInterval
                );
        }
}

The details of the simulation of an arrival are all missing above, replaced by bug notices. We need to flesh this out.

In contrast, the Sink subclass of Intersection is relatively easy. We won't give the code here to parse vehicle sinks.