1 package org.opentrafficsim.demo.trafficcontrol;
2
3 import java.awt.BorderLayout;
4 import java.awt.Container;
5 import java.awt.Dimension;
6 import java.io.ByteArrayInputStream;
7 import java.io.IOException;
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 import javax.swing.JScrollPane;
16
17 import org.djunits.value.vdouble.scalar.Duration;
18 import org.djunits.value.vdouble.scalar.Time;
19 import org.djutils.event.Event;
20 import org.djutils.event.EventListener;
21 import org.djutils.event.EventType;
22 import org.djutils.immutablecollections.ImmutableMap;
23 import org.djutils.io.URLResource;
24 import org.djutils.logger.CategoryLogger;
25 import org.opentrafficsim.core.dsol.AbstractOtsModel;
26 import org.opentrafficsim.core.dsol.OtsAnimator;
27 import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
28 import org.opentrafficsim.core.object.NonLocatedObject;
29 import org.opentrafficsim.core.perception.HistoryManagerDevs;
30 import org.opentrafficsim.demo.DefaultsFactory;
31 import org.opentrafficsim.demo.trafficcontrol.TrafCodDemo2.TrafCodModel;
32 import org.opentrafficsim.road.network.RoadNetwork;
33 import org.opentrafficsim.road.network.factory.xml.parser.XmlParser;
34 import org.opentrafficsim.swing.gui.OtsAnimationPanel;
35 import org.opentrafficsim.swing.gui.OtsSimulationApplication;
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.swing.gui.TabbedContentPane;
41 import nl.tudelft.simulation.language.DsolException;
42
43
44
45
46
47
48
49
50
51
52 public class TrafCodDemo2 extends OtsSimulationApplication<TrafCodModel>
53 {
54
55 private static final long serialVersionUID = 20161118L;
56
57
58
59
60
61
62
63 public TrafCodDemo2(final String title, final OtsAnimationPanel panel, final TrafCodModel model)
64 {
65 super(model, panel, DefaultsFactory.GTU_TYPE_MARKERS.toMap());
66 }
67
68
69
70
71
72
73 public static void main(final String[] args) throws IOException
74 {
75 demo(true);
76 }
77
78
79
80
81
82
83
84
85 public static String readStringFromURL(final URL url) throws IOException
86 {
87 try (Scanner scanner = new Scanner(url.openStream(), StandardCharsets.UTF_8.toString()))
88 {
89 scanner.useDelimiter("\\A");
90 return scanner.hasNext() ? scanner.next() : "";
91 }
92 }
93
94
95
96
97
98
99 public static void demo(final boolean exitOnClose) throws IOException
100 {
101 try
102 {
103 OtsAnimator simulator = new OtsAnimator("TrafCODDemo2");
104 URL url = URLResource.getResource("/resources/TrafCODDemo2/TrafCODDemo2.xml");
105 System.out.println("url is " + url);
106 String xml = readStringFromURL(url);
107 final TrafCodModel trafcodModel = new TrafCodModel(simulator, "TrafCODModel", "TrafCOD demonstration Model", xml);
108 simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), trafcodModel,
109 HistoryManagerDevs.noHistory(simulator));
110 OtsAnimationPanel animationPanel = new OtsAnimationPanel(trafcodModel.getNetwork().getExtent(),
111 new Dimension(800, 600), simulator, trafcodModel, DEFAULT_GTU_COLORERS, trafcodModel.getNetwork());
112 TrafCodDemo2 app = new TrafCodDemo2("TrafCOD demo complex crossing", animationPanel, trafcodModel);
113 app.setExitOnClose(exitOnClose);
114 animationPanel.enableSimulationControlButtons();
115 }
116 catch (SimRuntimeException | NamingException | RemoteException | DsolException exception)
117 {
118 exception.printStackTrace();
119 }
120 }
121
122
123
124
125 @Override
126 protected final void addTabs()
127 {
128 OtsAnimationPanel animationPanel = getAnimationPanel();
129 if (null == animationPanel)
130 {
131 return;
132 }
133 ImmutableMap<String, NonLocatedObject> nonLocatedObjectMap = getModel().getNetwork().getNonLocatedObjectMap();
134 for (NonLocatedObject ioi : nonLocatedObjectMap.values())
135 {
136 if (ioi instanceof TrafCod)
137 {
138 TrafCod trafCOD = (TrafCod) ioi;
139 Container controllerDisplayPanel = trafCOD.getDisplayContainer();
140 if (null != controllerDisplayPanel)
141 {
142 JPanel wrapper = new JPanel(new BorderLayout());
143 wrapper.add(new JScrollPane(controllerDisplayPanel));
144 TabbedContentPane tabbedPane = animationPanel.getTabbedPane();
145 tabbedPane.addTab(tabbedPane.getTabCount() - 1, trafCOD.getId(), wrapper);
146 }
147
148 trafCOD.addListener(getModel(), TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING);
149 trafCOD.addListener(getModel(), TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED);
150 trafCOD.addListener(getModel(), TrafficController.TRAFFICCONTROL_STATE_CHANGED);
151 trafCOD.addListener(getModel(), TrafficController.TRAFFICCONTROL_VARIABLE_CREATED);
152 trafCOD.addListener(getModel(), TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED);
153
154 }
155 }
156 }
157
158
159
160
161 public static class TrafCodModel extends AbstractOtsModel implements EventListener
162 {
163
164 private static final long serialVersionUID = 20161020L;
165
166
167 private RoadNetwork network;
168
169
170 private final String xml;
171
172
173
174
175
176
177
178 public TrafCodModel(final OtsSimulatorInterface simulator, final String shortName, final String description,
179 final String xml)
180 {
181 super(simulator, shortName, description);
182 this.xml = xml;
183 }
184
185 @Override
186 public void constructModel() throws SimRuntimeException
187 {
188 try
189 {
190 this.network = new RoadNetwork(getShortName(), getSimulator());
191 new XmlParser(this.network).setStream(new ByteArrayInputStream(this.xml.getBytes(StandardCharsets.UTF_8)))
192 .build();
193 }
194 catch (Exception exception)
195 {
196 exception.printStackTrace();
197 }
198 }
199
200 @Override
201 public final RoadNetwork getNetwork()
202 {
203 return this.network;
204 }
205
206 @Override
207 public void notify(final Event event) throws RemoteException
208 {
209 EventType type = event.getType();
210 Object[] payload = (Object[]) event.getContent();
211 if (TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING.equals(type))
212 {
213
214 return;
215 }
216 else if (TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED.equals(type))
217 {
218 CategoryLogger.always().info("Conflict group changed from {} to {}", (String) payload[1], (String) payload[2]);
219 }
220 else if (TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED.equals(type))
221 {
222 CategoryLogger.always().info("Variable changed %s <- %d %s", payload[1], payload[4], payload[5]);
223 }
224 else if (TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING.equals(type))
225 {
226 CategoryLogger.always().info("Warning " + payload[1]);
227 }
228 else
229 {
230 StringBuilder stringBuilder = new StringBuilder();
231 stringBuilder.append("TrafCODDemo received event of type " + event.getType() + ", payload [");
232 String separator = "";
233 for (Object o : payload)
234 {
235 stringBuilder.append(separator + o);
236 separator = ",";
237 }
238 stringBuilder.append("]");
239 CategoryLogger.always().info(stringBuilder.toString());
240 }
241 }
242
243 @Override
244 public String toString()
245 {
246 return "TrafCODModel [network=" + this.network.getId() + "]";
247 }
248
249 }
250
251 }