DirectBusStopPerception.java

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

  2. import java.util.LinkedHashSet;
  3. import java.util.Set;

  4. import org.djunits.value.vdouble.scalar.Length;
  5. import org.djutils.exceptions.Try;
  6. import org.opentrafficsim.base.parameters.ParameterException;
  7. import org.opentrafficsim.base.parameters.ParameterTypeLength;
  8. import org.opentrafficsim.base.parameters.ParameterTypes;
  9. import org.opentrafficsim.core.gtu.GtuException;
  10. import org.opentrafficsim.core.network.NetworkException;
  11. import org.opentrafficsim.core.network.route.Route;
  12. import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
  13. import org.opentrafficsim.road.gtu.lane.perception.AbstractPerceptionIterable;
  14. import org.opentrafficsim.road.gtu.lane.perception.LaneBasedObjectIterable;
  15. import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
  16. import org.opentrafficsim.road.gtu.lane.perception.LaneRecordInterface;
  17. import org.opentrafficsim.road.gtu.lane.perception.MultiLanePerceptionIterable;
  18. import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable;
  19. import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
  20. import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayBusStop;
  21. import org.opentrafficsim.road.network.lane.conflict.Conflict;
  22. import org.opentrafficsim.road.network.lane.object.BusStop;

  23. /**
  24.  * Bus stop perception.
  25.  * <p>
  26.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  27.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  28.  * </p>
  29.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  30.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  31.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  32.  */
  33. public class DirectBusStopPerception extends LaneBasedAbstractPerceptionCategory implements BusStopPerception
  34. {

  35.     /** */
  36.     private static final long serialVersionUID = 20170127;

  37.     /** Look ahead parameter type. */
  38.     protected static final ParameterTypeLength LOOKAHEAD = ParameterTypes.LOOKAHEAD;

  39.     /**
  40.      * @param perception LanePerception; perception
  41.      */
  42.     public DirectBusStopPerception(final LanePerception perception)
  43.     {
  44.         super(perception);
  45.     }

  46.     /**
  47.      * Returns bus stops.
  48.      * @return PerceptionCollectable&lt;HeadwayBusStop, BusStop&gt;; bus stops
  49.      */
  50.     public final PerceptionCollectable<HeadwayBusStop, BusStop> computeBusStops()
  51.     {
  52.         try
  53.         {
  54.             Route route = getGtu().getStrategicalPlanner().getRoute();
  55.             MultiLanePerceptionIterable<HeadwayBusStop, BusStop> stops = new MultiLanePerceptionIterable<>(getGtu());
  56.             for (RelativeLane lane : getPerception().getLaneStructure().getExtendedCrossSection())
  57.             {
  58.                 LaneRecordInterface<?> record = getPerception().getLaneStructure().getFirstRecord(lane);
  59.                 Length pos = record.getStartDistance().neg();
  60.                 pos = pos.plus(getGtu().getFront().dx());
  61.                 AbstractPerceptionIterable<HeadwayBusStop, BusStop,
  62.                         ?> it = new LaneBasedObjectIterable<HeadwayBusStop, BusStop>(getGtu(), BusStop.class, record,
  63.                                 Length.max(Length.ZERO, pos), true, getGtu().getParameters().getParameter(LOOKAHEAD),
  64.                                 getGtu().getFront(), route)
  65.                         {
  66.                             /** {@inheritDoc} */
  67.                             @Override
  68.                             public HeadwayBusStop perceive(final LaneBasedGtu perceivingGtu, final BusStop busStop,
  69.                                     final Length distance)
  70.                             {
  71.                                 Set<String> conflictIds = new LinkedHashSet<>();
  72.                                 for (Conflict conflict : busStop.getConflicts())
  73.                                 {
  74.                                     conflictIds.add(conflict.getId());
  75.                                 }
  76.                                 return Try.assign(
  77.                                         () -> new HeadwayBusStop(busStop, distance, lane, conflictIds, busStop.getLane()),
  78.                                         "Exception while creating bus stop headway.");
  79.                             }
  80.                         };
  81.                 stops.addIterable(lane, it);
  82.             }
  83.             return stops;
  84.         }
  85.         catch (GtuException | ParameterException exception)
  86.         {
  87.             throw new RuntimeException("Unexpected exception while perceiving bus stops.");
  88.         }
  89.     }

  90.     /** {@inheritDoc} */
  91.     @Override
  92.     public final PerceptionCollectable<HeadwayBusStop, BusStop> getBusStops()
  93.     {
  94.         return this.computeIfAbsent("busStops", () -> computeBusStops());
  95.     }

  96.     /** {@inheritDoc} */
  97.     @Override
  98.     public void updateAll() throws GtuException, NetworkException, ParameterException
  99.     {
  100.         // lazy evaluation
  101.     }

  102.     /** {@inheritDoc} */
  103.     @Override
  104.     public final String toString()
  105.     {
  106.         return "DirectBusStopPerception";
  107.     }

  108. }