View Javadoc
1   package org.opentrafficsim.road.network.control.rampmetering;
2   
3   import org.opentrafficsim.base.OtsRuntimeException;
4   import org.opentrafficsim.base.logger.Logger;
5   import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
6   
7   import nl.tudelft.simulation.dsol.SimRuntimeException;
8   
9   /**
10   * Ramp metering. This consist of a {@code RampMeteringSwitch} and a {@code RampMeteringLightController}.
11   * <p>
12   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
14   * </p>
15   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
17   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
18   */
19  public class RampMetering
20  {
21  
22      /** Simulator. */
23      private final OtsSimulatorInterface simulator;
24  
25      /** Ramp metering switch. */
26      private final RampMeteringSwitch rampSwitch;
27  
28      /** Ramp metering light controller. */
29      private final RampMeteringLightController rampLightController;
30  
31      /**
32       * Constructor.
33       * @param simulator simulator
34       * @param rampSwitch ramp metering switch
35       * @param rampLightController ramp metering light controller
36       */
37      public RampMetering(final OtsSimulatorInterface simulator, final RampMeteringSwitch rampSwitch,
38              final RampMeteringLightController rampLightController)
39      {
40          this.simulator = simulator;
41          this.rampSwitch = rampSwitch;
42          this.rampLightController = rampLightController;
43          control();
44      }
45  
46      /**
47       * Executes control in a cyclic manner.
48       */
49      private void control()
50      {
51          if (this.rampSwitch.isEnabled())
52          {
53              Logger.ots().info("Ramp-metering enabled.");
54              this.rampLightController.enable(this.rampSwitch.getCycleTime());
55          }
56          else
57          {
58              Logger.ots().info("Ramp-metering disabled.");
59              this.rampLightController.disable();
60          }
61          try
62          {
63              this.simulator.scheduleEventRel(this.rampSwitch.getInterval(), () -> control());
64          }
65          catch (SimRuntimeException exception)
66          {
67              throw new OtsRuntimeException("Interval from ramp metering switch is not a valid positive duration.", exception);
68          }
69      }
70  
71  }