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.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 HashSet<>();
42
43
44 private final Set<String> tempLeaders = new HashSet<>();
45
46
47 final Map<Class<? extends Incentive>, Desire> desireMap = new HashMap<>();
48
49
50 Synchronizable.State synchronizationState;
51
52
53 private String syncVehicle;
54
55
56
57
58
59
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
72
73
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
83
84 void finalizeStep()
85 {
86 this.leaders.clear();
87 this.leaders.addAll(this.tempLeaders);
88 this.tempLeaders.clear();
89 }
90
91
92
93
94
95 void setSyncVehicle(final HeadwayGTU gtu)
96 {
97 this.syncVehicle = gtu == null ? null : gtu.getId();
98 }
99
100
101
102
103
104
105 boolean isSyncVehicle(final HeadwayGTU gtu)
106 {
107 return this.syncVehicle == null ? false : gtu.getId().equals(this.syncVehicle);
108 }
109
110
111
112
113
114
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
134
135
136 Synchronization getSynchronization()
137 {
138 return this.synchronization;
139 }
140
141
142
143
144
145 Cooperation getCooperation()
146 {
147 return this.cooperation;
148 }
149
150
151
152
153
154 GapAcceptance getGapAcceptance()
155 {
156 return this.gapAcceptance;
157 }
158
159
160
161
162
163 Tailgating getTailGating()
164 {
165 return this.tailGating;
166 }
167
168
169 @Override
170 public Desire getLatestDesire(final Class<? extends Incentive> incentiveClass)
171 {
172 return this.desireMap.get(incentiveClass);
173 }
174
175
176 @Override
177 public Synchronizable.State getSynchronizationState()
178 {
179 return this.synchronizationState;
180 }
181
182
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 }