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