1 package org.opentrafficsim.demo.conflict;
2
3 import java.awt.Dimension;
4 import java.net.URL;
5 import java.rmi.RemoteException;
6
7 import javax.naming.NamingException;
8
9 import org.djunits.unit.DurationUnit;
10 import org.djunits.unit.LengthUnit;
11 import org.djunits.value.vdouble.scalar.Duration;
12 import org.djunits.value.vdouble.scalar.Length;
13 import org.djunits.value.vdouble.scalar.Time;
14 import org.djutils.io.URLResource;
15 import org.opentrafficsim.core.animation.gtu.colorer.DefaultSwitchableGTUColorer;
16 import org.opentrafficsim.core.dsol.AbstractOTSModel;
17 import org.opentrafficsim.core.dsol.OTSAnimator;
18 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
19 import org.opentrafficsim.core.gtu.GTUType;
20 import org.opentrafficsim.core.network.NetworkException;
21 import org.opentrafficsim.demo.conflict.TurboRoundaboutDemo.TurboRoundaboutModel;
22 import org.opentrafficsim.draw.core.OTSDrawingException;
23 import org.opentrafficsim.draw.road.TrafficLightAnimation;
24 import org.opentrafficsim.road.network.OTSRoadNetwork;
25 import org.opentrafficsim.road.network.factory.xml.old.XmlNetworkLaneParserOld;
26 import org.opentrafficsim.road.network.lane.CrossSectionLink;
27 import org.opentrafficsim.road.network.lane.Lane;
28 import org.opentrafficsim.road.network.lane.conflict.ConflictBuilder;
29 import org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight;
30 import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightColor;
31 import org.opentrafficsim.swing.gui.OTSAnimationPanel;
32 import org.opentrafficsim.swing.gui.OTSSimulationApplication;
33
34 import nl.tudelft.simulation.dsol.SimRuntimeException;
35
36
37
38
39
40
41
42
43
44
45
46 public class TurboRoundaboutDemo extends OTSSimulationApplication<TurboRoundaboutModel>
47 {
48
49 private static final long serialVersionUID = 20161211L;
50
51
52
53
54
55
56
57
58 public TurboRoundaboutDemo(final String title, final OTSAnimationPanel panel, final TurboRoundaboutModel model)
59 throws OTSDrawingException
60 {
61 super(model, panel);
62 }
63
64
65
66
67
68 public static void main(final String[] args)
69 {
70 demo(true);
71 }
72
73
74
75
76
77 public static void demo(final boolean exitOnClose)
78 {
79 try
80 {
81 OTSAnimator simulator = new OTSAnimator();
82 final TurboRoundaboutModel junctionModel = new TurboRoundaboutModel(simulator);
83 simulator.initialize(Time.ZERO, Duration.ZERO, Duration.createSI(3600.0), junctionModel);
84 OTSAnimationPanel animationPanel =
85 new OTSAnimationPanel(junctionModel.getNetwork().getExtent(), new Dimension(800, 600), simulator,
86 junctionModel, new DefaultSwitchableGTUColorer(), junctionModel.getNetwork());
87 TurboRoundaboutDemo app = new TurboRoundaboutDemo("Turbo-Roundabout demo", animationPanel, junctionModel);
88 app.setExitOnClose(exitOnClose);
89 }
90 catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException exception)
91 {
92 exception.printStackTrace();
93 }
94 }
95
96
97
98
99 static class TurboRoundaboutModel extends AbstractOTSModel
100 {
101
102 private static final long serialVersionUID = 20161211L;
103
104
105 private OTSRoadNetwork network;
106
107
108
109
110 TurboRoundaboutModel(final OTSSimulatorInterface simulator)
111 {
112 super(simulator);
113 }
114
115
116 @Override
117 public void constructModel() throws SimRuntimeException
118 {
119 try
120 {
121 URL url = URLResource.getResource("/conflict/TurboRoundabout.xml");
122 XmlNetworkLaneParserOld nlp = new XmlNetworkLaneParserOld(this.simulator, new DefaultSwitchableGTUColorer());
123 this.network = nlp.build(url, false);
124
125
126 ConflictBuilder.buildConflicts(this.network, this.network.getGtuType(GTUType.DEFAULTS.VEHICLE), this.simulator,
127 new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)));
128
129
130 for (Lane lane : ((CrossSectionLink) this.network.getLink("SEXITS")).getLanes())
131 {
132 SimpleTrafficLight trafficLight = new SimpleTrafficLight("light" + lane.getId(), lane,
133 new Length(150.0, LengthUnit.SI), this.simulator);
134
135 try
136 {
137 new TrafficLightAnimation(trafficLight, this.simulator);
138 }
139 catch (RemoteException | NamingException exception)
140 {
141 throw new NetworkException(exception);
142 }
143
144 trafficLight.setTrafficLightColor(TrafficLightColor.RED);
145 changePhase(trafficLight);
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166 }
167 catch (Exception exception)
168 {
169 exception.printStackTrace();
170 }
171 }
172
173
174
175
176
177
178 private void changePhase(final SimpleTrafficLight trafficLight) throws SimRuntimeException
179 {
180 switch (trafficLight.getTrafficLightColor())
181 {
182 case RED:
183 {
184 trafficLight.setTrafficLightColor(TrafficLightColor.GREEN);
185 this.simulator.scheduleEventRel(new Duration(15.0, DurationUnit.SECOND), this, this, "changePhase",
186 new Object[] {trafficLight});
187 break;
188 }
189 case YELLOW:
190 {
191 trafficLight.setTrafficLightColor(TrafficLightColor.RED);
192 this.simulator.scheduleEventRel(new Duration(56.0, DurationUnit.SECOND), this, this, "changePhase",
193 new Object[] {trafficLight});
194 break;
195 }
196 case GREEN:
197 {
198 trafficLight.setTrafficLightColor(TrafficLightColor.YELLOW);
199 this.simulator.scheduleEventRel(new Duration(4.0, DurationUnit.SECOND), this, this, "changePhase",
200 new Object[] {trafficLight});
201 break;
202 }
203 default:
204 {
205
206 }
207 }
208 }
209
210
211 @Override
212 public OTSRoadNetwork getNetwork()
213 {
214 return this.network;
215 }
216
217 }
218 }