MultiLanePerceptionIterable.java

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

  2. import java.util.HashMap;
  3. import java.util.Iterator;
  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-2018 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.     final Map<RelativeLane, Iterator<PrimaryIteratorEntry>> iterators = new HashMap<>();

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

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

  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 Lane; lane
  45.      * @param iterable AbstractPerceptionReiterable; 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-2018 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.         SortedMap<PrimaryIteratorEntry, RelativeLane> elements;

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

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

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

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

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

  113.             MultiLanePerceptionIterable.this.laneMap.put(next.object, lane);
  114.             return next;
  115.         }

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

  134.     }

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