Class RailTrack

java.lang.Object
com.amalgamasimulation.trains.RailTrack

public class RailTrack extends Object
Rail track represents a bi-directional section of rail road between any two rail nodes. All rail tracks are bi-directional and thus contain references to two graph arcs: forward arc and backward arc. Rail track is considered to be directed along its forward arc. The beginning of the rail track is at the same position as the beginning of the track's forward arc.
Author:
Andrey Malykhanov, Andrey Korotin
See Also:
  • Constructor Details

    • RailTrack

      protected RailTrack(String name, Graph<RailNode,RailArc>.Arc forwardArc, Graph<RailNode,RailArc>.Arc backwardArc)
      Creates a new instance of a rail track using the forward and backward arcs specified. All rail tracks are bidirectional.
      Parameters:
      name - name of the rail track
      forwardArc - forward arc
      backwardArc - backward arc
  • Method Details

    • afterHeadEntered

      protected void afterHeadEntered(Train train)
      Callback that is called immediately after head of a train enters this track.
      Parameters:
      train - train that enters the track
    • beforeTailExited

      protected void beforeTailExited(Train train)
      Callback that is called right before tail of a train exits this track.
      Parameters:
      train - train that exits the track
    • book

      public void book(BookingSlot bookingSlot)
      Inserts the specified BookingSlot into the booking sequence.

      The slot is placed into the sequence according to its starting time.

      Parameters:
      bookingSlot - the booking slot to insert into the sequence
    • getCurrentTimeBooker

      public Object getCurrentTimeBooker(double atTime)
      Returns the booker associated with the booking slot that includes the specified time.

      If no slot covers the given point in time, this method returns null.

      Parameters:
      atTime - the time for which to retrieve the active booker
      Returns:
      the booker object at the specified time, or null if none is found
    • getFreeMovementGaps

      public IntervalSet getFreeMovementGaps(BiPredicate<RailTrack,Object> considerBookedTracks)
      Returns an IntervalSet representing the time intervals during which this track is not booked (i.e., free for movement).

      The result is computed by excluding all intervals of bookings that satisfy the given BiPredicate.

      Parameters:
      considerBookedTracks - a predicate determining which bookings should be considered as occupying the track; the first argument is this RailTrack, and the second is the booker object
      Returns:
      an IntervalSet representing free (unbooked) time intervals
    • getBookingSlots

      public List<BookingSlot> getBookingSlots(BiPredicate<RailTrack,Object> considerBookedTracks)
      Returns a list of BookingSlots that satisfy the specified condition.

      This method filters the existing booking sequence according to the given BiPredicate, which determines whether a booking made by a particular booker on this RailTrack should be considered.

      Parameters:
      considerBookedTracks - a predicate that tests whether a booking should be included; the first argument is this RailTrack, and the second is the booker object
      Returns:
      a list of booking slots that satisfy the specified condition
    • getName

      public String getName()
      Returns the name of this train.
      Returns:
      name of this train
    • getPolyline

      public Polyline getPolyline()
      Returns a polyline representing the forward trajectory along the track.
      Returns:
      polyline
    • getLength

      public double getLength()
      Returns the length of the track, i.e. length of the polyline representing the forward or backward trajectory along the track, in pixels.
      Returns:
      length of the track
    • getTrains

      public List<Train> getTrains()
      Returns the list of trains occupying (maybe partially) this track. The list is sorted by trains' offset from the beginning of this track.
      Returns:
      list of trains occupying (maybe partially) this track
    • isFree

      public boolean isFree()
      Checks if there are no trains on this track.
      Returns:
      true if there are no trains on this track, false otherwise
    • getForwardArc

      public Graph<RailNode,RailArc>.Arc getForwardArc()
      Returns the graph arc corresponding to forward direction of this track.
      Returns:
      forward graph arc
    • getBackwardArc

      public Graph<RailNode,RailArc>.Arc getBackwardArc()
      Returns the graph arc corresponding to backward direction of this track.
      Returns:
      backward graph arc
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getBookingSequence

      public BookingSequence getBookingSequence()
      Returns the BookingSequence associated with this track.

      The booking sequence contains all BookingSlots representing reservations or occupied time intervals on this track.

      Returns:
      the BookingSequence that manages the booking slots for this track