View Javadoc
1   package org.opentrafficsim.road.network.sampling;
2   
3   import org.djunits.value.vdouble.scalar.Speed;
4   import org.opentrafficsim.core.gtu.GtuException;
5   import org.opentrafficsim.core.network.NetworkException;
6   import org.opentrafficsim.kpi.interfaces.GtuData;
7   import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
8   
9   /**
10   * Gtu representation in road sampler.
11   * <p>
12   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
14   * </p>
15   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
17   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
18   */
19  public class GtuDataRoad implements GtuData
20  {
21  
22      /** Gtu. */
23      private final LaneBasedGtu gtu;
24  
25      /**
26       * @param gtu LaneBasedGtu; gtu
27       */
28      public GtuDataRoad(final LaneBasedGtu gtu)
29      {
30          this.gtu = gtu;
31      }
32  
33      /**
34       * @return gtu.
35       */
36      public final LaneBasedGtu getGtu()
37      {
38          return this.gtu;
39      }
40  
41      /** {@inheritDoc} */
42      @Override
43      public final String getId()
44      {
45          return this.gtu.getId();
46      }
47  
48      /** {@inheritDoc} */
49      @Override
50      public final String getOriginId()
51      {
52          try
53          {
54              return this.gtu.getStrategicalPlanner().getRoute().originNode().getId();
55          }
56          catch (NetworkException exception)
57          {
58              throw new RuntimeException("Could not get origin node.", exception);
59          }
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      public final String getDestinationId()
65      {
66          try
67          {
68              return this.gtu.getStrategicalPlanner().getRoute().destinationNode().getId();
69          }
70          catch (NetworkException exception)
71          {
72              throw new RuntimeException("Could not get destination node.", exception);
73          }
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      public final String getGtuTypeId()
79      {
80          return this.gtu.getType().getId();
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      public final String getRouteId()
86      {
87          return this.gtu.getStrategicalPlanner().getRoute().getId();
88      }
89  
90      /** {@inheritDoc} */
91      @Override
92      public final Speed getReferenceSpeed()
93      {
94          try
95          {
96              double v1 = this.gtu.getReferencePosition().lane().getSpeedLimit(this.gtu.getType()).si;
97              double v2 = this.gtu.getMaximumSpeed().si;
98              return Speed.instantiateSI(v1 < v2 ? v1 : v2);
99          }
100         catch (GtuException exception)
101         {
102             // GTU was destroyed and is without a reference location
103             return Speed.NaN;
104         }
105         catch (NetworkException exception)
106         {
107             throw new RuntimeException("Could not obtain reference speed from GTU " + this.gtu, exception);
108         }
109     }
110 
111     /** {@inheritDoc} */
112     @Override
113     public final String toString()
114     {
115         return "GtuData [gtu=" + this.gtu + "]";
116     }
117 
118 }