-
Notifications
You must be signed in to change notification settings - Fork 768
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #559 from sulmar/add-mermaid-graph-style
Add mermaid graph style
- Loading branch information
Showing
4 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Stateless.Reflection; | ||
|
||
namespace Stateless.Graph | ||
{ | ||
/// <summary> | ||
/// Class to generate a MermaidGraph | ||
/// </summary> | ||
public static class MermaidGraph | ||
{ | ||
/// <summary> | ||
/// Generate a Mermaid graph from the state machine info | ||
/// </summary> | ||
/// <param name="machineInfo"></param> | ||
/// <returns></returns> | ||
public static string Format(StateMachineInfo machineInfo) | ||
{ | ||
var graph = new StateGraph(machineInfo); | ||
|
||
return graph.ToGraph(new MermaidGraphStyle()); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using Stateless.Reflection; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection.Emit; | ||
using System.Text; | ||
|
||
namespace Stateless.Graph | ||
{ | ||
/// <summary> | ||
/// Class to generate a graph in mermaid format | ||
/// </summary> | ||
public class MermaidGraphStyle : GraphStyleBase | ||
{ | ||
/// <summary> | ||
/// Returns the formatted text for a single superstate and its substates. | ||
/// For example, for DOT files this would be a subgraph containing nodes for all the substates. | ||
/// </summary> | ||
/// <param name="stateInfo">The superstate to generate text for</param> | ||
/// <returns>Description of the superstate, and all its substates, in the desired format</returns> | ||
public override string FormatOneCluster(SuperState stateInfo) | ||
{ | ||
string stateRepresentationString = ""; | ||
return stateRepresentationString; | ||
} | ||
|
||
/// <summary> | ||
/// Generate the text for a single decision node | ||
/// </summary> | ||
/// <param name="nodeName">Name of the node</param> | ||
/// <param name="label">Label for the node</param> | ||
/// <returns></returns> | ||
public override string FormatOneDecisionNode(string nodeName, string label) | ||
{ | ||
return String.Empty; | ||
} | ||
|
||
/// <summary> | ||
/// Generate the text for a single state | ||
/// </summary> | ||
/// <param name="state">The state to generate text for</param> | ||
/// <returns></returns> | ||
public override string FormatOneState(State state) | ||
{ | ||
return String.Empty; | ||
} | ||
|
||
/// <summary>Get the text that starts a new graph</summary> | ||
/// <returns></returns> | ||
public override string GetPrefix() | ||
{ | ||
return "stateDiagram-v2"; | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="initialState"></param> | ||
/// <returns></returns> | ||
public override string GetInitialTransition(StateInfo initialState) | ||
{ | ||
return $"\r\n[*] --> {initialState}"; | ||
} | ||
|
||
|
||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="sourceNodeName"></param> | ||
/// <param name="trigger"></param> | ||
/// <param name="actions"></param> | ||
/// <param name="destinationNodeName"></param> | ||
/// <param name="guards"></param> | ||
/// <returns></returns> | ||
public override string FormatOneTransition(string sourceNodeName, string trigger, IEnumerable<string> actions, string destinationNodeName, IEnumerable<string> guards) | ||
{ | ||
string label = trigger ?? ""; | ||
|
||
return FormatOneLine(sourceNodeName, destinationNodeName, label); | ||
} | ||
|
||
internal string FormatOneLine(string fromNodeName, string toNodeName, string label) | ||
{ | ||
return $"\t{fromNodeName} --> {toNodeName} : {label}"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Xunit; | ||
|
||
namespace Stateless.Tests | ||
{ | ||
public class MermaidGraphFixture | ||
{ | ||
[Fact] | ||
public void Format_InitialTransition_ShouldReturns() | ||
{ | ||
var expected = "stateDiagram-v2\r\n[*] --> A"; | ||
|
||
var sm = new StateMachine<State, Trigger>(State.A); | ||
|
||
var result = Graph.MermaidGraph.Format(sm.GetInfo()); | ||
|
||
Assert.Equal(expected, result); | ||
|
||
} | ||
|
||
[Fact] | ||
public void Format_SimpleTransition() | ||
{ | ||
var expected = "stateDiagram-v2\r\n\tA --> B : X\r\n[*] --> A"; | ||
|
||
var sm = new StateMachine<State, Trigger>(State.A); | ||
|
||
sm.Configure(State.A) | ||
.Permit(Trigger.X, State.B); | ||
|
||
var result = Graph.MermaidGraph.Format(sm.GetInfo()); | ||
|
||
Assert.Equal(expected, result); | ||
|
||
} | ||
|
||
[Fact] | ||
public void TwoSimpleTransitions() | ||
{ | ||
var expected = """ | ||
stateDiagram-v2 | ||
A --> B : X | ||
A --> C : Y | ||
"""; | ||
|
||
var sm = new StateMachine<State, Trigger>(State.A); | ||
|
||
sm.Configure(State.A) | ||
.Permit(Trigger.X, State.B) | ||
.Permit(Trigger.Y, State.C); | ||
|
||
var result = Graph.MermaidGraph.Format(sm.GetInfo()); | ||
|
||
Assert.Equal(expected, result); | ||
|
||
} | ||
} | ||
} |