How to correctly pass data in the Finite State machine in AKKA

user_mda :

I am following this example here from the doc

Here is part of the finite state machine I'm working with

    startWith(ACCEPTED, new myData());


    when(ACCEPTED, matchEvent(someMesage.class, MyData.class,
        (someMessage, myData) -> goTo(EVALUATING).replying(EVALUATING)));

    onTransition(matchState(ACCEPTED,EVALUATING, () -> {
      // Here I want to update the nextState data and pass it to another actor 
      // But the nextState data is always the unititalized object which is new Mydata() when the FSM initializes


    }));
    whenUnhandled(matchAnyEvent(
        (state, data) -> stay().replying("received unhandled request " + state.toString())));

    initialize();
  }

How do I correctly pass data between various states in the state machine?

How should the actor.tell call look like for the actor sending a message to this FSM actor

If I send the following message

  MyFSM.tell(new someMessage(myData), getSelf());

It correctly matches the event and the actor changes the state to EVALUATING and sends back an EVALUATING message. BUt what I really want is, modify 'myData' based on this state change and on transition, send this modified data to another actor.

But when I send a message of type someMessage I have no way to send the existing instance of myData and it is always uninitialized as part of the initialization of the state machine.

In other words, I am trying to manage the state of myData with the finite state machine.

How can I achieve his making the best use of the framework?

A working example from the above information will be really useful!

johanandren :

You can use using to provide a new state, like so:

when(ACCEPTED, matchEvent(someMesage.class, MyData.class,
  (someMessage, oldData) -> {
   MyData newState = new MyData(); // or transform the old into a new one
   return goTo(EVALUATING).using(newState).replying(EVALUATING);
}));

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=469426&siteId=1