Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Self-transition executes too many entry & exit actions #116

Closed
npetryk opened this issue Jun 24, 2020 · 2 comments
Closed

Self-transition executes too many entry & exit actions #116

npetryk opened this issue Jun 24, 2020 · 2 comments

Comments

@npetryk
Copy link

npetryk commented Jun 24, 2020

I would expect an external transition from S -> S to produce the following actions:

  1. exitS
  2. entryS

i.e. S -> (exitS) -> (entryS) -> S

Instead, I see twice as many

  1. exitS
  2. entryS
  3. exitS
  4. entryS

i.e. S -> (exitS) -> (entryS) -> (exitS) -> (entryS) -> S

Is this the expected behavior?


Example code:

import org.squirrelframework.foundation.fsm.StateMachineBuilderFactory;
import org.squirrelframework.foundation.fsm.StateMachineConfiguration;
import org.squirrelframework.foundation.fsm.UntypedStateMachine;
import org.squirrelframework.foundation.fsm.UntypedStateMachineBuilder;
import org.squirrelframework.foundation.fsm.annotation.StateMachineParameters;
import org.squirrelframework.foundation.fsm.impl.AbstractUntypedStateMachine;

public class Example {

  enum FSMEvent {
    INTERNAL_TRANSITION,
    SELF_TRANSITION
  }

  public static void main(String[] args) {
    UntypedStateMachineBuilder builder = StateMachineBuilderFactory.create(MyStateMachine.class);
    builder.defineState("S");
    builder.internalTransition().within("S").on(FSMEvent.INTERNAL_TRANSITION);
    builder.transition().from("S").to("S").on(FSMEvent.SELF_TRANSITION);
    UntypedStateMachine fsm =
        builder.newStateMachine("S", StateMachineConfiguration.create().enableDebugMode(true));
    fsm.start();

    System.out.println("Internal");
    fsm.fire(FSMEvent.INTERNAL_TRANSITION);
    System.out.println("Self");
    fsm.fire(FSMEvent.SELF_TRANSITION);
  }

  @StateMachineParameters(
      stateType = String.class,
      eventType = FSMEvent.class,
      contextType = String.class)
  static class MyStateMachine extends AbstractUntypedStateMachine {

    protected void entryS(String from, String to, FSMEvent event, String ctx) {}

    protected void exitS(String from, String to, FSMEvent event, String ctx) {}
  }
}

Output:

Internal
21:40:36.839 [main] INFO  o.s.f.fsm.StateMachineLogger - MyStateMachine(2fEFYCwbIl): Transition from "S" on "INTERNAL_TRANSITION" with context "null" begin.
21:40:36.844 [main] INFO  o.s.f.fsm.StateMachineLogger - MyStateMachine(2fEFYCwbIl): Transition from "S" to "S" on "INTERNAL_TRANSITION" complete which took 4.437 ms.
Self
21:40:36.844 [main] INFO  o.s.f.fsm.StateMachineLogger - MyStateMachine(2fEFYCwbIl): Transition from "S" on "SELF_TRANSITION" with context "null" begin.
21:40:36.845 [main] INFO  o.s.f.fsm.StateMachineLogger - Before execute method call action "exitS:-10" (1 of 4).
21:40:36.845 [main] INFO  o.s.f.fsm.StateMachineLogger - After execute method call action "exitS:-10" which took 209.2 μs.
21:40:36.846 [main] INFO  o.s.f.fsm.StateMachineLogger - Before execute method call action "entryS:-10" (2 of 4).
21:40:36.846 [main] INFO  o.s.f.fsm.StateMachineLogger - After execute method call action "entryS:-10" which took 168.6 μs.
21:40:36.846 [main] INFO  o.s.f.fsm.StateMachineLogger - Before execute method call action "exitS:-10" (3 of 4).
21:40:36.846 [main] INFO  o.s.f.fsm.StateMachineLogger - After execute method call action "exitS:-10" which took 103.8 μs.
21:40:36.846 [main] INFO  o.s.f.fsm.StateMachineLogger - Before execute method call action "entryS:-10" (4 of 4).
21:40:36.846 [main] INFO  o.s.f.fsm.StateMachineLogger - After execute method call action "entryS:-10" which took 101.6 μs.
21:40:36.847 [main] INFO  o.s.f.fsm.StateMachineLogger - MyStateMachine(2fEFYCwbIl): Transition from "S" to "S" on "SELF_TRANSITION" complete which took 2.093 ms.
@hekailiang
Copy link
Owner

hekailiang commented Jun 24, 2020

thx for reporting this issue, fixed in latest version. changelist

@npetryk
Copy link
Author

npetryk commented Jun 24, 2020

Wow, thanks for the quick follow up!

Would you like me to close this issue out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants