1 package org.opentrafficsim.web.test;
2
3 import java.net.URL;
4 import java.rmi.RemoteException;
5
6 import javax.naming.NamingException;
7
8 import org.djunits.unit.DurationUnit;
9 import org.djunits.unit.LengthUnit;
10 import org.djunits.value.vdouble.scalar.Duration;
11 import org.djunits.value.vdouble.scalar.Length;
12 import org.djutils.io.URLResource;
13 import org.opentrafficsim.core.dsol.AbstractOtsModel;
14 import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
15 import org.opentrafficsim.core.network.NetworkException;
16 import org.opentrafficsim.draw.road.TrafficLightAnimation;
17 import org.opentrafficsim.road.network.RoadNetwork;
18 import org.opentrafficsim.road.network.factory.xml.parser.XmlParser;
19 import org.opentrafficsim.road.network.lane.CrossSectionLink;
20 import org.opentrafficsim.road.network.lane.Lane;
21 import org.opentrafficsim.road.network.lane.conflict.ConflictBuilder;
22 import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
23 import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightColor;
24
25 import nl.tudelft.simulation.dsol.SimRuntimeException;
26
27
28
29
30
31
32
33
34
35 public class TJunctionModel extends AbstractOtsModel
36 {
37
38 private static final long serialVersionUID = 20161211L;
39
40
41 private RoadNetwork network;
42
43
44
45
46 public TJunctionModel(final OtsSimulatorInterface simulator)
47 {
48 super(simulator);
49 }
50
51
52 @Override
53 public void constructModel() throws SimRuntimeException
54 {
55 try
56 {
57 URL xmlURL = URLResource.getResource("/resources/xml/TJunction.xml");
58 this.network = new RoadNetwork("TJunction", getSimulator());
59 new XmlParser(this.network).setUrl(xmlURL).build();
60
61
62
63
64 ConflictBuilder.buildConflicts(this.network, this.simulator,
65 new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)));
66
67
68 Lane lane = ((CrossSectionLink) this.network.getLink("ECE")).getLanes().get(0);
69 TrafficLight trafficLight =
70 new TrafficLight("light", lane, new Length(50.0, LengthUnit.SI), this.simulator);
71 trafficLight.setTrafficLightColor(TrafficLightColor.RED);
72 changePhase(trafficLight);
73
74 }
75 catch (Exception exception)
76 {
77 exception.printStackTrace();
78 }
79 }
80
81
82
83
84
85
86 private void changePhase(final TrafficLight trafficLight) throws SimRuntimeException
87 {
88 switch (trafficLight.getTrafficLightColor())
89 {
90 case RED:
91 {
92 trafficLight.setTrafficLightColor(TrafficLightColor.GREEN);
93 this.simulator.scheduleEventRel(new Duration(30.0, DurationUnit.SECOND), this, "changePhase",
94 new Object[] {trafficLight});
95 break;
96 }
97 case YELLOW:
98 {
99 trafficLight.setTrafficLightColor(TrafficLightColor.RED);
100 this.simulator.scheduleEventRel(new Duration(56.0, DurationUnit.SECOND), this, "changePhase",
101 new Object[] {trafficLight});
102 break;
103 }
104 case GREEN:
105 {
106 trafficLight.setTrafficLightColor(TrafficLightColor.YELLOW);
107 this.simulator.scheduleEventRel(new Duration(4.0, DurationUnit.SECOND), this, "changePhase",
108 new Object[] {trafficLight});
109 break;
110 }
111 default:
112 {
113
114 }
115 }
116 }
117
118
119 @Override
120 public RoadNetwork getNetwork()
121 {
122 return this.network;
123 }
124 }