org-arl / fjage

Framework for Java and Groovy Agents
https://fjage.readthedocs.io/en/latest/
Other
26 stars 13 forks source link

fsm.setNextState() overwrites fsm.terminate() #324

Open ettersi opened 2 months ago

ettersi commented 2 months ago

The behaviour of fsm.terminate() is somewhat unfortunate: it only sets the next state to FINAL but doesn't actually enter that state until the FSM runs again. This means that if any piece of code calls fsm.setNextState() between the fsm.terminate() and the FSM actually terminating (e.g. by reacting to an incoming message, or in an onExit() hook), then the fsm.terminate() is effectively ignored.

I believe an easy way to fix this is to add an fsm.action() immediately after the fsm.terminate(). This way, we both suggest the state transition to the FSM and immediately execute, and therefore this makes sure that no other code can override the state transition. We may want to consider adding this fix to fsm.terminate().

mchitre commented 2 months ago

Terminate should not call action directly. State transitions should occur only on the main thread of the fsm. A simple fix may be to ignore state changes once the next state is marked as FINAL.

ettersi commented 2 months ago

State transitions should occur only on the main thread of the fsm.

AFAICT, only agents have threads, not behaviours? So as long you call the fsm.action() on the agent's thread, you should be fine? Having said that, I agree that ignoring state changes out of FINAL should work also.