std/sim

nodes, runs, faults

Node event: Delivered [r: msg], a record injected with deliver (used as when [n: Node] (sim.Delivered Open)).

Trace events: Started, Ended, Added [n: node], Removed [n: node], Crashed [n: node], Restarted [n: node], Changed [f: field] of [n: node] from [old: value] to [new: value], Injected [r: msg] into [n: node], Fired on [n: node], Broken [rule: text], Emitted [e: msg] by [n: node], Failed [error: text] (spec/08).

Phrases

sim.Seed [n: number]

Sets the seed of the run's PRNG; n is a whole number from 0 to 2^53, otherwise bad-argument. The --seed flag, when given, wins over sim.Seed; without either the seed is 1. The trace header records the seed in force when define main returns.

Gives
—
Contexts
main
Kind
phrase, native
Source
sim/sim.synod:3
Examples
sim/nodes

sim.StopAt [t: time]

End the run at simulated time t

Gives
—
Contexts
main
Kind
phrase, native
Source
sim/sim.synod:6
Examples
sim/failing, sim/invariant, sim/lifecycle, sim/messages, sim/nodes

sim.Spawn [p: package]

Create and start one node

Gives
p.Node
Contexts
main
Kind
phrase, native
Source
sim/sim.synod:9
Examples
sim/failing, sim/invariant, sim/lifecycle, sim/messages

sim.Cluster of [count: number] running [p: package]

Create and start count nodes

Gives
list of p.Node
Contexts
main
Kind
phrase, native
Source
sim/sim.synod:12
Examples
sim/nodes

sim.Crash [n: node]

Stop a node (spec/07)

Gives
—
Contexts
main
Kind
phrase, native
Source
sim/sim.synod:15
Examples
sim/lifecycle, sim/nodes

sim.Restart [n: node]

Restart a crashed node with fresh state

Gives
—
Contexts
main
Kind
phrase, native
Source
sim/sim.synod:18
Examples
sim/lifecycle

sim.Remove [n: node]

Remove a node for good

Gives
—
Contexts
main
Kind
phrase, native
Source
sim/sim.synod:21
Examples
sim/lifecycle

sim.Id [n: node]

1, 2, 3… in creation order

Gives
number
Contexts
node, main, check, look
Kind
phrase, native
Source
sim/sim.synod:24
Examples
sim/nodes

sim.Name [n: node]

"n1", "n2", …

Gives
text
Contexts
node, main, check, look
Kind
phrase, native
Source
sim/sim.synod:27
Examples
sim/nodes

sim.Nobody

The "no node" value

Gives
node
Contexts
node, main, check, look
Kind
phrase, native
Source
sim/sim.synod:30
Examples
sim/messages

sim.Nodes

All nodes not removed, in creation order

Gives
list of node
Contexts
main, check, look
Kind
phrase, native
Source
sim/sim.synod:33
Examples
sim/nodes

sim.NodesRunning [p: package]

Nodes running p

Gives
list of p.Node
Contexts
main, check, look
Kind
phrase, native
Source
sim/sim.synod:36
Examples
sim/invariant, sim/nodes

sim.IsUp [n: node]

Not crashed and not removed

Gives
bool
Contexts
main, check, look
Kind
phrase, native
Source
sim/sim.synod:39
Examples
sim/nodes

sim.StateRows [n: node]

One line per exported field of n: name and value as text

Gives
list of text
Contexts
look
Kind
phrase, native
Source
sim/sim.synod:81
Examples
sim/messages

Node events

sim.Delivered [r: msg]

Node event: a record injected with deliver

Gives
—
Contexts
node
Kind
node event, native
Source
sim/sim.synod:42
Examples
sim/messages

Trace events

sim.Started

Trace event: the run starts

Gives
—
Contexts
look
Kind
trace event, native
Source
sim/sim.synod:45
Examples
sim/lifecycle

sim.Ended

Trace event: the run ends

Gives
—
Contexts
look
Kind
trace event, native
Source
sim/sim.synod:48
Examples
sim/lifecycle

sim.Added [n: node]

Trace event: a node is created

Gives
—
Contexts
look
Kind
trace event, native
Source
sim/sim.synod:51
Examples
sim/lifecycle, sim/messages

sim.Removed [n: node]

Trace event: a node is removed with sim.Remove

Gives
—
Contexts
look
Kind
trace event, native
Source
sim/sim.synod:54
Examples
sim/lifecycle

sim.Crashed [n: node]

Trace event: a node is stopped with sim.Crash

Gives
—
Contexts
look
Kind
trace event, native
Source
sim/sim.synod:57
Examples
sim/lifecycle

sim.Restarted [n: node]

Trace event: a node is restarted with sim.Restart

Gives
—
Contexts
look
Kind
trace event, native
Source
sim/sim.synod:60
Examples
sim/lifecycle

sim.Changed [f: field] of [n: node] from [old: value] to [new: value]

Trace event: set changes an exported node field

Gives
—
Contexts
look
Kind
trace event, native
Source
sim/sim.synod:63
Examples
sim/messages

sim.Injected [r: msg] into [n: node]

Trace event: deliver injects a record from main

Gives
—
Contexts
look
Kind
trace event, native
Source
sim/sim.synod:66
Examples
sim/messages

sim.Fired on [n: node]

Trace event: an after timer fires on a node

Gives
—
Contexts
look
Kind
trace event, native
Source
sim/sim.synod:69
Examples
sim/messages

sim.Broken [rule: text]

Trace event: an always turns false

Gives
—
Contexts
look
Kind
trace event, native
Source
sim/sim.synod:72
Examples
sim/invariant

sim.Emitted [e: msg] by [n: node]

Trace event: emit e by n; n is sim.Nobody when main emits

