View Javadoc
1   package org.opentrafficsim.road.network.factory.opendrive;
2   
3   import java.util.HashMap;
4   import java.util.HashSet;
5   import java.util.Map;
6   import java.util.Set;
7   
8   import javax.naming.NamingException;
9   
10  import nl.tudelft.simulation.dsol.SimRuntimeException;
11  
12  import org.djunits.unit.TimeUnit;
13  import org.djunits.value.vdouble.scalar.Time;
14  import org.opentrafficsim.core.dsol.OTSAnimatorInterface;
15  import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
16  import org.opentrafficsim.core.gtu.GTUException;
17  import org.opentrafficsim.core.network.NetworkException;
18  import org.opentrafficsim.road.gtu.lane.object.AbstractTrafficLightNew;
19  import org.opentrafficsim.road.gtu.lane.object.TrafficLight.TrafficLightColor;
20  
21  /**
22   * Traffic Light Controller
23   * <p>
24   * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
25   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
26   * <p>
27   * @version $Revision: 1155 $, $LastChangedDate: 2015-07-26 01:01:13 +0200 (Sun, 26 Jul 2015) $, by $Author: averbraeck $,
28   *          initial version 15 jul. 2015 <br>
29   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
30   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
31   */
32  public class Controller
33  {
34  
35      /** */
36      private OTSDEVSSimulatorInterface simulator;
37  
38      /** */
39      private String id;
40  
41      /** */
42      private Map<Integer, Set<AbstractTrafficLightNew>> trafficLights = new HashMap<Integer, Set<AbstractTrafficLightNew>>();
43  
44      /**
45       * @param name the name of the OnOffTrafficLight
46       * @param simulator the simulator to avoid NullPointerExceptions
47       * @throws GTUException when GTU cannot be created.
48       * @throws NamingException if an error occurs when adding the animation handler
49       * @throws NetworkException when the GTU cannot be placed on the given lane
50       */
51      public Controller(final String name, final OTSDEVSSimulatorInterface simulator) throws GTUException,
52          NetworkException, NamingException
53      {
54          this.id = name;
55          this.simulator = simulator;
56  
57          try
58          {
59              // new DefaultBlockOnOffAnimation(this, getSimulator());
60              // animation
61              if (simulator instanceof OTSAnimatorInterface)
62              {
63                  // TODO
64              }
65              simulator.scheduleEventRel(new Time.Rel(0.0, TimeUnit.SECOND), this, this, "unBlock", null);
66          }
67          catch (SimRuntimeException exception)
68          {
69              exception.printStackTrace();
70          }
71      }
72  
73      /**
74       * 
75       */
76      protected void unBlock()
77      {
78          try
79          {
80              this.simulator.scheduleEventRel(new Time.Rel(96.0, TimeUnit.SECOND), this, this, "unBlock", null);
81          }
82          catch (SimRuntimeException exception)
83          {
84              exception.printStackTrace();
85          }
86  
87          if (this.trafficLights.containsKey(3))
88          {
89              for (AbstractTrafficLightNew light : this.trafficLights.get(3))
90              {
91                  try
92                  {
93                      // System.out.println("traffic light 3 at time " + this.simulator.getSimulatorTime() + " is " +
94                      // ((TrafficLight) light).getTrafficLightColor().toString());
95                      this.simulator.scheduleEventRel(new Time.Rel(0.0, TimeUnit.SECOND), this, light,
96                          "setTrafficLightColor", new Object[]{TrafficLightColor.GREEN});
97                      this.simulator.scheduleEventRel(new Time.Rel(15.0, TimeUnit.SECOND), this, light,
98                          "setTrafficLightColor", new Object[]{TrafficLightColor.YELLOW});
99                      this.simulator.scheduleEventRel(new Time.Rel(24.0, TimeUnit.SECOND), this, light,
100                         "setTrafficLightColor", new Object[]{TrafficLightColor.RED});
101                 }
102                 catch (SimRuntimeException exception)
103                 {
104                     exception.printStackTrace();
105                 }
106             }
107         }
108         if (this.trafficLights.containsKey(4))
109         {
110             for (AbstractTrafficLightNew light : this.trafficLights.get(4))
111             {
112                 try
113                 {
114                     this.simulator.scheduleEventRel(new Time.Rel(0.0, TimeUnit.SECOND), this, light,
115                         "setTrafficLightColor", new Object[]{TrafficLightColor.GREEN});
116                     this.simulator.scheduleEventRel(new Time.Rel(15.0, TimeUnit.SECOND), this, light,
117                         "setTrafficLightColor", new Object[]{TrafficLightColor.YELLOW});
118                     this.simulator.scheduleEventRel(new Time.Rel(24.0, TimeUnit.SECOND), this, light,
119                         "setTrafficLightColor", new Object[]{TrafficLightColor.RED});
120                 }
121                 catch (SimRuntimeException exception)
122                 {
123                     exception.printStackTrace();
124                 }
125             }
126         }
127         if (this.trafficLights.containsKey(6))
128         {
129             for (AbstractTrafficLightNew light : this.trafficLights.get(6))
130             {
131                 try
132                 {
133                     // System.out.println("traffic light 6 at time " + this.simulator.getSimulatorTime());
134 
135                     this.simulator.scheduleEventRel(new Time.Rel(24.0, TimeUnit.SECOND), this, light,
136                         "setTrafficLightColor", new Object[]{TrafficLightColor.GREEN});
137                     this.simulator.scheduleEventRel(new Time.Rel(39.0, TimeUnit.SECOND), this, light,
138                         "setTrafficLightColor", new Object[]{TrafficLightColor.YELLOW});
139                     this.simulator.scheduleEventRel(new Time.Rel(48.0, TimeUnit.SECOND), this, light,
140                         "setTrafficLightColor", new Object[]{TrafficLightColor.RED});
141                 }
142                 catch (SimRuntimeException exception)
143                 {
144                     exception.printStackTrace();
145                 }
146             }
147         }
148         if (this.trafficLights.containsKey(7))
149         {
150             for (AbstractTrafficLightNew light : this.trafficLights.get(7))
151             {
152                 try
153                 {
154                     this.simulator.scheduleEventRel(new Time.Rel(24.0, TimeUnit.SECOND), this, light,
155                         "setTrafficLightColor", new Object[]{TrafficLightColor.GREEN});
156                     this.simulator.scheduleEventRel(new Time.Rel(39.0, TimeUnit.SECOND), this, light,
157                         "setTrafficLightColor", new Object[]{TrafficLightColor.YELLOW});
158                     this.simulator.scheduleEventRel(new Time.Rel(48.0, TimeUnit.SECOND), this, light,
159                         "setTrafficLightColor", new Object[]{TrafficLightColor.RED});
160                 }
161                 catch (SimRuntimeException exception)
162                 {
163                     exception.printStackTrace();
164                 }
165             }
166         }
167         if (this.trafficLights.containsKey(9))
168         {
169             for (AbstractTrafficLightNew light : this.trafficLights.get(9))
170             {
171                 try
172                 {
173                     // System.out.println("traffic light 9 at time " + this.simulator.getSimulatorTime());
174 
175                     this.simulator.scheduleEventRel(new Time.Rel(48.0, TimeUnit.SECOND), this, light,
176                         "setTrafficLightColor", new Object[]{TrafficLightColor.GREEN});
177                     this.simulator.scheduleEventRel(new Time.Rel(63.0, TimeUnit.SECOND), this, light,
178                         "setTrafficLightColor", new Object[]{TrafficLightColor.YELLOW});
179                     this.simulator.scheduleEventRel(new Time.Rel(72.0, TimeUnit.SECOND), this, light,
180                         "setTrafficLightColor", new Object[]{TrafficLightColor.RED});
181                 }
182                 catch (SimRuntimeException exception)
183                 {
184                     exception.printStackTrace();
185                 }
186             }
187         }
188         if (this.trafficLights.containsKey(10))
189         {
190             for (AbstractTrafficLightNew light : this.trafficLights.get(10))
191             {
192                 try
193                 {
194                     this.simulator.scheduleEventRel(new Time.Rel(48.0, TimeUnit.SECOND), this, light,
195                         "setTrafficLightColor", new Object[]{TrafficLightColor.GREEN});
196                     this.simulator.scheduleEventRel(new Time.Rel(63.0, TimeUnit.SECOND), this, light,
197                         "setTrafficLightColor", new Object[]{TrafficLightColor.YELLOW});
198                     this.simulator.scheduleEventRel(new Time.Rel(72.0, TimeUnit.SECOND), this, light,
199                         "setTrafficLightColor", new Object[]{TrafficLightColor.RED});
200                 }
201                 catch (SimRuntimeException exception)
202                 {
203                     exception.printStackTrace();
204                 }
205             }
206         }
207         if (this.trafficLights.containsKey(12))
208         {
209             for (AbstractTrafficLightNew light : this.trafficLights.get(12))
210             {
211                 try
212                 {
213                     // System.out.println("traffic light 12 at time " + this.simulator.getSimulatorTime());
214 
215                     this.simulator.scheduleEventRel(new Time.Rel(72.0, TimeUnit.SECOND), this, light,
216                         "setTrafficLightColor", new Object[]{TrafficLightColor.GREEN});
217                     this.simulator.scheduleEventRel(new Time.Rel(87.0, TimeUnit.SECOND), this, light,
218                         "setTrafficLightColor", new Object[]{TrafficLightColor.YELLOW});
219                     this.simulator.scheduleEventRel(new Time.Rel(96.0, TimeUnit.SECOND), this, light,
220                         "setTrafficLightColor", new Object[]{TrafficLightColor.RED});
221                 }
222                 catch (SimRuntimeException exception)
223                 {
224                     exception.printStackTrace();
225                 }
226             }
227         }
228         if (this.trafficLights.containsKey(13))
229         {
230             for (AbstractTrafficLightNew light : this.trafficLights.get(13))
231             {
232                 try
233                 {
234                     this.simulator.scheduleEventRel(new Time.Rel(72.0, TimeUnit.SECOND), this, light,
235                         "setTrafficLightColor", new Object[]{TrafficLightColor.GREEN});
236                     this.simulator.scheduleEventRel(new Time.Rel(87.0, TimeUnit.SECOND), this, light,
237                         "setTrafficLightColor", new Object[]{TrafficLightColor.YELLOW});
238                     this.simulator.scheduleEventRel(new Time.Rel(96.0, TimeUnit.SECOND), this, light,
239                         "setTrafficLightColor", new Object[]{TrafficLightColor.RED});
240                 }
241                 catch (SimRuntimeException exception)
242                 {
243                     exception.printStackTrace();
244                 }
245             }
246         }
247     }
248 
249     /**
250      * @param sequence2 sequence2
251      * @param trafficLight trafficLight
252      */
253     public void addTrafficLight(int sequence2, AbstractTrafficLightNew trafficLight)
254     {
255         if (!this.trafficLights.containsKey(sequence2))
256         {
257             Set<AbstractTrafficLightNew> trafficLightsSet = new HashSet<AbstractTrafficLightNew>();
258             this.trafficLights.put(sequence2, trafficLightsSet);
259         }
260         this.trafficLights.get(sequence2).add(trafficLight);
261     }
262 
263 }