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.network.NetworkException;
20  import org.opentrafficsim.demo.conflict.TurboRoundaboutDemo.TurboRoundaboutModel;
21  import org.opentrafficsim.draw.core.OTSDrawingException;
22  import org.opentrafficsim.draw.road.TrafficLightAnimation;
23  import org.opentrafficsim.road.network.OTSRoadNetwork;
24  import org.opentrafficsim.road.network.factory.xml.parser.XmlNetworkLaneParser;
25  import org.opentrafficsim.road.network.lane.CrossSectionLink;
26  import org.opentrafficsim.road.network.lane.Lane;
27  import org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight;
28  import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightColor;
29  import org.opentrafficsim.swing.gui.OTSAnimationPanel;
30  import org.opentrafficsim.swing.gui.OTSSimulationApplication;
31  
32  import nl.tudelft.simulation.dsol.SimRuntimeException;
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  public class TurboRoundaboutDemo extends OTSSimulationApplication<TurboRoundaboutModel>
45  {
46      
47      private static final long serialVersionUID = 20161211L;
48  
49      
50  
51  
52  
53  
54  
55  
56      public TurboRoundaboutDemo(final String title, final OTSAnimationPanel panel, final TurboRoundaboutModel model)
57              throws OTSDrawingException
58      {
59          super(model, panel);
60      }
61  
62      
63  
64  
65  
66      public static void main(final String[] args)
67      {
68          demo(true);
69      }
70  
71      
72  
73  
74  
75      public static void demo(final boolean exitOnClose)
76      {
77          try
78          {
79              OTSAnimator simulator = new OTSAnimator();
80              final TurboRoundaboutModel junctionModel = new TurboRoundaboutModel(simulator);
81              simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), junctionModel);
82              OTSAnimationPanel animationPanel =
83                      new OTSAnimationPanel(junctionModel.getNetwork().getExtent(), new Dimension(800, 600), simulator,
84                              junctionModel, new DefaultSwitchableGTUColorer(), junctionModel.getNetwork());
85              TurboRoundaboutDemoRoundaboutDemo.html#TurboRoundaboutDemo">TurboRoundaboutDemo app = new TurboRoundaboutDemo("Turbo-Roundabout demo", animationPanel, junctionModel);
86              app.setExitOnClose(exitOnClose);
87          }
88          catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException exception)
89          {
90              exception.printStackTrace();
91          }
92      }
93  
94      
95  
96  
97      public static class TurboRoundaboutModel extends AbstractOTSModel
98      {
99          
100         private static final long serialVersionUID = 20161211L;
101 
102         
103         private OTSRoadNetwork network;
104 
105         
106 
107 
108         public TurboRoundaboutModel(final OTSSimulatorInterface simulator)
109         {
110             super(simulator);
111         }
112 
113         
114         @Override
115         public void constructModel() throws SimRuntimeException
116         {
117             try
118             {
119                 URL xmlURL = URLResource.getResource("/conflict/TurboRoundabout.xml");
120                 this.network = new OTSRoadNetwork("TurboRoundabout", true);
121                 XmlNetworkLaneParser.build(xmlURL, this.network, getSimulator(), true);
122 
123                 
124                 for (Lane lane : ((CrossSectionLink) this.network.getLink("SEXITS2")).getLanes())
125                 {
126                     SimpleTrafficLight trafficLight = new SimpleTrafficLight("light" + lane.getId(), lane,
127                             new Length(150.0, LengthUnit.SI), this.simulator);
128 
129                     try
130                     {
131                         new TrafficLightAnimation(trafficLight, this.simulator);
132                     }
133                     catch (RemoteException | NamingException exception)
134                     {
135                         throw new NetworkException(exception);
136                     }
137 
138                     trafficLight.setTrafficLightColor(TrafficLightColor.RED);
139                     changePhase(trafficLight);
140                 }
141 
142                 
143                 
144                 
145                 
146                 
147                 
148                 
149                 
150                 
151                 
152                 
153                 
154                 
155                 
156                 
157                 
158                 
159 
160             }
161             catch (Exception exception)
162             {
163                 exception.printStackTrace();
164             }
165         }
166 
167         
168 
169 
170 
171 
172         private void changePhase(final SimpleTrafficLight trafficLight) throws SimRuntimeException
173         {
174             switch (trafficLight.getTrafficLightColor())
175             {
176                 case RED:
177                 {
178                     trafficLight.setTrafficLightColor(TrafficLightColor.GREEN);
179                     this.simulator.scheduleEventRel(new Duration(15.0, DurationUnit.SECOND), this, this, "changePhase",
180                             new Object[] {trafficLight});
181                     break;
182                 }
183                 case YELLOW:
184                 {
185                     trafficLight.setTrafficLightColor(TrafficLightColor.RED);
186                     this.simulator.scheduleEventRel(new Duration(56.0, DurationUnit.SECOND), this, this, "changePhase",
187                             new Object[] {trafficLight});
188                     break;
189                 }
190                 case GREEN:
191                 {
192                     trafficLight.setTrafficLightColor(TrafficLightColor.YELLOW);
193                     this.simulator.scheduleEventRel(new Duration(4.0, DurationUnit.SECOND), this, this, "changePhase",
194                             new Object[] {trafficLight});
195                     break;
196                 }
197                 default:
198                 {
199                     
200                 }
201             }
202         }
203 
204         
205         @Override
206         public OTSRoadNetwork getNetwork()
207         {
208             return this.network;
209         }
210 
211     }
212 }