Another Approach to State

The OpenLaszlo platform contains a <state> tag. A <state> encapsulates a collection of properties (methods and attributes) and children that can be atomically attached to or removed from a node during program execution. This is actually pretty powerful, especially since a state can contain constraints. This is how dragging is implemented, for instance.

OpenLaszlo <state>s are independent from each other. Any number of <state>s may be applied to a view at once, or none.

In problem domains, some states really do combine this way. For example, a view may be being dragged, or not, independently of whether it contains unsaved data or not; and so on.

But there's another sense of "state", which is the state in a state machine. These states are mutually exclusive[1]. And they're a pain to implement with the <state> tag. You end up writing a code that turns on one state and turns off the others, in order to switch from one state-machine-state to another. Note the verbosity of the three state-changing lines in the fragment below:
[code]

...
...
..

...s1.apply(); s2.remove(); s3.remove(); ...
...s1.remove(); s2.apply(); s3.remove(); ...
...s1.remove(); s2.remove(); s3.apply(); ...

[/code]

Here's the cool thing, though. You can turn the states on and off automatically with constraints. Just add an attribute that names the state, and make the states conditional on the value of that attribute[2]:
[code]

...
...
...

...this.setAttribute('myState', 's1'); ...
...this.setAttribute('myState', 's2'); ...
...this.setAttribute('myState', 's3'); ...
[/code]

In fact, if the states are only used to hide and show views and layouts (and not to attach methods and attributes), you don't even need the <state> tag at all. You can simply conditionalize the views' visibility on the value of the attribute that holds the state:
[code]
...

3 Responses to “Another Approach to State”

  1. Oliver Steele » Blog Archive » Ruby and Laszlo Says:

    [...] Update: Now that I’ve done more Ruby and DHTML programming, I can see that the diagram above gives OpenLaszlo short shrift. Although OpenLaszlo is lower level that Ruby with respect to code generation and a MOP, the use of databinding and constraints makes it higher level in a different set of ways. [...]

  2. Candide Says:

    Hi,

    You approach to state is very interesting. I’m putting it into practice in my application. Only, I got confused by the attribute accessing subtleties that I had to face:

    (article not saved yet)

    (…)

    Debug.write(”saveArticleContent: ” + articleContent);
    if (this.getAttribute(”articlestatus”)==”new”) {
    Debug.write(”We should prompt the user with the name of the new article”);
    storycontrols.setAttribute(”mystate”, “createarticlestate”);
    }

    Note that this.mystate in the state constraints didn’t work. I understood that “this” inside the constraint was pointing to the same thing as storycontrols, but that’s apparently not the way the compiler understands it.

    Similarly I don’t understand why I couldn’t simply assign the “mystate” attribute using the “=” operator in the “saveArticleContent” method (which is a member of storyControls).

    Any help is really appreciated!

  3. Candide Says:

    Sorry I wanted to send code snippets along with my comment but that didn’t work. I you let me know your email address, I’d be glad to send it to you.