View Javadoc
1   package org.opentrafficsim.road.network.sampling.data;
2   
3   import java.util.Iterator;
4   
5   import org.opentrafficsim.kpi.sampling.data.ExtendedDataTypeString;
6   import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
7   import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
8   import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.NeighborsPerception;
9   import org.opentrafficsim.road.network.sampling.GtuData;
10  
11  /**
12   * Leader id in trajectory information.
13   * <p>
14   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
16   * <p>
17   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 19 mrt. 2019 <br>
18   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
20   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
21   */
22  public class LeaderId extends ExtendedDataTypeString<GtuData>
23  {
24  
25      /**
26       * Constructor.
27       */
28      public LeaderId()
29      {
30          super("leaderId");
31      }
32  
33      /** {@inheritDoc} */
34      @Override
35      public String getValue(final GtuData gtu)
36      {
37          NeighborsPerception neigbors =
38                  gtu.getGtu().getTacticalPlanner().getPerception().getPerceptionCategoryOrNull(NeighborsPerception.class);
39          if (neigbors == null)
40          {
41              throw new RuntimeException("Leader id can only be stored in trajectories if the GTU's have NeighborsPerception.");
42          }
43          Iterator<LaneBasedGTU> it = neigbors.getLeaders(RelativeLane.CURRENT).underlying();
44          return it.hasNext() ? it.next().getId() : "";
45      }
46  
47  }