| | | 1 | | using System.Linq.Expressions; |
| | | 2 | | using System.Reflection; |
| | | 3 | | using Elsa.Expressions.Helpers; |
| | | 4 | | using Elsa.Expressions.Models; |
| | | 5 | | using Elsa.Workflows; |
| | | 6 | | using Elsa.Workflows.Models; |
| | | 7 | | using JetBrains.Annotations; |
| | | 8 | | |
| | | 9 | | // ReSharper disable once CheckNamespace |
| | | 10 | | namespace Elsa.Extensions; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Provides extension methods for <see cref="IActivity"/>. |
| | | 14 | | /// </summary> |
| | | 15 | | [PublicAPI] |
| | | 16 | | public static class ActivityExtensions |
| | | 17 | | { |
| | | 18 | | /// <param name="activity">The activity to get the output from.</param> |
| | | 19 | | extension(IActivity activity) |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Gets the input properties of the specified activity. |
| | | 23 | | /// </summary> |
| | | 24 | | public IDictionary<string, Input> GetNamedInputs() |
| | | 25 | | { |
| | 61 | 26 | | var inputProps = activity.GetInputProperties().ToList(); |
| | | 27 | | |
| | 61 | 28 | | var query = |
| | 61 | 29 | | from inputProp in inputProps |
| | 484 | 30 | | let inputValue = (Input?)inputProp.GetValue(activity) |
| | 484 | 31 | | where inputValue != null |
| | 263 | 32 | | select (inputProp, inputValue); |
| | | 33 | | |
| | 667 | 34 | | return query.DistinctBy(x => x.inputProp.Name).ToDictionary(x => x.inputProp.Name, x => x.inputValue); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets the input properties of the specified activity. |
| | | 39 | | /// </summary> |
| | 0 | 40 | | public IEnumerable<Input> GetInputs() => GetNamedInputs(activity).Values; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets the input with the specified name. |
| | | 44 | | /// </summary> |
| | 0 | 45 | | public Input? GetInput(string inputName) => GetNamedInputs(activity).TryGetValue(inputName, out var input) ? inp |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets the output properties of the specified activity. |
| | | 49 | | /// </summary> |
| | | 50 | | public IEnumerable<NamedOutput> GetOutputs() |
| | | 51 | | { |
| | 290181 | 52 | | var outputProps = activity.GetType().GetProperties().Where(x => typeof(Output).IsAssignableFrom(x.PropertyTy |
| | | 53 | | |
| | 15135 | 54 | | var query = |
| | 15135 | 55 | | from outputProp in outputProps |
| | 7725 | 56 | | let output = (Output?)outputProp.GetValue(activity) |
| | 7725 | 57 | | where output != null |
| | 17427 | 58 | | select new NamedOutput(outputProp.Name, output); |
| | | 59 | | |
| | 17427 | 60 | | return query.Select(x => x!).ToList(); |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Gets the output with the specified name. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="context">The activity execution context.</param> |
| | | 67 | | /// <param name="outputName">Name of the output.</param> |
| | | 68 | | /// <returns>The output value.</returns> |
| | | 69 | | public object? GetOutput(ActivityExecutionContext context, string? outputName = null) |
| | | 70 | | { |
| | 1818 | 71 | | var workflowExecutionContext = context.WorkflowExecutionContext; |
| | 1818 | 72 | | var outputRegister = workflowExecutionContext.GetActivityOutputRegister(); |
| | | 73 | | |
| | | 74 | | // If the provided activity execution context is the same as the current activity's execution context, we re |
| | 1818 | 75 | | if(context.Activity.NodeId == activity.NodeId) |
| | 1818 | 76 | | return outputRegister.FindOutputByActivityInstanceId(context.Id, outputName); |
| | | 77 | | |
| | | 78 | | // If the provided activity execution context is different from the current activity's execution context, we |
| | 0 | 79 | | return outputRegister.FindOutputByActivityId(activity.Id, outputName); |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Gets the output with the specified name. |
| | | 84 | | /// </summary> |
| | | 85 | | /// <param name="context">The expression execution context.</param> |
| | | 86 | | /// <param name="outputName">Name of the output.</param> |
| | | 87 | | /// <returns>The output value.</returns> |
| | | 88 | | public object? GetOutput(ExpressionExecutionContext context, string? outputName = null) |
| | | 89 | | { |
| | 1818 | 90 | | var activityExecutionContext = context.GetActivityExecutionContext(); |
| | | 91 | | |
| | 1818 | 92 | | if (activityExecutionContext == null) |
| | 0 | 93 | | return null; |
| | | 94 | | |
| | 1818 | 95 | | return activity.GetOutput(activityExecutionContext, outputName); |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Gets the output with the specified name. |
| | | 100 | | /// </summary> |
| | | 101 | | /// <param name="context">The context.</param> |
| | | 102 | | /// <param name="outputName">Name of the output.</param> |
| | | 103 | | /// <typeparam name="T">The type of the output.</typeparam> |
| | | 104 | | /// <returns>The output value.</returns> |
| | | 105 | | public T? GetOutput<T>(ActivityExecutionContext context, string outputName) |
| | | 106 | | { |
| | 0 | 107 | | var outputValue = activity.GetOutput(context, outputName); |
| | 0 | 108 | | return outputValue == null ? default! : outputValue.ConvertTo<T>(); |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Gets the output with the specified name. |
| | | 113 | | /// </summary> |
| | | 114 | | /// <param name="context">The context.</param> |
| | | 115 | | /// <param name="outputName">Name of the output.</param> |
| | | 116 | | /// <typeparam name="T">The type of the output.</typeparam> |
| | | 117 | | /// <returns>The output value.</returns> |
| | | 118 | | public T? GetOutput<T>(ExpressionExecutionContext context, string outputName) |
| | | 119 | | { |
| | 0 | 120 | | return activity.GetOutput<T>(context.GetActivityExecutionContext(), outputName); |
| | | 121 | | } |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <param name="activity">The activity.</param> |
| | | 125 | | /// <typeparam name="TActivity">The type of the activity.</typeparam> |
| | | 126 | | extension<TActivity>(TActivity activity) |
| | | 127 | | { |
| | | 128 | | /// <summary> |
| | | 129 | | /// Gets the output with the specified name. |
| | | 130 | | /// </summary> |
| | | 131 | | /// <param name="context">The context.</param> |
| | | 132 | | /// <param name="outputExpression">The output expression.</param> |
| | | 133 | | /// <typeparam name="T">The type of the output.</typeparam> |
| | | 134 | | /// <returns>The output value.</returns> |
| | | 135 | | public T? GetOutput<T>(ActivityExecutionContext context, Expression<Func<TActivity, object?>> outputExpression) |
| | | 136 | | { |
| | 0 | 137 | | var outputName = outputExpression.GetPropertyName(); |
| | 0 | 138 | | return ((IActivity)activity!).GetOutput<T>(context, outputName); |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Gets the output with the specified name. |
| | | 143 | | /// </summary> |
| | | 144 | | /// <param name="context">The context.</param> |
| | | 145 | | /// <param name="outputExpression">The output expression.</param> |
| | | 146 | | /// <typeparam name="T">The type of the output.</typeparam> |
| | | 147 | | /// <returns>The output value.</returns> |
| | | 148 | | public T? GetOutput<T>(ExpressionExecutionContext context, Expression<Func<TActivity, object?>> outputExpression |
| | | 149 | | { |
| | 0 | 150 | | var outputName = outputExpression.GetPropertyName(); |
| | 0 | 151 | | return ((IActivity)activity!).GetOutput<T>(context, outputName); |
| | | 152 | | } |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <param name="activity">The activity.</param> |
| | | 156 | | extension(IActivity activity) |
| | | 157 | | { |
| | | 158 | | /// <summary> |
| | | 159 | | /// Gets the Result output of the specified activity. |
| | | 160 | | /// </summary> |
| | | 161 | | /// <param name="context">The context.</param> |
| | | 162 | | /// <typeparam name="T">The type as which to return the output.</typeparam> |
| | | 163 | | /// <returns>The output value.</returns> |
| | | 164 | | public T? GetResult<T>(ExpressionExecutionContext context) |
| | | 165 | | { |
| | 0 | 166 | | var value = GetResult(activity, context); |
| | 0 | 167 | | return value == null ? default! : value.ConvertTo<T>(); |
| | | 168 | | } |
| | | 169 | | |
| | | 170 | | /// <summary> |
| | | 171 | | /// Gets the Result output of the specified activity. |
| | | 172 | | /// </summary> |
| | | 173 | | /// <param name="context">The context.</param> |
| | | 174 | | /// <returns>The output value.</returns> |
| | | 175 | | public object? GetResult(ExpressionExecutionContext context) |
| | | 176 | | { |
| | 0 | 177 | | return activity.GetResult(context.GetActivityExecutionContext()); |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// Gets the Result output of the specified activity. |
| | | 182 | | /// </summary> |
| | | 183 | | /// <param name="context">The context.</param> |
| | | 184 | | /// <returns>The output value.</returns> |
| | | 185 | | public object? GetResult(ActivityExecutionContext context) |
| | | 186 | | { |
| | 0 | 187 | | return activity.GetOutput(context, ActivityOutputRegister.DefaultOutputName); |
| | | 188 | | } |
| | | 189 | | } |
| | | 190 | | |
| | | 191 | | /// <summary> |
| | | 192 | | /// Gets the Result output of the specified activity. |
| | | 193 | | /// </summary> |
| | | 194 | | /// <param name="context">The context.</param> |
| | | 195 | | /// <param name="activity">The activity.</param> |
| | | 196 | | /// <returns>The output value.</returns> |
| | | 197 | | public static object? GetResult(this ActivityExecutionContext context, IActivity activity) |
| | | 198 | | { |
| | 0 | 199 | | return activity.GetOutput(context, ActivityOutputRegister.DefaultOutputName); |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | /// <summary> |
| | | 203 | | /// Gets the result of the last activity. |
| | | 204 | | /// </summary> |
| | | 205 | | /// <param name="context">The context.</param> |
| | | 206 | | public static T? GetLastResult<T>(this ExpressionExecutionContext context) |
| | | 207 | | { |
| | 0 | 208 | | var value = GetLastResult(context.GetWorkflowExecutionContext()); |
| | 0 | 209 | | return value == null ? default! : value.ConvertTo<T>(); |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | /// <summary> |
| | | 213 | | /// Gets the result of the last activity. |
| | | 214 | | /// </summary> |
| | | 215 | | /// <param name="context">The context.</param> |
| | | 216 | | public static object? GetLastResult(this ActivityExecutionContext context) |
| | | 217 | | { |
| | 0 | 218 | | return context.WorkflowExecutionContext.GetLastResult(); |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | /// <summary> |
| | | 222 | | /// Gets the result of the last activity. |
| | | 223 | | /// </summary> |
| | | 224 | | /// <param name="context">The context.</param> |
| | | 225 | | public static object? GetLastResult(this WorkflowExecutionContext context) |
| | | 226 | | { |
| | 0 | 227 | | return context.GetLastActivityResult(); |
| | | 228 | | } |
| | | 229 | | |
| | | 230 | | extension(IActivity activity) |
| | | 231 | | { |
| | | 232 | | /// <summary> |
| | | 233 | | /// Gets the input properties of the specified activity. |
| | | 234 | | /// </summary> |
| | 1808 | 235 | | public IEnumerable<PropertyInfo> GetInputProperties() => activity.GetType().GetProperties().Where(x => typeof(In |
| | | 236 | | |
| | | 237 | | /// <summary> |
| | | 238 | | /// Gets the method for the specified method name on the specified activity. |
| | | 239 | | /// </summary> |
| | | 240 | | public TDelegate GetDelegate<TDelegate>(string methodName) where TDelegate : Delegate |
| | | 241 | | { |
| | 112 | 242 | | var activityType = activity.GetType(); |
| | | 243 | | const BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | Bindin |
| | 112 | 244 | | var resumeMethodInfo = default(MethodInfo?); |
| | 112 | 245 | | var currentType = activityType; |
| | | 246 | | |
| | 305 | 247 | | while (currentType != null && resumeMethodInfo == null) |
| | | 248 | | { |
| | 193 | 249 | | resumeMethodInfo = currentType.GetMethod(methodName, bindingFlags); |
| | 193 | 250 | | currentType = currentType.BaseType; |
| | | 251 | | } |
| | | 252 | | |
| | 112 | 253 | | if (resumeMethodInfo == null) |
| | 0 | 254 | | throw new Exception($"Can't find method name {methodName} on type {activityType} or its base type {activ |
| | | 255 | | |
| | 112 | 256 | | return resumeMethodInfo.IsStatic ? (TDelegate)Delegate.CreateDelegate(typeof(TDelegate), resumeMethodInfo) : |
| | | 257 | | } |
| | | 258 | | |
| | | 259 | | /// <summary> |
| | | 260 | | /// Gets the Resume method for the specified activity. |
| | | 261 | | /// </summary> |
| | 20 | 262 | | public ExecuteActivityDelegate GetResumeActivityDelegate(string resumeMethodName) => activity.GetDelegate<Execut |
| | | 263 | | |
| | | 264 | | /// <summary> |
| | | 265 | | /// Gets the Child Activity Completed method for the specified activity. |
| | | 266 | | /// </summary> |
| | 92 | 267 | | public ActivityCompletionCallback GetActivityCompletionCallback(string completionMethodName) => activity.GetDele |
| | | 268 | | } |
| | | 269 | | } |