-
Notifications
You must be signed in to change notification settings - Fork 0
ExportUml
Damian edited this page Dec 31, 2025
·
5 revisions
The Lite.StateMachine offers the ability to export UML of your state machine into the Graphviz (DOT graph) format. This is useful for documentation, discovering bugs early on, your blog posts, and more. The ExportUml(..) method features options to customize your graphs the way you see fit.
string ExportUml(...);
| Parameter | Default | Description |
|---|---|---|
IEnumerable<TStateId>? initialStateIds |
null | Entry-point initial state(s) at the top-level. |
bool includeLegend |
true | Include a legend subgraph that explains shapes and colors. |
IDictionary<Result, string>? transitionColors |
null | Color overrides for Result-based transitions. |
string parentToChildColor |
"Green" | Color used for parent → initial-child transition edges (default "Green"). |
string graphName |
"StateMachine" | Title of the graph (default "StateMachine"). |
bool rankLeftToRight |
true | Display as left-to-right layout or top-to-bottom. |
Func<TStateId, string>? nodeLabelSelector |
null | Optional label selector for nodes. |
string? legendText |
null | Optional custom legend description text. |
bool showParentInitialEdge |
true | Whether to also draw the parent → initial-child edge (green, labeled "initial") for composite parents. |
var machine = new StateMachine<StateId>()
.RegisterState<BasicState1>(StateId.State1, BasicStateId.State2)
.RegisterState<BasicState2>(StateId.State2, BasicStateId.State3)
.RegisterState<BasicState3>(StateId.State3);
var dot = machine.ExportUml(includeLegend: true, graphName: "Basic State Machine");
System.IO.File.WriteAllText("machine.dot", dot);Option to set a single or multiple initialStateId.
string dot = sm.ExportUml([StateId.State1], includeLegend: true);Use-case architecture example of multiple starting points:
if (appLicense == License.Pro) await machine.RunAsync(StateId.EntryProState);
if (appLicense == License.Demo) await machine.RunAsync(StateId.EntryDemoState);
// To set multiple initial states:
string dot = sm.ExportUml([StateId.EntryProState, StateId.EntryDemoState]);// Override transition colors & keep parent→initial edge (green)
var customColors = new Dictionary<Result, string>
{
[Result.Ok] = "#1E90FF", // DodgerBlue
[Result.Error] = "gold",
[Result.Failure] = "#FF3B30"
};
string dot = machine.ExportUml(
initialTopLevelStates: [StateId.State1],
includeLegend: true,
transitionColors: customColors,
parentToChildColor: "green3");Rendering DOT → SVG or PNG with Graphviz
using System.Diagnostics;
// Build DOT
string dot = sm.ExportUml([StateId.State1], includeLegend: true);
// Render to SVG
var psi = new ProcessStartInfo
{
FileName = "dot",
Arguments = "-Tsvg -o machine.svg",
RedirectStandardInput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
using var proc = Process.Start(psi)!;
proc.StandardInput.Write(dot);
proc.StandardInput.Close();
string err = proc.StandardError.ReadToEnd();
proc.WaitForExit();
if (proc.ExitCode != 0)
throw new InvalidOperationException($"Graphviz failed ({proc.ExitCode}): {err}");
// For PNG, change Arguments: "-Tpng -o machine.png"