No Zombies in State Machines

Sezen de Bruijn
ITNEXT
Published in
8 min readNov 17, 2020

--

So… a few weeks ago I had a bit of an Adventure (click here to read it), when a meme was shared in the SA group:

I wondered that too, and so I set to work. Creating hand-washing zombies along the way. This was mostly due to me using the wrong tool for the job. We’ll get back to that.

I wrote everything down, thinking others might get a laugh out of it too. But I should have known that when you share these types of things to a community of developers you quickly get a lot of additional insights that make your fingers itch to rework it all again.

So…. the adventure continues!

Hammers and screwdrivers

The day after publishing my article, when the wider Ecosystem became involved it became quite clear that State Machines would indeed have been a better approach.

To summarize the discussion, there were various valid points raised:

  • First and foremost: there are no zombies in State Machines. With a state machine you simply terminate all processes and close the case when the Case Status is updated to ‘not Alive’.
  • Secondly, trained humans are far better equipped to solve these types of problems without the straightjackets that processes supply. That is mostly because in this case many factors influence what is to be done and how it has to be done. There is no order.
  • A State Machine sets up your system to be much more flexible and change with the times. So if there were any new insights on how to deal with The Situation, we could easily adapt to the new protocols.

So why did I choose to do it with BPT at all?

The saying goes: if you have a hammer everything looks like a nail.

Well I have always had a hammer in the form of BPT. I recently came into possession of a screwdriver too, in the form of Case Management Framework… But I haven’t gotten quite so adapt yet at figuring out when something is a screw. Turns out that this project definitely was a screw.

And now it has become Friday again…. And I’m feeling just about done for the week. So, let’s give those State Machines a go.

To the drawingboard

This time I decided to actually think about what I was doing before diving in. Not my style usually, but I had heard good things about it.

So I drew some charts on my whiteboard. First the simple part:

People start of with clean hands, but then time passes, or they go outside, and now they need to wash their hands.
After the people have washed their hands, for twenty seconds, with soap, the hands are presumably clean again, so I added an arrow for that state transition as well:

So far, easy, but also chuck-full of zombies. In any of this states it is possible that the person stops being alive, so we need to deal with that, or otherwise we will inevitably wind up with a very hygienic population of the undead.
(Which, admittedly would be an upgrade from your average zombie, but still…)

That is better. Once someone moves into the not alive state, that is the state they will remain in. They cannot move back into any other state, and since there is no actions associated with this state, we will avoid the zombies.

Enough with all of the thinking! Let’s just do this!

The first thing is to set up a Case Management framework application. That is to say, I made myself a business entity for my Case data called ‘Concerned Citizen’, linked this to a process and entered the states and transitions.

Modelling the part where you can die no matter what you are doing was a bit of a confrontation with my own mortality. But existential crisis aside, we were quite well along the way.

Now it was time to make a completely manually operated version of this application, where a human being can simply decide when each state is being reached. One of the feedbacks that I received had been after all: “Trained humans are far better equipped to solve this without the straightjackets that processes supply. ”
So in order to facilitate those trained humans, I implemented a few screen.

It is quite simple overall: just a list and detail screen of Concerned Citizens. The detail screen is where the fun starts. When you create your citizen, the case is automatically created, and given the status of CleanHands. On the top of your screen you’ll see a few buttons appear: TimePassed, NotAlive and WentOutSide. These are the next statuses based on the Clean Hands Status.

Click on TimePassed and new options appear: WashHands and NotAlive.

You can click on the options as long as you want, unless you click on NotAlive. Now your buttons have disappeared and your Citizen has reached their final resting state.

All works! Except of course for a little data entry error which ensured that the only follow-up state after “WentOutside” was “TimePassed”. (This might actually be true, on a more meta-phyisical level, but it did not reflect the reality I wanted to create. Luckily this was easily fixed.)

One of the benefits of this manual approach is exactly what the community pointed out to me, that in the end of the day a human being is better able to navigate the complexities of such interacting protocols. The role of IT in such cases should just be to facilitate correct behavior. Automating things is not always the best approach.

Having said that….

Let’s Automate!

It is a girls’ prerogative to be obstinate, so while I do theoretically agree with the role of technology to be subservient to the human operator, I also feel that there are some tasks technology is better suited for. I have therefore decided to persist in my attempts of utilizing automated software to navigate the cruel realities of the world we currently reside in.
So let us try to create a solution that marries the consistency of technology with the more elegant intelligence of the human mind to create a Covid-Cyborg.

There are some time-based actions in this protocol, and a computer is very good at keeping time, so that seems like a good place to start adding some automation.

At first I made a dispatcher for the Next Statuses. This technology basically loops over all the statuses and for the relevant ones it points them to actions required to aid the State Transition.
For example: the transition to WashHands occurs when either two hours have passed or the status has been changed to WentOutside. So whenever the next stage could potentially be Time Passed we need to start a timer so that we can make the person Wash Hands after two hours.

When the timer runs out or the status gets updated to ‘Went OutSide’ we update the status to WashHands, and point back to the Dispatcher.

I also implemented a simple step to terminate all processes when the Concerned Citizen is no longer alive as part of our Zombie survival strategy.

And as soon as I published that, I spotted the problem… Do you see it as well? No? Well let’s zoom in a little bit…

The poor Concerned Citizen will wash their hands every two hours. Always, for as long as they are alive. This mean they will wake up every two hours at night to wash their hands.
Which is… undesirable.

So lets fix it!

I added the states into the system, which took about two minutes, and delightfully meant that the state was fully implemented in screen and process immediately.

So one of the take-aways from this experiment is definitely that when you have a chaotic mind like me, State Machines do allow you to switch your design around quite quickly. But another take-away is that for chaotic minds like mine, State Machines also allow you to accidentally mismodel in comically absurd ways .

For instance, I accidentally put somebody asleep whenever they were meant to wait two hours until they had to wash their hands instead. So I suppose this article could be called “I messed around with State Machines and accidentally gave all concerned citizens Narcolepsy .”

Cleaning up

So I cleaned up and extended my workflow code a bit so that my process now looks like this:

And did some minor improvements on the UI so that my screens now look like this:

Want to try it out?I put it onto my personal environment as well, you can play around with it here.
Prefer to have a look at the code? Reach out to me, I’ll hook you up with the .oap.

Looking forward to hear your suggestions on improvements.

I myself would love to add a subcase for mask-wearing and for testing and I probably could spend the rest of my life tweaking this application. But alas, work needs to be done.

Oh well, I’m sure another Friday will come along eventually.

--

--

Low-Code Solution Architect ★ helping you build IT Fast, Right and for the Future ★ Let’s just have some fun :-)