2018-10-12 18:53:06 +00:00
|
|
|
|
using System;
|
2018-10-14 10:24:36 +00:00
|
|
|
|
using System.Collections;
|
2018-10-12 18:53:06 +00:00
|
|
|
|
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
|
|
|
|
|
|
{
|
2018-10-14 10:24:36 +00:00
|
|
|
|
public WorkflowExecutionContext(Workflow workflow, WorkflowStatus status = WorkflowStatus.Idle)
|
2018-10-12 18:53:06 +00:00
|
|
|
|
{
|
2018-10-14 10:24:36 +00:00
|
|
|
|
Workflow = workflow;
|
2018-10-12 18:53:06 +00:00
|
|
|
|
Status = status;
|
2018-10-14 10:24:36 +00:00
|
|
|
|
IsFirstPass = true;
|
|
|
|
|
|
scheduledActivities = new Stack<IActivity>();
|
|
|
|
|
|
scopes = new Stack<WorkflowExecutionScope>();
|
2018-10-12 18:53:06 +00:00
|
|
|
|
|
2018-10-14 10:24:36 +00:00
|
|
|
|
BeginScope();
|
2018-10-12 18:53:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-14 10:24:36 +00:00
|
|
|
|
private readonly Stack<IActivity> scheduledActivities;
|
|
|
|
|
|
private readonly Stack<WorkflowExecutionScope> scopes;
|
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 WorkflowStatus Status { get; set; }
|
|
|
|
|
|
public bool HasScheduledActivities => scheduledActivities.Any();
|
|
|
|
|
|
public bool IsFirstPass { get; set; }
|
2018-10-14 10:24:36 +00:00
|
|
|
|
public IActivity CurrentActivity { get; private set; }
|
|
|
|
|
|
public WorkflowExecutionScope CurrentScope { get; private set; }
|
2018-10-12 18:53:06 +00:00
|
|
|
|
|
2018-10-14 10:24:36 +00:00
|
|
|
|
public void BeginScope()
|
|
|
|
|
|
{
|
|
|
|
|
|
scopes.Push(CurrentScope = new WorkflowExecutionScope());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void EndScope()
|
|
|
|
|
|
{
|
|
|
|
|
|
scopes.Pop();
|
|
|
|
|
|
CurrentScope = scopes.Peek();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 SetReturnValue(object value)
|
|
|
|
|
|
{
|
|
|
|
|
|
CurrentScope.ReturnValue = 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)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Finish()
|
|
|
|
|
|
{
|
|
|
|
|
|
Status = WorkflowStatus.Finished;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void ScheduleNextActivities(WorkflowExecutionContext workflowContext, SourceEndpoint endpoint)
|
|
|
|
|
|
{
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|