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