SinkSensor.java

  1. package org.opentrafficsim.road.network.lane.object.sensor;

  2. import java.rmi.RemoteException;

  3. import javax.naming.NamingException;

  4. import org.djunits.value.vdouble.scalar.Length;
  5. import org.opentrafficsim.core.compatibility.Compatible;
  6. import org.opentrafficsim.core.gtu.RelativePosition;
  7. import org.opentrafficsim.core.network.NetworkException;
  8. import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
  9. import org.opentrafficsim.road.network.animation.SinkAnimation;
  10. import org.opentrafficsim.road.network.lane.CrossSectionElement;
  11. import org.opentrafficsim.road.network.lane.Lane;

  12. import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
  13. import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
  14. import nl.tudelft.simulation.language.Throw;

  15. /**
  16.  * A SinkSensor is a sensor that deletes every GTU that hits it.
  17.  * <p>
  18.  * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands.<br>
  19.  * All rights reserved. <br>
  20.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  21.  * <p>
  22.  * $LastChangedDate: 2015-08-12 16:37:45 +0200 (Wed, 12 Aug 2015) $, @version $Revision: 1240 $, by $Author: averbraeck $,
  23.  * initial version an 30, 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 SinkSensor extends AbstractSensor
  28. {
  29.     /** */
  30.     private static final long serialVersionUID = 20150130L;

  31.     /**
  32.      * @param lane the lane that triggers the deletion of the GTU.
  33.      * @param position the position of the sensor
  34.      * @param simulator the simulator to enable animation.
  35.      * @throws NetworkException when the position on the lane is out of bounds w.r.t. the center line of the lane
  36.      */
  37.     public SinkSensor(final Lane lane, final Length position, final DEVSSimulatorInterface.TimeDoubleUnit simulator) throws NetworkException
  38.     {
  39.         super("SINK@" + lane.toString(), lane, position, RelativePosition.FRONT, simulator, Compatible.EVERYTHING);
  40.         try
  41.         {
  42.             new SinkAnimation(this, simulator);
  43.         }
  44.         catch (RemoteException | NamingException exception)
  45.         {
  46.             exception.printStackTrace();
  47.         }
  48.     }

  49.     /**
  50.      * @param dummy1 dummy
  51.      * @param lane the lane that triggers the deletion of the GTU.
  52.      * @param position the position of the sensor
  53.      * @param dummy2 dummy
  54.      * @param simulator the simulator to enable animation.
  55.      * @throws NetworkException when the position on the lane is out of bounds w.r.t. the center line of the lane
  56.      */
  57.     public SinkSensor(final String dummy1, final Lane lane, final Length position, final RelativePosition.TYPE dummy2,
  58.             final DEVSSimulatorInterface.TimeDoubleUnit simulator) throws NetworkException
  59.     {
  60.         super("SINK@" + lane.toString(), lane, position, RelativePosition.FRONT, simulator, Compatible.EVERYTHING);
  61.         try
  62.         {
  63.             new SinkAnimation(this, simulator);
  64.         }
  65.         catch (RemoteException | NamingException exception)
  66.         {
  67.             exception.printStackTrace();
  68.         }
  69.     }

  70.     /** {@inheritDoc} */
  71.     @Override
  72.     public final void triggerResponse(final LaneBasedGTU gtu)
  73.     {
  74.         gtu.destroy();
  75.     }

  76.     /** {@inheritDoc} */
  77.     @Override
  78.     public final String toString()
  79.     {
  80.         return "SinkSensor [Lane=" + this.getLane() + "]";
  81.     }

  82.     /** {@inheritDoc} */
  83.     @Override
  84.     @SuppressWarnings("checkstyle:designforextension")
  85.     public SinkSensor clone(final CrossSectionElement newCSE, final SimulatorInterface.TimeDoubleUnit newSimulator, final boolean animation)
  86.             throws NetworkException
  87.     {
  88.         Throw.when(!(newCSE instanceof Lane), NetworkException.class, "sensors can only be cloned for Lanes");
  89.         Throw.when(!(newSimulator instanceof DEVSSimulatorInterface.TimeDoubleUnit), NetworkException.class,
  90.                 "simulator should be a DEVSSimulator");
  91.         return new SinkSensor((Lane) newCSE, getLongitudinalPosition(), (DEVSSimulatorInterface.TimeDoubleUnit) newSimulator);
  92.     }

  93. }