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