processi

about processes and engines

state machine != workflow engine

update (2010) : this resource lifecycle post features two quotes that are enlightening when thinking about state and workflow.

update (2011) : a discussion between two engineers about state machines and workflow (guest post at Engine Yard)

This post is intended for Ruby developers. The idea for it came after numerous discussions with fellow programmers about state machines and workflow engines.

What motivates me for posting is the publication of the 3rd issue of the excellent Rails Magazine. It contains an article named “Workflow solutions with AASM”.

At first a word of warning, I wrote these lines with no intention of minimizing the potential of state machines vs workflow engines. I’m listing workflow engine and state machine implementations for Ruby at the end of this post.

There are a number of open questions in this post, I don’t intend to answer them now or later. There are so many different use cases in the wild.

spark

The article “Workflow solutions with AASM” starts with :

There are two main forms of workflows: sequential and
state-machine. Sequential workflows are predictable. They
utilize the rules and conditions we provide at the beginning
to progress a process forward. The workflow is in control of
the process.

A state-machine workflow is the reverse. It is driven by
external events to its completion. We define the states and
required transitions between those states. The workflow sits
and waits for an external event to occur before transitioning
to one of the defined states. The decision making process hap-
pens externally outside of the workflow – there is a structure
to be followed still like a sequential workflow but control is
passed to its external environment.

I’m clearly in the “sequential workflow” faction. But I refute the ‘sequential’ label. Most of the state/transitions set out there can be qualified as “sequential”. Any workflow engine, in order to present any interest to its users, has to implement a certain number of [control flow] workflow patterns. The number 1 control-flow pattern is named Sequence, granted. But if you take a look at the pattern that immediately follows, it’s Parallel Split. I can’t adhere to the “sequential workflow” appellation.

Note that most workflow engines (you can call then “sequential workflow” engines) strive to implement a large set of those control-flow patterns. This is usually done via their ‘process definition’ language. The goal is to let users express their scenarii / pattern in a not too verbose way. We can argue that state machines may implement any of the control-flow patterns, my guess is ‘yes’, but what’s the cost in code ?

The difference between “sequential workflows” and “state machines” seems to lie not in the predictability of the former, but rather in how the flow between activities or state is weaved, especially how/where it’s concentrated.

Are “sequential workflows” and “state machines” excluding one another ?

The article states “sequential workflow are predictable. They utilize the rules and conditions we provide at the beginning”. It’s the same for state machines. The vast majority of [Ruby] state machine implementations rely on behaviour specified at implementation time (or right before the application’s [re]start).

The second paragraph I quoted up here says that “decision making process happens externally outside of the workflow (…) control is passed to its external environment”. I argue that a “sequential workflow” engine should do the same. A workflow [engine] can’t take a decision by itself, this task is usually handled by a human or a dedicated algorithm (a piece of code, a rule engine, a coin tossing engine wired to internet, whatever).

case

Wait, we’ve been using this ‘workflow’ term quite liberally until now, what is it about ? Why do state machines seem to be the perfect vessel for it, at least in the eyes of the hard-working developer ?

Let’s look at how state machines are sold, at their use cases. Here is a classical example from a [Rails-oriented] state machine library :

  class Document < ActiveRecord::Base
    include StateFu

    def update_rss
      puts "new feed!"
      # ... do something here
    end

    machine( :status ) do
      state :draft do
        event :publish, :to => :published
      end

      state :published do
        on_entry :update_rss
        requires :author  # a database column
      end

      event :delete, :from => :ALL, :to => :deleted do
        execute :destroy
      end
    end
  end

It attaches behaviour (state and transitions) to a resource. If you look at the example of any Ruby state machine library out there, you will see this pattern : a set of states and transitions attached to a resource in the system (a Model).

Let’s move to an adjacent example, it’s also about a document, but it’s in the “sequential workflow” faction (ruote). (The ‘cursor’ expression in the process definition allows for the flow to be rewound or skipped…) :

Ruote.process_definition :name => 'doc review', revision => '0.2' do

  cursor do

    participant '${f:author}', :step => 'finalize document'
    participant '${f:review_team}', :step => 'review document'

    rewind :unless => '${f:approved}'

    concurrence do
      participant '${f:publisher}', :step => 'publish document'
      participant '${f:author}', :step => 'publication notification'
    end
  end
end

There is a document yes, but it could be a folder of documents. The process definition is a separate thing, meant to be turned into multiple process instances.

Workflow engines are mostly process definitions interpreters, graduating as “operating systems for business processes” (think ‘ps’ and ‘kill dash 9′ but with business processes / process instances).

As said, most of the Ruby state machine libraries out there are all about binding behaviour to resources / models. What about moving to the workflow engine camp and instead of binding to a system artefact (document, order, invoice, book, customer, …) why not attach state and transitions to a virtual artefact, a so-called “process instance” ? The state machine would move out of its shell and turn into a full workflow engine. Or is that so ?

cases

Back to the 90% of the cases : the state machine attached to a model. What if there is more than 1 “stateable” model ? Easy. But what if transition in model A provokes a transition in model B ? The “real workflow” now lies at the intersection of two models.

The workflow engine will expose a process definition while, with the state machines, we’ll have to extract it from two or more models. Another advantage of workflow engines is that they have process definition versioning. Most of them run process instances from process definition X at version 2 happily with process instances from version 4, concurrently.

