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.demo.conflict.TJunctionDemo.TJunctionModel;
19 import org.opentrafficsim.draw.core.OtsDrawingException;
20 import org.opentrafficsim.road.network.RoadNetwork;
21 import org.opentrafficsim.road.network.factory.xml.parser.XmlNetworkLaneParser;
22 import org.opentrafficsim.road.network.lane.CrossSectionLink;
23 import org.opentrafficsim.road.network.lane.Lane;
24 import org.opentrafficsim.road.network.lane.conflict.ConflictBuilder;
25 import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
26 import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightColor;
27 import org.opentrafficsim.swing.gui.OtsAnimationPanel;
28 import org.opentrafficsim.swing.gui.OtsSimulationApplication;
29
30 import nl.tudelft.simulation.dsol.SimRuntimeException;
31 import nl.tudelft.simulation.language.DSOLException;
32
33
34
35
36
37
38
39
40
41
42 public class TJunctionDemo extends OtsSimulationApplication<TJunctionModel>
43 {
44
45 private static final long serialVersionUID = 20161211L;
46
47
48
49
50
51
52
53
54 public TJunctionDemo(final String title, final OtsAnimationPanel panel, final TJunctionModel model)
55 throws OtsDrawingException
56 {
57 super(model, panel);
58 }
59
60
61
62
63
64 public static void main(final String[] args)
65 {
66 demo(true);
67 }
68
69
70
71
72
73 public static void demo(final boolean exitOnClose)
74 {
75 try
76 {
77 OtsAnimator simulator = new OtsAnimator("TJunctionDemo");
78 final TJunctionModel junctionModel = new TJunctionModel(simulator);
79 simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), junctionModel);
80 OtsAnimationPanel animationPanel = new OtsAnimationPanel(junctionModel.getNetwork().getExtent(),
81 new Dimension(800, 600), simulator, junctionModel, DEFAULT_COLORER, junctionModel.getNetwork());
82 TJunctionDemo app = new TJunctionDemo("T-Junction demo", animationPanel, junctionModel);
83 app.setExitOnClose(exitOnClose);
84 animationPanel.enableSimulationControlButtons();
85 }
86 catch (SimRuntimeException | NamingException | RemoteException | OtsDrawingException | DSOLException exception)
87 {
88 exception.printStackTrace();
89 }
90 }
91
92
93
94
95 public static class TJunctionModel extends AbstractOtsModel
96 {
97
98 private static final long serialVersionUID = 20161211L;
99
100
101 private RoadNetwork network;
102
103
104
105
106 public TJunctionModel(final OtsSimulatorInterface simulator)
107 {
108 super(simulator);
109 }
110
111
112 @Override
113 public void constructModel() throws SimRuntimeException
114 {
115 try
116 {
117 URL xmlURL = URLResource.getResource("/resources/conflict/TJunction.xml");
118 this.network = new RoadNetwork("TJunction", getSimulator());
119 XmlNetworkLaneParser.build(xmlURL, this.network, false);
120
121
122
123
124 ConflictBuilder.buildConflicts(this.network, this.simulator,
125 new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)));
126
127
128 Lane lane = ((CrossSectionLink) this.network.getLink("ECE")).getLanes().get(0);
129 TrafficLight trafficLight =
130 new TrafficLight("light", lane, new Length(50.0, LengthUnit.SI), this.simulator);
131
132 trafficLight.setTrafficLightColor(TrafficLightColor.RED);
133 changePhase(trafficLight);
134
135 }
136 catch (Exception exception)
137 {
138 exception.printStackTrace();
139 }
140 }
141
142
143
144
145
146
147 private void changePhase(final TrafficLight trafficLight) throws SimRuntimeException
148 {
149 switch (trafficLight.getTrafficLightColor())
150 {
151 case RED:
152 {
153 trafficLight.setTrafficLightColor(TrafficLightColor.GREEN);
154 this.simulator.scheduleEventRel(new Duration(30.0, DurationUnit.SECOND), 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, "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, "changePhase",
169 new Object[] {trafficLight});
170 break;
171 }
172 default:
173 {
174
175 }
176 }
177 }
178
179
180 @Override
181 public RoadNetwork getNetwork()
182 {
183 return this.network;
184 }
185
186 }
187 }