# Track
Date: 2020-11-15

de.hems.trafficsim

## Class Track

- java.lang.Object
- - java.util.Observable
  - - de.hems.trafficsim.Track

    public class Track
    extends java.util.Observable

Main model class of TrafficSim. Represents a round course containing vehicles.

### Field Summary

Fields
`protected float` `brakeProb` currently configured brake probability for all vehicles
`protected float` `delayedAvg` average over all velocities in the simulation, ignoring the first ten steps
`protected int` `historyLength` length of the history kept
`protected float` `lastAvg` average over all velocities in the last step
`protected java.util.concurrent.Semaphore` `listSemaphore` semaphore protecting the vtrlist
`protected float` `maxVelocity` currently configured max velocity for all vehicles
`protected float` `overallAvg` average over all velocities in the simulation
`protected long` `steps` counter for executed simulation steps
`protected float` `sumAvgMemory` sum of all vehicle speeds during the simulation, used for average calculation
`protected float` `sumDelAvgMemory` sum of all vehicle speeds during the simulation ignoring the first ten steps, used for average calculation
`protected float` `trackLength` length of the track
`protected java.util.List<Vehicle>` `vehicles` list a vehicles on the track
`protected java.util.List<java.util.List<VehicleTimeRecord>>` `vtrList` list of resulting time records of the simulation
`protected int` `waitTime` current configured wait time between two simulation steps

### Constructor Summary

Constructors
Constructor and Description
`Track(int numberVehicles, float trackLength, float brakeProb, float maxVelocity, int waitTime, int historyLength)` Constructor for a new Track.

### Method Summary

All Methods Instance Methods Concrete Methods
`float` `avg_step(int step)` Returns the average velocity of the given simulation step.
`protected java.util.List<Vehicle>` `createVehiclesList(int numberVehicles)` Utility function to add vehicles to the Track.
`float` `getDelayedAvg()` Getter for delayedAvg
`int` `getHistoryLength()` Getter for historyLength
`float` `getLastAvg()` Getter for lastAvg
`java.util.concurrent.Semaphore` `getListSemaphore()` Getter for listSemaphore
`float` `getOverallAvg()` Getter for overallAvg
`long` `getSteps()` Getter for steps
`float` `getTrackLength()` Getter for trackLength
`java.util.List<Vehicle>` `getVehicles()` Getter for vehicles
`java.util.List<java.util.List<VehicleTimeRecord>>` `getVtrList()` Getter for vtrList
`void` `setBrakeProb(float brakeProb)` Update the brake probability of all vehicles.
`void` `setMaxVelocity(float maxVelocity)` Update the maximum velocity of all vehicles.
`void` `setWaitTime(int waitTime)` Update the wait time of the simulation.
`void` `timeElapse()` Calculates on simulation step ahead and then waits for the configured wait time.
`protected void` `update_avg()` Utility function which updates the averages values with the results from the last simulation step.

### Methods inherited from class java.util.Observable

`addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### vehicles

    protected java.util.List<Vehicle> vehicles

list a vehicles on the track

#### vtrList

    protected java.util.List<java.util.List<VehicleTimeRecord>> vtrList

list of resulting time records of the simulation

#### trackLength

    protected float trackLength

length of the track

#### sumAvgMemory

    protected float sumAvgMemory

sum of all vehicle speeds during the simulation, used for average calculation

#### sumDelAvgMemory

    protected float sumDelAvgMemory

sum of all vehicle speeds during the simulation ignoring the first ten steps, used for average calculation

#### historyLength

    protected int historyLength

length of the history kept

#### overallAvg

    protected float overallAvg

average over all velocities in the simulation

#### delayedAvg

    protected float delayedAvg

average over all velocities in the simulation, ignoring the first ten steps

#### lastAvg

    protected float lastAvg

average over all velocities in the last step

#### waitTime

    protected int waitTime

current configured wait time between two simulation steps

#### maxVelocity

    protected float maxVelocity

currently configured max velocity for all vehicles

#### brakeProb

    protected float brakeProb

currently configured brake probability for all vehicles

#### steps

    protected long steps

counter for executed simulation steps

#### listSemaphore

    protected java.util.concurrent.Semaphore listSemaphore

semaphore protecting the vtrlist

### Constructor Detail

#### Track

    public Track(int numberVehicles,
                 float trackLength,
                 float brakeProb,
                 float maxVelocity,
                 int waitTime,
                 int historyLength)

Constructor for a new Track.

Parameters:
  `numberVehicles` - number of vehicles on the Track
  `trackLength` - length of the new Track
  `brakeProb` - probability of a vehicle to suddenly brake without reason
  `maxVelocity` - maxmimum velocity of the vehicles
  `waitTime` - time between two simulation steps to slow down the simulation artificially
  `historyLength` - length of the history to keep

### Method Detail

#### getVtrList

    public java.util.List<java.util.List<VehicleTimeRecord>> getVtrList()

Getter for vtrList

Returns:
  vtrList

#### getOverallAvg

    public float getOverallAvg()

Getter for overallAvg

Returns:
  overallAvg

#### getLastAvg

    public float getLastAvg()

Getter for lastAvg

Returns:
  lastAvg

#### getDelayedAvg

    public float getDelayedAvg()

Getter for delayedAvg

Returns:
  delayedAvg

#### getVehicles

    public java.util.List<Vehicle> getVehicles()

Getter for vehicles

Returns:
  vehicles

#### getTrackLength

    public float getTrackLength()

Getter for trackLength

Returns:
  trackLength

#### getSteps

    public long getSteps()

Getter for steps

Returns:
  steps

#### getListSemaphore

    public java.util.concurrent.Semaphore getListSemaphore()

Getter for listSemaphore

Returns:
  listSemaphore

#### getHistoryLength

    public int getHistoryLength()

Getter for historyLength

Returns:
  historyLength

#### createVehiclesList

    protected java.util.List<Vehicle> createVehiclesList(int numberVehicles)

Utility function to add vehicles to the Track.

Parameters:
  `numberVehicles` - number of vehicles to add
Returns:
  filled list with vehicles

#### setWaitTime

    public void setWaitTime(int waitTime)

Update the wait time of the simulation.

Parameters:
  `waitTime` - new wait time in ms

#### setBrakeProb

    public void setBrakeProb(float brakeProb)

Update the brake probability of all vehicles.

Parameters:
  `brakeProb` - new brake probability

#### setMaxVelocity

    public void setMaxVelocity(float maxVelocity)

Update the maximum velocity of all vehicles.

Parameters:
  `maxVelocity` - new maximum velocity

#### timeElapse

    public void timeElapse()

Calculates on simulation step ahead and then waits for the configured wait time.

#### avg_step

    public float avg_step(int step)

Returns the average velocity of the given simulation step.

Parameters:
  `step` - index of the step in the history list
Returns:
  average velocity

#### update_avg

    protected void update_avg()

Utility function which updates the averages values with the results from the last simulation step.