On the other hand, when a process instance goes ballistic it might take some time to repair it (align it with the reality of the business process). It might be easier with state machines, a simple SQL update usually, but what if there are multiple inter-related behaviours ? Pain on both sides.

Whatever the tool, you’ll have to carefully avoid locking yourself into your private hell of a system.

nuances

Kenneth Kalmer is using a mix of state_machine and ruote in their ISP in a box product. The state machine library is in charge of the state of its various models while the workflow engine does the orchestration. “Processes that drives the provision of services”

The two techniques may co-exist. Tinier states / transitions sets, more concise process definitions. Models managing their states, process instances coordinating the states of multiple models.

It’s OK to remove the workflow engine and replace it with a “super” state machine, but then you’re entering in the real workflow territory and the mindset is a bit different and at that level, you’ll be exposed to a flavour of change that directly involves your customer / end-user. Nowhere to hide.

machines

I’ve been linking profusely to my ruote workflow engine. The only other workflow engine I’ve spotted in the wild is rbpm, but its development stopped a long time ago.

Here is a non-exhaustive list of Ruby state machine implementations, in no specific order :

There’s a plethora of state machines versus very few (one or two) workflow engines. This could be interpreted in multiple ways.

State machines follow a well defined pattern, while workflow engines have to follow a set of patterns, and workflow engines strive to be “operating systems for business processes”. State machines are more humble.

So Rails Magazine #3 is out. Great read.

Written by John Mettraux

July 3, 2009 at 2:48 pm

12 Responses

Subscribe to comments with RSS.

  1. Hi John

    thanks for pointing out the differences between a state machine and workflow.

    The context of the article is to introduce the reader , albeit at an introductory level, how to utilise a plugin such as AASM. Perhaps the terminologies used may not be the best but I think personally if you do write an article in the magazine, there is a time element involved and it is not always possible to cover all the bases. Not an excuse just making a statement.

    Good job on your blog and plugin and I do hope you can perhaps follow it up with an article in the magazine at some point clarifying the issue.

    Chee Yeo

    July 3, 2009 at 3:16 pm

    • Hi Chee

      You’re right, it will take the entire magazine to explain the differences between the two. And the terminologies are overlapping, especially with the state machine libraries trying to reach out into workflow space.

      Like John pointed out in the post, and I pointed out in my “Ruote in 20 minutes” presentation, the two can happily co-exist and they work together amazingly well.

      Ciao !

      Kenneth Kalmer

      July 3, 2009 at 3:26 pm

  2. Hi Chee,

    great article, seems like you took my post ‘right’, as an explanation, not an ‘attack’.

    Congrats on the new company ! All the best !

    John Mettraux

    July 3, 2009 at 3:18 pm

  3. […] Mettraux posted a powerful read on the fact that state machines are not workflow engines, and I share the sentiment completely. I’m a vocal user of John’s ruote, a Ruby […]

  4. Hi John,

    Interesting read – I’m looking at implementing user definable workflows in a project at the moment, and evaluating whether my own StateFu, or Ruote, or both, makes sense for this project.

    So, this was a timely post which helped to spell out what’s different about Ruote.

    I’m interested in approaching the process orientation of Ruote with StateFu, which is fundamentally a state machine, by exploring e.g. message queueing, versioning object state and evolving machines, etc. It’ll be interesting to see how far the state-based model can be stretched into full-blown workflow engine territory without beginning to show signs of strain.

    /tips hat

    cheers,
    David

    David Lee

    July 10, 2009 at 4:38 am

    • Hi David,

      thanks for the comment and thanks for the state-fu example I borrowed.

      Think about process definitions and then process definition modifications and then process instance modifications. That should help you in your design decisions.

      Have fun !

      John

      John Mettraux

      July 10, 2009 at 4:50 am

  5. Thanks John,

    I’ve updated my README to point out some of the differences, and attempted to summarize the differences between StateFu’s and Ruote’s ability to implement workflow process flow control patterns (which I found very instructive wrt the differences in approach) here:

    http://github.com/davidlee/state-fu/blob/9d761b7a09e6ad3fd2610a8238b6365900a2dae5/WORKFLOW.textile

    I’d be grateful for any corrections or suggestions.

    best regards,
    David

    David Lee

    July 16, 2009 at 12:19 am

    • Hey David,

      excellent analysis, nicely done. I don’t see anything I could correct.

      Thanks for this conversation !

      Could I link the body of the blog post to your text ?

      John

      John Mettraux

      July 16, 2009 at 12:38 am

  6. Thank you first for this article !
    You say : I can’t adhere to the ‘sequential workflow’ appellation …
    So my question is : do you adhere to the ‘adaptative workflow’ appellation ?

    patfrat

    August 27, 2009 at 10:36 pm

    • Hello Patrick,

      I looked at “adaptive”‘s definition :

      http://en.wiktionary.org/wiki/adaptive

      Well, I can’t adhere to it either. I’d say that “adaptive” is a desired quality for any workflow system.

      Maybe something like “declarative workflow”, since there is a process definition.

      Interesting.

      John Mettraux

      August 27, 2009 at 11:31 pm

  7. Now, with this “declarative workflow” engine, if i want to build a “workflow application”, i have to build a wysiwyg form builder, a wysiwyg workflow definition builder with an authentification engine (Authlogic+Authorization or something else …)
    Why not ?
    I will try ruote as soon as possible with my current projects to learn it …
    Thank you for your work !

    patfrat

    August 31, 2009 at 7:24 am


Comments are closed.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: