hekailiang / squirrel

squirrel-foundation is a State Machine library, which provided a lightweight, easy use, type safe and programmable state machine implementation for Java.
http://hekailiang.github.io/squirrel/
Other
2.19k stars 540 forks source link

callMethod() for all combinations of states & events? #106

Closed ThomasDaheim closed 4 years ago

ThomasDaheim commented 4 years ago

Hi,

I was looking for a way to generically define a method that is called for any transition between states independent of the action.

Currently, I have to loop through all possible combinations to call myBuilder.externalTransition().... to set the same method in all cases. Is there a way to set this only once such that its used in any case?

Thanks in advance, Thomas

hekailiang commented 4 years ago

have u tried following ways? // since 0.3.1 // the same effect as add method transitFromAnyToCOnToC in your state machine builder.transit().fromAny().to("C").on("ToC").callMethod("fromAnyToC"); // the same effect as add method transitFromBToAnyOnToC in your state machine builder.transit().from("B").toAny().on("ToC").callMethod("fromBToAny"); // the same effect as add method transitFromBToAny in your state machine builder.transit().from("B").toAny().onAny().callMethod("fromBToAny");

ThomasDaheim commented 4 years ago

Have just tried it in both ways:

@Transitions({ @Transit(from="*", to="*", on="*", callMethod="handleTransition") })

and

myBuilder.transit().fromAny().toAny().onAny().callMethod("handleTransition");

Nut the function isn't called :-( Not sure if I would need to do anything else? I'm running 0.3.8...

hekailiang commented 4 years ago

there should be various listener can satisfied ur requirements, like Polymorphism Event Dispatch part

ThomasDaheim commented 4 years ago

Bingo! That worked like a charm.