uleroboticsgroup / yasmin

YASMIN (Yet Another State MachINe)
GNU General Public License v3.0
130 stars 25 forks source link

Transitions exceptions handling in MonitorState #6

Closed jkaniuka closed 1 year ago

jkaniuka commented 1 year ago

Hi, I built my own FSM based on monitor_demo.py example. While testing, I found that MonitorState class implementation does not take all possible input combinations into consideration. In execute() method of MonitorState class we wait for the first message to be received. Then in _monitorhandler() we generally check the contents of the message (for example using if/elif/else block). There are 3 possible scenarios:

  1. :green_circle: If _monitorhandler() returns outcome which is defined in state's outcomes everything works fine and we go to the next state of FSM.
  2. :red_circle: Transition was declared in _sm.addstate() but we did not consider it in _monitorhandler() (simple code snippet below):
    sm.add_state("STATE_1", TestState(self),
                     transitions={"done": "Task finished",
                                  "failed": "Task interrupted"})
    def monitor_handler(self, blackboard: Blackboard) -> str:
    if blackboard.msg.data == "done":
        return "done"

    In this case _monitorhandler() returns None and following Exception is being raised:

    raise Exception("Outcome " + outcome + " does not belong")
    TypeError: can only concatenate str (not "NoneType") to str
  3. :red_circle: Transition was not declared in _sm.addstate(). Same as above _monitorhandler() returns None and Exception is being raised: