View Javadoc
1   package org.opentrafficsim.road.network.lane.object.trafficlight;
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.RelativePosition.TYPE;
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  import org.opentrafficsim.road.network.lane.object.sensor.AbstractSensor;
12  import org.opentrafficsim.road.network.lane.object.sensor.TrafficLightSensor;
13  
14  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
15  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
16  
17  /**
18   * Embedded sensors used by a TrafficLightSensor.
19   * <p>
20   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
21   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
22   * <p>
23   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Feb 28, 2019 <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   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
27   */
28  public class FlankSensor extends AbstractSensor
29  {
30      /** */
31      private static final long serialVersionUID = 20161104L;
32  
33      /** The parent that must be informed of all flanks. */
34      private final TrafficLightSensor parent;
35  
36      /**
37       * Construct a new FlankSensor.
38       * @param id String; the name of the new FlankSensor
39       * @param lane Lane; the lane of the new FlankSensor
40       * @param longitudinalPosition Length; the longitudinal position of the new FlankSensor
41       * @param positionType TYPE; the position on the GTUs that triggers the new FlankSensor
42       * @param simulator DEVSSimulatorInterface.TimeDoubleUnit; the simulator engine
43       * @param parent TrafficLightSensor; the traffic light sensor that deploys this FlankSensor
44       * @param compatible Compatible; object that determines if a GTU is detectable by the new FlankSensor
45       * @throws NetworkException when the network is inconsistent
46       */
47      public FlankSensor(final String id, final Lane lane, final Length longitudinalPosition, final TYPE positionType,
48              final DEVSSimulatorInterface.TimeDoubleUnit simulator, final TrafficLightSensor parent, final Compatible compatible)
49              throws NetworkException
50      {
51          super(id, lane, longitudinalPosition, positionType, simulator, compatible);
52          this.parent = parent;
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      protected final void triggerResponse(final LaneBasedGTU gtu)
58      {
59          this.parent.signalDetection(this, gtu);
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      public final FlankSensor clone(final CrossSectionElement newCSE, final SimulatorInterface.TimeDoubleUnit newSimulator)
65              throws NetworkException
66      {
67          Throw.when(!(newCSE instanceof Lane), NetworkException.class, "sensors can only be cloned for Lanes");
68          Throw.when(!(newSimulator instanceof DEVSSimulatorInterface.TimeDoubleUnit), NetworkException.class,
69                  "simulator should be a DEVSSimulator");
70          // XXX should the parent of the clone be our parent??? And should the (cloned) parent not construct its own flank
71          // sensors?
72          return new FlankSensor(getId(), (Lane) newCSE, getLongitudinalPosition(), getPositionType(),
73                  (DEVSSimulatorInterface.TimeDoubleUnit) newSimulator, this.parent, super.getDetectedGTUTypes());
74      }
75  
76      /**
77       * Return the parent (TrafficLightSensor) of this FlankSensor.
78       * @return TrafficLightSensor; the parent of this flank sensor
79       */
80      public final TrafficLightSensor getParent()
81      {
82          return this.parent;
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public final String toString()
88      {
89          return "FlankSensor [parent=" + this.parent.getId() + "]";
90      }
91  
92  }