State Machine discord as system error detection

October 24, 2011

It takes three subsystem state machines to create an error adaptive system.

This morning in the shower I was thinking about my last post on SCXML. What do you use as states in a system? Easy question, but then I thought, sure its internal states of a process, but how does the external “real” come into the picture, via guards? That could get messy in a very complex real world system. So, what if the external system is also modeled as a state machine?

Now we have two state machines, internal system and external system. What is the advantage? Well, each can have a peak into the other or using historical information, compute how the other works. Then if this prediction is not matched, it is an error. That is, if the internal machine is at state x and it knows that the external machine should be at state y at this point, and it is not, it is a system error. What to do?

This is where a third error machine would come into use. The function of this machine is to bring the other two into resonance, reduce the entropy, heat.

Depending on whether the machines are Mealy or Moore based, one would detect errors at particular points, events, transitions, or states.

I remember reading years ago about fault tolerant hardware systems. The minimum components was three; similar.

Hmmm. I don’t know what everything above really means, if anything. :)

For future thinking …


ATi Radeon 7970 [CAD Animation Benchmark]

Using Commons SCXML with Groovy

October 16, 2011

The Watch example is rewritten using Groovy. The actual state machine document is created using Groovy‘s MarkupBuilder.

More then a decade ago I used a state machine approach to develop a web application framework. It worked, which surprised me most of all, lol! Since then I’ve wondered why there is no generic reuseable facility to use this approach.

SCXML is “…a general-purpose event-based state machine language that combines concepts from CCXML and Harel State Tables”. It would seem to fit the bill.

Many applications would probably benefit from having the control flow managed by some kind of flow engine. Sometimes we wind up with the internal object’s implicit FSM mixed into the application FSM in tangles of confusion and brittleness.

Modern software engineering is just the use of more ingenious complex frameworks for obfuscating the finite state machines really being created. This is especially true in the Java world, where there is a tendency toward frameitis.

Listing one below is a version of the Watch example presented on the Commons SCXML site. Instead of subclassing the AbstractStateMachine, this version uses the SCXMLExecutor and extends SCXMLListener. The listener allows the engine to invoke methods on the demo class by setting itself as the invoker: executor.registerInvokerClass(“watch”,getClass());

Note: The Groovy Grab will not work in Eclipse. I just put the required jars in the build path. The grab will work in the command line.

Listing one

// file: ScxmlDemo.groovy
import groovy.xml.*
import groovy.grape.*

import java.awt.datatransfer.Transferable;
import java.lang.reflect.Method
import org.apache.commons.scxml.*
import org.apache.commons.scxml.env.*
import org.apache.commons.scxml.invoke.*
import org.apache.commons.scxml.model.*
import org.apache.commons.scxml.env.jsp.*


 * Demo of 
 * "...State Chart XML (SCXML), which is a general-purpose 
 * event-based state machine language that combines 
 * concepts from CCXML and Harel State Tables."
 * Invocation:
 *   groovy src\ScxmlDemo.groovy
 * @author jbetancourt
public class ScxmlDemo implements SCXMLListener {
	SCXMLExecutor executor
	def namespace = ""
	Map methods = [:]
	enum Trans{
		String name;
		public Trans(String s){
			name = s

	/**   */
	public ScxmlDemo(){
				getDeclaredMethod(it, new Class[0]))

	/** Instead of writing XML, we use a DSL builder to create it */
	def createScxmlDoc(){
		def writer = new StringWriter()
		def builder = new MarkupBuilder(writer)


		new File("sm.xml").setText(writer.toString())

	/**   */
	public void execute(){
		def eh = new
		SCXML model = SCXMLParser.parse(
				new File("sm.xml").toURI().toURL(), eh)

		executor = new SCXMLExecutor()
		executor.setRootContext(new SimpleContext())
		executor.setEvaluator(new ELEvaluator())
		executor.setErrorReporter(new SimpleErrorReporter())
		executor.setEventdispatcher(new SimpleDispatcher())
		executor.addListener(model, this)


		def status = executor.getCurrentStatus().
		status.each{ println "Now state is: ${}" }

	/**  */
	public void fireEvent(name){
		def evts = [
			new TriggerEvent(name,
			TriggerEvent.SIGNAL_EVENT, null)

		executor.triggerEvents(evts as TriggerEvent[])

	/**  */
	def invoke(id){
		try {
			def name = 'do' + id.substring(0,1).
				toUpperCase() + id.substring(1)
			def method = methods[name]
			method.invoke(this, new Object[0]);
			return true
		}catch(Exception ex){
		return false

	/**  */
	public void onEntry(
	final TransitionTarget entered) {

	 * @param from The "source" transition target.
	 * @param to The "destination" transition target.
	 * @param transition The transition being followed.
	public void onTransition(
	   final TransitionTarget from,
	   final TransitionTarget to,
	   final Transition transition) {
		// nothing to do

	 * @param exited The transition target being exited.
	public void onExit(final TransitionTarget exited) {
		// nothing to do

	// the activities
	def show(s){println "I'm '$s' ..."}
	public void doReset() {show('reset')}
	public void doRunning() {show('running')}
	public void doPaused() {show('paused')}
	public void doStopped() {show('stopped')}

	public static main(args){
		def main = new ScxmlDemo()

Example run:

groovy src\ScxmlDemo.groovy
I'm 'reset' ...
I'm 'running' ...
Now state is: running

Presented was an example of implementing the Watch example application using the Groovy programming library. The Groovy MarkupBuilder gives a “better” language then XML for creating the state machine document.

SCXML can be the basis of a State-Oriented Programming approach.


Further Reading


My Funny Valentine / Bill Evans & Jim Hall


Get every new post delivered to your Inbox.