All Implemented Interfaces:
EventListener, Identifiable, EventListener, EventProducer, NonLocatedObject, ActuatedTrafficController, TrafficController

public class TrafCod extends AbstractTrafficController implements ActuatedTrafficController, EventListener
TrafCOD evaluator. TrafCOD is a language for writing traffic control programs. A TrafCOD program consists of a set of rules that must be evaluated repeatedly (until no more changes occurr) every time step. The time step size is 0.1 seconds.

Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See OpenTrafficSim License.

Author:
Wouter Schakel
  • Constructor Details

    • TrafCod

      public TrafCod(String controllerName, URL trafCodURL, OtsSimulatorInterface simulator, Container display, BufferedImage displayBackground, List<String> displayObjectLocations) throws TrafficControlException, nl.tudelft.simulation.dsol.SimRuntimeException, IOException
      Construct a new TrafCOD traffic light controller.
      Parameters:
      controllerName - name of this TrafCOD traffic light controller
      trafCodURL - the URL of the TrafCOD rules
      simulator - the simulation engine
      display - if non-null, a controller display is constructed and shown in the supplied container
      displayBackground - background for controller display image
      displayObjectLocations - list of sensors and traffic lights and their locations on the displayBackGround
      Throws:
      TrafficControlException - when a rule cannot be parsed
      nl.tudelft.simulation.dsol.SimRuntimeException - when scheduling the first evaluation event fails
      IOException - when loading the TrafCOD rules from the URL fails
    • TrafCod

      public TrafCod(String controllerName, List<String> trafCODRules, OtsSimulatorInterface simulator, BufferedImage displayBackground, List<String> displayObjectLocations) throws TrafficControlException, nl.tudelft.simulation.dsol.SimRuntimeException
      Construct a new TrafCOD traffic light controller.
      Parameters:
      controllerName - name of this TrafCOD traffic light controller
      trafCODRules - the TrafCOD rules
      simulator - the simulation engine
      displayBackground - background for controller display image
      displayObjectLocations - list of sensors and traffic lights and their locations on the displayBackGround
      Throws:
      TrafficControlException - when a rule cannot be parsed
      nl.tudelft.simulation.dsol.SimRuntimeException - when scheduling the first evaluation event fails
  • Method Details

    • loadTextFromURL

      public static List<String> loadTextFromURL(URL url) throws IOException
      Read a text from a URL and convert it to a list of strings.
      Parameters:
      url - the URL to open and read
      Returns:
      the lines read from the URL (trimmed).
      Throws:
      IOException - when opening or reading the URL failed.
    • checkConsistency

      public void checkConsistency() throws nl.tudelft.simulation.dsol.SimRuntimeException, TrafficControlException
      Check the consistency of the traffic control program and perform initializations that require a completely built network.
      Throws:
      nl.tudelft.simulation.dsol.SimRuntimeException - when the simulation model is not an OtsModelInterface
      TrafficControlException - when a required traffic light or sensor is not present in the network
    • getSimulator

      public OtsSimulatorInterface getSimulator()
      Retrieve the simulator.
      Returns:
      SimulatorInterface<Time, Duration, SimTimeDoubleUnit>
    • getStructureNumber

      public int getStructureNumber()
      Retrieve the structure number.
      Returns:
      the structureNumber
    • updateDetector

      public void updateDetector(String detectorId, boolean detectingGTU)
      Description copied from interface: ActuatedTrafficController
      Tell the traffic controller that the state of a detector has changed.
      Specified by:
      updateDetector in interface ActuatedTrafficController
      Parameters:
      detectorId - id of the detector
      detectingGTU - detecting GTU
    • traceVariablesOfStream

      public void traceVariablesOfStream(int stream, boolean trace)
      Switch tracing of all variables of a particular traffic stream, or all variables that do not have an associated traffic stream on or off.
      Parameters:
      stream - the traffic stream number, or TrafCOD.NO_STREAM to affect all variables that do not have an associated traffic stream
      trace - if true; switch on tracing; if false; switch off tracing
    • traceVariable

      public void traceVariable(String variableName, int stream, boolean trace)
      Switch tracing of one variable on or off.
      Parameters:
      variableName - name of the variable
      stream - traffic stream of the variable, or TrafCOD.NO_STREAM to select a variable that does not have an associated traffic stream
      trace - if true; switch on tracing; if false; switch off tracing
    • notify

      public void notify(Event event)
      Specified by:
      notify in interface EventListener
    • getFullId

      public String getFullId()
      Specified by:
      getFullId in interface NonLocatedObject
    • getDisplayContainer

      public Optional<Container> getDisplayContainer()
      Description copied from interface: ActuatedTrafficController
      Retrieve the Swing (for now) container in which the controller displays its current state.
      Specified by:
      getDisplayContainer in interface ActuatedTrafficController
      Returns:
      the display of the current state; may return empty
    • toString

      public String toString()
      Overrides:
      toString in class Object