对象预览器支持了值类型集合成员的简单预览

This commit is contained in:
fengjiayi
2024-09-24 22:39:43 +08:00
parent 8a502b77d4
commit 06f6d2f34b
28 changed files with 1674 additions and 859 deletions

View File

@@ -21,9 +21,13 @@ namespace Serein.NodeFlow
/// <param name="methodDetails"></param>
public class FlowStarter
{
/// <summary>
/// 全局触发器CTS
/// </summary>
public const string FlipFlopCtsName = "<>.FlowFlipFlopCts";
public FlowStarter()
{
SereinIOC = new SereinIOC();
}
/// <summary>
@@ -44,22 +48,25 @@ namespace Serein.NodeFlow
/// </summary>
Completion,
}
/// <summary>
/// 起点流程运行状态
/// </summary>
public RunState FlowState { get; private set; } = RunState.NoStart;
/// <summary>
/// 全局触发器运行状态
/// </summary>
public RunState FlipFlopState { get; private set; } = RunState.NoStart;
/// <summary>
/// 控制触发器
/// </summary>
private CancellationTokenSource _flipFlopCts = null;
public const string FlipFlopCtsName = "<>.FlowFlipFlopCts";
/// <summary>
/// 是否停止启动
/// </summary>
private bool IsStopStart = false;
public bool IsStopStart = false;
/// <summary>
/// 运行状态
/// </summary>
public RunState FlowState { get; private set; } = RunState.NoStart;
public RunState FlipFlopState { get; private set; } = RunState.NoStart;
/// <summary>
/// 运行时的IOC容器
/// </summary>
private ISereinIOC SereinIOC { get; } = null;
/// <summary>
/// 结束运行时需要执行的方法
/// </summary>
@@ -77,15 +84,17 @@ namespace Serein.NodeFlow
}
}
/// <summary>
/// 从选定的节点开始运行
/// </summary>
/// <param name="startNode"></param>
/// <returns></returns>
public async Task StartFlowInSelectNodeAsync(NodeModelBase startNode)
{
if (Context is null) return;
await startNode.StartExecute(Context); // 开始运行时从选定节点开始运行
}
// <summary>
// 开始运行
// </summary>
// <param name="startNode">起始节点</param>
// <param name="env">运行环境</param>
// <param name="runNodeMd">环境中已加载的所有节点方法</param>
// <param name="flipflopNodes">触发器节点</param>
// <returns></returns>
/// <summary>
/// 开始运行
@@ -130,11 +139,11 @@ namespace Serein.NodeFlow
var isNetFramework = false;
if (isNetFramework)
{
Context = new Serein.Library.Framework.NodeFlow.DynamicContext(SereinIOC, env);
Context = new Serein.Library.Framework.NodeFlow.DynamicContext(env);
}
else
{
Context = new Serein.Library.Core.NodeFlow.DynamicContext(SereinIOC, env); // 从起始节点启动流程时创建上下文
Context = new Serein.Library.Core.NodeFlow.DynamicContext(env); // 从起始节点启动流程时创建上下文
}
#endregion
@@ -151,13 +160,14 @@ namespace Serein.NodeFlow
{
nodeMd.ActingInstance = null;
}
SereinIOC.Reset(); // 开始运行时清空ioc中注册的实例
env.IOC.Reset(); // 开始运行时清空ioc中注册的实例
env.IOC.CustomRegisterInstance(typeof(ISereinIOC).FullName, env);
// 初始化ioc容器中的类型对象
foreach (var md in thisRuningMds)
{
if (md.ActingInstanceType != null)
{
SereinIOC.Register(md.ActingInstanceType);
env.IOC.Register(md.ActingInstanceType);
}
else
{
@@ -168,11 +178,11 @@ namespace Serein.NodeFlow
CheckStartState(); // 初始化IOC后检查状态
SereinIOC.Build(); // 流程启动前的初始化
env.IOC.Build(); // 流程启动前的初始化
foreach (var md in thisRuningMds)
{
md.ActingInstance = SereinIOC.GetOrRegisterInstantiate(md.ActingInstanceType);
md.ActingInstance = env.IOC.GetOrRegisterInstantiate(md.ActingInstanceType);
if(md.ActingInstance is null)
{
await Console.Out.WriteLineAsync($"{md.MethodName} - 无法获取类型[{md.ActingInstanceType}]的实例");
@@ -206,20 +216,20 @@ namespace Serein.NodeFlow
{
((Action<object, object?[]?>)md.MethodDelegate).Invoke(md.ActingInstance, [Context]);
}
Context.SereinIoc.Build(); // 绑定初始化时注册的类型
Context.Env.IOC.Build(); // 绑定初始化时注册的类型
foreach (var md in loadingMethods) // 加载
{
//object?[]? data = [md.ActingInstance, args];
//md.MethodDelegate.DynamicInvoke(data);
((Action<object, object?[]?>)md.MethodDelegate).Invoke(md.ActingInstance, [Context]);
}
Context.SereinIoc.Build(); // 预防有人在加载时才注册类型,再绑定一次
Context.Env.IOC.Build(); // 预防有人在加载时才注册类型,再绑定一次
#endregion
#region 退
ExitAction = () =>
{
SereinIOC.Run<WebServer>(web => {
env.IOC.Run<WebServer>(web => {
web?.Stop();
});
@@ -248,7 +258,7 @@ namespace Serein.NodeFlow
FlipFlopState = RunState.Running;
// 如果存在需要启动的触发器,则开始启动
_flipFlopCts = new CancellationTokenSource();
SereinIOC.CustomRegisterInstance(FlipFlopCtsName, _flipFlopCts,false);
env.IOC.CustomRegisterInstance(FlipFlopCtsName, _flipFlopCts,false);
// 使用 TaskCompletionSource 创建未启动的触发器任务
var tasks = flipflopNodes.Select(async node =>
@@ -270,7 +280,6 @@ namespace Serein.NodeFlow
catch (Exception ex)
{
await Console.Out.WriteLineAsync(ex.ToString());
// await Console.Out.WriteLineAsync(ex.Message);
}
finally
{
@@ -279,26 +288,26 @@ namespace Serein.NodeFlow
#endregion
}
public void AddFlipflopInRuning(SingleFlipflopNode singleFlipFlopNode, IFlowEnvironment flowEnvironment)
public void AddFlipflopInRuning(SingleFlipflopNode singleFlipFlopNode, IFlowEnvironment env)
{
_ = Task.Run(async () =>
{
// 设置对象
singleFlipFlopNode.MethodDetails.ActingInstance = SereinIOC.GetOrRegisterInstantiate(singleFlipFlopNode.MethodDetails.ActingInstanceType);
await FlipflopExecute(flowEnvironment,singleFlipFlopNode); // 启动触发器
singleFlipFlopNode.MethodDetails.ActingInstance = env.IOC.GetOrRegisterInstantiate(singleFlipFlopNode.MethodDetails.ActingInstanceType);
await FlipflopExecute(env,singleFlipFlopNode); // 启动触发器
});
}
/// <summary>
/// 启动全局触发器
/// </summary>
/// <param name="flowEnvironment">流程运行全局环境</param>
/// <param name="env">流程运行全局环境</param>
/// <param name="singleFlipFlopNode">需要全局监听信号的触发器</param>
/// <returns></returns>
private async Task FlipflopExecute(IFlowEnvironment flowEnvironment,SingleFlipflopNode singleFlipFlopNode)
private async Task FlipflopExecute(IFlowEnvironment env,SingleFlipflopNode singleFlipFlopNode)
{
var context = new DynamicContext(SereinIOC, flowEnvironment); // 启动全局触发器时新建上下文
var context = new DynamicContext(env); // 启动全局触发器时新建上下文
try
{
@@ -368,75 +377,6 @@ namespace Serein.NodeFlow
}
#if false
/// <summary>
/// 全局触发器开始执行相关分支
/// </summary>
/// <param name="context">上下文</param>
/// <param name="singleFlipFlopNode">被触发的全局触发器</param>
/// <param name="connectionType">分支类型</param>
/// <returns></returns>
public async Task GlobalFlipflopExecute(IDynamicContext context, SingleFlipflopNode singleFlipFlopNode,
ConnectionType connectionType, CancellationTokenSource cts)
{
bool skip = true;
Stack<NodeModelBase> stack = new Stack<NodeModelBase>();
stack.Push(singleFlipFlopNode);
while (stack.Count > 0 && !cts.IsCancellationRequested) // 循环中直到栈为空才会退出循环
{
// 从栈中弹出一个节点作为当前节点进行处理
var currentNode = stack.Pop();
// 设置方法执行的对象
//if (currentNode.MethodDetails?.ActingInstance == null && currentNode.MethodDetails?.ActingInstanceType is not null)
//{
// currentNode.MethodDetails.ActingInstance ??= context.SereinIoc.GetOrRegisterInstantiate(currentNode.MethodDetails.ActingInstanceType);
//}
// 首先执行上游分支
var upstreamNodes = currentNode.SuccessorNodes[ConnectionType.Upstream];
for (int i = upstreamNodes.Count - 1; i >= 0; i--)
{
upstreamNodes[i].PreviousNode = currentNode;
await upstreamNodes[i].StartExecute(context); // 执行全局触发器的上游分支
}
// 当前节点是已经触发了的全局触发器,所以跳过,难道每次都要判断一次?
if (skip)
{
skip = false;
}
else
{
currentNode.FlowData = await currentNode.ExecutingAsync(context);
if (currentNode.NextOrientation == ConnectionType.None)
{
break; // 不再执行
}
connectionType = currentNode.NextOrientation;
}
// 获取下一分支
var nextNodes = currentNode.SuccessorNodes[connectionType];
// 将下一个节点集合中的所有节点逆序推入栈中
for (int i = nextNodes.Count - 1; i >= 0; i--)
{
nextNodes[i].PreviousNode = currentNode;
stack.Push(nextNodes[i]);
}
}}
#endif
}
}