View Javadoc
1   package org.opentrafficsim.road.network.lane.object.trafficlight;
2   
3   import java.util.Set;
4   import java.util.SortedMap;
5   import java.util.TreeMap;
6   
7   import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
8   
9   /**
10   * Standard fields and methods for traffic light controllers.
11   * <p>
12   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
14   * </p>
15   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
16   * initial version Oct 4, 2016 <br>
17   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
18   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
19   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
20   */
21  public abstract class AbstractTrafficLightController implements TrafficLightController
22  {
23      /** the controller id. */
24      private final String id;
25  
26      /** the simulator. */
27      @SuppressWarnings("checkstyle:visibilitymodifier")
28      protected final DEVSSimulatorInterface.TimeDoubleUnit simulator;
29  
30      /** the phases with a number identifying them to create a sorted map. */
31      private final SortedMap<Integer, Set<TrafficLight>> phases = new TreeMap<>();
32  
33      /** the current phase. */
34      @SuppressWarnings("checkstyle:visibilitymodifier")
35      protected int currentPhase;
36  
37      /** the current light in the current phase. */
38      @SuppressWarnings("checkstyle:visibilitymodifier")
39      protected TrafficLightColor currentColor = TrafficLightColor.RED;
40  
41      /**
42       * Create a fixed time controller.
43       * @param id String; the controller id
44       * @param simulator DEVSSimulatorInterface.TimeDoubleUnit; the simulator
45       */
46      public AbstractTrafficLightController(final String id, final DEVSSimulatorInterface.TimeDoubleUnit simulator)
47      {
48          this.id = id;
49          this.simulator = simulator;
50      }
51  
52      /** {@inheritDoc} */
53      @Override
54      public final int getNumberOfPhases()
55      {
56          return this.phases.size();
57      }
58  
59      /** {@inheritDoc} */
60      @Override
61      public final int getCurrentPhase()
62      {
63          return this.currentPhase;
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public final void addTrafficLightToPhase(final int phaseId, final TrafficLight trafficLight) throws TrafficLightException
69      {
70          if (!this.phases.containsKey(phaseId))
71          {
72              throw new TrafficLightException("Phase " + phaseId + " for traffic light " + this.id + " was not defined");
73          }
74          this.phases.get(phaseId).add(trafficLight);
75          trafficLight.setTrafficLightColor(TrafficLightColor.RED);
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public final String getId()
81      {
82          return this.id;
83      }
84  
85  }