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.OTSModelInterface;
20  import org.opentrafficsim.core.network.OTSLink;
21  import org.opentrafficsim.core.network.OTSNetwork;
22  import org.opentrafficsim.core.network.OTSNode;
23  import org.opentrafficsim.road.animation.AnimationToggles;
24  import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser;
25  import org.opentrafficsim.road.network.lane.CrossSectionLink;
26  import org.opentrafficsim.road.network.lane.Lane;
27  import org.opentrafficsim.road.network.lane.conflict.ConflictBuilder;
28  import org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight;
29  import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightColor;
30  import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
31  import org.opentrafficsim.simulationengine.OTSSimulationException;
32  
33  import nl.tudelft.simulation.dsol.SimRuntimeException;
34  import nl.tudelft.simulation.dsol.simtime.SimTimeDoubleUnit;
35  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
36  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
37  import nl.tudelft.simulation.language.io.URLResource;
38  
39  /**
40   * <p>
41   * Copyright (c) 2013-2018 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() throws OTSSimulationException
58      {
59          return new TurboRoundaboutModel();
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      protected final void addAnimationToggles()
65      {
66          AnimationToggles.setIconAnimationTogglesFull(this);
67          hideAnimationClass(OTSLink.class);
68          hideAnimationClass(OTSNode.class);
69          // addToggleAnimationButtonText("Block", LaneBlock.class, "Show/hide Blocks", false);
70          // addToggleAnimationButtonText("BlockId", LaneBlockAnimation.Text.class, "Show/hide Block Ids", false);
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public final String shortName()
76      {
77          return "Turbo roundabout demonstration";
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public final String description()
83      {
84          return "Turbo roundabout demonstration";
85      }
86  
87      /**
88       * The simulation model.
89       */
90      class TurboRoundaboutModel implements OTSModelInterface
91      {
92  
93          /** */
94          private static final long serialVersionUID = 20161211L;
95  
96          /** The network. */
97          private OTSNetwork network;
98  
99          /** Simulator. */
100         private DEVSSimulatorInterface.TimeDoubleUnit simulator;
101 
102         /** {@inheritDoc} */
103         @Override
104         public void constructModel(final SimulatorInterface<Time, Duration, SimTimeDoubleUnit> arg0)
105                 throws SimRuntimeException
106         {
107             this.simulator = (DEVSSimulatorInterface.TimeDoubleUnit) arg0;
108             try
109             {
110                 URL url = URLResource.getResource("/conflict/TurboRoundabout.xml");
111                 XmlNetworkLaneParser nlp = new XmlNetworkLaneParser(this.simulator, getColorer());
112                 this.network = nlp.build(url, false);
113 
114                 // add conflicts
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(15.0, DurationUnit.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, DurationUnit.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, DurationUnit.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, SimTimeDoubleUnit> getSimulator()
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(Time.ZERO, Duration.ZERO, new Duration(60.0, DurationUnit.MINUTE),
212                             new ArrayList<Property<?>>(), null, true);
213                 }
214                 catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
215                 {
216                     exception.printStackTrace();
217                 }
218             }
219         });
220     }
221 
222 }