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