Simulation listeners reference
Script's variables
baseDir
- Type:
string - Usage:
Path to the simulation folder.
new File(baseDir, 'template.xlsx').withInputStream { in ->
def workbook = new XSSFWorkbook(in)
...
}
outputDir
- Type:
string - Usage:
Path to the simulation output folder.
new File(outputDir, 'report.xlsx').withOutputStream { out ->
workbook.write(out)
}
out
- Type:
java.lang.PrintStream - Usage:
The "standard" output stream for the listener. The script redirects all the output functions (
println("Hello world") // out is implicitprint,println, etc.) to thisPrintStream.
registry advanced
- Type:
com.trenolab.trilogy.simulator.SimEventsListenerRegistry - Usage:
Registers or deregisters event listeners. Direct registry usage is required only in some particular cases; in most cases, the usage of
registry.register(myListener)
registry.unregister(myListener)eventListenermethod will be sufficient.
context advanced
- Type:
org.openide.util.Lookup - Usage:
It's the global lookup of the script. It can be used to obtain all the collaborators and singleton instances of the simulation. Direct context usage is required only in particular situations cases; in most cases, the usage of
context.lookup(MyClass)lookuporlookupAllmethods will be sufficient.
Script methods
eventListener (EventType) { ... }
- Signature:
void eventListener(Class eventType, Closure listener) - Arguments:
eventType: the class that represents the event type. 👷👷🏾listener: the closure to be called each time an event is emitted by a simulation entity. The argument of the closure contains the object with the event data.
- Usage:
Registers a listener, that is, a closure that will be called each time the specified event is emitted by a simulation entity.
eventListener(SimRouteOccupiedEvent) { evt ->
routeOccupyTimes[evt.route][evt.train] = evt.time
}
eventListener ([options], EventType) { ... } 1.5.0+
- Signature:
void eventListener(Map options, Class eventType, Closure listener) - Arguments:
options: additional options.condition: a closure to be called before the listener. The listener will be called only if this closure returnstrue. The delegate of the closure is the event.
eventType: the class that represents the event type. 👷👷🏾listener: the closure to be called each time the listener is emitted by a simulation entity when thecondition, if present, returnstrue. The argument of the closure contains the object with the event data.
- Usage:
Registers a listener, that is, a closure that will be called each time the event of the specified type is emitted by a simulation entity. The additional operations allow you to specify a condition to be evaluated before the listener is called.
eventListener(SimTrainMovementEvent, condition: {
entry.stop && eventType == EventType.DEPARTURE
}) { evt ->
...
}
eventListener (String) { ... } 1.5.0+
- Signature:
void eventListener(String eventName, Closure listener) - Arguments:
eventName: the name of the event.listener: the closure to be called each time the event is emitted through theemitmethod. The closure argument contains the object with the event data.
- Usage:
eventListener('homeRouteOccupied') { evt ->
...
}
Registers a listener for a named event, emitted by the script itself, or by other scripts, through emit method.
emit (String, Object) { ... } 1.5.0+
- Signature:
void emit(String eventName, Object eventData) - Arguments:
eventName: the name of the emitted event.eventData: the object with the event data.
- Usage:
Emitts a named event
eventListener(SimRouteOccupiedEvent) { evt ->
if (evt.startingSignal.home) {
emit('homeRouteOccupied', [
train: evt.train,
station: evt.startingSignal.station
])
}
}
stop
- Signature:
void stop() - Usage:
Called from inside the event listener, deregisters the listener itself.
eventListener(SimRouteOccupiedEvent) { evt ->
if (evt.startingSignal.home) {
stop()
}
}