Class AbstractAutomaton
- All Implemented Interfaces:
Runnable
- Direct Known Subclasses:
SampleAutomaton,SampleAutomaton2,SampleAutomaton3
Each individual automaton object runs in a separate thread, so they can operate independently. This class handles thread creation and scheduling, and provides a number of services for the user code.
Subclasses provide a "handle()" function, which does the needed work, and optionally a "init()" function. These can use any JMRI resources for input and output. It should not spin on a condition without explicit wait requests; it is more efficient to use the explicit wait services when waiting for some specific condition.
handle() is executed repeatedly until either the Automate object's stop() method is called, or handle() returns "false". Returning "true" will just cause handle() to be invoked again, so you can cleanly restart the Automaton's handle processing by returning from multiple points in the function.
Since handle() executes outside the GUI thread, it is important that access to GUI (AWT, Swing) objects be scheduled through the various service routines.
Services are provided by public member functions, described below. They must only be invoked from within the init() and handle() methods, as they must be used in a delayable thread. If invoked from the GUI thread, for example, the program will appear to hang. To help ensure this, a warning will be logged if they are used outside the proper thread.
For general use, e.g. in scripts, the most useful functions are:
- Wait for a specific number of milliseconds:
waitMsec(int) - Wait for a specific sensor to be active:
waitSensorActive(jmri.Sensor)This is also available in a form that will wait for any of a group of sensors to be active. - Wait for a specific sensor to be inactive:
waitSensorInactive(jmri.Sensor)This is also available in a form that will wait for any of a group of sensors to be inactive. - Wait for a specific sensor to be in a specific state:
waitSensorState(jmri.Sensor, int) - Wait for a specific sensor to change:
waitSensorChange(int, jmri.Sensor) - Wait for a specific signal head to show a specific appearance:
waitSignalHeadState(jmri.SignalHead, int) - Wait for a specific signal mast to show a specific aspect:
waitSignalMastState(jmri.SignalMast, String) - Wait for a specific warrant to change run state:
waitWarrantRunState(Warrant, int) - Wait for a specific warrant to enter or leave a specific block:
waitWarrantBlock(Warrant, String, boolean) - Wait for a specific warrant to enter the next block or to stop:
waitWarrantBlockChange(Warrant) - Set a group of turnouts and wait for them to be consistent (actual
position matches desired position):
setTurnouts(jmri.Turnout[], jmri.Turnout[]) - Wait for a group of turnouts to be consistent (actually as set):
waitTurnoutConsistent(jmri.Turnout[]) - Wait for any one of a number of Sensors, Turnouts and/or other objects to
change:
waitChange(jmri.NamedBean[]) - Wait for any one of a number of Sensors, Turnouts and/or other objects to
change, up to a specified time:
waitChange(jmri.NamedBean[], int) - Obtain a DCC throttle:
getThrottle(int, boolean, int) - Read a CV from decoder on programming track:
readServiceModeCV(java.lang.String) - Write a value to a CV in a decoder on the programming track:
writeServiceModeCV(java.lang.String, int) - Write a value to a CV in a decoder on the main track:
writeOpsModeCV(java.lang.String, int, boolean, int)
Although this is named an "Abstract" class, it's actually concrete so scripts can easily use some of the methods.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescription(package private) JFrame(package private) String(package private) JFrameprotected booleanControl optional debugging prompt.(package private) NamedBean[](package private) int[](package private) BlockingQueue<PropertyChangeEvent> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) voidDeprecated, for removal: This API element is subject to removal in a future version.(package private) voiddone()Part of the internal implementation; not for general use.intgetCount()Get the number of times the handle routine has executed since the last timestart()was called on it.getName()Get the thread name.getThrottle(int address, boolean longAddress) getThrottle(int address, boolean longAddress, int waitSecs) Obtains a DCC throttle, including waiting for the command station response.getThrottle(BasicRosterEntry re, int waitSecs) Obtains a DCC throttle, including waiting for the command station response.protected booleanhandle()User-provided main routine.protected voidinit()User-provided initialization routine.booleanIs the thread in this object currently running?booleanIndicates that object is waiting on a waitSomething call.intRead a CV on the service track, including waiting for completion.voidrun()Part of the implementation; not for general use.final voidUpdate the name of this object.voidsetTurnouts(Turnout[] closed, Turnout[] thrown) Convenience function to set a bunch of turnouts and wait until they are all in a consistent statevoidstart()Start this automat's processing loop.voidstop()Stop the thread as soon as possible.protected voidwait(int milliseconds) Part of the internal implementation, not intended for users.voidwaitChange(NamedBean[] mInputs) Wait forever for one of a list of NamedBeans (sensors, signal heads and/or turnouts) to change.voidwaitChange(NamedBean[] mInputs, int maxDelay) Wait, up to a specified time, for one of a list of NamedBeans (sensors, signal heads and/or turnouts) to change their state.voidwaitChangePrecheck(NamedBean[] mInputs) Remembers the current state of a set of NamedBeans so that a later looping call to waitChange(..) on that same list won't miss any intervening changes.voidwaitMsec(int milliseconds) Wait for a specified time and then return control.voidwaitSensorActive(Sensor mSensor) Wait for a sensor to be active.voidwaitSensorActive(Sensor[] mSensors) Wait for one of a list of sensors to be be active.intwaitSensorChange(int mState, Sensor mSensor) Wait for a sensor to change state.voidwaitSensorChange(Sensor[] mSensors) Wait for one of an array of sensors to change.voidwaitSensorInactive(Sensor mSensor) Wait for a sensor to be inactive.voidwaitSensorInactive(Sensor[] mSensors) Wait for one of a list of sensors to be be inactive.voidwaitSensorState(Sensor[] mSensors, int state) Wait for one of a list of sensors to be be in a selected state.voidwaitSensorState(Sensor mSensor, int state) Internal service routine to wait for one sensor to be in (or become in) a specific state.voidwaitSignalHeadState(SignalHead mSignalHead, int state) Internal service routine to wait for one SignalHead to be in (or become in) a specific state.voidwaitSignalMastState(SignalMast mSignalMast, String aspect) Internal service routine to wait for one signal mast to be showing a specific aspectvoidwaitTurnoutConsistent(Turnout[] mTurnouts) Wait for a list of turnouts to all be in a consistent statevoidwaitWarrantBlock(Warrant warrant, String block, boolean occupied) Wait for a warrant to enter a named block.waitWarrantBlockChange(Warrant warrant) Wait for a warrant to either enter a new block or to stop running.voidwaitWarrantRunState(Warrant warrant, int state) Wait for a warrant to change into or out of running state.booleanwriteOpsModeCV(String cv, int value, boolean longAddress, int loco) Write a CV in ops mode, including waiting for completion.booleanwriteServiceModeCV(String cv, int value) Write a CV on the service track, including waiting for completion.
-
Field Details
-
promptOnWait
Control optional debugging prompt. If this is set true, each call to wait() will prompt the user whether to continue. -
waitChangePrecheckBeans
-
waitChangePrecheckStates
int[] waitChangePrecheckStates -
waitChangeQueue
-
messageFrame
-
message
-
debugWaitFrame
-
-
Constructor Details
-
AbstractAutomaton
public AbstractAutomaton() -
AbstractAutomaton
-
-
Method Details
-
start
Start this automat's processing loop.Will execute the init() method, if present, then repeatedly execute the handle() method until complete.
-
isRunning
Is the thread in this object currently running? -
run
Part of the implementation; not for general use.This is invoked on currentThread.
-
stop
Stop the thread as soon as possible.This interrupts the thread, which will cause it to terminate soon. There's no guarantee as to how long that will take. It should be just a few milliseconds.
To see if the thread has terminated, check the
isRunning()status method.Once the thread has terminated, it can be started again.
-
done
void done()Part of the internal implementation; not for general use.Common internal end-time processing
-
getCount
Get the number of times the handle routine has executed since the last timestart()was called on it.Used by classes such as those in
jmri.jmrit.automat.monitorto monitor progress.- Returns:
- the number of times
handle()has executed in this AbstractAutomation since it was last started.
-
getName
Get the thread name. Used by classes monitoring this AbstractAutomation, such asjmri.jmrit.automat.monitor.- Returns:
- the name of this thread
- See Also:
-
setName
Update the name of this object.name is not a bound parameter, so changes are not notified to listeners.
If you're going to use this, it should generally be called before calling
start().- Parameters:
name- the new name- See Also:
-
defaultName
Deprecated, for removal: This API element is subject to removal in a future version. -
init
User-provided initialization routine.This is called exactly once each time the object's start() method is called. This is where you put all the code that needs to be run when your object starts up: Finding sensors and turnouts, getting a throttle, etc.
-
handle
User-provided main routine.This is run repeatedly until it signals the end by returning false. Many automata are intended to run forever, and will always return true.
- Returns:
- false to terminate the automaton, for example due to an error.
-
waitMsec
Wait for a specified time and then return control.- Parameters:
milliseconds- the number of milliseconds to wait
-
isWaiting
Indicates that object is waiting on a waitSomething call.Specifically, the wait has progressed far enough that any change to the waited-on-condition will be detected.
- Returns:
- true if waiting; false otherwise
-
wait
Part of the internal implementation, not intended for users.This handles exceptions internally, so they needn't clutter up the code. Note that the current implementation doesn't guarantee the time, either high or low.
Because of the way Jython access handles synchronization, this is explicitly synchronized internally.
- Parameters:
milliseconds- the number of milliseconds to wait
-
waitSensorChange
Wait for a sensor to change state.The current (OK) state of the Sensor is passed to avoid a possible race condition. The new state is returned for a similar reason.
This works by registering a listener, which is likely to run in another thread. That listener then interrupts the automaton's thread, who confirms the change.
- Parameters:
mState- Current state of the sensormSensor- Sensor to watch- Returns:
- newly detected Sensor state
-
waitSensorActive
Wait for a sensor to be active. (Returns immediately if already active)- Parameters:
mSensor- Sensor to watch
-
waitSensorInactive
Wait for a sensor to be inactive. (Returns immediately if already inactive)- Parameters:
mSensor- Sensor to watch
-
waitSensorState
Internal service routine to wait for one sensor to be in (or become in) a specific state.Used by waitSensorActive and waitSensorInactive
This works by registering a listener, which is likely to run in another thread. That listener then interrupts this thread to confirm the change.
- Parameters:
mSensor- the sensor to wait forstate- the expected state
-
waitSensorInactive
Wait for one of a list of sensors to be be inactive.- Parameters:
mSensors- sensors to wait on
-
waitSensorActive
Wait for one of a list of sensors to be be active.- Parameters:
mSensors- sensors to wait on
-
waitSensorState
Wait for one of a list of sensors to be be in a selected state.This works by registering a listener, which is likely to run in another thread. That listener then interrupts the automaton's thread, who confirms the change.
- Parameters:
mSensors- Array of sensors to watchstate- State to check (static value from jmri.Sensors)
-
waitSignalHeadState
Internal service routine to wait for one SignalHead to be in (or become in) a specific state.This works by registering a listener, which is likely to run in another thread. That listener then interrupts this thread to confirm the change.
- Parameters:
mSignalHead- the signal head to wait forstate- the expected state
-
waitSignalMastState
Internal service routine to wait for one signal mast to be showing a specific aspectThis works by registering a listener, which is likely to run in another thread. That listener then interrupts this thread to confirm the change.
- Parameters:
mSignalMast- the mast to wait foraspect- the expected aspect
-
waitWarrantRunState
Wait for a warrant to change into or out of running state.This works by registering a listener, which is likely to run in another thread. That listener then interrupts the automaton's thread, who confirms the change.
- Parameters:
warrant- The name of the warrant to watchstate- State to check (static value from jmri.logix.warrant)
-
waitWarrantBlock
Wait for a warrant to enter a named block.This works by registering a listener, which is likely to run in another thread. That listener then interrupts this thread to confirm the change.
- Parameters:
warrant- The name of the warrant to watchblock- block to checkoccupied- Determines whether to wait for the block to become occupied or unoccupied
-
waitWarrantBlockChange
Wait for a warrant to either enter a new block or to stop running.This works by registering a listener, which is likely to run in another thread. That listener then interrupts the automaton's thread, who confirms the change.
- Parameters:
warrant- The name of the warrant to watch- Returns:
- The name of the block that was entered or null if the warrant is no longer running.
-
waitTurnoutConsistent
Wait for a list of turnouts to all be in a consistent stateThis works by registering a listener, which is likely to run in another thread. That listener then interrupts the automaton's thread, who confirms the change.
- Parameters:
mTurnouts- list of turnouts to watch
-
setTurnouts
Convenience function to set a bunch of turnouts and wait until they are all in a consistent state- Parameters:
closed- turnouts to set to closed statethrown- turnouts to set to thrown state
-
waitChange
Wait, up to a specified time, for one of a list of NamedBeans (sensors, signal heads and/or turnouts) to change their state.Registers a listener on each of the NamedBeans listed. The listener is likely to run in another thread. Each fired listener then queues a check to the automaton's thread.
- Parameters:
mInputs- Array of NamedBeans to watchmaxDelay- maximum amount of time (milliseconds) to wait before continuing anyway. -1 means forever
-
waitChangePrecheck
Remembers the current state of a set of NamedBeans so that a later looping call to waitChange(..) on that same list won't miss any intervening changes.- Parameters:
mInputs- Array of NamedBeans to watch
-
waitChange
Wait forever for one of a list of NamedBeans (sensors, signal heads and/or turnouts) to change.- Parameters:
mInputs- Array of NamedBeans to watch
-
waitSensorChange
Wait for one of an array of sensors to change.This is an older method, now superceded by waitChange, which can wait for any NamedBean.
- Parameters:
mSensors- Array of sensors to watch
-
getThrottle
Obtains a DCC throttle, including waiting for the command station response.- Parameters:
address- Numeric address valuelongAddress- true if this is a long address, false for a short addresswaitSecs- number of seconds to wait for throttle to acquire before returning null- Returns:
- A usable throttle, or null if error
-
getThrottle
-
getThrottle
Obtains a DCC throttle, including waiting for the command station response.- Parameters:
re- specifies the desired locomotivewaitSecs- number of seconds to wait for throttle to acquire before returning null- Returns:
- A usable throttle, or null if error
-
getThrottle
-
writeServiceModeCV
Write a CV on the service track, including waiting for completion.- Parameters:
cv- Number 1 through 512value- Value 0-255 to be written- Returns:
- true if completed OK
-
readServiceModeCV
Read a CV on the service track, including waiting for completion.- Parameters:
cv- Number 1 through 512- Returns:
- -1 if error, else value
-
writeOpsModeCV
Write a CV in ops mode, including waiting for completion.- Parameters:
cv- Number 1 through 512value- 0-255 value to be writtenloco- Locomotive decoder addresslongAddress- true is the locomotive is using a long address- Returns:
- true if completed OK
-