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