2016-08-17 39 views
1

背景:獲取關於在無狀態的保護條件允許觸發器(.NET狀態機庫)

我有相同TriggerPermitIf定義互斥guard條件,將導致過渡到根據這些條件的不同狀態。

守衛有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

我不相信有一種方法,以目前做到這一點,但有一些討論圍繞更完整的自省API的可能性在這裏:https://github.com/dotnet-state-machine/stateless/pull/100 –

回答

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); 
    } 
}