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