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.OTSModelInterface;
20 import org.opentrafficsim.core.network.OTSLink;
21 import org.opentrafficsim.core.network.OTSNetwork;
22 import org.opentrafficsim.core.network.OTSNode;
23 import org.opentrafficsim.road.animation.AnimationToggles;
24 import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser;
25 import org.opentrafficsim.road.network.lane.CrossSectionLink;
26 import org.opentrafficsim.road.network.lane.Lane;
27 import org.opentrafficsim.road.network.lane.conflict.ConflictBuilder;
28 import org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight;
29 import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightColor;
30 import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
31 import org.opentrafficsim.simulationengine.OTSSimulationException;
32
33 import nl.tudelft.simulation.dsol.SimRuntimeException;
34 import nl.tudelft.simulation.dsol.simtime.SimTimeDoubleUnit;
35 import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
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() throws OTSSimulationException
58 {
59 return new TurboRoundaboutModel();
60 }
61
62
63 @Override
64 protected final void addAnimationToggles()
65 {
66 AnimationToggles.setIconAnimationTogglesFull(this);
67 hideAnimationClass(OTSLink.class);
68 hideAnimationClass(OTSNode.class);
69
70
71 }
72
73
74 @Override
75 public final String shortName()
76 {
77 return "Turbo roundabout demonstration";
78 }
79
80
81 @Override
82 public final String description()
83 {
84 return "Turbo roundabout demonstration";
85 }
86
87
88
89
90 class TurboRoundaboutModel implements OTSModelInterface
91 {
92
93
94 private static final long serialVersionUID = 20161211L;
95
96
97 private OTSNetwork network;
98
99
100 private DEVSSimulatorInterface.TimeDoubleUnit simulator;
101
102
103 @Override
104 public void constructModel(final SimulatorInterface<Time, Duration, SimTimeDoubleUnit> arg0)
105 throws SimRuntimeException
106 {
107 this.simulator = (DEVSSimulatorInterface.TimeDoubleUnit) arg0;
108 try
109 {
110 URL url = URLResource.getResource("/conflict/TurboRoundabout.xml");
111 XmlNetworkLaneParser nlp = new XmlNetworkLaneParser(this.simulator, getColorer());
112 this.network = nlp.build(url, false);
113
114
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(15.0, DurationUnit.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, DurationUnit.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, DurationUnit.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, SimTimeDoubleUnit> getSimulator()
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(Time.ZERO, Duration.ZERO, new Duration(60.0, DurationUnit.MINUTE),
212 new ArrayList<Property<?>>(), null, true);
213 }
214 catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
215 {
216 exception.printStackTrace();
217 }
218 }
219 });
220 }
221
222 }