1   package org.opentrafficsim.road.gtu.lane.tactical.util.lmrs;
2   
3   import java.util.LinkedHashMap;
4   import java.util.LinkedHashSet;
5   import java.util.Map;
6   import java.util.Set;
7   
8   import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
9   import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable;
10  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
11  import org.opentrafficsim.road.gtu.lane.tactical.DesireBased;
12  import org.opentrafficsim.road.gtu.lane.tactical.Synchronizable;
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  public final class LmrsData implements DesireBased, Synchronizable
26  {
27  
28      
29      private final Synchronization synchronization;
30  
31      
32      private final Cooperation cooperation;
33  
34      
35      private final GapAcceptance gapAcceptance;
36  
37      
38      private final Tailgating tailgating;
39  
40      
41      private final Set<String> leaders = new LinkedHashSet<>();
42  
43      
44      private final Set<String> tempLeaders = new LinkedHashSet<>();
45  
46      
47      private final Map<Class<? extends Incentive>, Desire> desireMap = new LinkedHashMap<>();
48  
49      
50      private Synchronizable.State synchronizationState = Synchronizable.State.NONE;
51  
52      
53      private String syncVehicle;
54  
55      
56      private boolean humanLongitudinalControl = true;
57  
58      
59  
60  
61  
62  
63  
64      public LmrsData(final Synchronization synchronization, final Cooperation cooperation, final GapAcceptance gapAcceptance,
65              final Tailgating tailgating)
66      {
67          this.synchronization = synchronization;
68          this.cooperation = cooperation;
69          this.gapAcceptance = gapAcceptance;
70          this.tailgating = tailgating;
71      }
72  
73      
74  
75  
76  
77  
78      boolean isNewLeader(final HeadwayGTU gtu)
79      {
80          this.tempLeaders.add(gtu.getId());
81          return !this.leaders.contains(gtu.getId());
82      }
83  
84      
85  
86  
87      void finalizeStep()
88      {
89          this.leaders.clear();
90          this.leaders.addAll(this.tempLeaders);
91          this.tempLeaders.clear();
92      }
93  
94      
95  
96  
97  
98      void setSyncVehicle(final HeadwayGTU gtu)
99      {
100         this.syncVehicle = gtu == null ? null : gtu.getId();
101     }
102 
103     
104 
105 
106 
107 
108     boolean isSyncVehicle(final HeadwayGTU gtu)
109     {
110         return this.syncVehicle == null ? false : gtu.getId().equals(this.syncVehicle);
111     }
112 
113     
114 
115 
116 
117 
118 
119     HeadwayGTU getSyncVehicle(final PerceptionCollectable<HeadwayGTU, LaneBasedGTU> adjLeaders)
120     {
121         if (this.syncVehicle == null)
122         {
123             return null;
124         }
125         for (HeadwayGTU leader : adjLeaders)
126         {
127             if (leader.getId().equals(this.syncVehicle))
128             {
129                 return leader;
130             }
131         }
132         return null;
133     }
134 
135     
136 
137 
138 
139     Synchronization getSynchronization()
140     {
141         return this.synchronization;
142     }
143 
144     
145 
146 
147 
148     Cooperation getCooperation()
149     {
150         return this.cooperation;
151     }
152 
153     
154 
155 
156 
157     GapAcceptance getGapAcceptance()
158     {
159         return this.gapAcceptance;
160     }
161 
162     
163 
164 
165 
166     Tailgating getTailgating()
167     {
168         return this.tailgating;
169     }
170 
171     
172     @Override
173     public Desire getLatestDesire(final Class<? extends Incentive> incentiveClass)
174     {
175         return this.desireMap.get(incentiveClass);
176     }
177     
178     
179 
180 
181 
182     Map<Class<? extends Incentive>, Desire> getDesireMap()
183     {
184         return this.desireMap;
185     }
186 
187     
188 
189 
190 
191     void setSynchronizationState(final Synchronizable.State synchronizationState)
192     {
193         this.synchronizationState = synchronizationState;
194     }
195 
196     
197     @Override
198     public Synchronizable.State getSynchronizationState()
199     {
200         return this.synchronizationState;
201     }
202 
203     
204 
205 
206     boolean isHumanLongitudinalControl()
207     {
208         return this.humanLongitudinalControl;
209     }
210 
211     
212 
213 
214     public void setHumanLongitudinalControl(final boolean humanLongitudinalControl)
215     {
216         this.humanLongitudinalControl = humanLongitudinalControl;
217     }
218 
219     
220     @Override
221     public String toString()
222     {
223         return "LmrsData [synchronization=" + this.synchronization + ", leaders=" + this.leaders + ", tempLeaders="
224                 + this.tempLeaders + ", syncVehicle=" + this.syncVehicle + "]";
225     }
226 
227 }