4

Any state machine of reasonable complexity requires some entry actions to be performed upon entry to a state. For instance, UML State Machine diagrams have a special action for this purpose. Unfortunately I don't see how I can model such entry actions (or exit actions) in Akka FSM. Performing the actions on transitions (using underscore outgoing state) doesn't work since entry actions are intended to change the state data (e.g. preparing data structures required for operation in the new state). Any advice on how to model entry/exit actions in Akka FSM?

acjay
  • 34,571
  • 6
  • 57
  • 100
Jacob Eckel
  • 1,633
  • 1
  • 13
  • 22
  • By entry/exit actions you mean the actions that executed before actor started or after actor stopped? Or do you mean side effects on state transitions? I think your question isn't clean enough. – Mustafa Simav Sep 12 '15 at 20:55
  • Entry actions in state-machine terminology are functions performed upon entry to a state. Not related to Actor stop/start life-cycle. – Jacob Eckel Sep 16 '15 at 09:33
  • I got it. Why don't you use `goto(NewState).using(newData)` syntax to change state data? – Mustafa Simav Sep 16 '15 at 09:54

4 Answers4

1

As noted in Akka's FSM documentation, An initial currentState -> currentState notification will be triggered [after calling initialize].

Something like this should work:

onTransition {
  case InitialState -> InitialState =>
    // Do stuff
}
jake256
  • 192
  • 1
  • 3
0

I had the same problem and I played a while modifying the original FSM trait to deal with entry/exit.

https://github.com/jock71/jock-akka-fsm/blob/master/src/main/scala/jok/akka/fsm/FsmMod.scala

An usage example can be found at:

https://github.com/jock71/jock-akka-fsm/blob/master/src/main/scala/jok/akka/fsm/TestAkkaFsm.scala

Not very clear to me how to deal with StateData when is specified both in goto using and changed in entry handler

0

The book Akka in Action also uses the entry actions from UML. They implement entry actions using transitions with wildcards and use external events to change the state (but not the state data).

There is a complete scala example on github. The relevant part in scala:

onTransition {
    case _ -> WaitForRequests => {
      if (!nextStateData.pendingRequests.isEmpty) {
        // go to next state
        self ! PendingRequests
      }
    }

Or translated into java:

onTransition(
  matchState(null, WaitForRequests.class, () -> {
    if (!nextStateData().pendingRequests.isEmpty()) {
        // go to next state
        self().tell(PendingRequests, self());
        }
    }
  })
Synox
  • 1,148
  • 2
  • 17
  • 38
-1

If you want to add entry or exit actions for a state you need to use write some PartialFunction on the onTransition.
For example, this is how you model entry or exit actions in AkkaFSM:

onTransition {
   case _ -> StateA => /* This is an entry action for StateA. Do something here. You can send messages to actors (or self), so some state checks or setups. */
   case StateA -> _ => /* This is an exit action for StateA. Do something here. You can send messages to actors (or self), so some state post-checks or any cleanup task for the state */
}
Josep Prat
  • 493
  • 5
  • 11
  • 1
    The question explicitly related to this option and explained why it is not adequate - not possible to change the state data. – Jacob Eckel Mar 29 '16 at 11:50
  • I guess I misunderstood your question, because I thought you just wanted to perform checks or actions on the State itself or some local var in the actor, not on the Data itself. I'll leave the answer even if wrong as it answers the question i the title. – Josep Prat Mar 30 '16 at 06:19