Statestep Tutorial

Using the Constraint Table

The constraint table is a kind of logic spreadsheet which helps you to specify and to see at a glance what combinations of conditions are possible.

An example of a constraint is: "If there's snow then it must be cold outside." A formula can be used to express this more precisely:

Weather = {snow} => Temperature = {cold}

Here, the symbol "=>" means "implies".

However, the easiest way to express simple constraints like this is to use the constraint table. Open the file "activities.s2" from the "samples" subdirectory of the distribution. Now click on the Model menu and open the constraint table window. You'll see a mostly empty table; the values in the shaded (grey) cells are filled in automatically. Looking down the Weather column of the table, find the cell containing the value snow. In the empty cell to the left of this (in the Temperature column), right-click and select the value cold.

Single constraint

You've just defined the constraint mentioned above: if Weather is snow then Temperature must be cold. Every unshaded cell in the table allows you to express a constraint of this kind: the "if" part of the constraint is always given by the shaded cell on the same row.

Leaving a cell empty has the same effect as selecting "don't care" in that cell. One way of making these "don't cares" explicit is to right-click on any cell, click Display in the menu and select Show inferences (or skip the menu and just press Ctrl-I). Here's what you should now see:

Derivation screenshot

Most of the empty cells now just have a '*' in them. However, the program has also made some inferences: from the constraint we've already given it, it has deduced that Weather cannot be snow if Temperature is either hot or warm; it uses a different colour (orange) to alert us to the fact that snow is impossible in these cases.

Let's enter the following two constraints into the table:

Weather = {snow} => Wind = {breeze | calm}

Temperature = {cold} => Wind = {breeze | gale}

The character '|' means "or": so, for the first of these constraints, you should select both the values breeze and calm for Wind in the row where snow is in a shaded cell. The constraint table should now look like this:

Contradiction screenshot

Even though we've said that Wind can be calm when Weather is snow, the value calm is marked impossible in this case. Why? Right-click in the cell where calm is coloured orange and select Explain from the menu...

Explanation screenshot

Considered together, these two constraints imply that if Weather is snow then Wind is limited to breeze or gale; it cannot be calm.

Tip: Words coloured blue are links: if you click on a constraint in the explanation then the constraint table will be displayed with the appropriate table cell selected.

The constraint table offers a systematic way of identifying all possible constraints between pairs of variables: just enter something in every cell in the table, either a list of one or more values or "don't care". When there are no green cells left you know you've considered all the possibilities.

Note that there's some redundancy in the table: it's actually necessary only to fill in either all the cells above and to the right of the shaded cells or those below and to the left of them; unless you've made a mistake, the derived contents of the empty cells on the other side of the diagonal will then be as you expect. If we had been filling in cells on only one side of the diagonal up to now then our table might now look like this:

Using only top-right half

Tip: A quick way of clearing a group of cells is to select those cells and press the space key; to set the contents of the selected cell(s) to "don't care", press 'X' instead.

Of course, you might also wish to express constraints that involve more than two variables. As we'll see later, such a constraint can always be written as a formula.

Next: Rules and Constraints