-
Notifications
You must be signed in to change notification settings - Fork 24
/
StateMachineDiagramPrinter.cs
99 lines (77 loc) · 3.26 KB
/
StateMachineDiagramPrinter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Stateless;
namespace Tests
{
public class StateMachineDiagramPrinter<TState, TTrigger>
{
private const string FullTemplate = @"digraph g{{
{0}
{1}
}}";
private readonly StateMachine<TState, TTrigger> _stateMachine;
public StateMachineDiagramPrinter(StateMachine<TState, TTrigger> stateMachine)
{
_stateMachine = stateMachine;
}
public string ToDiagram()
{
var stateConfigField = _stateMachine.GetType().GetField("_stateConfiguration", BindingFlags.Instance | BindingFlags.NonPublic);
var stateConfig = stateConfigField.GetValue(_stateMachine) as IEnumerable;
var results = IterateStates(stateConfig).ToList();
var allStates = results.SelectMany(t => new[] {t.From, t.To}).Distinct().Select(t => t.ToString());
return string.Format(FullTemplate, ToString(allStates), ToString(results));
}
private IEnumerable<Transition> IterateStates(IEnumerable stateConfig)
{
foreach (object o in stateConfig)
{
var fromState = (TState) GetPublicProperty(o, "Key");
var value = GetPublicProperty(o, "Value");
var triggersField = (IEnumerable) GetPrivateField(value, "_triggerBehaviours");
foreach (var trigger in triggersField)
{
var triggerValue = (TTrigger) GetPublicProperty(trigger, "Key");
var triggerValues = (IEnumerable) GetPublicProperty(trigger, "Value");
foreach (var transitionValue in triggerValues)
{
var toState = (TState) GetPrivateField(transitionValue, "_destination");
yield return new Transition(fromState, triggerValue, toState);
}
}
}
}
private object GetPublicProperty(object source, string propertyName)
{
var property = source.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance);
return property.GetValue(source);
}
private object GetPrivateField(object source, string fieldName)
{
var field = source.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
return field.GetValue(source);
}
private static string ToString(IEnumerable<string> states)
{
return string.Join(";\n", states) + ";\nnode[shape=plaintext]";
}
private static string ToString(IEnumerable<Transition> transitions)
{
return string.Join("\n", transitions.Select(t => string.Format("{0} -> {2} [label={1}]", t.From, t.Trigger, t.To)));
}
private class Transition
{
public TState From { get; private set; }
public TTrigger Trigger { get; private set; }
public TState To { get; private set; }
public Transition(TState @from, TTrigger transition, TState to)
{
From = @from;
Trigger = transition;
To = to;
}
}
}
}