0

我该如何PermitIf使用SetTriggerParameters

在这个例子中,我正在模拟一个电机,它可以前进、后退或关闭(不移动)。前进和后退可以是一种double速度,因此我需要SetTriggerParameters<double>. 但是,将速度设置为 0 会关闭电机。

我希望禁止能够触发TurnOnForwardsTurnOnBackwards使用 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);

这可能吗?

4

1 回答 1

1

您现在可能已经弄清楚了,但是如果您使用参数化触发器,它将编译:

    machine.Configure(State.Off)
        .PermitIf(forwardsTrigger, State.Forwards, (speed) => speed > 0) // no error 
        .PermitIf(backwardsTrigger, State.Forwards, (speed) => speed < 0) // no error
        .Permit(Triggers.TurnOnForwards, State.Forwards)
        .Permit(Triggers.TurnOnBackwards, State.Backwards);
于 2021-05-17T06:12:21.593 回答