View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.mental;
2   
3   import java.util.Iterator;
4   import java.util.LinkedHashMap;
5   import java.util.Map;
6   import java.util.SortedSet;
7   
8   import org.djunits.value.vdouble.scalar.Length;
9   import org.opentrafficsim.base.parameters.ParameterException;
10  import org.opentrafficsim.base.parameters.Parameters;
11  import org.opentrafficsim.core.gtu.GtuException;
12  import org.opentrafficsim.core.gtu.RelativePosition;
13  import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
14  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
15  import org.opentrafficsim.road.gtu.lane.perception.LaneStructure.Entry;
16  import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
17  import org.opentrafficsim.road.network.lane.object.Distraction;
18  
19  /**
20   * Task-demand for road-side distraction.
21   * <p>
22   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
23   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
24   * </p>
25   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
26   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
27   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
28   */
29  public class TaskRoadSideDistraction extends AbstractTask
30  {
31  
32      /** Odometer values at distraction. */
33      private Map<Distraction, Double> odos = new LinkedHashMap<>();
34  
35      /** Constructor. */
36      public TaskRoadSideDistraction()
37      {
38          super("road-side distraction");
39      }
40  
41      /** {@inheritDoc} */
42      @Override
43      public double calculateTaskDemand(final LanePerception perception, final LaneBasedGtu gtu, final Parameters parameters)
44              throws ParameterException, GtuException
45      {
46          Map<RelativeLane, SortedSet<Entry<Distraction>>> map =
47                  perception.getLaneStructure().getDownstreamObjects(Distraction.class, gtu, RelativePosition.FRONT);
48          // (re)put all downstream distractions in the odos map
49          double odo = gtu.getOdometer().si;
50          for (RelativeLane lane : map.keySet())
51          {
52              for (Entry<Distraction> entry : map.get(lane))
53              {
54                  this.odos.put(entry.getLaneBasedObject(), odo + entry.getDistance().si);
55              }
56          }
57          // loop over all distractions in odos
58          Iterator<Distraction> it = this.odos.keySet().iterator();
59          double demand = 0.0;
60          while (it.hasNext())
61          {
62              Distraction next = it.next();
63              Double distraction = next.getDistraction(Length.instantiateSI(odo - this.odos.get(next)));
64              if (distraction == null)
65              {
66                  it.remove();
67              }
68              else
69              {
70                  demand += distraction;
71              }
72          }
73          return demand;
74      }
75  
76  }