Senate bus


According to Allen Downey in his text The Little Book of Semaphores, the Senate bus example was inspired by the Senate Bus at Wellesley College. Passengers come to a bus stop to catch the Senate bus. The bus can hold 50 passengers. When the bus stops at the bus stop, the waiting passengers board. If the bus fills up, then any passengers who cannot board, must wait until the bus shows up again. Likewise, any passenger who arrives at the stop during the time the bus is boarding also must wait until the next cycle.


The root class for this example creates the bus stop, the bus, and the passengers all typed as separate.

The bus stop, modeled by class STATION has features that can be used by the bus and by passengers. Access to thesefeatures is restricted to the appropriate client classes through the clients part of the feature clause. Clients of type PASSENGER can access {STATION}.pass_enter. A client of type BUS can access {STATION}.bus_enter, {STATION}.pick_up, and {STATION}.leave, as well as a status feature {STATION}.bus_is_waiting and two passenger queues {STATION}.waiting_list and {STATION}.checked_in_list.

The lifecycle of a passenger is simple: enter the bus stop. This is accomplished by making a separate call to {STATION}.enter and passing Current (the passenger object itself) as an argument.

The lifecycle of the bus is slightly more complex: enter the bus stop, pick up passengers, leave the bus stop, wait for a short time. The bus repeats this sequence forever. The routines in class BUS for entering the bus stop, picking up passengers, and leaving the bus stop all accept as an argument the separate bus stop object (a_station: separate STATION) and make a separate call to the corresponding routine in STATION.

Features of the bus stop (class STATION) manage the queues for waiting and checked-in passengers and whether a bus is at the bus stop. Passengers are added to the waiting queue when they arrive at the station. When the bus leaves the station, any waiting passengers are transferred to the checked-in queue. When the bus arrives at the station, the passengers on the checked-in queue are allowed to board the bus (up to the first 50 passengers, that is), and the boarding passengers are then removed from the checked-in queue.