View Javadoc
1   package org.opentrafficsim.demo.conflict;
2   
3   import java.awt.Dimension;
4   import java.io.Serializable;
5   import java.net.URL;
6   import java.rmi.RemoteException;
7   
8   import javax.naming.NamingException;
9   
10  import org.djunits.unit.DurationUnit;
11  import org.djunits.unit.LengthUnit;
12  import org.djunits.value.vdouble.scalar.Duration;
13  import org.djunits.value.vdouble.scalar.Length;
14  import org.djunits.value.vdouble.scalar.Time;
15  import org.djutils.io.URLResource;
16  import org.opentrafficsim.core.animation.gtu.colorer.DefaultSwitchableGtuColorer;
17  import org.opentrafficsim.core.dsol.AbstractOtsModel;
18  import org.opentrafficsim.core.dsol.OtsAnimator;
19  import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
20  import org.opentrafficsim.core.network.NetworkException;
21  import org.opentrafficsim.demo.conflict.TurboRoundaboutDemo.TurboRoundaboutModel;
22  import org.opentrafficsim.draw.core.OtsDrawingException;
23  import org.opentrafficsim.draw.road.TrafficLightAnimation;
24  import org.opentrafficsim.road.network.RoadNetwork;
25  import org.opentrafficsim.road.network.factory.xml.parser.XmlNetworkLaneParser;
26  import org.opentrafficsim.road.network.lane.CrossSectionLink;
27  import org.opentrafficsim.road.network.lane.Lane;
28  import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
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  import nl.tudelft.simulation.language.DSOLException;
35  
36  /**
37   * <p>
38   * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
39   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
40   * </p>
41   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
42   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
43   * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
44   */
45  public class TurboRoundaboutDemo extends OtsSimulationApplication<TurboRoundaboutModel>
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 TurboRoundaboutModel; the model
55       * @throws OtsDrawingException on animation error
56       */
57      public TurboRoundaboutDemo(final String title, final OtsAnimationPanel panel, final TurboRoundaboutModel 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("TurboRoundaboutDemo");
81              final TurboRoundaboutModel junctionModel = new TurboRoundaboutModel(simulator);
82              simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), junctionModel);
83              OtsAnimationPanel animationPanel =
84                      new OtsAnimationPanel(junctionModel.getNetwork().getExtent(), new Dimension(800, 600), simulator,
85                              junctionModel, new DefaultSwitchableGtuColorer(), junctionModel.getNetwork());
86              TurboRoundaboutDemo app = new TurboRoundaboutDemo("Turbo-Roundabout demo", animationPanel, junctionModel);
87              app.setExitOnClose(exitOnClose);
88              animationPanel.enableSimulationControlButtons();
89          }
90          catch (SimRuntimeException | NamingException | RemoteException | OtsDrawingException | DSOLException exception)
91          {
92              exception.printStackTrace();
93          }
94      }
95  
96      /**
97       * The simulation model.
98       */
99      public static class TurboRoundaboutModel extends AbstractOtsModel
100     {
101         /** */
102         private static final long serialVersionUID = 20161211L;
103 
104         /** The network. */
105         private RoadNetwork network;
106 
107         /**
108          * @param simulator OtsSimulatorInterface; the simulator for this model
109          */
110         public TurboRoundaboutModel(final OtsSimulatorInterface simulator)
111         {
112             super(simulator);
113         }
114 
115         /** {@inheritDoc} */
116         @Override
117         public void constructModel() throws SimRuntimeException
118         {
119             try
120             {
121                 URL xmlURL = URLResource.getResource("/resources/conflict/TurboRoundabout.xml");
122                 this.network = new RoadNetwork("TurboRoundabout", getSimulator());
123                 XmlNetworkLaneParser.build(xmlURL, this.network, true);
124 
125                 // add trafficlights
126                 for (Lane lane : ((CrossSectionLink) this.network.getLink("SEXITS2")).getLanes())
127                 {
128                     TrafficLight trafficLight = new TrafficLight("light" + lane.getId(), lane,
129                             new Length(150.0, LengthUnit.SI), this.simulator);
130                     trafficLight.setTrafficLightColor(TrafficLightColor.RED);
131                     changePhase(trafficLight);
132                 }
133 
134                 // test for ignoring conflicting GTU's upstream of traffic light
135                 // for (Lane lane : ((CrossSectionLink) this.network.getLink("SBEA")).getLanes())
136                 // {
137                 // SimpleTrafficLight trafficLight = new SimpleTrafficLight("light" + lane.getId(), lane,
138                 // new Length(10.0, LengthUnit.SI), this.simulator);
139                 //
140                 // try
141                 // {
142                 // new TrafficLightAnimation(trafficLight, simulator);
143                 // }
144                 // catch (RemoteException | NamingException exception)
145                 // {
146                 // throw new NetworkException(exception);
147                 // }
148                 //
149                 // trafficLight.setTrafficLightColor(TrafficLightColor.GREEN);
150                 // }
151 
152             }
153             catch (Exception exception)
154             {
155                 exception.printStackTrace();
156             }
157         }
158 
159         /**
160          * Changes color of traffic light.
161          * @param trafficLight SimpleTrafficLight; traffic light
162          * @throws SimRuntimeException scheduling error
163          */
164         private void changePhase(final TrafficLight trafficLight) throws SimRuntimeException
165         {
166             switch (trafficLight.getTrafficLightColor())
167             {
168                 case RED:
169                 {
170                     trafficLight.setTrafficLightColor(TrafficLightColor.GREEN);
171                     this.simulator.scheduleEventRel(new Duration(15.0, DurationUnit.SECOND), this, "changePhase",
172                             new Object[] {trafficLight});
173                     break;
174                 }
175                 case YELLOW:
176                 {
177                     trafficLight.setTrafficLightColor(TrafficLightColor.RED);
178                     this.simulator.scheduleEventRel(new Duration(56.0, DurationUnit.SECOND), this, "changePhase",
179                             new Object[] {trafficLight});
180                     break;
181                 }
182                 case GREEN:
183                 {
184                     trafficLight.setTrafficLightColor(TrafficLightColor.YELLOW);
185                     this.simulator.scheduleEventRel(new Duration(4.0, DurationUnit.SECOND), this, "changePhase",
186                             new Object[] {trafficLight});
187                     break;
188                 }
189                 default:
190                 {
191                     //
192                 }
193             }
194         }
195 
196         /** {@inheritDoc} */
197         @Override
198         public RoadNetwork getNetwork()
199         {
200             return this.network;
201         }
202 
203     }
204 }