1 package org.opentrafficsim.demo.conflict;
2
3 import static org.opentrafficsim.core.gtu.GTUType.VEHICLE;
4
5 import java.net.URL;
6 import java.rmi.RemoteException;
7 import java.util.ArrayList;
8
9 import javax.naming.NamingException;
10 import javax.swing.SwingUtilities;
11
12 import org.djunits.unit.DurationUnit;
13 import org.djunits.unit.LengthUnit;
14 import org.djunits.value.vdouble.scalar.Duration;
15 import org.djunits.value.vdouble.scalar.Length;
16 import org.djunits.value.vdouble.scalar.Time;
17 import org.opentrafficsim.base.modelproperties.Property;
18 import org.opentrafficsim.base.modelproperties.PropertyException;
19 import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
20 import org.opentrafficsim.core.dsol.OTSModelInterface;
21 import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
22 import org.opentrafficsim.core.gtu.animation.GTUColorer;
23 import org.opentrafficsim.core.network.OTSNetwork;
24 import org.opentrafficsim.road.animation.AnimationToggles;
25 import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser;
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.simulationengine.AbstractWrappableAnimation;
32 import org.opentrafficsim.simulationengine.OTSSimulationException;
33
34 import nl.tudelft.simulation.dsol.SimRuntimeException;
35 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
36 import nl.tudelft.simulation.language.io.URLResource;
37
38
39
40
41
42
43
44
45
46
47
48 public class TurboRoundaboutDemo extends AbstractWrappableAnimation
49 {
50
51
52 private static final long serialVersionUID = 20161211L;
53
54
55 @Override
56 protected final OTSModelInterface makeModel(final GTUColorer colorer) throws OTSSimulationException
57 {
58 return new TurboRoundaboutModel();
59 }
60
61
62 @Override
63 protected final void addAnimationToggles()
64 {
65 AnimationToggles.setIconAnimationTogglesFull(this);
66
67
68 }
69
70
71 @Override
72 public final String shortName()
73 {
74 return "Turbo roundabout demonstration";
75 }
76
77
78 @Override
79 public final String description()
80 {
81 return "Turbo roundabout demonstration";
82 }
83
84
85
86
87 class TurboRoundaboutModel implements OTSModelInterface
88 {
89
90
91 private static final long serialVersionUID = 20161211L;
92
93
94 private OTSNetwork network;
95
96
97 private OTSDEVSSimulatorInterface simulator;
98
99
100 @Override
101 public void constructModel(final SimulatorInterface<Time, Duration, OTSSimTimeDouble> arg0)
102 throws SimRuntimeException, RemoteException
103 {
104 this.simulator = (OTSDEVSSimulatorInterface) arg0;
105 try
106 {
107 URL url = URLResource.getResource("/conflict/TurboRoundabout.xml");
108 XmlNetworkLaneParser nlp = new XmlNetworkLaneParser(this.simulator);
109 this.network = nlp.build(url);
110
111
112 ConflictBuilder.buildConflicts(this.network, VEHICLE, this.simulator,
113 new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)));
114
115
116 for (Lane lane : ((CrossSectionLink) this.network.getLink("SEXITS")).getLanes())
117 {
118 SimpleTrafficLight trafficLight = new SimpleTrafficLight("light" + lane.getId(), lane,
119 new Length(150.0, LengthUnit.SI), this.simulator);
120 trafficLight.setTrafficLightColor(TrafficLightColor.RED);
121 changePhase(trafficLight);
122 }
123
124
125
126
127
128
129
130
131
132 }
133 catch (Exception exception)
134 {
135 exception.printStackTrace();
136 }
137 }
138
139
140
141
142
143
144 private void changePhase(final SimpleTrafficLight trafficLight) throws SimRuntimeException
145 {
146 switch (trafficLight.getTrafficLightColor())
147 {
148 case RED:
149 {
150 trafficLight.setTrafficLightColor(TrafficLightColor.GREEN);
151 this.simulator.scheduleEventRel(new Duration(15.0, DurationUnit.SECOND), this, this, "changePhase",
152 new Object[] { trafficLight });
153 break;
154 }
155 case YELLOW:
156 {
157 trafficLight.setTrafficLightColor(TrafficLightColor.RED);
158 this.simulator.scheduleEventRel(new Duration(56.0, DurationUnit.SECOND), this, this, "changePhase",
159 new Object[] { trafficLight });
160 break;
161 }
162 case GREEN:
163 {
164 trafficLight.setTrafficLightColor(TrafficLightColor.YELLOW);
165 this.simulator.scheduleEventRel(new Duration(4.0, DurationUnit.SECOND), this, this, "changePhase",
166 new Object[] { trafficLight });
167 break;
168 }
169 default:
170 {
171
172 }
173 }
174 }
175
176
177 @Override
178 public SimulatorInterface<Time, Duration, OTSSimTimeDouble> getSimulator() throws RemoteException
179 {
180 return this.simulator;
181 }
182
183
184 @Override
185 public OTSNetwork getNetwork()
186 {
187 return this.network;
188 }
189
190 }
191
192
193
194
195
196
197 public static void main(final String[] args) throws SimRuntimeException
198 {
199 SwingUtilities.invokeLater(new Runnable()
200 {
201 @Override
202 public void run()
203 {
204 try
205 {
206 TurboRoundaboutDemo animation = new TurboRoundaboutDemo();
207
208 animation.buildAnimator(Time.ZERO, Duration.ZERO, new Duration(60.0, DurationUnit.MINUTE),
209 new ArrayList<Property<?>>(), null, true);
210 }
211 catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
212 {
213 exception.printStackTrace();
214 }
215 }
216 });
217 }
218
219 }