1 package org.opentrafficsim.demo.conflict;
2
3 import java.awt.Dimension;
4 import java.io.Serializable;
5 import java.net.URL;
6 import java.rmi.RemoteException;
7
8 import javax.naming.NamingException;
9
10 import org.djunits.unit.DurationUnit;
11 import org.djunits.unit.LengthUnit;
12 import org.djunits.value.vdouble.scalar.Duration;
13 import org.djunits.value.vdouble.scalar.Length;
14 import org.djunits.value.vdouble.scalar.Time;
15 import org.djutils.io.URLResource;
16 import org.opentrafficsim.core.dsol.AbstractOTSModel;
17 import org.opentrafficsim.core.dsol.OTSAnimator;
18 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
19 import org.opentrafficsim.core.gtu.GTUType;
20 import org.opentrafficsim.core.network.NetworkException;
21 import org.opentrafficsim.demo.conflict.TJunctionDemo.TJunctionModel;
22 import org.opentrafficsim.draw.core.OTSDrawingException;
23 import org.opentrafficsim.draw.road.TrafficLightAnimation;
24 import org.opentrafficsim.road.network.OTSRoadNetwork;
25 import org.opentrafficsim.road.network.factory.xml.parser.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.swing.gui.OTSAnimationPanel;
32 import org.opentrafficsim.swing.gui.OTSSimulationApplication;
33
34 import nl.tudelft.simulation.dsol.SimRuntimeException;
35 import nl.tudelft.simulation.language.DSOLException;
36
37
38
39
40
41
42
43
44
45
46
47 public class TJunctionDemo extends OTSSimulationApplication<TJunctionModel>
48 {
49
50 private static final long serialVersionUID = 20161211L;
51
52
53
54
55
56
57
58
59 public TJunctionDemo(final String title, final OTSAnimationPanel panel, final TJunctionModel model)
60 throws OTSDrawingException
61 {
62 super(model, panel);
63 }
64
65
66
67
68
69 public static void main(final String[] args)
70 {
71 demo(true);
72 }
73
74
75
76
77
78 public static void demo(final boolean exitOnClose)
79 {
80 try
81 {
82 OTSAnimator simulator = new OTSAnimator("TJunctionDemo");
83 final TJunctionModel junctionModel = new TJunctionModel(simulator);
84 simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), junctionModel);
85 OTSAnimationPanel animationPanel = new OTSAnimationPanel(junctionModel.getNetwork().getExtent(),
86 new Dimension(800, 600), simulator, junctionModel, DEFAULT_COLORER, junctionModel.getNetwork());
87 TJunctionDemo/TJunctionDemo.html#TJunctionDemo">TJunctionDemo app = new TJunctionDemo("T-Junction demo", animationPanel, junctionModel);
88 app.setExitOnClose(exitOnClose);
89 animationPanel.enableSimulationControlButtons();
90 }
91 catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
92 {
93 exception.printStackTrace();
94 }
95 }
96
97
98
99
100 public static class TJunctionModel extends AbstractOTSModel
101 {
102
103 private static final long serialVersionUID = 20161211L;
104
105
106 private OTSRoadNetwork network;
107
108
109
110
111 public TJunctionModel(final OTSSimulatorInterface simulator)
112 {
113 super(simulator);
114 }
115
116
117 @Override
118 public void constructModel() throws SimRuntimeException
119 {
120 try
121 {
122 URL xmlURL = URLResource.getResource("/conflict/TJunction.xml");
123 this.network = new OTSRoadNetwork("TJunction", true, getSimulator());
124 XmlNetworkLaneParser.build(xmlURL, this.network, false);
125
126
127
128
129 ConflictBuilder.buildConflicts(this.network, this.network.getGtuType(GTUType.DEFAULTS.VEHICLE), this.simulator,
130 new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)));
131
132
133 Lane lane = ((CrossSectionLink) this.network.getLink("ECE")).getLanes().get(0);
134 SimpleTrafficLight trafficLight =
135 new SimpleTrafficLight("light", lane, new Length(50.0, LengthUnit.SI), this.simulator);
136
137 try
138 {
139 new TrafficLightAnimation(trafficLight, this.simulator);
140 }
141 catch (RemoteException | NamingException exception)
142 {
143 throw new NetworkException(exception);
144 }
145 trafficLight.setTrafficLightColor(TrafficLightColor.RED);
146 changePhase(trafficLight);
147
148 }
149 catch (Exception exception)
150 {
151 exception.printStackTrace();
152 }
153 }
154
155
156
157
158
159
160 private void changePhase(final SimpleTrafficLight trafficLight) throws SimRuntimeException
161 {
162 switch (trafficLight.getTrafficLightColor())
163 {
164 case RED:
165 {
166 trafficLight.setTrafficLightColor(TrafficLightColor.GREEN);
167 this.simulator.scheduleEventRel(new Duration(30.0, DurationUnit.SECOND), this, this, "changePhase",
168 new Object[] {trafficLight});
169 break;
170 }
171 case YELLOW:
172 {
173 trafficLight.setTrafficLightColor(TrafficLightColor.RED);
174 this.simulator.scheduleEventRel(new Duration(56.0, DurationUnit.SECOND), this, this, "changePhase",
175 new Object[] {trafficLight});
176 break;
177 }
178 case GREEN:
179 {
180 trafficLight.setTrafficLightColor(TrafficLightColor.YELLOW);
181 this.simulator.scheduleEventRel(new Duration(4.0, DurationUnit.SECOND), this, this, "changePhase",
182 new Object[] {trafficLight});
183 break;
184 }
185 default:
186 {
187
188 }
189 }
190 }
191
192
193 @Override
194 public OTSRoadNetwork getNetwork()
195 {
196 return this.network;
197 }
198
199
200 @Override
201 public Serializable getSourceId()
202 {
203 return "TJunctionModel";
204 }
205
206 }
207 }