Gives
—
Contexts
look
Kind
trace event, native
Source
sim/sim.synod:75
Examples
sim/messages

sim.Failed [error: text]

Trace event: a run-time error stops the run

Gives
—
Contexts
look
Kind
trace event, native
Source
sim/sim.synod:78
Examples
sim/failing

Examples

failing

Shows the event sim.Failed: the run stops with an error and the look shows it; its run fails on purpose with the code in expect

This run stops on purpose with the diagnostic division-by-zero.

# package main
# Shows the event sim.Failed: the run stops with an error and the look shows it; its run fails on purpose with the code in expect
use example.com/library/sim/failing
use std/sim

define main
  set a to (sim.Spawn failing)
  set a.failing.Count to 0
  sim.StopAt 5s
# package failing
use std/look
use std/sim
use std/time

Node has
  set Count to 0

define [n: Node] start
  time.Every 1s
    set n.Count to (n.Count plus 1)
    if (n.Count is 2)
      set n.Count to (n.Count divided by 0)

# Failed is the last fact of a run that stopped on a run-time error
when (sim.Failed error)
  draw (look.Label error) as banner

invariant

Shows the event sim.Broken, recorded when an always rule turns false

# package main
# Shows the event sim.Broken, recorded when an always rule turns false
use example.com/library/sim/invariant
use std/sim

define main
  set a to (sim.Spawn invariant)
  set a.invariant.Count to 0
  sim.StopAt 6s
# package invariant
use std/look
use std/sim
use std/time

Node has
  set Count to 0

define [n: Node] start
  time.Every 1s
    set n.Count to (n.Count plus 1)

# The rule: no running node ever counts past three
define Calm
  each n in (sim.NodesRunning invariant)
    if (n.Count more than 3)
      result false
  result true

always Calm

# Broken is recorded once, when the rule turns false
when (sim.Broken rule)
  draw (look.Label rule) as alarm
  look.Place alarm above look.Center

lifecycle

Shows sim.Spawn, sim.Crash, sim.Restart, sim.Remove and the events Started, Added, Crashed, Restarted, Removed and Ended

# package main
# Shows sim.Spawn, sim.Crash, sim.Restart, sim.Remove and the events Started, Added, Crashed, Restarted, Removed and Ended
use example.com/library/sim/lifecycle
use std/sim

define main
  set a to (sim.Spawn lifecycle)
  set b to (sim.Spawn lifecycle)
  after 1s
    sim.Crash a
  after 2s
    sim.Restart a
  after 3s
    sim.Remove b
  sim.StopAt 4s
# package lifecycle
use std/look
use std/sim

Node has
  set Hits to 0

when sim.Started
  look.Layout "row"
  draw (look.Label "A node's life: added, crashed, restarted, removed") as title
  look.Place title above look.Center

when (sim.Added n)
  draw (look.Card for n) as card for n
  look.Pulse card

when (sim.Crashed n)
  look.Fade (card of n) to 0.3

when (sim.Restarted n)
  look.Fade (card of n) to 1

when (sim.Removed n)
  look.Remove (card of n)

when sim.Ended
  draw (look.Label "The run is over") as footer
  look.Place footer below look.Center

messages

Shows deliver, sim.Delivered, sim.Injected, sim.Fired, sim.Changed, sim.Emitted, sim.Nobody and sim.StateRows

# package main
# Shows deliver, sim.Delivered, sim.Injected, sim.Fired, sim.Changed, sim.Emitted, sim.Nobody and sim.StateRows
use example.com/library/sim/messages
use std/sim

define main
  set a to (sim.Spawn messages)
  deliver (message messages.Request with Text "hello") to a
  emit (message messages.Said with Text "from main") by sim.Nobody
  sim.StopAt 2s
# package messages
use std/look
use std/sim

Request has
  set Text to ""

Said has
  set Text to ""

Node has
  set Seen to 0

# Delivered runs on the node that receives a message injected by main
when [n: Node] (sim.Delivered Request)
  set n.Seen to (n.Seen plus 1)

# An after timer firing on a node is a Fired fact
define [n: Node] start
  after 500ms
    set n.Seen to (n.Seen plus 10)

when (sim.Injected r into n)
  look.Effect "glow" on (card of n)

when (sim.Fired on n)
  look.Pulse (card of n)

# Changing an exported field is recorded as Changed
when (sim.Changed f of n from old to new)
  look.Pulse (card of n)

when (sim.Emitted Said by n)
  draw (look.Label Said.Text) as caption
  look.Place caption above look.Center

# StateRows lists every exported field of the node as text
when (sim.Added n)
  draw (look.Card for n) as card for n
  each row in (sim.StateRows n)
    draw (look.Label row) as line
    look.Place line below (card of n)

nodes

Shows sim.Seed, sim.Cluster of, sim.Nodes, sim.NodesRunning, sim.IsUp, sim.Id, sim.Name and sim.StopAt

# package main
# Shows sim.Seed, sim.Cluster of, sim.Nodes, sim.NodesRunning, sim.IsUp, sim.Id, sim.Name and sim.StopAt
use example.com/library/sim/nodes
use std/lesson
use std/lists
use std/sim
use std/text

define main
  sim.Seed 7
  set group to (sim.Cluster of 3 running nodes)
  sim.Crash (item 1 of group)
  after 1s
    lesson.Note (text.Join {"Nodes of the package: ", (text.From (lists.Size (sim.NodesRunning nodes)))})
    each n in sim.Nodes
      lesson.Note (text.Join {(sim.Name n), " is number ", (text.From (sim.Id n)), ", up: ", (text.From (sim.IsUp n))})
  sim.StopAt 3s
# package nodes
Node has
  set Hits to 0