Monday, December 15, 2008

The New Upstart State Machine, Part 2

Continued from here.

With some understanding of states, we can now explain dependencies.

A dependency simply says "state foo cannot be up unless state bar is also up." In this post I reflect this relationship with an arrow (→) which can be read "depends on" (or "implies").

mystate(crunchy: yes)→thystate(crunchy: yes, id: 7)

In the above dependency, mystate(crunchy: yes) can only be up if thystate(crunchy: yes, id: 7) is up. Take note, mystate(crunchy: no) is not effected by this dependency, as you might expect, but neither is mystate(crunchy: yes, delicious: definitely). Remember, different parameters = different states. thystate in the above example is matched with equal strictness.

Defining dependencies in this manner is rarely useful. Usually we want to define dependencies between large classes of states, not individual ones. For example, suppose we wanted the above dependency to be satisfied for any mystate and thystate with equivalent levels of crunchiness:

mystate(crunchy: %1)→thystate(crunchy: %1, id: 7)

In this notation mystate(crunchy: yes) would depend on thystate(crunchy: yes, id: 7), and mystate(crunchy: sometimes) would depend on thystate(crunchy: sometimes, id: 7), etc.

Suppose we want thystate to have an id, but don't care about its value.

mystate(crunchy: %1)→thystate(crunchy: %1, id: %2)

Now the id parameter is set to match a pattern which doesn't appear anywhere else, meaning it can accept any value.

Its also possible we might have some leeway in what we depend on. Perhaps any thystate with the two parameters we match will serve our purpose.

mystate(crunchy: %1)→thystate(crunchy: %1, id: %2, *)

Now thystate(crunchy: yes, id: 7, crap: 3, morecrap:5) would satisfy mystate(crunchy: yes)'s dependency as well as its more minimal forms above.

Finally, we will occasionally need to specify a parameter that may or may not be present. We use ? to denote this:

mystate(crunchy: %1)→thystate(crunchy?: %1, id: %2, *)

This has interesting meaning: thystate(crunchy: yes, id: 7) will satisfy the dependency for mystate(crunchy: yes), but thystate(id: 7) will satisfy the dependency for mystate(crunchy: anything).

I'm trying to make this as sparse as I know how, and in the process a lot of it feels stranger for the deprivation of context, but hopefully in the coming days, a few more people will know what the hell I'm on about :D

0 comments: