4

I have a state machine in which I might have to restart something when needed (Say restart a hardware). But after restarting, I need to run some tests on the thing. If the tests fail, I need to stop the thing and exit the state machine with a failure status.

In case of failure, how should I handle the 2nd stopping of the thing? The state machine diagram loses its readability if I do it the following way. And the implementation becomes ugly since I need to decide Stop thing state's output based on the previous state (SUCCESS the first time and ERROR if the state was reached after failing the tests)

Option1

My other option is to create a 2nd instance of the Stop thing state as follows. But then it makes the state machine bigger

Option2

Which one of these is the correct approach? Or is there another way to approach these kind of problems?

If it matters here, I am using ROS and smach for the state machine implementation

3

1 Answer 1

5

Your first statemachine does not accurately reflect the desired behaviour you described. In the first statemachine, you keep retrying to restart the "thing" until either the tests pass or stopping fails.

Your second statemachine does show the desired behaviour of trying to restart the "thing" once and stopping it again when the tests fail.

Note that a state is not defined by the actions that are performed in the state, but more by the transitions leading out of the state. This also means that a state should not need to know how it was reached. If the path leading up to a state is relevant for how the state reacts to events, then it is really multiple states. As the transitions for Stop thing 1 and Stop thing 2 are completely different, it is entirely correct to model them as two different states, even if they take the same actions and respond (albeit differently) to the same events.

3
  • Sorry yeah that was misleading. Edited the question on the first state machine. Stop thing state will exit with SUCCESS if it is the first time and ERROR if the state was reached after failing the tests in the fist state machine
    – doca
    Apr 6, 2020 at 13:00
  • 6
    @TeshanShanukaJ, a state should not need to know how it was reached. If the path leading up to a state is relevant for how the state reacts to events, then it is really multiple states. Apr 6, 2020 at 13:12
  • 1
    Bart, say it in the answer. Apr 6, 2020 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.