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.OTSNetwork;
21  import org.opentrafficsim.road.animation.AnimationToggles;
22  import org.opentrafficsim.road.gtu.lane.plan.operational.LaneOperationalPlanBuilder;
23  import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser;
24  import org.opentrafficsim.road.network.lane.CrossSectionLink;
25  import org.opentrafficsim.road.network.lane.Lane;
26  import org.opentrafficsim.road.network.lane.conflict.ConflictBuilder;
27  import org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight;
28  import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightColor;
29  import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
30  import org.opentrafficsim.simulationengine.OTSSimulationException;
31  
32  import nl.tudelft.simulation.dsol.SimRuntimeException;
33  import nl.tudelft.simulation.dsol.simtime.SimTimeDoubleUnit;
34  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
35  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
36  import nl.tudelft.simulation.language.io.URLResource;
37  
38  /**
39   * <p>
40   * Copyright (c) 2013-2018 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 TJunctionDemo extends AbstractWrappableAnimation
49  {
50  
51      /** */
52      private static final long serialVersionUID = 20161211L;
53  
54      /** {@inheritDoc} */
55      @Override
56      protected final OTSModelInterface makeModel() throws OTSSimulationException
57      {
58          return new TJunctionModel();
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      protected final void addAnimationToggles()
64      {
65          AnimationToggles.setIconAnimationTogglesStandard(this);
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public final String shortName()
71      {
72          return "T-junction demonstration";
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public final String description()
78      {
79          return "T-junction demonstration";
80      }
81  
82      /**
83       * The simulation model.
84       */
85      class TJunctionModel implements OTSModelInterface
86      {
87  
88          /** */
89          private static final long serialVersionUID = 20161211L;
90  
91          /** The network. */
92          private OTSNetwork network;
93  
94          /** Simulator. */
95          private DEVSSimulatorInterface.TimeDoubleUnit simulator;
96  
97          /** {@inheritDoc} */
98          @Override
99          public void constructModel(final SimulatorInterface<Time, Duration, SimTimeDoubleUnit> arg0)
100                 throws SimRuntimeException
101         {
102             this.simulator = (DEVSSimulatorInterface.TimeDoubleUnit) arg0;
103             try
104             {
105                 URL url = URLResource.getResource("/conflict/TJunction.xml");
106                 XmlNetworkLaneParser nlp = new XmlNetworkLaneParser(this.simulator, TJunctionDemo.this.getColorer());
107                 this.network = nlp.build(url, false);
108 
109                 // add conflicts
110                 // ((CrossSectionLink) this.network.getLink("SCEC")).setPriority(Priority.STOP);
111                 // ((CrossSectionLink) this.network.getLink("SCWC")).setPriority(Priority.STOP);
112                 ConflictBuilder.buildConflicts(this.network, VEHICLE, this.simulator,
113                         new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)));
114 
115                 // add trafficlight after
116                 Lane lane = ((CrossSectionLink) this.network.getLink("ECE")).getLanes().get(0);
117                 SimpleTrafficLight trafficLight =
118                         new SimpleTrafficLight("light", lane, new Length(50.0, LengthUnit.SI), this.simulator);
119                 trafficLight.setTrafficLightColor(TrafficLightColor.RED);
120                 changePhase(trafficLight);
121 
122             }
123             catch (Exception exception)
124             {
125                 exception.printStackTrace();
126             }
127         }
128 
129         /**
130          * Changes color of traffic light.
131          * @param trafficLight traffic light
132          * @throws SimRuntimeException scheduling error
133          */
134         private void changePhase(final SimpleTrafficLight trafficLight) throws SimRuntimeException
135         {
136             switch (trafficLight.getTrafficLightColor())
137             {
138                 case RED:
139                 {
140                     trafficLight.setTrafficLightColor(TrafficLightColor.GREEN);
141                     this.simulator.scheduleEventRel(new Duration(30.0, DurationUnit.SECOND), this, this, "changePhase",
142                             new Object[] { trafficLight });
143                     break;
144                 }
145                 case YELLOW:
146                 {
147                     trafficLight.setTrafficLightColor(TrafficLightColor.RED);
148                     this.simulator.scheduleEventRel(new Duration(56.0, DurationUnit.SECOND), this, this, "changePhase",
149                             new Object[] { trafficLight });
150                     break;
151                 }
152                 case GREEN:
153                 {
154                     trafficLight.setTrafficLightColor(TrafficLightColor.YELLOW);
155                     this.simulator.scheduleEventRel(new Duration(4.0, DurationUnit.SECOND), this, this, "changePhase",
156                             new Object[] { trafficLight });
157                     break;
158                 }
159                 default:
160                 {
161                     //
162                 }
163             }
164         }
165 
166         /** {@inheritDoc} */
167         @Override
168         public SimulatorInterface<Time, Duration, SimTimeDoubleUnit> getSimulator()
169         {
170             return this.simulator;
171         }
172 
173         /** {@inheritDoc} */
174         @Override
175         public OTSNetwork getNetwork()
176         {
177             return this.network;
178         }
179 
180     }
181 
182     /**
183      * Main program.
184      * @param args String[]; the command line arguments (not used)
185      * @throws SimRuntimeException should never happen
186      */
187     public static void main(final String[] args) throws SimRuntimeException
188     {
189         LaneOperationalPlanBuilder.INSTANT_LANE_CHANGES = true;
190         SwingUtilities.invokeLater(new Runnable()
191         {
192             @Override
193             public void run()
194             {
195                 try
196                 {
197                     TJunctionDemo animation = new TJunctionDemo();
198                     // 1 hour simulation run for testing
199                     animation.buildAnimator(Time.ZERO, Duration.ZERO, new Duration(60.0, DurationUnit.MINUTE),
200                             new ArrayList<Property<?>>(), null, true);
201                 }
202                 catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
203                 {
204                     exception.printStackTrace();
205                 }
206             }
207         });
208     }
209 
210 }