View Javadoc
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   * TJunctionModel.java.
29   * <p>
30   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
31   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
32   * </p>
33   * @author <a href="https://github.com/averbraeck" target="_blank">Alexander Verbraeck</a>
34   */
35  public class TJunctionModel extends AbstractOtsModel
36  {
37      /** */
38      private static final long serialVersionUID = 20161211L;
39  
40      /** The network. */
41      private RoadNetwork network;
42  
43      /**
44       * @param simulator OtsSimulatorInterface; the simulator for this model
45       */
46      public TJunctionModel(final OtsSimulatorInterface simulator)
47      {
48          super(simulator);
49      }
50  
51      /** {@inheritDoc} */
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              // add conflicts
62              // ((CrossSectionLink) this.network.getLink("SCEC")).setPriority(Priority.STOP);
63              // ((CrossSectionLink) this.network.getLink("SCWC")).setPriority(Priority.STOP);
64              ConflictBuilder.buildConflicts(this.network, this.simulator,
65                      new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)));
66  
67              // add trafficlight after
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       * Changes color of traffic light.
83       * @param trafficLight SimpleTrafficLight; traffic light
84       * @throws SimRuntimeException scheduling error
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     /** {@inheritDoc} */
119     @Override
120     public RoadNetwork getNetwork()
121     {
122         return this.network;
123     }
124 }