1 package org.opentrafficsim.road.gtu.lane.tactical.cacc;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.opentrafficsim.core.gtu.GTU;
7 import org.opentrafficsim.core.network.LateralDirectionality;
8
9 import nl.tudelft.simulation.dsol.SimRuntimeException;
10
11
12
13
14
15
16
17
18
19
20
21
22 public class Platoon
23 {
24
25
26 private final List<String> gtuList = new ArrayList<>();
27
28
29 private LateralDirectionality laneChangeDir = LateralDirectionality.NONE;
30
31
32 private List<String> changedGtus = new ArrayList<>();
33
34
35
36
37
38 public void addGtu(final String id)
39 {
40 this.gtuList.add(id);
41 }
42
43
44
45
46
47 public int size()
48 {
49 return this.gtuList.size();
50 }
51
52
53
54
55
56
57 public String getId(int index)
58 {
59 return this.gtuList.get(index);
60 }
61
62
63
64
65
66 public void addLaneChange(final GTU gtu)
67 {
68 this.changedGtus.add(gtu.getId());
69 if (this.changedGtus.size() == size())
70 {
71
72 try
73 {
74 gtu.getSimulator().scheduleEventNow(this, this, "endLaneChangeProcess", null);
75 }
76 catch (SimRuntimeException exception)
77 {
78 throw new RuntimeException(exception);
79 }
80 }
81 }
82
83
84
85
86 @SuppressWarnings("unused")
87 private void endLaneChangeProcess()
88 {
89 this.laneChangeDir = LateralDirectionality.NONE;
90 this.changedGtus.clear();
91 }
92
93
94
95
96
97 public boolean canInitiateLaneChangeProcess()
98 {
99 return this.laneChangeDir.isNone();
100 }
101
102
103
104
105
106 public void initiateLaneChange(final LateralDirectionality laneChangeDirection)
107 {
108 this.laneChangeDir = laneChangeDirection;
109 }
110
111
112
113
114
115
116 public int getIndex(final String gtuId)
117 {
118 return this.gtuList.indexOf(gtuId);
119 }
120
121
122
123
124
125
126 public boolean isInPlatoon(final String gtuId)
127 {
128 return this.gtuList.contains(gtuId);
129 }
130
131
132
133
134
135
136 public LateralDirectionality shouldChangeLane(final String gtuId)
137 {
138 if (this.changedGtus.isEmpty())
139 {
140 return size() == (getIndex(gtuId) + 1) ? this.laneChangeDir : LateralDirectionality.NONE;
141 }
142 else if (this.changedGtus.contains(gtuId))
143 {
144 return LateralDirectionality.NONE;
145 }
146 else
147 {
148 return this.laneChangeDir;
149 }
150
151 }
152
153
154
155
156
157 public boolean laneChangeInProgress()
158 {
159 return !this.changedGtus.isEmpty();
160 }
161
162
163
164
165
166 public int numberOfChanged()
167 {
168 return this.changedGtus.size();
169 }
170
171 }