View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.mental;
2   
3   import java.util.HashMap;
4   import java.util.Iterator;
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-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
23   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
24   * <p>
25   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 5 apr. 2018 <br>
26   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
27   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
28   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
29   */
30  public class TaskRoadSideDistraction extends AbstractTask
31  {
32  
33      /** Odometer values at distraction. */
34      private Map<Distraction, Double> odos = new HashMap<>();
35  
36      /** Constructor. */
37      public TaskRoadSideDistraction()
38      {
39          super("road-side distraction");
40      }
41  
42      /** {@inheritDoc} */
43      @Override
44      public double calculateTaskDemand(final LanePerception perception, final LaneBasedGTU gtu, final Parameters parameters)
45              throws ParameterException, GTUException
46      {
47          Map<RelativeLane, SortedSet<Entry<Distraction>>> map =
48                  perception.getLaneStructure().getDownstreamObjects(Distraction.class, gtu, RelativePosition.FRONT);
49          // (re)put all downstream distractions in the odos map
50          double odo = gtu.getOdometer().si;
51          for (RelativeLane lane : map.keySet())
52          {
53              for (Entry<Distraction> entry : map.get(lane))
54              {
55                  this.odos.put(entry.getLaneBasedObject(), odo + entry.getDistance().si);
56              }
57          }
58          // loop over all distractions in odos
59          Iterator<Distraction> it = this.odos.keySet().iterator();
60          double demand = 0.0;
61          while (it.hasNext())
62          {
63              Distraction next = it.next();
64              Double distraction = next.getDistraction(Length.createSI(odo - this.odos.get(next)));
65              if (distraction == null)
66              {
67                  it.remove();
68              }
69              else
70              {
71                  demand += distraction;
72              }
73          }
74          return demand;
75      }
76  
77  }