| | | 1 | | using Elsa.Expressions.Helpers; |
| | | 2 | | using Elsa.Expressions.Models; |
| | | 3 | | using Humanizer; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows.Memory; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Represents a variable that references a memory block. |
| | | 9 | | /// </summary> |
| | | 10 | | public class Variable : MemoryBlockReference |
| | | 11 | | { |
| | | 12 | | /// <inheritdoc /> |
| | 1523 | 13 | | public Variable() |
| | | 14 | | { |
| | 1523 | 15 | | } |
| | | 16 | | |
| | | 17 | | /// <inheritdoc /> |
| | 447 | 18 | | public Variable(string name) |
| | | 19 | | { |
| | 447 | 20 | | Id = GetIdFromName(name); |
| | 447 | 21 | | Name = name; |
| | 447 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | 447 | 25 | | public Variable(string name, object? value = null) : this(name) |
| | | 26 | | { |
| | 447 | 27 | | Value = value; |
| | 447 | 28 | | } |
| | | 29 | | |
| | 7 | 30 | | public Variable(string name, object? value = null, string? id = null) : this(name, value) |
| | | 31 | | { |
| | 7 | 32 | | if (id != null) |
| | | 33 | | { |
| | 7 | 34 | | Id = id; |
| | | 35 | | } |
| | 7 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// The name of the variable. |
| | | 40 | | /// </summary> |
| | 2402 | 41 | | public string Name { get; set; } = null!; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// A default value for the variable. |
| | | 45 | | /// </summary> |
| | 1367 | 46 | | public object? Value { get; set; } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// The storage driver type to use for persistence. |
| | | 50 | | /// If no driver is specified, the referenced memory block will remain in memory for as long as the expression execu |
| | | 51 | | /// </summary> |
| | 1212 | 52 | | public Type? StorageDriverType { get; set; } |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | 285 | 55 | | public override MemoryBlock Declare() => new(Value, new VariableBlockMetadata(this, StorageDriverType, false)); |
| | | 56 | | |
| | 447 | 57 | | private string GetIdFromName(string? name) => $"{name?.Camelize() ?? "Unnamed"}{nameof(Variable)}"; |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Represents a variable that references a memory block. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <typeparam name="T">The type of the variable.</typeparam> |
| | | 64 | | public class Variable<T> : Variable |
| | | 65 | | { |
| | | 66 | | /// <inheritdoc /> |
| | | 67 | | public Variable() |
| | | 68 | | { |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <inheritdoc /> |
| | | 72 | | [Obsolete("Use the constructor that takes a name parameter instead.", true)] |
| | | 73 | | public Variable(T value) |
| | | 74 | | { |
| | | 75 | | Value = value; |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <inheritdoc /> |
| | | 79 | | public Variable(string name, T value) : base(name, value) |
| | | 80 | | { |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | public Variable(string name, T value, string? id = null) : base(name, value, id) |
| | | 84 | | { |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Gets the value of the variable. |
| | | 89 | | /// </summary> |
| | | 90 | | public T? Get(ActivityExecutionContext context) => Get(context.ExpressionExecutionContext).ConvertTo<T?>(); |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Gets the value of the variable. |
| | | 94 | | /// </summary> |
| | | 95 | | public new T? Get(ExpressionExecutionContext context) => base.Get(context).ConvertTo<T?>(); |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Sets the <see cref="Variable.StorageDriverType"/> to the specified type. |
| | | 99 | | /// </summary> |
| | | 100 | | public Variable<T> WithStorageDriver<TDriver>() where TDriver:IStorageDriver |
| | | 101 | | { |
| | | 102 | | StorageDriverType = typeof(TDriver); |
| | | 103 | | return this; |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | public Variable<T> WithId(string id) |
| | | 107 | | { |
| | | 108 | | Id = id; |
| | | 109 | | return this; |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | public Variable<T> WithName(string name) |
| | | 113 | | { |
| | | 114 | | Name = name; |
| | | 115 | | return this; |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | public Variable<T> WithValue(T value) |
| | | 119 | | { |
| | | 120 | | Value = value; |
| | | 121 | | return this; |
| | | 122 | | } |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Provides metadata about the variable block. |
| | | 127 | | /// </summary> |
| | | 128 | | public record VariableBlockMetadata(Variable Variable, Type? StorageDriverType, bool IsInitialized); |