1 package org.opentrafficsim.demo.trafficcontrol;
2
3 import java.awt.BorderLayout;
4 import java.awt.Dimension;
5 import java.net.URL;
6 import java.rmi.RemoteException;
7 import java.util.HashSet;
8 import java.util.Set;
9
10 import javax.naming.NamingException;
11 import javax.swing.JPanel;
12 import javax.swing.JScrollPane;
13
14 import org.djunits.unit.LengthUnit;
15 import org.djunits.value.vdouble.scalar.Duration;
16 import org.djunits.value.vdouble.scalar.Length;
17 import org.djunits.value.vdouble.scalar.Time;
18 import org.djutils.io.URLResource;
19 import org.opentrafficsim.core.compatibility.Compatible;
20 import org.opentrafficsim.core.dsol.AbstractOTSModel;
21 import org.opentrafficsim.core.dsol.OTSAnimator;
22 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
23 import org.opentrafficsim.core.gtu.RelativePosition;
24 import org.opentrafficsim.core.network.NetworkException;
25 import org.opentrafficsim.core.network.OTSNetwork;
26 import org.opentrafficsim.demo.trafficcontrol.TrafCODDemo.TrafCODModel;
27 import org.opentrafficsim.draw.core.OTSDrawingException;
28 import org.opentrafficsim.draw.road.TrafficLightAnimation;
29 import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser;
30 import org.opentrafficsim.road.network.lane.CrossSectionLink;
31 import org.opentrafficsim.road.network.lane.Lane;
32 import org.opentrafficsim.road.network.lane.object.sensor.TrafficLightSensor;
33 import org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight;
34 import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
35 import org.opentrafficsim.swing.gui.OTSAnimationPanel;
36 import org.opentrafficsim.swing.gui.OTSSimulationApplication;
37 import org.opentrafficsim.trafficcontrol.TrafficController;
38 import org.opentrafficsim.trafficcontrol.trafcod.TrafCOD;
39
40 import nl.tudelft.simulation.dsol.SimRuntimeException;
41 import nl.tudelft.simulation.event.EventInterface;
42 import nl.tudelft.simulation.event.EventListenerInterface;
43 import nl.tudelft.simulation.event.EventType;
44
45
46
47
48
49
50
51
52
53
54
55 public class TrafCODDemo extends OTSSimulationApplication<TrafCODModel>
56 {
57
58 private static final long serialVersionUID = 20161118L;
59
60
61
62
63
64
65
66
67 public TrafCODDemo(final String title, final OTSAnimationPanel panel, final TrafCODModel model) throws OTSDrawingException
68 {
69 super(model, panel);
70 }
71
72
73
74
75
76 public static void main(final String[] args)
77 {
78 demo(true);
79 }
80
81
82
83
84
85 public static void demo(final boolean exitOnClose)
86 {
87 try
88 {
89 OTSAnimator simulator = new OTSAnimator();
90 final TrafCODModel trafcodModel = new TrafCODModel(simulator);
91 simulator.initialize(Time.ZERO, Duration.ZERO, Duration.createSI(3600.0), trafcodModel);
92 OTSAnimationPanel animationPanel = new OTSAnimationPanel(trafcodModel.getNetwork().getExtent(),
93 new Dimension(800, 600), simulator, trafcodModel, DEFAULT_COLORER, trafcodModel.getNetwork());
94 TrafCODDemo app = new TrafCODDemo("TrafCOD demo simple crossing", animationPanel, trafcodModel);
95 app.setExitOnClose(exitOnClose);
96 }
97 catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException exception)
98 {
99 exception.printStackTrace();
100 }
101 }
102
103
104
105
106 protected void addTabs()
107 {
108 JScrollPane scrollPane = new JScrollPane(getModel().getControllerDisplayPanel());
109 JPanel wrapper = new JPanel(new BorderLayout());
110 wrapper.add(scrollPane);
111 getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount() - 1,
112 getModel().getTrafCOD().getId(), wrapper);
113 }
114
115
116
117
118 static class TrafCODModel extends AbstractOTSModel implements EventListenerInterface
119 {
120
121 private static final long serialVersionUID = 20161020L;
122
123
124 private OTSNetwork network;
125
126
127 private TrafCOD trafCOD;
128
129
130 private JPanel controllerDisplayPanel = new JPanel(new BorderLayout());
131
132
133
134
135 TrafCODModel(final OTSSimulatorInterface simulator)
136 {
137 super(simulator);
138 }
139
140
141 @Override
142 public void constructModel() throws SimRuntimeException
143 {
144 try
145 {
146 URL url = URLResource.getResource("/TrafCODDemo1/TrafCODDemo1.xml");
147 XmlNetworkLaneParser nlp = new XmlNetworkLaneParser(getSimulator());
148 this.network = nlp.build(url, true);
149
150 Lane laneNX = (Lane) ((CrossSectionLink) this.network.getLink("N", "X")).getCrossSectionElement("FORWARD");
151 Lane laneWX = (Lane) ((CrossSectionLink) this.network.getLink("W", "X")).getCrossSectionElement("FORWARD");
152 Set<TrafficLight> trafficLights = new HashSet<>();
153 SimpleTrafficLight tl08 =
154 new SimpleTrafficLight("TL08", laneWX, new Length(296, LengthUnit.METER), getSimulator());
155 trafficLights.add(tl08);
156
157 try
158 {
159 new TrafficLightAnimation(tl08, this.simulator);
160 }
161 catch (RemoteException | NamingException exception)
162 {
163 throw new NetworkException(exception);
164 }
165
166 SimpleTrafficLight tl11 =
167 new SimpleTrafficLight("TL11", laneNX, new Length(296, LengthUnit.METER), getSimulator());
168 trafficLights.add(tl11);
169
170 try
171 {
172 new TrafficLightAnimation(tl11, this.simulator);
173 }
174 catch (RemoteException | NamingException exception)
175 {
176 throw new NetworkException(exception);
177 }
178
179 Set<TrafficLightSensor> sensors = new HashSet<>();
180 sensors.add(new TrafficLightSensor("D081", laneWX, new Length(292, LengthUnit.METER), laneWX,
181 new Length(294, LengthUnit.METER), null, RelativePosition.FRONT, RelativePosition.REAR, getSimulator(),
182 Compatible.EVERYTHING));
183 sensors.add(new TrafficLightSensor("D082", laneWX, new Length(260, LengthUnit.METER), laneWX,
184 new Length(285, LengthUnit.METER), null, RelativePosition.FRONT, RelativePosition.REAR, getSimulator(),
185 Compatible.EVERYTHING));
186 sensors.add(new TrafficLightSensor("D111", laneNX, new Length(292, LengthUnit.METER), laneNX,
187 new Length(294, LengthUnit.METER), null, RelativePosition.FRONT, RelativePosition.REAR, getSimulator(),
188 Compatible.EVERYTHING));
189 sensors.add(new TrafficLightSensor("D112", laneNX, new Length(260, LengthUnit.METER), laneNX,
190 new Length(285, LengthUnit.METER), null, RelativePosition.FRONT, RelativePosition.REAR, getSimulator(),
191 Compatible.EVERYTHING));
192 String controllerName = "Simple TrafCOD controller";
193 this.trafCOD = new TrafCOD(controllerName, URLResource.getResource("/TrafCODDemo1/simpleTest.tfc"),
194 trafficLights, sensors, getSimulator(), this.controllerDisplayPanel);
195 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING);
196 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING);
197 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED);
198 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_STATE_CHANGED);
199 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_VARIABLE_CREATED);
200 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED);
201
202 addListener(this.trafCOD, TrafficController.TRAFFICCONTROL_SET_TRACING);
203
204
205
206
207
208
209
210
211
212
213
214 }
215 catch (Exception exception)
216 {
217 exception.printStackTrace();
218 }
219 }
220
221
222 @Override
223 public final OTSNetwork getNetwork()
224 {
225 return this.network;
226 }
227
228
229
230
231 public final TrafCOD getTrafCOD()
232 {
233 return this.trafCOD;
234 }
235
236
237
238
239 public final JPanel getControllerDisplayPanel()
240 {
241 return this.controllerDisplayPanel;
242 }
243
244
245 @Override
246 public void notify(final EventInterface event) throws RemoteException
247 {
248 EventType type = event.getType();
249 Object[] payload = (Object[]) event.getContent();
250 if (TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING.equals(type))
251 {
252
253 return;
254 }
255 else if (TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED.equals(type))
256 {
257 System.out.println("Conflict group changed from " + ((String) payload[1]) + " to " + ((String) payload[2]));
258 }
259 else if (TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED.equals(type))
260 {
261 System.out.println(String.format("Variable changed %s <- %d %s", payload[1], payload[4], payload[5]));
262 }
263 else if (TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING.equals(type))
264 {
265 System.out.println("Warning " + payload[1]);
266 }
267 else
268 {
269 System.out.print("TrafCODDemo received event of type " + event.getType() + ", payload [");
270 String separator = "";
271 for (Object o : payload)
272 {
273 System.out.print(separator + o);
274 separator = ",";
275 }
276 System.out.println("]");
277 }
278 }
279
280 }
281
282 }