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.gtu.GTUType;
16  import org.opentrafficsim.core.network.NetworkException;
17  import org.opentrafficsim.draw.road.TrafficLightAnimation;
18  import org.opentrafficsim.road.network.OTSRoadNetwork;
19  import org.opentrafficsim.road.network.factory.xml.parser.XmlNetworkLaneParser;
20  import org.opentrafficsim.road.network.lane.CrossSectionLink;
21  import org.opentrafficsim.road.network.lane.Lane;
22  import org.opentrafficsim.road.network.lane.conflict.ConflictBuilder;
23  import org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight;
24  import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightColor;
25  
26  import nl.tudelft.simulation.dsol.SimRuntimeException;
27  
28  /**
29   * TJunctionModel.java. <br>
30   * <br>
31   * Copyright (c) 2003-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
32   * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
33   * source code and binary code of this software is proprietary information of Delft University of Technology.
34   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
35   */
36  public class TJunctionModel extends AbstractOTSModel
37  {
38      /** */
39      private static final long serialVersionUID = 20161211L;
40  
41      /** The network. */
42      private OTSRoadNetwork network;
43  
44      /**
45       * @param simulator OTSSimulatorInterface; the simulator for this model
46       */
47      public TJunctionModel(final OTSSimulatorInterface simulator)
48      {
49          super(simulator);
50      }
51  
52      /** {@inheritDoc} */
53      @Override
54      public void constructModel() throws SimRuntimeException
55      {
56          try
57          {
58              URL xmlURL = URLResource.getResource("/xml/TJunction.xml");
59              this.network = new OTSRoadNetwork("TJunction", true);
60              XmlNetworkLaneParser.build(xmlURL, this.network, getSimulator());
61  
62              // add conflicts
63              // ((CrossSectionLink) this.network.getLink("SCEC")).setPriority(Priority.STOP);
64              // ((CrossSectionLink) this.network.getLink("SCWC")).setPriority(Priority.STOP);
65              ConflictBuilder.buildConflicts(this.network, this.network.getGtuType(GTUType.DEFAULTS.VEHICLE), this.simulator,
66                      new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)));
67  
68              // add trafficlight after
69              Lane lane = ((CrossSectionLink) this.network.getLink("ECE")).getLanes().get(0);
70              SimpleTrafficLight trafficLight =
71                      new SimpleTrafficLight("light", lane, new Length(50.0, LengthUnit.SI), this.simulator);
72  
73              try
74              {
75                  new TrafficLightAnimation(trafficLight, this.simulator);
76              }
77              catch (RemoteException | NamingException exception)
78              {
79                  throw new NetworkException(exception);
80              }
81              trafficLight.setTrafficLightColor(TrafficLightColor.RED);
82              changePhase(trafficLight);
83  
84          }
85          catch (Exception exception)
86          {
87              exception.printStackTrace();
88          }
89      }
90  
91      /**
92       * Changes color of traffic light.
93       * @param trafficLight SimpleTrafficLight; traffic light
94       * @throws SimRuntimeException scheduling error
95       */
96      private void changePhase(final SimpleTrafficLight trafficLight) throws SimRuntimeException
97      {
98          switch (trafficLight.getTrafficLightColor())
99          {
100             case RED:
101             {
102                 trafficLight.setTrafficLightColor(TrafficLightColor.GREEN);
103                 this.simulator.scheduleEventRel(new Duration(30.0, DurationUnit.SECOND), this, this, "changePhase",
104                         new Object[] {trafficLight});
105                 break;
106             }
107             case YELLOW:
108             {
109                 trafficLight.setTrafficLightColor(TrafficLightColor.RED);
110                 this.simulator.scheduleEventRel(new Duration(56.0, DurationUnit.SECOND), this, this, "changePhase",
111                         new Object[] {trafficLight});
112                 break;
113             }
114             case GREEN:
115             {
116                 trafficLight.setTrafficLightColor(TrafficLightColor.YELLOW);
117                 this.simulator.scheduleEventRel(new Duration(4.0, DurationUnit.SECOND), this, this, "changePhase",
118                         new Object[] {trafficLight});
119                 break;
120             }
121             default:
122             {
123                 //
124             }
125         }
126     }
127 
128     /** {@inheritDoc} */
129     @Override
130     public OTSRoadNetwork getNetwork()
131     {
132         return this.network;
133     }
134 }