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 TJunctionDemo 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 TJunctionModel();
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 "T-junction demonstration";
74 }
75
76
77 @Override
78 public final String description()
79 {
80 return "T-junction demonstration";
81 }
82
83
84
85
86 class TJunctionModel 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/TJunction.xml");
107 XmlNetworkLaneParser nlp = new XmlNetworkLaneParser(this.simulator);
108 this.network = nlp.build(url);
109
110
111 ((CrossSectionLink) this.network.getLink("ECSC")).setPriority(Priority.PRIORITY);
112 ((CrossSectionLink) this.network.getLink("ECWC")).setPriority(Priority.PRIORITY);
113 ((CrossSectionLink) this.network.getLink("WCSC")).setPriority(Priority.PRIORITY);
114 ((CrossSectionLink) this.network.getLink("WCEC")).setPriority(Priority.PRIORITY);
115
116
117 ConflictBuilder.buildConflicts(this.network, VEHICLE, this.simulator,
118 new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)));
119
120
121 Lane lane = ((CrossSectionLink) this.network.getLink("ECE")).getLanes().get(0);
122 SimpleTrafficLight trafficLight =
123 new SimpleTrafficLight("light", lane, new Length(50.0, LengthUnit.SI), this.simulator);
124 trafficLight.setTrafficLightColor(TrafficLightColor.RED);
125 changePhase(trafficLight);
126
127 }
128 catch (Exception exception)
129 {
130 exception.printStackTrace();
131 }
132 }
133
134
135
136
137
138
139 private void changePhase(final SimpleTrafficLight trafficLight) throws SimRuntimeException
140 {
141 switch (trafficLight.getTrafficLightColor())
142 {
143 case RED:
144 {
145 trafficLight.setTrafficLightColor(TrafficLightColor.GREEN);
146 this.simulator.scheduleEventRel(new Duration(30.0, TimeUnit.SECOND), this, this, "changePhase",
147 new Object[] { trafficLight });
148 break;
149 }
150 case YELLOW:
151 {
152 trafficLight.setTrafficLightColor(TrafficLightColor.RED);
153 this.simulator.scheduleEventRel(new Duration(56.0, TimeUnit.SECOND), this, this, "changePhase",
154 new Object[] { trafficLight });
155 break;
156 }
157 case GREEN:
158 {
159 trafficLight.setTrafficLightColor(TrafficLightColor.YELLOW);
160 this.simulator.scheduleEventRel(new Duration(4.0, TimeUnit.SECOND), this, this, "changePhase",
161 new Object[] { trafficLight });
162 break;
163 }
164 default:
165 {
166
167 }
168 }
169 }
170
171
172 @Override
173 public SimulatorInterface<Time, Duration, OTSSimTimeDouble> getSimulator() throws RemoteException
174 {
175 return this.simulator;
176 }
177
178
179 @Override
180 public OTSNetwork getNetwork()
181 {
182 return this.network;
183 }
184
185 }
186
187
188
189
190
191
192 public static void main(final String[] args) throws SimRuntimeException
193 {
194 SwingUtilities.invokeLater(new Runnable()
195 {
196 @Override
197 public void run()
198 {
199 try
200 {
201 TJunctionDemo animation = new TJunctionDemo();
202
203 animation.buildAnimator(new Time(0.0, TimeUnit.SECOND), new Duration(0.0, TimeUnit.SECOND),
204 new Duration(60.0, TimeUnit.MINUTE), new ArrayList<Property<?>>(), null, true);
205 }
206 catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
207 {
208 exception.printStackTrace();
209 }
210 }
211 });
212 }
213
214 }