Live Sequence Charts for Mermaid

A step-by-step tutorial. No background needed.

Part 2: From stories to rules · Lesson 11 of 20

Facts and checks

Charts can check facts along the way. Is there oat milk left? Has the order been paid? Here is a rule for oat lattes that uses both:

Drawing…
Chart text — try changing itOpen in playground

Facts: variables

A variable is a named fact that can change, like the number of oat milk cartons. Declare it near the top with var:

var barista.oatCartons: int[0..20]
var till.paid: bool

barista.oatCartons belongs to the barista: it's something the barista knows. After the colon comes the variable's type, the kind of value it holds:

Type Holds Example
bool true or false var till.paid: bool
int a whole number var queue: int
int[0..20] a whole number in a range var barista.oatCartons: int[0..20]
{a, b, c} one of a list of words var barista.milk: {dairy, oat, none}

A variable can also belong to nobody in particular: var queue: int.

Checks: conditions

A condition is a check, drawn as a six-sided box on the lines of the participants it names. After condition, list the participants, then a colon, then what must be true.

condition barista, till: paid is shared by the barista and the till, so its box spans both lines. A shared condition makes everyone involved meet at it: the condition is checked once both have reached it, and neither moves on before it has been checked. Here, that means the barista can't hand over the latte until the till shows the order as paid.

Just paid is short for paid = true, because paid is a bool. With a list of words, the word alone works the same way: oat is short for milk = oat.

Hot and cold checks

Conditions have a temperature, like everything else:

  • A hot condition must be true when it is reached. If it is false, the rule is broken. It's drawn with a solid outline, and it's the usual kind.
  • A cold condition is a question. If it is false, the chart ends quietly at that point, and nothing more is required. It's drawn with a dashed outline.

In "Oat lattes", cold condition barista: oatCartons > 0 says: if the café is out of oat milk, this rule has nothing more to say. Without the cold check, an oat latte order with no oat milk left would break the rule.

Writing checks

Checks can use:

  • comparisons: =, != (not equal), <, <=, >, >=;
  • arithmetic: +, -, *, /;
  • and, or and not, with brackets to group: not (paid or free);
  • numbers, true and false, and the words from a {…} type.

A check can also be plain words in double quotes, such as condition barista: "the cup is not cracked". The page draws it as written and doesn't check it. That's useful when the fact is hard to put into variables.

Try it

Add a variable var barista.milk: {dairy, oat, none} and replace the oat check with a cold check that the milk is oat.

Show a model answer
Drawing…
Chart text — try changing itOpen in playground

cold condition barista: oat means the same thing.

Check your understanding

A customer pays, but the till fails to record it, so paid is false at the shared check. What does the rule say should happen? What would it say if the check were written cold condition barista, till: paid?

Show the answer

As written, the check is hot, so a false paid breaks the rule: the café has taken money for an order it can't account for. That is exactly the kind of failure the rule is there to catch.

With a cold check, a false paid ends the rule quietly. The barista wouldn't have to hand over the latte, and nothing would count as wrong. A cold check suits situations that are acceptable; a hot check suits ones that must never occur.