0

I'm attempting to improve my understanding of FSMs by using them in an application responsible for deploying software to a staging system. A component of this application is responsible for ensuring that git repositories are installed, up-to-date and correctly checked-out. I have modelled the possible states of the repository as a finite-state machine. Most examples that I have seen assume that there will be a series of external events driving the FSM, but in this case there is just an end goal: ensure that the repository is installed, up-to-date and checked-out. I could hard-wire the FSM so that it fires the events necessary to ensure that it reaches the checked-out state: if state is missing, install; if state is out-of-date, update; and so on. But what if I want the system to be more flexible, such that it can be extended to allow multiple predetermined outcomes? I could write a number of conditional statements that control the flow of events sent to the FSM, but that doesn't feel right: it defeats the object of using an FSM in the first place. It feels as though multiple FSMs are required: one that encapsulates the behaviour of the git repository, and one or more that describe the path to take to reach the desired end goal.

At this point, I wonder if my understanding of FSMs is flawed. Is it common to have an FSM control itself by issuing the events required to reach a particular state? And what about having one FSM control another? Is there a name for this type of system?

Update

As requested, here's my state diagram:

enter image description here

Mike
  • 21,301
  • 2
  • 42
  • 65
  • I'm not sure it has a name, but it sounds a lot like gluing two Moore machines together. http://en.wikipedia.org/wiki/Moore_machine – Linear May 06 '15 at 06:55
  • Also, I'm not sure what an FSM adds here, it sounds like you're just trying to do modular control flow. – Linear May 06 '15 at 06:58
  • @Jsor: An FSM may be overkill in this particular case. I started with simple procedural conditional statements, but they quickly grew unwieldy. It seemed like a good opportunity to experiment with FSMs. Thanks for the Moore Machine reference, and the modular control flow hint - I'll do some more reading. – Mike May 06 '15 at 07:08

2 Answers2

0

I don't think your understanding is flawed, but the FSM machinery does seem unwarranted.

"A component of this application is responsible for ensuring that git repositories are installed, up-to-date and correctly checked-out. I have modelled the possible states of the repository as a finite-state machine."

Would you mind posting one of your FSMs? My guess is that you are not getting very interesting ones, as FSMs are an overkill to express the sort of state transitions you described, eg:

0 -> git repos are installed -1> git repos are up-to-date -2> correctly checked out

You get an uninteresting FSM, which just goes through a sequence of states. Alternation (the other building block of FSMs) doesn't make much sense if you have dependencies.

I guess a simple sequence of dependencies would be enough to model your system:

git repos are correctly checked out <- git repos are up-to-date
git repos are up-to-date <- git repos are installed

meaning for the git repos to be correctly checked out one has to have (depends on) the git repos being up-to-date. If you represent your whole system just with the dependencies, topological sort will be enough to produce a correct ordering. The nice thing is that whenever in topsort you find a set of nodes with no dependencies on it (think of the naive algorithm) you can check any of them in any order...or in parallel if you wish. The FSM formalism does not easily give you that.

user1666959
  • 1,805
  • 12
  • 11
0

An Event Driven FSM states that transitions occur due to an event. If you were to model this as an FSM, then each action should emit an event at completion that would be fed back to the FSM. The event than determines the next transition. This approach is not uncommon. I think an FSM works well here provided you conform your code to become event driven.

Regarding FSMs working together - there is a notion of hierarchical FSMs

Another possibility - you may want to use a process or job control DSL. Often, they have an FSM hidden under the covers - but you express your logic using a higher level language.

Andrew Hall
  • 153
  • 1
  • 7