我该如何PermitIf
使用SetTriggerParameters
?
在这个例子中,我正在模拟一个电机,它可以前进、后退或关闭(不移动)。前进和后退可以是一种double
速度,因此我需要SetTriggerParameters<double>
. 但是,将速度设置为 0 会关闭电机。
我希望禁止能够触发TurnOnForwards
或TurnOnBackwards
使用 0 参数 - 我希望TurnOff
触发器明确地成为关闭电机的方式。否则,您将到达当前状态Forwards/Backwards
但速度为 0 的情况。
这是我得到的错误:
CS1503 参数 1:无法从 'UserQuery.Triggers' 转换为 'Stateless.StateMachine<UserQuery.State, UserQuery.Triggers>.TriggerWithParameters'
public enum State
{
Off,
Forwards,
Backwards
}
public enum Triggers
{
TurnOff,
TurnOnForwards,
TurnOnBackwards,
}
var machine = new StateMachine<State, Triggers>(State.Off);
var forwardsTrigger = machine.SetTriggerParameters<double>(Triggers.TurnOnForwards);
var backwardsTrigger = machine.SetTriggerParameters<double>(Triggers.TurnOnBackwards);
machine.Configure(State.Off)
.PermitIf<double>(Triggers.TurnOnForwards, State.Forwards, (speed) => speed > 0) // error
.PermitIf<double>(Triggers.TurnOnBackwards, State.Forwards, (speed) => speed < 0) // error
.Permit(Triggers.TurnOnForwards, State.Forwards)
.Permit(Triggers.TurnOnBackwards, State.Backwards);
machine.Configure(State.Forwards)
.Permit(Triggers.TurnOnBackwards, State.Backwards)
.Permit(Triggers.TurnOff, State.Off);
machine.Configure(State.Backwards)
.Permit(Triggers.TurnOnForwards, State.Forwards)
.Permit(Triggers.TurnOff, State.Off);
machine.Fire(Triggers.TurnOnForwards);
machine.Fire(Triggers.TurnOff);
machine.Fire(Triggers.TurnOnBackwards);
machine.Fire(Triggers.TurnOff);
这可能吗?