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