document.write('<div class="groovy" style="font-family: monospace;"><br />');
document.write('<span style="color: #808080; font-style: italic;">/**<br />');
document.write('* GOAL: Write a quick-and-dirty DSL in Groovy that emulates Bruce Tate\'s <br />');
document.write('*&nbsp; &nbsp; &nbsp; &nbsp;example &quot;acts_as_statemachine&quot; Ruby code at&nbsp; <br />');
document.write('*&nbsp; &nbsp; &nbsp; &nbsp;http://www-128.ibm.com/developerworks/java/library/j-cb03137/index.html,<br />');
document.write('*&nbsp; &nbsp; &nbsp; &nbsp;within 20 minutes or less (i.e. steer clear of MOP-magic engineering ;).<br />');
document.write('*&nbsp; &nbsp; <br />');
document.write('* For more context, see the Apr\'07 &quot;Symbols&quot; thread on the groovy-user mailing<br />');
document.write('* list.<br />');
document.write('*/</span><br />');
document.write('<br />');
document.write('<span style="color: #808080; font-style: italic;">////////////////////////////////////////////////////////////</span><br />');
document.write('<span style="color: #808080; font-style: italic;">// Initialize</span><br />');
document.write('<span style="color: #808080; font-style: italic;">////////////////////////////////////////////////////////////</span><br />');
document.write('<a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span style="color: #000000; font-weight: bold;">def</span></a> fsm = <a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20new"><span style="color: #000000; font-weight: bold;">new</span></a> StateMachine<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />');
document.write('fsm.<span style="color: #006600;">current_state</span> = <span style="color: #ff0000;">\'submitted\'</span>&nbsp; <span style="color: #808080; font-style: italic;">// pretend to look up something with state from dBase</span><br />');
document.write('<br />');
document.write('<span style="color: #808080; font-style: italic;">////////////////////////////////////////////////////////////</span><br />');
document.write('<span style="color: #808080; font-style: italic;">// Define</span><br />');
document.write('<span style="color: #808080; font-style: italic;">////////////////////////////////////////////////////////////</span><br />');
document.write('fsm.<span style="color: #006600;">state</span> <span style="color: #ff0000;">\'submitted\'</span><br />');
document.write('fsm.<span style="color: #006600;">state</span> <span style="color: #ff0000;">\'processing\'</span><br />');
document.write('fsm.<span style="color: #006600;">state</span> <span style="color: #ff0000;">\'nonprofit_reviewing\'</span><br />');
document.write('fsm.<span style="color: #006600;">state</span> <span style="color: #ff0000;">\'accepted\'</span><br />');
document.write('<br />');
document.write('fsm.<span style="color: #006600;">event</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">\'accept\'</span>, <span style="color: #66cc66;">&#91;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />');
document.write('&nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>from:<span style="color: #ff0000;">\'processing\'</span>,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; to:<span style="color: #ff0000;">\'accepted\'</span><span style="color: #66cc66;">&#93;</span>,<br />');
document.write('&nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>from:<span style="color: #ff0000;">\'nonprofit_reviewing\'</span>,&nbsp; &nbsp;to:<span style="color: #ff0000;">\'accepted\'</span><span style="color: #66cc66;">&#93;</span><br />');
document.write('<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><br />');
document.write('<br />');
document.write('fsm.<span style="color: #006600;">event</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">\'receive\'</span>, <span style="color: #66cc66;">&#91;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />');
document.write('&nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>from:<span style="color: #ff0000;">\'submitted\'</span>,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; to:<span style="color: #ff0000;">\'processing\'</span><span style="color: #66cc66;">&#93;</span><br />');
document.write('<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><br />');
document.write('fsm.<span style="color: #006600;">event</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">\'send_for_review\'</span>, <span style="color: #66cc66;">&#91;</span><br />');
document.write('&nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>from:<span style="color: #ff0000;">\'processing\'</span>,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;to:<span style="color: #ff0000;">\'nonprofit_reviewing\'</span><span style="color: #66cc66;">&#93;</span>,<br />');
document.write('&nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>from:<span style="color: #ff0000;">\'nonprofit_reviewing\'</span>,&nbsp; to:<span style="color: #ff0000;">\'processing\'</span><span style="color: #66cc66;">&#93;</span>,<br />');
document.write('&nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>from:<span style="color: #ff0000;">\'accepted\'</span>,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; to:<span style="color: #ff0000;">\'nonprofit_reviewing\'</span><span style="color: #66cc66;">&#93;</span><br />');
document.write('<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><br />');
document.write('fsm.<span style="color: #006600;">event</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">\'revise\'</span>, <span style="color: #66cc66;">&#91;</span><br />');
document.write('&nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>from:<span style="color: #ff0000;">\'accepted\'</span>,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; to:<span style="color: #ff0000;">\'processing\'</span><span style="color: #66cc66;">&#93;</span><br />');
document.write('<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><br />');
document.write('<a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20println"><span style="color: #993399;">println</span></a> fsm <br />');
document.write('<a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20println"><span style="color: #993399;">println</span></a> <span style="color: #ff0000;">\'----------\'</span><br />');
document.write('<a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20println"><span style="color: #993399;">println</span></a> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />');
document.write('&nbsp; &nbsp; <br />');
document.write('<span style="color: #808080; font-style: italic;">////////////////////////////////////////////////////////////</span><br />');
document.write('<span style="color: #808080; font-style: italic;">// Use:</span><br />');
document.write('<span style="color: #808080; font-style: italic;">////////////////////////////////////////////////////////////&nbsp; &nbsp; </span><br />');
document.write('<a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20println"><span style="color: #993399;">println</span></a> fsm.<span style="color: #006600;">current_state</span><br />');
document.write('<a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20assert"><span style="color: #000000; font-weight: bold;">assert</span></a>&nbsp; fsm.<span style="color: #006600;">current_state</span> == <span style="color: #ff0000;">\'submitted\'</span><br />');
document.write('fsm.<span style="color: #006600;">fire</span> <span style="color: #ff0000;">\'receive\'</span><br />');
document.write('fsm.<span style="color: #006600;">fire</span> <span style="color: #ff0000;">\'accept\'</span><br />');
document.write('<a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20println"><span style="color: #993399;">println</span></a> fsm.<span style="color: #006600;">current_state</span><br />');
document.write('<a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20assert"><span style="color: #000000; font-weight: bold;">assert</span></a>&nbsp; fsm.<span style="color: #006600;">current_state</span> == <span style="color: #ff0000;">\'accepted\'</span><br />');
document.write('<br />');
document.write('<br />');
document.write('<br />');
document.write('<br />');
document.write('<span style="color: #808080; font-style: italic;">/** A list/map-based implementation of a StateMachine DSL-ish thingy */</span><br />');
document.write('<a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20public"><span style="color: #000000; font-weight: bold;">public</span></a> <a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20class"><span style="color: #000000; font-weight: bold;">class</span></a> StateMachine <span style="color: #66cc66;">&#123;</span>&nbsp; &nbsp; <br />');
document.write('&nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span style="color: #000000; font-weight: bold;">def</span></a> current_state&nbsp; =&nbsp; <span style="color: #ff0000;">\'initial\'</span>&nbsp; <span style="color: #808080; font-style: italic;">// literals are intern()ed automatically</span><br />');
document.write('&nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span style="color: #000000; font-weight: bold;">def</span></a> valid_states&nbsp; &nbsp;= <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">\'initial\'</span><span style="color: #66cc66;">&#93;</span><br />');
document.write('&nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span style="color: #000000; font-weight: bold;">def</span></a> transitions&nbsp; &nbsp; = <span style="color: #66cc66;">&#91;</span>:<span style="color: #66cc66;">&#93;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// a LUT from events to defined transitions</span><br />');
document.write('&nbsp; &nbsp; <br />');
document.write('&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/** add a new valid state to the machine */</span><br />');
document.write('&nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span style="color: #000000; font-weight: bold;">def</span></a> state = <span style="color: #66cc66;">&#123;</span>symbolic_name -&gt; valid_states&lt;&lt;symbolic_name; valid_states.<span style="color: #006600;">unique</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span><br />');
document.write('&nbsp; &nbsp; <br />');
document.write('&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/** define $transitions, which maps from (state,event) --&gt; state */</span><br />');
document.write('&nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span style="color: #000000; font-weight: bold;">def</span></a> event<span style="color: #66cc66;">&#40;</span>event,tuples<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; tuples.<a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20each"><span style="color: #663399;">each</span></a> <span style="color: #66cc66;">&#123;</span> transition -&gt;<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20assert"><span style="color: #000000; font-weight: bold;">assert</span></a> valid_states.<a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20contains"><span style="color: #CC0099;">contains</span></a><span style="color: #66cc66;">&#40;</span>transition.<span style="color: #006600;">from</span><span style="color: #66cc66;">&#41;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20assert"><span style="color: #000000; font-weight: bold;">assert</span></a> valid_states.<a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20contains"><span style="color: #CC0099;">contains</span></a><span style="color: #66cc66;">&#40;</span>transition.<span style="color: #006600;">to</span><span style="color: #66cc66;">&#41;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span style="color: #000000; font-weight: bold;">def</span></a> trigger = <span style="color: #66cc66;">&#91;</span>event:event, from:transition.<span style="color: #006600;">from</span><span style="color: #66cc66;">&#93;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transitions<span style="color: #66cc66;">&#91;</span>trigger<span style="color: #66cc66;">&#93;</span> = transition.<span style="color: #006600;">to</span>&nbsp; &nbsp; &nbsp; &nbsp; <br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>&nbsp; &nbsp; <br />');
document.write('&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />');
document.write('&nbsp; &nbsp; <br />');
document.write('&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/** have the machine process an event */</span><br />');
document.write('&nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span style="color: #000000; font-weight: bold;">def</span></a> fire <span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span style="color: #000000; font-weight: bold;">def</span></a> context = <span style="color: #66cc66;">&#91;</span>event:event, from:current_state<span style="color: #66cc66;">&#93;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; <br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//BUG: Map::find() doesn\'t work with two-param Closures?</span><br />');
document.write('<span style="color: #808080; font-style: italic;">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;def transition = transitions.find&nbsp; &nbsp; {trigger,_ -&gt; trigger==context }</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span style="color: #000000; font-weight: bold;">def</span></a> transition = transitions.<a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20findAll"><span style="color: #663399;">findAll</span></a> <span style="color: #66cc66;">&#123;</span>trigger,_ -&gt; trigger==context <span style="color: #66cc66;">&#125;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20if"><span style="color: #b1b100;">if</span></a> <span style="color: #66cc66;">&#40;</span>transition<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_state = transition<span style="color: #66cc66;">&#91;</span>context<span style="color: #66cc66;">&#93;</span>&nbsp; <span style="color: #808080; font-style: italic;">// findAll() version</span><br />');
document.write('<span style="color: #808080; font-style: italic;">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_state = transition.value&nbsp; &nbsp; &nbsp;// find() version</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>&nbsp; &nbsp; &nbsp;<br />');
document.write('&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />');
document.write('&nbsp; &nbsp; <br />');
document.write('&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/** pretty-print the states and their defined transitions */</span><br />');
document.write('&nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20public"><span style="color: #000000; font-weight: bold;">public</span></a> <a target="_blank"&nbsp; href="http://www.google.de/search?as_q=String&num=100&hl=en&as_occt=url&as_sitesearch=java.sun.com%2Fj2se%2F1.5.0%2Fdocs%2Fapi%2F"><span style="color: #aaaadd; font-weight: bold;">String</span></a> toString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span style="color: #000000; font-weight: bold;">def</span></a> result = <span style="color: #ff0000;">&quot;&quot;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; valid_states.<a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20each"><span style="color: #663399;">each</span></a> <span style="color: #66cc66;">&#123;</span> state -&gt;<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result <a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20&amp;lt;"><span style="color: #b1b100;">&lt;&lt;</span></a>= <span style="color: #ff0000;">&quot;state ${state}:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>&nbsp; <span style="color: #808080; font-style: italic;">// using &lt;&lt;= to coerce to StringBuffer</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transitions.<a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20each"><span style="color: #663399;">each</span></a> <span style="color: #66cc66;">&#123;</span> transition -&gt;<br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span style="color: #000000; font-weight: bold;">def</span></a> from&nbsp; = transition.<span style="color: #006600;">key</span>.<span style="color: #006600;">from</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span style="color: #000000; font-weight: bold;">def</span></a> event = transition.<span style="color: #006600;">key</span>.<span style="color: #006600;">event</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span style="color: #000000; font-weight: bold;">def</span></a> to&nbsp; &nbsp; = transition.<span style="color: #006600;">value</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20if"><span style="color: #b1b100;">if</span></a> <span style="color: #66cc66;">&#40;</span>from==state <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>result&lt;&lt; <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span> |-- ${event} --&gt; ${to}<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">&#125;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />');
document.write('&nbsp; &nbsp; &nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20return"><span style="color: #000000; font-weight: bold;">return</span></a> result<br />');
document.write('&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />');
document.write('&nbsp; &nbsp; <br />');
document.write('<span style="color: #66cc66;">&#125;</span><br />');
document.write('&nbsp;</div>');
document.write('<br />&nbsp;<br /><div style="font-size: 12px">Brought to you by the community at <a href="http://www.bytemycode.com/snippets/snippet/639/1/" target="_blank">byteMyCode</a>.</div>');

