mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-03-03 00:00:49 +08:00
重写了Emit构造委托的执行
This commit is contained in:
@@ -204,11 +204,7 @@ namespace Serein.NodeFlow.Base
|
||||
md.ActingInstance ??= context.Env.IOC.Get(md.ActingInstanceType);
|
||||
object instance = md.ActingInstance;
|
||||
|
||||
//bool haveParameter = md.ExplicitDatas.Length > 0;
|
||||
//bool haveResult = md.ReturnType != typeof(void);
|
||||
// Type? taskResult = null;
|
||||
//bool isTask = md.ReturnType is not null && MethodDetailsHelper.IsGenericTask(md.ReturnType, out taskResult);
|
||||
//bool isTaskHaveResult = taskResult is not null;
|
||||
|
||||
object? result = null;
|
||||
|
||||
//Console.WriteLine($"(isTask, isTaskHaveResult):{(isTask, isTaskHaveResult)}");
|
||||
@@ -217,49 +213,7 @@ namespace Serein.NodeFlow.Base
|
||||
// Action/Func([方法作用的实例],[可能的参数值],[可能的返回值])
|
||||
|
||||
object?[]? args = GetParameters(context, this, md);
|
||||
var delType = dd.EmitMethodType;
|
||||
var del = dd.EmitDelegate;
|
||||
if (delType == EmitHelper.EmitMethodType.HasResultTask && del is Func<object, object?[]?, Task<object?>> hasResultTask)
|
||||
{
|
||||
result = await hasResultTask(instance, args);
|
||||
}
|
||||
else if (delType == EmitHelper.EmitMethodType.Task && del is Func<object, object?[]?, Task> task)
|
||||
{
|
||||
await task.Invoke(instance, args);
|
||||
result = null;
|
||||
}
|
||||
else if (delType == EmitHelper.EmitMethodType.Func && del is Func<object, object?[]?, object?> func)
|
||||
{
|
||||
result = func.Invoke(instance, args);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotImplementedException("构造委托无法正确调用");
|
||||
}
|
||||
|
||||
//if (isTask)
|
||||
//{
|
||||
// // 异步方法(因为返回了Task,所以排除Action<>委托的可能)
|
||||
// result = (haveParameter, isTaskHaveResult) switch
|
||||
// {
|
||||
// (false, false) => await ExecutionAsync((Func<object, Task>)del, instance), // 调用节点方法,返回方法传回类型
|
||||
// (true, false) => await ExecutionAsync((Func<object, object?[]?, Task>)del, instance, parameters), // 调用节点方法,获取入参参数,返回方法返回类型
|
||||
// (false, true) => await ExecutionAsync((Func<object, Task<object?>>)del, instance), // 调用节点方法,返回方法传回类型
|
||||
// (true, true) => await ExecutionAsync((Func<object, object?[]?, Task<object?>>)del, instance, parameters), // 调用节点方法,获取入参参数,返回方法返回类型
|
||||
// };
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// // 非异步方法
|
||||
// result = (haveParameter, haveResult) switch
|
||||
// {
|
||||
// (false, false) => Execution((Action<object>)del, instance), // 调用节点方法,返回null
|
||||
// (true, false) => Execution((Action<object, object?[]?>)del, instance, parameters), // 调用节点方法,返回null
|
||||
// (false, true) => Execution((Func<object, object?>)del, instance), // 调用节点方法,返回方法传回类型
|
||||
// (true, true) => Execution((Func<object, object?[]?, object?>)del, instance, parameters), // 调用节点方法,获取入参参数,返回方法返回类型
|
||||
// };
|
||||
//}
|
||||
|
||||
result = await dd.Invoke(md.ActingInstance, args);
|
||||
NextOrientation = ConnectionType.IsSucceed;
|
||||
return result;
|
||||
}
|
||||
@@ -273,47 +227,6 @@ namespace Serein.NodeFlow.Base
|
||||
}
|
||||
|
||||
|
||||
#region 节点转换的委托类型
|
||||
public static object? Execution(Action<object> del, object instance)
|
||||
{
|
||||
del.Invoke(instance);
|
||||
return null;
|
||||
}
|
||||
public static object? Execution(Action<object, object?[]?> del, object instance, object?[]? parameters)
|
||||
{
|
||||
del.Invoke(instance, parameters);
|
||||
return null;
|
||||
}
|
||||
public static object? Execution(Func<object, object?> del, object instance)
|
||||
{
|
||||
return del.Invoke(instance);
|
||||
}
|
||||
public static object? Execution(Func<object, object?[]?, object?> del, object instance, object?[]? parameters)
|
||||
{
|
||||
return del.Invoke(instance, parameters);
|
||||
}
|
||||
|
||||
|
||||
public static async Task<object?> ExecutionAsync(Func<object, Task> del, object instance)
|
||||
{
|
||||
await del.Invoke(instance);
|
||||
return null;
|
||||
}
|
||||
public static async Task<object?> ExecutionAsync(Func<object, object?[]?, Task> del, object instance, object?[]? parameters)
|
||||
{
|
||||
await del.Invoke(instance, parameters);
|
||||
return null;
|
||||
}
|
||||
public static async Task<object?> ExecutionAsync(Func<object, Task<object?>> del, object instance)
|
||||
{
|
||||
return await del.Invoke(instance);
|
||||
}
|
||||
public static async Task<object?> ExecutionAsync(Func<object, object?[]?, Task<object?>> del, object instance, object?[]? parameters)
|
||||
{
|
||||
return await del.Invoke(instance, parameters);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取对应的参数数组
|
||||
|
||||
@@ -984,10 +984,10 @@ namespace Serein.NodeFlow
|
||||
{
|
||||
continue;
|
||||
}
|
||||
var methods = MethodDetailsHelper.GetMethodsToProcess(type);
|
||||
var methods = NodeMethodDetailsHelper.GetMethodsToProcess(type);
|
||||
foreach(var method in methods)
|
||||
{
|
||||
(var md, var del) = MethodDetailsHelper.CreateMethodDetails(type, method, assemblyName);
|
||||
(var md, var del) = NodeMethodDetailsHelper.CreateMethodDetails(type, method, assemblyName);
|
||||
if(md is null || del is null)
|
||||
{
|
||||
Console.WriteLine($"无法加载方法信息:{assemblyName}-{type}-{method}");
|
||||
|
||||
@@ -73,7 +73,7 @@ namespace Serein.NodeFlow
|
||||
/// <summary>
|
||||
/// 结束运行时需要执行的方法
|
||||
/// </summary>
|
||||
private Action ExitAction { get; set; } = null;
|
||||
private Func<Task> ExitAction { get; set; } = null;
|
||||
/// <summary>
|
||||
/// 运行的上下文
|
||||
/// </summary>
|
||||
@@ -136,7 +136,6 @@ namespace Serein.NodeFlow
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region 选择运行环境的上下文
|
||||
|
||||
// 判断使用哪一种流程上下文
|
||||
@@ -234,7 +233,8 @@ namespace Serein.NodeFlow
|
||||
{
|
||||
throw new Exception("不存在对应委托");
|
||||
}
|
||||
((Func<object, object[], object>)dd.EmitDelegate).Invoke(md.ActingInstance, [Context]);
|
||||
await dd.Invoke(md.ActingInstance, [Context]);
|
||||
//((Func<object, object[], object>)dd.EmitDelegate).Invoke(md.ActingInstance, [Context]);
|
||||
}
|
||||
Context.Env.IOC.Build(); // 绑定初始化时注册的类型
|
||||
|
||||
@@ -254,14 +254,15 @@ namespace Serein.NodeFlow
|
||||
{
|
||||
throw new Exception("不存在对应委托");
|
||||
}
|
||||
await dd.Invoke(md.ActingInstance, [Context]);
|
||||
//((Action<object, object?[]?>)del).Invoke(md.ActingInstance, [Context]);
|
||||
((Func<object, object[], object>)dd.EmitDelegate).Invoke(md.ActingInstance, [Context]);
|
||||
//((Func<object, object[], object>)dd.EmitDelegate).Invoke(md.ActingInstance, [Context]);
|
||||
}
|
||||
Context.Env.IOC.Build(); // 预防有人在加载时才注册类型,再绑定一次
|
||||
#endregion
|
||||
|
||||
#region 设置流程退出时的回调函数
|
||||
ExitAction = () =>
|
||||
ExitAction = async () =>
|
||||
{
|
||||
env.IOC.Run<WebApiServer>(web => {
|
||||
web?.Stop();
|
||||
@@ -273,7 +274,8 @@ namespace Serein.NodeFlow
|
||||
{
|
||||
throw new Exception("不存在对应委托");
|
||||
}
|
||||
((Func<object, object[], object>)dd.EmitDelegate).Invoke(md.ActingInstance, [Context]);
|
||||
await dd.Invoke(md.ActingInstance, [Context]);
|
||||
//((Func<object, object[], object>)dd.EmitDelegate).Invoke(md.ActingInstance, [Context]);
|
||||
}
|
||||
|
||||
TerminateAllGlobalFlipflop();
|
||||
|
||||
@@ -24,13 +24,12 @@ namespace Serein.NodeFlow.Model
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
//public override object? Executing(IDynamicContext context)
|
||||
public override Task<object?> ExecutingAsync(IDynamicContext context)
|
||||
public override async Task<object?> ExecutingAsync(IDynamicContext context)
|
||||
{
|
||||
// 条件区域中遍历每个条件节点
|
||||
foreach (SingleConditionNode? node in ConditionNodes)
|
||||
{
|
||||
var state = Judge(context, node);
|
||||
var state = await JudgeAsync(context, node);
|
||||
NextOrientation = state; // 每次判读完成后,设置区域后继方向为判断结果
|
||||
if (state != ConnectionType.IsSucceed)
|
||||
{
|
||||
@@ -42,11 +41,11 @@ namespace Serein.NodeFlow.Model
|
||||
}
|
||||
|
||||
|
||||
private ConnectionType Judge(IDynamicContext context, SingleConditionNode node)
|
||||
private async Task<ConnectionType> JudgeAsync(IDynamicContext context, SingleConditionNode node)
|
||||
{
|
||||
try
|
||||
{
|
||||
node.ExecutingAsync(context);
|
||||
await node.ExecutingAsync(context);
|
||||
return node.NextOrientation;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
@@ -40,32 +40,46 @@ namespace Serein.NodeFlow.Model
|
||||
object instance = md.ActingInstance;
|
||||
try
|
||||
{
|
||||
Task<IFlipflopContext> flipflopTask;
|
||||
var args = GetParameters(context, this, md);
|
||||
var delType = dd.EmitMethodType;
|
||||
var del = dd.EmitDelegate;
|
||||
if (delType == EmitHelper.EmitMethodType.HasResultTask && del is Func<object, object?[]?, Task<object>> hasResultTask)
|
||||
var result = await dd.Invoke(md.ActingInstance, args);
|
||||
if (result is IFlipflopContext flipflopContext)
|
||||
{
|
||||
var flipflopTaskObj = await hasResultTask(instance, args);
|
||||
if(flipflopTaskObj is IFlipflopContext flipflopContext)
|
||||
NextOrientation = flipflopContext.State.ToContentType();
|
||||
if (flipflopContext.TriggerData is null || flipflopContext.TriggerData.Type == Library.NodeFlow.Tool.TriggerType.Overtime)
|
||||
{
|
||||
NextOrientation = flipflopContext.State.ToContentType();
|
||||
if (flipflopContext.TriggerData is null || flipflopContext.TriggerData.Type == Library.NodeFlow.Tool.TriggerType.Overtime)
|
||||
{
|
||||
throw new FlipflopException(base.MethodDetails.MethodName + "触发器超时触发。Guid" + base.Guid);
|
||||
}
|
||||
return flipflopContext.TriggerData.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new FlipflopException("触发器节点返回了非预期的类型", true, FlipflopException.CancelClass.Flow);
|
||||
throw new FlipflopException(base.MethodDetails.MethodName + "触发器超时触发。Guid" + base.Guid);
|
||||
}
|
||||
return flipflopContext.TriggerData.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new FlipflopException("触发器节点构造了非预期的委托", true, FlipflopException.CancelClass.Flow);
|
||||
throw new FlipflopException("触发器节点返回了非预期的类型", true, FlipflopException.CancelClass.Flow);
|
||||
}
|
||||
|
||||
// Task<IFlipflopContext> flipflopTask;
|
||||
//var delType = dd.EmitMethodType;
|
||||
//var del = dd.EmitDelegate;
|
||||
//if (delType == EmitHelper.EmitMethodType.HasResultTask && del is Func<object, object?[]?, Task<object>> hasResultTask)
|
||||
//{
|
||||
// var flipflopTaskObj = await hasResultTask(instance, args);
|
||||
// if(flipflopTaskObj is IFlipflopContext flipflopContext)
|
||||
// {
|
||||
// NextOrientation = flipflopContext.State.ToContentType();
|
||||
// if (flipflopContext.TriggerData is null || flipflopContext.TriggerData.Type == Library.NodeFlow.Tool.TriggerType.Overtime)
|
||||
// {
|
||||
// throw new FlipflopException(base.MethodDetails.MethodName + "触发器超时触发。Guid" + base.Guid);
|
||||
// }
|
||||
// return flipflopContext.TriggerData.Value;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// throw new FlipflopException("触发器节点返回了非预期的类型", true, FlipflopException.CancelClass.Flow);
|
||||
// }
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// throw new FlipflopException("触发器节点构造了非预期的委托", true, FlipflopException.CancelClass.Flow);
|
||||
//}
|
||||
|
||||
}
|
||||
catch (FlipflopException ex)
|
||||
{
|
||||
|
||||
@@ -11,7 +11,7 @@ using System.Text.RegularExpressions;
|
||||
|
||||
namespace Serein.NodeFlow.Tool;
|
||||
|
||||
public static class MethodDetailsHelper
|
||||
public static class NodeMethodDetailsHelper
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
@@ -57,7 +57,7 @@ public static class MethodDetailsHelper
|
||||
public static (MethodDetails?, DelegateDetails?) CreateMethodDetails(Type type, MethodInfo method, string assemblyName)
|
||||
{
|
||||
var attribute = method.GetCustomAttribute<NodeActionAttribute>();
|
||||
if(attribute is null)
|
||||
if(attribute is null || attribute.Scan == false)
|
||||
{
|
||||
return (null, null);
|
||||
}
|
||||
Reference in New Issue
Block a user