Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
I think you've got an architectural error. A state machine (SM) determines it's own actions based on it's state and the messages it may receive. The calling code may send messages (information) t...
Answer
#2: Post edited
I've written a dozen state machines, and I think there's an architectural error. A state machine (SM) determines it's own actions based on it's state and the messages it may receive. The calling code may send messages (information) to a SM. But the calling code shouldn't send actions (imperative commands) to a SM.- A SM should be able to ignore a message which it doesn't know how to react to. Maybe in some states the SM reacts to the message, in other states it doesn't react, and the SM happens to be in a state in which it doesn't react. Maybe the SM never reacts to that message in a any state. In these cases, the SM should ignore the message if doesn't know how to react to it. The SM shouldn't raise an exception, because this situation is normal. The calling code which sends the message shouldn't know the state of the SM. It should pump messages to the SM, and let the SM take care of itself.
- ```python
- def next_state(self, state_machine: "StateMachine", action: str) -> "State":
- """
- return the new state caused by the action
- """
- if not action in self._action_map:
- raise ValueError(f"State {self} does not support action {action}")
- new_state = state_machine.get_state(self._action_map[action])
- return new_state
- ```
- I think you've got an architectural error. A state machine (SM) determines it's own actions based on it's state and the messages it may receive. The calling code may send messages (information) to a SM. But the calling code shouldn't send actions (imperative commands) to a SM.
- A SM should be able to ignore a message which it doesn't know how to react to. Maybe in some states the SM reacts to the message, in other states it doesn't react, and the SM happens to be in a state in which it doesn't react. Maybe the SM never reacts to that message in a any state. In these cases, the SM should ignore the message if doesn't know how to react to it. The SM shouldn't raise an exception, because this situation is normal. The calling code which sends the message shouldn't know the state of the SM. It should pump messages to the SM, and let the SM take care of itself.
- ```python
- def next_state(self, state_machine: "StateMachine", action: str) -> "State":
- """
- return the new state caused by the action
- """
- if not action in self._action_map:
- raise ValueError(f"State {self} does not support action {action}")
- new_state = state_machine.get_state(self._action_map[action])
- return new_state
- ```
#1: Initial revision
I've written a dozen state machines, and I think there's an architectural error. A state machine (SM) determines it's own actions based on it's state and the messages it may receive. The calling code may send messages (information) to a SM. But the calling code shouldn't send actions (imperative commands) to a SM. A SM should be able to ignore a message which it doesn't know how to react to. Maybe in some states the SM reacts to the message, in other states it doesn't react, and the SM happens to be in a state in which it doesn't react. Maybe the SM never reacts to that message in a any state. In these cases, the SM should ignore the message if doesn't know how to react to it. The SM shouldn't raise an exception, because this situation is normal. The calling code which sends the message shouldn't know the state of the SM. It should pump messages to the SM, and let the SM take care of itself. ```python def next_state(self, state_machine: "StateMachine", action: str) -> "State": """ return the new state caused by the action """ if not action in self._action_map: raise ValueError(f"State {self} does not support action {action}") new_state = state_machine.get_state(self._action_map[action]) return new_state ```