View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.categories;
2   
3   import java.util.function.Supplier;
4   
5   import org.djunits.value.vdouble.scalar.Length;
6   import org.djunits.value.vdouble.scalar.LinearDensity;
7   import org.opentrafficsim.core.gtu.GTU;
8   import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.Intermediate;
9   import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.PerceptionAccumulator;
10  import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.PerceptionCollector;
11  import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.PerceptionFinalizer;
12  import org.opentrafficsim.road.gtu.lane.perception.categories.AnticipationDensity.CountAndDistance;
13  
14  /**
15   * Collector to determine density based on GTUs.
16   * <p>
17   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
19   * <p>
20   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 13 mrt. 2018 <br>
21   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
23   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
24   */
25  public class AnticipationDensity implements PerceptionCollector<LinearDensity, GTU, CountAndDistance>
26  {
27  
28      /** {@inheritDoc} */
29      @Override
30      public Supplier<CountAndDistance> getIdentity()
31      {
32          return new Supplier<CountAndDistance>()
33          {
34              /** {@inheritDoc} */
35              @Override
36              public CountAndDistance get()
37              {
38                  return new CountAndDistance();
39              }
40          };
41      }
42  
43      /** {@inheritDoc} */
44      @Override
45      public PerceptionAccumulator<GTU, CountAndDistance> getAccumulator()
46      {
47          return new PerceptionAccumulator<GTU, CountAndDistance>()
48          {
49              /** {@inheritDoc} */
50              @Override
51              public Intermediate<CountAndDistance> accumulate(final Intermediate<CountAndDistance> intermediate,
52                      final GTU object, final Length distance)
53              {
54                  intermediate.getObject().increaseCount();
55                  intermediate.getObject().setDistance(distance);
56                  return intermediate;
57              }
58          };
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      public PerceptionFinalizer<LinearDensity, CountAndDistance> getFinalizer()
64      {
65          return new PerceptionFinalizer<LinearDensity, CountAndDistance>()
66          {
67              /** {@inheritDoc} */
68              @Override
69              public LinearDensity collect(final CountAndDistance intermediate)
70              {
71                  return LinearDensity.createSI(intermediate.getDistance().si / intermediate.getCount());
72              }
73          };
74      }
75  
76      /**
77       * Intermediate data to determine density.
78       * <p>
79       * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
80       * <br>
81       * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
82       * <p>
83       * @version $Revision$, $LastChangedDate$, by $Author$, initial version 13 mrt. 2018 <br>
84       * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
85       * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
86       * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
87       */
88      public static class CountAndDistance
89      {
90  
91          /** Count. */
92          private int count;
93  
94          /** Distance. */
95          private Length distance;
96  
97          /**
98           * @return count.
99           */
100         public int getCount()
101         {
102             return this.count;
103         }
104 
105         /**
106          * Increases the GTU count by 1.
107          */
108         public void increaseCount()
109         {
110             this.count++;
111         }
112 
113         /**
114          * @return distance.
115          */
116         public Length getDistance()
117         {
118             return this.distance;
119         }
120 
121         /**
122          * @param distance Length; set distance.
123          */
124         public void setDistance(final Length distance)
125         {
126             this.distance = distance;
127         }
128     }
129 
130 }