重构了运行逻辑。上下文使用对象池封装,节点方法调用时间传递CancellationTokenSource用来中止任务

This commit is contained in:
fengjiayi
2025-03-20 22:54:10 +08:00
parent 2168c5ec66
commit 9941f24c5d
27 changed files with 830 additions and 621 deletions

View File

@@ -36,14 +36,18 @@ namespace Serein.NodeFlow.Model
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override async Task<object?> ExecutingAsync(IDynamicContext context)
public override async Task<object?> ExecutingAsync(IDynamicContext context, CancellationToken token)
{
try
{
// 条件区域中遍历每个条件节点
foreach (SingleConditionNode? node in ConditionNodes)
{
var state = await node.ExecutingAsync(context);
if (token.IsCancellationRequested)
{
return null;
}
var state = await node.ExecutingAsync(context, token);
if (context.NextOrientation != ConnectionInvokeType.IsSucceed)
{
// 如果条件不通过,立刻推出循环

View File

@@ -110,8 +110,9 @@ namespace Serein.NodeFlow.Model
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override async Task<object?> ExecutingAsync(IDynamicContext context)
public override async Task<object?> ExecutingAsync(IDynamicContext context, CancellationToken token)
{
if (token.IsCancellationRequested) return null;
// 接收上一节点参数or自定义参数内容
object? parameter;
object? result = null;

View File

@@ -1,4 +1,5 @@
using Serein.Library;
using Newtonsoft.Json.Linq;
using Serein.Library;
using Serein.Library.Api;
using Serein.Library.Utils;
using Serein.Library.Utils.SereinExpression;
@@ -91,8 +92,10 @@ namespace Serein.NodeFlow.Model
}
public override async Task<object?> ExecutingAsync(IDynamicContext context)
public override async Task<object?> ExecutingAsync(IDynamicContext context, CancellationToken token)
{
if(token.IsCancellationRequested) return null;
object? parameter = null;// context.TransmissionData(this); // 表达式节点使用上一节点数据
var pd = MethodDetails.ParameterDetailss[0];

View File

@@ -1,6 +1,7 @@
using Serein.Library.Api;
using Serein.Library;
using Serein.Library.Utils;
using System;
namespace Serein.NodeFlow.Model
{
@@ -21,7 +22,7 @@ namespace Serein.NodeFlow.Model
/// <param name="context"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public override async Task<object?> ExecutingAsync(IDynamicContext context)
public override async Task<object?> ExecutingAsync(IDynamicContext context, CancellationToken token)
{
#region
if (DebugSetting.IsInterrupt) // 执行触发前
@@ -37,13 +38,18 @@ namespace Serein.NodeFlow.Model
{
throw new Exception("不存在对应委托");
}
object instance = md.ActingInstance;
var args = await GetParametersAsync(context);
var instance = context.Env.IOC.Get(md.ActingInstanceType);
await dd.InvokeAsync(instance, [context]);
var args = await GetParametersAsync(context, token);
// 因为这里会返回不确定的泛型 IFlipflopContext<TRsult>
// 而我们只需要获取到 State 和 Value返回的数据
// 所以使用 dynamic 类型接收
dynamic dynamicFlipflopContext = await dd.InvokeAsync(md.ActingInstance, args);
if (token.IsCancellationRequested)
{
return null;
}
dynamic dynamicFlipflopContext = await dd.InvokeAsync(instance, args);
FlipflopStateType flipflopStateType = dynamicFlipflopContext.State;
context.NextOrientation = flipflopStateType.ToContentType();

View File

@@ -115,8 +115,9 @@ namespace Serein.NodeFlow.Model
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override async Task<object> ExecutingAsync(IDynamicContext context)
public override async Task<object?> ExecutingAsync(IDynamicContext context, CancellationToken token)
{
if (token.IsCancellationRequested) return null;
if (string.IsNullOrEmpty(KeyName))
{
context.NextOrientation = ConnectionInvokeType.IsError;

View File

@@ -165,13 +165,15 @@ namespace Serein.NodeFlow.Model
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override async Task<object?> ExecutingAsync(IDynamicContext context)
public override async Task<object?> ExecutingAsync(IDynamicContext context, CancellationToken token)
{
var @params = await GetParametersAsync(context);
if(token.IsCancellationRequested) return null;
var @params = await GetParametersAsync(context, token);
if(token.IsCancellationRequested) return null;
//context.AddOrUpdate($"{context.Guid}_{this.Guid}_Params", @params[0]); // 后面再改
ReloadScript();// 每次都重新解析
ReloadScript();// 每次都重新解析
IScriptInvokeContext scriptContext = new ScriptInvokeContext(context);
@@ -193,6 +195,9 @@ namespace Serein.NodeFlow.Model
var envEvent = (IFlowEnvironmentEvent)context.Env;
envEvent.OnFlowRunComplete += onFlowStop; // 防止运行后台流程
if (token.IsCancellationRequested) return null;
var result = await ScriptInterpreter.InterpretAsync(scriptContext, mainNode); // 从入口节点执行
envEvent.OnFlowRunComplete -= onFlowStop;
//SereinEnv.WriteLine(InfoType.INFO, "FlowContext Guid : " + context.Guid);

View File

@@ -15,12 +15,13 @@ namespace Serein.NodeFlow.Model
{
}
public override async Task<object> ExecutingAsync(IDynamicContext context)
public override async Task<object?> ExecutingAsync(IDynamicContext context, CancellationToken token)
{
if (token.IsCancellationRequested) return null;
if(Adapter is null)
{
var result = await base.ExecutingAsync(context);
var result = await base.ExecutingAsync(context, token);
if (result is IEmbeddedContent adapter)
{
this.Adapter = adapter;
@@ -39,7 +40,7 @@ namespace Serein.NodeFlow.Model
iflowContorl.OnExecuting(data);
}
return Task.FromResult<object?>(null);
return null;
}
}
}