我最近使用了Stateless状态机。我可以像这样定义转换规则等:
stateMachine.Configure(State.Unknown)
.Permit(Trigger.StartApplication, State.Initialized)
.OnEntry(this.DoBeforeTransition)
.OnExit(this.DoAfterTransition);
stateMachine.Configure(State.Initialized)
.Permit(Trigger.CheckSomething, State.SomethingChecked)
.OnEntry(this.DoBeforeTransition)
.OnExit(this.DoAfterTransition);
然后你就可以触发一个触发器来改变状态。但是,如果要进入特定状态,则需要知道当前状态以及下一个状态。因此,"客户"如果没有定义直接过渡,状态机需要知识如何达到某种状态。是否有可能称之为" goto"并且机器会激活所有必需的触发器吗?
答案 0 :(得分:1)
你可以这样做只有一个" Permit"每个州。如果您有多个" Permit",那么您无法自动移动工作流程(必须有一些原因可以选择一个Permit / Trigger而不是另一个)。当我说你"不能",这在技术上不是,它实际上是。
以下是自动移动工作流程的示例。
using Stateless;
using System;
using System.Runtime.CompilerServices;
namespace MyExample.BAL.WorkFlows
{
public class TelephoneCallWorkFlow
{
private static volatile StateMachine<TelephoneCallStateEnum, TelephoneCallTriggerEnum> SingletonInstance;
public StateMachine<TelephoneCallStateEnum, TelephoneCallTriggerEnum> Instance
{
[MethodImpl(MethodImplOptions.Synchronized)]
get
{
if (SingletonInstance == null)
{
SingletonInstance = new StateMachine<TelephoneCallStateEnum, TelephoneCallTriggerEnum>(TelephoneCallStateEnum.OffHook);
SingletonInstance.Configure(TelephoneCallStateEnum.OffHook)
.Permit(TelephoneCallTriggerEnum.CallDialed, TelephoneCallStateEnum.Ringing);
SingletonInstance.Configure(TelephoneCallStateEnum.Ringing)
//removing so there is only one valid path workflow//.Permit(TelephoneCallTriggerEnum.HungUp, TelephoneCallStateEnum.OffHook)
.Permit(TelephoneCallTriggerEnum.CallConnected, TelephoneCallStateEnum.Connected);
SingletonInstance.Configure(TelephoneCallStateEnum.Connected)
//.OnEntry(t => StartCallTimer())
//.OnExit(t => StopCallTimer())
//removing so there is only one valid path workflow//.Permit(TelephoneCallTriggerEnum.LeftMessage, TelephoneCallStateEnum.OffHook)
//removing so there is only one valid path workflow//.Permit(TelephoneCallTriggerEnum.HungUp, TelephoneCallStateEnum.OffHook)
.Permit(TelephoneCallTriggerEnum.PlacedOnHold, TelephoneCallStateEnum.OnHold)
;
SingletonInstance.Configure(TelephoneCallStateEnum.OnHold)
//removing so there is only one valid path workflow//.SubstateOf(TelephoneCallStateEnum.Connected)
//removing so there is only one valid path workflow//.Permit(TelephoneCallTriggerEnum.TakenOffHold, TelephoneCallStateEnum.Connected)
//removing so there is only one valid path workflow//.Permit(TelephoneCallTriggerEnum.HungUp, TelephoneCallStateEnum.OffHook)
.Permit(TelephoneCallTriggerEnum.PhoneHurledAgainstWall, TelephoneCallStateEnum.PhoneDestroyed)
;
}
return SingletonInstance;
}
}
public void Fire(TelephoneCallTriggerEnum trigger)
{
Console.WriteLine("............[Firing:] {0}", trigger);
this.Instance.Fire(trigger);
}
}
}
public enum TelephoneCallStateEnum
{
OffHook,
Ringing,
Connected,
OnHold,
PhoneDestroyed
}
public enum TelephoneCallTriggerEnum
{
CallDialed,
HungUp,
CallConnected,
LeftMessage,
PlacedOnHold,
TakenOffHold,
PhoneHurledAgainstWall
}
现在&#34;自动移动&#34;特技。
TelephoneCallWorkFlow tcwf1 = new TelephoneCallWorkFlow();
IEnumerable<TelephoneCallTriggerEnum> myPermittedTriggers = tcwf1.Instance.PermittedTriggers;
while (null != myPermittedTriggers && myPermittedTriggers.Count() > 0)
{
if (myPermittedTriggers.Count() > 1)
{
throw new ArgumentOutOfRangeException("You cannot auto-move the workflow when there's more than one trigger");
}
TelephoneCallTriggerEnum nextTrigger = myPermittedTriggers.FirstOrDefault();
Console.WriteLine("About to call the 'next' trigger: --> {0}", nextTrigger);
tcwf1.Fire(nextTrigger);
Console.WriteLine("CurrentState: --> {0}", tcwf1.Instance.State);
myPermittedTriggers = tcwf1.Instance.PermittedTriggers;
}
你基本上得到PermittedTriggers,并得到第一个(并且为了自动移动工作,每个状态应该只有一个Permitted-Trigger).....然后调用那个触发器。
同样,实际上(在技术上),如果每个州有一个许可/触发,你只会这样做。这就是为什么我有一个例外,如果有超过1.你可以&#34;得到第一个&#34;如果超过1,那就没有任何意义。