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.OTSRoadNetwork;
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.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  import nl.tudelft.simulation.language.DSOLException;
35  
36  /**
37   * <p>
38   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
39   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
40   * <p>
41   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 11 dec. 2016 <br>
42   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
43   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
44   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
45   */
46  public class TurboRoundaboutDemo extends OTSSimulationApplication<TurboRoundaboutModel>
47  {
48      /** */
49      private static final long serialVersionUID = 20161211L;
50  
51      /**
52       * Create a T-Junction demo.
53       * @param title String; the title of the Frame
54       * @param panel OTSAnimationPanel; the tabbed panel to display
55       * @param model TurboRoundaboutModel; the model
56       * @throws OTSDrawingException on animation error
57       */
58      public TurboRoundaboutDemo(final String title, final OTSAnimationPanel panel, final TurboRoundaboutModel model)
59              throws OTSDrawingException
60      {
61          super(model, panel);
62      }
63  
64      /**
65       * Main program.
66       * @param args String[]; the command line arguments (not used)
67       */
68      public static void main(final String[] args)
69      {
70          demo(true);
71      }
72  
73      /**
74       * Start the demo.
75       * @param exitOnClose boolean; when running stand-alone: true; when running as part of a demo: false
76       */
77      public static void demo(final boolean exitOnClose)
78      {
79          try
80          {
81              OTSAnimator simulator = new OTSAnimator("TurboRoundaboutDemo");
82              final TurboRoundaboutModel junctionModel = new TurboRoundaboutModel(simulator);
83              simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), junctionModel);
84              OTSAnimationPanel animationPanel =
85                      new OTSAnimationPanel(junctionModel.getNetwork().getExtent(), new Dimension(800, 600), simulator,
86                              junctionModel, new DefaultSwitchableGTUColorer(), junctionModel.getNetwork());
87              TurboRoundaboutDemoRoundaboutDemo.html#TurboRoundaboutDemo">TurboRoundaboutDemo app = new TurboRoundaboutDemo("Turbo-Roundabout demo", animationPanel, junctionModel);
88              app.setExitOnClose(exitOnClose);
89              animationPanel.enableSimulationControlButtons();
90          }
91          catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
92          {
93              exception.printStackTrace();
94          }
95      }
96  
97      /**
98       * The simulation model.
99       */
100     public static class TurboRoundaboutModel extends AbstractOTSModel
101     {
102         /** */
103         private static final long serialVersionUID = 20161211L;
104 
105         /** The network. */
106         private OTSRoadNetwork network;
107 
108         /**
109          * @param simulator OTSSimulatorInterface; the simulator for this model
110          */
111         public TurboRoundaboutModel(final OTSSimulatorInterface simulator)
112         {
113             super(simulator);
114         }
115 
116         /** {@inheritDoc} */
117         @Override
118         public void constructModel() throws SimRuntimeException
119         {
120             try
121             {
122                 URL xmlURL = URLResource.getResource("/conflict/TurboRoundabout.xml");
123                 this.network = new OTSRoadNetwork("TurboRoundabout", true, getSimulator());
124                 XmlNetworkLaneParser.build(xmlURL, this.network, true);
125 
126                 // add trafficlights
127                 for (Lane lane : ((CrossSectionLink) this.network.getLink("SEXITS2")).getLanes())
128                 {
129                     SimpleTrafficLight trafficLight = new SimpleTrafficLight("light" + lane.getId(), lane,
130                             new Length(150.0, LengthUnit.SI), this.simulator);
131 
132                     try
133                     {
134                         new TrafficLightAnimation(trafficLight, this.simulator);
135                     }
136                     catch (RemoteException | NamingException exception)
137                     {
138                         throw new NetworkException(exception);
139                     }
140 
141                     trafficLight.setTrafficLightColor(TrafficLightColor.RED);
142                     changePhase(trafficLight);
143                 }
144 
145                 // test for ignoring conflicting GTU's upstream of traffic light
146                 // for (Lane lane : ((CrossSectionLink) this.network.getLink("SBEA")).getLanes())
147                 // {
148                 // SimpleTrafficLight trafficLight = new SimpleTrafficLight("light" + lane.getId(), lane,
149                 // new Length(10.0, LengthUnit.SI), this.simulator);
150                 //
151                 // try
152                 // {
153                 // new TrafficLightAnimation(trafficLight, simulator);
154                 // }
155                 // catch (RemoteException | NamingException exception)
156                 // {
157                 // throw new NetworkException(exception);
158                 // }
159                 //
160                 // trafficLight.setTrafficLightColor(TrafficLightColor.GREEN);
161                 // }
162 
163             }
164             catch (Exception exception)
165             {
166                 exception.printStackTrace();
167             }
168         }
169 
170         /**
171          * Changes color of traffic light.
172          * @param trafficLight SimpleTrafficLight; traffic light
173          * @throws SimRuntimeException scheduling error
174          */
175         private void changePhase(final SimpleTrafficLight trafficLight) throws SimRuntimeException
176         {
177             switch (trafficLight.getTrafficLightColor())
178             {
179                 case RED:
180                 {
181                     trafficLight.setTrafficLightColor(TrafficLightColor.GREEN);
182                     this.simulator.scheduleEventRel(new Duration(15.0, DurationUnit.SECOND), this, this, "changePhase",
183                             new Object[] {trafficLight});
184                     break;
185                 }
186                 case YELLOW:
187                 {
188                     trafficLight.setTrafficLightColor(TrafficLightColor.RED);
189                     this.simulator.scheduleEventRel(new Duration(56.0, DurationUnit.SECOND), this, this, "changePhase",
190                             new Object[] {trafficLight});
191                     break;
192                 }
193                 case GREEN:
194                 {
195                     trafficLight.setTrafficLightColor(TrafficLightColor.YELLOW);
196                     this.simulator.scheduleEventRel(new Duration(4.0, DurationUnit.SECOND), this, this, "changePhase",
197                             new Object[] {trafficLight});
198                     break;
199                 }
200                 default:
201                 {
202                     //
203                 }
204             }
205         }
206 
207         /** {@inheritDoc} */
208         @Override
209         public OTSRoadNetwork getNetwork()
210         {
211             return this.network;
212         }
213 
214         /** {@inheritDoc} */
215         @Override
216         public Serializable getSourceId()
217         {
218             return "TurboRoundaboutModel";
219         }
220 
221     }
222 }