Skip to main content

Simulation listeners reference

Script's variables

baseDir

  • Type: string
  • Usage:
    new File(baseDir, 'template.xlsx').withInputStream { in ->
    def workbook = new XSSFWorkbook(in)
    ...
    }
    Path to the simulation folder.

outputDir

  • Type: string
  • Usage:
    new File(outputDir, 'report.xlsx').withOutputStream { out ->
    workbook.write(out)
    }
    Path to the simulation output folder.

out

  • Type: java.lang.PrintStream
  • Usage:
    println("Hello world") // out is implicit
    The "standard" output stream for the listener. The script redirects all the output functions (print, println, etc.) to this PrintStream.

registry advanced

  • Type: com.trenolab.trilogy.simulator.SimEventsListenerRegistry
  • Usage:
    registry.register(myListener)
    registry.unregister(myListener)
    Registers or deregisters event listeners. Direct registry usage is required only in some particular cases; in most cases, the usage of eventListener method will be sufficient.

context advanced

  • Type: org.openide.util.Lookup
  • Usage:
    context.lookup(MyClass)
    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 lookup or lookupAll methods 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:
    eventListener(SimRouteOccupiedEvent) { evt ->
    routeOccupyTimes[evt.route][evt.train] = evt.time
    }
    Registers a listener, that is, a closure that will be called each time the specified event is emitted by a simulation entity.

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 returns true. 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 the condition, if present, returns true. The argument of the closure contains the object with the event data.
  • Usage:
    eventListener(SimTrainMovementEvent, condition: { 
    entry.stop && eventType == EventType.DEPARTURE
    }) { evt ->
    ...
    }
    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 (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 the emit method. 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:
    eventListener(SimRouteOccupiedEvent) { evt ->
    if (evt.startingSignal.home) {
    emit('homeRouteOccupied', [
    train: evt.train,
    station: evt.startingSignal.station
    ])
    }
    }
    Emitts a named event

stop

  • Signature: void stop()
  • Usage:
    eventListener(SimRouteOccupiedEvent) { evt ->
    if (evt.startingSignal.home) {
    stop()
    }
    }
    Called from inside the event listener, deregisters the listener itself.

Global methods and functions