View Javadoc
1   package org.opentrafficsim.demo.conflict;
2   
3   import static org.opentrafficsim.core.gtu.GTUType.VEHICLE;
4   
5   import java.net.URL;
6   import java.rmi.RemoteException;
7   import java.util.ArrayList;
8   
9   import javax.naming.NamingException;
10  import javax.swing.SwingUtilities;
11  
12  import org.djunits.unit.LengthUnit;
13  import org.djunits.unit.TimeUnit;
14  import org.djunits.value.vdouble.scalar.Duration;
15  import org.djunits.value.vdouble.scalar.Length;
16  import org.djunits.value.vdouble.scalar.Time;
17  import org.opentrafficsim.base.modelproperties.Property;
18  import org.opentrafficsim.base.modelproperties.PropertyException;
19  import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
20  import org.opentrafficsim.core.dsol.OTSModelInterface;
21  import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
22  import org.opentrafficsim.core.gtu.animation.GTUColorer;
23  import org.opentrafficsim.core.network.OTSNetwork;
24  import org.opentrafficsim.road.animation.AnimationToggles;
25  import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser;
26  import org.opentrafficsim.road.network.lane.CrossSectionLink;
27  import org.opentrafficsim.road.network.lane.CrossSectionLink.Priority;
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.simulationengine.AbstractWrappableAnimation;
33  import org.opentrafficsim.simulationengine.OTSSimulationException;
34  
35  import nl.tudelft.simulation.dsol.SimRuntimeException;
36  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
37  import nl.tudelft.simulation.language.io.URLResource;
38  
39  /**
40   * <p>
41   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
42   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
43   * <p>
44   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 11 dec. 2016 <br>
45   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
46   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
47   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
48   */
49  public class TurboRoundaboutDemo extends AbstractWrappableAnimation
50  {
51  
52      /** */
53      private static final long serialVersionUID = 20161211L;
54  
55      /** {@inheritDoc} */
56      @Override
57      protected final OTSModelInterface makeModel(final GTUColorer colorer) throws OTSSimulationException
58      {
59          return new TurboRoundaboutModel();
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      protected final void addAnimationToggles()
65      {
66          AnimationToggles.setTextAnimationTogglesStandard(this);
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      public final String shortName()
72      {
73          return "Turbo roundabout demonstration";
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      public final String description()
79      {
80          return "Turbo roundabout demonstration";
81      }
82  
83      /**
84       * The simulation model.
85       */
86      class TurboRoundaboutModel implements OTSModelInterface
87      {
88  
89          /** */
90          private static final long serialVersionUID = 20161211L;
91  
92          /** The network. */
93          private OTSNetwork network;
94  
95          /** Simulator. */
96          private OTSDEVSSimulatorInterface simulator;
97  
98          /** {@inheritDoc} */
99          @Override
100         public void constructModel(final SimulatorInterface<Time, Duration, OTSSimTimeDouble> arg0)
101                 throws SimRuntimeException, RemoteException
102         {
103             this.simulator = (OTSDEVSSimulatorInterface) arg0;
104             try
105             {
106                 URL url = URLResource.getResource("/conflict/TurboRoundabout.xml");
107                 XmlNetworkLaneParser nlp = new XmlNetworkLaneParser(this.simulator);
108                 this.network = nlp.build(url);
109 
110                 // add conflicts
111                 ((CrossSectionLink) this.network.getLink("EBNA")).setPriority(Priority.PRIORITY);
112                 ((CrossSectionLink) this.network.getLink("NBWA")).setPriority(Priority.PRIORITY);
113                 ((CrossSectionLink) this.network.getLink("WBSA")).setPriority(Priority.PRIORITY);
114                 ((CrossSectionLink) this.network.getLink("SBEA")).setPriority(Priority.PRIORITY);
115                 ConflictBuilder.buildConflicts(this.network, VEHICLE, this.simulator,
116                         new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)));
117 
118                 // add trafficlights
119                 for (Lane lane : ((CrossSectionLink) this.network.getLink("SEXITS")).getLanes())
120                 {
121                     SimpleTrafficLight trafficLight = new SimpleTrafficLight("light" + lane.getId(), lane,
122                             new Length(150.0, LengthUnit.SI), this.simulator);
123                     trafficLight.setTrafficLightColor(TrafficLightColor.RED);
124                     changePhase(trafficLight);
125                 }
126                 
127                 // test for ignoring conflicting GTU's upstream of traffic light
128 //                for (Lane lane : ((CrossSectionLink) this.network.getLink("SBEA")).getLanes())
129 //                {
130 //                    SimpleTrafficLight trafficLight = new SimpleTrafficLight("light" + lane.getId(), lane,
131 //                            new Length(10.0, LengthUnit.SI), this.simulator);
132 //                    trafficLight.setTrafficLightColor(TrafficLightColor.GREEN);
133 //                }
134                 
135             }
136             catch (Exception exception)
137             {
138                 exception.printStackTrace();
139             }
140         }
141 
142         /**
143          * Changes color of traffic light.
144          * @param trafficLight traffic light
145          * @throws SimRuntimeException scheduling error
146          */
147         private void changePhase(final SimpleTrafficLight trafficLight) throws SimRuntimeException
148         {
149             switch (trafficLight.getTrafficLightColor())
150             {
151                 case RED:
152                 {
153                     trafficLight.setTrafficLightColor(TrafficLightColor.GREEN);
154                     this.simulator.scheduleEventRel(new Duration(30.0, TimeUnit.SECOND), this, this, "changePhase",
155                             new Object[] { trafficLight });
156                     break;
157                 }
158                 case YELLOW:
159                 {
160                     trafficLight.setTrafficLightColor(TrafficLightColor.RED);
161                     this.simulator.scheduleEventRel(new Duration(56.0, TimeUnit.SECOND), this, this, "changePhase",
162                             new Object[] { trafficLight });
163                     break;
164                 }
165                 case GREEN:
166                 {
167                     trafficLight.setTrafficLightColor(TrafficLightColor.YELLOW);
168                     this.simulator.scheduleEventRel(new Duration(4.0, TimeUnit.SECOND), this, this, "changePhase",
169                             new Object[] { trafficLight });
170                     break;
171                 }
172                 default:
173                 {
174                     //
175                 }
176             }
177         }
178 
179         /** {@inheritDoc} */
180         @Override
181         public SimulatorInterface<Time, Duration, OTSSimTimeDouble> getSimulator() throws RemoteException
182         {
183             return this.simulator;
184         }
185 
186         /** {@inheritDoc} */
187         @Override
188         public OTSNetwork getNetwork()
189         {
190             return this.network;
191         }
192 
193     }
194 
195     /**
196      * Main program.
197      * @param args String[]; the command line arguments (not used)
198      * @throws SimRuntimeException should never happen
199      */
200     public static void main(final String[] args) throws SimRuntimeException
201     {
202         SwingUtilities.invokeLater(new Runnable()
203         {
204             @Override
205             public void run()
206             {
207                 try
208                 {
209                     TurboRoundaboutDemo animation = new TurboRoundaboutDemo();
210                     // 1 hour simulation run for testing
211                     animation.buildAnimator(new Time(0.0, TimeUnit.SECOND), new Duration(0.0, TimeUnit.SECOND),
212                             new Duration(60.0, TimeUnit.MINUTE), new ArrayList<Property<?>>(), null, true);
213                 }
214                 catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
215                 {
216                     exception.printStackTrace();
217                 }
218             }
219         });
220     }
221 
222 }