Files
serein-flow/NodeFlow/FlowStarter.cs

164 lines
6.2 KiB
C#
Raw Normal View History

using Serein.Library.Api;
using Serein.Library.Core.NodeFlow;
using Serein.Library.Entity;
using Serein.Library.Enums;
using Serein.Library.Utils;
using Serein.NodeFlow.Base;
2024-08-06 16:09:46 +08:00
using Serein.NodeFlow.Model;
namespace Serein.NodeFlow
{
/// <summary>
/// 流程启动器
/// </summary>
/// <param name="serviceContainer"></param>
/// <param name="methodDetails"></param>
public class FlowStarter(ISereinIoc serviceContainer, List<MethodDetails> methodDetails)
2024-08-06 16:09:46 +08:00
{
private readonly ISereinIoc ServiceContainer = serviceContainer;
2024-08-06 16:09:46 +08:00
private readonly List<MethodDetails> methodDetails = methodDetails;
private Action ExitAction = null; //退出方法
private IDynamicContext context = null; //上下文
public NodeRunCts MainCts;
2024-08-06 16:09:46 +08:00
/// <summary>
/// 开始运行
2024-08-06 16:09:46 +08:00
/// </summary>
/// <param name="nodes"></param>
/// <returns></returns>
// public async Task RunAsync(List<NodeModelBase> nodes, IFlowEnvironment flowEnvironment)
public async Task RunAsync(NodeModelBase startNode, IFlowEnvironment flowEnvironment, List<SingleFlipflopNode> flipflopNodes)
2024-08-06 16:09:46 +08:00
{
// var startNode = nodes.FirstOrDefault(p => p.IsStart);
2024-08-06 16:09:46 +08:00
if (startNode == null) { return; }
var isNetFramework = true;
if (isNetFramework)
{
context = new Serein.Library.Framework.NodeFlow.DynamicContext(ServiceContainer, flowEnvironment);
}
else
{
context = new Serein.Library.Core.NodeFlow.DynamicContext(ServiceContainer, flowEnvironment);
}
2024-08-06 16:09:46 +08:00
MainCts = ServiceContainer.CreateServiceInstance<NodeRunCts>();
2024-08-06 16:09:46 +08:00
var initMethods = methodDetails.Where(it => it.MethodDynamicType == NodeType.Init).ToList();
var loadingMethods = methodDetails.Where(it => it.MethodDynamicType == NodeType.Loading).ToList();
var exitMethods = methodDetails.Where(it => it.MethodDynamicType == NodeType.Exit).ToList();
2024-08-06 16:09:46 +08:00
ExitAction = () =>
{
//ServiceContainer.Run<WebServer>((web) =>
//{
// web?.Stop();
//});
2024-08-06 16:09:46 +08:00
foreach (MethodDetails? md in exitMethods)
{
object?[]? args = [context];
object?[]? data = [md.ActingInstance, args];
md.MethodDelegate.DynamicInvoke(data);
}
if (context != null && context.NodeRunCts != null && !context.NodeRunCts.IsCancellationRequested)
{
context.NodeRunCts.Cancel();
}
if (MainCts != null && !MainCts.IsCancellationRequested) MainCts.Cancel();
ServiceContainer.Reset();
};
foreach (var md in initMethods) // 初始化 - 调用方法
{
object?[]? args = [context];
object?[]? data = [md.ActingInstance, args];
md.MethodDelegate.DynamicInvoke(data);
}
context.SereinIoc.Build();
2024-08-06 16:09:46 +08:00
foreach (var md in loadingMethods) // 加载
{
object?[]? args = [context];
object?[]? data = [md.ActingInstance, args];
md.MethodDelegate.DynamicInvoke(data);
}
// 运行触发器节点
2024-08-06 16:09:46 +08:00
var singleFlipflopNodes = flipflopNodes.Select(it => (SingleFlipflopNode)it).ToArray();
// 使用 TaskCompletionSource 创建未启动的任务
var tasks = singleFlipflopNodes.Select(async node =>
{
await FlipflopExecute(node, flowEnvironment);
2024-08-06 16:09:46 +08:00
}).ToArray();
try
{
await Task.Run(async () =>
{
await Task.WhenAll([startNode.StartExecution(context), .. tasks]);
});
2024-08-06 16:09:46 +08:00
}
catch (Exception ex)
{
await Console.Out.WriteLineAsync(ex.ToString());
}
}
/// <summary>
/// 启动触发器
/// </summary>
private async Task FlipflopExecute(SingleFlipflopNode singleFlipFlopNode, IFlowEnvironment flowEnvironment)
2024-08-06 16:09:46 +08:00
{
DynamicContext context = new DynamicContext(ServiceContainer, flowEnvironment);
2024-08-06 16:09:46 +08:00
MethodDetails md = singleFlipFlopNode.MethodDetails;
var del = md.MethodDelegate;
2024-08-06 16:09:46 +08:00
try
{
//var func = md.ExplicitDatas.Length == 0 ? (Func<object, object, Task<FlipflopContext<dynamic>>>)del : (Func<object, object[], Task<FlipflopContext<dynamic>>>)del;
var func = md.ExplicitDatas.Length == 0 ? (Func<object, object, Task<IFlipflopContext>>)del : (Func<object, object[], Task<IFlipflopContext>>)del;
2024-08-06 16:09:46 +08:00
while (!MainCts.IsCancellationRequested) // 循环中直到栈为空才会退出
{
object?[]? parameters = singleFlipFlopNode.GetParameters(context, md);
// 调用委托并获取结果
IFlipflopContext flipflopContext = await func.Invoke(md.ActingInstance, parameters);
2024-08-06 16:09:46 +08:00
if (flipflopContext.State == FlowStateType.Succeed)
2024-08-06 16:09:46 +08:00
{
singleFlipFlopNode.FlowState = FlowStateType.Succeed;
2024-08-06 16:09:46 +08:00
singleFlipFlopNode.FlowData = flipflopContext.Data;
var tasks = singleFlipFlopNode.PreviousNodes[ConnectionType.IsSucceed].Select(nextNode =>
2024-08-06 16:09:46 +08:00
{
var context = new DynamicContext(ServiceContainer,flowEnvironment);
2024-08-06 16:09:46 +08:00
nextNode.PreviousNode = singleFlipFlopNode;
return nextNode.StartExecution(context);
2024-08-06 16:09:46 +08:00
}).ToArray();
Task.WaitAll(tasks);
}
else
{
break;
}
2024-08-06 16:09:46 +08:00
}
}
catch (Exception ex)
{
await Console.Out.WriteLineAsync(ex.ToString());
}
}
public void Exit()
{
ExitAction?.Invoke();
}
}
}