优化了中断功能。

This commit is contained in:
fengjiayi
2024-09-22 17:37:32 +08:00
parent c930c870a6
commit b63a5f4c62
45 changed files with 4821 additions and 174 deletions

View File

@@ -78,10 +78,42 @@ namespace Serein.NodeFlow.Base
/// </summary>
public Exception RuningException { get; set; } = null;
/// <summary>
/// 当前传递数据(执行了节点对应的方法,才会存在值)
/// 控制FlowData在同一时间只会被同一个线程更改。
/// </summary>
protected object? FlowData { get; set; } = null;
private readonly ReaderWriterLockSlim _flowDataLock = new ReaderWriterLockSlim();
private object? _flowData;
/// <summary>
/// 当前传递数据(执行了节点对应的方法,才会存在值)。
/// </summary>
protected object? FlowData
{
get
{
_flowDataLock.EnterReadLock();
try
{
return _flowData;
}
finally
{
_flowDataLock.ExitReadLock();
}
}
set
{
_flowDataLock.EnterWriteLock();
try
{
_flowData = value;
}
finally
{
_flowDataLock.ExitWriteLock();
}
}
}
}

View File

@@ -27,15 +27,6 @@ namespace Serein.NodeFlow.Base
#region
public Action? CancelInterruptCallback;
/// <summary>
/// 中断节点
/// </summary>
public void Interrupt()
{
this.DebugSetting.InterruptClass = InterruptClass.Branch;
}
/// <summary>
/// 不再中断
@@ -43,9 +34,9 @@ namespace Serein.NodeFlow.Base
public void CancelInterrupt()
{
this.DebugSetting.InterruptClass = InterruptClass.None;
CancelInterruptCallback?.Invoke();
CancelInterruptCallback = null;
DebugSetting.CancelInterruptCallback?.Invoke();
}
#endregion
#region /
@@ -105,6 +96,17 @@ namespace Serein.NodeFlow.Base
/// <returns></returns>
public async Task StartExecute(IDynamicContext context)
{
if (DebugSetting.InterruptClass != InterruptClass.None) // 执行触发前
{
var cancelType = await this.DebugSetting.GetInterruptTask();
//if (cancelType == CancelType.Discard)
//{
// this.NextOrientation = ConnectionType.None;
// return;
//}
await Console.Out.WriteLineAsync($"[{this.MethodDetails.MethodName}]中断已{cancelType},开始执行后继分支");
return;
}
Stack<NodeModelBase> stack = new Stack<NodeModelBase>();
stack.Push(this);
@@ -126,27 +128,30 @@ namespace Serein.NodeFlow.Base
var upstreamNodes = currentNode.SuccessorNodes[ConnectionType.Upstream];
for (int i = upstreamNodes.Count - 1; i >= 0; i--)
{
if (upstreamNodes[i].DebugSetting.IsEnable) // 排除未启用的上游节点
// 筛选出启用的节点、未被中断的节点
if (upstreamNodes[i].DebugSetting.IsEnable && upstreamNodes[i].DebugSetting.InterruptClass == InterruptClass.None)
{
upstreamNodes[i].PreviousNode = currentNode;
var upNewFlowData = await upstreamNodes[i].ExecutingAsync(context); // 执行流程节点的上游分支
await FlowRefreshDataOrInterrupt(context, upstreamNodes[i], upNewFlowData); // 执行上游分支后刷新上游节点数据
await upstreamNodes[i].StartExecute(context); // 执行流程节点的上游分支
}
}
// 执行当前节点
var newFlowData = await currentNode.ExecutingAsync(context);
await FlowRefreshDataOrInterrupt(context, currentNode, newFlowData); // 执行当前节点后刷新数据
#endregion
#region
if (cts == null || cts.IsCancellationRequested || currentNode.NextOrientation == ConnectionType.None)
{
// 不再执行
break;
}
await RefreshFlowDataAndExpInterrupt(context, currentNode, newFlowData); // 执行当前节点后刷新数据
#endregion
#region
// 选择后继分支
var nextNodes = currentNode.SuccessorNodes[currentNode.NextOrientation];
@@ -154,8 +159,8 @@ namespace Serein.NodeFlow.Base
// 将下一个节点集合中的所有节点逆序推入栈中
for (int i = nextNodes.Count - 1; i >= 0; i--)
{
// 排除未启用的节点
if (nextNodes[i].DebugSetting.IsEnable)
// 筛选出启用的节点、未被中断的节点
if (nextNodes[i].DebugSetting.IsEnable && nextNodes[i].DebugSetting.InterruptClass == InterruptClass.None)
{
nextNodes[i].PreviousNode = currentNode;
stack.Push(nextNodes[i]);
@@ -165,6 +170,15 @@ namespace Serein.NodeFlow.Base
}
}
public void ThorwExitExecuting(CancellationTokenSource cts,NodeModelBase currentNode)
{
if (cts == null || cts.IsCancellationRequested || currentNode.NextOrientation == ConnectionType.None)
{
// 不再执行
throw new Exception("退出方法节点的执行");
}
}
/// <summary>
/// 执行节点对应的方法
/// </summary>
@@ -173,12 +187,16 @@ namespace Serein.NodeFlow.Base
public virtual async Task<object?> ExecutingAsync(IDynamicContext context)
{
#region
if (DebugSetting.InterruptClass != InterruptClass.None && TryCreateInterruptTask(context, this, out Task<CancelType>? task)) // 执行节点前检查中断
if (DebugSetting.InterruptClass != InterruptClass.None) // 执行触发前
{
string guid = this.Guid.ToString();
this.CancelInterruptCallback ??= () => context.FlowEnvironment.ChannelFlowInterrupt.TriggerSignal(guid);
var cancelType = await task!;
await Console.Out.WriteLineAsync($"[{this.MethodDetails.MethodName}]中断已{(cancelType == CancelType.Manual ? "" : "")},开始执行后继分支");
var cancelType = await this.DebugSetting.GetInterruptTask();
//if(cancelType == CancelType.Discard)
//{
// this.NextOrientation = ConnectionType.None;
// return null;
//}
await Console.Out.WriteLineAsync($"[{this.MethodDetails.MethodName}]中断已{cancelType},开始执行后继分支");
}
#endregion
@@ -213,7 +231,6 @@ namespace Serein.NodeFlow.Base
}
#region
public static object? Execution(Action<object> del, object instance)
{
@@ -326,64 +343,52 @@ namespace Serein.NodeFlow.Base
/// 更新节点数据,并检查监视表达式
/// </summary>
/// <param name="newData"></param>
public static async Task FlowRefreshDataOrInterrupt(IDynamicContext context , NodeModelBase nodeModel, object? newData = null)
public static async Task RefreshFlowDataAndExpInterrupt(IDynamicContext context , NodeModelBase nodeModel, object? newData = null)
{
string guid = nodeModel.Guid;
if (newData is not null)
// 检查是否存在监视表达式
if (newData is not null && nodeModel.DebugSetting.InterruptExpressions.Count > 0)
{
// 判断是否存在表达式
bool isInterrupt = false;
// 判断监视表达式
for (int i = 0; i < nodeModel.DebugSetting.InterruptExpression.Count && !isInterrupt; i++)
// 表达式环境下判断是否需要执行中断
bool isExpInterrupt = false;
// 判断执行监视表达式,直到为 true 时退出
for (int i = 0; i < nodeModel.DebugSetting.InterruptExpressions.Count && !isExpInterrupt; i++)
{
string? exp = nodeModel.DebugSetting.InterruptExpression[i];
isInterrupt = SereinConditionParser.To(newData, exp);
string? exp = nodeModel.DebugSetting.InterruptExpressions[i];
isExpInterrupt = SereinConditionParser.To(newData, exp);
}
if (isInterrupt) // 触发中断
if (isExpInterrupt) // 触发中断
{
nodeModel.Interrupt();
if(TryCreateInterruptTask(context, nodeModel, out Task<CancelType>? task))
InterruptClass interruptClass = InterruptClass.Branch; // 分支中断
if (context.FlowEnvironment.SetNodeInterrupt(nodeModel.Guid, interruptClass))
{
nodeModel.CancelInterruptCallback ??= () => context.FlowEnvironment.ChannelFlowInterrupt.TriggerSignal(guid);
var cancelType = await task!;
await Console.Out.WriteLineAsync($"[{nodeModel.MethodDetails.MethodName}]中断已{(cancelType == CancelType.Manual ? "" : "")},开始执行后继分支");
var cancelType = await nodeModel.DebugSetting.GetInterruptTask();
//if (cancelType == CancelType.Discard)
//{
// nodeModel.NextOrientation = ConnectionType.None;
// return;
//}
await Console.Out.WriteLineAsync($"[{nodeModel.MethodDetails.MethodName}]中断已{cancelType},开始执行后继分支");
}
}
}
nodeModel.FlowData = newData;
//else if (nodeModel.DebugSetting.InterruptClass != InterruptClass.None)
//{
// var cancelType = await nodeModel.DebugSetting.InterruptTask;
// await Console.Out.WriteLineAsync($"[{nodeModel.MethodDetails.MethodName}]中断已{(cancelType == CancelType.Manual ? "手动取消" : "自动取消")},开始执行后继分支");
//}
nodeModel.FlowData = newData; // 替换数据
// 节点是否监视了数据,如果是,调用环境接口触发其相关事件。
if (nodeModel.DebugSetting.IsMonitorFlowData)
{
context.FlowEnvironment.FlowDataUpdateNotification(guid, newData);
context.FlowEnvironment.FlowDataNotification(guid, newData);
}
}
public static bool TryCreateInterruptTask(IDynamicContext context, NodeModelBase currentNode, out Task<CancelType>? task)
{
bool haveTask;
Console.WriteLine($"[{currentNode.MethodDetails.MethodName}]在当前分支中断");
if (currentNode.DebugSetting.InterruptClass == InterruptClass.None)
{
haveTask = false;
task = null;
}
else if (currentNode.DebugSetting.InterruptClass == InterruptClass.Branch) // 中断当前分支
{
haveTask = true;
task = context.FlowEnvironment.ChannelFlowInterrupt.CreateChannelWithTimeoutAsync(currentNode.Guid, TimeSpan.FromSeconds(60 * 30)); // 中断30分钟
}
else
{
haveTask = false;
task = null;
}
return haveTask;
}
/// <summary>
/// 释放对象
/// </summary>

View File

@@ -11,6 +11,7 @@ using Serein.NodeFlow.Tool;
using System.Collections.Concurrent;
using System.Reflection;
using System.Xml.Linq;
using static Serein.Library.Utils.ChannelFlowInterrupt;
using static Serein.NodeFlow.FlowStarter;
namespace Serein.NodeFlow
@@ -57,7 +58,7 @@ namespace Serein.NodeFlow
/// <summary>
/// 节点的命名空间
/// </summary>
public const string NodeSpaceName = $"{nameof(Serein)}.{nameof(Serein.NodeFlow)}.{nameof(Serein.NodeFlow.Model)}";
public const string SpaceName = $"{nameof(Serein)}.{nameof(Serein.NodeFlow)}.{nameof(Serein.NodeFlow.Model)}";
#region
/// <summary>
@@ -112,16 +113,21 @@ namespace Serein.NodeFlow
#endregion
/// <summary>
/// 环境名称
/// </summary>
public string EnvName { get; set; } = SpaceName;
/// <summary>
/// 是否全局中断
/// </summary>
public bool IsGlobalInterrupt { get; set; }
/// <summary>
/// 流程中断器
/// </summary>
public ChannelFlowInterrupt ChannelFlowInterrupt { get; set; }
/// <summary>
/// 是否全局中断
/// </summary>
public bool IsGlobalInterrupt { get; set; }
/// <summary>
/// 存储加载的程序集路径
@@ -466,7 +472,8 @@ namespace Serein.NodeFlow
/// <exception cref="NotImplementedException"></exception>
public void RemoteNode(string nodeGuid)
{
NodeModelBase remoteNode = GuidToModel(nodeGuid);
var remoteNode = GuidToModel(nodeGuid);
if (remoteNode is null) return;
if (remoteNode.IsStart)
{
return;
@@ -522,8 +529,10 @@ namespace Serein.NodeFlow
public void ConnectNode(string fromNodeGuid, string toNodeGuid, ConnectionType connectionType)
{
// 获取起始节点与目标节点
NodeModelBase fromNode = GuidToModel(fromNodeGuid);
NodeModelBase toNode = GuidToModel(toNodeGuid);
var fromNode = GuidToModel(fromNodeGuid);
var toNode = GuidToModel(toNodeGuid);
if (fromNode is null) return;
if (toNode is null) return;
// 开始连接
ConnectNode(fromNode, toNode, connectionType); // 外部调用连接方法
@@ -539,8 +548,10 @@ namespace Serein.NodeFlow
public void RemoteConnect(string fromNodeGuid, string toNodeGuid, ConnectionType connectionType)
{
// 获取起始节点与目标节点
NodeModelBase fromNode = GuidToModel(fromNodeGuid);
NodeModelBase toNode = GuidToModel(toNodeGuid);
var fromNode = GuidToModel(fromNodeGuid);
var toNode = GuidToModel(toNodeGuid);
if (fromNode is null) return;
if (toNode is null) return;
RemoteConnect(fromNode, toNode, connectionType);
//fromNode.SuccessorNodes[connectionType].Remove(toNode);
@@ -606,29 +617,9 @@ namespace Serein.NodeFlow
/// <param name="newNodeGuid"></param>
public void SetStartNode(string newNodeGuid)
{
NodeModelBase newStartNodeModel = GuidToModel(newNodeGuid);
var newStartNodeModel = GuidToModel(newNodeGuid);
if (newStartNodeModel is null) return;
SetStartNode(newStartNodeModel);
//if (string.IsNullOrEmpty(newNodeGuid))
//{
// return;
//}
//if (Nodes.TryGetValue(newNodeGuid, out NodeModelBase? newStartNodeModel))
//{
// if (newStartNodeModel != null)
// {
// SetStartNode(newStartNodeModel);
// //var oldNodeGuid = "";
// //if(StartNode != null)
// //{
// // oldNodeGuid = StartNode.Guid;
// // StartNode.IsStart = false;
// //}
// //newStartNodeModel.IsStart = true;
// //StartNode = newStartNodeModel;
// //OnStartNodeChange?.Invoke(new StartNodeChangeEventArgs(oldNodeGuid, newNodeGuid));
// }
//}
}
/// <summary>
@@ -637,22 +628,63 @@ namespace Serein.NodeFlow
/// <param name="nodeGuid">被中断的目标节点Guid</param>
/// <param name="interruptClass">中断级别</param>
/// <returns>操作是否成功</returns>
public bool NodeInterruptChange(string nodeGuid, InterruptClass interruptClass)
public bool SetNodeInterrupt(string nodeGuid, InterruptClass interruptClass)
{
NodeModelBase nodeModel = GuidToModel(nodeGuid);
var nodeModel = GuidToModel(nodeGuid);
if (nodeModel is null) return false;
if (interruptClass == InterruptClass.None)
{
nodeModel.CancelInterrupt();
}
else if (interruptClass == InterruptClass.Branch)
{
nodeModel.DebugSetting.CancelInterruptCallback?.Invoke();
nodeModel.DebugSetting.GetInterruptTask = () =>
{
//ChannelFlowInterrupt.EnableDiscardMode(nodeGuid,true);
return ChannelFlowInterrupt.GetOrCreateChannelAsync(nodeGuid);
};
nodeModel.DebugSetting.CancelInterruptCallback = () =>
{
ChannelFlowInterrupt.TriggerSignal(nodeGuid);
//ChannelFlowInterrupt.EnableDiscardMode(nodeGuid, false);
};
}
else if (interruptClass == InterruptClass.Global) // 全局……做不了omg
{
return false;
}
nodeModel.DebugSetting.InterruptClass = interruptClass;
OnNodeInterruptStateChange.Invoke(new NodeInterruptStateChangeEventArgs(nodeGuid, interruptClass));
return true;
}
public bool AddInterruptExpression(string nodeGuid, string expression)
{
var nodeModel = GuidToModel(nodeGuid);
if (nodeModel is null) return false;
if (nodeModel.DebugSetting.InterruptExpressions.Contains(expression))
{
Console.WriteLine("表达式已存在");
return false;
}
else
{
nodeModel.DebugSetting.InterruptExpressions.Add(expression);
return true;
}
}
/// <summary>
/// 监视节点的数据
/// </summary>
/// <param name="nodeGuid">需要监视的节点Guid</param>
public void SetNodeFLowDataMonitorState(string nodeGuid, bool isMonitor)
{
NodeModelBase nodeModel = GuidToModel(nodeGuid);
var nodeModel = GuidToModel(nodeGuid);
if (nodeModel is null) return;
nodeModel.DebugSetting.IsMonitorFlowData = isMonitor;
}
@@ -660,29 +692,51 @@ namespace Serein.NodeFlow
/// 节点数据更新通知
/// </summary>
/// <param name="nodeGuid"></param>
public void FlowDataUpdateNotification(string nodeGuid, object flowData)
public void FlowDataNotification(string nodeGuid, object flowData)
{
OnMonitorObjectChange?.Invoke(new MonitorObjectEventArgs(nodeGuid, flowData));
}
public Task<CancelType> GetOrCreateGlobalInterruptAsync()
{
IsGlobalInterrupt = true;
return ChannelFlowInterrupt.GetOrCreateChannelAsync(this.EnvName);
}
/// <summary>
/// Guid 转 NodeModel
/// </summary>
/// <param name="nodeGuid">节点Guid</param>
/// <returns>节点Model</returns>
/// <exception cref="ArgumentNullException">无法获取节点、Guid/节点为null时报错</exception>
private NodeModelBase GuidToModel(string nodeGuid)
private NodeModelBase? GuidToModel(string nodeGuid)
{
if (string.IsNullOrEmpty(nodeGuid))
{
throw new ArgumentNullException("not contains - Guid没有对应节点:" + (nodeGuid));
//throw new ArgumentNullException("not contains - Guid没有对应节点:" + (nodeGuid));
return null;
}
if (!Nodes.TryGetValue(nodeGuid, out NodeModelBase? nodeModel) || nodeModel is null)
{
throw new ArgumentNullException("null - Guid存在对应节点,但节点为null:" + (nodeGuid));
//throw new ArgumentNullException("null - Guid存在对应节点,但节点为null:" + (nodeGuid));
return null;
}
return nodeModel;
}
#endregion
#region

View File

@@ -303,20 +303,22 @@ namespace Serein.NodeFlow
while (!_flipFlopCts.IsCancellationRequested)
{
var newFlowData = await singleFlipFlopNode.ExecutingAsync(context);
await NodeModelBase.FlowRefreshDataOrInterrupt(context, singleFlipFlopNode, newFlowData); // 全局触发器触发后刷新该触发器的节点数据
var newFlowData = await singleFlipFlopNode.ExecutingAsync(context); // 获取触发器等待Task
await NodeModelBase.RefreshFlowDataAndExpInterrupt(context, singleFlipFlopNode, newFlowData); // 全局触发器触发后刷新该触发器的节点数据
if (singleFlipFlopNode.NextOrientation != ConnectionType.None)
{
var nextNodes = singleFlipFlopNode.SuccessorNodes[singleFlipFlopNode.NextOrientation];
for (int i = nextNodes.Count - 1; i >= 0 && !_flipFlopCts.IsCancellationRequested; i--)
{
if (nextNodes[i].DebugSetting.IsEnable) // 排除未启用的后继节点
// 筛选出启用的节点、未被中断的节点
if (nextNodes[i].DebugSetting.IsEnable && nextNodes[i].DebugSetting.InterruptClass == InterruptClass.None)
{
nextNodes[i].PreviousNode = singleFlipFlopNode;
await nextNodes[i].StartExecute(context); // 启动执行触发器后继分支的节点
}
}
}
}
}
catch (Exception ex)

View File

@@ -22,12 +22,16 @@ namespace Serein.NodeFlow.Model
public override async Task<object?> ExecutingAsync(IDynamicContext context)
{
#region
if (DebugSetting.InterruptClass != InterruptClass.None && TryCreateInterruptTask(context, this, out Task<CancelType>? task)) // 执行触发前
if (DebugSetting.InterruptClass != InterruptClass.None) // 执行触发前
{
string guid = this.Guid.ToString();
this.CancelInterruptCallback ??= () => context.FlowEnvironment.ChannelFlowInterrupt.TriggerSignal(guid);
var cancelType = await task!;
await Console.Out.WriteLineAsync($"[{this.MethodDetails.MethodName}]中断已{(cancelType == CancelType.Manual ? "" : "")},开始执行后继分支");
var cancelType = await this.DebugSetting.GetInterruptTask();
//if (cancelType == CancelType.Discard)
//{
// this.NextOrientation = ConnectionType.None;
// return null;
//}
await Console.Out.WriteLineAsync($"[{this.MethodDetails.MethodName}]中断已{cancelType},开始执行后继分支");
}
#endregion