elsa-core/src/Flowsharp.Abstractions/Models/WorkflowExecutionContext.cs

92 lines
2.7 KiB
C#
Raw Normal View History

2018-10-12 18:53:06 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
2018-10-14 10:24:36 +00:00
using System.Threading;
using System.Threading.Tasks;
using Flowsharp.Activities;
2018-10-12 18:53:06 +00:00
namespace Flowsharp.Models
{
public class WorkflowExecutionContext
{
public WorkflowExecutionContext(Workflow workflow)
2018-10-12 18:53:06 +00:00
{
2018-10-14 10:24:36 +00:00
Workflow = workflow;
IsFirstPass = true;
scheduledActivities = new Stack<IActivity>();
2018-10-12 18:53:06 +00:00
}
2018-10-14 10:24:36 +00:00
private readonly Stack<IActivity> scheduledActivities;
2018-10-12 18:53:06 +00:00
2018-10-14 10:24:36 +00:00
public Workflow Workflow { get; }
2018-10-12 18:53:06 +00:00
public bool HasScheduledActivities => scheduledActivities.Any();
public bool IsFirstPass { get; set; }
2018-10-14 10:24:36 +00:00
public IActivity CurrentActivity { get; private set; }
2018-10-14 11:49:29 +00:00
public WorkflowExecutionScope CurrentScope
{
get => Workflow.CurrentScope;
set => Workflow.CurrentScope = value;
}
2018-10-12 18:53:06 +00:00
2018-10-14 10:24:36 +00:00
public void BeginScope()
{
2018-10-14 11:49:29 +00:00
Workflow.Scopes.Push(CurrentScope = new WorkflowExecutionScope());
2018-10-14 10:24:36 +00:00
}
public void EndScope()
{
2018-10-14 11:49:29 +00:00
Workflow.Scopes.Pop();
CurrentScope = Workflow.Scopes.Peek();
2018-10-14 10:24:36 +00:00
}
public void ScheduleActivity(IActivity activity)
2018-10-12 18:53:06 +00:00
{
2018-10-14 10:24:36 +00:00
scheduledActivities.Push(activity);
2018-10-12 18:53:06 +00:00
}
2018-10-14 10:24:36 +00:00
public IActivity PopScheduledActivity()
2018-10-12 18:53:06 +00:00
{
2018-10-14 10:24:36 +00:00
CurrentActivity = scheduledActivities.Pop();
return CurrentActivity;
}
public void SetLastResult(object value)
2018-10-14 10:24:36 +00:00
{
CurrentScope.LastResult = value;
2018-10-12 18:53:06 +00:00
}
2018-10-14 10:24:36 +00:00
public void Fault(Exception exception, IActivity activity)
2018-10-12 18:53:06 +00:00
{
throw new NotImplementedException();
}
2018-10-14 10:24:36 +00:00
public Task HaltAsync(CancellationToken cancellationToken)
{
var activity = CurrentActivity;
2018-10-16 13:00:25 +00:00
if (!Workflow.BlockingActivities.Contains(activity))
{
2018-10-16 13:00:25 +00:00
Workflow.BlockingActivities.Add(activity);
}
Workflow.Status = WorkflowStatus.Halted;
return Task.CompletedTask;
2018-10-14 10:24:36 +00:00
}
public void Finish()
{
Workflow.Status = WorkflowStatus.Finished;
2018-10-14 10:24:36 +00:00
}
2018-10-15 12:15:36 +00:00
public void ScheduleNextActivities(WorkflowExecutionContext workflowContext, SourceEndpoint endpoint)
2018-10-14 10:24:36 +00:00
{
var completedActivity = workflowContext.CurrentActivity;
var connections = Workflow.Connections.Where(x => x.Source.Activity == completedActivity && x.Source.Name == endpoint.Name);
foreach (var connection in connections)
{
workflowContext.ScheduleActivity(connection.Target.Activity);
}
}
2018-10-12 18:53:06 +00:00
}
}