Skip to content

Commit

Permalink
Add stars by event names on transitions which are guarded
Browse files Browse the repository at this point in the history
  • Loading branch information
canton7 committed Aug 6, 2015
1 parent 50868a7 commit a28b7ab
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/StateMechanic/ITransition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,10 @@ public interface ITransition<out TState> where TState : class, IState
/// Gets a value indicating whether this transition is an inner transition, i.e. whether the <see cref="From"/> and <see cref="To"/> states are the same, and no exit/entry handles are invoked
/// </summary>
bool IsInnerTransition { get; }

/// <summary>
/// Gets a value indicating whether this transition has a guard
/// </summary>
bool HasGuard { get; }
}
}
25 changes: 15 additions & 10 deletions src/StateMechanic/StateMachineDotPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@ namespace StateMechanic
/// </summary>
public class StateMachineDotPrinter
{
private static readonly string[] colors = new[]
{
"#0075dc", "#993f00", "#4c005c", "#191919", "#005c31", "#2bce48", "#808080", "#8f7c00", "#c20088", "#ffa405", "#ffa8bb",
"#426600", "#ff0010", "#00998f", "#740aff", "#990000", "#ff5005", "#4d4d4d", "#5da5da", "#faa43a", "#60bd68", "#f17cb0",
"#b2912f", "#b276b2", "#f15854"
};

private readonly IStateMachine stateMachine;

private Dictionary<IState, string> stateToColorMapping = new Dictionary<IState, string>();
private int colorUseCount = 0;

/// <summary>
/// Gets the list of colors that will be used if <see cref="Colorize"/> is true
/// </summary>
public List<string> Colors { get; private set; }

/// <summary>
/// Gets or sets a value indicating whether colors should be used
/// </summary>
Expand All @@ -34,6 +32,12 @@ public class StateMachineDotPrinter
public StateMachineDotPrinter(IStateMachine stateMachine)
{
this.stateMachine = stateMachine;
this.Colors = new List<string>()
{
"#0075dc", "#993f00", "#4c005c", "#191919", "#005c31", "#2bce48", "#808080", "#8f7c00", "#c20088", "#ffa405", "#ffa8bb",
"#426600", "#ff0010", "#00998f", "#740aff", "#990000", "#ff5005", "#4d4d4d", "#5da5da", "#faa43a", "#60bd68", "#f17cb0",
"#b2912f", "#b276b2", "#f15854"
};
}

/// <summary>
Expand Down Expand Up @@ -61,8 +65,8 @@ private string ColorForState(IState state)
if (this.stateToColorMapping.TryGetValue(state, out color))
return color;

color = colors[this.colorUseCount];
this.colorUseCount = (this.colorUseCount + 1) % colors.Length;
color = this.Colors[this.colorUseCount];
this.colorUseCount = (this.colorUseCount + 1) % this.Colors.Count;
this.stateToColorMapping[state] = color;

return color;
Expand Down Expand Up @@ -100,11 +104,12 @@ private void RenderStateMachine(StringBuilder sb, IStateMachine stateMachine, st
{
// If the source has a child state machine, then lhead is the name of that
// Likewise dest and ltail
sb.AppendFormat("{0}\"{1}\" -> \"{2}\" [label=\"{3}\"{4}{5}{6}];\n",
sb.AppendFormat("{0}\"{1}\" -> \"{2}\" [label=\"{3}{4}\"{5}{6}{7}];\n",
indent,
transition.From.ChildStateMachine == null ? transition.From.Name : transition.From.ChildStateMachine.InitialState.Name,
transition.To.ChildStateMachine == null ? transition.To.Name : transition.To.ChildStateMachine.InitialState.Name,
transition.Event.Name,
transition.HasGuard ? "*" : "",
this.Colorize && transition.To != stateMachine.InitialState ? String.Format(" color=\"{0}\" fontcolor=\"{0}\"", this.ColorForState(transition.To)) : "",
transition.From.ChildStateMachine == null ? "" : String.Format(" ltail=\"cluster_{0}\"", transition.From.Name),
transition.To.ChildStateMachine == null ? "" : String.Format(" lhead=\"cluster_{0}\"", transition.To.Name));
Expand Down
10 changes: 10 additions & 0 deletions src/StateMechanic/Transition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ public Func<TransitionInfo<TState>, bool> Guard
set { this.innerTransition.Guard = value; }
}

/// <summary>
/// Gets a value indicating whether this transition has a guard
/// </summary>
public bool HasGuard { get { return this.Guard != null; } }

internal Transition(ITransitionInner<TState, Event, TransitionInfo<TState>> innerTransition)
{
this.innerTransition = innerTransition;
Expand Down Expand Up @@ -192,6 +197,11 @@ public Func<TransitionInfo<TState, TEventData>, bool> Guard
set { this.innerTransition.Guard = value; }
}

/// <summary>
/// Gets a value indicating whether this transition has a guard
/// </summary>
public bool HasGuard { get { return this.Guard != null; } }

internal Transition(ITransitionInner<TState, Event<TEventData>, TransitionInfo<TState, TEventData>> innerTransition)
{
this.innerTransition = innerTransition;
Expand Down

0 comments on commit a28b7ab

Please sign in to comment.