1 package org.opentrafficsim.demo.trafficcontrol;
2
3 import java.awt.BorderLayout;
4 import java.awt.Dimension;
5 import java.io.ByteArrayInputStream;
6 import java.io.IOException;
7 import java.io.Serializable;
8 import java.net.URL;
9 import java.nio.charset.StandardCharsets;
10 import java.rmi.RemoteException;
11 import java.util.Scanner;
12
13 import javax.naming.NamingException;
14 import javax.swing.JPanel;
15
16 import org.djunits.value.vdouble.scalar.Duration;
17 import org.djunits.value.vdouble.scalar.Time;
18 import org.djutils.event.EventInterface;
19 import org.djutils.event.EventListenerInterface;
20 import org.djutils.event.EventTypeInterface;
21 import org.djutils.io.URLResource;
22 import org.opentrafficsim.core.dsol.AbstractOTSModel;
23 import org.opentrafficsim.core.dsol.OTSAnimator;
24 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
25 import org.opentrafficsim.demo.trafficcontrol.TrafCODDemo2_Generators.TrafCODModel;
26 import org.opentrafficsim.draw.core.OTSDrawingException;
27 import org.opentrafficsim.road.network.OTSRoadNetwork;
28 import org.opentrafficsim.road.network.factory.xml.parser.XmlNetworkLaneParser;
29 import org.opentrafficsim.swing.gui.OTSAnimationPanel;
30 import org.opentrafficsim.swing.gui.OTSSimulationApplication;
31 import org.opentrafficsim.trafficcontrol.TrafficController;
32 import org.opentrafficsim.trafficcontrol.trafcod.TrafCOD;
33
34 import nl.tudelft.simulation.dsol.SimRuntimeException;
35 import nl.tudelft.simulation.language.DSOLException;
36
37
38
39
40
41
42
43
44
45
46
47 public class TrafCODDemo2_Generators extends OTSSimulationApplication<TrafCODModel>
48 {
49
50 private static final long serialVersionUID = 20161118L;
51
52
53
54
55
56
57
58
59 public TrafCODDemo2_Generators(final String title, final OTSAnimationPanel panel, final TrafCODModel model)
60 throws OTSDrawingException
61 {
62 super(model, panel);
63 }
64
65
66
67
68
69
70 public static void main(final String[] args) throws IOException
71 {
72 demo(true);
73 }
74
75
76
77
78
79
80
81
82 public static String readStringFromURL(final URL url) throws IOException
83 {
84 try (Scanner scanner = new Scanner(url.openStream(), StandardCharsets.UTF_8.toString()))
85 {
86 scanner.useDelimiter("\\A");
87 return scanner.hasNext() ? scanner.next() : "";
88 }
89 }
90
91
92
93
94
95
96 public static void demo(final boolean exitOnClose) throws IOException
97 {
98 try
99 {
100 OTSAnimator simulator = new OTSAnimator("TrafCODDemo2_Generators");
101 URL url = URLResource.getResource("/TrafCODDemo2/TrafCODDemo2_Generators.xml");
102 String xml = readStringFromURL(url);
103 final TrafCODModel trafcodModel = new TrafCODModel(simulator, "TrafCODModel", "TrafCOD demonstration Model", xml);
104 simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), trafcodModel);
105 OTSAnimationPanel animationPanel = new OTSAnimationPanel(trafcodModel.getNetwork().getExtent(),
106 new Dimension(800, 600), simulator, trafcodModel, DEFAULT_COLORER, trafcodModel.getNetwork());
107 TrafCODDemo2_Generators app =
108 new TrafCODDemo2_Generators("TrafCOD demo complex crossing", animationPanel, trafcodModel);
109 app.setExitOnClose(exitOnClose);
110 }
111 catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
112 {
113 exception.printStackTrace();
114 }
115 }
116
117
118
119
120 @Override
121 protected final void addTabs()
122 {
123
124
125
126
127
128
129 }
130
131
132
133
134 static class TrafCODModel extends AbstractOTSModel implements EventListenerInterface
135 {
136
137 private static final long serialVersionUID = 20161020L;
138
139
140 private OTSRoadNetwork network;
141
142
143 private final String xml;
144
145
146 private TrafCOD trafCOD;
147
148
149 private JPanel controllerDisplayPanel = new JPanel(new BorderLayout());
150
151
152
153
154
155
156
157 TrafCODModel(final OTSSimulatorInterface simulator, final String shortName, final String description, final String xml)
158 {
159 super(simulator, shortName, description);
160 this.xml = xml;
161 }
162
163
164 @Override
165 public void constructModel() throws SimRuntimeException
166 {
167 try
168 {
169 this.network = new OTSRoadNetwork(getShortName(), true, getSimulator());
170 XmlNetworkLaneParser.build(new ByteArrayInputStream(this.xml.getBytes(StandardCharsets.UTF_8)), this.network,
171 false);
172
173 String controllerName = "TrafCOD_complex";
174 this.trafCOD = new TrafCOD(controllerName, URLResource.getResource("/TrafCODDemo2/Intersection12Dir.tfc"),
175 getSimulator(), this.controllerDisplayPanel, null, null);
176 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING);
177 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING);
178 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED);
179 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_STATE_CHANGED);
180 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_VARIABLE_CREATED);
181 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED);
182
183 addListener(this.trafCOD, TrafficController.TRAFFICCONTROL_SET_TRACING);
184
185
186
187
188
189
190
191
192
193
194
195 }
196 catch (Exception exception)
197 {
198 exception.printStackTrace();
199 }
200 }
201
202
203 @Override
204 public final OTSRoadNetwork getNetwork()
205 {
206 return this.network;
207 }
208
209
210
211
212 public final TrafCOD getTrafCOD()
213 {
214 return this.trafCOD;
215 }
216
217
218
219
220 public final JPanel getControllerDisplayPanel()
221 {
222 return this.controllerDisplayPanel;
223 }
224
225
226 @Override
227 public void notify(final EventInterface event) throws RemoteException
228 {
229 EventTypeInterface type = event.getType();
230 Object[] payload = (Object[]) event.getContent();
231 if (TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING.equals(type))
232 {
233
234 return;
235 }
236 else if (TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED.equals(type))
237 {
238 System.out.println("Conflict group changed from " + ((String) payload[1]) + " to " + ((String) payload[2]));
239 }
240 else if (TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED.equals(type))
241 {
242 System.out.println(String.format("Variable changed %s <- %d %s", payload[1], payload[4], payload[5]));
243 }
244 else if (TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING.equals(type))
245 {
246 System.out.println("Warning " + payload[1]);
247 }
248 else
249 {
250 System.out.print("TrafCODDemo received event of type " + event.getType() + ", payload [");
251 String separator = "";
252 for (Object o : payload)
253 {
254 System.out.print(separator + o);
255 separator = ",";
256 }
257 System.out.println("]");
258 }
259 }
260
261
262 @Override
263 public Serializable getSourceId()
264 {
265 return "TrafCODModel";
266 }
267
268 }
269
270 }