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