mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-03-19 16:06:33 +08:00
使用异步重构了节点执行方法,将触发器节点与其他节点统一。使用Channel代替Tcs更改了信号触发,使其符合异步编程的习惯。增加了节点是否启用勾选框、参数遮罩勾选框,节点右键面板增加中断功能(试验)。增加了选择后被选择的节点的视觉效果。更改平移缩放逻辑,使其更加符合一般的使用习惯。
This commit is contained in:
@@ -22,12 +22,14 @@ namespace Serein.NodeFlow.Base
|
||||
PreviousNodes[ctType] = [];
|
||||
SuccessorNodes[ctType] = [];
|
||||
}
|
||||
DebugSetting = new NodeDebugSetting();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 是否中断(调试中断功能)
|
||||
/// 调试功能
|
||||
/// </summary>
|
||||
public bool IsInterrupt { get; set; }
|
||||
public NodeDebugSetting DebugSetting { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 节点对应的控件类型
|
||||
@@ -84,13 +86,7 @@ namespace Serein.NodeFlow.Base
|
||||
}
|
||||
|
||||
|
||||
public class DebugInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否中断
|
||||
/// </summary>
|
||||
public bool IsInterrupt { get;set; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -11,6 +11,7 @@ using System.Linq;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static Serein.Library.Utils.ChannelFlowInterrupt;
|
||||
|
||||
namespace Serein.NodeFlow.Base
|
||||
{
|
||||
@@ -20,6 +21,32 @@ namespace Serein.NodeFlow.Base
|
||||
/// </summary>
|
||||
public abstract partial class NodeModelBase : IDynamicFlowNode
|
||||
{
|
||||
|
||||
|
||||
#region 调试中断
|
||||
|
||||
public Action? CancelInterruptCallback;
|
||||
|
||||
/// <summary>
|
||||
/// 中断节点
|
||||
/// </summary>
|
||||
public void Interrupt()
|
||||
{
|
||||
this.DebugSetting.InterruptClass = InterruptClass.Branch;
|
||||
this.DebugSetting.IsInterrupt = true;
|
||||
}
|
||||
/// <summary>
|
||||
/// 不再中断
|
||||
/// </summary>
|
||||
public void CancelInterrupt()
|
||||
{
|
||||
this.DebugSetting.InterruptClass = InterruptClass.None;
|
||||
this.DebugSetting.IsInterrupt = false;
|
||||
CancelInterruptCallback?.Invoke();
|
||||
CancelInterruptCallback = null;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 导出/导入项目文件节点信息
|
||||
|
||||
internal abstract Parameterdata[] GetParameterdatas();
|
||||
@@ -84,19 +111,19 @@ namespace Serein.NodeFlow.Base
|
||||
|
||||
#endregion
|
||||
|
||||
#region 节点方法的执行
|
||||
|
||||
/// <summary>
|
||||
/// 开始执行
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public async Task StartExecution(IDynamicContext context)
|
||||
public async Task StartExecute(IDynamicContext context)
|
||||
{
|
||||
var cts = context.SereinIoc.GetOrRegisterInstantiate<CancellationTokenSource>();
|
||||
|
||||
Stack<NodeModelBase> stack = new Stack<NodeModelBase>();
|
||||
stack.Push(this);
|
||||
|
||||
while (stack.Count > 0 && !cts.IsCancellationRequested) // 循环中直到栈为空才会退出循环
|
||||
{
|
||||
// 从栈中弹出一个节点作为当前节点进行处理
|
||||
@@ -108,51 +135,112 @@ namespace Serein.NodeFlow.Base
|
||||
currentNode.MethodDetails.ActingInstance ??= context.SereinIoc.GetOrRegisterInstantiate(currentNode.MethodDetails.ActingInstanceType);
|
||||
}
|
||||
|
||||
//if (TryCreateInterruptTask(context, currentNode, out Task<CancelType>? task))
|
||||
//{
|
||||
// var cancelType = await task!;
|
||||
// await Console.Out.WriteLineAsync($"[{currentNode.MethodDetails.MethodName}]中断已{(cancelType == CancelType.Manual ? "手动取消" : "自动取消")},开始执行后继分支");
|
||||
//}
|
||||
|
||||
#region 执行相关
|
||||
// 首先执行上游分支
|
||||
var upstreamNodes = currentNode.SuccessorNodes[ConnectionType.Upstream];
|
||||
for (int i = upstreamNodes.Count - 1; i >= 0; i--)
|
||||
{
|
||||
upstreamNodes[i].PreviousNode = currentNode;
|
||||
await upstreamNodes[i].StartExecution(context); // 执行上游分支
|
||||
if (upstreamNodes[i].DebugSetting.IsEnable) // 排除未启用的上游节点
|
||||
{
|
||||
upstreamNodes[i].PreviousNode = currentNode;
|
||||
await upstreamNodes[i].StartExecute(context); // 执行流程节点的上游分支
|
||||
}
|
||||
}
|
||||
|
||||
currentNode.FlowData = currentNode.ExecutingAsync(context); // 流程中正常执行
|
||||
|
||||
// 判断是否为触发器节点,如果是,则开始等待。
|
||||
if (currentNode.MethodDetails != null && currentNode.MethodDetails.MethodDynamicType == NodeType.Flipflop)
|
||||
{
|
||||
|
||||
currentNode.FlowData = await currentNode.ExecuteAsync(context); // 流程中遇到了触发器
|
||||
}
|
||||
else
|
||||
{
|
||||
currentNode.FlowData = currentNode.Execute(context); // 流程中正常执行
|
||||
}
|
||||
//if (currentNode.MethodDetails != null && currentNode.MethodDetails.MethodDynamicType == NodeType.Flipflop)
|
||||
//{
|
||||
|
||||
if(currentNode.NextOrientation == ConnectionType.None)
|
||||
{
|
||||
// 不再执行
|
||||
break;
|
||||
}
|
||||
// currentNode.FlowData = await currentNode.ExecutingFlipflopAsync(context); // 流程中遇到了触发器
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
|
||||
//}
|
||||
#endregion
|
||||
|
||||
// 获取下一分支
|
||||
|
||||
|
||||
|
||||
#region 执行完成
|
||||
if (currentNode.NextOrientation == ConnectionType.None) break; // 不再执行
|
||||
|
||||
|
||||
// 选择后继分支
|
||||
var nextNodes = currentNode.SuccessorNodes[currentNode.NextOrientation];
|
||||
|
||||
// 将下一个节点集合中的所有节点逆序推入栈中
|
||||
for (int i = nextNodes.Count - 1; i >= 0; i--)
|
||||
{
|
||||
nextNodes[i].PreviousNode = currentNode;
|
||||
stack.Push(nextNodes[i]);
|
||||
}
|
||||
// 排除未启用的节点
|
||||
if (nextNodes[i].DebugSetting.IsEnable)
|
||||
{
|
||||
nextNodes[i].PreviousNode = currentNode;
|
||||
stack.Push(nextNodes[i]);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
public static bool TryCreateInterruptTask(IDynamicContext context, NodeModelBase currentNode, out Task<CancelType>? task)
|
||||
{
|
||||
if (!currentNode.DebugSetting.IsInterrupt)
|
||||
{
|
||||
task = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
Task<CancelType>? result = null;
|
||||
bool haveTask = false;
|
||||
Console.WriteLine($"[{currentNode.MethodDetails.MethodName}]在当前分支中断");
|
||||
if (currentNode.DebugSetting.InterruptClass == InterruptClass.None)
|
||||
{
|
||||
haveTask = false;
|
||||
task = null;
|
||||
currentNode.DebugSetting.IsInterrupt = false; // 纠正设置
|
||||
}
|
||||
if (currentNode.DebugSetting.InterruptClass == InterruptClass.Branch) // 中断当前分支
|
||||
{
|
||||
currentNode.DebugSetting.IsInterrupt = true;
|
||||
haveTask = true;
|
||||
task = context.FlowEnvironment.ChannelFlowInterrupt.CreateChannelWithTimeoutAsync(currentNode.Guid, TimeSpan.FromSeconds(1));
|
||||
currentNode.CancelInterruptCallback ??= () => context.FlowEnvironment.ChannelFlowInterrupt.TriggerSignal(currentNode.Guid);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
haveTask = false;
|
||||
task = null;
|
||||
}
|
||||
|
||||
return haveTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行节点对应的方法
|
||||
/// </summary>
|
||||
/// <param name="context">流程上下文</param>
|
||||
/// <returns>节点传回数据对象</returns>
|
||||
public virtual object? Execute(IDynamicContext context)
|
||||
public virtual async Task<object?> ExecutingAsync(IDynamicContext context)
|
||||
{
|
||||
#region 调试中断
|
||||
if (TryCreateInterruptTask(context, this, out Task<CancelType>? task))
|
||||
{
|
||||
var cancelType = await task!;
|
||||
await Console.Out.WriteLineAsync($"[{this.MethodDetails.MethodName}]中断已{(cancelType == CancelType.Manual ? "手动取消" : "自动取消")},开始执行后继分支");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
MethodDetails md = MethodDetails;
|
||||
var del = md.MethodDelegate;
|
||||
object instance = md.ActingInstance;
|
||||
@@ -186,38 +274,7 @@ namespace Serein.NodeFlow.Base
|
||||
/// <param name="context"></param>
|
||||
/// <returns>节点传回数据对象</returns>
|
||||
/// <exception cref="RuningException"></exception>
|
||||
public virtual async Task<object?> ExecuteAsync(IDynamicContext context)
|
||||
{
|
||||
MethodDetails md = MethodDetails;
|
||||
Delegate del = md.MethodDelegate;
|
||||
object instance = md.ActingInstance;
|
||||
var haveParameter = md.ExplicitDatas.Length >= 0;
|
||||
try
|
||||
{
|
||||
// 调用委托并获取结果
|
||||
Task<IFlipflopContext> flipflopTask = haveParameter switch
|
||||
{
|
||||
true => ((Func<object, object?[]?, Task<IFlipflopContext>>)del).Invoke(instance, GetParameters(context, md)), // 执行流程中的触发器方法时获取入参参数
|
||||
false => ((Func<object, Task<IFlipflopContext>>)del).Invoke(instance),
|
||||
};
|
||||
|
||||
IFlipflopContext flipflopContext = (await flipflopTask) ?? throw new FlipflopException("没有返回上下文");
|
||||
NextOrientation = flipflopContext.State.ToContentType();
|
||||
return flipflopContext.Data;
|
||||
}
|
||||
//catch(FlipflopException ex)
|
||||
//{
|
||||
// NextOrientation = ConnectionType.IsError;
|
||||
// RuningException = ex;
|
||||
// return null;
|
||||
//}
|
||||
catch (Exception ex)
|
||||
{
|
||||
NextOrientation = ConnectionType.IsError;
|
||||
RuningException = ex;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#region 节点转换的委托类型
|
||||
@@ -248,45 +305,34 @@ namespace Serein.NodeFlow.Base
|
||||
public object?[]? GetParameters(IDynamicContext context, MethodDetails md)
|
||||
{
|
||||
// 用正确的大小初始化参数数组
|
||||
var types = md.ExplicitDatas.Select(it => it.DataType).ToArray();
|
||||
if (types.Length == 0)
|
||||
if (md.ExplicitDatas.Length == 0)
|
||||
{
|
||||
return [md.ActingInstance];
|
||||
return [];// md.ActingInstance
|
||||
}
|
||||
|
||||
object?[]? parameters = new object[types.Length];
|
||||
object?[]? parameters = new object[md.ExplicitDatas.Length];
|
||||
var flowData = PreviousNode?.FlowData; // 当前传递的数据
|
||||
var previousDataType = flowData?.GetType();
|
||||
|
||||
for (int i = 0; i < types.Length; i++)
|
||||
for (int i = 0; i < parameters.Length; i++)
|
||||
{
|
||||
//if (flowData is null)
|
||||
//{
|
||||
// parameters[i] = md.ExplicitDatas[i].DataType switch
|
||||
// {
|
||||
// Type t when t == typeof(IDynamicContext) => context, // 上下文
|
||||
// Type t when t == typeof(MethodDetails) => md, // 节点方法描述
|
||||
// Type t when t == typeof(NodeModelBase) => this, // 节点实体类
|
||||
// _ => null,
|
||||
// };
|
||||
// continue; // 上一节点数据为空,提前跳过
|
||||
//}
|
||||
object? inputParameter; //
|
||||
|
||||
object? inputParameter; // 存放解析的临时参数
|
||||
var ed = md.ExplicitDatas[i]; // 方法入参描述
|
||||
|
||||
|
||||
|
||||
if (ed.IsExplicitData)
|
||||
{
|
||||
|
||||
if (ed.DataValue.StartsWith("@get", StringComparison.OrdinalIgnoreCase))
|
||||
|
||||
if (ed.DataValue.StartsWith("@get", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// 执行表达式从上一节点获取对象
|
||||
inputParameter = SerinExpressionEvaluator.Evaluate(ed.DataValue, flowData, out _);
|
||||
inputParameter = SerinExpressionEvaluator.Evaluate(ed.DataValue, flowData, out _);
|
||||
}
|
||||
else
|
||||
else
|
||||
{
|
||||
// 使用输入的固定值
|
||||
inputParameter = ed.DataValue;
|
||||
inputParameter = ed.DataValue;
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -303,16 +349,26 @@ namespace Serein.NodeFlow.Base
|
||||
Type t when t == typeof(MethodDetails) => md, // 节点方法描述
|
||||
Type t when t == typeof(NodeModelBase) => this, // 节点实体类
|
||||
Type t when t == typeof(Guid) => new Guid(inputParameter?.ToString()),
|
||||
Type t when t == typeof(decimal) => decimal.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(DateTime) => DateTime.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(string) => inputParameter?.ToString(),
|
||||
Type t when t == typeof(char) => char.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(bool) => bool.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(byte) => byte.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(int) => int.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(long) => long.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(DateTime) => DateTime.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(float) => float.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(double) => double.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(bool) => inputParameter is null ? false : bool.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(float) => inputParameter is null ? 0F : float.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(decimal) => inputParameter is null ? 0 : decimal.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(double) => inputParameter is null ? 0 : double.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(sbyte) => inputParameter is null ? 0 : sbyte.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(byte) => inputParameter is null ? 0 : byte.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(short) => inputParameter is null ? 0 : short.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(ushort) => inputParameter is null ? 0U : ushort.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(int) => inputParameter is null ? 0 : int.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(uint) => inputParameter is null ? 0U : uint.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(long) => inputParameter is null ? 0L : long.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(ulong) => inputParameter is null ? 0UL : ulong.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(nint) => inputParameter is null ? 0 : nint.Parse(inputParameter?.ToString()),
|
||||
Type t when t == typeof(nuint) => inputParameter is null ? 0 : nuint.Parse(inputParameter?.ToString()),
|
||||
|
||||
|
||||
|
||||
Type t when t.IsEnum => Enum.Parse(ed.DataType, ed.DataValue),// 需要枚举
|
||||
Type t when t.IsArray => (inputParameter as Array)?.Cast<object>().ToList(),
|
||||
Type t when t.IsGenericType && t.GetGenericTypeDefinition() == typeof(List<>) => inputParameter,
|
||||
@@ -329,6 +385,10 @@ namespace Serein.NodeFlow.Base
|
||||
return parameters;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// json文本反序列化为对象
|
||||
/// </summary>
|
||||
|
||||
@@ -7,14 +7,8 @@ using Serein.Library.Utils;
|
||||
using Serein.NodeFlow.Base;
|
||||
using Serein.NodeFlow.Model;
|
||||
using Serein.NodeFlow.Tool;
|
||||
using System.Diagnostics;
|
||||
using System.Net.Mime;
|
||||
using System.Numerics;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Xml.Linq;
|
||||
using static Serein.NodeFlow.FlowStarter;
|
||||
|
||||
namespace Serein.NodeFlow
|
||||
@@ -37,9 +31,8 @@ namespace Serein.NodeFlow
|
||||
*/
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 运行环境
|
||||
/// </summary>
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
@@ -47,74 +40,99 @@ namespace Serein.NodeFlow
|
||||
/// </summary>
|
||||
public class FlowEnvironment : IFlowEnvironment
|
||||
{
|
||||
public FlowEnvironment()
|
||||
{
|
||||
ChannelFlowInterrupt = new ChannelFlowInterrupt();
|
||||
LoadedAssemblyPaths = new List<string>();
|
||||
LoadedAssemblies = new List<Assembly>();
|
||||
MethodDetailss = new List<MethodDetails>();
|
||||
Nodes = new Dictionary<string, NodeModelBase>();
|
||||
FlipflopNodes = new List<SingleFlipflopNode>();
|
||||
IsGlobalInterrupt = false;
|
||||
flowStarter = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 节点的命名空间
|
||||
/// </summary>
|
||||
public const string NodeSpaceName = $"{nameof(Serein)}.{nameof(Serein.NodeFlow)}.{nameof(Serein.NodeFlow.Model)}";
|
||||
|
||||
#region 环境接口事件
|
||||
/// <summary>
|
||||
/// 加载Dll
|
||||
/// </summary>
|
||||
public event LoadDLLHandler OnDllLoad;
|
||||
|
||||
/// <summary>
|
||||
/// 项目加载完成
|
||||
/// </summary>
|
||||
public event ProjectLoadedHandler OnProjectLoaded;
|
||||
|
||||
/// <summary>
|
||||
/// 节点连接属性改变事件
|
||||
/// </summary>
|
||||
public event NodeConnectChangeHandler OnNodeConnectChange;
|
||||
|
||||
/// <summary>
|
||||
/// 节点创建事件
|
||||
/// </summary>
|
||||
public event NodeCreateHandler OnNodeCreate;
|
||||
|
||||
/// <summary>
|
||||
/// 移除节点事件
|
||||
/// </summary>
|
||||
public event NodeRemoteHandler OnNodeRemote;
|
||||
|
||||
/// <summary>
|
||||
/// 起始节点变化事件
|
||||
/// </summary>
|
||||
public event StartNodeChangeHandler OnStartNodeChange;
|
||||
|
||||
/// <summary>
|
||||
/// 流程运行完成时间
|
||||
/// </summary>
|
||||
public event FlowRunCompleteHandler OnFlowRunComplete;
|
||||
|
||||
private FlowStarter? flowStarter = null;
|
||||
#endregion
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 一种轻量的IOC容器
|
||||
/// 流程中断器
|
||||
/// </summary>
|
||||
// public SereinIoc SereinIoc { get; } = new SereinIoc();
|
||||
public ChannelFlowInterrupt ChannelFlowInterrupt { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否全局中断
|
||||
/// </summary>
|
||||
public bool IsGlobalInterrupt { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 存储加载的程序集路径
|
||||
/// </summary>
|
||||
public List<string> LoadedAssemblyPaths { get; } = [];
|
||||
public List<string> LoadedAssemblyPaths { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 存储加载的程序集
|
||||
/// </summary>
|
||||
public List<Assembly> LoadedAssemblies { get; } = [];
|
||||
public List<Assembly> LoadedAssemblies { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 存储所有方法信息
|
||||
/// </summary>
|
||||
public List<MethodDetails> MethodDetailss { get; } = [];
|
||||
public List<MethodDetails> MethodDetailss { get; }
|
||||
|
||||
|
||||
public Dictionary<string, NodeModelBase> Nodes { get; } = [];
|
||||
|
||||
public List<NodeModelBase> Regions { get; } = [];
|
||||
/// <summary>
|
||||
/// 环境加载的节点集合
|
||||
/// </summary>
|
||||
public Dictionary<string, NodeModelBase> Nodes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 存放触发器节点(运行时全部调用)
|
||||
/// </summary>
|
||||
public List<SingleFlipflopNode> FlipflopNodes { get; } = [];
|
||||
public List<SingleFlipflopNode> FlipflopNodes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 私有属性
|
||||
/// 起始节点私有属性
|
||||
/// </summary>
|
||||
private NodeModelBase _startNode;
|
||||
|
||||
@@ -138,12 +156,19 @@ namespace Serein.NodeFlow
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 流程启动器(每次运行时都会重新new一个)
|
||||
/// </summary>
|
||||
private FlowStarter? flowStarter;
|
||||
|
||||
/// <summary>
|
||||
/// 异步运行
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task StartAsync()
|
||||
{
|
||||
ChannelFlowInterrupt?.CancelAllTasks();
|
||||
flowStarter = new FlowStarter();
|
||||
List<SingleFlipflopNode> flipflopNodes = Nodes.Values.Where(it => it.MethodDetails?.MethodDynamicType == NodeType.Flipflop && it.IsStart == false)
|
||||
.Select(it => (SingleFlipflopNode)it)
|
||||
@@ -171,6 +196,7 @@ namespace Serein.NodeFlow
|
||||
}
|
||||
public void Exit()
|
||||
{
|
||||
ChannelFlowInterrupt?.CancelAllTasks();
|
||||
flowStarter?.Exit();
|
||||
OnFlowRunComplete?.Invoke(new FlowEventArgs());
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ using Serein.Library.Web;
|
||||
using Serein.NodeFlow.Base;
|
||||
using Serein.NodeFlow.Model;
|
||||
using System.ComponentModel.Design;
|
||||
using static Serein.Library.Utils.ChannelFlowInterrupt;
|
||||
|
||||
namespace Serein.NodeFlow
|
||||
{
|
||||
@@ -22,6 +23,7 @@ namespace Serein.NodeFlow
|
||||
{
|
||||
SereinIOC = new SereinIOC();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 流程运行状态
|
||||
/// </summary>
|
||||
@@ -60,13 +62,12 @@ namespace Serein.NodeFlow
|
||||
/// 结束运行时需要执行的方法
|
||||
/// </summary>
|
||||
private Action ExitAction { get; set; } = null;
|
||||
|
||||
/// <summary>
|
||||
/// 运行的上下文
|
||||
/// </summary>
|
||||
private IDynamicContext Context { get; set; } = null;
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 开始运行
|
||||
/// </summary>
|
||||
@@ -106,7 +107,7 @@ namespace Serein.NodeFlow
|
||||
#endregion
|
||||
|
||||
#region 初始化运行环境的Ioc容器
|
||||
// 清除节点使用的对象
|
||||
// 清除节点使用的对象,筛选出需要初始化的方法描述
|
||||
var thisRuningMds = new List<MethodDetails>();
|
||||
thisRuningMds.AddRange(runNodeMd.Where(md => md is not null));
|
||||
thisRuningMds.AddRange(initMethods.Where(md => md is not null));
|
||||
@@ -213,11 +214,11 @@ namespace Serein.NodeFlow
|
||||
// 使用 TaskCompletionSource 创建未启动的触发器任务
|
||||
var tasks = flipflopNodes.Select(async node =>
|
||||
{
|
||||
await FlipflopExecute(node, env);
|
||||
await FlipflopExecute(env,node);
|
||||
}).ToArray();
|
||||
_ = Task.WhenAll(tasks);
|
||||
}
|
||||
await startNode.StartExecution(Context); // 从起始节点开始运行
|
||||
await startNode.StartExecute(Context); // 开始运行时从起始节点开始运行
|
||||
// 等待结束
|
||||
if (FlipFlopCts != null)
|
||||
{
|
||||
@@ -240,15 +241,17 @@ namespace Serein.NodeFlow
|
||||
{
|
||||
// 设置对象
|
||||
singleFlipFlopNode.MethodDetails.ActingInstance = SereinIOC.GetOrRegisterInstantiate(singleFlipFlopNode.MethodDetails.ActingInstanceType);
|
||||
await FlipflopExecute(singleFlipFlopNode, flowEnvironment); // 启动触发器
|
||||
await FlipflopExecute(flowEnvironment,singleFlipFlopNode); // 启动触发器
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 启动全局触发器
|
||||
/// </summary>
|
||||
private async Task FlipflopExecute(SingleFlipflopNode singleFlipFlopNode, IFlowEnvironment flowEnvironment)
|
||||
/// <param name="flowEnvironment">流程运行全局环境</param>
|
||||
/// <param name="singleFlipFlopNode">需要全局监听信号的触发器</param>
|
||||
/// <returns></returns>
|
||||
private async Task FlipflopExecute(IFlowEnvironment flowEnvironment,SingleFlipflopNode singleFlipFlopNode)
|
||||
{
|
||||
var context = new DynamicContext(SereinIOC, flowEnvironment);
|
||||
MethodDetails md = singleFlipFlopNode.MethodDetails;
|
||||
@@ -259,6 +262,7 @@ namespace Serein.NodeFlow
|
||||
{
|
||||
md.ActingInstance ??= context.SereinIoc.GetOrRegisterInstantiate(md.ActingInstanceType);
|
||||
}
|
||||
object?[]? parameters = singleFlipFlopNode.GetParameters(context, singleFlipFlopNode.MethodDetails); // 启动全局触发器时获取入参参数
|
||||
// 设置委托对象
|
||||
var func = md.ExplicitDatas.Length == 0 ?
|
||||
(Func<object, object, Task<IFlipflopContext>>)del :
|
||||
@@ -267,49 +271,13 @@ namespace Serein.NodeFlow
|
||||
{
|
||||
while (!FlipFlopCts.IsCancellationRequested)
|
||||
{
|
||||
object?[]? parameters = singleFlipFlopNode.GetParameters(context, singleFlipFlopNode.MethodDetails); // 启动全局触发器时获取入参参数
|
||||
IFlipflopContext flipflopContext = await func.Invoke(md.ActingInstance, parameters);// 首先开始等待触发器
|
||||
_ = GlobalFlipflopExecute(singleFlipFlopNode, context);
|
||||
IFlipflopContext flipflopContext = await func.Invoke(md.ActingInstance, parameters);// 开始等待全局触发器的触发
|
||||
var connectionType = flipflopContext.State.ToContentType();
|
||||
if (connectionType != ConnectionType.None)
|
||||
{
|
||||
await GlobalFlipflopExecute(context, singleFlipFlopNode, connectionType);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//while (!FlipFlopCts.IsCancellationRequested)
|
||||
//{
|
||||
// if (singleFlipFlopNode.NotExitPreviousNode() == false)
|
||||
// {
|
||||
// break;
|
||||
// }
|
||||
|
||||
// object?[]? parameters = singleFlipFlopNode.GetParameters(context, md);
|
||||
// if (md.ActingInstance == null)
|
||||
// {
|
||||
// md.ActingInstance = context.SereinIoc.GetOrRegisterInstantiate(md.ActingInstanceType);
|
||||
// }
|
||||
|
||||
// IFlipflopContext flipflopContext = await func.Invoke(md.ActingInstance, parameters);
|
||||
// ConnectionType connection = flipflopContext.State.ToContentType();
|
||||
|
||||
// if (connection != ConnectionType.None)
|
||||
// {
|
||||
// singleFlipFlopNode.NextOrientation = connection;
|
||||
// singleFlipFlopNode.FlowData = flipflopContext.Data;
|
||||
|
||||
// var tasks = singleFlipFlopNode.SuccessorNodes.Values
|
||||
// .SelectMany(nodeList => nodeList)
|
||||
// .Select(nextNode =>
|
||||
// {
|
||||
// var nextContext = new DynamicContext(SereinIOC, flowEnvironment);
|
||||
// nextNode.PreviousNode = singleFlipFlopNode;
|
||||
// return nextNode.StartExecution(nextContext); // 全局触发器收到信号,开始执行
|
||||
// }).ToArray();
|
||||
|
||||
// await Task.WhenAll(tasks);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// break;
|
||||
// }
|
||||
//}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -317,18 +285,25 @@ namespace Serein.NodeFlow
|
||||
}
|
||||
}
|
||||
|
||||
public async Task GlobalFlipflopExecute(SingleFlipflopNode singleFlipFlopNode, IDynamicContext context)
|
||||
/// <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)
|
||||
{
|
||||
if (FlipFlopCts.IsCancellationRequested)
|
||||
if (FlipFlopCts.IsCancellationRequested )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bool skip = true;
|
||||
var cts = context.SereinIoc.GetOrRegisterInstantiate<CancellationTokenSource>();
|
||||
Stack<NodeModelBase> stack = new Stack<NodeModelBase>();
|
||||
stack.Push(singleFlipFlopNode);
|
||||
|
||||
ConnectionType connectionType = ConnectionType.IsSucceed;
|
||||
|
||||
while (stack.Count > 0 && !cts.IsCancellationRequested) // 循环中直到栈为空才会退出循环
|
||||
{
|
||||
@@ -346,7 +321,7 @@ namespace Serein.NodeFlow
|
||||
for (int i = upstreamNodes.Count - 1; i >= 0; i--)
|
||||
{
|
||||
upstreamNodes[i].PreviousNode = currentNode;
|
||||
await upstreamNodes[i].StartExecution(context); // 执行上游分支
|
||||
await upstreamNodes[i].StartExecute(context); // 执行全局触发器的上游分支
|
||||
}
|
||||
|
||||
// 当前节点是已经触发了的全局触发器,所以跳过,难道每次都要判断一次?
|
||||
@@ -356,15 +331,8 @@ namespace Serein.NodeFlow
|
||||
}
|
||||
else
|
||||
{
|
||||
// 判断是否为触发器节点,如果是,则开始等待。
|
||||
if (currentNode.MethodDetails != null && currentNode.MethodDetails.MethodDynamicType == NodeType.Flipflop)
|
||||
{
|
||||
currentNode.FlowData = await currentNode.ExecuteAsync(context); // 流程中遇到了触发器
|
||||
}
|
||||
else
|
||||
{
|
||||
currentNode.FlowData = currentNode.Execute(context); // 流程中正常执行
|
||||
}
|
||||
currentNode.FlowData = await currentNode.ExecutingAsync(context);
|
||||
|
||||
if (currentNode.NextOrientation == ConnectionType.None)
|
||||
{
|
||||
break; // 不再执行
|
||||
|
||||
@@ -20,7 +20,8 @@ namespace Serein.NodeFlow.Model
|
||||
ActionNodes = actionNodes;
|
||||
}
|
||||
|
||||
public override object? Execute(IDynamicContext context)
|
||||
//public override async Task<object?> Executing(IDynamicContext context)
|
||||
public override Task<object?> ExecutingAsync(IDynamicContext context)
|
||||
{
|
||||
throw new NotImplementedException("动作区域暂未实现");
|
||||
}
|
||||
|
||||
@@ -24,7 +24,8 @@ namespace Serein.NodeFlow.Model
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public override object? Execute(IDynamicContext context)
|
||||
//public override object? Executing(IDynamicContext context)
|
||||
public override Task<object?> ExecutingAsync(IDynamicContext context)
|
||||
{
|
||||
// 条件区域中遍历每个条件节点
|
||||
foreach (SingleConditionNode? node in ConditionNodes)
|
||||
@@ -37,7 +38,7 @@ namespace Serein.NodeFlow.Model
|
||||
break;
|
||||
}
|
||||
}
|
||||
return PreviousNode?.FlowData;
|
||||
return Task.FromResult( PreviousNode?.FlowData);
|
||||
}
|
||||
|
||||
|
||||
@@ -45,7 +46,7 @@ namespace Serein.NodeFlow.Model
|
||||
{
|
||||
try
|
||||
{
|
||||
node.Execute(context);
|
||||
node.ExecutingAsync(context);
|
||||
return node.NextOrientation;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
@@ -28,7 +28,8 @@ namespace Serein.NodeFlow.Model
|
||||
public string Expression { get; set; }
|
||||
|
||||
|
||||
public override object? Execute(IDynamicContext context)
|
||||
//public override object? Executing(IDynamicContext context)
|
||||
public override Task<object?> ExecutingAsync(IDynamicContext context)
|
||||
{
|
||||
// 接收上一节点参数or自定义参数内容
|
||||
object? result;
|
||||
@@ -52,7 +53,7 @@ namespace Serein.NodeFlow.Model
|
||||
}
|
||||
|
||||
Console.WriteLine($"{result} {Expression} -> " + NextOrientation);
|
||||
return result;
|
||||
return Task.FromResult(result);
|
||||
}
|
||||
|
||||
internal override Parameterdata[] GetParameterdatas()
|
||||
|
||||
@@ -18,7 +18,8 @@ namespace Serein.NodeFlow.Model
|
||||
public string Expression { get; set; }
|
||||
|
||||
|
||||
public override object? Execute(IDynamicContext context)
|
||||
//public override async Task<object?> Executing(IDynamicContext context)
|
||||
public override Task<object?> ExecutingAsync(IDynamicContext context)
|
||||
{
|
||||
var data = PreviousNode?.FlowData;
|
||||
|
||||
@@ -37,13 +38,13 @@ namespace Serein.NodeFlow.Model
|
||||
}
|
||||
|
||||
NextOrientation = ConnectionType.IsSucceed;
|
||||
return result;
|
||||
return Task.FromResult(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
NextOrientation = ConnectionType.IsError;
|
||||
RuningException = ex;
|
||||
return PreviousNode?.FlowData;
|
||||
return Task.FromResult(PreviousNode?.FlowData);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,18 +1,67 @@
|
||||
using Serein.Library.Api;
|
||||
using Serein.Library.Entity;
|
||||
using Serein.Library.Enums;
|
||||
using Serein.Library.Ex;
|
||||
using Serein.NodeFlow.Base;
|
||||
using static Serein.Library.Utils.ChannelFlowInterrupt;
|
||||
|
||||
namespace Serein.NodeFlow.Model
|
||||
{
|
||||
|
||||
public class SingleFlipflopNode : NodeModelBase
|
||||
{
|
||||
public override object? Execute(IDynamicContext context)
|
||||
//public override async Task<object?> Executing(IDynamicContext context)
|
||||
//public override Task<object?> ExecutingAsync(IDynamicContext context)
|
||||
//{
|
||||
// NextOrientation = Library.Enums.ConnectionType.IsError;
|
||||
// RuningException = new FlipflopException ("无法以非await/async的形式调用触发器");
|
||||
// return null;
|
||||
//}
|
||||
|
||||
|
||||
public override async Task<object?> ExecutingAsync(IDynamicContext context)
|
||||
{
|
||||
NextOrientation = Library.Enums.ConnectionType.IsError;
|
||||
RuningException = new FlipflopException ("无法以非await/async的形式调用触发器");
|
||||
return null;
|
||||
#region 执行前中断
|
||||
if (TryCreateInterruptTask(context, this, out Task<CancelType>? task))
|
||||
{
|
||||
var cancelType = await task!;
|
||||
await Console.Out.WriteLineAsync($"[{this.MethodDetails.MethodName}]中断已{(cancelType == CancelType.Manual ? "手动取消" : "自动取消")},开始执行后继分支");
|
||||
}
|
||||
#endregion
|
||||
|
||||
MethodDetails md = MethodDetails;
|
||||
Delegate del = md.MethodDelegate;
|
||||
object instance = md.ActingInstance;
|
||||
var haveParameter = md.ExplicitDatas.Length >= 0;
|
||||
try
|
||||
{
|
||||
// 调用委托并获取结果
|
||||
Task<IFlipflopContext> flipflopTask = haveParameter switch
|
||||
{
|
||||
true => ((Func<object, object?[]?, Task<IFlipflopContext>>)del).Invoke(instance, GetParameters(context, md)), // 执行流程中的触发器方法时获取入参参数
|
||||
false => ((Func<object, Task<IFlipflopContext>>)del).Invoke(instance),
|
||||
};
|
||||
|
||||
IFlipflopContext flipflopContext = (await flipflopTask) ?? throw new FlipflopException("没有返回上下文");
|
||||
NextOrientation = flipflopContext.State.ToContentType();
|
||||
if(flipflopContext.TriggerData.Type == Library.NodeFlow.Tool.TriggerType.Overtime)
|
||||
{
|
||||
throw new FlipflopException("");
|
||||
}
|
||||
return flipflopContext.TriggerData.Value;
|
||||
}
|
||||
catch (FlipflopException ex)
|
||||
{
|
||||
NextOrientation = ConnectionType.None;
|
||||
RuningException = ex;
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
NextOrientation = ConnectionType.IsError;
|
||||
RuningException = ex;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
internal override Parameterdata[] GetParameterdatas()
|
||||
|
||||
Reference in New Issue
Block a user