Groovy reference
This section is common to groovy listeners and scripted simulations. It lists various scripting methods that were not introduced, available only if called from Groovy.
Methods
include(scriptPath)
- Signature:
void include(String scriptPath) - Arguments:
scriptPathpath, relative to the calling script.
- Usage:
Executes the script in
include('config.script')scriptPathand includes its bindings in the caller script bindings. It is useful for finding configuration files written in groovy.
loadScript(scriptPath)
- Signature:
void loadScript(String scriptPath) - Arguments:
scriptPathpath, relative to the calling script.
- Usage:
Loads and parses the script in
loadScript('other-script.script')scriptPathwith the same shell as the calling script. It is useful for including snippets in an event listener.
loadScript(scriptPath, delegate)
- Signature:
void loadScript(String scriptPath, Object delegate) - Arguments:
scriptPathpath, relative to the calling script.delegateobject used as a delegate for the script to be loaded.
- Usage:
Loads and parses the script in
def delegate = new MyDelegate()
loadScript('other-script.script', delegate)scriptPathwith the same shell as the calling script. It is useful for including snippets in an event listener.
Events
Extension methods
Simulation settings
SimulationOptionConfiguration getAt(optionName)
- Signature:
SimulationOptionConfiguration getAt[String optionName], which is the same assettings[optionName] - Arguments:
optionNamename of the simulation option
- Usage:
Returns the configuration of the specified simulation option.
settings['dispatching'].enabled = false
Timetable
Timetable filter(expression)
👷👷🏾Timetable filter(trainNumbers)
👷👷🏾MacroTrain getAt(expression)
- Signature:
MacroTrain getAt[String expression], which is the same astimetable[expression] - Arguments:
expressiontrain number, or expression that represents a predicate on MacroTrain.
- Usage:
Returns the MacroTrain with the specified train number or that satisfies the predicate, in the form of an expression. For a complete predicate list, see Train matchers.
def train = timetable['1A23']
MacroTrain find(expression)
- Signature:
MacroTrain find(String expression) - Arguments:
expressionexpression that represents a predicate on MacroTrain.
- Usage:
Equivale a
timetable.find('train number = 1A23 and operator = SW')getAt(expression). Returns the MacroTrain of the Timetable that satisfies the predicate, in the form of an expression.
MacroTrain find(predicate)
- Signature:
MacroTrain find(Closure predicate) - Arguments:
predicateclosure that takes as argument a MacroTrain and returns true to select the train.
- Usage:
Returns the MacroTrain of the Timetable that satisfies the predicate, in the form of closure.
timetable.find { it.trainNumber = '1A23' && it.operator.name = 'SW' }
Iterable<MacroTrain> findAll()
- Signature:
Iterable<MacroTrain> findAll() - Usage:
Returns all the MacroTrains that are part of the Timetable.
def trains = timetable.findAll()
Iterable<MacroTrain> findAll(expression)
- Signature:
Iterable<MacroTrain> findAll(String expression) - Arguments:
expressionexpression that represents a predicate on MacroTrain.
- Usage:
Returns all the MacroTrains of the Timetable that satisfy the given predicate, in the form of an expression. For a complete predicate list, see Train matchers.
def trains = timetable.findAll('operator = SW')
Iterable<MacroTrain> findAll(predicate)
- Signature:
Iterable<MacroTrain> findAll(Closure predicate) - Arguments:
expressionclosure that takes as argument a MacroTrain and returns true to select the train.
- Usage:
Returns all the MacroTrains of the Timetable that satisfy the given predicate, in the form of closure.
def trains = timetable.findAll { it.operator.name = 'SW' }
Iterable<MacroTrain> findAll(expression, sort)
- Signature:
Iterable<MacroTrain> findAll(String expression, String sort) - Arguments:
expressionexpression that represents a predicate on MacroTrain.sortexpression that represents the sorting order criterion. The form isattr [ desc ] [ , attr [ desc] ]*that is, an expression that represents a train attribute, optionally followed by thedescclause; more expressions can be concatenated, separated by a comma. Furthermore, for the attributes that cannot be defined, it is possible to use theoroperator, for example,attr1 or attr2 [ desc ]. In this case, if the first attribute is not defined, the second one will be evaluated. For a complete predicate list, see Train matchers.
- Usage:
Returns all the MacroTrains of the Timetable that satisfy the given predicate, in the form of an expression. For a complete predicate list, see Train matchers. Furthermore, the MacroTrains are ordered by the specified criterion. The criterion can list more attributes; if one attribute is not defined, or has the same value for two trains, the next attribute is examined, and so on.
def trains = timetable.findAll('operator = SW', 'departure time at LIVST')
MacroTrain find(expression, sort)
- Signature:
MacroTrain find(String expression, String sort) - Arguments:
expressionexpression that represents a predicate on MacroTrain.sortexpression that represents the sorting order criterion. The form isattr [ desc ] [ , attr [ desc] ]*that is, an expression that represents a train attribute, optionally followed by thedescclause; more expressions can be concatenated, separated by a comma. Furthermore, for the attributes that cannot be defined, it is possible to use theoroperator, for example,attr1 or attr2 [ desc ]. In this case, if the first attribute is not defined, the second one will be evaluated. For a complete predicate list, see Train matchers.
- Usage:
Returns the first MacroTrains of the Timetable that satisfies the given predicate, in the specified order. Corresponds to the first MacroTrain returned by the
def train = timetable.find('calling at LIVST and station = GIDEAPK at next entry after LIVST and departure time > 12:00:00 at LIVST', 'departure time at LIVST')Timetable.findAll(expression, sort).
Map<?, MacroTrain> groupBy(keyExtractor)
- Signature:
Map<?, MacroTrain> groupBy(Closure keyExtractor) - Arguments:
keyExtractorclosure that takes as a parameter a MacroTrain and returns the grouping key.
- Usage:
Groups the trains of the Timetable based on the returned closure key.
def trainsByOperatorName = timetable.groupBy { it.operator.name }
int count()
- Signature:
int count() - Usage:
Returns the number of trains present in the timetable.
def count = timetable.count()
int timetable.count(expression)
- Signature:
int count(String expression) - Arguments:
expressionexpression that represents a predicate on MacroTrain.
- Usage:
Returns the number of trains in the Timetable that satisfy the given predicate, in the form of an expression. For a complete predicate list, see Train matchers.
def count = timetable.count('operator = SW')
int count(predicate)
- Signature:
int count(Closure predicate) - Arguments:
predicateclosure that takes as argument a MacroTrain and returns true to count the train.
- Usage:
Returns the number of trains present in the Timetable that satisfy the predicate.
def count = timetable.count { it.operator.name = 'SW' }
each(closure)
- Signature:
void each(Closure closure) - Arguments:
closureclosure that takes as argument a MacroTrain.
- Usage:
Calls the closure on each train of the timetable.
timetable.each { println it.operator.name }
each(expression, closure)
- Signature:
void each(String expression, Closure closure) - Arguments:
expressionexpression that represents a predicate on MacroTrain.closureclosure that takes as argument a MacroTrain.
- Usage:
Calls the closure on each train of the timetable that satisfies the predicate.
timetable.each('operator = SW') { println it.trainNumber }
each(filterExpression, sortExpression, Closure)
- Signature:
void each(String filterExpression, String sortExpression, Closure closure) - Arguments:
filterExpressionexpression that represents a predicate on MacroTrain.sortExpressionexpression that represents a sorting order criterion. The form isattr [ desc ] [ , attr [ desc] ]*, that is, an expression that represents a train attribute, optionally followed by thedescclause; more expressions can be concatenated, separated by a comma. For a complete predicate list, see Train matchers.closureclosure that takes as argument a MacroTrain.
- Usage:
Calls the closure on each train of the Timetable that satisfies the predicate, in the order established by the sorting order criterion.
timetable.each('operator = SW and origin = LIVST', 'departure time from LIVST') { println it.trainNumber }
SimulationTimetable
Since SimulationTimetable extends Timetable, all the methods seen for the Timetable can be called on SimulationTimetable as well, except that they return a MacroTrainCourse instead of a MacroTrain, or a Iterable<MacroTrainCourse> instead of a Iterable<MacroTrain>.
MacroTrainCourse getAt(expression)
- Signature:
MacroTrainCourse getAt[String expression], which is equivalent totimetable[expression] - Arguments:
expressiontrain number, or the key of the train composed bytrain number@data, or an expression that represents a predicate on MacroTrainCourse.
- Usage:
Returns the MacroTrainCourse with the specified train number or key. If there are more courses with the same number, any MacroTrainCourse will be returned.
def course = timetable['1A23@2021-05-01']
SimulationActualTimetable
Since SimulationActualTimetable extends SimulationTimetable, all the methods seen for the SimulationTimetable can be called on SimulationActualTimetable as well, except that they return MacroActualTrainCourse instead of a MacroTrainCourse, or a Iterable<MacroActualTrainCourse> instead of a Iterable<MacroTrainCourse>.
MacroTrain
MacroTrainPathEntry getAt(entryIndex)
- Signature:
MacroTrainPathEntry getAt(int entryIndex), which is the same astrain[entryIndex] - Arguments:
entryIndexindex of the path entry, 0-based.
- Usage:
Returns the MacroTrainPathEntry with the specified index.
def entry = timetable['1A23@2021-05-01'][0]
MacroTrainPathEntry getAt(expression)
- Signature:
MacroTrainPathEntry getAt(String expression), which is the same astrain[expression] - Arguments:
expressionshort code of the station, or expression that represents a predicate on MacroTrainPathEntry. Furthermore, for the trains that pass more times the same station (loop trains, or turnback along the path) and that consequently have more entries with the same station code, it is possible to specify which entry to return with thestationCode#indexsyntax, with index equal to0for the first found entry,1for the second, and so forth.
- Usage:
Returns the first MacroTrainPathEntry that satisfies the expression. For a complete list, see Path entry matcher.
def entry = timetable['1A23@2021-05-01'][0]
MacroTrainPathEntry find(expression)
- Signature:
MacroTrainPathEntry find(String expression) - Arguments:
expressionexpression that represents a predicate on MacroTrainPathEntry
- Usage:
Returns the first MacroTrainPathEntry that satisfies the expression. For a complete list, see Path entry matcher.
def entry = timetable['1A23@2021-05-01']['origin']
Iterable<MacroTrainPathEntry> findAll(expression)
- Signature:
Iterable<MacroTrainPathEntry> find(String expression) - Arguments:
expressionexpression that represents a predicate on MacroTrainPathEntry
- Usage:
Returns all the MacroTrainPathEntry that satisfies the expression. For a complete list, see Path entry matcher.
def turnbacks = timetable['1A23@2021-05-01'].findAll('turnback')
Object call(expression)
- Signature:
Object call(String expression), which is the same astrain(expression) - Arguments:
expressionexpression that represents a property of MacroTrain
- Usage:
Evaluates the expression in the MacronTrain context
def origin = timetable['1A23@2021-05-01']('origin')
MacroTrainCourse
The available methods are the same as for MacroTrains
MacroActualTrainCourse
The available methods are the same as for MacroTrainCourse
SimulationResults
SimulationResults.Train getAt(expression)
- Signature:
SimulationResults.Train getAt(String expression), which is the same assimulationResults[expression] - Arguments:
expressiontrain number, or expression that represents a predicate on MacroTrainCourse.
- Usage:
Returns the results of the simulation of the first train with the specified number, or that satisfies the predicate, in the form of an expression. For a complete predicate list, see Train matchers.
def trainResults = simulationResults['1A23@2021-05-01']
SimulationResults.Train find(String expression)
- Signature:
SimulationResults.Train find(String expression) - Arguments:
expressionthe expression that represents a predicate on MacroTrain
- Usage:
Returns the results of the simulation of the first train that satisfies the predicate, in the form of an expression. For a complete predicate list, see Train matchers.
def train = simulationResults.find('origin = LIVST and departure time = 11:03:00')
Iterable\u003CSimulationResults.Train\u003E findAll(String expression)
- Signature:
Iterable<SimulationResults.Train> findAll(String expression) - Arguments:
expressionexpression that represents a predicate on MacroTrain
- Usage:
Returns the results of the simulation of trains that satisfy the predicate. For a complete predicate list, see Train matchers.
simulationResults.findAll('origin = LIVST').each { ... }
SimulationResults.Train
SimulationResults.PathEntry getAt(index)
- Signature:
SimulationResults.PathEntry getAt(int index), which is the same astrainResults[index] - Arguments:
indexindex of the entry
- Usage:
Returns the results of the simulation for the entry specified by the index
def entryResults = trainResults[0]
SimulationResults.PathEntry find(String expression)
- Signature:
SimulationResults.PathEntry find(String expression) - Arguments:
expressionexpression that represents a predicate on MacronTrainPathEntry
- Usage:
Returns the results of the simulation for the entry that satisfies the expression. For a complete predicate list, see Train matchers.
def origin = trainResults.find('origin')
SimulationResults.PathEntry findFirst(String expression)
- Signature:
SimulationResults.PathEntry findFirst(String expression) - Arguments:
expressionexpression that represents a predicate on MacronTrianPathEntry
- Usage:
Returns the simulation results of the first entry that satisfies the expression. For a complete predicate list, see Train matchers.
def origin = trainResults.findFirst('LIVST')
SimulationResults.PathEntry findLast(String expression)
- Signature:
SimulationResults.PathEntry findLast(String expression) - Arguments:
expressionexpression that represents a predicate on MacronTrianPathEntry
- Usage:
def origin = trainResults.findLast('LIVST')
Restituisce i risultati di simulazione dell'ultima entry che soddisfa l'espressione. For a complete predicate list, see Train matchers.
Iterable<SimulationResults.PathEntry> findAll(String expression)
- Signature:
Iterable<SimulationResults.PathEntry> findAll(String expression) - Arguments:
expressionexpression that represents a predicate on MacronTrianPathEntry
- Usage:
trainResults.findAll('stop').each { ... }
Returns all the results of the simulation for the entries that satisfy the expression. For a complete predicate list, see Train matchers.
DescriptiveStatistics
Please see the Official documentation, here we will only consider the extension methods available in groovy scripts.
property nonNegativeMean
- Signature:
double nonNegativeMeaan - Returns: the non negative mean of this statistics
- Usage:
double val = descriptiveStatistics.nonNegativeMean
Returns the mean of all the values, assigning zero to the negative ones.
property punctualityIndex
- Signature:
double punctualityIndex[int threshold],double punctualityIndex[duration threshold] - Arguments:
thresholddelay threshold, in seconds orDuration.
- Usage:
double val = descriptiveStatistics.punctualityIndex[5.minutes]
Returns the percentage of the values in the statistics that are smaller than the threshold
property percentile
- Signature:
double percentile[double p] - Arguments:
pthe requested percentile (scaled from 0 - 100)
- Returns: an estimate for the p-th percentile of the stored data
- Usage:
double val = descriptiveStatistics.percentile[95]
Returns the estimate of the p-th percentile.
ANTLR-based statistics
trainStatistic
- Signature:
Double Collection<MacroActualTrainCourse>.trainStatistic(String expr) - Arguments:
expran ANTLR-based expression on trains, such asavg (delay at destination)
- Returns: the result of the computation across the collection of trains.
- Usage:
double val = trains.trainStatistic('avg (delay at destination)')
asTrainStatistic
- Signature:
Function<Collection<MacroActualTrainCourse>, Double> String.asTrainStatistic() - Returns: a function to compute the statistic across a collection of trains.
- Usage:
double fn = 'avg (delay at destination)'.asTrainStatistic()
double val = fn(trains)
entryStatistic
- Signature:
Double Collection<MacroTrainActualPathEntry>.entryStatistic(String expr) - Arguments:
expran ANTLR-based expression on entries, such asavg (arrival delay)
- Returns: the result of the computation across the collection of entries.
- Usage:
double val = entries.entryStatistic('avg (arrival delay)')
asTrainStatistic
- Signature:
Function<Collection<MacroTrainActualPathEntry>, Double> String.asEntryStatistic() - Returns: a function to compute the statistic across a collection of entries.
- Usage:
double fn = 'avg (arrival delay)'.asEntryStatistic()
double val = fn(entries)
Free functions
Measure units
m(originalUnit), m(originalUnit, hint)
- Signature:
String m(String originalUnit),String m(String originalUnit, String hint) - Arguments:
originalUnitSI unit of measurement.hintoptional, disambiguates the units of measurement used for different magnitudes, for example, 'm' for lengths of progressive kilometers.
- Usage:
IS unit of measurement -> predefined unit of measurement
m('m', 'kmpoint') // -> km
m(originalValue, originalUnit), m(originalValue, originalUnit, hint)
- Signature:
String m(Number originalValue, String originalUnit),String m(Number originalValue, String originalUnit, String hint) - Arguments:
originalValuevalue expressed in the SI unit of measurement.originalUnitSI unit of measurement.hintoptional, disambiguates the units of measurement used for different magnitudes, for example, 'm' for lengths of progressive kilometers.
- Usage:
Converts a numeric value expressed in the SI unit of measurement to a textual value expressed in the predefined measurement system unit.
m(10, 'm/s') // -> 36
m(originalValue, originalUnit), m(originalValue, originalUnit, hint)
- Signature:
String mm(Number originalValue, String originalUnit),String mm(Number originalValue, String originalUnit, String hint) - Arguments:
originalValuevalue expressed in the SI unit of measurement.originalUnitSI unit of measurement.hintoptional, disambiguates the units of measurement used for different magnitudes, for example, 'm' for lengths of progressive kilometers.
- Usage:
Converts a numeric value expressed in the SI unit of measurement to a textual value expressed in the predefined measurement system unit, with the appropriate suffix.
mm(10, 'm/s') // -> 36 km/h
v(text, originalUnit), m(originalValue, originalUnit, hint) 1.4.23+
- Signature:
String m(String text, String originalUnit),String m(String text, String originalUnit, String hint) - Arguments:
texttext that represents a value in the predefined measurement system unit.originalUnitSI unit of measurement.hintoptional, disambiguates the units of measurement used for different magnitudes, for example, 'm' for lengths of progressive kilometers.
- Usage:
Converts a numeric value expressed in the SI unit of measurement to a textual value expressed in the predefined measurement system unit, in a numeric value in SI.
v('36', 'm/s') // -> 3106
Time
t(time)
- Signature:
String t(Number time)v1.4.22+ - Arguments:
timetime expressed in seconds.
- Usage:
t(12345) // -> '03:25:45'
t(123456) // -> '34:17:36'
Converts a time expressed in seconds in a time with hhh:mm:ss format. The hour field can go over 23 if the time exceeds 24 hours. It's the inverse function of s(time).
s(time)
- Signature:
String s(String time)v1.4.22+ - Arguments:
timetime in hhh:mm:ss format.
- Usage:
t('03:25:45') // -> 12345
t('34:17:36') // -> 123456
Converts a time in hhh:mm:ss format in a time expressed in seconds. It's the inverse function of t(time).