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 TJunctionDemo 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 TJunctionModel();
59 }
60
61
62 @Override
63 protected final void addAnimationToggles()
64 {
65 AnimationToggles.setIconAnimationTogglesFull(this);
66 }
67
68
69 @Override
70 public final String shortName()
71 {
72 return "T-junction demonstration";
73 }
74
75
76 @Override
77 public final String description()
78 {
79 return "T-junction demonstration";
80 }
81
82
83
84
85 class TJunctionModel implements OTSModelInterface
86 {
87
88
89 private static final long serialVersionUID = 20161211L;
90
91
92 private OTSNetwork network;
93
94
95 private OTSDEVSSimulatorInterface simulator;
96
97
98 @Override
99 public void constructModel(final SimulatorInterface<Time, Duration, OTSSimTimeDouble> arg0)
100 throws SimRuntimeException, RemoteException
101 {
102 this.simulator = (OTSDEVSSimulatorInterface) arg0;
103 try
104 {
105 URL url = URLResource.getResource("/conflict/TJunction.xml");
106 XmlNetworkLaneParser nlp = new XmlNetworkLaneParser(this.simulator);
107 this.network = nlp.build(url);
108
109
110
111
112 ConflictBuilder.buildConflicts(this.network, VEHICLE, this.simulator,
113 new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)));
114
115
116 Lane lane = ((CrossSectionLink) this.network.getLink("ECE")).getLanes().get(0);
117 SimpleTrafficLight trafficLight =
118 new SimpleTrafficLight("light", lane, new Length(50.0, LengthUnit.SI), this.simulator);
119 trafficLight.setTrafficLightColor(TrafficLightColor.RED);
120 changePhase(trafficLight);
121
122 }
123 catch (Exception exception)
124 {
125 exception.printStackTrace();
126 }
127 }
128
129
130
131
132
133
134 private void changePhase(final SimpleTrafficLight trafficLight) throws SimRuntimeException
135 {
136 switch (trafficLight.getTrafficLightColor())
137 {
138 case RED:
139 {
140 trafficLight.setTrafficLightColor(TrafficLightColor.GREEN);
141 this.simulator.scheduleEventRel(new Duration(30.0, DurationUnit.SECOND), this, this, "changePhase",
142 new Object[] { trafficLight });
143 break;
144 }
145 case YELLOW:
146 {
147 trafficLight.setTrafficLightColor(TrafficLightColor.RED);
148 this.simulator.scheduleEventRel(new Duration(56.0, DurationUnit.SECOND), this, this, "changePhase",
149 new Object[] { trafficLight });
150 break;
151 }
152 case GREEN:
153 {
154 trafficLight.setTrafficLightColor(TrafficLightColor.YELLOW);
155 this.simulator.scheduleEventRel(new Duration(4.0, DurationUnit.SECOND), this, this, "changePhase",
156 new Object[] { trafficLight });
157 break;
158 }
159 default:
160 {
161
162 }
163 }
164 }
165
166
167 @Override
168 public SimulatorInterface<Time, Duration, OTSSimTimeDouble> getSimulator() throws RemoteException
169 {
170 return this.simulator;
171 }
172
173
174 @Override
175 public OTSNetwork getNetwork()
176 {
177 return this.network;
178 }
179
180 }
181
182
183
184
185
186
187 public static void main(final String[] args) throws SimRuntimeException
188 {
189 SwingUtilities.invokeLater(new Runnable()
190 {
191 @Override
192 public void run()
193 {
194 try
195 {
196 TJunctionDemo animation = new TJunctionDemo();
197
198 animation.buildAnimator(Time.ZERO, Duration.ZERO, new Duration(60.0, DurationUnit.MINUTE),
199 new ArrayList<Property<?>>(), null, true);
200 }
201 catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
202 {
203 exception.printStackTrace();
204 }
205 }
206 });
207 }
208
209 }