| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using Elsa.Workflows.Activities; |
| | | 3 | | using Elsa.Workflows.Memory; |
| | | 4 | | using Elsa.Workflows.Models; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Workflows.Builders; |
| | | 7 | | |
| | | 8 | | /// <inheritdoc /> |
| | 1032 | 9 | | public class WorkflowBuilder(IActivityVisitor activityVisitor, IIdentityGraphService identityGraphService, IActivityRegi |
| | | 10 | | : IWorkflowBuilder |
| | | 11 | | { |
| | | 12 | | /// <inheritdoc /> |
| | 1040 | 13 | | public string? Id { get; set; } |
| | | 14 | | |
| | | 15 | | /// <inheritdoc /> |
| | 3570 | 16 | | public string? DefinitionId { get; set; } |
| | | 17 | | |
| | | 18 | | /// <inheritdoc /> |
| | 1096 | 19 | | public string? TenantId { get; set; } |
| | | 20 | | |
| | | 21 | | /// <inheritdoc /> |
| | 2068 | 22 | | public int Version { get; set; } = 1; |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | 1104 | 25 | | public string? Name { get; set; } |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | 1032 | 28 | | public string? Description { get; set; } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | 1032 | 31 | | public bool IsReadonly { get; set; } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | 1048 | 34 | | public bool IsSystem { get; set; } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | 2064 | 37 | | public IActivity? Root { get; set; } |
| | | 38 | | |
| | | 39 | | /// <inheritdoc /> |
| | 2274 | 40 | | public ICollection<Variable> Variables { get; set; } = new List<Variable>(); |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | 2189 | 43 | | public ICollection<InputDefinition> Inputs { get; set; } = new List<InputDefinition>(); |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | 2098 | 46 | | public ICollection<OutputDefinition> Outputs { get; set; } = new List<OutputDefinition>(); |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | 2064 | 49 | | public ICollection<string> Outcomes { get; set; } = new List<string>(); |
| | | 50 | | |
| | | 51 | | /// <inheritdoc /> |
| | 4128 | 52 | | public Variable? Result { get; set; } = new(); |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | 2064 | 55 | | public IDictionary<string, object> CustomProperties { get; set; } = new Dictionary<string, object>(); |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | 2155 | 58 | | public WorkflowOptions WorkflowOptions { get; } = new(); |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | | 61 | | public IWorkflowBuilder WithDefinitionId(string definitionId) |
| | | 62 | | { |
| | 910 | 63 | | DefinitionId = definitionId; |
| | 910 | 64 | | return this; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | public IWorkflowBuilder WithId(string id) |
| | | 68 | | { |
| | 0 | 69 | | Id = id; |
| | 0 | 70 | | return this; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <inheritdoc /> |
| | | 74 | | public IWorkflowBuilder WithTenantId(string tenantId) |
| | | 75 | | { |
| | 32 | 76 | | TenantId = tenantId; |
| | 32 | 77 | | return this; |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <inheritdoc /> |
| | | 81 | | [Obsolete("Use the overload that takes a name instead. This overload will be removed in a future version.")] |
| | | 82 | | public Variable<T> WithVariable<T>() |
| | | 83 | | { |
| | 67 | 84 | | var variable = new Variable<T>(null!, default!); |
| | 67 | 85 | | Variables.Add(variable); |
| | 67 | 86 | | variable.WithWorkflowStorage(); |
| | 67 | 87 | | variable.Id = null!; // This ensures that a deterministic ID is assigned by the builder. |
| | 67 | 88 | | return variable; |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <inheritdoc /> |
| | | 92 | | public Variable<T> WithVariable<T>(string name, T value) |
| | | 93 | | { |
| | 126 | 94 | | var variable = new Variable<T>(name, value); |
| | 126 | 95 | | Variables.Add(variable); |
| | 126 | 96 | | return variable; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <inheritdoc /> |
| | | 100 | | [Obsolete("Use the overload that takes a name instead. This overload will be removed in a future version.")] |
| | | 101 | | public Variable<T> WithVariable<T>(T value) |
| | | 102 | | { |
| | 0 | 103 | | var variable = WithVariable<T>(); |
| | 0 | 104 | | variable.Value = value; |
| | 0 | 105 | | return variable; |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <inheritdoc /> |
| | | 109 | | public IWorkflowBuilder WithVariable<T>(Variable<T> variable) |
| | | 110 | | { |
| | 17 | 111 | | Variables.Add(variable); |
| | 17 | 112 | | return this; |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <inheritdoc /> |
| | | 116 | | public IWorkflowBuilder WithVariable(Variable variable) |
| | | 117 | | { |
| | 0 | 118 | | Variables.Add(variable); |
| | 0 | 119 | | return this; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <inheritdoc /> |
| | | 123 | | public IWorkflowBuilder WithVariables(params Variable[] variables) |
| | | 124 | | { |
| | 0 | 125 | | foreach (var variable in variables) |
| | 0 | 126 | | Variables.Add(variable); |
| | 0 | 127 | | return this; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <inheritdoc /> |
| | | 131 | | public InputDefinition WithInput<T>(string name, string? description = default) |
| | | 132 | | { |
| | 125 | 133 | | return WithInput(name, typeof(T), description); |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <inheritdoc /> |
| | | 137 | | public InputDefinition WithInput(string name, Type type, string? description = default) |
| | | 138 | | { |
| | 125 | 139 | | return WithInput(inputDefinition => |
| | 125 | 140 | | { |
| | 125 | 141 | | inputDefinition.Name = name; |
| | 125 | 142 | | inputDefinition.Type = type; |
| | 125 | 143 | | |
| | 125 | 144 | | if (description != null) |
| | 16 | 145 | | inputDefinition.Description = description; |
| | 250 | 146 | | }); |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <inheritdoc /> |
| | | 150 | | public InputDefinition WithInput(string name, Type type, Action<InputDefinition>? setup = default) |
| | | 151 | | { |
| | 0 | 152 | | return WithInput(inputDefinition => |
| | 0 | 153 | | { |
| | 0 | 154 | | inputDefinition.Name = name; |
| | 0 | 155 | | inputDefinition.Type = type; |
| | 0 | 156 | | setup?.Invoke(inputDefinition); |
| | 0 | 157 | | }); |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <inheritdoc /> |
| | | 161 | | public InputDefinition WithInput(Action<InputDefinition> setup) |
| | | 162 | | { |
| | 125 | 163 | | var inputDefinition = new InputDefinition(); |
| | 125 | 164 | | setup(inputDefinition); |
| | 125 | 165 | | Inputs.Add(inputDefinition); |
| | 125 | 166 | | return inputDefinition; |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | /// <inheritdoc /> |
| | | 170 | | public IWorkflowBuilder WithInput(InputDefinition inputDefinition) |
| | | 171 | | { |
| | 0 | 172 | | Inputs.Add(inputDefinition); |
| | 0 | 173 | | return this; |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | public OutputDefinition WithOutput<T>(string name, string? description = default) |
| | | 177 | | { |
| | 34 | 178 | | return WithOutput(name, typeof(T), description); |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | public OutputDefinition WithOutput(string name, Type type, string? description = default) |
| | | 182 | | { |
| | 34 | 183 | | return WithOutput(outputDefinition => |
| | 34 | 184 | | { |
| | 34 | 185 | | outputDefinition.Name = name; |
| | 34 | 186 | | outputDefinition.Type = type; |
| | 34 | 187 | | |
| | 34 | 188 | | if (description != null) |
| | 0 | 189 | | outputDefinition.Description = description; |
| | 68 | 190 | | }); |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | public OutputDefinition WithOutput(string name, Type type, Action<OutputDefinition>? setup = default) |
| | | 194 | | { |
| | 0 | 195 | | return WithOutput(outputDefinition => |
| | 0 | 196 | | { |
| | 0 | 197 | | outputDefinition.Name = name; |
| | 0 | 198 | | outputDefinition.Type = type; |
| | 0 | 199 | | setup?.Invoke(outputDefinition); |
| | 0 | 200 | | }); |
| | | 201 | | } |
| | | 202 | | |
| | | 203 | | public OutputDefinition WithOutput(Action<OutputDefinition> setup) |
| | | 204 | | { |
| | 34 | 205 | | var outputDefinition = new OutputDefinition(); |
| | 34 | 206 | | setup(outputDefinition); |
| | 34 | 207 | | return WithOutput(outputDefinition); |
| | | 208 | | } |
| | | 209 | | |
| | | 210 | | public OutputDefinition WithOutput(OutputDefinition outputDefinition) |
| | | 211 | | { |
| | 34 | 212 | | Outputs.Add(outputDefinition); |
| | 34 | 213 | | return outputDefinition; |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | /// <inheritdoc /> |
| | | 217 | | public IWorkflowBuilder WithCustomProperty(string name, object value) |
| | | 218 | | { |
| | 0 | 219 | | CustomProperties[name] = value; |
| | 0 | 220 | | return this; |
| | | 221 | | } |
| | | 222 | | |
| | | 223 | | /// <inheritdoc /> |
| | | 224 | | public IWorkflowBuilder WithActivationStrategyType<T>() where T : IWorkflowActivationStrategy |
| | | 225 | | { |
| | 0 | 226 | | WorkflowOptions.ActivationStrategyType = typeof(T); |
| | 0 | 227 | | return this; |
| | | 228 | | } |
| | | 229 | | |
| | | 230 | | /// <summary> |
| | | 231 | | /// Marks the workflow as readonly. |
| | | 232 | | /// </summary> |
| | | 233 | | public IWorkflowBuilder AsReadonly() |
| | | 234 | | { |
| | 0 | 235 | | IsReadonly = true; |
| | 0 | 236 | | return this; |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | /// <summary> |
| | | 240 | | /// Marks the workflow as a system workflow. |
| | | 241 | | /// System workflows are not visible in the UI by default and are not meant to be modified by users. |
| | | 242 | | /// </summary> |
| | | 243 | | public IWorkflowBuilder AsSystemWorkflow() |
| | | 244 | | { |
| | 16 | 245 | | IsSystem = true; |
| | 16 | 246 | | return this; |
| | | 247 | | } |
| | | 248 | | |
| | | 249 | | /// <inheritdoc /> |
| | | 250 | | public async Task<Workflow> BuildWorkflowAsync(CancellationToken cancellationToken = default) |
| | | 251 | | { |
| | 1032 | 252 | | var definitionId = string.IsNullOrEmpty(DefinitionId) ? string.Empty : DefinitionId; |
| | 1032 | 253 | | var id = string.IsNullOrEmpty(Id) ? string.Empty : Id; |
| | 1032 | 254 | | var tenantId = string.IsNullOrEmpty(TenantId) ? null : TenantId; |
| | 1032 | 255 | | var root = Root ?? new Sequence(); |
| | 1032 | 256 | | var identity = new WorkflowIdentity(definitionId, Version, id, tenantId); |
| | 1032 | 257 | | var publication = WorkflowPublication.LatestAndPublished; |
| | 1032 | 258 | | var name = string.IsNullOrEmpty(Name) ? definitionId : Name; |
| | 1032 | 259 | | var workflowMetadata = new WorkflowMetadata(name, Description); |
| | 1032 | 260 | | var workflow = new Workflow(identity, publication, workflowMetadata, WorkflowOptions, root, Variables, Inputs, O |
| | | 261 | | |
| | | 262 | | // If a Result variable is defined, install it into the workflow, so we can capture the output into it. |
| | 1032 | 263 | | if (Result != null) |
| | | 264 | | { |
| | 1032 | 265 | | workflow.ResultVariable = Result; |
| | 1032 | 266 | | workflow.Result = new Output<object>(Result); |
| | | 267 | | } |
| | | 268 | | |
| | 1032 | 269 | | var graph = await activityVisitor.VisitAsync(workflow, cancellationToken); |
| | 1032 | 270 | | var nodes = graph.Flatten().ToList(); |
| | | 271 | | |
| | | 272 | | // Register all activity types first. The identity graph service will need to know about all activity types. |
| | 5167 | 273 | | var distinctActivityTypes = nodes.Select(x => x.Activity.GetType()).Distinct().ToList(); |
| | 1032 | 274 | | await activityRegistry.RegisterAsync(distinctActivityTypes, cancellationToken); |
| | | 275 | | |
| | | 276 | | // Assign identities to all activities. |
| | 1032 | 277 | | await identityGraphService.AssignIdentitiesAsync(nodes); |
| | | 278 | | |
| | | 279 | | // Give unnamed variables in each variable container a predictable name. |
| | 7088 | 280 | | var variableContainers = nodes.Where(x => x.Activity is IVariableContainer).Select(x => (IVariableContainer)x.Ac |
| | | 281 | | |
| | 5906 | 282 | | foreach (var container in variableContainers) |
| | | 283 | | { |
| | 1921 | 284 | | var index = 0; |
| | 2133 | 285 | | var unnamedVariables = container.Variables.Where(x => string.IsNullOrWhiteSpace(x.Name)).ToList(); |
| | | 286 | | |
| | 4010 | 287 | | foreach (var unnamedVariable in unnamedVariables) |
| | 84 | 288 | | unnamedVariable.Name = $"Variable_{index++}"; |
| | | 289 | | } |
| | | 290 | | |
| | 1032 | 291 | | return workflow; |
| | 1032 | 292 | | } |
| | | 293 | | |
| | | 294 | | /// <inheritdoc /> |
| | | 295 | | public async Task<Workflow> BuildWorkflowAsync(IWorkflow workflowDefinition, CancellationToken cancellationToken = d |
| | | 296 | | { |
| | 558 | 297 | | DefinitionId = workflowDefinition.GetType().Name; |
| | 558 | 298 | | await workflowDefinition.BuildAsync(this, cancellationToken); |
| | 558 | 299 | | return await BuildWorkflowAsync(cancellationToken); |
| | 558 | 300 | | } |
| | | 301 | | } |