View Javadoc
1   package org.opentrafficsim.road.network.lane.object.sensor;
2   
3   import org.djunits.value.vdouble.scalar.Length;
4   import org.djutils.exceptions.Throw;
5   import org.opentrafficsim.core.compatibility.Compatible;
6   import org.opentrafficsim.core.gtu.GTUDirectionality;
7   import org.opentrafficsim.core.gtu.RelativePosition;
8   import org.opentrafficsim.core.network.NetworkException;
9   import org.opentrafficsim.core.network.Node;
10  import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
11  import org.opentrafficsim.road.network.lane.CrossSectionElement;
12  import org.opentrafficsim.road.network.lane.Lane;
13  
14  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
15  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
16  
17  /**
18   * A DestinationSensor is a sensor that deletes a GTU that has the node it will pass after this sensor as its destination.
19   * <p>
20   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands.<br>
21   * All rights reserved. <br>
22   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
23   * <p>
24   * $LastChangedDate: 2015-08-12 16:37:45 +0200 (Wed, 12 Aug 2015) $, @version $Revision: 1240 $, by $Author: averbraeck $,
25   * initial version an 30, 2015 <br>
26   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
27   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
28   */
29  public class DestinationSensor extends AbstractSensor
30  {
31      /** */
32      private static final long serialVersionUID = 20150130L;
33  
34      /** the destination node for which this is the DestinationSensor. */
35      private final Node destinationNode;
36  
37      /**
38       * @param lane Lane; the lane that triggers the deletion of the GTU.
39       * @param position Length; the position of the sensor
40       * @param gtuDirectionality GTUDirectionality; GTU directionality
41       * @param simulator DEVSSimulatorInterface.TimeDoubleUnit; the simulator to enable animation.
42       * @throws NetworkException when the position on the lane is out of bounds w.r.t. the center line of the lane
43       */
44      public DestinationSensor(final Lane lane, final Length position, final GTUDirectionality gtuDirectionality,
45              final DEVSSimulatorInterface.TimeDoubleUnit simulator) throws NetworkException
46      {
47          this(lane, position, gtuDirectionality.isPlus() ? Compatible.PLUS : Compatible.MINUS, simulator);
48      }
49  
50      /**
51       * @param lane Lane; the lane that triggers the deletion of the GTU.
52       * @param position Length; the position of the sensor
53       * @param compatible Compatible; compatible GTU type and direction
54       * @param simulator DEVSSimulatorInterface.TimeDoubleUnit; 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 DestinationSensor(final Lane lane, final Length position, final Compatible compatible,
58              final DEVSSimulatorInterface.TimeDoubleUnit simulator) throws NetworkException
59      {
60          super("DESTINATION@" + lane.getFullId(), lane, position, RelativePosition.FRONT, simulator,
61                  makeGeometry(lane, position, 1.0), compatible);
62          this.destinationNode = compatible.equals(PLUS) || compatible.equals(EVERYTHING) ? lane.getParentLink().getEndNode()
63                  : lane.getParentLink().getStartNode();
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public final void triggerResponse(final LaneBasedGTU gtu)
69      {
70          try
71          {
72              if (gtu.getStrategicalPlanner().getRoute() == null
73                      || gtu.getStrategicalPlanner().getRoute().destinationNode().equals(this.destinationNode))
74              {
75                  gtu.destroy();
76              }
77          }
78          catch (NetworkException exception)
79          {
80              getSimulator().getLogger().always().error(exception, "Error destroying GTU: {} at destination sensor: {}", gtu, toString());
81          }
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public final String toString()
87      {
88          return "DestinationSensor [Lane=" + this.getLane() + "]";
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      @SuppressWarnings("checkstyle:designforextension")
94      public DestinationSensor clone(final CrossSectionElement newCSE, final SimulatorInterface.TimeDoubleUnit newSimulator)
95              throws NetworkException
96      {
97          Throw.when(!(newCSE instanceof Lane), NetworkException.class, "sensors can only be cloned for Lanes");
98          Throw.when(!(newSimulator instanceof DEVSSimulatorInterface.TimeDoubleUnit), NetworkException.class,
99                  "simulator should be a DEVSSimulator");
100         return new DestinationSensor((Lane) newCSE, getLongitudinalPosition(), getDetectedGTUTypes(),
101                 (DEVSSimulatorInterface.TimeDoubleUnit) newSimulator);
102     }
103 
104 }