LeaderId.java

  1. package org.opentrafficsim.road.network.sampling.data;

  2. import java.util.Iterator;

  3. import org.opentrafficsim.kpi.sampling.data.ExtendedDataTypeString;
  4. import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
  5. import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
  6. import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.NeighborsPerception;
  7. import org.opentrafficsim.road.network.sampling.GtuData;

  8. /**
  9.  * Leader id in trajectory information.
  10.  * <p>
  11.  * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  12.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  13.  * <p>
  14.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 19 mrt. 2019 <br>
  15.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  16.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  17.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  18.  */
  19. public class LeaderId extends ExtendedDataTypeString<GtuData>
  20. {

  21.     /**
  22.      * Constructor.
  23.      */
  24.     public LeaderId()
  25.     {
  26.         super("leaderId");
  27.     }

  28.     /** {@inheritDoc} */
  29.     @Override
  30.     public String getValue(final GtuData gtu)
  31.     {
  32.         NeighborsPerception neigbors =
  33.                 gtu.getGtu().getTacticalPlanner().getPerception().getPerceptionCategoryOrNull(NeighborsPerception.class);
  34.         if (neigbors == null)
  35.         {
  36.             throw new RuntimeException("Leader id can only be stored in trajectories if the GTU's have NeighborsPerception.");
  37.         }
  38.         Iterator<LaneBasedGTU> it = neigbors.getLeaders(RelativeLane.CURRENT).underlying();
  39.         return it.hasNext() ? it.next().getId() : "";
  40.     }

  41. }