Class TrafCod
java.lang.Object
org.djutils.event.LocalEventProducer
org.opentrafficsim.trafficcontrol.AbstractTrafficController
org.opentrafficsim.trafficcontrol.trafcod.TrafCod
- All Implemented Interfaces:
Serializable,Remote,EventListener,org.djutils.base.Identifiable,org.djutils.event.EventListener,org.djutils.event.EventProducer,org.opentrafficsim.core.object.NonLocatedObject,ActuatedTrafficController,TrafficController
public class TrafCod
extends AbstractTrafficController
implements ActuatedTrafficController, org.djutils.event.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
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static enumStates of the rule parser.(package private) static enumTypes of TrafCOD tokens. -
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final StringComment starter in TrafCOD.(package private) static final org.djunits.value.vdouble.scalar.DurationThe evaluation interval of TrafCOD.(package private) static final intVersion of the supported TrafCOD files.Fields inherited from interface org.djutils.event.EventProducer
FIRST_POSITION, LAST_POSITIONFields inherited from interface org.opentrafficsim.trafficcontrol.TrafficController
BEING_CLONED, NO_STREAM, OFF, RUNNING, SHUTTING_DOWN, STARTING_UP, TRAFFIC_LIGHT_CHANGED, TRAFFICCONTROL_CONFLICT_GROUP_CHANGED, TRAFFICCONTROL_CONTROLLER_CREATED, TRAFFICCONTROL_CONTROLLER_EVALUATING, TRAFFICCONTROL_CONTROLLER_WARNING, TRAFFICCONTROL_SET_TRACING, TRAFFICCONTROL_STATE_CHANGED, TRAFFICCONTROL_TRACED_VARIABLE_UPDATED, TRAFFICCONTROL_VARIABLE_CREATED -
Constructor Summary
ConstructorsConstructorDescriptionTrafCod(String controllerName, URL trafCodURL, org.opentrafficsim.core.dsol.OtsSimulatorInterface simulator, Container display, BufferedImage displayBackground, List<String> displayObjectLocations) Construct a new TrafCOD traffic light controller.TrafCod(String controllerName, List<String> trafCODRules, org.opentrafficsim.core.dsol.OtsSimulatorInterface simulator, BufferedImage displayBackground, List<String> displayObjectLocations) Construct a new TrafCOD traffic light controller. -
Method Summary
Modifier and TypeMethodDescriptionvoidCheck the consistency of the traffic control program and perform initializations that require a completely built network.(package private) voidfireTrafCODEvent(org.djutils.event.EventType eventType, Object[] payload) Fire an event on behalf of this TrafCOD engine (used for tracing variable changes).Retrieve the Swing (for now) container in which the controller displays its current state.org.opentrafficsim.core.dsol.OtsSimulatorInterfaceRetrieve the simulator.intRetrieve the structure number.loadTextFromURL(URL url) Read a text from a URL and convert it to a list of strings.voidnotify(org.djutils.event.Event event) (package private) static StringPrint a tokenized rule.toString()voidtraceVariable(String variableName, int stream, boolean trace) Switch tracing of one variable on or off.voidtraceVariablesOfStream(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.voidupdateDetector(String detectorId, boolean detectingGTU) Tell the traffic controller that the state of a detector has changed.Methods inherited from class org.opentrafficsim.trafficcontrol.AbstractTrafficController
getIdMethods inherited from class org.djutils.event.LocalEventProducer
addListener, addListener, addListener, addListener, fireEvent, fireEvent, fireEvent, fireTimedEvent, fireTimedEvent, fireTimedEvent, fireUnverifiedEvent, fireUnverifiedEvent, fireUnverifiedTimedEvent, fireUnverifiedTimedEvent, getEventListenerMap, getEventTypesWithListeners, getListenerReferences, hasListeners, numberOfListeners, removeAllListeners, removeAllListeners, removeListenerMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.djutils.event.EventProducer
addListener, addListener, addListener, addListener, fireEvent, fireEvent, fireEvent, fireTimedEvent, fireTimedEvent, fireTimedEvent, fireUnverifiedEvent, fireUnverifiedEvent, fireUnverifiedTimedEvent, fireUnverifiedTimedEvent, getEventListenerMap, getEventTypesWithListeners, getListenerReferences, hasListeners, numberOfListeners, removeAllListeners, removeAllListeners, removeListenerMethods inherited from interface org.opentrafficsim.trafficcontrol.TrafficController
getId
-
Field Details
-
TrafCod_VERSION
static final int TrafCod_VERSIONVersion of the supported TrafCOD files.- See Also:
-
EVALUATION_INTERVAL
static final org.djunits.value.vdouble.scalar.Duration EVALUATION_INTERVALThe evaluation interval of TrafCOD. -
COMMENT_PREFIX
Comment starter in TrafCOD.- See Also:
-
-
Constructor Details
-
TrafCod
public TrafCod(String controllerName, URL trafCodURL, org.opentrafficsim.core.dsol.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 controllertrafCodURL- the URL of the TrafCOD rulessimulator- the simulation enginedisplay- if non-null, a controller display is constructed and shown in the supplied containerdisplayBackground- background for controller display imagedisplayObjectLocations- list of sensors and traffic lights and their locations on thedisplayBackGround- Throws:
TrafficControlException- when a rule cannot be parsednl.tudelft.simulation.dsol.SimRuntimeException- when scheduling the first evaluation event failsIOException- when loading the TrafCOD rules from the URL fails
-
TrafCod
public TrafCod(String controllerName, List<String> trafCODRules, org.opentrafficsim.core.dsol.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 controllertrafCODRules- the TrafCOD rulessimulator- the simulation enginedisplayBackground- background for controller display imagedisplayObjectLocations- list of sensors and traffic lights and their locations on thedisplayBackGround- Throws:
TrafficControlException- when a rule cannot be parsednl.tudelft.simulation.dsol.SimRuntimeException- when scheduling the first evaluation event fails
-
-
Method Details
-
loadTextFromURL
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, TrafficControlExceptionCheck 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 OtsModelInterfaceTrafficControlException- when a required traffic light or sensor is not present in the network
-
printRule
Print a tokenized rule.- Parameters:
tokens- the tokensprintValues- if true; print the values of all encountered variable; if false; do not print the values of all encountered variable- Returns:
- a textual approximation of the original rule
- Throws:
TrafficControlException- when tokens does not match the expected grammar
-
getSimulator
public org.opentrafficsim.core.dsol.OtsSimulatorInterface getSimulator()Retrieve the simulator.- Returns:
- SimulatorInterface<Time, Duration, SimTimeDoubleUnit>
-
getStructureNumber
public int getStructureNumber()Retrieve the structure number.- Returns:
- the structureNumber
-
updateDetector
Description copied from interface:ActuatedTrafficControllerTell the traffic controller that the state of a detector has changed.- Specified by:
updateDetectorin interfaceActuatedTrafficController- Parameters:
detectorId- id of the detector
-
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, orTrafCOD.NO_STREAMto affect all variables that do not have an associated traffic streamtrace- if true; switch on tracing; if false; switch off tracing
-
traceVariable
Switch tracing of one variable on or off.- Parameters:
variableName- name of the variablestream- traffic stream of the variable, orTrafCOD.NO_STREAMto select a variable that does not have an associated traffic streamtrace- if true; switch on tracing; if false; switch off tracing
-
notify
- Specified by:
notifyin interfaceorg.djutils.event.EventListener- Throws:
RemoteException
-
fireTrafCODEvent
Fire an event on behalf of this TrafCOD engine (used for tracing variable changes).- Parameters:
eventType- the type of the eventpayload- the payload of the event
-
getFullId
- Specified by:
getFullIdin interfaceorg.opentrafficsim.core.object.NonLocatedObject
-
getDisplayContainer
Description copied from interface:ActuatedTrafficControllerRetrieve the Swing (for now) container in which the controller displays its current state.- Specified by:
getDisplayContainerin interfaceActuatedTrafficController- Returns:
- the display of the current state; may return null!
-
toString
-