| | | 1 | | using Elsa.Dsl.ElsaScript.Ast; |
| | | 2 | | using Elsa.Dsl.ElsaScript.Contracts; |
| | | 3 | | using Elsa.Dsl.ElsaScript.Helpers; |
| | | 4 | | using Elsa.Expressions.Models; |
| | | 5 | | using Elsa.Workflows; |
| | | 6 | | using Elsa.Workflows.Activities; |
| | | 7 | | using Elsa.Workflows.Activities.Flowchart.Models; |
| | | 8 | | using Elsa.Workflows.Memory; |
| | | 9 | | using Elsa.Workflows.Models; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Dsl.ElsaScript.Compiler; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Compiles an ElsaScript AST into Elsa workflows. |
| | | 15 | | /// </summary> |
| | 277 | 16 | | public class ElsaScriptCompiler(IActivityRegistryLookupService activityRegistryLookupService, IElsaScriptParser parser) |
| | | 17 | | { |
| | 1 | 18 | | private static readonly Dictionary<string, string> LanguageMappings = new(StringComparer.OrdinalIgnoreCase) |
| | 1 | 19 | | { |
| | 1 | 20 | | ["js"] = "JavaScript", |
| | 1 | 21 | | ["cs"] = "CSharp", |
| | 1 | 22 | | ["py"] = "Python", |
| | 1 | 23 | | ["liquid"] = "Liquid" |
| | 1 | 24 | | }; |
| | | 25 | | |
| | 277 | 26 | | private string _defaultExpressionLanguage = "JavaScript"; |
| | 277 | 27 | | private readonly Dictionary<string, Variable> _variables = new(); |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | public async Task<Workflow> CompileAsync(string source, CancellationToken cancellationToken = default) |
| | | 31 | | { |
| | 12 | 32 | | var programNode = parser.Parse(source); |
| | 12 | 33 | | return await CompileAsync(programNode, cancellationToken); |
| | 12 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public async Task<Workflow> CompileAsync(ProgramNode programNode, CancellationToken cancellationToken = default) |
| | | 38 | | { |
| | | 39 | | // Get the single workflow (enforced by parser) |
| | 12 | 40 | | var workflowNode = programNode.Workflows.First(); |
| | | 41 | | |
| | | 42 | | // Merge global use statements with workflow-level ones |
| | | 43 | | // Create a temporary workflow node with merged use statements |
| | 12 | 44 | | var mergedWorkflowNode = new WorkflowNode |
| | 12 | 45 | | { |
| | 12 | 46 | | Id = workflowNode.Id, |
| | 12 | 47 | | Metadata = workflowNode.Metadata, |
| | 12 | 48 | | UseStatements = [..programNode.GlobalUseStatements, ..workflowNode.UseStatements], |
| | 12 | 49 | | Body = workflowNode.Body |
| | 12 | 50 | | }; |
| | | 51 | | |
| | 12 | 52 | | return await CompileWorkflowNodeAsync(mergedWorkflowNode, cancellationToken); |
| | 12 | 53 | | } |
| | | 54 | | |
| | | 55 | | private async Task<Workflow> CompileWorkflowNodeAsync(WorkflowNode workflowNode, CancellationToken cancellationToken |
| | | 56 | | { |
| | 12 | 57 | | _variables.Clear(); |
| | 12 | 58 | | _defaultExpressionLanguage = "JavaScript"; |
| | | 59 | | |
| | | 60 | | // Process use statements (workflow-level overrides global) |
| | 42 | 61 | | foreach (var useNode in workflowNode.UseStatements) |
| | | 62 | | { |
| | 9 | 63 | | if (useNode.Type == UseType.Expressions) |
| | | 64 | | { |
| | 7 | 65 | | _defaultExpressionLanguage = MapLanguageName(useNode.Value); |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | // Compile body statements |
| | 12 | 70 | | var activities = new List<IActivity>(); |
| | 58 | 71 | | foreach (var statement in workflowNode.Body) |
| | | 72 | | { |
| | 17 | 73 | | var activity = await CompileStatementAsync(statement, cancellationToken); |
| | 17 | 74 | | if (activity != null) |
| | | 75 | | { |
| | 13 | 76 | | activities.Add(activity); |
| | | 77 | | } |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | // Create the root activity (Sequence containing all statements) |
| | 12 | 81 | | var root = activities.Count == 1 |
| | 12 | 82 | | ? activities[0] |
| | 12 | 83 | | : new Sequence |
| | 12 | 84 | | { |
| | 12 | 85 | | Activities = activities |
| | 12 | 86 | | }; |
| | | 87 | | |
| | | 88 | | // Extract metadata with defaults |
| | 12 | 89 | | var definitionId = GetMetadataValue<string>(workflowNode.Metadata, "DefinitionId") ?? workflowNode.Id; |
| | 12 | 90 | | var displayName = GetMetadataValue<string>(workflowNode.Metadata, "DisplayName") ?? workflowNode.Id; |
| | 12 | 91 | | var description = GetMetadataValue<string>(workflowNode.Metadata, "Description") ?? string.Empty; |
| | 12 | 92 | | var definitionVersionId = GetMetadataValue<string>(workflowNode.Metadata, "DefinitionVersionId") ?? $"{definitio |
| | 12 | 93 | | var version = GetMetadataValueOrDefault(workflowNode.Metadata, "Version", 1); |
| | 12 | 94 | | var usableAsActivity = GetMetadataValue<bool?>(workflowNode.Metadata, "UsableAsActivity"); |
| | | 95 | | |
| | | 96 | | // Create the workflow |
| | 12 | 97 | | var workflow = new Workflow |
| | 12 | 98 | | { |
| | 12 | 99 | | Name = displayName, |
| | 12 | 100 | | Identity = new WorkflowIdentity(definitionId, version, definitionVersionId, null), |
| | 12 | 101 | | WorkflowMetadata = new(displayName, description, ToolVersion: new("3.6.0")), |
| | 12 | 102 | | Root = root, |
| | 12 | 103 | | Variables = _variables.Values.ToList(), |
| | 12 | 104 | | Options = new() |
| | 12 | 105 | | { |
| | 12 | 106 | | UsableAsActivity = usableAsActivity |
| | 12 | 107 | | } |
| | 12 | 108 | | }; |
| | | 109 | | |
| | 12 | 110 | | return workflow; |
| | 12 | 111 | | } |
| | | 112 | | |
| | | 113 | | private T? GetMetadataValue<T>(Dictionary<string, object> metadata, string key) => |
| | 60 | 114 | | metadata.TryGetValue(key, out var value) ? ConvertValue<T>(value, default) : default; |
| | | 115 | | |
| | | 116 | | private T GetMetadataValueOrDefault<T>(Dictionary<string, object> metadata, string key, T defaultValue) => |
| | 12 | 117 | | metadata.TryGetValue(key, out var value) ? ConvertValue(value, defaultValue) : defaultValue; |
| | | 118 | | |
| | | 119 | | private static T? ConvertValue<T>(object value, T? defaultValue) |
| | | 120 | | { |
| | | 121 | | // Handle direct type match |
| | 6 | 122 | | if (value is T typedValue) |
| | 5 | 123 | | return typedValue; |
| | | 124 | | |
| | | 125 | | // Try to convert |
| | | 126 | | try |
| | | 127 | | { |
| | 1 | 128 | | var targetType = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T); |
| | 1 | 129 | | return (T)Convert.ChangeType(value, targetType); |
| | | 130 | | } |
| | 0 | 131 | | catch (Exception) |
| | | 132 | | { |
| | | 133 | | // Return default value if conversion fails |
| | 0 | 134 | | return defaultValue; |
| | | 135 | | } |
| | 1 | 136 | | } |
| | | 137 | | |
| | | 138 | | private async Task<IActivity?> CompileStatementAsync(StatementNode statement, CancellationToken cancellationToken = |
| | | 139 | | { |
| | 24 | 140 | | return statement switch |
| | 24 | 141 | | { |
| | 4 | 142 | | VariableDeclarationNode varDecl => CompileVariableDeclaration(varDecl), |
| | 13 | 143 | | ActivityInvocationNode actInv => await CompileActivityInvocationAsync(actInv, cancellationToken), |
| | 1 | 144 | | BlockNode block => await CompileBlockAsync(block, cancellationToken), |
| | 0 | 145 | | IfNode ifNode => await CompileIfAsync(ifNode, cancellationToken), |
| | 0 | 146 | | ForEachNode forEach => await CompileForEachAsync(forEach, cancellationToken), |
| | 2 | 147 | | ForNode forNode => await CompileForAsync(forNode, cancellationToken), |
| | 0 | 148 | | WhileNode whileNode => await CompileWhileAsync(whileNode, cancellationToken), |
| | 0 | 149 | | SwitchNode switchNode => await CompileSwitchAsync(switchNode, cancellationToken), |
| | 3 | 150 | | FlowchartNode flowchart => await CompileFlowchartAsync(flowchart, cancellationToken), |
| | 1 | 151 | | ListenNode listen => await CompileListenAsync(listen, cancellationToken), |
| | 0 | 152 | | _ => throw new NotSupportedException($"Statement type {statement.GetType().Name} is not supported") |
| | 24 | 153 | | }; |
| | 24 | 154 | | } |
| | | 155 | | |
| | | 156 | | private IActivity? CompileVariableDeclaration(VariableDeclarationNode varDecl) |
| | | 157 | | { |
| | | 158 | | // Create and register the variable |
| | 4 | 159 | | var initialValue = varDecl.Value != null ? EvaluateConstantExpression(varDecl.Value) : null; |
| | 4 | 160 | | var variable = new Variable(varDecl.Name, initialValue); |
| | 4 | 161 | | _variables[varDecl.Name] = variable; |
| | | 162 | | |
| | | 163 | | // Variable declarations don't produce activities themselves |
| | 4 | 164 | | return null; |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | private async Task<IActivity> CompileActivityInvocationAsync(ActivityInvocationNode actInv, CancellationToken cancel |
| | | 168 | | { |
| | | 169 | | // Try to find the activity type by name - try several strategies |
| | 14 | 170 | | var activityDescriptor = await activityRegistryLookupService.FindAsync(actInv.ActivityName); |
| | | 171 | | |
| | | 172 | | // If not found, try with "Elsa." prefix |
| | 14 | 173 | | if (activityDescriptor == null) |
| | | 174 | | { |
| | 14 | 175 | | activityDescriptor = await activityRegistryLookupService.FindAsync($"Elsa.{actInv.ActivityName}"); |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | // If still not found, search by descriptor name |
| | 14 | 179 | | if (activityDescriptor == null) |
| | | 180 | | { |
| | 0 | 181 | | activityDescriptor = await activityRegistryLookupService.FindAsync(d => d.Name == actInv.ActivityName); |
| | | 182 | | } |
| | | 183 | | |
| | 14 | 184 | | if (activityDescriptor == null) |
| | | 185 | | { |
| | 0 | 186 | | throw new InvalidOperationException($"Activity '{actInv.ActivityName}' not found in registry"); |
| | | 187 | | } |
| | | 188 | | |
| | 14 | 189 | | var activityType = activityDescriptor.ClrType; |
| | | 190 | | |
| | | 191 | | // Separate named and positional arguments |
| | | 192 | | var namedArgs = actInv.Arguments.Where(a => a.Name != null).ToList(); |
| | | 193 | | var positionalArgs = actInv.Arguments.Where(a => a.Name == null).ToList(); |
| | | 194 | | |
| | | 195 | | IActivity activity; |
| | | 196 | | |
| | | 197 | | // If we have positional arguments, try to find a matching constructor |
| | 14 | 198 | | if (positionalArgs.Any()) |
| | | 199 | | { |
| | 12 | 200 | | activity = InstantiateActivityUsingConstructor(activityType, positionalArgs); |
| | | 201 | | } |
| | | 202 | | else |
| | | 203 | | { |
| | | 204 | | // No positional arguments, use default constructor |
| | 2 | 205 | | var activityConstructorContext = new ActivityConstructorContext(activityDescriptor, ActivityActivator.Create |
| | 2 | 206 | | activity = activityDescriptor.Constructor(activityConstructorContext); |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | // Set named argument properties |
| | 28 | 210 | | foreach (var arg in namedArgs) |
| | | 211 | | { |
| | 0 | 212 | | var property = activityType.GetProperty(arg.Name!); |
| | | 213 | | |
| | 0 | 214 | | if (property != null) |
| | | 215 | | { |
| | 0 | 216 | | var value = CompileExpression(arg.Value, property.PropertyType); |
| | 0 | 217 | | property.SetValue(activity, value); |
| | | 218 | | } |
| | | 219 | | else |
| | | 220 | | { |
| | 0 | 221 | | throw new InvalidOperationException($"Property '{arg.Name}' not found on activity type '{activityType.Na |
| | | 222 | | } |
| | | 223 | | } |
| | | 224 | | |
| | 14 | 225 | | return activity; |
| | 14 | 226 | | } |
| | | 227 | | |
| | | 228 | | private async Task<IActivity> CompileBlockAsync(BlockNode block, CancellationToken cancellationToken = default) |
| | | 229 | | { |
| | 1 | 230 | | var activities = new List<IActivity>(); |
| | | 231 | | |
| | 6 | 232 | | foreach (var statement in block.Statements) |
| | | 233 | | { |
| | 2 | 234 | | var activity = await CompileStatementAsync(statement, cancellationToken); |
| | 2 | 235 | | if (activity != null) |
| | | 236 | | { |
| | 2 | 237 | | activities.Add(activity); |
| | | 238 | | } |
| | | 239 | | } |
| | | 240 | | |
| | 1 | 241 | | return new Sequence |
| | 1 | 242 | | { |
| | 1 | 243 | | Activities = activities |
| | 1 | 244 | | }; |
| | 1 | 245 | | } |
| | | 246 | | |
| | | 247 | | private async Task<IActivity> CompileIfAsync(IfNode ifNode, CancellationToken cancellationToken = default) |
| | | 248 | | { |
| | 0 | 249 | | var condition = CompileExpressionAsInput<bool>(ifNode.Condition); |
| | 0 | 250 | | var thenActivity = await CompileStatementAsync(ifNode.Then, cancellationToken); |
| | 0 | 251 | | var elseActivity = ifNode.Else != null ? await CompileStatementAsync(ifNode.Else, cancellationToken) : null; |
| | | 252 | | |
| | 0 | 253 | | return new If(condition) |
| | 0 | 254 | | { |
| | 0 | 255 | | Then = thenActivity, |
| | 0 | 256 | | Else = elseActivity |
| | 0 | 257 | | }; |
| | 0 | 258 | | } |
| | | 259 | | |
| | | 260 | | private async Task<IActivity> CompileForEachAsync(ForEachNode forEach, CancellationToken cancellationToken = default |
| | | 261 | | { |
| | | 262 | | Variable loopVariable; |
| | | 263 | | |
| | 0 | 264 | | if (forEach.DeclaresVariable) |
| | | 265 | | { |
| | | 266 | | // Create a new loop variable |
| | 0 | 267 | | loopVariable = new Variable<object>(forEach.VariableName, null!); |
| | 0 | 268 | | _variables[forEach.VariableName] = loopVariable; |
| | | 269 | | } |
| | | 270 | | else |
| | | 271 | | { |
| | | 272 | | // Reuse existing variable |
| | 0 | 273 | | if (!_variables.TryGetValue(forEach.VariableName, out loopVariable!)) |
| | | 274 | | { |
| | 0 | 275 | | throw new InvalidOperationException($"Variable '{forEach.VariableName}' is not declared. Use 'var {forEa |
| | | 276 | | } |
| | | 277 | | } |
| | | 278 | | |
| | 0 | 279 | | var items = CompileExpressionAsInput<ICollection<object>>(forEach.Collection); |
| | 0 | 280 | | var body = await CompileStatementAsync(forEach.Body, cancellationToken); |
| | | 281 | | |
| | 0 | 282 | | var forEachActivity = new ForEach<object>(items) |
| | 0 | 283 | | { |
| | 0 | 284 | | CurrentValue = new(loopVariable), |
| | 0 | 285 | | Body = body |
| | 0 | 286 | | }; |
| | | 287 | | |
| | 0 | 288 | | return forEachActivity; |
| | 0 | 289 | | } |
| | | 290 | | |
| | | 291 | | private async Task<IActivity> CompileForAsync(ForNode forNode, CancellationToken cancellationToken = default) |
| | | 292 | | { |
| | | 293 | | Variable loopVariable; |
| | | 294 | | |
| | 2 | 295 | | if (forNode.DeclaresVariable) |
| | | 296 | | { |
| | | 297 | | // Create a new loop variable |
| | 2 | 298 | | loopVariable = new Variable<int>(forNode.VariableName, 0); |
| | 2 | 299 | | _variables[forNode.VariableName] = loopVariable; |
| | | 300 | | } |
| | | 301 | | else |
| | | 302 | | { |
| | | 303 | | // Reuse existing variable |
| | 0 | 304 | | if (!_variables.TryGetValue(forNode.VariableName, out loopVariable!)) |
| | | 305 | | { |
| | 0 | 306 | | throw new InvalidOperationException($"Variable '{forNode.VariableName}' is not declared. Use 'var {forNo |
| | | 307 | | } |
| | | 308 | | } |
| | | 309 | | |
| | 2 | 310 | | var start = CompileExpressionAsInput<int>(forNode.Start); |
| | 2 | 311 | | var end = CompileExpressionAsInput<int>(forNode.End); |
| | 2 | 312 | | var step = CompileExpressionAsInput<int>(forNode.Step); |
| | 2 | 313 | | var body = await CompileStatementAsync(forNode.Body, cancellationToken); |
| | | 314 | | |
| | 2 | 315 | | var forActivity = new For |
| | 2 | 316 | | { |
| | 2 | 317 | | Start = start, |
| | 2 | 318 | | End = end, |
| | 2 | 319 | | Step = step, |
| | 2 | 320 | | OuterBoundInclusive = new Input<bool>(forNode.IsInclusive), |
| | 2 | 321 | | CurrentValue = new Output<object?>(loopVariable), |
| | 2 | 322 | | Body = body |
| | 2 | 323 | | }; |
| | | 324 | | |
| | 2 | 325 | | return forActivity; |
| | 2 | 326 | | } |
| | | 327 | | |
| | | 328 | | private async Task<IActivity> CompileWhileAsync(WhileNode whileNode, CancellationToken cancellationToken = default) |
| | | 329 | | { |
| | 0 | 330 | | var condition = CompileExpressionAsInput<bool>(whileNode.Condition); |
| | 0 | 331 | | var body = await CompileStatementAsync(whileNode.Body, cancellationToken); |
| | | 332 | | |
| | 0 | 333 | | return new While(condition) |
| | 0 | 334 | | { |
| | 0 | 335 | | Body = body |
| | 0 | 336 | | }; |
| | 0 | 337 | | } |
| | | 338 | | |
| | | 339 | | private async Task<IActivity> CompileSwitchAsync(SwitchNode switchNode, CancellationToken cancellationToken = defaul |
| | | 340 | | { |
| | 0 | 341 | | var cases = new List<SwitchCase>(); |
| | | 342 | | |
| | 0 | 343 | | foreach (var caseNode in switchNode.Cases) |
| | | 344 | | { |
| | 0 | 345 | | var caseExpression = CompileExpressionAsExpression(caseNode.Value); |
| | 0 | 346 | | var caseBody = await CompileStatementAsync(caseNode.Body, cancellationToken); |
| | 0 | 347 | | cases.Add(new("Case", caseExpression, caseBody!)); |
| | 0 | 348 | | } |
| | | 349 | | |
| | 0 | 350 | | var defaultActivity = switchNode.Default != null ? await CompileStatementAsync(switchNode.Default, cancellationT |
| | | 351 | | |
| | 0 | 352 | | return new Switch |
| | 0 | 353 | | { |
| | 0 | 354 | | Cases = cases, |
| | 0 | 355 | | Default = defaultActivity |
| | 0 | 356 | | }; |
| | 0 | 357 | | } |
| | | 358 | | |
| | | 359 | | private async Task<IActivity> CompileFlowchartAsync(FlowchartNode flowchart, CancellationToken cancellationToken = d |
| | | 360 | | { |
| | | 361 | | // Register flowchart-scoped variables |
| | 6 | 362 | | foreach (var varDecl in flowchart.Variables) |
| | | 363 | | { |
| | 0 | 364 | | CompileVariableDeclaration(varDecl); |
| | | 365 | | } |
| | | 366 | | |
| | | 367 | | // Compile all labeled activities and build a label-to-activity map |
| | 3 | 368 | | var labelToActivity = new Dictionary<string, IActivity>(); |
| | 12 | 369 | | foreach (var labeledNode in flowchart.Activities) |
| | | 370 | | { |
| | 3 | 371 | | var activity = await CompileStatementAsync(labeledNode.Activity, cancellationToken); |
| | 3 | 372 | | if (activity != null) |
| | | 373 | | { |
| | 3 | 374 | | labelToActivity[labeledNode.Label] = activity; |
| | | 375 | | } |
| | 3 | 376 | | } |
| | | 377 | | |
| | | 378 | | // Create connections |
| | 3 | 379 | | var connections = new List<Connection>(); |
| | 8 | 380 | | foreach (var connNode in flowchart.Connections) |
| | | 381 | | { |
| | 1 | 382 | | if (!labelToActivity.TryGetValue(connNode.Source, out var sourceActivity)) |
| | 0 | 383 | | throw new InvalidOperationException($"Source label '{connNode.Source}' not found in flowchart"); |
| | | 384 | | |
| | 1 | 385 | | if (!labelToActivity.TryGetValue(connNode.Target, out var targetActivity)) |
| | 0 | 386 | | throw new InvalidOperationException($"Target label '{connNode.Target}' not found in flowchart"); |
| | | 387 | | |
| | 1 | 388 | | var source = new Endpoint(sourceActivity, connNode.Outcome); |
| | 1 | 389 | | var target = new Endpoint(targetActivity); |
| | 1 | 390 | | connections.Add(new Connection(source, target)); |
| | | 391 | | } |
| | | 392 | | |
| | | 393 | | // Create flowchart activity |
| | 3 | 394 | | var flowchartActivity = new Workflows.Activities.Flowchart.Activities.Flowchart |
| | 3 | 395 | | { |
| | 3 | 396 | | Activities = labelToActivity.Values.ToList(), |
| | 3 | 397 | | Connections = connections |
| | 3 | 398 | | }; |
| | | 399 | | |
| | | 400 | | // Set entry point if specified |
| | 3 | 401 | | if (!string.IsNullOrEmpty(flowchart.EntryPoint)) |
| | | 402 | | { |
| | 2 | 403 | | if (!labelToActivity.TryGetValue(flowchart.EntryPoint, out var startActivity)) |
| | 0 | 404 | | throw new InvalidOperationException($"Entry point label '{flowchart.EntryPoint}' not found in flowchart" |
| | | 405 | | |
| | 2 | 406 | | flowchartActivity.Start = startActivity; |
| | | 407 | | } |
| | | 408 | | |
| | 3 | 409 | | return flowchartActivity; |
| | 3 | 410 | | } |
| | | 411 | | |
| | | 412 | | private async Task<IActivity> CompileListenAsync(ListenNode listen, CancellationToken cancellationToken = default) |
| | | 413 | | { |
| | | 414 | | // Listen is just a regular activity invocation that can start a workflow |
| | 1 | 415 | | var activity = await CompileActivityInvocationAsync(listen.Activity, cancellationToken); |
| | | 416 | | |
| | | 417 | | // Try to set CanStartWorkflow if the activity supports it |
| | 1 | 418 | | var canStartWorkflowProp = activity.GetType().GetProperty("CanStartWorkflow"); |
| | 1 | 419 | | if (canStartWorkflowProp != null && canStartWorkflowProp.PropertyType == typeof(bool)) |
| | | 420 | | { |
| | 1 | 421 | | canStartWorkflowProp.SetValue(activity, true); |
| | | 422 | | } |
| | | 423 | | |
| | 1 | 424 | | return activity; |
| | 1 | 425 | | } |
| | | 426 | | |
| | | 427 | | private Input<T> CompileExpressionAsInput<T>(ExpressionNode exprNode) |
| | | 428 | | { |
| | 18 | 429 | | if (exprNode is LiteralNode literal) |
| | | 430 | | { |
| | 12 | 431 | | return new(new Literal(literal.Value!)); |
| | | 432 | | } |
| | | 433 | | |
| | 6 | 434 | | if (exprNode is IdentifierNode identifier) |
| | | 435 | | { |
| | | 436 | | // Reference to a variable |
| | 1 | 437 | | if (_variables.TryGetValue(identifier.Name, out var variable)) |
| | | 438 | | { |
| | 1 | 439 | | return new(variable); |
| | | 440 | | } |
| | | 441 | | |
| | | 442 | | // If not found, treat as a literal |
| | 0 | 443 | | return new(new Literal<T>(default!)); |
| | | 444 | | } |
| | | 445 | | |
| | 5 | 446 | | if (exprNode is ElsaExpressionNode elsaExpr) |
| | | 447 | | { |
| | 5 | 448 | | var language = elsaExpr.Language != null ? MapLanguageName(elsaExpr.Language) : _defaultExpressionLanguage; |
| | 5 | 449 | | var expression = new Expression(language, elsaExpr.Expression); |
| | 5 | 450 | | return new(expression); |
| | | 451 | | } |
| | | 452 | | |
| | 0 | 453 | | if (exprNode is ArrayLiteralNode arrayLiteral) |
| | | 454 | | { |
| | | 455 | | // For array literals, evaluate to a constant array if all elements are literals |
| | 0 | 456 | | var elements = arrayLiteral.Elements.Select(EvaluateConstantExpression).ToArray(); |
| | 0 | 457 | | return new((T)(object)elements); |
| | | 458 | | } |
| | | 459 | | |
| | 0 | 460 | | throw new NotSupportedException($"Expression type {exprNode.GetType().Name} is not supported"); |
| | | 461 | | } |
| | | 462 | | |
| | | 463 | | private Expression CompileExpressionAsExpression(ExpressionNode exprNode) |
| | | 464 | | { |
| | 0 | 465 | | if (exprNode is LiteralNode literal) |
| | | 466 | | { |
| | 0 | 467 | | return Expression.LiteralExpression(literal.Value); |
| | | 468 | | } |
| | | 469 | | |
| | 0 | 470 | | if (exprNode is ElsaExpressionNode elsaExpr) |
| | | 471 | | { |
| | 0 | 472 | | var language = elsaExpr.Language != null ? MapLanguageName(elsaExpr.Language) : _defaultExpressionLanguage; |
| | 0 | 473 | | return new(language, elsaExpr.Expression); |
| | | 474 | | } |
| | | 475 | | |
| | 0 | 476 | | throw new NotSupportedException($"Expression type {exprNode.GetType().Name} is not supported as Expression"); |
| | | 477 | | } |
| | | 478 | | |
| | | 479 | | private object CompileExpression(ExpressionNode exprNode, Type targetType) |
| | | 480 | | { |
| | | 481 | | // Check if targetType is already Input<T> |
| | | 482 | | Type innerType; |
| | 12 | 483 | | if (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(Input<>)) |
| | | 484 | | { |
| | | 485 | | // Extract the T from Input<T> |
| | 12 | 486 | | innerType = targetType.GetGenericArguments()[0]; |
| | | 487 | | } |
| | | 488 | | else |
| | | 489 | | { |
| | 0 | 490 | | innerType = targetType; |
| | | 491 | | } |
| | | 492 | | |
| | | 493 | | // Use reflection to call CompileExpressionAsInput<T> |
| | 12 | 494 | | var method = GetType().GetMethod(nameof(CompileExpressionAsInput), |
| | 12 | 495 | | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); |
| | 12 | 496 | | var genericMethod = method!.MakeGenericMethod(innerType); |
| | | 497 | | |
| | 12 | 498 | | return genericMethod.Invoke(this, [exprNode])!; |
| | | 499 | | } |
| | | 500 | | |
| | | 501 | | private object? EvaluateConstantExpression(ExpressionNode exprNode) |
| | | 502 | | { |
| | 4 | 503 | | if (exprNode is LiteralNode literal) |
| | | 504 | | { |
| | 4 | 505 | | return literal.Value; |
| | | 506 | | } |
| | | 507 | | |
| | 0 | 508 | | if (exprNode is ArrayLiteralNode arrayLiteral) |
| | | 509 | | { |
| | 0 | 510 | | return arrayLiteral.Elements.Select(EvaluateConstantExpression).ToArray(); |
| | | 511 | | } |
| | | 512 | | |
| | | 513 | | // For non-constant expressions, return null |
| | 0 | 514 | | return null; |
| | | 515 | | } |
| | | 516 | | |
| | | 517 | | private IActivity InstantiateActivityUsingConstructor(Type activityType, List<ArgumentNode> positionalArgs) |
| | | 518 | | { |
| | | 519 | | // Get all public constructors |
| | 12 | 520 | | var constructors = activityType.GetConstructors(System.Reflection.BindingFlags.Public | System.Reflection.Bindin |
| | | 521 | | |
| | | 522 | | // Filter constructors that: |
| | | 523 | | // 1. Have the same number of required Input<T> parameters as positional arguments (excluding optional params) |
| | | 524 | | // 2. All non-optional parameters are Input<T> types |
| | 12 | 525 | | var matchingConstructors = new List<(System.Reflection.ConstructorInfo ctor, System.Reflection.ParameterInfo[] i |
| | | 526 | | |
| | 176 | 527 | | foreach (var ctor in constructors) |
| | | 528 | | { |
| | 76 | 529 | | var parameters = ctor.GetParameters(); |
| | | 530 | | |
| | | 531 | | // Filter to only Input<T> parameters that are not optional (don't have default values or CallerMemberName a |
| | 76 | 532 | | var inputParams = parameters.Where(p => |
| | 228 | 533 | | p.ParameterType.IsGenericType && |
| | 228 | 534 | | p.ParameterType.GetGenericTypeDefinition() == typeof(Input<>) && |
| | 228 | 535 | | !p.IsOptional && |
| | 228 | 536 | | !p.GetCustomAttributes(typeof(System.Runtime.CompilerServices.CallerFilePathAttribute), false).Any() && |
| | 228 | 537 | | !p.GetCustomAttributes(typeof(System.Runtime.CompilerServices.CallerLineNumberAttribute), false).Any() & |
| | 228 | 538 | | !p.GetCustomAttributes(typeof(System.Runtime.CompilerServices.CallerMemberNameAttribute), false).Any() |
| | 76 | 539 | | ).ToArray(); |
| | | 540 | | |
| | | 541 | | // Check if the number of required Input<T> params matches our positional args |
| | 76 | 542 | | if (inputParams.Length == positionalArgs.Count) |
| | | 543 | | { |
| | 12 | 544 | | matchingConstructors.Add((ctor, inputParams)); |
| | | 545 | | } |
| | | 546 | | } |
| | | 547 | | |
| | 12 | 548 | | if (!matchingConstructors.Any()) |
| | | 549 | | { |
| | 0 | 550 | | throw new InvalidOperationException( |
| | 0 | 551 | | $"No matching constructor found for activity type '{activityType.Name}' with {positionalArgs.Count} posi |
| | 0 | 552 | | $"Constructors must have Input<T> parameters matching the number of positional arguments."); |
| | | 553 | | } |
| | | 554 | | |
| | 12 | 555 | | if (matchingConstructors.Count > 1) |
| | | 556 | | { |
| | 0 | 557 | | throw new InvalidOperationException( |
| | 0 | 558 | | $"Multiple matching constructors found for activity type '{activityType.Name}' with {positionalArgs.Coun |
| | 0 | 559 | | $"Please use named arguments to disambiguate."); |
| | | 560 | | } |
| | | 561 | | |
| | 12 | 562 | | var (selectedCtor, selectedInputParams) = matchingConstructors[0]; |
| | | 563 | | |
| | | 564 | | // Build the constructor arguments |
| | 12 | 565 | | var ctorArgs = new List<object?>(); |
| | 12 | 566 | | var allParams = selectedCtor.GetParameters(); |
| | | 567 | | |
| | 96 | 568 | | foreach (var param in allParams) |
| | | 569 | | { |
| | | 570 | | // Check if this is one of our Input<T> parameters |
| | 36 | 571 | | var inputParamIndex = Array.IndexOf(selectedInputParams, param); |
| | | 572 | | |
| | 36 | 573 | | if (inputParamIndex >= 0) |
| | | 574 | | { |
| | | 575 | | // This is an Input<T> parameter - compile the corresponding positional argument |
| | 12 | 576 | | var arg = positionalArgs[inputParamIndex]; |
| | 12 | 577 | | var value = CompileExpression(arg.Value, param.ParameterType); |
| | 12 | 578 | | ctorArgs.Add(value); |
| | | 579 | | } |
| | 24 | 580 | | else if (param.IsOptional) |
| | | 581 | | { |
| | | 582 | | // This is an optional parameter (like CallerFilePath) - use its default value |
| | 24 | 583 | | ctorArgs.Add(param.DefaultValue); |
| | | 584 | | } |
| | | 585 | | else |
| | | 586 | | { |
| | | 587 | | // This shouldn't happen if our filtering is correct |
| | 0 | 588 | | throw new InvalidOperationException( |
| | 0 | 589 | | $"Unexpected non-optional, non-Input<T> parameter '{param.Name}' in constructor for '{activityType.N |
| | | 590 | | } |
| | | 591 | | } |
| | | 592 | | |
| | | 593 | | // Instantiate the activity using the constructor |
| | 12 | 594 | | var activity = (IActivity)selectedCtor.Invoke(ctorArgs.ToArray()); |
| | 12 | 595 | | return activity; |
| | | 596 | | } |
| | | 597 | | |
| | | 598 | | private static string MapLanguageName(string dslLanguage) => |
| | 12 | 599 | | LanguageMappings.TryGetValue(dslLanguage, out var mapped) ? mapped : dslLanguage; |
| | | 600 | | } |