BusSchedule.java

  1. package org.opentrafficsim.road.gtu.lane.tactical.pt;

  2. import java.util.LinkedHashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.Set;

  6. import org.djunits.value.vdouble.scalar.Duration;
  7. import org.djunits.value.vdouble.scalar.Time;
  8. import org.djutils.exceptions.Throw;
  9. import org.opentrafficsim.core.gtu.GtuType;
  10. import org.opentrafficsim.core.network.NetworkException;
  11. import org.opentrafficsim.core.network.Node;
  12. import org.opentrafficsim.core.network.route.Route;

  13. /**
  14.  * <p>
  15.  * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  16.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  17.  * </p>
  18.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  19.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  20.  * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
  21.  */
  22. public class BusSchedule extends Route
  23. {

  24.     /** */
  25.     private static final long serialVersionUID = 20170124L;

  26.     /** Line of the bus schedule. */
  27.     private final String line;

  28.     /** List of bus stops. */
  29.     private final Map<String, BusStopInfo> schedule = new LinkedHashMap<>();

  30.     /** Map of actual departures stored per bus stop. */
  31.     private final Map<String, Time> actualDeparturesBusStop = new LinkedHashMap<>();

  32.     /** Map of actual departures stored per conflict. */
  33.     private final Map<String, Time> actualDeparturesConflict = new LinkedHashMap<>();

  34.     /**
  35.      * @param id String; id
  36.      * @param gtuType GtuType; the GtuType for which this is a route
  37.      * @param nodes List&lt;Node&gt;; nodes
  38.      * @param line String; line of the bus schedule
  39.      * @throws NetworkException if intermediate nodes are missing in the route.
  40.      */
  41.     public BusSchedule(final String id, final GtuType gtuType, final List<Node> nodes, final String line)
  42.             throws NetworkException
  43.     {
  44.         super(id, gtuType, nodes);
  45.         this.line = line;
  46.     }

  47.     /**
  48.      * @param id String; id
  49.      * @param gtuType GtuType; the GtuType for which this is a route
  50.      * @param line String; line of the bus schedule
  51.      */
  52.     public BusSchedule(final String id, final GtuType gtuType, final String line)
  53.     {
  54.         super(id, gtuType);
  55.         this.line = line;
  56.     }

  57.     /**
  58.      * Adds a stop to the schedule.
  59.      * @param busStopId String; bus stop id
  60.      * @param departureTime Time; departure time
  61.      * @param dwellTime Duration; dwell time
  62.      * @param forceSchedule boolean; whether to wait until departure time
  63.      */
  64.     public final void addBusStop(final String busStopId, final Time departureTime, final Duration dwellTime,
  65.             final boolean forceSchedule)
  66.     {
  67.         Throw.whenNull(busStopId, "Bus stop id may not be null.");
  68.         Throw.whenNull(departureTime, "Departure time may not be null.");
  69.         Throw.whenNull(dwellTime, "Dwell time may not be null.");
  70.         this.schedule.put(busStopId, new BusStopInfo(departureTime, dwellTime, forceSchedule));
  71.     }

  72.     /**
  73.      * Whether the bus of this line should stop for this bus stop. False if not the correct line, or already stopped.
  74.      * @param busStopId String; id of bus stop
  75.      * @param time Time; time to check
  76.      * @return whether the bus of this line should stop for this bus stop
  77.      */
  78.     public final boolean isLineStop(final String busStopId, final Time time)
  79.     {
  80.         return this.schedule.containsKey(busStopId) && (!this.actualDeparturesConflict.containsKey(busStopId)
  81.                 || time.lt(this.actualDeparturesConflict.get(busStopId)));
  82.     }

  83.     /**
  84.      * Returns departure time for the given bus stop.
  85.      * @param busStopId String; id of bus stop
  86.      * @return departure time for the given bus stop
  87.      */
  88.     public final Time getDepartureTime(final String busStopId)
  89.     {
  90.         checkStop(busStopId);
  91.         return this.schedule.get(busStopId).getDepartureTime();
  92.     }

  93.     /**
  94.      * Returns dwell time for the given bus stop.
  95.      * @param busStopId String; id of bus stop
  96.      * @return dwell time for the given bus stop
  97.      */
  98.     public final Duration getDwellTime(final String busStopId)
  99.     {
  100.         checkStop(busStopId);
  101.         return this.schedule.get(busStopId).getDwellTime();
  102.     }

  103.     /**
  104.      * Returns whether the departure time is enforced.
  105.      * @param busStopId String; id of bus stop
  106.      * @return whether the departure time is enforced
  107.      */
  108.     public final boolean isForceSchedule(final String busStopId)
  109.     {
  110.         checkStop(busStopId);
  111.         return this.schedule.get(busStopId).isForceSchedule();
  112.     }

  113.     /**
  114.      * Throws exception when the bus stop is not part of this schedule.
  115.      * @param busStopId String; id of bus stop
  116.      * @throws IllegalArgumentException if the bus stop is not part of this schedule
  117.      */
  118.     private void checkStop(final String busStopId)
  119.     {
  120.         Throw.when(!this.schedule.containsKey(busStopId), IllegalArgumentException.class, "Bus stop %s is not for schedule %s.",
  121.                 busStopId, this);
  122.     }

  123.     /**
  124.      * Set actual departure time.
  125.      * @param busStopId String; bus stop id
  126.      * @param conflictIds Set&lt;String&gt;; conflicts downstream of the bus stop
  127.      * @param time Time; actual departure time
  128.      */
  129.     public final void setActualDeparture(final String busStopId, final Set<String> conflictIds, final Time time)
  130.     {
  131.         this.actualDeparturesBusStop.put(busStopId, time);
  132.         for (String conflictId : conflictIds)
  133.         {
  134.             this.actualDeparturesConflict.put(conflictId, time);
  135.         }
  136.     }

  137.     /**
  138.      * Return the actual departure time.
  139.      * @param busStopId String; bus stop id
  140.      * @return actual departure time, {@code null} if not given
  141.      */
  142.     public final Time getActualDepartureBusStop(final String busStopId)
  143.     {
  144.         return this.actualDeparturesBusStop.get(busStopId);
  145.     }

  146.     /**
  147.      * Return the actual departure time.
  148.      * @param conflictId String; conflict id
  149.      * @return actual departure time, {@code null} if not given
  150.      */
  151.     public final Time getActualDepartureConflict(final String conflictId)
  152.     {
  153.         return this.actualDeparturesConflict.get(conflictId);
  154.     }

  155.     /**
  156.      * @return line.
  157.      */
  158.     public final String getLine()
  159.     {
  160.         return this.line;
  161.     }

  162.     /** {@inheritDoc} */
  163.     @Override
  164.     public final String toString()
  165.     {
  166.         return "BusSchedule [id=" + getId() + ", line=" + this.line + "]";
  167.     }

  168.     /**
  169.      * Class to contain info regarding a stop in the schedule.
  170.      * <p>
  171.      * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  172.      * <br>
  173.      * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  174.      * </p>
  175.      * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  176.      * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  177.      * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
  178.      */
  179.     private class BusStopInfo
  180.     {

  181.         /** Departure time. */
  182.         private final Time departureTime;

  183.         /** Dwell time. */
  184.         private final Duration dwellTime;

  185.         /** Whether to wait until departure time. */
  186.         private final boolean forceSchedule;

  187.         /**
  188.          * @param departureTime Time; departure time
  189.          * @param dwellTime Duration; dwell time
  190.          * @param forceSchedule boolean; whether to wait until departure time
  191.          */
  192.         BusStopInfo(final Time departureTime, final Duration dwellTime, final boolean forceSchedule)
  193.         {
  194.             this.departureTime = departureTime;
  195.             this.dwellTime = dwellTime;
  196.             this.forceSchedule = forceSchedule;
  197.         }

  198.         /**
  199.          * @return departureTime.
  200.          */
  201.         public final Time getDepartureTime()
  202.         {
  203.             return this.departureTime;
  204.         }

  205.         /**
  206.          * @return dwellTime.
  207.          */
  208.         public final Duration getDwellTime()
  209.         {
  210.             return this.dwellTime;
  211.         }

  212.         /**
  213.          * @return forceSchedule.
  214.          */
  215.         public final boolean isForceSchedule()
  216.         {
  217.             return this.forceSchedule;
  218.         }

  219.         /** {@inheritDoc} */
  220.         @Override
  221.         public String toString()
  222.         {
  223.             return "BusStopInfo [departureTime=" + this.departureTime + ", dwellTime=" + this.dwellTime + ", forceSchedule="
  224.                     + this.forceSchedule + "]";
  225.         }

  226.     }

  227. }