hu
Bejelentkezés

← Vissza az API-referenciához

Things A collection

All things of one sort on your layout, by name: all trains, all blocks, all stations. A map from name to object that TRAX Control keeps up to date: a new train appears in it, a renamed one changes with it.

Mire: To get a thing by name (trains.IC), to see whether something is there (find, containsKey), and to go through them all (each, values, findAll). A script reads a collection; it cannot change it.

def ic = trains.find("IC")
trains.each { name, train -> log name }
def occupied = blocks.values().findAll { it.occupied }

Metódusok

find → Thing

The thing with this name, or null.

The object with exactly this name, or null when there is none. Unlike trains.IC and trains["IC"], find throws no exception, but it is case-sensitive.

def museum = trains.find("Heritage")
if (museum) {
   museum.startAgent()
}

each

Every one of them.

Iterates over the map: the closure gets the name and the object. A snapshot of that moment.

blocks.each { name, Block block ->
   log "${name}: ${block.occupied}"
}

values → Collection

All of them.

The objects as a (read-only) Collection, for all of Groovy's collection methods: findAll, collect, any, every, sort, groupBy, count.

def byKind = places.values().groupBy { it.kind }
log byKind.collectEntries { kind, list -> [kind, list.size()] }

findAll → Collection

Those that match.

Filtering goes through values(): findAll gives a new list of the objects for which the closure is true. That list is yours to change.

def freeTracks = blocks.values().findAll { !it.occupied && it.place == stations.South }

containsKey → boolean

Whether one has this name.

Whether there is an object with exactly this name (case-sensitive).

if (devices.containsKey("Tablet")) {
   log "the tablet is registered"
}