View Javadoc
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.core.gtu.GTUType;
19  import org.opentrafficsim.core.network.NetworkException;
20  import org.opentrafficsim.demo.conflict.TJunctionDemo.TJunctionModel;
21  import org.opentrafficsim.draw.core.OTSDrawingException;
22  import org.opentrafficsim.draw.road.TrafficLightAnimation;
23  import org.opentrafficsim.road.network.OTSRoadNetwork;
24  import org.opentrafficsim.road.network.factory.xml.parser.XmlNetworkLaneParser;
25  import org.opentrafficsim.road.network.lane.CrossSectionLink;
26  import org.opentrafficsim.road.network.lane.Lane;
27  import org.opentrafficsim.road.network.lane.conflict.ConflictBuilder;
28  import org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight;
29  import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightColor;
30  import org.opentrafficsim.swing.gui.OTSAnimationPanel;
31  import org.opentrafficsim.swing.gui.OTSSimulationApplication;
32  
33  import nl.tudelft.simulation.dsol.SimRuntimeException;
34  
35  /**
36   * <p>
37   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
38   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
39   * <p>
40   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 11 dec. 2016 <br>
41   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
42   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
43   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
44   */
45  public class TJunctionDemo extends OTSSimulationApplication<TJunctionModel>
46  {
47      /** */
48      private static final long serialVersionUID = 20161211L;
49  
50      /**
51       * Create a T-Junction demo.
52       * @param title String; the title of the Frame
53       * @param panel OTSAnimationPanel; the tabbed panel to display
54       * @param model TJunctionModel; the model
55       * @throws OTSDrawingException on animation error
56       */
57      public TJunctionDemo(final String title, final OTSAnimationPanel panel, final TJunctionModel model)
58              throws OTSDrawingException
59      {
60          super(model, panel);
61      }
62  
63      /**
64       * Main program.
65       * @param args String[]; the command line arguments (not used)
66       */
67      public static void main(final String[] args)
68      {
69          demo(true);
70      }
71  
72      /**
73       * Start the demo.
74       * @param exitOnClose boolean; when running stand-alone: true; when running as part of a demo: false
75       */
76      public static void demo(final boolean exitOnClose)
77      {
78          try
79          {
80              OTSAnimator simulator = new OTSAnimator();
81              final TJunctionModel junctionModel = new TJunctionModel(simulator);
82              simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), junctionModel);
83              OTSAnimationPanel animationPanel = new OTSAnimationPanel(junctionModel.getNetwork().getExtent(),
84                      new Dimension(800, 600), simulator, junctionModel, DEFAULT_COLORER, junctionModel.getNetwork());
85              TJunctionDemo/TJunctionDemo.html#TJunctionDemo">TJunctionDemo app = new TJunctionDemo("T-Junction demo", animationPanel, junctionModel);
86              app.setExitOnClose(exitOnClose);
87          }
88          catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException exception)
89          {
90              exception.printStackTrace();
91          }
92      }
93  
94      /**
95       * The simulation model.
96       */
97      public static class TJunctionModel extends AbstractOTSModel
98      {
99          /** */
100         private static final long serialVersionUID = 20161211L;
101 
102         /** The network. */
103         private OTSRoadNetwork network;
104 
105         /**
106          * @param simulator OTSSimulatorInterface; the simulator for this model
107          */
108         public TJunctionModel(final OTSSimulatorInterface simulator)
109         {
110             super(simulator);
111         }
112 
113         /** {@inheritDoc} */
114         @Override
115         public void constructModel() throws SimRuntimeException
116         {
117             try
118             {
119                 URL xmlURL = URLResource.getResource("/conflict/TJunction.xml");
120                 this.network = new OTSRoadNetwork("TJunction", true);
121                 XmlNetworkLaneParser.build(xmlURL, this.network, getSimulator(), false);
122 
123                 // add conflicts
124                 // ((CrossSectionLink) this.network.getLink("SCEC")).setPriority(Priority.STOP);
125                 // ((CrossSectionLink) this.network.getLink("SCWC")).setPriority(Priority.STOP);
126                 ConflictBuilder.buildConflicts(this.network, this.network.getGtuType(GTUType.DEFAULTS.VEHICLE), this.simulator,
127                         new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)));
128 
129                 // add trafficlight after
130                 Lane lane = ((CrossSectionLink) this.network.getLink("ECE")).getLanes().get(0);
131                 SimpleTrafficLight trafficLight =
132                         new SimpleTrafficLight("light", lane, new Length(50.0, LengthUnit.SI), this.simulator);
133 
134                 try
135                 {
136                     new TrafficLightAnimation(trafficLight, this.simulator);
137                 }
138                 catch (RemoteException | NamingException exception)
139                 {
140                     throw new NetworkException(exception);
141                 }
142                 trafficLight.setTrafficLightColor(TrafficLightColor.RED);
143                 changePhase(trafficLight);
144 
145             }
146             catch (Exception exception)
147             {
148                 exception.printStackTrace();
149             }
150         }
151 
152         /**
153          * Changes color of traffic light.
154          * @param trafficLight SimpleTrafficLight; traffic light
155          * @throws SimRuntimeException scheduling error
156          */
157         private void changePhase(final SimpleTrafficLight trafficLight) throws SimRuntimeException
158         {
159             switch (trafficLight.getTrafficLightColor())
160             {
161                 case RED:
162                 {
163                     trafficLight.setTrafficLightColor(TrafficLightColor.GREEN);
164                     this.simulator.scheduleEventRel(new Duration(30.0, DurationUnit.SECOND), this, this, "changePhase",
165                             new Object[] {trafficLight});
166                     break;
167                 }
168                 case YELLOW:
169                 {
170                     trafficLight.setTrafficLightColor(TrafficLightColor.RED);
171                     this.simulator.scheduleEventRel(new Duration(56.0, DurationUnit.SECOND), this, this, "changePhase",
172                             new Object[] {trafficLight});
173                     break;
174                 }
175                 case GREEN:
176                 {
177                     trafficLight.setTrafficLightColor(TrafficLightColor.YELLOW);
178                     this.simulator.scheduleEventRel(new Duration(4.0, DurationUnit.SECOND), this, this, "changePhase",
179                             new Object[] {trafficLight});
180                     break;
181                 }
182                 default:
183                 {
184                     //
185                 }
186             }
187         }
188 
189         /** {@inheritDoc} */
190         @Override
191         public OTSRoadNetwork getNetwork()
192         {
193             return this.network;
194         }
195 
196     }
197 }