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