squirrel状态学习和使用

2020-11-13  本文已影响0人  _行走的五花肉_

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 */

状态机生命周期节点执行顺序

  1. @onTransitionBegin
  2. 这里首先判断Condition.isSatisfied
  3. 如果发生拒绝事件, 执行@TransitionDeclinedEvent, 否则继续往下执行
  4. @onBeforeActionExecuted
  5. 这里执行自定义的action(action有三种, exit/transit/entry), 先执行exit即@State中的exitCallMethod
  6. 如果action发生异常, 则执行@OnActionExecException
  7. @OnAfterActionExecuted
  8. @OnBeforeActionExecuted
  9. 这里执行自定义的action(action有三种, exit/transit/entry), 这里执行transit即@Trasit中的callMethod
  10. @OnAfterActionExecuted
  11. @OnBeforeActionExecuted
  12. 这里执行自定义的action(action有三种, exit/transit/entry), 这里执行entry即@State中的entryCallMethod
  13. @onAfterActionExecuted
  14. 如果没有发生异常,执行@onTransitionComplete, 如果发生异常, 执行@OnTransitionException
  15. 最后执行@OnTransitionEnd

Transit定义

常见正常操作

@Transitions({
        @Transit(from = "PROTECTING", to = "TERMINATION_CLAIM", on = "CLAIM_SETTLE", callMethod = "toTerminationClaim", when = Core2020PolicyStateCondition.class),
})
执行顺序如下:
  1. 在状态机start时,首先entryPROTECTING
  2. 执行condition
  3. 执行exitPROTECTING
  4. 执行callMethod = "toTerminationClaim"
  5. 执行condition
  6. 执行FromAnyToAny
  7. 执行entry TERMINATION_CLAIM
  8. 最终状态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),
})
执行顺序如下:
  1. 在状态机start时,首先entryPROTECTING
  2. 执行condition
  3. 执行exitPROTECTING
  4. 执行entryPROTECTING
  5. 执行exitPROTECTING
  6. 执行callMethod = "toTerminationInvalidTime"
  7. 执行condition以及fromAnyToAny
  8. 执行entryPROTECTING
  9. 最终状态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(约定优于配置)的方式>

上一篇下一篇

猜你喜欢

热点阅读