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.Optional;
7 import java.util.Set;
8
9 import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
10 import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable;
11 import org.opentrafficsim.road.gtu.lane.perception.object.PerceivedGtu;
12 import org.opentrafficsim.road.gtu.lane.tactical.DesireBased;
13 import org.opentrafficsim.road.gtu.lane.tactical.Synchronizable;
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
65 public LmrsData(final Synchronization synchronization, final Cooperation cooperation, final GapAcceptance gapAcceptance,
66 final Tailgating tailgating)
67 {
68 this.synchronization = synchronization;
69 this.cooperation = cooperation;
70 this.gapAcceptance = gapAcceptance;
71 this.tailgating = tailgating;
72 }
73
74
75
76
77
78
79 boolean isNewLeader(final PerceivedGtu gtu)
80 {
81 this.tempLeaders.add(gtu.getId());
82 return !this.leaders.contains(gtu.getId());
83 }
84
85
86
87
88 void finalizeStep()
89 {
90 this.leaders.clear();
91 this.leaders.addAll(this.tempLeaders);
92 this.tempLeaders.clear();
93 }
94
95
96
97
98
99 void setSyncVehicle(final PerceivedGtu gtu)
100 {
101 this.syncVehicle = gtu == null ? null : gtu.getId();
102 }
103
104
105
106
107
108
109 boolean isSyncVehicle(final PerceivedGtu gtu)
110 {
111 return this.syncVehicle == null ? false : gtu.getId().equals(this.syncVehicle);
112 }
113
114
115
116
117
118
119
120 PerceivedGtu getSyncVehicle(final PerceptionCollectable<PerceivedGtu, LaneBasedGtu> adjLeaders)
121 {
122 if (this.syncVehicle == null)
123 {
124 return null;
125 }
126 for (PerceivedGtu leader : adjLeaders)
127 {
128 if (leader.getId().equals(this.syncVehicle))
129 {
130 return leader;
131 }
132 }
133 return null;
134 }
135
136
137
138
139
140 Synchronization getSynchronization()
141 {
142 return this.synchronization;
143 }
144
145
146
147
148
149 Cooperation getCooperation()
150 {
151 return this.cooperation;
152 }
153
154
155
156
157
158 GapAcceptance getGapAcceptance()
159 {
160 return this.gapAcceptance;
161 }
162
163
164
165
166
167 Tailgating getTailgating()
168 {
169 return this.tailgating;
170 }
171
172 @Override
173 public Optional<Desire> getLatestDesire(final Class<? extends Incentive> incentiveClass)
174 {
175 return Optional.ofNullable(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 @Override
197 public Synchronizable.State getSynchronizationState()
198 {
199 return this.synchronizationState;
200 }
201
202
203
204
205
206 boolean isHumanLongitudinalControl()
207 {
208 return this.humanLongitudinalControl;
209 }
210
211
212
213
214
215 public void setHumanLongitudinalControl(final boolean humanLongitudinalControl)
216 {
217 this.humanLongitudinalControl = humanLongitudinalControl;
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 }