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