1 package org.opentrafficsim.demo.conflictAndControl;
2
3 import java.awt.BorderLayout;
4 import java.awt.Dimension;
5 import java.io.Serializable;
6 import java.net.URL;
7 import java.rmi.RemoteException;
8 import java.util.LinkedHashSet;
9 import java.util.Set;
10
11 import javax.naming.NamingException;
12 import javax.swing.JPanel;
13 import javax.swing.JScrollPane;
14
15 import org.djunits.unit.LengthUnit;
16 import org.djunits.value.vdouble.scalar.Duration;
17 import org.djunits.value.vdouble.scalar.Length;
18 import org.djunits.value.vdouble.scalar.Time;
19 import org.djutils.event.EventInterface;
20 import org.djutils.event.EventListenerInterface;
21 import org.djutils.event.EventTypeInterface;
22 import org.djutils.io.URLResource;
23 import org.opentrafficsim.core.animation.gtu.colorer.DefaultSwitchableGTUColorer;
24 import org.opentrafficsim.core.compatibility.Compatible;
25 import org.opentrafficsim.core.dsol.AbstractOTSModel;
26 import org.opentrafficsim.core.dsol.OTSAnimator;
27 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
28 import org.opentrafficsim.core.gtu.GTUType;
29 import org.opentrafficsim.core.gtu.RelativePosition;
30 import org.opentrafficsim.core.network.NetworkException;
31 import org.opentrafficsim.demo.conflictAndControl.DemoTrafcodAndTurbo.TrafCODModel;
32 import org.opentrafficsim.draw.core.OTSDrawingException;
33 import org.opentrafficsim.draw.road.TrafficLightAnimation;
34 import org.opentrafficsim.road.network.OTSRoadNetwork;
35 import org.opentrafficsim.road.network.factory.xml.parser.XmlNetworkLaneParser;
36 import org.opentrafficsim.road.network.lane.CrossSectionLink;
37 import org.opentrafficsim.road.network.lane.CrossSectionLink.Priority;
38 import org.opentrafficsim.road.network.lane.Lane;
39 import org.opentrafficsim.road.network.lane.conflict.ConflictBuilder;
40 import org.opentrafficsim.road.network.lane.object.sensor.TrafficLightSensor;
41 import org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight;
42 import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
43 import org.opentrafficsim.swing.gui.OTSAnimationPanel;
44 import org.opentrafficsim.swing.gui.OTSSimulationApplication;
45 import org.opentrafficsim.trafficcontrol.TrafficController;
46 import org.opentrafficsim.trafficcontrol.trafcod.TrafCOD;
47
48 import nl.tudelft.simulation.dsol.SimRuntimeException;
49 import nl.tudelft.simulation.language.DSOLException;
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public class DemoTrafcodAndTurbo extends OTSSimulationApplication<TrafCODModel>
64 {
65
66 private static final long serialVersionUID = 20161118L;
67
68
69
70
71
72
73
74
75 public DemoTrafcodAndTurbo(final String title, final OTSAnimationPanel panel, final TrafCODModel model)
76 throws OTSDrawingException
77 {
78 super(model, panel);
79 }
80
81
82
83
84
85 public static void main(final String[] args)
86 {
87 demo(true);
88 }
89
90
91
92
93
94 public static void demo(final boolean exitOnClose)
95 {
96 try
97 {
98 OTSAnimator simulator = new OTSAnimator("DemoTrafcodAndTurbo");
99 final TrafCODModel junctionModel = new TrafCODModel(simulator);
100 simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), junctionModel);
101 OTSAnimationPanel animationPanel =
102 new OTSAnimationPanel(junctionModel.getNetwork().getExtent(), new Dimension(800, 600), simulator,
103 junctionModel, new DefaultSwitchableGTUColorer(), junctionModel.getNetwork());
104 DemoTrafcodAndTurbotrol/DemoTrafcodAndTurbo.html#DemoTrafcodAndTurbo">DemoTrafcodAndTurbo app = new DemoTrafcodAndTurbo("TrafCOD Turbo demo", animationPanel, junctionModel);
105 app.setExitOnClose(exitOnClose);
106 }
107 catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
108 {
109 exception.printStackTrace();
110 }
111 }
112
113
114
115
116 @Override
117 protected void addTabs()
118 {
119 JScrollPane scrollPane = new JScrollPane(getModel().getControllerDisplayPanel());
120 JPanel wrapper = new JPanel(new BorderLayout());
121 wrapper.add(scrollPane);
122 getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount() - 1,
123 getModel().getTrafCOD().getId(), wrapper);
124 }
125
126
127
128
129 static class TrafCODModel extends AbstractOTSModel implements EventListenerInterface
130 {
131
132 private static final long serialVersionUID = 20161020L;
133
134
135 private OTSRoadNetwork network;
136
137
138 private TrafCOD trafCOD;
139
140
141 private JPanel controllerDisplayPanel = new JPanel(new BorderLayout());
142
143
144
145
146 TrafCODModel(final OTSSimulatorInterface simulator)
147 {
148 super(simulator);
149 }
150
151
152 @Override
153 public void constructModel() throws SimRuntimeException
154 {
155 try
156 {
157 URL xmlURL = URLResource.getResource("/conflictAndControl/TurboRoundaboutAndSignal.xml");
158 this.network = new OTSRoadNetwork("TurboRoundaboutAndSignal", true, getSimulator());
159 XmlNetworkLaneParser.build(xmlURL, this.network, false);
160
161
162 ((CrossSectionLink) this.network.getLink("EBNA")).setPriority(Priority.PRIORITY);
163 ((CrossSectionLink) this.network.getLink("NBWA")).setPriority(Priority.PRIORITY);
164 ((CrossSectionLink) this.network.getLink("WBSA")).setPriority(Priority.PRIORITY);
165 ((CrossSectionLink) this.network.getLink("SBEA")).setPriority(Priority.PRIORITY);
166 ConflictBuilder.buildConflicts(this.network, this.network.getGtuType(GTUType.DEFAULTS.VEHICLE), this.simulator,
167 new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)));
168
169
170
171
172
173
174
175
176 String[] directions = {"E", "S", "W", "N"};
177
178 Set<TrafficLight> trafficLights = new LinkedHashSet<>();
179 Set<TrafficLightSensor> sensors = new LinkedHashSet<>();
180 Length stopLineMargin = new Length(0.1, LengthUnit.METER);
181 Length headDetectorLength = new Length(1, LengthUnit.METER);
182 Length headDetectorMargin = stopLineMargin.plus(headDetectorLength).plus(new Length(3, LengthUnit.METER));
183 Length longDetectorLength = new Length(30, LengthUnit.METER);
184 Length longDetectorMargin = stopLineMargin.plus(longDetectorLength).plus(new Length(10, LengthUnit.METER));
185 int stream = 1;
186 for (String direction : directions)
187 {
188 for (int laneNumber = 3; laneNumber >= 1; laneNumber--)
189 {
190 Lane lane = (Lane) ((CrossSectionLink) this.network.getLink(direction + "S", direction + "C"))
191 .getCrossSectionElement("FORWARD" + laneNumber);
192 if (lane != null)
193 {
194 if (stream != 7)
195 {
196 TrafficLight tl = new SimpleTrafficLight(String.format("%02d", stream), lane,
197 lane.getLength().minus(stopLineMargin), this.simulator);
198 trafficLights.add(tl);
199
200 try
201 {
202 new TrafficLightAnimation(tl, this.simulator);
203 }
204 catch (RemoteException | NamingException exception)
205 {
206 throw new NetworkException(exception);
207 }
208
209 sensors.add(new TrafficLightSensor(String.format("D%02d1", stream), lane,
210 lane.getLength().minus(headDetectorMargin), lane,
211 lane.getLength().minus(headDetectorMargin).plus(headDetectorLength), null,
212 RelativePosition.FRONT, RelativePosition.REAR, this.simulator, Compatible.EVERYTHING));
213 sensors.add(new TrafficLightSensor(String.format("D%02d2", stream), lane,
214 lane.getLength().minus(longDetectorMargin), lane,
215 lane.getLength().minus(longDetectorMargin).plus(longDetectorLength), null,
216 RelativePosition.FRONT, RelativePosition.REAR, this.simulator, Compatible.EVERYTHING));
217 }
218 else
219 {
220 lane = (Lane) ((CrossSectionLink) this.network.getLink("ESS1", "ESS"))
221 .getCrossSectionElement("FORWARD");
222 TrafficLight tl = new SimpleTrafficLight(String.format("%02d", stream), lane,
223 lane.getLength().minus(stopLineMargin), this.simulator);
224 trafficLights.add(tl);
225
226 try
227 {
228 new TrafficLightAnimation(tl, this.simulator);
229 }
230 catch (RemoteException | NamingException exception)
231 {
232 throw new NetworkException(exception);
233 }
234
235 sensors.add(new TrafficLightSensor(String.format("D%02d1", stream), lane,
236 lane.getLength().minus(headDetectorMargin), lane,
237 lane.getLength().minus(headDetectorMargin).plus(headDetectorLength), null,
238 RelativePosition.FRONT, RelativePosition.REAR, this.simulator, Compatible.EVERYTHING));
239 sensors.add(new TrafficLightSensor(String.format("D%02d2", stream), lane,
240 lane.getLength().minus(longDetectorMargin), lane,
241 lane.getLength().minus(longDetectorMargin).plus(longDetectorLength), null,
242 RelativePosition.FRONT, RelativePosition.REAR, this.simulator, Compatible.EVERYTHING));
243
244 }
245
246 }
247 stream++;
248 }
249 }
250 String controllerName = "Not so simple TrafCOD controller";
251 this.trafCOD = new TrafCOD(controllerName, URLResource.getResource("/conflictAndControl/Intersection12Dir.tfc"),
252 this.simulator, this.controllerDisplayPanel, null, null);
253 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING);
254 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING);
255 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED);
256 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_STATE_CHANGED);
257 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_VARIABLE_CREATED);
258 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED);
259
260
261 addListener(this.trafCOD, TrafficController.TRAFFICCONTROL_SET_TRACING);
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281 }
282 catch (Exception exception)
283 {
284 exception.printStackTrace();
285 }
286 }
287
288
289 @Override
290 public final OTSRoadNetwork getNetwork()
291 {
292 return this.network;
293 }
294
295
296
297
298 public final TrafCOD getTrafCOD()
299 {
300 return this.trafCOD;
301 }
302
303
304
305
306 public final JPanel getControllerDisplayPanel()
307 {
308 return this.controllerDisplayPanel;
309 }
310
311
312 @Override
313 public void notify(final EventInterface event) throws RemoteException
314 {
315 EventTypeInterface type = event.getType();
316 Object[] payload = (Object[]) event.getContent();
317 if (TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING.equals(type))
318 {
319
320
321 return;
322 }
323 else if (TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED.equals(type))
324 {
325 System.out.println("Conflict group changed from " + ((String) payload[1]) + " to " + ((String) payload[2]));
326 }
327 else if (TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED.equals(type))
328 {
329 System.out.println(String.format("Variable changed %s <- %d %s", payload[1], payload[4], payload[5]));
330 }
331 else if (TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING.equals(type))
332 {
333 System.out.println("Warning " + payload[1]);
334 }
335 else
336 {
337 System.out.print("TrafCODDemo received event of type " + event.getType() + ", payload [");
338 String separator = "";
339 for (Object o : payload)
340 {
341 System.out.print(separator + o);
342 separator = ",";
343 }
344 System.out.println("]");
345 }
346 }
347
348
349 @Override
350 public Serializable getSourceId()
351 {
352 return "TrafCODModel";
353 }
354 }
355 }