1 package org.opentrafficsim.road.network.factory.opendrive;
2
3 import java.io.Serializable;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.Map;
7 import java.util.Set;
8
9 import javax.naming.NamingException;
10
11 import org.djunits.unit.DurationUnit;
12 import org.djunits.value.vdouble.scalar.Duration;
13 import org.opentrafficsim.core.gtu.GTUException;
14 import org.opentrafficsim.core.network.NetworkException;
15 import org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight;
16 import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightColor;
17
18 import nl.tudelft.simulation.dsol.SimRuntimeException;
19 import nl.tudelft.simulation.dsol.simulators.AnimatorInterface;
20 import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
21
22
23
24
25
26
27
28
29
30
31
32
33 public class Controller implements Serializable
34 {
35
36
37 private static final long serialVersionUID = 20150715L;
38
39
40 private DEVSSimulatorInterface.TimeDoubleUnit simulator;
41
42
43 private String id;
44
45
46 private Map<Integer, Set<SimpleTrafficLight>> trafficLights = new HashMap<Integer, Set<SimpleTrafficLight>>();
47
48
49
50
51
52
53
54
55 public Controller(final String name, final DEVSSimulatorInterface.TimeDoubleUnit simulator)
56 throws GTUException, NetworkException, NamingException
57 {
58 this.id = name;
59 this.simulator = simulator;
60
61 try
62 {
63
64
65 if (simulator instanceof AnimatorInterface)
66 {
67
68 }
69 simulator.scheduleEventRel(Duration.ZERO, this, this, "unBlock", null);
70 }
71 catch (SimRuntimeException exception)
72 {
73 exception.printStackTrace();
74 }
75 }
76
77
78
79
80 protected void unBlock()
81 {
82 try
83 {
84 this.simulator.scheduleEventRel(new Duration(96.0, DurationUnit.SECOND), this, this, "unBlock", null);
85 }
86 catch (SimRuntimeException exception)
87 {
88 exception.printStackTrace();
89 }
90
91 if (this.trafficLights.containsKey(3))
92 {
93 for (SimpleTrafficLight light : this.trafficLights.get(3))
94 {
95 try
96 {
97
98
99 this.simulator.scheduleEventRel(Duration.ZERO, this, light, "setTrafficLightColor",
100 new Object[] { TrafficLightColor.GREEN });
101 this.simulator.scheduleEventRel(new Duration(15.0, DurationUnit.SECOND), this, light,
102 "setTrafficLightColor", new Object[] { TrafficLightColor.YELLOW });
103 this.simulator.scheduleEventRel(new Duration(24.0, DurationUnit.SECOND), this, light,
104 "setTrafficLightColor", new Object[] { TrafficLightColor.RED });
105 }
106 catch (SimRuntimeException exception)
107 {
108 exception.printStackTrace();
109 }
110 }
111 }
112 if (this.trafficLights.containsKey(4))
113 {
114 for (SimpleTrafficLight light : this.trafficLights.get(4))
115 {
116 try
117 {
118 this.simulator.scheduleEventRel(Duration.ZERO, this, light, "setTrafficLightColor",
119 new Object[] { TrafficLightColor.GREEN });
120 this.simulator.scheduleEventRel(new Duration(15.0, DurationUnit.SECOND), this, light,
121 "setTrafficLightColor", new Object[] { TrafficLightColor.YELLOW });
122 this.simulator.scheduleEventRel(new Duration(24.0, DurationUnit.SECOND), this, light,
123 "setTrafficLightColor", new Object[] { TrafficLightColor.RED });
124 }
125 catch (SimRuntimeException exception)
126 {
127 exception.printStackTrace();
128 }
129 }
130 }
131 if (this.trafficLights.containsKey(6))
132 {
133 for (SimpleTrafficLight light : this.trafficLights.get(6))
134 {
135 try
136 {
137
138
139 this.simulator.scheduleEventRel(new Duration(24.0, DurationUnit.SECOND), this, light,
140 "setTrafficLightColor", new Object[] { TrafficLightColor.GREEN });
141 this.simulator.scheduleEventRel(new Duration(39.0, DurationUnit.SECOND), this, light,
142 "setTrafficLightColor", new Object[] { TrafficLightColor.YELLOW });
143 this.simulator.scheduleEventRel(new Duration(48.0, DurationUnit.SECOND), this, light,
144 "setTrafficLightColor", new Object[] { TrafficLightColor.RED });
145 }
146 catch (SimRuntimeException exception)
147 {
148 exception.printStackTrace();
149 }
150 }
151 }
152 if (this.trafficLights.containsKey(7))
153 {
154 for (SimpleTrafficLight light : this.trafficLights.get(7))
155 {
156 try
157 {
158 this.simulator.scheduleEventRel(new Duration(24.0, DurationUnit.SECOND), this, light,
159 "setTrafficLightColor", new Object[] { TrafficLightColor.GREEN });
160 this.simulator.scheduleEventRel(new Duration(39.0, DurationUnit.SECOND), this, light,
161 "setTrafficLightColor", new Object[] { TrafficLightColor.YELLOW });
162 this.simulator.scheduleEventRel(new Duration(48.0, DurationUnit.SECOND), this, light,
163 "setTrafficLightColor", new Object[] { TrafficLightColor.RED });
164 }
165 catch (SimRuntimeException exception)
166 {
167 exception.printStackTrace();
168 }
169 }
170 }
171 if (this.trafficLights.containsKey(9))
172 {
173 for (SimpleTrafficLight light : this.trafficLights.get(9))
174 {
175 try
176 {
177
178
179 this.simulator.scheduleEventRel(new Duration(48.0, DurationUnit.SECOND), this, light,
180 "setTrafficLightColor", new Object[] { TrafficLightColor.GREEN });
181 this.simulator.scheduleEventRel(new Duration(63.0, DurationUnit.SECOND), this, light,
182 "setTrafficLightColor", new Object[] { TrafficLightColor.YELLOW });
183 this.simulator.scheduleEventRel(new Duration(72.0, DurationUnit.SECOND), this, light,
184 "setTrafficLightColor", new Object[] { TrafficLightColor.RED });
185 }
186 catch (SimRuntimeException exception)
187 {
188 exception.printStackTrace();
189 }
190 }
191 }
192 if (this.trafficLights.containsKey(10))
193 {
194 for (SimpleTrafficLight light : this.trafficLights.get(10))
195 {
196 try
197 {
198 this.simulator.scheduleEventRel(new Duration(48.0, DurationUnit.SECOND), this, light,
199 "setTrafficLightColor", new Object[] { TrafficLightColor.GREEN });
200 this.simulator.scheduleEventRel(new Duration(63.0, DurationUnit.SECOND), this, light,
201 "setTrafficLightColor", new Object[] { TrafficLightColor.YELLOW });
202 this.simulator.scheduleEventRel(new Duration(72.0, DurationUnit.SECOND), this, light,
203 "setTrafficLightColor", new Object[] { TrafficLightColor.RED });
204 }
205 catch (SimRuntimeException exception)
206 {
207 exception.printStackTrace();
208 }
209 }
210 }
211 if (this.trafficLights.containsKey(12))
212 {
213 for (SimpleTrafficLight light : this.trafficLights.get(12))
214 {
215 try
216 {
217
218
219 this.simulator.scheduleEventRel(new Duration(72.0, DurationUnit.SECOND), this, light,
220 "setTrafficLightColor", new Object[] { TrafficLightColor.GREEN });
221 this.simulator.scheduleEventRel(new Duration(87.0, DurationUnit.SECOND), this, light,
222 "setTrafficLightColor", new Object[] { TrafficLightColor.YELLOW });
223 this.simulator.scheduleEventRel(new Duration(96.0, DurationUnit.SECOND), this, light,
224 "setTrafficLightColor", new Object[] { TrafficLightColor.RED });
225 }
226 catch (SimRuntimeException exception)
227 {
228 exception.printStackTrace();
229 }
230 }
231 }
232 if (this.trafficLights.containsKey(13))
233 {
234 for (SimpleTrafficLight light : this.trafficLights.get(13))
235 {
236 try
237 {
238 this.simulator.scheduleEventRel(new Duration(72.0, DurationUnit.SECOND), this, light,
239 "setTrafficLightColor", new Object[] { TrafficLightColor.GREEN });
240 this.simulator.scheduleEventRel(new Duration(87.0, DurationUnit.SECOND), this, light,
241 "setTrafficLightColor", new Object[] { TrafficLightColor.YELLOW });
242 this.simulator.scheduleEventRel(new Duration(96.0, DurationUnit.SECOND), this, light,
243 "setTrafficLightColor", new Object[] { TrafficLightColor.RED });
244 }
245 catch (SimRuntimeException exception)
246 {
247 exception.printStackTrace();
248 }
249 }
250 }
251 }
252
253
254
255
256
257 public void addTrafficLight(int sequence2, SimpleTrafficLight trafficLight)
258 {
259 if (!this.trafficLights.containsKey(sequence2))
260 {
261 Set<SimpleTrafficLight> trafficLightsSet = new HashSet<SimpleTrafficLight>();
262 this.trafficLights.put(sequence2, trafficLightsSet);
263 }
264 this.trafficLights.get(sequence2).add(trafficLight);
265 }
266
267
268 @Override
269 public final String toString()
270 {
271 return "Controller [id=" + this.id + ", trafficLights=" + this.trafficLights + "]";
272 }
273
274 }