using Serein.Library.Api;
using Serein.Library.Attributes;
using Serein.Library.Core.NodeFlow;
using Serein.Library.Entity;
using Serein.Library.Enums;
using Serein.Library.Ex;
using Serein.Library.Utils;
using Serein.Library.Web;
using Serein.NodeFlow.Base;
using Serein.NodeFlow.Model;
using System.Collections.Concurrent;
using System.ComponentModel.Design;
using System.Runtime.CompilerServices;
using System.Xml.Linq;
using static Serein.Library.Utils.ChannelFlowInterrupt;
namespace Serein.NodeFlow
{
///
/// 流程启动器
///
///
///
public class FlowStarter
{
///
/// 全局触发器CTS
///
public const string FlipFlopCtsName = "<>.FlowFlipFlopCts";
public FlowStarter()
{
}
///
/// 流程运行状态
///
public enum RunState
{
///
/// 等待开始
///
NoStart,
///
/// 正在运行
///
Running,
///
/// 运行完成
///
Completion,
}
///
/// 起点流程运行状态
///
public RunState FlowState { get; private set; } = RunState.NoStart;
///
/// 全局触发器运行状态
///
public RunState FlipFlopState { get; private set; } = RunState.NoStart;
///
/// 控制触发器
///
private CancellationTokenSource _flipFlopCts = null;
///
/// 是否停止启动
///
private bool IsStopStart = false;
///
/// 结束运行时需要执行的方法
///
private Func ExitAction { get; set; } = null;
///
/// 运行的上下文
///
private IDynamicContext Context { get; set; } = null;
private void CheckStartState()
{
if (IsStopStart)
{
throw new Exception("停止启动");
}
}
///
/// 从选定的节点开始运行
///
///
///
public async Task StartFlowInSelectNodeAsync(NodeModelBase startNode)
{
if (Context is null) return;
await startNode.StartFlowAsync(Context); // 开始运行时从选定节点开始运行
}
///
/// 开始运行
///
/// 运行环境
/// 环境中已加载的所有节点
/// 初始化方法
/// 加载时方法
/// 结束时方法
///
public async Task RunAsync(IFlowEnvironment env,
List nodes,
Dictionary> autoRegisterTypes,
List initMethods,
List loadingMethods,
List exitMethods)
{
FlowState = RunState.Running; // 开始运行
NodeModelBase? startNode = nodes.FirstOrDefault(node => node.IsStart);
if (startNode is null) {
FlowState = RunState.Completion; // 不存在起点,退出流程
return;
}
#region 获取所有触发器,以及已加载节点的方法信息
List runNodeMd;
List flipflopNodes;
flipflopNodes = nodes.Where(it => it.MethodDetails?.MethodDynamicType == NodeType.Flipflop && it.IsStart == false)
.Select(it => (SingleFlipflopNode)it)
.Where(node => node is SingleFlipflopNode flipflopNode && flipflopNode.NotExitPreviousNode())
.ToList();// 获取需要再运行开始之前启动的触发器节点
runNodeMd = nodes.Select(item => item.MethodDetails).ToList(); // 获取环境中所有节点的方法信息
#endregion
#region 选择运行环境的上下文
// 判断使用哪一种流程上下文
#if NET6_0_OR_GREATER
Context = new Serein.Library.Core.NodeFlow.DynamicContext(env); // 从起始节点启动流程时创建上下文
#else
Context = new Serein.Library.Framework.NodeFlow.DynamicContext(env);
#endif
#endregion
#region 初始化运行环境的Ioc容器
// 清除节点使用的对象,筛选出需要初始化的方法描述
var thisRuningMds = new List();
thisRuningMds.AddRange(runNodeMd.Where(md => md?.ActingInstanceType is not null));
thisRuningMds.AddRange(initMethods.Where(md => md?.ActingInstanceType is not null));
thisRuningMds.AddRange(loadingMethods.Where(md => md?.ActingInstanceType is not null));
thisRuningMds.AddRange(exitMethods.Where(md => md?.ActingInstanceType is not null));
// .AddRange(initMethods).AddRange(loadingMethods).a
foreach (var nodeMd in thisRuningMds)
{
nodeMd.ActingInstance = null;
}
env.IOC.CustomRegisterInstance(typeof(ISereinIOC).FullName, env);
// 初始化ioc容器中的类型对象
foreach (var md in thisRuningMds)
{
if (md.ActingInstanceType != null)
{
env.IOC.Register(md.ActingInstanceType);
}
else
{
await Console.Out.WriteLineAsync($"{md.MethodName} - 没有类型声明");
IsStopStart = true;
}
}
if (IsStopStart) return;// 检查所有dll节点是否存在类型
env.IOC.Build(); // 流程启动前的初始化
foreach (var md in thisRuningMds)
{
md.ActingInstance = env.IOC.Get(md.ActingInstanceType);
if(md.ActingInstance is null)
{
await Console.Out.WriteLineAsync($"{md.MethodName} - 无法获取类型[{md.ActingInstanceType}]的实例");
IsStopStart = true;
}
}
if (IsStopStart)
{
return;// 调用节点初始化后检查状态
}
#endregion
#region 检查并修正初始化、加载时、退出时方法作用的对象,保证后续不会报错(已注释)
//foreach (var md in initMethods) // 初始化
//{
// md.ActingInstance ??= Context.SereinIoc.GetOrRegisterInstantiate(md.ActingInstanceType);
//}
//foreach (var md in loadingMethods) // 加载
//{
// md.ActingInstance ??= Context.SereinIoc.GetOrRegisterInstantiate(md.ActingInstanceType);
//}
//foreach (var md in exitMethods) // 初始化
//{
// md.ActingInstance ??= Context.SereinIoc.GetOrRegisterInstantiate(md.ActingInstanceType);
//}
#endregion
#region 执行初始化,绑定IOC容器,再执行加载时
if (autoRegisterTypes.TryGetValue(RegisterSequence.FlowInit, out var flowInitTypes))
{
foreach (var type in flowInitTypes)
{
env.IOC.Register(type); // 初始化前注册
}
}
Context.Env.IOC.Build(); // 绑定初始化时注册的类型
//object?[]? args = [Context];
foreach (var md in initMethods) // 初始化
{
if (!env.TryGetDelegateDetails(md.MethodName, out var dd))
{
throw new Exception("不存在对应委托");
}
await dd.Invoke(md.ActingInstance, [Context]);
//((Func