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.dsol.AbstractOtsModel;
16  import org.opentrafficsim.core.dsol.OtsAnimator;
17  import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
18  import org.opentrafficsim.demo.conflict.TJunctionDemo.TJunctionModel;
19  import org.opentrafficsim.draw.core.OtsDrawingException;
20  import org.opentrafficsim.road.network.RoadNetwork;
21  import org.opentrafficsim.road.network.factory.xml.parser.XmlNetworkLaneParser;
22  import org.opentrafficsim.road.network.lane.CrossSectionLink;
23  import org.opentrafficsim.road.network.lane.Lane;
24  import org.opentrafficsim.road.network.lane.conflict.ConflictBuilder;
25  import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
26  import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightColor;
27  import org.opentrafficsim.swing.gui.OtsAnimationPanel;
28  import org.opentrafficsim.swing.gui.OtsSimulationApplication;
29  
30  import nl.tudelft.simulation.dsol.SimRuntimeException;
31  import nl.tudelft.simulation.language.DSOLException;
32  
33  /**
34   * <p>
35   * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
36   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
37   * </p>
38   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
39   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
40   * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
41   */
42  public class TJunctionDemo extends OtsSimulationApplication<TJunctionModel>
43  {
44      /** */
45      private static final long serialVersionUID = 20161211L;
46  
47      /**
48       * Create a T-Junction demo.
49       * @param title String; the title of the Frame
50       * @param panel OtsAnimationPanel; the tabbed panel to display
51       * @param model TJunctionModel; the model
52       * @throws OtsDrawingException on animation error
53       */
54      public TJunctionDemo(final String title, final OtsAnimationPanel panel, final TJunctionModel model)
55              throws OtsDrawingException
56      {
57          super(model, panel);
58      }
59  
60      /**
61       * Main program.
62       * @param args String[]; the command line arguments (not used)
63       */
64      public static void main(final String[] args)
65      {
66          demo(true);
67      }
68  
69      /**
70       * Start the demo.
71       * @param exitOnClose boolean; when running stand-alone: true; when running as part of a demo: false
72       */
73      public static void demo(final boolean exitOnClose)
74      {
75          try
76          {
77              OtsAnimator simulator = new OtsAnimator("TJunctionDemo");
78              final TJunctionModel junctionModel = new TJunctionModel(simulator);
79              simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), junctionModel);
80              OtsAnimationPanel animationPanel = new OtsAnimationPanel(junctionModel.getNetwork().getExtent(),
81                      new Dimension(800, 600), simulator, junctionModel, DEFAULT_COLORER, junctionModel.getNetwork());
82              TJunctionDemo app = new TJunctionDemo("T-Junction demo", animationPanel, junctionModel);
83              app.setExitOnClose(exitOnClose);
84              animationPanel.enableSimulationControlButtons();
85          }
86          catch (SimRuntimeException | NamingException | RemoteException | OtsDrawingException | DSOLException exception)
87          {
88              exception.printStackTrace();
89          }
90      }
91  
92      /**
93       * The simulation model.
94       */
95      public static class TJunctionModel extends AbstractOtsModel
96      {
97          /** */
98          private static final long serialVersionUID = 20161211L;
99  
100         /** The network. */
101         private RoadNetwork network;
102 
103         /**
104          * @param simulator OtsSimulatorInterface; the simulator for this model
105          */
106         public TJunctionModel(final OtsSimulatorInterface simulator)
107         {
108             super(simulator);
109         }
110 
111         /** {@inheritDoc} */
112         @Override
113         public void constructModel() throws SimRuntimeException
114         {
115             try
116             {
117                 URL xmlURL = URLResource.getResource("/resources/conflict/TJunction.xml");
118                 this.network = new RoadNetwork("TJunction", getSimulator());
119                 XmlNetworkLaneParser.build(xmlURL, this.network, false);
120 
121                 // add conflicts
122                 // ((CrossSectionLink) this.network.getLink("SCEC")).setPriority(Priority.STOP);
123                 // ((CrossSectionLink) this.network.getLink("SCWC")).setPriority(Priority.STOP);
124                 ConflictBuilder.buildConflicts(this.network, this.simulator,
125                         new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)));
126 
127                 // add trafficlight after
128                 Lane lane = ((CrossSectionLink) this.network.getLink("ECE")).getLanes().get(0);
129                 TrafficLight trafficLight =
130                         new TrafficLight("light", lane, new Length(50.0, LengthUnit.SI), this.simulator);
131 
132                 trafficLight.setTrafficLightColor(TrafficLightColor.RED);
133                 changePhase(trafficLight);
134 
135             }
136             catch (Exception exception)
137             {
138                 exception.printStackTrace();
139             }
140         }
141 
142         /**
143          * Changes color of traffic light.
144          * @param trafficLight SimpleTrafficLight; traffic light
145          * @throws SimRuntimeException scheduling error
146          */
147         private void changePhase(final TrafficLight trafficLight) throws SimRuntimeException
148         {
149             switch (trafficLight.getTrafficLightColor())
150             {
151                 case RED:
152                 {
153                     trafficLight.setTrafficLightColor(TrafficLightColor.GREEN);
154                     this.simulator.scheduleEventRel(new Duration(30.0, DurationUnit.SECOND), 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, "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, "changePhase",
169                             new Object[] {trafficLight});
170                     break;
171                 }
172                 default:
173                 {
174                     //
175                 }
176             }
177         }
178 
179         /** {@inheritDoc} */
180         @Override
181         public RoadNetwork getNetwork()
182         {
183             return this.network;
184         }
185 
186     }
187 }