squirrel状态学习和使用
squirrel使用参考
Squirrel使用 | 城的灯
https://segmentfault.com/a/1190000009906469
https://github.com/hekailiang/squirrel
内置事件
State Machine Lifecycle Events
|—StateMachineEvent /* Base event of all state machine event /
|—StartEvent / Fired when state machine started /
|—TerminateEvent / Fired when state machine terminated /
|—TransitionEvent / Base event of all transition event /
|—TransitionBeginEvent / Fired when transition began /
|—TransitionCompleteEvent / Fired when transition completed /
|—TransitionExceptionEvent / Fired when transition threw exception /
|—TransitionDeclinedEvent / Fired when transition declined /
|—TransitionEndEvent / Fired when transition end no matter declined or complete */
状态机生命周期节点执行顺序
- @onTransitionBegin
- 这里首先判断Condition.isSatisfied
- 如果发生拒绝事件, 执行@TransitionDeclinedEvent, 否则继续往下执行
- @onBeforeActionExecuted
- 这里执行自定义的action(action有三种, exit/transit/entry), 先执行exit即@State中的exitCallMethod
- 如果action发生异常, 则执行@OnActionExecException
- @OnAfterActionExecuted
- @OnBeforeActionExecuted
- 这里执行自定义的action(action有三种, exit/transit/entry), 这里执行transit即@Trasit中的callMethod
- @OnAfterActionExecuted
- @OnBeforeActionExecuted
- 这里执行自定义的action(action有三种, exit/transit/entry), 这里执行entry即@State中的entryCallMethod
- @onAfterActionExecuted
- 如果没有发生异常,执行@onTransitionComplete, 如果发生异常, 执行@OnTransitionException
- 最后执行@OnTransitionEnd
Transit定义
常见正常操作
@Transitions({
@Transit(from = "PROTECTING", to = "TERMINATION_CLAIM", on = "CLAIM_SETTLE", callMethod = "toTerminationClaim", when = Core2020PolicyStateCondition.class),
})
执行顺序如下:
- 在状态机start时,首先entryPROTECTING
- 执行condition
- 执行exitPROTECTING
- 执行callMethod = "toTerminationClaim"
- 执行condition
- 执行FromAnyToAny
- 执行entry TERMINATION_CLAIM
- 最终状态TERMINATION_CLAIM
结论: 可以看出不同@Transit之间的范围是exit>transi, to状态的entry,是状态机内部最后做的
重复定义相同event
@Transitions({
@Transit(from = "PROTECTING", to = "TERMINATION_INVALID_TIME", on = "CLAIM_SETTLE", callMethod = "toTerminationInvalidTime", when = Core2020PolicyStateCondition.class),
@Transit(from = "PROTECTING", to = "TERMINATION_CLAIM", on = "CLAIM_SETTLE", callMethod = "toTerminationClaim", when = Core2020PolicyStateCondition.class),
})
from和to相等
@Transitions({
@Transit(from = "PROTECTING", to = "PROTECTING", on = "CLAIM_SETTLE", callMethod = "toTerminationInvalidTime", when = Core2020PolicyStateCondition.class),
})
执行顺序如下:
- 在状态机start时,首先entryPROTECTING
- 执行condition
- 执行exitPROTECTING
- 执行entryPROTECTING
- 执行exitPROTECTING
- 执行callMethod = "toTerminationInvalidTime"
- 执行condition以及fromAnyToAny
- 执行entryPROTECTING
- 最终状态PROTECTING
注意: 多了一次entryPROTECTING和一次exitPROTECTING的结果让人很迷惑, 最好不要这么用, 可以定义成
internal
的, 避免触发entry/exit
其他
@Transitions({
@Transit(from = "*", to = "*", on = "*", callMethod = "transitFromAnyToAnyWhenCore2020", when = Core2020PolicyStateCondition.class),
@Transit(from = "SUSPEND", to = "PROTECTING", on = "PROTECT_RESTART", callMethod = "toProtectRestart", when = Core2020PolicyStateCondition.class),
@Transit(from = "*", to = "*", on = "*", callMethod = "transitFromAnyToAnyWhenCore2020", when = Core2020PolicyStateCondition.class),
})
这样: 第二个先执行, 然后执行第一个和第三个
约定优于配置
如果存在约定名称的方法,
同时也存在配置的方法, 则先执行配置的方法, 再执行约定的方法,
例如:
@States({
@State(name = "PROTECTING", exitCallMethod = "exitPROTECTING2"),
})
同时存在`exitPROTECTING`
则先执行exitPROTECTING2,再执行exitPROTECTING, 在他们前后分别执行onBeforeActionExecuted和onAfterActionExecuted
* <Convention Over Configuration(约定优于配置)的方式>
* exit[StateName] The method will be invoked when exit state 'A'.
* So as the entry[StateName] ,beforeExitAny/afterExitAny and beforeEntryAny/afterEntryAny.
* Other Supported Naming Patterns:
* transitFrom[fromStateName]To[toStateName]On[eventName]When[conditionName]
* transitFrom[fromStateName]To[toStateName]On[eventName]
* transitFromAnyTo[toStateName]On[eventName]
* transitFrom[fromStateName]ToAnyOn[eventName]
* transitFrom[fromStateName]To[toStateName]
* on[eventName]
* </Convention Over Configuration(约定优于配置)的方式>