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.TJunctionDemo.TJunctionModel;
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 TJunctionDemo extends OTSSimulationApplication<TJunctionModel>
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 TJunctionModel; the model
57       * @throws OTSDrawingException on animation error
58       */
59      public TJunctionDemo(final String title, final OTSAnimationPanel panel, final TJunctionModel 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 TJunctionModel junctionModel = new TJunctionModel(simulator);
84              simulator.initialize(Time.ZERO, Duration.ZERO, Duration.createSI(3600.0), junctionModel);
85              OTSAnimationPanel animationPanel = new OTSAnimationPanel(junctionModel.getNetwork().getExtent(),
86                      new Dimension(800, 600), simulator, junctionModel, DEFAULT_COLORER, junctionModel.getNetwork());
87              TJunctionDemo app = new TJunctionDemo("T-Junction demo", animationPanel, junctionModel);
88              app.setExitOnClose(exitOnClose);
89          }
90          catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException exception)
91          {
92              exception.printStackTrace();
93          }
94      }
95  
96      /**
97       * The simulation model.
98       */
99      static class TJunctionModel extends AbstractOTSModel
100     {
101         /** */
102         private static final long serialVersionUID = 20161211L;
103 
104         /** The network. */
105         private OTSNetwork network;
106 
107         /**
108          * @param simulator OTSSimulatorInterface; the simulator for this model
109          */
110         TJunctionModel(final OTSSimulatorInterface simulator)
111         {
112             super(simulator);
113         }
114 
115         /** {@inheritDoc} */
116         @Override
117         public void constructModel() throws SimRuntimeException
118         {
119             try
120             {
121                 URL url = URLResource.getResource("/conflict/TJunction.xml");
122                 XmlNetworkLaneParser nlp = new XmlNetworkLaneParser(this.simulator, new DefaultSwitchableGTUColorer());
123                 this.network = nlp.build(url, false);
124 
125                 // add conflicts
126                 // ((CrossSectionLink) this.network.getLink("SCEC")).setPriority(Priority.STOP);
127                 // ((CrossSectionLink) this.network.getLink("SCWC")).setPriority(Priority.STOP);
128                 ConflictBuilder.buildConflicts(this.network, VEHICLE, this.simulator,
129                         new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)));
130 
131                 // add trafficlight after
132                 Lane lane = ((CrossSectionLink) this.network.getLink("ECE")).getLanes().get(0);
133                 SimpleTrafficLight trafficLight =
134                         new SimpleTrafficLight("light", lane, new Length(50.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                 trafficLight.setTrafficLightColor(TrafficLightColor.RED);
145                 changePhase(trafficLight);
146 
147             }
148             catch (Exception exception)
149             {
150                 exception.printStackTrace();
151             }
152         }
153 
154         /**
155          * Changes color of traffic light.
156          * @param trafficLight SimpleTrafficLight; traffic light
157          * @throws SimRuntimeException scheduling error
158          */
159         private void changePhase(final SimpleTrafficLight trafficLight) throws SimRuntimeException
160         {
161             switch (trafficLight.getTrafficLightColor())
162             {
163                 case RED:
164                 {
165                     trafficLight.setTrafficLightColor(TrafficLightColor.GREEN);
166                     this.simulator.scheduleEventRel(new Duration(30.0, DurationUnit.SECOND), this, this, "changePhase",
167                             new Object[] { trafficLight });
168                     break;
169                 }
170                 case YELLOW:
171                 {
172                     trafficLight.setTrafficLightColor(TrafficLightColor.RED);
173                     this.simulator.scheduleEventRel(new Duration(56.0, DurationUnit.SECOND), this, this, "changePhase",
174                             new Object[] { trafficLight });
175                     break;
176                 }
177                 case GREEN:
178                 {
179                     trafficLight.setTrafficLightColor(TrafficLightColor.YELLOW);
180                     this.simulator.scheduleEventRel(new Duration(4.0, DurationUnit.SECOND), this, this, "changePhase",
181                             new Object[] { trafficLight });
182                     break;
183                 }
184                 default:
185                 {
186                     //
187                 }
188             }
189         }
190 
191         /** {@inheritDoc} */
192         @Override
193         public OTSNetwork getNetwork()
194         {
195             return this.network;
196         }
197 
198     }
199 }