GtuTypeAssumptions.java

  1. package org.opentrafficsim.road.gtu.lane.perception;

  2. import java.io.Serializable;
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;

  5. import org.djunits.value.vdouble.scalar.Speed;
  6. import org.djutils.exceptions.Throw;
  7. import org.opentrafficsim.base.parameters.Parameters;
  8. import org.opentrafficsim.core.gtu.GtuType;
  9. import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
  10. import org.opentrafficsim.road.network.lane.LaneType;

  11. /**
  12.  * <p>
  13.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  14.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  15.  * </p>
  16.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  17.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  18.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  19.  */
  20. public class GtuTypeAssumptions implements Serializable
  21. {
  22.     /** */
  23.     private static final long serialVersionUID = 20160527L;

  24.     /** stored car following model of the observed GTU. */
  25.     private final Map<GtuType, CarFollowingModel> carFollowingModelMap = new LinkedHashMap<>();

  26.     /** stored parameters of the observed GTU. */
  27.     private final Map<GtuType, Parameters> parametersMap = new LinkedHashMap<>();

  28.     /** stored speed limit info of the observed GTU. */
  29.     private final Map<GtuType, Map<LaneType, Speed>> laneTypeSpeedMap = new LinkedHashMap<>();

  30.     /**
  31.      * Set the car following model for a certain GtuType as an assumption for that GtuType.
  32.      * @param gtuType GtuType; the GtuType to set the model for
  33.      * @param carFollowingModel CarFollowingModel; the model to set for the GtuType
  34.      */
  35.     public final void setCarFollowingModel(final GtuType gtuType, final CarFollowingModel carFollowingModel)
  36.     {
  37.         Throw.whenNull(gtuType, "gtuType cannot be null");
  38.         Throw.whenNull(carFollowingModel, "carFollowingModel cannot be null");
  39.         this.carFollowingModelMap.put(gtuType, carFollowingModel);
  40.     }

  41.     /**
  42.      * Set the parameters for a certain GtuType as an assumption for that GtuType.
  43.      * @param gtuType GtuType; the GtuType to set the model for
  44.      * @param parameters Parameters; the model to set for the GtuType
  45.      */
  46.     public final void setParameters(final GtuType gtuType, final Parameters parameters)
  47.     {
  48.         Throw.whenNull(gtuType, "gtuType cannot be null");
  49.         Throw.whenNull(parameters, "parameters cannot be null");
  50.         this.parametersMap.put(gtuType, parameters);
  51.     }

  52.     /**
  53.      * Set the maximum speed for a certain GtuType on a certain LaneType as an assumption for that GtuType.
  54.      * @param gtuType GtuType; the GtuType to set the model for
  55.      * @param laneType LaneType; the laneType to set the speed for
  56.      * @param maxSpeed Speed; the maximum speed on the laneType for the given GtuType
  57.      */
  58.     public final void setLaneTypeMaxSpeed(final GtuType gtuType, final LaneType laneType, final Speed maxSpeed)
  59.     {
  60.         Throw.whenNull(gtuType, "gtuType cannot be null");
  61.         Throw.whenNull(laneType, "laneType cannot be null");
  62.         Throw.whenNull(maxSpeed, "maxSpeed cannot be null");
  63.         Map<LaneType, Speed> maxLaneTypeSpeed = this.laneTypeSpeedMap.get(gtuType);
  64.         if (maxLaneTypeSpeed == null)
  65.         {
  66.             maxLaneTypeSpeed = new LinkedHashMap<>();
  67.             this.laneTypeSpeedMap.put(gtuType, maxLaneTypeSpeed);
  68.         }
  69.         maxLaneTypeSpeed.put(laneType, maxSpeed);
  70.     }

  71.     /**
  72.      * Return the car following model for a certain GtuType as an assumption for that GtuType.
  73.      * @param gtuType GtuType; the GtuType to get the model for
  74.      * @return the car following model for the GtuType, or <b>null</b> when there is no information for the gtuType
  75.      */
  76.     public final CarFollowingModel getCarFollowingModel(final GtuType gtuType)
  77.     {
  78.         return this.carFollowingModelMap.get(gtuType);
  79.     }

  80.     /**
  81.      * Return the parameters model for a certain GtuType as an assumption for that GtuType.
  82.      * @param gtuType GtuType; the GtuType to get the model for
  83.      * @return the parameters for the GtuType, or <b>null</b> when there is no information for the gtuType
  84.      */
  85.     public final Parameters getParameters(final GtuType gtuType)
  86.     {
  87.         return this.parametersMap.get(gtuType);
  88.     }

  89.     /**
  90.      * Return the maximum speed on a LaneType for a certain GtuType as an assumption for that GtuType.
  91.      * @param gtuType GtuType; the GtuType to get the maximum speed for
  92.      * @param laneType LaneType; the LaneType to get the maximum speed for
  93.      * @return the maximum speed for the GtuType on the LaneType, or <b>null</b> when there is no information for the
  94.      *         combination of gtuType and laneType
  95.      */
  96.     public final Speed getLaneTypeMaxSpeed(final GtuType gtuType, final LaneType laneType)
  97.     {
  98.         if (!this.laneTypeSpeedMap.containsKey(gtuType))
  99.         {
  100.             return null;
  101.         }
  102.         return this.laneTypeSpeedMap.get(gtuType).get(laneType);
  103.     }

  104.     /**
  105.      * Return a safe copy of the maximum speed for all LaneTypes for a certain GtuType as an assumption for that GtuType.
  106.      * @param gtuType GtuType; the GtuType to get the maximum speed for
  107.      * @return a map with a safe copy of the maximum speed for the GtuType on all LaneTypes, or <b>null</b> when there is no
  108.      *         information for the gtuType
  109.      */
  110.     public final Map<LaneType, Speed> getMaxSpeeds(final GtuType gtuType)
  111.     {
  112.         if (!this.laneTypeSpeedMap.containsKey(gtuType))
  113.         {
  114.             return null;
  115.         }
  116.         Map<LaneType, Speed> maxSpeeds = new LinkedHashMap<>();
  117.         maxSpeeds.putAll(this.laneTypeSpeedMap.get(gtuType));
  118.         return maxSpeeds;
  119.     }

  120.     /** {@inheritDoc} */
  121.     @Override
  122.     public final int hashCode()
  123.     {
  124.         final int prime = 31;
  125.         int result = 1;
  126.         result = prime * result + ((this.parametersMap == null) ? 0 : this.parametersMap.hashCode());
  127.         result = prime * result + ((this.carFollowingModelMap == null) ? 0 : this.carFollowingModelMap.hashCode());
  128.         result = prime * result + ((this.laneTypeSpeedMap == null) ? 0 : this.laneTypeSpeedMap.hashCode());
  129.         return result;
  130.     }

  131.     /** {@inheritDoc} */
  132.     @Override
  133.     @SuppressWarnings("checkstyle:needbraces")
  134.     public final boolean equals(final Object obj)
  135.     {
  136.         if (this == obj)
  137.             return true;
  138.         if (obj == null)
  139.             return false;
  140.         if (getClass() != obj.getClass())
  141.             return false;
  142.         GtuTypeAssumptions other = (GtuTypeAssumptions) obj;
  143.         if (this.parametersMap == null)
  144.         {
  145.             if (other.parametersMap != null)
  146.                 return false;
  147.         }
  148.         else if (!this.parametersMap.equals(other.parametersMap))
  149.             return false;
  150.         if (this.carFollowingModelMap == null)
  151.         {
  152.             if (other.carFollowingModelMap != null)
  153.                 return false;
  154.         }
  155.         else if (!this.carFollowingModelMap.equals(other.carFollowingModelMap))
  156.             return false;
  157.         if (this.laneTypeSpeedMap == null)
  158.         {
  159.             if (other.laneTypeSpeedMap != null)
  160.                 return false;
  161.         }
  162.         else if (!this.laneTypeSpeedMap.equals(other.laneTypeSpeedMap))
  163.             return false;
  164.         return true;
  165.     }

  166.     /** {@inheritDoc} */
  167.     @Override
  168.     public final String toString()
  169.     {
  170.         return "GTUTypeAssumptions [carFollowingModelMap=" + this.carFollowingModelMap + ", parametersMap=" + this.parametersMap
  171.                 + ", laneTypeSpeedMap=" + this.laneTypeSpeedMap + "]";
  172.     }

  173. }