glyph / automat

Self-service finite-state machines for the programmer on the go.
MIT License
583 stars 64 forks source link

How to model error states? #116

Closed jackgeek closed 2 years ago

jackgeek commented 4 years ago

I am trying to use this library to model a calculator. Some commands put the calculator into an error state. For example trying to press the square root button when the current value is a negative number. I don't see how I can model this with automat.

I have a state called awaiting_number_input which is used to receive number inputs (i.e. digits 0-9 or decimal point) and then I have an input called apply_unary_operator which accepts an operator class as a parameter. I then setup an output called _apply_unary_operator which actually uses the class to calculate the result of the operator. This allows me to have a generic calculator state machine which understands unary operators and binary operators can be applied but does not require specific states or inputs for every operator.

The problem is that the Square Root operator should throw the machine into an error state if the current value is negative. So I want to be able to define transitions from various states where it is valid to apply a unary operator from to the "unary_operator_applied" state. These transitions can use the _apply_unary_operator output and I want the output to be able to divert the transition to the "error" state if applying the operator fails.

Is this possible?

glyph commented 4 years ago

It depends what you mean by “error state”. It sounds like just another state to me!

We could use a bit more clarity around what automat does when you feed it an input with no defined transition though.

jackgeek commented 4 years ago

Imagine a state machine modeling a calculator. Number Entry(some number) -> Operator(divide) -> Number Entry(0) -> Equals. Normally I would be going to a state where I can enter another number or another operator, but this operation failed so I need to divert to an error state.

I guess I could externalize the logic so I have a wrapper that wraps the calculator FSM and intercepts all the user inputs and maintain state there. Maybe my approach which is to keep the internal calculator state (I.e the numbers and operators entered) in the calculator FSM is not right.

On Mon 11 Nov 2019, 23:40 Glyph, notifications@github.com wrote:

It depends what you mean by “error state”. It sounds like just another state to me!

We could use a bit more clarity around what automat does when you feed it an input with no defined transition though.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/glyph/automat/issues/116?email_source=notifications&email_token=AABRLOSQDY2XFWHXHWX2NN3QTHUPJA5CNFSM4I4N2FCKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEDYQVBY#issuecomment-552667783, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABRLOQ6TFZIAYQXFCNSIMTQTHUPJANCNFSM4I4N2FCA .

glyph commented 2 years ago

I'm going to close this just because I don't think it's clear enough as stated what the work involved in closing it would be. Please feel free to reopen with a more precise definition of 'error state'.