View Javadoc
1   package org.opentrafficsim.demo.conflict;
2   
3   import java.awt.Dimension;
4   import java.net.URL;
5   import java.rmi.RemoteException;
6   
7   import javax.naming.NamingException;
8   
9   import org.djunits.unit.DurationUnit;
10  import org.djunits.unit.LengthUnit;
11  import org.djunits.value.vdouble.scalar.Duration;
12  import org.djunits.value.vdouble.scalar.Length;
13  import org.djunits.value.vdouble.scalar.Time;
14  import org.djutils.io.URLResource;
15  import org.opentrafficsim.core.animation.gtu.colorer.DefaultSwitchableGTUColorer;
16  import org.opentrafficsim.core.dsol.AbstractOTSModel;
17  import org.opentrafficsim.core.dsol.OTSAnimator;
18  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
19  import org.opentrafficsim.core.gtu.GTUType;
20  import org.opentrafficsim.core.network.NetworkException;
21  import org.opentrafficsim.demo.conflict.TJunctionDemo.TJunctionModel;
22  import org.opentrafficsim.draw.core.OTSDrawingException;
23  import org.opentrafficsim.draw.road.TrafficLightAnimation;
24  import org.opentrafficsim.road.network.OTSRoadNetwork;
25  import org.opentrafficsim.road.network.factory.xml.old.XmlNetworkLaneParserOld;
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.swing.gui.OTSAnimationPanel;
32  import org.opentrafficsim.swing.gui.OTSSimulationApplication;
33  
34  import nl.tudelft.simulation.dsol.SimRuntimeException;
35  
36  /**
37   * <p>
38   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
39   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
40   * <p>
41   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 11 dec. 2016 <br>
42   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
43   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
44   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
45   */
46  public class TJunctionDemo extends OTSSimulationApplication<TJunctionModel>
47  {
48      /** */
49      private static final long serialVersionUID = 20161211L;
50  
51      /**
52       * Create a T-Junction demo.
53       * @param title String; the title of the Frame
54       * @param panel OTSAnimationPanel; the tabbed panel to display
55       * @param model TJunctionModel; the model
56       * @throws OTSDrawingException on animation error
57       */
58      public TJunctionDemo(final String title, final OTSAnimationPanel panel, final TJunctionModel model)
59              throws OTSDrawingException
60      {
61          super(model, panel);
62      }
63  
64      /**
65       * Main program.
66       * @param args String[]; the command line arguments (not used)
67       */
68      public static void main(final String[] args)
69      {
70          demo(true);
71      }
72  
73      /**
74       * Start the demo.
75       * @param exitOnClose boolean; when running stand-alone: true; when running as part of a demo: false
76       */
77      public static void demo(final boolean exitOnClose)
78      {
79          try
80          {
81              OTSAnimator simulator = new OTSAnimator();
82              final TJunctionModel junctionModel = new TJunctionModel(simulator);
83              simulator.initialize(Time.ZERO, Duration.ZERO, Duration.createSI(3600.0), junctionModel);
84              OTSAnimationPanel animationPanel = new OTSAnimationPanel(junctionModel.getNetwork().getExtent(),
85                      new Dimension(800, 600), simulator, junctionModel, DEFAULT_COLORER, junctionModel.getNetwork());
86              TJunctionDemo app = new TJunctionDemo("T-Junction demo", animationPanel, junctionModel);
87              app.setExitOnClose(exitOnClose);
88          }
89          catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException exception)
90          {
91              exception.printStackTrace();
92          }
93      }
94  
95      /**
96       * The simulation model.
97       */
98      static class TJunctionModel extends AbstractOTSModel
99      {
100         /** */
101         private static final long serialVersionUID = 20161211L;
102 
103         /** The network. */
104         private OTSRoadNetwork network;
105 
106         /**
107          * @param simulator OTSSimulatorInterface; the simulator for this model
108          */
109         TJunctionModel(final OTSSimulatorInterface simulator)
110         {
111             super(simulator);
112         }
113 
114         /** {@inheritDoc} */
115         @Override
116         public void constructModel() throws SimRuntimeException
117         {
118             try
119             {
120                 URL url = URLResource.getResource("/conflict/TJunction.xml");
121                 XmlNetworkLaneParserOld nlp = new XmlNetworkLaneParserOld(this.simulator, new DefaultSwitchableGTUColorer());
122                 this.network = nlp.build(url, false);
123 
124                 // add conflicts
125                 // ((CrossSectionLink) this.network.getLink("SCEC")).setPriority(Priority.STOP);
126                 // ((CrossSectionLink) this.network.getLink("SCWC")).setPriority(Priority.STOP);
127                 ConflictBuilder.buildConflicts(this.network, this.network.getGtuType(GTUType.DEFAULTS.VEHICLE), this.simulator,
128                         new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)));
129 
130                 // add trafficlight after
131                 Lane lane = ((CrossSectionLink) this.network.getLink("ECE")).getLanes().get(0);
132                 SimpleTrafficLight trafficLight =
133                         new SimpleTrafficLight("light", lane, new Length(50.0, LengthUnit.SI), this.simulator);
134 
135                 try
136                 {
137                     new TrafficLightAnimation(trafficLight, this.simulator);
138                 }
139                 catch (RemoteException | NamingException exception)
140                 {
141                     throw new NetworkException(exception);
142                 }
143                 trafficLight.setTrafficLightColor(TrafficLightColor.RED);
144                 changePhase(trafficLight);
145 
146             }
147             catch (Exception exception)
148             {
149                 exception.printStackTrace();
150             }
151         }
152 
153         /**
154          * Changes color of traffic light.
155          * @param trafficLight SimpleTrafficLight; traffic light
156          * @throws SimRuntimeException scheduling error
157          */
158         private void changePhase(final SimpleTrafficLight trafficLight) throws SimRuntimeException
159         {
160             switch (trafficLight.getTrafficLightColor())
161             {
162                 case RED:
163                 {
164                     trafficLight.setTrafficLightColor(TrafficLightColor.GREEN);
165                     this.simulator.scheduleEventRel(new Duration(30.0, DurationUnit.SECOND), this, this, "changePhase",
166                             new Object[] {trafficLight});
167                     break;
168                 }
169                 case YELLOW:
170                 {
171                     trafficLight.setTrafficLightColor(TrafficLightColor.RED);
172                     this.simulator.scheduleEventRel(new Duration(56.0, DurationUnit.SECOND), this, this, "changePhase",
173                             new Object[] {trafficLight});
174                     break;
175                 }
176                 case GREEN:
177                 {
178                     trafficLight.setTrafficLightColor(TrafficLightColor.YELLOW);
179                     this.simulator.scheduleEventRel(new Duration(4.0, DurationUnit.SECOND), this, this, "changePhase",
180                             new Object[] {trafficLight});
181                     break;
182                 }
183                 default:
184                 {
185                     //
186                 }
187             }
188         }
189 
190         /** {@inheritDoc} */
191         @Override
192         public OTSRoadNetwork getNetwork()
193         {
194             return this.network;
195         }
196 
197     }
198 }