AbstractTrafficLightController.java

  1. package org.opentrafficsim.road.network.lane.object.trafficlight;

  2. import java.util.Set;
  3. import java.util.SortedMap;
  4. import java.util.TreeMap;

  5. import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;

  6. /**
  7.  * Standard fields and methods for traffic light controllers.
  8.  * <p>
  9.  * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  10.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
  11.  * </p>
  12.  * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  13.  * initial version Oct 4, 2016 <br>
  14.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  15.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  16.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  17.  */
  18. public abstract class AbstractTrafficLightController implements TrafficLightController
  19. {
  20.     /** the controller id. */
  21.     private final String id;

  22.     /** the simulator. */
  23.     @SuppressWarnings("checkstyle:visibilitymodifier")
  24.     protected final DEVSSimulatorInterface.TimeDoubleUnit simulator;

  25.     /** the phases with a number identifying them to create a sorted map. */
  26.     private final SortedMap<Integer, Set<TrafficLight>> phases = new TreeMap<>();

  27.     /** the current phase. */
  28.     @SuppressWarnings("checkstyle:visibilitymodifier")
  29.     protected int currentPhase;

  30.     /** the current light in the current phase. */
  31.     @SuppressWarnings("checkstyle:visibilitymodifier")
  32.     protected TrafficLightColor currentColor = TrafficLightColor.RED;

  33.     /**
  34.      * Create a fixed time controller.
  35.      * @param id String; the controller id
  36.      * @param simulator DEVSSimulatorInterface.TimeDoubleUnit; the simulator
  37.      */
  38.     public AbstractTrafficLightController(final String id, final DEVSSimulatorInterface.TimeDoubleUnit simulator)
  39.     {
  40.         this.id = id;
  41.         this.simulator = simulator;
  42.     }

  43.     /** {@inheritDoc} */
  44.     @Override
  45.     public final int getNumberOfPhases()
  46.     {
  47.         return this.phases.size();
  48.     }

  49.     /** {@inheritDoc} */
  50.     @Override
  51.     public final int getCurrentPhase()
  52.     {
  53.         return this.currentPhase;
  54.     }

  55.     /** {@inheritDoc} */
  56.     @Override
  57.     public final void addTrafficLightToPhase(final int phaseId, final TrafficLight trafficLight) throws TrafficLightException
  58.     {
  59.         if (!this.phases.containsKey(phaseId))
  60.         {
  61.             throw new TrafficLightException("Phase " + phaseId + " for traffic light " + this.id + " was not defined");
  62.         }
  63.         this.phases.get(phaseId).add(trafficLight);
  64.         trafficLight.setTrafficLightColor(TrafficLightColor.RED);
  65.     }

  66.     /** {@inheritDoc} */
  67.     @Override
  68.     public final String getId()
  69.     {
  70.         return this.id;
  71.     }

  72. }