Lesson 10: Rendering
A registered workflow rendered as an SVG diagram, outside any host: for a documentation build, a design review or a test.
/// <summary>Renders a registered workflow as an SVG diagram, outside any HTTP request.</summary>
public static class DiagramExport
{
public static async Task<string> RenderSvgAsync(
IServiceProvider services,
string workflowName,
CancellationToken cancellationToken = default)
{
// The canonical definition of the registered workflow, compiled into a graph of nodes and edges.
var definition = services.GetRequiredService<IWorkflowRuntimeDefinitionStore>().GetRequiredDefinition(workflowName);
var graph = services.GetRequiredService<IWorkflowRenderGraphCompiler>().Compile(definition);
// The layout engine named by WorkflowRendering:LayoutProvider (ElkSharp when unset).
var engine = services.GetRequiredService<IWorkflowRenderLayoutEngineResolver>().Resolve();
var layout = await engine.LayoutAsync(
graph,
new WorkflowRenderLayoutRequest
{
Direction = WorkflowRenderLayoutDirection.LeftToRight,
Effort = WorkflowRenderLayoutEffort.Balanced,
},
cancellationToken);
// The SVG renderer adds the licence notice when the host passes none.
return services.GetRequiredService<IWorkflowRenderSvgRenderer>()
.RenderSvg(layout, definition.Descriptor.DisplayName, licenceNotice: null);
}
}
The services it needs
AddWorkflowEngineCoreServices, AddWorkflowElkSharpRenderLayout (package StellaOps.Workflow.Renderer.ElkSharp), AddWorkflowSvgRenderer (package StellaOps.Workflow.Renderer.Svg) and the workflow’s registration. No store is needed.
Concepts
- The render graph compiler turns the canonical definition into nodes and edges.
- The layout engine positions them: direction, spacing and effort (
Draft,Balanced,Best). - The SVG renderer draws the positioned graph and adds the licence notice along the bottom edge.
- Over HTTP,
GET /api/workflow/diagrams/{name}returns the positioned graph as JSON.
More: Rendering.
The complete code
The lesson’s complete code. It uses the namespace of the tutorials; put the classes in a namespace of yours.
DiagramExport.cs:
using Microsoft.Extensions.DependencyInjection;
using StellaOps.Workflow.Abstractions;
namespace Examples.Workflow.Tutorials.Rendering;
/// <summary>Renders a registered workflow as an SVG diagram, outside any HTTP request.</summary>
public static class DiagramExport
{
public static async Task<string> RenderSvgAsync(
IServiceProvider services,
string workflowName,
CancellationToken cancellationToken = default)
{
// The canonical definition of the registered workflow, compiled into a graph of nodes and edges.
var definition = services.GetRequiredService<IWorkflowRuntimeDefinitionStore>().GetRequiredDefinition(workflowName);
var graph = services.GetRequiredService<IWorkflowRenderGraphCompiler>().Compile(definition);
// The layout engine named by WorkflowRendering:LayoutProvider (ElkSharp when unset).
var engine = services.GetRequiredService<IWorkflowRenderLayoutEngineResolver>().Resolve();
var layout = await engine.LayoutAsync(
graph,
new WorkflowRenderLayoutRequest
{
Direction = WorkflowRenderLayoutDirection.LeftToRight,
Effort = WorkflowRenderLayoutEffort.Balanced,
},
cancellationToken);
// The SVG renderer adds the licence notice when the host passes none.
return services.GetRequiredService<IWorkflowRenderSvgRenderer>()
.RenderSvg(layout, definition.Descriptor.DisplayName, licenceNotice: null);
}
}
