View Javadoc
1   package org.opentrafficsim.road.gtu.lane;
2   
3   import java.rmi.RemoteException;
4   
5   import javax.naming.NamingException;
6   
7   import nl.tudelft.simulation.dsol.SimRuntimeException;
8   
9   import org.opentrafficsim.core.dsol.OTSAnimatorInterface;
10  import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
11  import org.opentrafficsim.core.gtu.GTUException;
12  import org.opentrafficsim.core.network.NetworkException;
13  import org.opentrafficsim.road.gtu.animation.DefaultBlockOnOffAnimation;
14  import org.opentrafficsim.road.network.lane.Lane;
15  
16  /**
17   * Special GTU that cannot move, but it can be seen by other GTUs.
18   * <p>
19   * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
20   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
21   * <p>
22   * @version $Revision: 1155 $, $LastChangedDate: 2015-07-26 01:01:13 +0200 (Sun, 26 Jul 2015) $, by $Author: averbraeck $,
23   *          initial version 15 jul. 2015 <br>
24   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
25   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
26   */
27  public class LaneBlockOnOff extends AbstractTrafficLight
28  {
29      /** */
30      private static final long serialVersionUID = 20150624L;
31  
32      /**
33       * @param name the name of the OnOffTrafficLight
34       * @param lane The lane where the block has to be put
35       * @param position the position on the lane as a length
36       * @param simulator the simulator to avoid NullPointerExceptions
37       * @throws GTUException when GTU cannot be created.
38       * @throws NamingException if an error occurs when adding the animation handler
39       * @throws NetworkException when the GTU cannot be placed on the given lane
40       */
41      public LaneBlockOnOff(final String name, final Lane lane, final Length.Rel position,
42          final OTSDEVSSimulatorInterface simulator) throws GTUException, NetworkException, NamingException
43      {
44          super(name, lane, position, simulator);
45  
46          try
47          {
48              new DefaultBlockOnOffAnimation(this, getSimulator());
49              // animation
50              if (simulator instanceof OTSAnimatorInterface)
51              {
52                  // TODO
53              }
54              getSimulator().scheduleEventRel(new Time.Rel(60.0, SECOND), this, this, "changeColorTime", null);
55          }
56          catch (RemoteException | SimRuntimeException exception)
57          {
58              exception.printStackTrace();
59          }
60      }
61  
62      protected void changeColorTime()
63      {
64          setBlocked(!isBlocked());
65  
66          try
67          {
68              getSimulator().scheduleEventRel(new Time.Rel(60.0, SECOND), this, this, "changeColorTime", null);
69          }
70          catch (SimRuntimeException exception)
71          {
72              exception.printStackTrace();
73          }
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      public String toString()
79      {
80          return "LaneBlockOnOff [lane=" + this.lane + ", position=" + this.position + "]";
81      }
82  
83  }