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
19
20
21
22
23
24
25
26
27
28
29 public class DestinationSensor extends AbstractSensor
30 {
31
32 private static final long serialVersionUID = 20150130L;
33
34
35 private final Node destinationNode;
36
37
38
39
40
41
42
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
52
53
54
55
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
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
85 @Override
86 public final String toString()
87 {
88 return "DestinationSensor [Lane=" + this.getLane() + "]";
89 }
90
91
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 }