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