The idea here is to avoid having to code separate
controller classes for cases where SimpleController
won't work. Basically, using BSF, you can embed the
logic for determining which transition to take directly
in the application XML. Here's an example from the
DemoApp:
<Event name="Login"
controllerName="org.jstatemachine.controller.BSFController">
<StateTransition entryCondition="DEFAULT"
resultStateId="LoginFailed" name="LoginInvalid">
<UserProperties>
<UserProperty key="Condition"
value="authentication fails" />
<UserProperty key="language" value="jython" />
<UserProperty key="expression"
value="request.request.getParameter(&apos;loginName&apos;)
!= &apos;cfelaco&apos;" />
</UserProperties>
</StateTransition>
<StateTransition entryCondition="DEFAULT"
resultStateId="AuthenticatedArea" name="LoginValid">
<UserProperties>
<UserProperty key="Condition"
value="authentication passes" />
<UserProperty key="language" value="jython" />
<UserProperty key="expression"
value="request.request.getParameter(&apos;loginName&apos;)
== &apos;cfelaco&apos;" />
<UserProperty key="side-effect"
value="requestContext.getUserSession().addAuthToken(&apos;user&apos;)"
/>
</UserProperties>
</StateTransition>
</Event>
For each transtion, you add 2, possibly 3 user
properties: language, expression and side-effects. For
each transition, the BSFController evaluates the
expression using the BSFEngine for the given language.
If the result of the expression is true (in a loose
scriptable sense), then the transition is taken. The
"side-effect" script is executed if the transition is
taken.
An alternate, simpler method would be to just associate
an expression with the event itself that would return
the appropriate transition. There are pros and cons to
both approaches. The transition based approach is nice
because you could possibly embed the expression in the
graphical state chart.
If this feature is put in, you should also extend the
<UserProperty> implementation to allow the value to be
specified as a text element rather than an attribute.
This would make it possible to embed multi-line scripts
as CDATA elements.
I have a rough working version of the BSFController
class if you're interested.
Logged In: YES
user_id=151201
Chris,
That looks like it could be a very useful addition to the project. If you could
forward me the code for a look I'd be very grateful. (Either email it to me or
attach it to this feature request.)
Having worked on another approach based on building generic expressions
for gaurd conditions, I know one problem with the idea is ensuring only one
gaurd condition is true at a time, or ensuring they are evaluated in a given
order. (Again, ensuring the result is unambiguous ;-) )
I've just added to my list an item 'Change the apploader to preserve
transition ordering'...
There are a great many ways to write controllers. I think what I may do with
this is create a repository on the jstatemachine.org site for Controllers or
controller patterns as they are developed and submitted by people like
yourself.
Thanks,
Brian.