LogixNG Reference - Chapter 13

Jython Scripting Support

Jython is an important component provided by JMRI to make it possible to extend the JMRI features. LogixNG continues that ability to run scripts supporting JMRI using the same actions that were part of the original Logix. LogixNG extends that support to include the ability to extend LogixNG itself, by providing access to the LogixNG managers, access to global and local (since 5.7.6) variables and receiving results from a script, etc.

The following is a summary of the scripting options.

The LogixNG includes additional bindings to support LogixNG access.

These work like the standard bindings, such as masts.getSignalMast('mastName'). For example: myTable = logixngTables.getNamedTable('tableName').

The LogixNG bindings only exist when the script or command is invoked by LogixNG. Other scripts, such as startup actions or scripts run using the script editor, need to create their own LogixNG bindings. Here is an example of creating a binding for the GlobalVariableManager.

globalVarMgr = jmri.InstanceManager.getDefault(jmri.jmrit.logixng.GlobalVariableManager)
      

since 5.7.6Symbol table support

The LogixNG symbol table contains a list of local and global variables. Global variables are also available using the GlobalVariableManager.

Local variables are only in the symbol table. They are also very transient. They only exist during the execution of a ConditionalNG or Module and when within the scope of the logic.

When a script or command is executed by LogixNG, the symbolTable is added to the Jython context as a Python variable. This variable can be used to read and write the content of global variables and current local variables. Note: If strict typing has been enabled for the variable type, that will be enforced.

Sample script for accessing LogixNG content

A LogixNG ConditionalNG uses two local variables. The time-of-day variable contains a word representing the time of the day. The A1 if then else uses the System Clock function to get the current hour. The script is called to add the word Good to the time of day and update the greeting local variable. Then the current Local Variables are displayed on the JMRI system console. Note: This simple example does not require a script since LogixNG can do the whole process.

Chapter 13 greeting conditional

The script gets the value of the time-of-day local variable, adds the word Good and updates the greeting local variable

import java
import jmri

greeting = 'Good {}'.format(symbolTable.getValue('time_of_day'))
symbolTable.setValue('greeting', greeting)
        

The Log local variables action shows the local variable values.

WARN  - Log local variables:
WARN  -     Name: greeting, Value: Good morning
WARN  -     Name: time_of_day, Value: morning
WARN  - Log local variables done
        

Return to the Reference TOC