在Stateless(.NET状态机库)中获取具有保护条件的允许触发器

时间:2016-08-17 12:16:53

标签: c# stateless-state-machine

背景

我有Trigger相同的guard条件定义了PermitIf,这将导致根据这些条件转换到不同的状态。

警卫已定义Descriptions,并且它们在导出的DOT图表的括号中很好地显示,因此很容易遵循。

machine
   .Configure(Status.Registered)
   .PermitIf(Activity.Submit, Status.Submitted, IsGoodRating, "Is good  rating")
   .PermitIf(Activity.Submit, Status.Denied, IsBadRating, "Is bad rating")

现在,我知道有machine.PermittedTriggers属性可以返回当前状态下允许的触发器,这很好,但这是问题。

问题:

在这种情况下,有没有办法获得PermittedTriggers的列表,其中包含状态Registered的相应保护条件?

1 个答案:

答案 0 :(得分:1)

我猜你可以这样做,但是......我不知道它有多大帮助:

 class Program
{
    enum Status { Registered, Submitted, Denied }

    static void Main(string[] args)
    {
        int currentRating = 91;
        RatingTrigger RatingTrigger = new RatingTrigger();

        StateMachine<Status, Trigger> _sm = new StateMachine<Status, Trigger>(Status.Registered);
        _sm.Configure(Status.Registered)
            .PermitIf(RatingTrigger, Status.Submitted, () => RatingTrigger.Guard(currentRating), RatingTrigger.GuardDescription)
            .PermitIf(RatingTrigger, Status.Denied, () => RatingTrigger.Guard(currentRating), RatingTrigger.GuardDescription);

        var list = _sm.PermittedTriggers;

        foreach (var item in list)
        {
            if (item.GetType().Equals(typeof(RatingTrigger)))
            {
                Console.WriteLine(((RatingTrigger)item).GuardDescription);
            }
            else
            Console.WriteLine(item);
        }

    }
    private static bool evaluate()
    {
        return true;
    }
}
public abstract class Trigger
{
    public abstract bool Guard(object something);
}
public class RatingTrigger : Trigger
{
    public string GuardDescription = "This Guard evaluaties the current rating. Retuns true if good rating, false if bad rating";
    public override bool Guard(object rating)
    {
         return  (((int)rating) > 90);
    }
}