MultiLanePerceptionIterable.java

  1. package org.opentrafficsim.road.gtu.lane.perception;

  2. import java.util.Iterator;
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.NoSuchElementException;
  6. import java.util.SortedMap;
  7. import java.util.TreeMap;

  8. import org.djunits.value.vdouble.scalar.Length;
  9. import org.opentrafficsim.base.parameters.ParameterException;
  10. import org.opentrafficsim.core.gtu.GTUException;
  11. import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
  12. import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;

  13. /**
  14.  * Iterable class to search over multiple lanes.
  15.  * <p>
  16.  * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  17.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  18.  * <p>
  19.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 20 feb. 2018 <br>
  20.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  21.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  22.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  23.  * @param <H> headway type
  24.  * @param <U> underlying headway type
  25.  */
  26. public class MultiLanePerceptionIterable<H extends Headway, U> extends AbstractPerceptionReiterable<H, U>
  27. {

  28.     /** Set of iterators per lane. */
  29.     private final Map<RelativeLane, Iterator<PrimaryIteratorEntry>> iterators = new LinkedHashMap<>();

  30.     /** Map of lane per object. */
  31.     private final Map<U, RelativeLane> laneMap = new LinkedHashMap<>();

  32.     /** Map of iterable per lane. */
  33.     private final Map<RelativeLane, AbstractPerceptionReiterable<H, U>> iterables = new LinkedHashMap<>();

  34.     /**
  35.      * Constructor.
  36.      * @param perceivingGtu LaneBasedGTU; perceiving GTU
  37.      */
  38.     public MultiLanePerceptionIterable(final LaneBasedGTU perceivingGtu)
  39.     {
  40.         super(perceivingGtu);
  41.     }

  42.     /**
  43.      * Adds an iterable for a lane.
  44.      * @param lane RelativeLane; lane
  45.      * @param iterable AbstractPerceptionReiterable&lt;H, U&gt;; iterable
  46.      */
  47.     public void addIterable(final RelativeLane lane, final AbstractPerceptionReiterable<H, U> iterable)
  48.     {
  49.         this.iterators.put(lane, iterable.getPrimaryIterator());
  50.         this.iterables.put(lane, iterable);
  51.     }

  52.     /** {@inheritDoc} */
  53.     @Override
  54.     public Iterator<PrimaryIteratorEntry> primaryIterator()
  55.     {
  56.         return new MultiLaneIterator();
  57.     }

  58.     /**
  59.      * Iterator that returns the closest element from a set of lanes.
  60.      * <p>
  61.      * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  62.      * <br>
  63.      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  64.      * <p>
  65.      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 21 feb. 2018 <br>
  66.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  67.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  68.      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  69.      */
  70.     private class MultiLaneIterator implements Iterator<PrimaryIteratorEntry>
  71.     {

  72.         /** Sorted elements per lane. */
  73.         private SortedMap<PrimaryIteratorEntry, RelativeLane> elements;

  74.         /** Constructor. */
  75.         MultiLaneIterator()
  76.         {
  77.             //
  78.         }

  79.         /** {@inheritDoc} */
  80.         @Override
  81.         public boolean hasNext()
  82.         {
  83.             assureNext();
  84.             return !this.elements.isEmpty();
  85.         }

  86.         /** {@inheritDoc} */
  87.         @SuppressWarnings("synthetic-access")
  88.         @Override
  89.         public PrimaryIteratorEntry next()
  90.         {
  91.             assureNext();
  92.             if (this.elements.isEmpty())
  93.             {
  94.                 throw new NoSuchElementException();
  95.             }

  96.             // get and remove next
  97.             PrimaryIteratorEntry next = this.elements.firstKey();
  98.             RelativeLane lane = this.elements.get(next);
  99.             this.elements.remove(next);

  100.             // prepare next
  101.             Iterator<PrimaryIteratorEntry> laneIterator = MultiLanePerceptionIterable.this.iterators.get(lane);
  102.             if (laneIterator != null)
  103.             {
  104.                 if (laneIterator.hasNext())
  105.                 {
  106.                     this.elements.put(laneIterator.next(), lane);
  107.                 }
  108.                 else
  109.                 {
  110.                     // remove it, it has no more elements to offer
  111.                     MultiLanePerceptionIterable.this.iterators.remove(lane);
  112.                 }
  113.             }

  114.             MultiLanePerceptionIterable.this.laneMap.put(next.getObject(), lane);
  115.             return next;
  116.         }

  117.         /**
  118.          * Starts the process.
  119.          */
  120.         @SuppressWarnings("synthetic-access")
  121.         public void assureNext()
  122.         {
  123.             if (this.elements == null)
  124.             {
  125.                 this.elements = new TreeMap<>();
  126.                 for (RelativeLane lane : MultiLanePerceptionIterable.this.iterators.keySet())
  127.                 {
  128.                     Iterator<PrimaryIteratorEntry> laneIterator = MultiLanePerceptionIterable.this.iterators.get(lane);
  129.                     if (laneIterator.hasNext())
  130.                     {
  131.                         this.elements.put(laneIterator.next(), lane);
  132.                     }
  133.                 }
  134.             }
  135.         }

  136.     }

  137.     /** {@inheritDoc} */
  138.     @Override
  139.     public H perceive(final LaneBasedGTU perceivingGtu, final U object, final Length distance)
  140.             throws GTUException, ParameterException
  141.     {
  142.         return this.iterables.get(this.laneMap.get(object)).perceive(perceivingGtu, object, distance);
  143.     }
  144. }