Files
serein-flow/NodeFlow/FlowStarter.cs

196 lines
7.6 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
2024-08-06 16:09:46 +08:00
{
public FlowStarter(ISereinIoc serviceContainer/*, List<MethodDetails> methodDetails*/)
{
SereinIoc = serviceContainer;
}
private ISereinIoc SereinIoc { get; }
// private List<MethodDetails> MethodDetailss { get; }
private Action ExitAction { get; set; } = null; //退出方法
private IDynamicContext Context { get; set; } = null; //上下文
public NodeRunCts MainCts { get; set; }
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<MethodDetails> methodDetailss, 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(SereinIoc, flowEnvironment);
}
else
{
Context = new Serein.Library.Core.NodeFlow.DynamicContext(SereinIoc, flowEnvironment);
}
2024-08-06 16:09:46 +08:00
MainCts = SereinIoc.CreateServiceInstance<NodeRunCts>();
foreach (var md in methodDetailss)
{
SereinIoc.Register(md.ActingInstanceType);
}
SereinIoc.Build();
foreach (var md in flipflopNodes.Select(it => it.MethodDetails).ToArray())
{
md.ActingInstance = SereinIoc.GetOrCreateServiceInstance(md.ActingInstanceType);
}
foreach (var md in methodDetailss)
{
md.ActingInstance = SereinIoc.GetOrCreateServiceInstance(md.ActingInstanceType);
}
2024-08-06 16:09:46 +08:00
var initMethods = methodDetailss.Where(it => it.MethodDynamicType == NodeType.Init).ToList();
var loadingMethods = methodDetailss.Where(it => it.MethodDynamicType == NodeType.Loading).ToList();
var exitMethods = methodDetailss.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)
{
md.ActingInstance = Context.SereinIoc.GetOrInstantiate(md.ActingInstanceType);
object?[]? args = [Context];
2024-08-06 16:09:46 +08:00
object?[]? data = [md.ActingInstance, args];
md.MethodDelegate.DynamicInvoke(data);
}
if (Context != null && Context.NodeRunCts != null && !Context.NodeRunCts.IsCancellationRequested)
2024-08-06 16:09:46 +08:00
{
Context.NodeRunCts.Cancel();
2024-08-06 16:09:46 +08:00
}
if (MainCts != null && !MainCts.IsCancellationRequested) MainCts.Cancel();
SereinIoc.Reset();
2024-08-06 16:09:46 +08:00
};
Context.SereinIoc.Build();
2024-08-06 16:09:46 +08:00
foreach (var md in initMethods) // 初始化 - 调用方法
{
md.ActingInstance ??= Context.SereinIoc.GetOrInstantiate(md.ActingInstanceType);
object?[]? args = [Context];
2024-08-06 16:09:46 +08:00
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) // 加载
{
md.ActingInstance ??= Context.SereinIoc.GetOrInstantiate(md.ActingInstanceType);
object?[]? args = [Context];
2024-08-06 16:09:46 +08:00
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();
_ = Task.WhenAll(tasks);
2024-08-06 16:09:46 +08:00
try
{
await Task.Run(async () =>
{
await startNode.StartExecution(Context);
//await Task.WhenAll([startNode.StartExecution(Context), .. tasks]);
});
// 等待结束
while (!MainCts.IsCancellationRequested)
{
await Task.Delay(100);
}
2024-08-06 16:09:46 +08:00
}
catch (Exception ex)
{
await Console.Out.WriteLineAsync(ex.ToString());
}
2024-08-06 16:09:46 +08:00
}
/// <summary>
/// 启动触发器
/// </summary>
private async Task FlipflopExecute(SingleFlipflopNode singleFlipFlopNode, IFlowEnvironment flowEnvironment)
2024-08-06 16:09:46 +08:00
{
DynamicContext context = new DynamicContext(SereinIoc, 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);
// 调用委托并获取结果
md.ActingInstance = context.SereinIoc.GetOrInstantiate(md.ActingInstanceType);
IFlipflopContext flipflopContext = await func.Invoke(md.ActingInstance, parameters);
2024-08-06 16:09:46 +08:00
2024-09-15 22:07:10 +08:00
ConnectionType connection = flipflopContext.State.ToContentType();
if (connection != ConnectionType.None)
2024-08-06 16:09:46 +08:00
{
2024-09-15 22:07:10 +08:00
singleFlipFlopNode.NextOrientation = connection;
2024-08-06 16:09:46 +08:00
singleFlipFlopNode.FlowData = flipflopContext.Data;
2024-09-15 22:07:10 +08:00
var tasks = singleFlipFlopNode.SuccessorNodes[connection].Select(nextNode =>
2024-08-06 16:09:46 +08:00
{
var context = new DynamicContext(SereinIoc,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();
}
}
}