Search code examples
c#state-machine

Having a state machine as a guard of another state machine


I am designing a system that simulates the flow of a document in an organization. For the sake of simplicity let's assume that the flow has the following states:

  • Opened
  • Evaluated
  • Rejected
  • Accepted

There are times that some external resources are required to be available to proceed. Thus if a resource isn't available the whole flow should be put on hold. I imagine that there's another (somehow) parallel state machine that has two states:

  • In Progress
  • On Hold

The way that I thought I could solve this problem was to check the state of the second state machine as a guard condition in every transition of the first state machine. But I wonder if there's a common way or pattern for solving this kind of problem?

BTW, I want to implement this state machines using the Stateless or bbv Common(Appccelerate) libraries.


Solution

  • With a UML state machine you could use hierarchical states together with a history states.

    • In Progress
      • Opened
      • Evaluated
      • Rejected
      • Accepted
      • (H) History state
    • On Hold

    Events for the substates of 'In Progress' will only be handled, if 'In Progress' and one of it's substates is active.

    The (H) history state can be used to reactivate the most recently active substate when 'In Progress' becomes active.