View Javadoc
1   package org.opentrafficsim.road.gtu.lane.tactical.util.lmrs;
2   
3   import java.util.HashMap;
4   import java.util.HashSet;
5   import java.util.Map;
6   import java.util.Set;
7   
8   import org.opentrafficsim.road.gtu.animation.DesireBased;
9   import org.opentrafficsim.road.gtu.animation.Synchronizable;
10  import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
11  import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable;
12  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
13  
14  /**
15   * Keeps data for LMRS for a specific GTU.
16   * <p>
17   * Copyright (c) 2013-2018 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 8 nov. 2016 <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 final class LmrsData implements DesireBased, Synchronizable
26  {
27  
28      /** Form of synchronization. */
29      private final Synchronization synchronization;
30  
31      /** Form of cooperation. */
32      private final Cooperation cooperation;
33  
34      /** Form of gap-acceptance. */
35      private final GapAcceptance gapAcceptance;
36  
37      /** Form of tail gating. */
38      private final Tailgating tailGating;
39  
40      /** Most recent leaders. */
41      private final Set<String> leaders = new HashSet<>();
42  
43      /** Current leaders. */
44      private final Set<String> tempLeaders = new HashSet<>();
45  
46      /** Latest desire value for visualization. */
47      final Map<Class<? extends Incentive>, Desire> desireMap = new HashMap<>();
48  
49      /** Synchronization state. */
50      State synchronizationState;
51  
52      /** Vehicle that is being synchronized to. */
53      private String syncVehicle;
54  
55      /**
56       * @param synchronization synchronization
57       * @param cooperation cooperation
58       * @param gapAcceptance gap-acceptance
59       * @param tailGating tail gating
60       */
61      public LmrsData(final Synchronization synchronization, final Cooperation cooperation, final GapAcceptance gapAcceptance,
62              final Tailgating tailGating)
63      {
64          this.synchronization = synchronization;
65          this.cooperation = cooperation;
66          this.gapAcceptance = gapAcceptance;
67          this.tailGating = tailGating;
68      }
69  
70      /**
71       * Checks if the given leader is a new leader.
72       * @param gtu gtu to check
73       * @return whether the gtu is a new leader
74       */
75      boolean isNewLeader(final HeadwayGTU gtu)
76      {
77          this.tempLeaders.add(gtu.getId());
78          return !this.leaders.contains(gtu.getId());
79      }
80  
81      /**
82       * Remembers the leaders of the current time step (those forwarded to isNewLeader()) for the next time step.
83       */
84      void finalizeStep()
85      {
86          this.leaders.clear();
87          this.leaders.addAll(this.tempLeaders);
88          this.tempLeaders.clear();
89      }
90  
91      /**
92       * Remembers the gtu that is synchronized to.
93       * @param gtu gtu that is synchronized to
94       */
95      void setSyncVehicle(final HeadwayGTU gtu)
96      {
97          this.syncVehicle = gtu == null ? null : gtu.getId();
98      }
99  
100     /**
101      * Returns whether the provided gtu is the gtu that is synchronized to.
102      * @param gtu gtu to inquiry
103      * @return whether the provided gtu is the gtu that is synchronized to
104      */
105     boolean isSyncVehicle(final HeadwayGTU gtu)
106     {
107         return this.syncVehicle == null ? false : gtu.getId().equals(this.syncVehicle);
108     }
109 
110     /**
111      * Returns the gtu from the set that is the current sync vehicle, or {@code null} of there is no sync vehicle or it is not
112      * in the set.
113      * @param adjLeaders leaders in adjacent lane
114      * @return gtu from the set that is the current sync vehicle
115      */
116     HeadwayGTU getSyncVehicle(final PerceptionCollectable<HeadwayGTU, LaneBasedGTU> adjLeaders)
117     {
118         if (this.syncVehicle == null)
119         {
120             return null;
121         }
122         for (HeadwayGTU leader : adjLeaders)
123         {
124             if (leader.getId().equals(this.syncVehicle))
125             {
126                 return leader;
127             }
128         }
129         return null;
130     }
131 
132     /**
133      * Returns the synchronization.
134      * @return synchronization
135      */
136     Synchronization getSynchronization()
137     {
138         return this.synchronization;
139     }
140 
141     /**
142      * Returns the cooperation.
143      * @return cooperation
144      */
145     Cooperation getCooperation()
146     {
147         return this.cooperation;
148     }
149 
150     /**
151      * Return the gap-acceptance.
152      * @return gap-acceptance
153      */
154     GapAcceptance getGapAcceptance()
155     {
156         return this.gapAcceptance;
157     }
158     
159     /**
160      * Return the tail gating.
161      * @return gap-acceptance
162      */
163     Tailgating getTailGating()
164     {
165         return this.tailGating;
166     }
167 
168     /** {@inheritDoc} */
169     @Override
170     public Desire getLatestDesire(final Class<? extends Incentive> incentiveClass)
171     {
172         return this.desireMap.get(incentiveClass);
173     }
174 
175     /** {@inheritDoc} */
176     @Override
177     public State getSynchronizationState()
178     {
179         return this.synchronizationState;
180     }
181 
182     /** {@inheritDoc} */
183     @Override
184     public String toString()
185     {
186         return "LmrsData [synchronization=" + this.synchronization + ", leaders=" + this.leaders + ", tempLeaders="
187                 + this.tempLeaders + ", syncVehicle=" + this.syncVehicle + "]";
188     }
189 
190 }