DestinationSensor.java

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

  2. import org.djunits.value.vdouble.scalar.Length;
  3. import org.djutils.exceptions.Throw;
  4. import org.opentrafficsim.core.compatibility.Compatible;
  5. import org.opentrafficsim.core.gtu.GTUDirectionality;
  6. import org.opentrafficsim.core.gtu.RelativePosition;
  7. import org.opentrafficsim.core.network.NetworkException;
  8. import org.opentrafficsim.core.network.Node;
  9. import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
  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. /**
  15.  * A DestinationSensor is a sensor that deletes a GTU that has the node it will pass after this sensor as its destination.
  16.  * <p>
  17.  * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands.<br>
  18.  * All rights reserved. <br>
  19.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  20.  * <p>
  21.  * $LastChangedDate: 2015-08-12 16:37:45 +0200 (Wed, 12 Aug 2015) $, @version $Revision: 1240 $, by $Author: averbraeck $,
  22.  * initial version an 30, 2015 <br>
  23.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  24.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  25.  */
  26. public class DestinationSensor extends AbstractSensor
  27. {
  28.     /** */
  29.     private static final long serialVersionUID = 20150130L;

  30.     /** the destination node for which this is the DestinationSensor. */
  31.     private final Node destinationNode;

  32.     /**
  33.      * @param lane Lane; the lane that triggers the deletion of the GTU.
  34.      * @param position Length; the position of the sensor
  35.      * @param gtuDirectionality GTUDirectionality; GTU directionality
  36.      * @param simulator DEVSSimulatorInterface.TimeDoubleUnit; the simulator to enable animation.
  37.      * @throws NetworkException when the position on the lane is out of bounds w.r.t. the center line of the lane
  38.      */
  39.     public DestinationSensor(final Lane lane, final Length position, final GTUDirectionality gtuDirectionality,
  40.             final DEVSSimulatorInterface.TimeDoubleUnit simulator) throws NetworkException
  41.     {
  42.         this(lane, position, gtuDirectionality.isPlus() ? Compatible.PLUS : Compatible.MINUS, simulator);
  43.     }

  44.     /**
  45.      * @param lane Lane; the lane that triggers the deletion of the GTU.
  46.      * @param position Length; the position of the sensor
  47.      * @param compatible Compatible; compatible GTU type and direction
  48.      * @param simulator DEVSSimulatorInterface.TimeDoubleUnit; the simulator to enable animation.
  49.      * @throws NetworkException when the position on the lane is out of bounds w.r.t. the center line of the lane
  50.      */
  51.     public DestinationSensor(final Lane lane, final Length position, final Compatible compatible,
  52.             final DEVSSimulatorInterface.TimeDoubleUnit simulator) throws NetworkException
  53.     {
  54.         super("DESTINATION@" + lane.getFullId(), lane, position, RelativePosition.FRONT, simulator,
  55.                 makeGeometry(lane, position, 1.0), compatible);
  56.         this.destinationNode = compatible.equals(PLUS) || compatible.equals(EVERYTHING) ? lane.getParentLink().getEndNode()
  57.                 : lane.getParentLink().getStartNode();
  58.     }

  59.     /** {@inheritDoc} */
  60.     @Override
  61.     public final void triggerResponse(final LaneBasedGTU gtu)
  62.     {
  63.         try
  64.         {
  65.             if (gtu.getStrategicalPlanner().getRoute() == null
  66.                     || gtu.getStrategicalPlanner().getRoute().destinationNode().equals(this.destinationNode))
  67.             {
  68.                 gtu.destroy();
  69.             }
  70.         }
  71.         catch (NetworkException exception)
  72.         {
  73.             getSimulator().getLogger().always().error(exception, "Error destroying GTU: {} at destination sensor: {}", gtu, toString());
  74.         }
  75.     }

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

  82.     /** {@inheritDoc} */
  83.     @Override
  84.     @SuppressWarnings("checkstyle:designforextension")
  85.     public DestinationSensor clone(final CrossSectionElement newCSE, final SimulatorInterface.TimeDoubleUnit newSimulator)
  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 DestinationSensor((Lane) newCSE, getLongitudinalPosition(), getDetectedGTUTypes(),
  92.                 (DEVSSimulatorInterface.TimeDoubleUnit) newSimulator);
  93.     }

  94. }