2 dimensional Arrays or the best approach on this?

I am currently rebuilding my washing machine state machine and i like to a more formal way with the fsm. My approach is to define the fsm with a 2 dimensinal array. One dimension is the current stae, the other the new input value and the array value is the new output state.

My questio is now how to deine a static 2 dimensinal array to define the state machine. i seached a little bit but it seems that a typical array is not the solution. So what is a solution to define the array or something that behaves like ab array and access the values

Thanks
Thomas

Just in case you haven’t seen these:

But both of these just use if else statements to represent the state machine.

I can think of a couple of ways to solve this.

Approach 1: MAP transform

Put the data structure into a .map file with the current state + the input as the key and the new state as the value.

running-pausepressed = paused
paused-pausedpressed = running
...

Then in the code you would have:

    val newState = transform("MAP", "fsm.map", StateItem.state.toString + "-" + InputItem.state.toString)

Personally I think this is the easiest to work with approach.

Approach 2: Map

This approach builds the same thing using a Map instead of the .map file.

import java.util.Map

val fsm = createHashMap( "running-pausepressed" -> "paused",
                         "paused-pausedpressed" -> "running", ...
                        )

...

    val newState = fsm.get(StateItem.state.toString + "-" + InputItem.state.toString)

The key to both of these approaches is realizing that you don’t need a 2D array to represent this data. If you treat the two-dimensional address just as a key then you can just use a Map.

@rlkoshak Two fantastic approaches I would never have come up with. At the moment, however, I am still stuck in the 2 dimensional, which I like more when representing as as static variable, where one dimension represetes the events and the other the states. I ´really have to think about your approach.

Thanks a lot

My approaches are a 2D array. It just isn’t represented in memory that way.

The only practical difference is you concatenate the x,y coordinates as the address instead of using the typical [x][y] addressing you would use for an array (which as you know is not possible in Rules DSL).

You can still think about it as a 2D array. You can draw it as a 2D array. You can even concatenate the x,y address to look just like the above, complete with the brackets if you want. If you use the Map approach you can even populate the Map by listing each of the states in two dimensions if it helps.

There really is nothing different in either of these approaches in how YOU need to think about it. The only difference is how you pull the value out of the “2D array”.

And another advantage is you can use human readable and understandable values for the states and the inputs. If you use a 2D array, you would only be able to use integers for the x,y coordinates.

As you said it is just in My mind.