SimpleReportingSensor.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.dsol.OTSSimulatorInterface;
  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.lane.CrossSectionElement;
  10. import org.opentrafficsim.road.network.lane.Lane;

  11. /**
  12.  * Sensor that prints which GTU triggers it.
  13.  * <p>
  14.  * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands.<br>
  15.  * All rights reserved. <br>
  16.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  17.  * <p>
  18.  * $LastChangedDate: 2015-08-12 16:37:45 +0200 (Wed, 12 Aug 2015) $, @version $Revision: 1240 $, by $Author: averbraeck $,
  19.  * initial version an 30, 2015 <br>
  20.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  21.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  22.  */
  23. public class SimpleReportingSensor extends AbstractSensor
  24. {
  25.     /** */
  26.     private static final long serialVersionUID = 20150130L;

  27.     /**
  28.      * Construct a new SimpleReportingSensor.
  29.      * @param lane Lane; the lane on which the new SimpleReportingSensor will be located
  30.      * @param position Length; the position of the sensor along the lane
  31.      * @param triggerPosition RelativePosition.TYPE; the relative position type (e.g., FRONT, BACK) of the vehicle that triggers
  32.      *            the sensor
  33.      * @param id String; the id of the new SimpleReportingSensor
  34.      * @param simulator OTSSimulatorInterface; the simulator to enable animation
  35.      * @param compatible Compatible; object that can decide if a particular GTU type in a particular driving direction will
  36.      *            trigger the new SimpleReportingSensor
  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 SimpleReportingSensor(final String id, final Lane lane, final Length position,
  40.             final RelativePosition.TYPE triggerPosition, final OTSSimulatorInterface simulator,
  41.             final Compatible compatible) throws NetworkException
  42.     {
  43.         super(id, lane, position, triggerPosition, simulator, compatible);
  44.     }

  45.     /** {@inheritDoc} */
  46.     @Override
  47.     public final void triggerResponse(final LaneBasedGTU gtu)
  48.     {
  49.         System.out.println(this + " triggered by " + getPositionType().getName() + " of " + gtu);
  50.     }

  51.     /** {@inheritDoc} */
  52.     @Override
  53.     @SuppressWarnings("checkstyle:designforextension")
  54.     public SimpleReportingSensor clone(final CrossSectionElement newCSE, final OTSSimulatorInterface newSimulator)
  55.             throws NetworkException
  56.     {
  57.         Throw.when(!(newCSE instanceof Lane), NetworkException.class, "sensors can only be cloned for Lanes");
  58.         Throw.when(!(newSimulator instanceof OTSSimulatorInterface), NetworkException.class,
  59.                 "simulator should be a DEVSSimulator");
  60.         return new SimpleReportingSensor(getId(), (Lane) newCSE, getLongitudinalPosition(), getPositionType(),
  61.                 (OTSSimulatorInterface) newSimulator, getDetectedGTUTypes());

  62.         // the sensor creates its own animation (for now)
  63.     }

  64.     /** {@inheritDoc} */
  65.     @Override
  66.     public final String toString()
  67.     {
  68.         return "SimpleReportingSensor []";
  69.     }

  70. }