{
  "glassbox": 3,
  "id": "zone",
  "title": "Heating-zone controller",

  "notes": [
    "One zone of a heating system. The facts its guards read are named for the",
    "columns of the `zone` table they come from (`target_temp`, `window_open`),",
    "plus two the host works out: the latest `reading` and the `low_setpoint`,",
    "which is target_temp - hysteresis. The format has no arithmetic in guards,",
    "so a derived quantity is the host's to compute and the machine's to compare.",
    "",
    "What is claimed of it is in model/ (Machine.tla, Properties.tla and the",
    "obligations): this file is lowered to TLA+ and SMT and the claims are",
    "checked of that."
  ],

  "initial": "off",

  "states": [
    { "id": "off",     "label": "Off",     "entry": ["heater-off"] },
    { "id": "idle",    "label": "Idle",    "entry": ["heater-off"] },
    { "id": "heating", "label": "Heating", "entry": ["heater-on"] },
    { "id": "fault",   "label": "Fault",   "entry": ["heater-off", "latch-fault"], "exit": ["clear-fault"] }
  ],

  "events": [
    { "id": "power-on",       "label": "power on",       "source": "user" },
    { "id": "power-off",      "label": "power off",      "source": "user" },
    { "id": "reset",          "label": "reset",          "source": "user" },
    { "id": "reading",        "label": "temperature reading", "source": "runtime" },
    { "id": "window-changed", "label": "window opened or closed", "source": "runtime" },
    { "id": "sensor-lost",    "label": "sensor lost",    "source": "runtime" }
  ],

  "facts": [
    { "id": "reading",      "label": "The latest temperature, in tenths of a degree", "kind": "quantity" },
    { "id": "target_temp",  "label": "The zone's target temperature",              "kind": "quantity" },
    { "id": "low_setpoint", "label": "The temperature below which heating starts", "kind": "quantity" },
    { "id": "window_open",  "label": "The zone's window is open",                  "kind": "truth" }
  ],

  "refusals": [
    { "id": "in-fault", "text": "the zone has faulted; reset it first" }
  ],

  "commands": [
    { "id": "heater-on",   "label": "Open the zone valve" },
    { "id": "heater-off",  "label": "Close the zone valve" },
    { "id": "latch-fault", "label": "Latch the fault and light the lamp" },
    { "id": "clear-fault", "label": "Clear the fault" }
  ],

  "rules": [
    { "from": "off", "on": "power-on",       "cases": [ { "to": "idle" } ] },
    { "from": "off", "on": "power-off",      "cases": [ { "stay": true } ] },
    { "from": "off", "on": "reset",          "cases": [ { "stay": true } ] },
    { "from": "off", "on": "reading",        "cases": [ { "stay": true } ] },
    { "from": "off", "on": "window-changed", "cases": [ { "stay": true } ] },
    { "from": "off", "on": "sensor-lost",    "cases": [ { "stay": true } ] },

    { "from": "idle", "on": "reading", "cases": [
      { "when": { "all": [
          { "not": { "fact": "window_open" } },
          { "op": "lt", "left": { "fact": "reading" }, "right": { "fact": "low_setpoint" } }
        ] }, "to": "heating" },
      { "stay": true }
    ]},
    { "from": "idle", "on": "power-off",      "cases": [ { "to": "off" } ] },
    { "from": "idle", "on": "sensor-lost",    "cases": [ { "to": "fault" } ] },
    { "from": "idle", "on": "power-on",       "cases": [ { "stay": true } ] },
    { "from": "idle", "on": "reset",          "cases": [ { "stay": true } ] },
    { "from": "idle", "on": "window-changed", "cases": [ { "stay": true } ] },

    { "from": "heating", "on": "reading", "cases": [
      { "when": { "op": "ge", "left": { "fact": "reading" }, "right": { "fact": "target_temp" } }, "to": "idle" },
      { "stay": true }
    ]},
    { "from": "heating", "on": "window-changed", "cases": [
      { "when": { "fact": "window_open" }, "to": "idle" },
      { "stay": true }
    ]},
    { "from": "heating", "on": "power-off",   "cases": [ { "to": "off" } ] },
    { "from": "heating", "on": "sensor-lost", "cases": [ { "to": "fault" } ] },
    { "from": "heating", "on": "power-on",    "cases": [ { "stay": true } ] },
    { "from": "heating", "on": "reset",       "cases": [ { "stay": true } ] },

    { "from": "fault", "on": "reset",          "cases": [ { "to": "idle" } ] },
    { "from": "fault", "on": "power-on",       "cases": [ { "refuse": "in-fault" } ] },
    { "from": "fault", "on": "power-off",      "cases": [ { "refuse": "in-fault" } ] },
    { "from": "fault", "on": "reading",        "cases": [ { "stay": true } ] },
    { "from": "fault", "on": "window-changed", "cases": [ { "stay": true } ] },
    { "from": "fault", "on": "sensor-lost",    "cases": [ { "stay": true } ] }
  ]
}
