View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception;
2   
3   import java.util.Optional;
4   
5   import org.opentrafficsim.base.parameters.ParameterException;
6   import org.opentrafficsim.base.parameters.ParameterTypeLength;
7   import org.opentrafficsim.base.parameters.ParameterTypes;
8   import org.opentrafficsim.core.gtu.GtuException;
9   import org.opentrafficsim.core.gtu.perception.AbstractPerception;
10  import org.opentrafficsim.core.network.NetworkException;
11  import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
12  import org.opentrafficsim.road.gtu.lane.perception.mental.Mental;
13  import org.opentrafficsim.road.gtu.lane.perception.structure.LaneStructure;
14  
15  /**
16   * The perception module of a GTU based on lanes. It is responsible for perceiving (sensing) the environment of the GTU, which
17   * includes the locations of other GTUs. Perception is done at a certain time, and the perceived information might have a
18   * limited validity. In that sense, Perception is stateful. Information can be requested as often as needed, but will only be
19   * recalculated when asked explicitly. This abstract class provides the building blocks for lane-based perception. <br>
20   * Perception for lane-based GTUs involves information about GTUs in front of the owner GTU on the same lane (the 'leader' GTU),
21   * parallel vehicles (important if we want to change lanes), distance to other vehicles on parallel lanes, as well in front as
22   * to the back (important if we want to change lanes), and information about obstacles, traffic lights, speed signs, and ending
23   * lanes.
24   * <p>
25   * This class allows {@code PerceptionCategory}s that are either eager or lazy. All categories will have the {@code updateAll}
26   * method invoked prior to an operational plan being determined. Categories may ignore this and instead evaluate results only
27   * when the tactical planner requests them.
28   * <p>
29   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
30   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
31   * </p>
32   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
33   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
34   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
35   */
36  public class CategoricalLanePerception extends AbstractPerception<LaneBasedGtu> implements LanePerception
37  {
38  
39      /** Look ahead parameter type. */
40      protected static final ParameterTypeLength LOOKAHEAD = ParameterTypes.LOOKAHEAD;
41  
42      /** Look back parameter type. */
43      protected static final ParameterTypeLength LOOKBACK = ParameterTypes.LOOKBACK;
44  
45      /** Lane structure. */
46      private LaneStructure laneStructure = null;
47  
48      /** Mental module. */
49      private Mental mental;
50  
51      /**
52       * Create a new LanePerception module without mental module.
53       * @param gtu GTU
54       */
55      public CategoricalLanePerception(final LaneBasedGtu gtu)
56      {
57          super(gtu);
58          this.mental = null;
59      }
60  
61      /**
62       * Create a new LanePerception module with mental module.
63       * @param gtu GTU
64       * @param mental mental module
65       */
66      public CategoricalLanePerception(final LaneBasedGtu gtu, final Mental mental)
67      {
68          super(gtu);
69          this.mental = mental;
70      }
71  
72      @Override
73      public final LaneStructure getLaneStructure() throws ParameterException
74      {
75          if (this.laneStructure == null)
76          {
77              this.laneStructure = new LaneStructure(getGtu(), getGtu().getParameters().getParameter(LOOKBACK),
78                      getGtu().getParameters().getParameter(LOOKAHEAD));
79          }
80          return this.laneStructure;
81      }
82  
83      @Override
84      public Optional<Mental> getMental()
85      {
86          return Optional.ofNullable(this.mental);
87      }
88  
89      @Override
90      public void perceive() throws GtuException, NetworkException, ParameterException
91      {
92          if (this.mental != null)
93          {
94              this.mental.apply(this);
95          }
96      }
97  
98  }