Closed yeqown closed 3 years ago
oops, I find clues by reading code, then I write the code as following:
func prepareStateMachine(gSeq uint8) *stateless.StateMachine {
sm := stateless.NewStateMachine(_OFFLINE)
sm.SetTriggerParameters(_ENTER, reflect.TypeOf(emptySeq))
sm.SetTriggerParameters(_LEAVE, reflect.TypeOf(emptySeq))
sm.Configure(_OFFLINE).
PermitDynamic(_ENTER, func(ctx context.Context, i ...interface{}) (stateless.State, error) {
seq := i[0].(uint8)
fmt.Printf("[off] enter seq=%d\n", seq)
if seq <= gSeq {
return _OFFLINE, nil
}
gSeq = seq
return _ONLINE, nil
}).
PermitDynamic(_LEAVE, func(ctx context.Context, i ...interface{}) (stateless.State, error) {
seq := i[0].(uint8)
fmt.Printf("[off] leave seq=%d\n", seq)
if seq <= gSeq {
return _OFFLINE, nil
}
gSeq = seq
return _OFFLINE, nil
})
sm.Configure(_ONLINE).
PermitDynamic(_ENTER, func(ctx context.Context, i ...interface{}) (stateless.State, error) {
seq := i[0].(uint8)
fmt.Printf("[on] enter seq=%d\n", seq)
if seq <= gSeq {
return _ONLINE, nil
}
gSeq = seq
return _ONLINE, nil
}).
PermitDynamic(_LEAVE, func(ctx context.Context, i ...interface{}) (stateless.State, error) {
seq := i[0].(uint8)
fmt.Printf("[on] leave seq=%d\n", seq)
if seq < gSeq {
return _ONLINE, nil
}
gSeq = seq
return _OFFLINE, nil
})
return sm
}
It seems I have solved this ... :)
As the title describes, I'm asking for advice that can help me. My state machine looks like this:
With my thought of
stateless
, thestate
could not contain any variable? just like this:I must explain why the state machine looks maybe weird:
seq
is the key to help me keep the state correct. Maybe there is another way to design the state machine, but for now, I need thisseq
variable. A new state machine design could be grateful.Drop event
means the action to changestate
,but I didn't find a clue to define this instateless
Trigger
could be defined as the actual way to change the state, howeverstateless
is not designed like this, right?Finally, I wonder couldstateless
help me in this case? Is there any advice on my state machine? it must be better that this issue could helpstateless
to add new features.