1 package org.opentrafficsim.demo.trafficcontrol;
2
3 import java.awt.BorderLayout;
4 import java.net.URL;
5 import java.rmi.RemoteException;
6 import java.util.ArrayList;
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 import javax.swing.SwingUtilities;
14
15 import org.djunits.unit.DurationUnit;
16 import org.djunits.unit.LengthUnit;
17 import org.djunits.value.vdouble.scalar.Duration;
18 import org.djunits.value.vdouble.scalar.Length;
19 import org.djunits.value.vdouble.scalar.Time;
20 import org.opentrafficsim.base.modelproperties.Property;
21 import org.opentrafficsim.base.modelproperties.PropertyException;
22 import org.opentrafficsim.core.compatibility.Compatible;
23 import org.opentrafficsim.core.dsol.OTSModelInterface;
24 import org.opentrafficsim.core.gtu.RelativePosition;
25 import org.opentrafficsim.core.network.OTSNetwork;
26 import org.opentrafficsim.road.animation.AnimationToggles;
27 import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser;
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.road.network.lane.object.trafficlight.TrafficLight;
33 import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
34 import org.opentrafficsim.simulationengine.OTSSimulationException;
35 import org.opentrafficsim.simulationengine.SimpleSimulatorInterface;
36 import org.opentrafficsim.trafficcontrol.TrafficController;
37 import org.opentrafficsim.trafficcontrol.trafcod.TrafCOD;
38
39 import nl.tudelft.simulation.dsol.SimRuntimeException;
40 import nl.tudelft.simulation.dsol.simtime.SimTimeDoubleUnit;
41 import nl.tudelft.simulation.dsol.simulators.DEVSSimulator;
42 import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
43 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
44 import nl.tudelft.simulation.event.EventInterface;
45 import nl.tudelft.simulation.event.EventListenerInterface;
46 import nl.tudelft.simulation.event.EventProducer;
47 import nl.tudelft.simulation.event.EventType;
48 import nl.tudelft.simulation.language.io.URLResource;
49
50
51
52
53
54
55
56
57
58
59
60 public class TrafCODDemo extends AbstractWrappableAnimation
61 {
62
63
64 private static final long serialVersionUID = 20161118L;
65
66
67
68
69
70
71 public static void main(final String[] args) throws SimRuntimeException
72 {
73 SwingUtilities.invokeLater(new Runnable()
74 {
75 @Override
76 public void run()
77 {
78 try
79 {
80 TrafCODDemo model = new TrafCODDemo();
81
82 model.buildAnimator(Time.ZERO, Duration.ZERO, new Duration(60.0, DurationUnit.MINUTE),
83 new ArrayList<Property<?>>(), null, true);
84 }
85 catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
86 {
87 exception.printStackTrace();
88 }
89 }
90 });
91 }
92
93
94 private JPanel controllerDisplayPanel = new JPanel(new BorderLayout());
95
96
97 private TrafCOD trafCOD;
98
99
100 @Override
101 public final String shortName()
102 {
103 return "TrafCOD demonstration 1";
104 }
105
106
107 @Override
108 public final String description()
109 {
110 return "TrafCOD demonstration";
111 }
112
113
114 @Override
115 protected final void addTabs(final SimpleSimulatorInterface simulator) throws OTSSimulationException, PropertyException
116 {
117 JScrollPane scrollPane = new JScrollPane(TrafCODDemo.this.controllerDisplayPanel);
118 JPanel wrapper = new JPanel(new BorderLayout());
119 wrapper.add(scrollPane);
120 addTab(getTabCount() - 1, this.trafCOD.getId(), wrapper);
121 }
122
123
124 @Override
125 protected final OTSModelInterface makeModel() throws OTSSimulationException
126 {
127 return new TrafCODModel();
128 }
129
130
131 @Override
132 protected final void addAnimationToggles()
133 {
134 AnimationToggles.setTextAnimationTogglesStandard(this);
135 }
136
137
138
139
140 class TrafCODModel extends EventProducer implements OTSModelInterface, EventListenerInterface
141 {
142
143 private static final long serialVersionUID = 20161020L;
144
145
146 private OTSNetwork network;
147
148 @SuppressWarnings("synthetic-access")
149 @Override
150 public void constructModel(final SimulatorInterface<Time, Duration, SimTimeDoubleUnit> theSimulator)
151 throws SimRuntimeException
152 {
153 try
154 {
155 URL url = URLResource.getResource("/TrafCODDemo1/TrafCODDemo1.xml");
156 XmlNetworkLaneParser nlp = new XmlNetworkLaneParser((DEVSSimulatorInterface.TimeDoubleUnit) theSimulator);
157 this.network = nlp.build(url, true);
158
159 Lane laneNX = (Lane) ((CrossSectionLink) this.network.getLink("N", "X")).getCrossSectionElement("FORWARD");
160 Lane laneWX = (Lane) ((CrossSectionLink) this.network.getLink("W", "X")).getCrossSectionElement("FORWARD");
161 Set<TrafficLight> trafficLights = new HashSet<>();
162 trafficLights.add(new SimpleTrafficLight("TL08", laneWX, new Length(296, LengthUnit.METER),
163 (DEVSSimulatorInterface.TimeDoubleUnit) theSimulator));
164 trafficLights.add(new SimpleTrafficLight("TL11", laneNX, new Length(296, LengthUnit.METER),
165 (DEVSSimulatorInterface.TimeDoubleUnit) theSimulator));
166 Set<TrafficLightSensor> sensors = new HashSet<>();
167 sensors.add(new TrafficLightSensor("D081", laneWX, new Length(292, LengthUnit.METER), laneWX,
168 new Length(294, LengthUnit.METER), null, RelativePosition.FRONT, RelativePosition.REAR,
169 (DEVSSimulatorInterface.TimeDoubleUnit) theSimulator, Compatible.EVERYTHING));
170 sensors.add(new TrafficLightSensor("D082", laneWX, new Length(260, LengthUnit.METER), laneWX,
171 new Length(285, LengthUnit.METER), null, RelativePosition.FRONT, RelativePosition.REAR,
172 (DEVSSimulatorInterface.TimeDoubleUnit) theSimulator, Compatible.EVERYTHING));
173 sensors.add(new TrafficLightSensor("D111", laneNX, new Length(292, LengthUnit.METER), laneNX,
174 new Length(294, LengthUnit.METER), null, RelativePosition.FRONT, RelativePosition.REAR,
175 (DEVSSimulatorInterface.TimeDoubleUnit) theSimulator, Compatible.EVERYTHING));
176 sensors.add(new TrafficLightSensor("D112", laneNX, new Length(260, LengthUnit.METER), laneNX,
177 new Length(285, LengthUnit.METER), null, RelativePosition.FRONT, RelativePosition.REAR,
178 (DEVSSimulatorInterface.TimeDoubleUnit) theSimulator, Compatible.EVERYTHING));
179 String controllerName = "Simple TrafCOD controller";
180 TrafCODDemo.this.trafCOD = new TrafCOD(controllerName, URLResource.getResource("/TrafCODDemo1/simpleTest.tfc"),
181 trafficLights, sensors, (DEVSSimulator<Time, Duration, SimTimeDoubleUnit>) theSimulator,
182 TrafCODDemo.this.controllerDisplayPanel);
183 TrafCODDemo.this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING);
184 TrafCODDemo.this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING);
185 TrafCODDemo.this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED);
186 TrafCODDemo.this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_STATE_CHANGED);
187 TrafCODDemo.this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_VARIABLE_CREATED);
188 TrafCODDemo.this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED);
189
190 addListener(TrafCODDemo.this.trafCOD, TrafficController.TRAFFICCONTROL_SET_TRACING);
191
192
193
194
195
196
197
198
199
200
201
202 }
203 catch (Exception exception)
204 {
205 exception.printStackTrace();
206 }
207 }
208
209 @SuppressWarnings("synthetic-access")
210 @Override
211 public SimulatorInterface<Time, Duration, SimTimeDoubleUnit> getSimulator()
212 {
213 return TrafCODDemo.this.trafCOD.getSimulator();
214 }
215
216
217 @Override
218 public final OTSNetwork getNetwork()
219 {
220 return this.network;
221 }
222
223
224 @Override
225 public void notify(final EventInterface event) throws RemoteException
226 {
227 EventType type = event.getType();
228 Object[] payload = (Object[]) event.getContent();
229 if (TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING.equals(type))
230 {
231
232 return;
233 }
234 else if (TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED.equals(type))
235 {
236 System.out.println("Conflict group changed from " + ((String) payload[1]) + " to " + ((String) payload[2]));
237 }
238 else if (TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED.equals(type))
239 {
240 System.out.println(String.format("Variable changed %s <- %d %s", payload[1], payload[4], payload[5]));
241 }
242 else if (TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING.equals(type))
243 {
244 System.out.println("Warning " + payload[1]);
245 }
246 else
247 {
248 System.out.print("TrafCODDemo received event of type " + event.getType() + ", payload [");
249 String separator = "";
250 for (Object o : payload)
251 {
252 System.out.print(separator + o);
253 separator = ",";
254 }
255 System.out.println("]");
256 }
257 }
258
259 }
260
261 }