DirectBusStopPerception.java

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

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

  4. import org.djunits.value.vdouble.scalar.Length;
  5. import org.opentrafficsim.base.TimeStampedObject;
  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.gtu.Try;
  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.LaneRecord;
  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-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  27.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  28.  * <p>
  29.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 27 jan. 2017 <br>
  30.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  31.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  32.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  33.  */
  34. public class DirectBusStopPerception extends LaneBasedAbstractPerceptionCategory implements BusStopPerception
  35. {

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

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

  40.     /** Bus stops. */
  41.     private TimeStampedObject<PerceptionCollectable<HeadwayBusStop, BusStop>> busStops;

  42.     /**
  43.      * @param perception perception
  44.      */
  45.     public DirectBusStopPerception(final LanePerception perception)
  46.     {
  47.         super(perception);
  48.     }

  49.     /** {@inheritDoc} */
  50.     @Override
  51.     public final void updateBusStops() throws GTUException, ParameterException
  52.     {
  53.         Route route = getGtu().getStrategicalPlanner().getRoute();
  54.         MultiLanePerceptionIterable<HeadwayBusStop, BusStop> stops = new MultiLanePerceptionIterable<>(getGtu());
  55.         for (RelativeLane lane : getPerception().getLaneStructure().getExtendedCrossSection())
  56.         {
  57.             LaneRecord<?> record = getPerception().getLaneStructure().getFirstRecord(lane);
  58.             Length pos = record.getStartDistance().neg();
  59.             pos = record.getDirection().isPlus() ? pos.plus(getGtu().getFront().getDx())
  60.                     : pos.minus(getGtu().getFront().getDx());
  61.             AbstractPerceptionIterable<HeadwayBusStop, BusStop, ?> it = new LaneBasedObjectIterable<HeadwayBusStop, BusStop>(
  62.                     getGtu(), BusStop.class, record, Length.max(Length.ZERO, pos),
  63.                     getGtu().getParameters().getParameter(LOOKAHEAD), getGtu().getFront(), route)
  64.             {
  65.                 /** {@inheritDoc} */
  66.                 @Override
  67.                 public HeadwayBusStop perceive(final LaneBasedGTU perceivingGtu, final BusStop busStop, final Length distance)
  68.                 {
  69.                     Set<String> conflictIds = new HashSet<>();
  70.                     for (Conflict conflict : busStop.getConflicts())
  71.                     {
  72.                         conflictIds.add(conflict.getId());
  73.                     }
  74.                     return Try.assign(() -> new HeadwayBusStop(busStop, distance, lane, conflictIds),
  75.                             "Exception while creating bus stop headway.");
  76.                 }
  77.             };
  78.             stops.addIterable(lane, it);
  79.         }
  80.         this.busStops = new TimeStampedObject<>(stops, getTimestamp());
  81.     }

  82.     /** {@inheritDoc} */
  83.     @Override
  84.     public final PerceptionCollectable<HeadwayBusStop, BusStop> getBusStops()
  85.     {
  86.         return this.busStops.getObject();
  87.     }

  88.     /**
  89.      * Returns the time stamped bus stops.
  90.      * @return time stamped bus stops
  91.      */
  92.     public final TimeStampedObject<PerceptionCollectable<HeadwayBusStop, BusStop>> getTimeStampedBusStops()
  93.     {
  94.         return this.busStops;
  95.     }

  96.     /** {@inheritDoc} */
  97.     @Override
  98.     public final String toString()
  99.     {
  100.         return "DirectBusStopPerception";
  101.     }

  102. }