Lesson 3: Decisions

An incident is routed by severity: a critical incident pages the on-call engineer and, when it is customer facing, notifies the support desk; other incidents go to a triage task, whose answer closes or escalates the incident.

A decision on an expression

// A decision on any boolean expression.
.WhenExpression(
    "Critical?",
    WorkflowExpr.Eq(WorkflowExpr.Path("state.severity"), WorkflowExpr.String("critical")),
    critical => critical
        .Call("Page on-call engineer", new EndpointAddress("paging", "/pages"),
            WorkflowExpr.Obj(WorkflowExpr.Prop("incidentId", WorkflowExpr.Path("state.incidentId"))))

A nested decision on a state flag

// A nested decision on a boolean state value.
.WhenStateFlag(
    "customerFacing",
    true,
    "Customer facing?",
    customerFacing => customerFacing
        .Call("Notify support desk", new HttpAddress("support", "/notifications"),
            WorkflowExpr.Obj(WorkflowExpr.Prop("incidentId", WorkflowExpr.Path("state.incidentId"))))
        .Complete(),
    internalOnly => internalOnly.Complete()),

A decision on the completion payload

// A decision on a field of the completion payload ("payload.action").
.OnComplete(flow => flow
    .WhenPayloadEquals(
        "action",
        "escalate",
        "Escalate?",
        escalate => escalate
            .Set("severity", WorkflowExpr.String("critical"))
            .Call("Page on-call engineer", new EndpointAddress("paging", "/pages"),
                WorkflowExpr.Obj(WorkflowExpr.Prop("incidentId", WorkflowExpr.Path("state.incidentId"))))
            .Complete(),
        close => close.Complete())))

Concepts

The complete code

The lesson’s complete code. It uses the namespace of the tutorials; put the classes in a namespace of yours.

IncidentTriageWorkflow.cs:

using StellaOps.Workflow.Abstractions;
using StellaOps.Workflow.Contracts;

namespace Examples.Workflow.Tutorials.Decisions;

public sealed class IncidentTriageRequest
{
    [WorkflowBusinessId]
    public string IncidentId { get; set; } = string.Empty;

    public string Severity { get; set; } = string.Empty;

    public bool CustomerFacing { get; set; }
}

/// <summary>
/// Routes an incident by severity. A critical incident pages the on-call engineer; a customer-facing
/// one also notifies the support desk; everything else goes to the triage queue, where the triager
/// either closes the incident or escalates it.
/// </summary>
public sealed class IncidentTriageWorkflow : IDeclarativeWorkflow<IncidentTriageRequest>
{
    public string WorkflowName => "IncidentTriage";

    public string WorkflowVersion => "1.0.0";

    public string DisplayName => "Incident triage";

    public IReadOnlyCollection<string> WorkflowRoles { get; } = ["triager"];

    public WorkflowSpec<IncidentTriageRequest> Spec { get; } = WorkflowSpec.For<IncidentTriageRequest>()
        .InitializeState(
            WorkflowExpr.Obj(
                WorkflowExpr.Prop("incidentId", WorkflowExpr.Path("start.incidentId")),
                WorkflowExpr.Prop("severity", WorkflowExpr.Path("start.severity")),
                WorkflowExpr.Prop("customerFacing", WorkflowExpr.Path("start.customerFacing"))))
        .AddTask(
            WorkflowHumanTask.For<IncidentTriageRequest>("Triage incident", "IncidentTriage", "incidents/triage")
                .WithPayload(WorkflowExpr.Obj(WorkflowExpr.Prop("incidentId", WorkflowExpr.Path("state.incidentId"))))
                // A decision on a field of the completion payload ("payload.action").
                .OnComplete(flow => flow
                    .WhenPayloadEquals(
                        "action",
                        "escalate",
                        "Escalate?",
                        escalate => escalate
                            .Set("severity", WorkflowExpr.String("critical"))
                            .Call("Page on-call engineer", new EndpointAddress("paging", "/pages"),
                                WorkflowExpr.Obj(WorkflowExpr.Prop("incidentId", WorkflowExpr.Path("state.incidentId"))))
                            .Complete(),
                        close => close.Complete())))
        .StartWith(flow => flow
            // A decision on any boolean expression.
            .WhenExpression(
                "Critical?",
                WorkflowExpr.Eq(WorkflowExpr.Path("state.severity"), WorkflowExpr.String("critical")),
                critical => critical
                    .Call("Page on-call engineer", new EndpointAddress("paging", "/pages"),
                        WorkflowExpr.Obj(WorkflowExpr.Prop("incidentId", WorkflowExpr.Path("state.incidentId"))))
                    // A nested decision on a boolean state value.
                    .WhenStateFlag(
                        "customerFacing",
                        true,
                        "Customer facing?",
                        customerFacing => customerFacing
                            .Call("Notify support desk", new HttpAddress("support", "/notifications"),
                                WorkflowExpr.Obj(WorkflowExpr.Prop("incidentId", WorkflowExpr.Path("state.incidentId"))))
                            .Complete(),
                        internalOnly => internalOnly.Complete()),
                routine => routine.ActivateTask("Triage incident")))
        .Build();

    public IReadOnlyCollection<WorkflowTaskDescriptor> Tasks => Spec.TaskDescriptors;
}

The canonical form

The C# compiles to a canonical JSON definition, the document the engine validates, stores and renders. Write it to a file with the export helper of lesson 9:

Next: Lesson 4: Human tasks.