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