PerceptionCollectable.java

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

  2. import java.util.Iterator;
  3. import java.util.function.Supplier;

  4. import org.djunits.value.vdouble.scalar.Length;
  5. import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;

  6. /**
  7.  * Iterable that additionally provides support for PerceptionCollectors. These gather raw data, to only 'perceive' the result.
  8.  * <p>
  9.  * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  10.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  11.  * <p>
  12.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 1 mrt. 2018 <br>
  13.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  14.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  15.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  16.  * @param <H> headway type
  17.  * @param <U> underlying object type
  18.  */
  19. public interface PerceptionCollectable<H extends Headway, U> extends PerceptionIterable<H>
  20. {

  21.     /**
  22.      * Collect the underlying objects in to a perceived result. This methodology is loosely based on Stream.collect().
  23.      * @param collector PerceptionCollector&lt;C, ? super U, I&gt;; collector
  24.      * @param <C> collection result type
  25.      * @param <I> intermediate type
  26.      * @return C; collection result
  27.      */
  28.     default <C, I> C collect(final PerceptionCollector<C, ? super U, I> collector)
  29.     {
  30.         return collect(collector.getIdentity(), collector.getAccumulator(), collector.getFinalizer());
  31.     }

  32.     /**
  33.      * Collect the underlying objects in to a perceived result. This methodology is loosely based on Stream.collect().
  34.      * @param identity Supplier&lt;I&gt;; the initial intermediate result value
  35.      * @param accumulator PerceptionAccumulator&lt;? super U, I&gt;; accumulator
  36.      * @param finalizer PerceptionFinalizer&lt;C, I&gt;; finalizer
  37.      * @param <C> collection result type
  38.      * @param <I> intermediate type
  39.      * @return C; collection result
  40.      */
  41.     <C, I> C collect(Supplier<I> identity, PerceptionAccumulator<? super U, I> accumulator,
  42.             PerceptionFinalizer<C, I> finalizer);

  43.     /**
  44.      * Returns an iterator over the underlying objects.
  45.      * @return Iterator&lt;U&gt;; iterator
  46.      */
  47.     Iterator<U> underlying();

  48.     /**
  49.      * Returns an iterator over the underlying objects coupled with the distance.
  50.      * @return Iterator&lt;UnderlyingDistance&lt;U&gt;&gt;; iterator
  51.      */
  52.     Iterator<UnderlyingDistance<U>> underlyingWithDistance();

  53.     /**
  54.      * Combination of an accumulator and a finalizer.
  55.      * <p>
  56.      * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  57.      * <br>
  58.      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  59.      * <p>
  60.      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 28 feb. 2018 <br>
  61.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  62.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  63.      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  64.      * @param <C> collection result type
  65.      * @param <U> underlying object type
  66.      * @param <I> intermediate result type
  67.      */
  68.     public interface PerceptionCollector<C, U, I>
  69.     {
  70.         /**
  71.          * Returns the identity value, the initial intermediate value.
  72.          * @return I; identity value, the initial intermediate value
  73.          */
  74.         Supplier<I> getIdentity();

  75.         /**
  76.          * Returns the accumulator.
  77.          * @return PerceptionAccumulator; accumulator
  78.          */
  79.         PerceptionAccumulator<U, I> getAccumulator();

  80.         /**
  81.          * Returns the finalizer.
  82.          * @return PerceptionFinalizer; finalizer
  83.          */
  84.         PerceptionFinalizer<C, I> getFinalizer();
  85.     }

  86.     /**
  87.      * Accumulates an object one at a time in to an accumulating intermediate result.
  88.      * <p>
  89.      * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  90.      * <br>
  91.      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  92.      * <p>
  93.      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 28 feb. 2018 <br>
  94.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  95.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  96.      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  97.      * @param <U> underlying object type
  98.      * @param <I> intermediate result type
  99.      */
  100.     public interface PerceptionAccumulator<U, I>
  101.     {
  102.         /**
  103.          * Accumulate the next object to intermediate result.
  104.          * @param intermediate Intermediate&lt;I&gt;; intermediate result before accumulation of object
  105.          * @param object U; next object to include
  106.          * @param distance Length; distance to the considered object
  107.          * @return I; intermediate result after accumulation of object
  108.          */
  109.         Intermediate<I> accumulate(Intermediate<I> intermediate, U object, Length distance);
  110.     }

  111.     /**
  112.      * Translates the last intermediate result of an accumulator in to the collection output.
  113.      * <p>
  114.      * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  115.      * <br>
  116.      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  117.      * <p>
  118.      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 28 feb. 2018 <br>
  119.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  120.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  121.      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  122.      * @param <C> collection result type
  123.      * @param <I> intermediate result type
  124.      */
  125.     public interface PerceptionFinalizer<C, I>
  126.     {
  127.         /**
  128.          * Translate the last intermediate result in to a final result.
  129.          * @param intermediate I; last intermediate result
  130.          * @return C; final result
  131.          */
  132.         C collect(I intermediate);
  133.     }

  134.     /**
  135.      * Wrapper of intermediate result with info for the iterator algorithm.
  136.      * <p>
  137.      * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  138.      * <br>
  139.      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  140.      * <p>
  141.      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 3 apr. 2018 <br>
  142.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  143.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  144.      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>]
  145.      * @param <I> intermediate result type
  146.      */
  147.     class Intermediate<I>
  148.     {
  149.         /** Number of underlying object being iterated. */
  150.         private int number = 0;

  151.         /** Intermediate object. */
  152.         private I object;

  153.         /** Whether to stop accumulating. */
  154.         private boolean stop = false;

  155.         /**
  156.          * Constructor.
  157.          * @param object I; identity value
  158.          */
  159.         public Intermediate(final I object)
  160.         {
  161.             this.object = object;
  162.         }

  163.         /**
  164.          * Get intermediate object.
  165.          * @return I; intermediate object
  166.          */
  167.         public I getObject()
  168.         {
  169.             return this.object;
  170.         }

  171.         /**
  172.          * Set intermediate object.
  173.          * @param object I; intermediate object
  174.          */
  175.         public void setObject(final I object)
  176.         {
  177.             this.object = object;
  178.         }

  179.         /**
  180.          * Returns the number of the underlying object currently being accumulated, starts at 0 for the first.
  181.          * @return int; number of the underlying object currently being accumulated
  182.          */
  183.         public int getNumber()
  184.         {
  185.             return this.number;
  186.         }

  187.         /**
  188.          * Method for the iterator to increase the underlying object number.
  189.          */
  190.         public void step()
  191.         {
  192.             this.number++;
  193.         }

  194.         /**
  195.          * Method for the accumulator to indicate the iterator can stop.
  196.          */
  197.         public void stop()
  198.         {
  199.             this.stop = true;
  200.         }

  201.         /**
  202.          * Method for the iterator to check if it can stop.
  203.          * @return boolean; whether the iterator can stop
  204.          */
  205.         public boolean isStop()
  206.         {
  207.             return this.stop;
  208.         }
  209.     }

  210.     /**
  211.      * Wrapper for object and its distance.
  212.      * <p>
  213.      * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  214.      * <br>
  215.      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  216.      * <p>
  217.      * @version $Revision$, $LastChangedDate$, by $Author$, initial version Aug 6, 2019 <br>
  218.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  219.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  220.      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  221.      * @param <U> underlying object type
  222.      */
  223.     class UnderlyingDistance<U>
  224.     {
  225.         /** Object. */
  226.         final U object;

  227.         /** Distance. */
  228.         final Length distance;

  229.         /**
  230.          * @param object U; object
  231.          * @param distance Length; distance
  232.          */
  233.         public UnderlyingDistance(final U object, final Length distance)
  234.         {
  235.             this.object = object;
  236.             this.distance = distance;
  237.         }

  238.         /**
  239.          * @return U; object.
  240.          */
  241.         public U getObject()
  242.         {
  243.             return this.object;
  244.         }

  245.         /**
  246.          * @return Length; distance.
  247.          */
  248.         public Length getDistance()
  249.         {
  250.             return this.distance;
  251.         }
  252.     }

  253. }